FIX : keyboard bind issue, can not be setting of analog key.

ADD: set the dead zone to the axis of the DirectInput.
This commit is contained in:
nachume 2013-05-19 18:12:22 +09:00
parent 1947718c43
commit d24e5be256
5 changed files with 60 additions and 29 deletions

View file

@ -36,6 +36,8 @@ inline UINT* ControlMapping::GetDeviceButtonsMap(UINT curDevice)
return pButtonsMap + buttonsCount; return pButtonsMap + buttonsCount;
case CONTROLS_XINPUT_INDEX: case CONTROLS_XINPUT_INDEX:
return pButtonsMap + (buttonsCount * 2); return pButtonsMap + (buttonsCount * 2);
case CONTROLS_KEYBOARD_ANALOG_INDEX:
return pButtonsMap + (buttonsCount * 3);
} }
return NULL; return NULL;
} }
@ -67,17 +69,16 @@ ControlMapping::ControlMapping(UINT nButtons) :
dinput = std::shared_ptr<DinputDevice>(new DinputDevice()); dinput = std::shared_ptr<DinputDevice>(new DinputDevice());
xinput = std::shared_ptr<XinputDevice>(new XinputDevice()); xinput = std::shared_ptr<XinputDevice>(new XinputDevice());
pButtonsMap = new UINT[3 * nButtons]; pButtonsMap = new UINT[CONTROLS_DEVICE_NUM * nButtons];
ZeroMemory(pButtonsMap, sizeof(UINT) * CONTROLS_DEVICE_NUM * nButtons); ZeroMemory(pButtonsMap, sizeof(UINT) * CONTROLS_DEVICE_NUM * nButtons);
for (int i = 0; i < nButtons; i++) { for (UINT i = 0; i < nButtons; i++) {
*(GetDeviceButtonsMap(CONTROLS_KEYBOARD_INDEX) + i) = key_pad_map[i * 2]; *(GetDeviceButtonsMap(CONTROLS_KEYBOARD_INDEX) + i) = key_pad_map[i * 2];
*(GetDeviceButtonsMap(CONTROLS_DIRECT_INPUT_INDEX) + i) = dinput_ctrl_map[i * 2]; *(GetDeviceButtonsMap(CONTROLS_DIRECT_INPUT_INDEX) + i) = dinput_ctrl_map[i * 2];
*(GetDeviceButtonsMap(CONTROLS_XINPUT_INDEX) + i) = xinput_ctrl_map[i * 2]; *(GetDeviceButtonsMap(CONTROLS_XINPUT_INDEX) + i) = xinput_ctrl_map[i * 2];
} }
ZeroMemory(&keyboardAnalogMap, sizeof(keyboardAnalogMap));
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
keyboardAnalogMap[i] = analog_ctrl_map[i * 2]; *(GetDeviceButtonsMap(CONTROLS_KEYBOARD_ANALOG_INDEX) + i) = (UINT)analog_ctrl_map[i * 2];
} }
} }
@ -94,12 +95,13 @@ void ControlMapping::UpdateState()
rawState.button = -1; rawState.button = -1;
switch(currentDevicePage) switch(currentDevicePage)
{ {
case CONTROLS_KEYBOARD_INDEX: // keyboard case CONTROLS_KEYBOARD_INDEX:
case CONTROLS_KEYBOARD_ANALOG_INDEX:
{ {
; // leave it to KeyboardProc. ; // leave it to KeyboardProc.
} }
break; break;
case CONTROLS_DIRECT_INPUT_INDEX: // directinput case CONTROLS_DIRECT_INPUT_INDEX:
{ {
dinput->UpdateRawStateSingle(rawState); dinput->UpdateRawStateSingle(rawState);
@ -110,7 +112,7 @@ void ControlMapping::UpdateState()
} }
} }
break; break;
case CONTROLS_XINPUT_INDEX: // xinput case CONTROLS_XINPUT_INDEX:
{ {
xinput->UpdateRawStateSingle(rawState); xinput->UpdateRawStateSingle(rawState);
UINT newButton = (rawState.button != rawState.prevButton && rawState.prevButton == -1) UINT newButton = (rawState.button != rawState.prevButton && rawState.prevButton == -1)
@ -126,13 +128,13 @@ void ControlMapping::UpdateState()
void ControlMapping::BindToDevices() void ControlMapping::BindToDevices()
{ {
for (int i = 0; i < buttonsCount; i++) { for (UINT i = 0; i < buttonsCount; i++) {
key_pad_map[i * 2] = *(GetDeviceButtonsMap(CONTROLS_KEYBOARD_INDEX) + i); key_pad_map[i * 2] = *(GetDeviceButtonsMap(CONTROLS_KEYBOARD_INDEX) + i);
dinput_ctrl_map[i * 2] = *(GetDeviceButtonsMap(CONTROLS_DIRECT_INPUT_INDEX) + i); dinput_ctrl_map[i * 2] = *(GetDeviceButtonsMap(CONTROLS_DIRECT_INPUT_INDEX) + i);
xinput_ctrl_map[i * 2] = *(GetDeviceButtonsMap(CONTROLS_XINPUT_INDEX) + i); xinput_ctrl_map[i * 2] = *(GetDeviceButtonsMap(CONTROLS_XINPUT_INDEX) + i);
} }
for (int i = 0; i < 4; i++) { for (UINT i = 0; i < 4; i++) {
analog_ctrl_map[i * 2] = keyboardAnalogMap[i]; analog_ctrl_map[i * 2] = (USHORT)*(GetDeviceButtonsMap(CONTROLS_KEYBOARD_ANALOG_INDEX) + i);
} }
} }

View file

@ -36,10 +36,12 @@ struct RawInputState
#define XBOX_CODE_LEFTTRIGER 0x00010000 #define XBOX_CODE_LEFTTRIGER 0x00010000
#define XBOX_CODE_RIGHTTRIGER 0x00020000 #define XBOX_CODE_RIGHTTRIGER 0x00020000
#define CONTROLS_KEYBOARD_INDEX 0 #define CONTROLS_KEYBOARD_INDEX 0
#define CONTROLS_DIRECT_INPUT_INDEX 1 #define CONTROLS_DIRECT_INPUT_INDEX 1
#define CONTROLS_XINPUT_INDEX 2 #define CONTROLS_XINPUT_INDEX 2
#define CONTROLS_DEVICE_NUM 3 #define CONTROLS_KEYBOARD_ANALOG_INDEX 3
#define CONTROLS_DEVICE_NUM 4
extern bool saveControlsToFile(); extern bool saveControlsToFile();
extern bool loadControlsFromFile(); extern bool loadControlsFromFile();
@ -65,6 +67,8 @@ public:
void SetTargetButton(UINT buttonIdx); void SetTargetButton(UINT buttonIdx);
UINT GetTargetButton(); UINT GetTargetButton();
void SetBindCode(UINT newCode); void SetBindCode(UINT newCode);
void SetBindCode(UINT newCode, UINT buttonIdx);
void SetBindCode(UINT newCode, UINT deviceIdx, UINT buttonIdx);
UINT GetBindCode(); UINT GetBindCode();
UINT GetBindCode(UINT buttonIdx); UINT GetBindCode(UINT buttonIdx);
UINT GetBindCode(UINT deviceIdx, UINT buttonIdx); UINT GetBindCode(UINT deviceIdx, UINT buttonIdx);
@ -73,8 +77,8 @@ private:
ControlMapping(UINT nButtons); ControlMapping(UINT nButtons);
ControlMapping(); ControlMapping();
inline UINT* GetDeviceButtonsMap(UINT curDevice); inline UINT* GetDeviceButtonsMap(UINT curDevice);
void SetBindCode(UINT newCode, UINT buttonIdx);
void SetBindCode(UINT newCode, UINT deviceIdx, UINT buttonIdx); private:
std::shared_ptr<DinputDevice> dinput; std::shared_ptr<DinputDevice> dinput;
std::shared_ptr<XinputDevice> xinput; std::shared_ptr<XinputDevice> xinput;
UINT currentDevicePage; UINT currentDevicePage;
@ -82,5 +86,4 @@ private:
UINT buttonsCount; UINT buttonsCount;
RawInputState rawState; RawInputState rawState;
UINT *pButtonsMap; UINT *pButtonsMap;
USHORT keyboardAnalogMap[4];
}; };

