Cleanup, confine g_controllerMap access to KeyMap.cpp

This commit is contained in:
Henrik Rydgård 2023-04-01 19:56:02 +02:00
parent d523005c2b
commit c1b5aed9b7
5 changed files with 46 additions and 23 deletions

View file

@ -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;

View file

@ -515,20 +515,28 @@ bool InputMappingToPspButton(const InputMapping &mapping, std::vector<int> *pspB
}
bool InputMappingsFromPspButton(int btn, std::vector<MultiInputMapping> *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";
}

View file

@ -149,11 +149,11 @@ namespace KeyMap {
FixedTinyVec<InputMapping, 3> 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<int, std::vector<MultiInputMapping>> 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<int> g_seenDeviceIds;
extern int g_controllerMapGeneration;
// Key & Button names
@ -179,6 +179,9 @@ namespace KeyMap {
bool InputMappingToPspButton(const InputMapping &mapping, std::vector<int> *pspButtons);
bool InputMappingsFromPspButton(int btn, std::vector<MultiInputMapping> *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);

View file

@ -222,8 +222,7 @@ UI::EventReturn SingleControlMapper::OnAddMouse(UI::EventParams &params) {
UI::EventReturn SingleControlMapper::OnDelete(UI::EventParams &params) {
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 &params) {
KeyMap::g_controllerMap.clear();
KeyMap::g_controllerMapGeneration++;
KeyMap::ClearAllMappings();
return UI::EVENT_DONE;
}

View file

@ -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);