From 7b33325cacf35346bcad00e27a5781c7b2b60e78 Mon Sep 17 00:00:00 2001 From: iota97 Date: Wed, 8 Sep 2021 08:54:59 +0200 Subject: [PATCH] Double binding fix --- Core/KeyMap.cpp | 19 +++++++++++++++++++ Core/KeyMap.h | 2 ++ UI/ControlMappingScreen.cpp | 12 ++++++++---- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Core/KeyMap.cpp b/Core/KeyMap.cpp index 76b173dab8..6b9627eaa6 100644 --- a/Core/KeyMap.cpp +++ b/Core/KeyMap.cpp @@ -603,6 +603,25 @@ bool IsKeyMapped(int device, int key) { return false; } +bool ReplaceSingleKeyMapping(int btn, int index, KeyDef key) { + // Check for duplicate + for (int i = 0; i < g_controllerMap[btn].size(); ++i) { + if (i != index && g_controllerMap[btn][i] == key) { + g_controllerMap[btn].erase(g_controllerMap[btn].begin()+index); + g_controllerMapGeneration++; + + UpdateNativeMenuKeys(); + return false; + } + } + KeyMap::g_controllerMap[btn][index] = key; + g_controllerMapGeneration++; + + g_seenDeviceIds.insert(key.deviceId); + UpdateNativeMenuKeys(); + return true; +} + void SetKeyMapping(int btn, KeyDef key, bool replace) { if (key.keyCode < 0) return; diff --git a/Core/KeyMap.h b/Core/KeyMap.h index 2126bcdc82..b2a1266e9c 100644 --- a/Core/KeyMap.h +++ b/Core/KeyMap.h @@ -128,6 +128,8 @@ namespace KeyMap { // Configure the key mapping. // Any configuration will be saved to the Core config. void SetKeyMapping(int psp_key, KeyDef key, bool replace); + // Return false if bind was a duplicate and got removed + bool ReplaceSingleKeyMapping(int btn, int index, KeyDef key); // Configure an axis mapping, saves the configuration. // Direction is negative or positive. diff --git a/UI/ControlMappingScreen.cpp b/UI/ControlMappingScreen.cpp index 1db57b1949..fa404f1c47 100644 --- a/UI/ControlMappingScreen.cpp +++ b/UI/ControlMappingScreen.cpp @@ -166,13 +166,17 @@ void SingleControlMapper::MappedCallback(KeyDef kdf) { replaceAllButton_->SetFocus(); break; case REPLACEONE: - KeyMap::g_controllerMap[pspKey_][actionIndex_] = kdf; - KeyMap::g_controllerMapGeneration++; - if (actionIndex_ < rows_.size()) + { + bool success = KeyMap::ReplaceSingleKeyMapping(pspKey_, actionIndex_, kdf); + if (!success) { + replaceAllButton_->SetFocus(); // Last got removed as a duplicate + } else if (actionIndex_ < rows_.size()) { rows_[actionIndex_]->SetFocus(); - else + } else { SetFocus(); + } break; + } default: SetFocus(); break;