From bf7f582c3caf6b358f3a83c48e5f67bbe15063c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 31 Oct 2013 16:50:27 +0100 Subject: [PATCH] If an input (axis or key) is mapped to multiple targets, trigger them all, not just the first one. --- Common/KeyMap.cpp | 18 ++++----- Common/KeyMap.h | 7 +--- UI/EmuScreen.cpp | 97 +++++++++++++++++++++++++---------------------- 3 files changed, 61 insertions(+), 61 deletions(-) diff --git a/Common/KeyMap.cpp b/Common/KeyMap.cpp index f83cf56b0f..57fcacf812 100644 --- a/Common/KeyMap.cpp +++ b/Common/KeyMap.cpp @@ -671,27 +671,23 @@ KeyDef AxisDef(int deviceId, int axisId, int direction) { return KeyDef(deviceId, TranslateKeyCodeFromAxis(axisId, direction)); } -static bool FindKeyMapping(int deviceId, int key, int *psp_button) { +static bool FindKeyMapping(int deviceId, int key, std::vector *psp_button) { // Brute force, let's optimize later for (auto iter = g_controllerMap.begin(); iter != g_controllerMap.end(); ++iter) { for (auto iter2 = iter->second.begin(); iter2 != iter->second.end(); ++iter2) { if (*iter2 == KeyDef(deviceId, key)) { - *psp_button = iter->first; - return true; + psp_button->push_back(iter->first); } } } - return false; + return psp_button->size() > 0; } -int KeyToPspButton(int deviceId, int key) { +bool KeyToPspButton(int deviceId, int key, std::vector *pspKeys) { int search_start_layer = 0; int psp_button; - if (FindKeyMapping(deviceId, key, &psp_button)) - return psp_button; - - return KEYMAP_ERROR_UNKNOWN_KEY; + return FindKeyMapping(deviceId, key, pspKeys); } // TODO: vector output @@ -708,9 +704,9 @@ bool KeyFromPspButton(int btn, std::vector *keys) { return false; } -int AxisToPspButton(int deviceId, int axisId, int direction) { +bool AxisToPspButton(int deviceId, int axisId, int direction, std::vector *pspKeys) { int key = TranslateKeyCodeFromAxis(axisId, direction); - return KeyToPspButton(deviceId, key); + return KeyToPspButton(deviceId, key, pspKeys); } bool AxisFromPspButton(int btn, int *deviceId, int *axisId, int *direction) { diff --git a/Common/KeyMap.h b/Common/KeyMap.h index 604a861e2f..bcc49f6a27 100644 --- a/Common/KeyMap.h +++ b/Common/KeyMap.h @@ -124,10 +124,7 @@ namespace KeyMap { // Use if to translate KeyMap Keys to PSP // buttons. You should have already translated // your platform's keys to KeyMap keys. - // - // Returns KEYMAP_ERROR_UNKNOWN_KEY - // for any unmapped key - int KeyToPspButton(int deviceId, int key); + bool KeyToPspButton(int deviceId, int key, std::vector *pspKeys); bool KeyFromPspButton(int btn, std::vector *keys); int TranslateKeyCodeToAxis(int keyCode, int &direction); @@ -141,7 +138,7 @@ namespace KeyMap { // Direction is negative or positive. void SetAxisMapping(int btn, int deviceId, int axisId, int direction, bool replace); - int AxisToPspButton(int deviceId, int axisId, int direction); + bool AxisToPspButton(int deviceId, int axisId, int direction, std::vector *pspKeys); bool AxisFromPspButton(int btn, int *deviceId, int *axisId, int *direction); std::string NamePspButtonFromAxis(int deviceId, int axisId, int direction); diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index 30ef7aa48c..4a2ac5f37d 100644 --- a/UI/EmuScreen.cpp +++ b/UI/EmuScreen.cpp @@ -312,11 +312,11 @@ void EmuScreen::key(const KeyInput &key) { pauseTrigger_ = true; } - int result = KeyMap::KeyToPspButton(key.deviceId, key.keyCode); - if (result == KEYMAP_ERROR_UNKNOWN_KEY) - return; - - pspKey(result, key.flags); + std::vector pspKeys; + KeyMap::KeyToPspButton(key.deviceId, key.keyCode, &pspKeys); + for (size_t i = 0; i < pspKeys.size(); i++) { + pspKey(pspKeys[i], key.flags); + } } void EmuScreen::pspKey(int pspKeyCode, int flags) { @@ -352,51 +352,58 @@ void EmuScreen::axis(const AxisInput &axis) { } void EmuScreen::processAxis(const AxisInput &axis, int direction) { - int result = KeyMap::AxisToPspButton(axis.deviceId, axis.axisId, direction); - int resultOpposite = KeyMap::AxisToPspButton(axis.deviceId, axis.axisId, -direction); + std::vector results; + KeyMap::AxisToPspButton(axis.deviceId, axis.axisId, direction, &results); + std::vector resultsOpposite; + KeyMap::AxisToPspButton(axis.deviceId, axis.axisId, -direction, &resultsOpposite); - if (result == KEYMAP_ERROR_UNKNOWN_KEY) - return; + for (size_t i = 0; i < results.size(); i++) { + int result = results[i]; + switch (result) { + case VIRTKEY_AXIS_X_MIN: + __CtrlSetAnalogX(-fabs(axis.value), CTRL_STICK_LEFT); + break; + case VIRTKEY_AXIS_X_MAX: + __CtrlSetAnalogX(fabs(axis.value), CTRL_STICK_LEFT); + break; + case VIRTKEY_AXIS_Y_MIN: + __CtrlSetAnalogY(-fabs(axis.value), CTRL_STICK_LEFT); + break; + case VIRTKEY_AXIS_Y_MAX: + __CtrlSetAnalogY(fabs(axis.value), CTRL_STICK_LEFT); + break; - switch (result) { - case VIRTKEY_AXIS_X_MIN: - __CtrlSetAnalogX(-fabs(axis.value), CTRL_STICK_LEFT); - break; - case VIRTKEY_AXIS_X_MAX: - __CtrlSetAnalogX(fabs(axis.value), CTRL_STICK_LEFT); - break; - case VIRTKEY_AXIS_Y_MIN: - __CtrlSetAnalogY(-fabs(axis.value), CTRL_STICK_LEFT); - break; - case VIRTKEY_AXIS_Y_MAX: - __CtrlSetAnalogY(fabs(axis.value), CTRL_STICK_LEFT); - break; + case VIRTKEY_AXIS_RIGHT_X_MIN: + __CtrlSetAnalogX(-fabs(axis.value), CTRL_STICK_RIGHT); + break; + case VIRTKEY_AXIS_RIGHT_X_MAX: + __CtrlSetAnalogX(fabs(axis.value), CTRL_STICK_RIGHT); + break; + case VIRTKEY_AXIS_RIGHT_Y_MIN: + __CtrlSetAnalogY(-fabs(axis.value), CTRL_STICK_RIGHT); + break; + case VIRTKEY_AXIS_RIGHT_Y_MAX: + __CtrlSetAnalogY(fabs(axis.value), CTRL_STICK_RIGHT); + break; - case VIRTKEY_AXIS_RIGHT_X_MIN: - __CtrlSetAnalogX(-fabs(axis.value), CTRL_STICK_RIGHT); - break; - case VIRTKEY_AXIS_RIGHT_X_MAX: - __CtrlSetAnalogX(fabs(axis.value), CTRL_STICK_RIGHT); - break; - case VIRTKEY_AXIS_RIGHT_Y_MIN: - __CtrlSetAnalogY(-fabs(axis.value), CTRL_STICK_RIGHT); - break; - case VIRTKEY_AXIS_RIGHT_Y_MAX: - __CtrlSetAnalogY(fabs(axis.value), CTRL_STICK_RIGHT); - break; + default: + if (axis.value >= AXIS_BIND_THRESHOLD || axis.value <= -AXIS_BIND_THRESHOLD) { + pspKey(result, KEY_DOWN); - default: - if (axis.value >= AXIS_BIND_THRESHOLD || axis.value <= -AXIS_BIND_THRESHOLD) { - pspKey(result, KEY_DOWN); - - // Also unpress the other direction. - result = KeyMap::AxisToPspButton(axis.deviceId, axis.axisId, axis.value >= 0 ? -1 : 1); - if (result != KEYMAP_ERROR_UNKNOWN_KEY) + // Also unpress the other direction. + std::vector opposite; + KeyMap::AxisToPspButton(axis.deviceId, axis.axisId, axis.value >= 0 ? -1 : 1, &opposite); + for (size_t i = 0; i < opposite.size(); i++) { + pspKey(opposite[i], KEY_UP); + } + // Hm, why do we use a different way below? + } else { + // Release both directions, trying to deal with some erratic controllers that can cause it to stick. pspKey(result, KEY_UP); - } else { - // Release both directions, trying to deal with some erratic controllers that can cause it to stick. - pspKey(result, KEY_UP); - pspKey(resultOpposite, KEY_UP); + for (size_t i = 0; i < resultsOpposite.size(); i++) { + pspKey(resultsOpposite[i], KEY_UP); + } + } } } }