From d24e5be256c71b591d96d73a6da4ff847037496a Mon Sep 17 00:00:00 2001 From: nachume Date: Sun, 19 May 2013 18:12:22 +0900 Subject: [PATCH] FIX : keyboard bind issue, can not be setting of analog key. ADD: set the dead zone to the axis of the DirectInput. --- Windows/ControlMapping.cpp | 22 ++++++++++++---------- Windows/ControlMapping.h | 17 ++++++++++------- Windows/DinputDevice.cpp | 23 ++++++++++++++++++----- Windows/KeyboardDevice.cpp | 3 ++- Windows/WndMainWindow.cpp | 24 ++++++++++++++++++------ 5 files changed, 60 insertions(+), 29 deletions(-) diff --git a/Windows/ControlMapping.cpp b/Windows/ControlMapping.cpp index acb884a835..392374fd32 100644 --- a/Windows/ControlMapping.cpp +++ b/Windows/ControlMapping.cpp @@ -36,6 +36,8 @@ inline UINT* ControlMapping::GetDeviceButtonsMap(UINT curDevice) return pButtonsMap + buttonsCount; case CONTROLS_XINPUT_INDEX: return pButtonsMap + (buttonsCount * 2); + case CONTROLS_KEYBOARD_ANALOG_INDEX: + return pButtonsMap + (buttonsCount * 3); } return NULL; } @@ -67,17 +69,16 @@ ControlMapping::ControlMapping(UINT nButtons) : dinput = std::shared_ptr(new DinputDevice()); xinput = std::shared_ptr(new XinputDevice()); - pButtonsMap = new UINT[3 * nButtons]; + pButtonsMap = new 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_DIRECT_INPUT_INDEX) + i) = dinput_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++) { - 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; switch(currentDevicePage) { - case CONTROLS_KEYBOARD_INDEX: // keyboard + case CONTROLS_KEYBOARD_INDEX: + case CONTROLS_KEYBOARD_ANALOG_INDEX: { ; // leave it to KeyboardProc. } break; - case CONTROLS_DIRECT_INPUT_INDEX: // directinput + case CONTROLS_DIRECT_INPUT_INDEX: { dinput->UpdateRawStateSingle(rawState); @@ -110,7 +112,7 @@ void ControlMapping::UpdateState() } } break; - case CONTROLS_XINPUT_INDEX: // xinput + case CONTROLS_XINPUT_INDEX: { xinput->UpdateRawStateSingle(rawState); UINT newButton = (rawState.button != rawState.prevButton && rawState.prevButton == -1) @@ -126,13 +128,13 @@ void ControlMapping::UpdateState() 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); dinput_ctrl_map[i * 2] = *(GetDeviceButtonsMap(CONTROLS_DIRECT_INPUT_INDEX) + i); xinput_ctrl_map[i * 2] = *(GetDeviceButtonsMap(CONTROLS_XINPUT_INDEX) + i); } - for (int i = 0; i < 4; i++) { - analog_ctrl_map[i * 2] = keyboardAnalogMap[i]; + for (UINT i = 0; i < 4; i++) { + analog_ctrl_map[i * 2] = (USHORT)*(GetDeviceButtonsMap(CONTROLS_KEYBOARD_ANALOG_INDEX) + i); } } diff --git a/Windows/ControlMapping.h b/Windows/ControlMapping.h index 9bea829760..8eeb2772a2 100644 --- a/Windows/ControlMapping.h +++ b/Windows/ControlMapping.h @@ -36,10 +36,12 @@ struct RawInputState #define XBOX_CODE_LEFTTRIGER 0x00010000 #define XBOX_CODE_RIGHTTRIGER 0x00020000 -#define CONTROLS_KEYBOARD_INDEX 0 -#define CONTROLS_DIRECT_INPUT_INDEX 1 -#define CONTROLS_XINPUT_INDEX 2 -#define CONTROLS_DEVICE_NUM 3 +#define CONTROLS_KEYBOARD_INDEX 0 +#define CONTROLS_DIRECT_INPUT_INDEX 1 +#define CONTROLS_XINPUT_INDEX 2 +#define CONTROLS_KEYBOARD_ANALOG_INDEX 3 +#define CONTROLS_DEVICE_NUM 4 + extern bool saveControlsToFile(); extern bool loadControlsFromFile(); @@ -65,6 +67,8 @@ public: void SetTargetButton(UINT buttonIdx); UINT GetTargetButton(); void SetBindCode(UINT newCode); + void SetBindCode(UINT newCode, UINT buttonIdx); + void SetBindCode(UINT newCode, UINT deviceIdx, UINT buttonIdx); UINT GetBindCode(); UINT GetBindCode(UINT buttonIdx); UINT GetBindCode(UINT deviceIdx, UINT buttonIdx); @@ -73,8 +77,8 @@ private: ControlMapping(UINT nButtons); ControlMapping(); inline UINT* GetDeviceButtonsMap(UINT curDevice); - void SetBindCode(UINT newCode, UINT buttonIdx); - void SetBindCode(UINT newCode, UINT deviceIdx, UINT buttonIdx); + +private: std::shared_ptr dinput; std::shared_ptr xinput; UINT currentDevicePage; @@ -82,5 +86,4 @@ private: UINT buttonsCount; RawInputState rawState; UINT *pButtonsMap; - USHORT keyboardAnalogMap[4]; }; diff --git a/Windows/DinputDevice.cpp b/Windows/DinputDevice.cpp index 9fcc510bd9..37fbfcdef1 100644 --- a/Windows/DinputDevice.cpp +++ b/Windows/DinputDevice.cpp @@ -107,13 +107,26 @@ DinputDevice::DinputDevice() DIPROPRANGE diprg; diprg.diph.dwSize = sizeof(DIPROPRANGE); - diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER); + diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER); diprg.diph.dwHow = DIPH_DEVICE; diprg.diph.dwObj = 0; diprg.lMin = -10000; - diprg.lMax = 10000; - + diprg.lMax = 10000; + 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() @@ -170,8 +183,8 @@ int DinputDevice::UpdateState(InputState &input_state) if (analog) { - input_state.pad_lstick_x = (float)js.lX / 10000.f; - input_state.pad_lstick_y = -((float)js.lY / 10000.f); + input_state.pad_lstick_x += (float)js.lX / 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) diff --git a/Windows/KeyboardDevice.cpp b/Windows/KeyboardDevice.cpp index 50eb0c2f6b..85eb2ae9f2 100644 --- a/Windows/KeyboardDevice.cpp +++ b/Windows/KeyboardDevice.cpp @@ -71,7 +71,8 @@ int KeyboardDevice::UpdateState(InputState &input_state) { break; } } - + + // keyboard device input_state.pad_lstick_x += analogX; input_state.pad_lstick_y += analogY; return 0; diff --git a/Windows/WndMainWindow.cpp b/Windows/WndMainWindow.cpp index efddffe1a4..47e5faa570 100644 --- a/Windows/WndMainWindow.cpp +++ b/Windows/WndMainWindow.cpp @@ -989,7 +989,12 @@ namespace MainWindow HWND hDlg = GetParent(hEdit); const char *str = getVirtualKeyName(wParam); 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); RECT rc = getRedrawRect(hEdit); InvalidateRect(hDlg, &rc, false); @@ -1134,11 +1139,15 @@ namespace MainWindow 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); 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), "XInput"); ComboBox_AddString(GetDlgItem(hDlg, IDC_FORCE_INPUT_DEVICE), "DirectInput"); @@ -1223,13 +1232,16 @@ namespace MainWindow { 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); - if (i >= IDC_EDIT_KEY_ANALOG_UP - CONTROLS_IDC_EDIT_BIGIN) { - Edit_SetReadOnly(hEdit, FALSE); - } 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); } break;