View file

@ -107,13 +107,26 @@ DinputDevice::DinputDevice()
DIPROPRANGE diprg; DIPROPRANGE diprg;
diprg.diph.dwSize = sizeof(DIPROPRANGE); diprg.diph.dwSize = sizeof(DIPROPRANGE);
diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER); diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
diprg.diph.dwHow = DIPH_DEVICE; diprg.diph.dwHow = DIPH_DEVICE;
diprg.diph.dwObj = 0; diprg.diph.dwObj = 0;
diprg.lMin = -10000; diprg.lMin = -10000;
diprg.lMax = 10000; diprg.lMax = 10000;
analog = FAILED(pJoystick->SetProperty(DIPROP_RANGE, &diprg.diph))?false:true; analog = FAILED(pJoystick->SetProperty(DIPROP_RANGE, &diprg.diph))?false:true;
// Other devices suffer If do not set the dead zone.
// TODO: the dead zone will make configurable in the Control dialog.
DIPROPDWORD dipw;
dipw.diph.dwSize = sizeof(DIPROPDWORD);
dipw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
dipw.diph.dwHow = DIPH_DEVICE;
dipw.diph.dwObj = 0;
// dwData 1000 is deadzone(0% - 10%)
dipw.dwData = 1000;
analog |= FAILED(pJoystick->SetProperty(DIPROP_DEADZONE, &dipw.diph))?false:true;
} }
DinputDevice::~DinputDevice() DinputDevice::~DinputDevice()
@ -170,8 +183,8 @@ int DinputDevice::UpdateState(InputState &input_state)
if (analog) if (analog)
{ {
input_state.pad_lstick_x = (float)js.lX / 10000.f; input_state.pad_lstick_x += (float)js.lX / 10000.f;
input_state.pad_lstick_y = -((float)js.lY / 10000.f); input_state.pad_lstick_y += -((float)js.lY / 10000.f);
} }
for (u8 i = 0; i < sizeof(dinput_ctrl_map)/sizeof(dinput_ctrl_map[0]); i += 2) for (u8 i = 0; i < sizeof(dinput_ctrl_map)/sizeof(dinput_ctrl_map[0]); i += 2)

