diff --git a/Core/ControlMapper.cpp b/Core/ControlMapper.cpp index e959fd3b5c..8d87d53604 100644 --- a/Core/ControlMapper.cpp +++ b/Core/ControlMapper.cpp @@ -227,8 +227,6 @@ bool ControlMapper::UpdatePSPState(const InputMapping &changedMapping) { // If a mapping could consist of a combo, we could trivially check it here. for (auto &multiMapping : inputMappings) { - if (multiMapping.empty()) - continue; // Check if the changed mapping was involved in this PSP key. if (multiMapping.mappings.contains(changedMapping)) { changedButtonMask |= mask; diff --git a/Core/KeyMap.cpp b/Core/KeyMap.cpp index 694402eb9a..4cb96497c2 100644 --- a/Core/KeyMap.cpp +++ b/Core/KeyMap.cpp @@ -515,20 +515,28 @@ bool InputMappingToPspButton(const InputMapping &mapping, std::vector *pspB } bool InputMappingsFromPspButton(int btn, std::vector *mappings, bool ignoreMouse) { + auto iter = g_controllerMap.find(btn); + if (iter == g_controllerMap.end()) { + return false; + } bool mapped = false; - for (auto iter = g_controllerMap.begin(); iter != g_controllerMap.end(); ++iter) { - if (iter->first == btn) { - for (auto iter2 = iter->second.begin(); iter2 != iter->second.end(); ++iter2) { - if (mappings && (!ignoreMouse || iter2->HasMouse())) { - mapped = true; - mappings->push_back(*iter2); - } - } + for (auto iter2 = iter->second.begin(); iter2 != iter->second.end(); ++iter2) { + if (mappings && (!ignoreMouse || iter2->HasMouse())) { + mapped = true; + mappings->push_back(*iter2); } } return mapped; } +bool PspButtonHasMappings(int btn) { + auto iter = g_controllerMap.find(btn); + if (iter == g_controllerMap.end()) { + return false; + } + return !iter->second.empty(); +} + MappedAnalogAxes MappedAxesForDevice(int deviceId) { MappedAnalogAxes result{}; @@ -605,6 +613,16 @@ bool ReplaceSingleKeyMapping(int btn, int index, MultiInputMapping key) { return true; } +void DeleteNthMapping(int key, int number) { + auto iter = g_controllerMap.find(key); + if (iter != g_controllerMap.end()) { + if (number < iter->second.size()) { + iter->second.erase(iter->second.begin() + number); + g_controllerMapGeneration++; + } + } +} + void SetInputMapping(int btn, const MultiInputMapping &key, bool replace) { if (key.empty()) { g_controllerMap.erase(btn); @@ -717,6 +735,11 @@ void SaveToIni(IniFile &file) { } } +void ClearAllMappings() { + g_controllerMap.clear(); + g_controllerMapGeneration++; +} + bool IsOuya(const std::string &name) { return name == "OUYA:OUYA Console"; } diff --git a/Core/KeyMap.h b/Core/KeyMap.h index 283ccd35b1..139cbaab3c 100644 --- a/Core/KeyMap.h +++ b/Core/KeyMap.h @@ -149,11 +149,11 @@ namespace KeyMap { FixedTinyVec mappings; }; - // Once the multimappings are inserted here, they must not be empty. - // If one would be, delete the whole entry from the map instead. typedef std::map> KeyMapping; - extern KeyMapping g_controllerMap; + // Once the multimappings are inserted here, they must not be empty. + // If one would be, delete the whole entry from the map instead. + // This is automatically handled by SetInputMapping. extern std::set g_seenDeviceIds; extern int g_controllerMapGeneration; // Key & Button names @@ -179,6 +179,9 @@ namespace KeyMap { bool InputMappingToPspButton(const InputMapping &mapping, std::vector *pspButtons); bool InputMappingsFromPspButton(int btn, std::vector *keys, bool ignoreMouse); + // Simplified check. + bool PspButtonHasMappings(int btn); + // Configure the key or axis mapping. // Any configuration will be saved to the Core config. void SetInputMapping(int psp_key, const MultiInputMapping &key, bool replace); @@ -189,6 +192,8 @@ namespace KeyMap { void LoadFromIni(IniFile &iniFile); void SaveToIni(IniFile &iniFile); + void ClearAllMappings(); + void DeleteNthMapping(int key, int number); void SetDefaultKeyMap(DefaultMaps dmap, bool replace); diff --git a/UI/ControlMappingScreen.cpp b/UI/ControlMappingScreen.cpp index b6d4424172..eebdee261e 100644 --- a/UI/ControlMappingScreen.cpp +++ b/UI/ControlMappingScreen.cpp @@ -222,8 +222,7 @@ UI::EventReturn SingleControlMapper::OnAddMouse(UI::EventParams ¶ms) { UI::EventReturn SingleControlMapper::OnDelete(UI::EventParams ¶ms) { int index = atoi(params.v->Tag().c_str()); - KeyMap::g_controllerMap[pspKey_].erase(KeyMap::g_controllerMap[pspKey_].begin() + index); - KeyMap::g_controllerMapGeneration++; + KeyMap::DeleteNthMapping(pspKey_, index); if (index + 1 < (int)rows_.size()) rows_[index]->SetFocus(); @@ -285,8 +284,7 @@ void ControlMappingScreen::update() { } UI::EventReturn ControlMappingScreen::OnClearMapping(UI::EventParams ¶ms) { - KeyMap::g_controllerMap.clear(); - KeyMap::g_controllerMapGeneration++; + KeyMap::ClearAllMappings(); return UI::EVENT_DONE; } diff --git a/Windows/MainWindowMenu.cpp b/Windows/MainWindowMenu.cpp index 1491d2d375..73e5046879 100644 --- a/Windows/MainWindowMenu.cpp +++ b/Windows/MainWindowMenu.cpp @@ -170,8 +170,8 @@ namespace MainWindow { } void DoTranslateMenus(HWND hWnd, HMENU menu) { - auto useDefHotkey = [](int virtkey) { - return KeyMap::g_controllerMap[virtkey].empty(); + auto useDefHotkey = [](int virtKey) { + return !KeyMap::PspButtonHasMappings(virtKey); }; TranslateMenuItem(menu, ID_FILE_MENU); @@ -536,8 +536,7 @@ namespace MainWindow { case ID_FILE_SAVESTATE_NEXT_SLOT_HC: { - if (KeyMap::g_controllerMap[VIRTKEY_NEXT_SLOT].empty()) - { + if (!KeyMap::PspButtonHasMappings(VIRTKEY_NEXT_SLOT)) { SaveState::NextSlot(); NativeMessageReceived("savestate_displayslot", ""); } @@ -559,7 +558,7 @@ namespace MainWindow { case ID_FILE_QUICKLOADSTATE_HC: { - if (KeyMap::g_controllerMap[VIRTKEY_LOAD_STATE].empty()) + if (!KeyMap::PspButtonHasMappings(VIRTKEY_LOAD_STATE)) { SetCursor(LoadCursor(0, IDC_WAIT)); SaveState::LoadSlot(PSP_CoreParameter().fileToStart, g_Config.iCurrentStateSlot, SaveStateActionFinished); @@ -575,7 +574,7 @@ namespace MainWindow { case ID_FILE_QUICKSAVESTATE_HC: { - if (KeyMap::g_controllerMap[VIRTKEY_SAVE_STATE].empty()) + if (!KeyMap::PspButtonHasMappings(VIRTKEY_SAVE_STATE)) { SetCursor(LoadCursor(0, IDC_WAIT)); SaveState::SaveSlot(PSP_CoreParameter().fileToStart, g_Config.iCurrentStateSlot, SaveStateActionFinished);