Options: Extract keymapper handling from options

This commit is contained in:
Gleb Mazovetskiy
2025-01-22 23:14:45 +00:00
parent 7afdbe8fdc
commit acc335be17
6 changed files with 100 additions and 62 deletions
+4 -46
View File
@@ -1167,9 +1167,9 @@ std::vector<OptionEntryBase *> KeymapperOptions::GetEntries()
KeymapperOptions::Action::Action(std::string_view key, const char *name, const char *description, uint32_t defaultKey, std::function<void()> actionPressed, std::function<void()> actionReleased, std::function<bool()> enable, unsigned index)
: OptionEntryBase(key, OptionEntryFlags::None, name, description)
, defaultKey(defaultKey)
, actionPressed(std::move(actionPressed))
, actionReleased(std::move(actionReleased))
, defaultKey(defaultKey)
, enable(std::move(enable))
, dynamicIndex(index)
{
@@ -1278,53 +1278,11 @@ void KeymapperOptions::CommitActions()
actions.reverse();
}
void KeymapperOptions::KeyPressed(uint32_t key) const
const KeymapperOptions::Action *KeymapperOptions::findAction(uint32_t key) const
{
if (key >= SDLK_a && key <= SDLK_z) {
key -= 'a' - 'A';
}
auto it = keyIDToAction.find(key);
if (it == keyIDToAction.end())
return; // Ignore unmapped keys.
const Action &action = it->second.get();
// Check that the action can be triggered and that the chat textbox is not
// open.
if (!action.actionPressed || (action.enable && !action.enable()) || ChatFlag)
return;
action.actionPressed();
}
void KeymapperOptions::KeyReleased(SDL_Keycode key) const
{
if (key >= SDLK_a && key <= SDLK_z) {
key = static_cast<SDL_Keycode>(static_cast<Sint32>(key) - ('a' - 'A'));
}
auto it = keyIDToAction.find(key);
if (it == keyIDToAction.end())
return; // Ignore unmapped keys.
const Action &action = it->second.get();
// Check that the action can be triggered and that the chat or gold textbox is not
// open. If either of those textboxes are open, only return if the key can be used for entry into the box
if (!action.actionReleased || (action.enable && !action.enable()) || ((ChatFlag && IsTextEntryKey(key)) || (DropGoldFlag && IsNumberEntryKey(key))))
return;
action.actionReleased();
}
bool KeymapperOptions::IsTextEntryKey(SDL_Keycode vkey) const
{
return IsAnyOf(vkey, SDLK_ESCAPE, SDLK_RETURN, SDLK_KP_ENTER, SDLK_BACKSPACE, SDLK_DOWN, SDLK_UP) || (vkey >= SDLK_SPACE && vkey <= SDLK_z);
}
bool KeymapperOptions::IsNumberEntryKey(SDL_Keycode vkey) const
{
return ((vkey >= SDLK_0 && vkey <= SDLK_9) || vkey == SDLK_BACKSPACE);
if (it == keyIDToAction.end()) return nullptr;
return &it->second.get();
}
std::string_view KeymapperOptions::KeyNameForAction(std::string_view actionName) const