View file

@ -71,7 +71,8 @@ int KeyboardDevice::UpdateState(InputState &input_state) {
break; break;
} }
} }
// keyboard device
input_state.pad_lstick_x += analogX; input_state.pad_lstick_x += analogX;
input_state.pad_lstick_y += analogY; input_state.pad_lstick_y += analogY;
return 0; return 0;

View file

@ -989,7 +989,12 @@ namespace MainWindow
HWND hDlg = GetParent(hEdit); HWND hDlg = GetParent(hEdit);
const char *str = getVirtualKeyName(wParam); const char *str = getVirtualKeyName(wParam);
if (str) { if (str) {
pCtrlDlgState->pCtrlMap->SetBindCode(wParam); if (nCtrlID >= IDC_EDIT_KEY_ANALOG_UP) {
pCtrlDlgState->pCtrlMap->SetBindCode(wParam, CONTROLS_KEYBOARD_ANALOG_INDEX,
nCtrlID - IDC_EDIT_KEY_ANALOG_UP);
} else {
pCtrlDlgState->pCtrlMap->SetBindCode(wParam);
}
SetWindowTextA(hEdit, str); SetWindowTextA(hEdit, str);
RECT rc = getRedrawRect(hEdit); RECT rc = getRedrawRect(hEdit);
InvalidateRect(hDlg, &rc, false); InvalidateRect(hDlg, &rc, false);
@ -1134,11 +1139,15 @@ namespace MainWindow
DeleteObject(hResBM); DeleteObject(hResBM);
} }
for (u32 i = 0; i <= CONTROLS_IDC_EDIT_END - CONTROLS_IDC_EDIT_BIGIN; i++) { for (u32 i = 0; i <= IDC_EDIT_KEYRIGHT - CONTROLS_IDC_EDIT_BIGIN; i++) {
HWND hEdit = GetDlgItem(hDlg, CONTROLS_IDC_EDIT_BIGIN + i); HWND hEdit = GetDlgItem(hDlg, CONTROLS_IDC_EDIT_BIGIN + i);
SetWindowTextA(hEdit, getVirtualKeyName(pCtrlDlgState->pCtrlMap->GetBindCode(CONTROLS_KEYBOARD_INDEX, i))); SetWindowTextA(hEdit, getVirtualKeyName(pCtrlDlgState->pCtrlMap->GetBindCode(CONTROLS_KEYBOARD_INDEX, i)));
} }
for (u32 i = 0; i <= CONTROLS_IDC_EDIT_END - IDC_EDIT_KEY_ANALOG_UP; i++) {
HWND hEdit = GetDlgItem(hDlg, IDC_EDIT_KEY_ANALOG_UP + i);
SetWindowTextA(hEdit, getVirtualKeyName(pCtrlDlgState->pCtrlMap->GetBindCode(CONTROLS_KEYBOARD_ANALOG_INDEX, i)));
}
ComboBox_AddString(GetDlgItem(hDlg, IDC_FORCE_INPUT_DEVICE), "None"); ComboBox_AddString(GetDlgItem(hDlg, IDC_FORCE_INPUT_DEVICE), "None");
ComboBox_AddString(GetDlgItem(hDlg, IDC_FORCE_INPUT_DEVICE), "XInput"); ComboBox_AddString(GetDlgItem(hDlg, IDC_FORCE_INPUT_DEVICE), "XInput");
ComboBox_AddString(GetDlgItem(hDlg, IDC_FORCE_INPUT_DEVICE), "DirectInput"); ComboBox_AddString(GetDlgItem(hDlg, IDC_FORCE_INPUT_DEVICE), "DirectInput");
@ -1223,13 +1232,16 @@ namespace MainWindow
{ {
case CONTROLS_KEYBOARD_INDEX: case CONTROLS_KEYBOARD_INDEX:
{ {
for (u32 i = 0; i <= CONTROLS_IDC_EDIT_END - CONTROLS_IDC_EDIT_BIGIN; i++) { for (u32 i = 0; i <= IDC_EDIT_KEYRIGHT - CONTROLS_IDC_EDIT_BIGIN; i++) {
HWND hEdit = GetDlgItem(hDlg, CONTROLS_IDC_EDIT_BIGIN + i); HWND hEdit = GetDlgItem(hDlg, CONTROLS_IDC_EDIT_BIGIN + i);
if (i >= IDC_EDIT_KEY_ANALOG_UP - CONTROLS_IDC_EDIT_BIGIN) {
Edit_SetReadOnly(hEdit, FALSE);
}
SetWindowTextA(hEdit, getVirtualKeyName(pCtrlDlgState->pCtrlMap->GetBindCode(i))); SetWindowTextA(hEdit, getVirtualKeyName(pCtrlDlgState->pCtrlMap->GetBindCode(i)));
} }
for (u32 i = 0; i <= CONTROLS_IDC_EDIT_END - IDC_EDIT_KEY_ANALOG_UP; i++) {
HWND hEdit = GetDlgItem(hDlg, IDC_EDIT_KEY_ANALOG_UP + i);
Edit_SetReadOnly(hEdit, FALSE);
SetWindowText(hEdit, getVirtualKeyName(pCtrlDlgState->pCtrlMap->GetBindCode(
CONTROLS_KEYBOARD_ANALOG_INDEX, i)));
}
InvalidateRect(hDlg, 0, 0); InvalidateRect(hDlg, 0, 0);
} }
break; break;