Move CBinds::GetKeyId function to IInput::FindKeyByName

This commit is contained in:
Robert Müller 2024-08-04 12:17:47 +02:00
parent 585615a7cd
commit b28bac89d4
5 changed files with 23 additions and 23 deletions

View file

@ -367,6 +367,26 @@ bool CInput::KeyState(int Key) const
return m_aInputState[Key]; 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() void CInput::UpdateMouseState()
{ {
const int MouseState = SDL_GetMouseState(nullptr, nullptr); const int MouseState = SDL_GetMouseState(nullptr, nullptr);

View file

@ -136,6 +136,7 @@ public:
bool AltIsPressed() const override { return KeyState(KEY_LALT) || KeyState(KEY_RALT); } bool AltIsPressed() const override { return KeyState(KEY_LALT) || KeyState(KEY_RALT); }
bool KeyIsPressed(int Key) const override { return KeyState(Key); } 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]; } 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(); } size_t NumJoysticks() const override { return m_vJoysticks.size(); }
CJoystick *GetJoystick(size_t Index) override { return &m_vJoysticks[Index]; } CJoystick *GetJoystick(size_t Index) override { return &m_vJoysticks[Index]; }

View file

@ -58,6 +58,7 @@ public:
virtual bool KeyIsPressed(int Key) const = 0; virtual bool KeyIsPressed(int Key) const = 0;
virtual bool KeyPress(int Key, bool CheckCounter = false) 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]; } 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 // joystick
class IJoystick class IJoystick

View file

@ -376,26 +376,6 @@ void CBinds::ConUnbindAll(IConsole::IResult *pResult, void *pUserData)
pBinds->UnbindAll(); 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) int CBinds::GetBindSlot(const char *pBindString, int *pModifierCombination)
{ {
*pModifierCombination = MODIFIER_NONE; *pModifierCombination = MODIFIER_NONE;
@ -420,7 +400,7 @@ int CBinds::GetBindSlot(const char *pBindString, int *pModifierCombination)
else else
break; break;
} }
return GetKeyId(*pModifierCombination == MODIFIER_NONE ? aMod : pKey + 1); return Input()->FindKeyByName(*pModifierCombination == MODIFIER_NONE ? aMod : pKey + 1);
} }
const char *CBinds::GetModifierName(int Modifier) const char *CBinds::GetModifierName(int Modifier)

View file

@ -12,8 +12,6 @@ class IConfigManager;
class CBinds : public CComponent class CBinds : public CComponent
{ {
int GetKeyId(const char *pKeyName);
static void ConBind(IConsole::IResult *pResult, void *pUserData); static void ConBind(IConsole::IResult *pResult, void *pUserData);
static void ConBinds(IConsole::IResult *pResult, void *pUserData); static void ConBinds(IConsole::IResult *pResult, void *pUserData);
static void ConUnbind(IConsole::IResult *pResult, void *pUserData); static void ConUnbind(IConsole::IResult *pResult, void *pUserData);