From b28bac89d4832b2277183e26f3952e01396ba402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 4 Aug 2024 12:17:47 +0200 Subject: [PATCH] Move `CBinds::GetKeyId` function to `IInput::FindKeyByName` --- src/engine/client/input.cpp | 20 ++++++++++++++++++++ src/engine/client/input.h | 1 + src/engine/input.h | 1 + src/game/client/components/binds.cpp | 22 +--------------------- src/game/client/components/binds.h | 2 -- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/engine/client/input.cpp b/src/engine/client/input.cpp index 8e3da5c42..5711a5119 100644 --- a/src/engine/client/input.cpp +++ b/src/engine/client/input.cpp @@ -367,6 +367,26 @@ bool CInput::KeyState(int Key) const return m_aInputState[Key]; } +int CInput::FindKeyByName(const char *pKeyName) const +{ + // check for numeric + if(pKeyName[0] == '&') + { + int Key = str_toint(pKeyName + 1); + if(Key > KEY_FIRST && Key < KEY_LAST) + return Key; // numeric + } + + // search for key + for(int Key = KEY_FIRST; Key < KEY_LAST; Key++) + { + if(str_comp_nocase(pKeyName, KeyName(Key)) == 0) + return Key; + } + + return KEY_UNKNOWN; +} + void CInput::UpdateMouseState() { const int MouseState = SDL_GetMouseState(nullptr, nullptr); diff --git a/src/engine/client/input.h b/src/engine/client/input.h index bc94ad707..908e133e1 100644 --- a/src/engine/client/input.h +++ b/src/engine/client/input.h @@ -136,6 +136,7 @@ public: bool AltIsPressed() const override { return KeyState(KEY_LALT) || KeyState(KEY_RALT); } bool KeyIsPressed(int Key) const override { return KeyState(Key); } bool KeyPress(int Key, bool CheckCounter) const override { return CheckCounter ? (m_aInputCount[Key] == m_InputCounter) : m_aInputCount[Key]; } + int FindKeyByName(const char *pKeyName) const override; size_t NumJoysticks() const override { return m_vJoysticks.size(); } CJoystick *GetJoystick(size_t Index) override { return &m_vJoysticks[Index]; } diff --git a/src/engine/input.h b/src/engine/input.h index f80a210f4..4bb6e67a7 100644 --- a/src/engine/input.h +++ b/src/engine/input.h @@ -58,6 +58,7 @@ public: virtual bool KeyIsPressed(int Key) const = 0; virtual bool KeyPress(int Key, bool CheckCounter = false) const = 0; const char *KeyName(int Key) const { return (Key >= 0 && Key < g_MaxKeys) ? g_aaKeyStrings[Key] : g_aaKeyStrings[0]; } + virtual int FindKeyByName(const char *pKeyName) const = 0; // joystick class IJoystick diff --git a/src/game/client/components/binds.cpp b/src/game/client/components/binds.cpp index 2370b8afb..8f123a1a5 100644 --- a/src/game/client/components/binds.cpp +++ b/src/game/client/components/binds.cpp @@ -376,26 +376,6 @@ void CBinds::ConUnbindAll(IConsole::IResult *pResult, void *pUserData) pBinds->UnbindAll(); } -int CBinds::GetKeyId(const char *pKeyName) -{ - // check for numeric - if(pKeyName[0] == '&') - { - int i = str_toint(pKeyName + 1); - if(i > 0 && i < KEY_LAST) - return i; // numeric - } - - // search for key - for(int i = 0; i < KEY_LAST; i++) - { - if(str_comp_nocase(pKeyName, Input()->KeyName(i)) == 0) - return i; - } - - return 0; -} - int CBinds::GetBindSlot(const char *pBindString, int *pModifierCombination) { *pModifierCombination = MODIFIER_NONE; @@ -420,7 +400,7 @@ int CBinds::GetBindSlot(const char *pBindString, int *pModifierCombination) else break; } - return GetKeyId(*pModifierCombination == MODIFIER_NONE ? aMod : pKey + 1); + return Input()->FindKeyByName(*pModifierCombination == MODIFIER_NONE ? aMod : pKey + 1); } const char *CBinds::GetModifierName(int Modifier) diff --git a/src/game/client/components/binds.h b/src/game/client/components/binds.h index 39a0b8aca..471d115f6 100644 --- a/src/game/client/components/binds.h +++ b/src/game/client/components/binds.h @@ -12,8 +12,6 @@ class IConfigManager; class CBinds : public CComponent { - int GetKeyId(const char *pKeyName); - static void ConBind(IConsole::IResult *pResult, void *pUserData); static void ConBinds(IConsole::IResult *pResult, void *pUserData); static void ConUnbind(IConsole::IResult *pResult, void *pUserData);