From 5201f0372086c972e095f5f25688c7f466ff8da2 Mon Sep 17 00:00:00 2001 From: Andrey Bondrov Date: Fri, 7 Nov 2014 22:40:53 +1000 Subject: [PATCH 1/5] Add a hotkey to swap D-pad and left analog stick keys --- Common/KeyMap.cpp | 25 +++++++++++++++++++++++++ Common/KeyMap.h | 2 ++ UI/EmuScreen.cpp | 4 ++++ 3 files changed, 31 insertions(+) diff --git a/Common/KeyMap.cpp b/Common/KeyMap.cpp index bf7314a9fd..d93ff24492 100644 --- a/Common/KeyMap.cpp +++ b/Common/KeyMap.cpp @@ -650,6 +650,8 @@ const KeyMap_IntStrPair psp_button_names[] = { {VIRTKEY_AXIS_RIGHT_Y_MIN, "RightAn.Down"}, {VIRTKEY_AXIS_RIGHT_X_MIN, "RightAn.Left"}, {VIRTKEY_AXIS_RIGHT_X_MAX, "RightAn.Right"}, + + {VIRTKEY_AXIS_SWAP, "AxisSwap"}, }; const int AXIS_BIND_NKCODE_START = 4000; @@ -920,4 +922,27 @@ const std::set &GetSeenPads() { } +void SwapKeys(int btn1, int btn2) { + std::vector keys1; + std::vector keys2; + KeyFromPspButton(psp_button_names[btn1].key, &keys1); + KeyFromPspButton(psp_button_names[btn2].key, &keys2); + RemoveButtonMapping(psp_button_names[btn1].key); + RemoveButtonMapping(psp_button_names[btn2].key); + for (size_t j = 0; j < keys1.size(); j++) { + SetKeyMapping(psp_button_names[btn2].key, KeyDef(keys1[j].deviceId, keys1[j].keyCode), false); + } + for (size_t j = 0; j < keys2.size(); j++) { + SetKeyMapping(psp_button_names[btn1].key, KeyDef(keys2[j].deviceId, keys2[j].keyCode), false); + } +} + +// Swap direction buttons and left analog axis +void SwapAxis() { + SwapKeys(0, 12); + SwapKeys(1, 13); + SwapKeys(2, 14); + SwapKeys(3, 15); +} + } // KeyMap diff --git a/Common/KeyMap.h b/Common/KeyMap.h index 30fae0c7ea..974f13ecae 100644 --- a/Common/KeyMap.h +++ b/Common/KeyMap.h @@ -47,6 +47,7 @@ enum { VIRTKEY_NEXT_SLOT = 0x1000f, VIRTKEY_TOGGLE_FULLSCREEN = 0x10010, VIRTKEY_ANALOG_LIGHTLY = 0x10011, + VIRTKEY_AXIS_SWAP = 0x10012, VIRTKEY_LAST, VIRTKEY_COUNT = VIRTKEY_LAST - VIRTKEY_FIRST }; @@ -156,6 +157,7 @@ namespace KeyMap { void RestoreDefault(); + void SwapAxis(); void UpdateConfirmCancelKeys(); void NotifyPadConnected(const std::string &name); diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index 5534901bfb..49c04050fc 100644 --- a/UI/EmuScreen.cpp +++ b/UI/EmuScreen.cpp @@ -290,6 +290,10 @@ void EmuScreen::onVKeyDown(int virtualKeyCode) { pauseTrigger_ = true; break; + case VIRTKEY_AXIS_SWAP: + KeyMap::SwapAxis(); + break; + case VIRTKEY_AXIS_X_MIN: case VIRTKEY_AXIS_X_MAX: setVKeyAnalogX(CTRL_STICK_LEFT, VIRTKEY_AXIS_X_MIN, VIRTKEY_AXIS_X_MAX); From 38268a3d37d05930a98e0eccccdc5655aa89f062 Mon Sep 17 00:00:00 2001 From: Andrey Bondrov Date: Sat, 13 Dec 2014 10:36:13 +1000 Subject: [PATCH 2/5] Use constants instead of hardcoded numbers for keys swap --- Common/KeyMap.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Common/KeyMap.cpp b/Common/KeyMap.cpp index d93ff24492..300627f782 100644 --- a/Common/KeyMap.cpp +++ b/Common/KeyMap.cpp @@ -939,10 +939,10 @@ void SwapKeys(int btn1, int btn2) { // Swap direction buttons and left analog axis void SwapAxis() { - SwapKeys(0, 12); - SwapKeys(1, 13); - SwapKeys(2, 14); - SwapKeys(3, 15); + SwapKeys(CTRL_UP, VIRTKEY_AXIS_Y_MAX); + SwapKeys(CTRL_DOWN, VIRTKEY_AXIS_Y_MIN); + SwapKeys(CTRL_LEFT, VIRTKEY_AXIS_X_MIN); + SwapKeys(CTRL_RIGHT, VIRTKEY_AXIS_X_MAX); } } // KeyMap From c41e4ef9055df28362b950b7cf99b2873d8a3dac Mon Sep 17 00:00:00 2001 From: Andrey Bondrov Date: Sat, 13 Dec 2014 12:13:42 +1000 Subject: [PATCH 3/5] Don't make Axis <-> D-pad swap persistent --- Common/KeyMap.cpp | 54 ++++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/Common/KeyMap.cpp b/Common/KeyMap.cpp index 300627f782..d3f35634f3 100644 --- a/Common/KeyMap.cpp +++ b/Common/KeyMap.cpp @@ -44,6 +44,8 @@ struct DefMappingStruct { KeyMapping g_controllerMap; std::set g_seenPads; +bool g_swapped_keys = false; + static const DefMappingStruct defaultQwertyKeyboardKeyMap[] = { {CTRL_SQUARE, NKCODE_A}, {CTRL_TRIANGLE, NKCODE_S}, @@ -717,12 +719,36 @@ KeyDef AxisDef(int deviceId, int axisId, int direction) { return KeyDef(deviceId, TranslateKeyCodeFromAxis(axisId, direction)); } +int SwappedKey(int btn) { + if (g_swapped_keys) { + switch (btn) { + case CTRL_UP: btn = VIRTKEY_AXIS_Y_MAX; + break; + case VIRTKEY_AXIS_Y_MAX: btn = CTRL_UP; + break; + case CTRL_DOWN: btn = VIRTKEY_AXIS_Y_MIN; + break; + case VIRTKEY_AXIS_Y_MIN: btn = CTRL_DOWN; + break; + case CTRL_LEFT: btn = VIRTKEY_AXIS_X_MIN; + break; + case VIRTKEY_AXIS_X_MIN: btn = CTRL_LEFT; + break; + case CTRL_RIGHT: btn = VIRTKEY_AXIS_X_MAX; + break; + case VIRTKEY_AXIS_X_MAX: btn = CTRL_RIGHT; + break; + } + } + return btn; +} + 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->push_back(iter->first); + psp_button->push_back(SwappedKey(iter->first)); } } } @@ -749,7 +775,7 @@ bool KeyFromPspButton(int btn, std::vector *keys) { bool AxisToPspButton(int deviceId, int axisId, int direction, std::vector *pspKeys) { int key = TranslateKeyCodeFromAxis(axisId, direction); - return KeyToPspButton(deviceId, key, pspKeys); + return KeyToPspButton(deviceId, key, pspKeys); } bool AxisFromPspButton(int btn, int *deviceId, int *axisId, int *direction) { @@ -921,28 +947,12 @@ const std::set &GetSeenPads() { return g_seenPads; } - -void SwapKeys(int btn1, int btn2) { - std::vector keys1; - std::vector keys2; - KeyFromPspButton(psp_button_names[btn1].key, &keys1); - KeyFromPspButton(psp_button_names[btn2].key, &keys2); - RemoveButtonMapping(psp_button_names[btn1].key); - RemoveButtonMapping(psp_button_names[btn2].key); - for (size_t j = 0; j < keys1.size(); j++) { - SetKeyMapping(psp_button_names[btn2].key, KeyDef(keys1[j].deviceId, keys1[j].keyCode), false); - } - for (size_t j = 0; j < keys2.size(); j++) { - SetKeyMapping(psp_button_names[btn1].key, KeyDef(keys2[j].deviceId, keys2[j].keyCode), false); - } -} - // Swap direction buttons and left analog axis void SwapAxis() { - SwapKeys(CTRL_UP, VIRTKEY_AXIS_Y_MAX); - SwapKeys(CTRL_DOWN, VIRTKEY_AXIS_Y_MIN); - SwapKeys(CTRL_LEFT, VIRTKEY_AXIS_X_MIN); - SwapKeys(CTRL_RIGHT, VIRTKEY_AXIS_X_MAX); + if (g_swapped_keys) + g_swapped_keys = false; + else + g_swapped_keys = true; } } // KeyMap From 89c6f7645617b0180a2d86285926c38b275b74f7 Mon Sep 17 00:00:00 2001 From: Andrey Bondrov Date: Sat, 13 Dec 2014 12:20:31 +1000 Subject: [PATCH 4/5] Less confusing function name --- Common/KeyMap.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Common/KeyMap.cpp b/Common/KeyMap.cpp index d3f35634f3..810e1c7f68 100644 --- a/Common/KeyMap.cpp +++ b/Common/KeyMap.cpp @@ -719,7 +719,7 @@ KeyDef AxisDef(int deviceId, int axisId, int direction) { return KeyDef(deviceId, TranslateKeyCodeFromAxis(axisId, direction)); } -int SwappedKey(int btn) { +int CheckAxisSwap(int btn) { if (g_swapped_keys) { switch (btn) { case CTRL_UP: btn = VIRTKEY_AXIS_Y_MAX; @@ -748,7 +748,7 @@ static bool FindKeyMapping(int deviceId, int key, std::vector *psp_button) 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->push_back(SwappedKey(iter->first)); + psp_button->push_back(CheckAxisSwap(iter->first)); } } } From 35d014916216dac26b0c2773b8750773fd98a55b Mon Sep 17 00:00:00 2001 From: Andrey Bondrov Date: Sun, 14 Dec 2014 00:29:58 +1000 Subject: [PATCH 5/5] Less code for bool variable inversion --- Common/KeyMap.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Common/KeyMap.cpp b/Common/KeyMap.cpp index 810e1c7f68..863106c63c 100644 --- a/Common/KeyMap.cpp +++ b/Common/KeyMap.cpp @@ -949,10 +949,7 @@ const std::set &GetSeenPads() { // Swap direction buttons and left analog axis void SwapAxis() { - if (g_swapped_keys) - g_swapped_keys = false; - else - g_swapped_keys = true; + g_swapped_keys = !g_swapped_keys; } } // KeyMap