2675: support multiple modifiers for binds r=def- a=plsplsplslol



Co-authored-by: plsplsplslol <alpootagpro@gmail.com>
This commit is contained in:
bors[bot] 2020-09-15 08:37:38 +00:00 committed by GitHub
commit 981d5d4e99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 41 deletions

View file

@ -12,7 +12,7 @@ bool CBinds::CBindsSpecial::OnInput(IInput::CEvent Event)
int Mask = m_pBinds->GetModifierMask(Input());
bool ret = false;
for(int Mod = 0; Mod < MODIFIER_COUNT; Mod++)
for(int Mod = 0; Mod < MODIFIER_COMBINATION_COUNT; Mod++)
{
if(Mask&(1 << Mod) && m_pBinds->m_aapKeyBindings[Mod][Event.m_Key])
m_pBinds->GetConsole()->ExecuteLineStroked(Event.m_Flags&IInput::FLAG_PRESS, m_pBinds->m_aapKeyBindings[Mod][Event.m_Key]);
@ -33,7 +33,7 @@ CBinds::CBinds()
CBinds::~CBinds()
{
for(int i = 0; i < KEY_LAST; i++)
for(int j = 0; j < MODIFIER_COUNT; j++)
for(int j = 0; j < MODIFIER_COMBINATION_COUNT; j++)
if(m_aapKeyBindings[j][i])
free(m_aapKeyBindings[j][i]);
}
@ -66,10 +66,7 @@ void CBinds::Bind(int KeyID, const char *pStr, bool FreeOnly, int Modifier)
int Size = str_length(pStr) + 1;
m_aapKeyBindings[Modifier][KeyID] = (char *)malloc(Size);
str_copy(m_aapKeyBindings[Modifier][KeyID], pStr, Size);
if(Modifier)
str_format(aBuf, sizeof(aBuf), "bound %s+%s (%d) = %s", GetModifierName(Modifier), Input()->KeyName(KeyID), KeyID, m_aapKeyBindings[Modifier][KeyID]);
else
str_format(aBuf, sizeof(aBuf), "bound %s (%d) = %s", Input()->KeyName(KeyID), KeyID, m_aapKeyBindings[Modifier][KeyID]);
str_format(aBuf, sizeof(aBuf), "bound %s%s (%d) = %s", GetKeyBindModifiersName(Modifier), Input()->KeyName(KeyID), KeyID, m_aapKeyBindings[Modifier][KeyID]);
}
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "binds", aBuf);
}
@ -133,17 +130,10 @@ bool CBinds::OnInput(IInput::CEvent e)
if(!Mask)
Mask = 1 << MODIFIER_NONE;
if(Mask == ((1 << MODIFIER_CTRL) | (1 << MODIFIER_SHIFT)))
{
if(m_aapKeyBindings[0][e.m_Key] && e.m_Flags&IInput::FLAG_RELEASE)
Console()->ExecuteLineStroked(0, m_aapKeyBindings[0][e.m_Key]);
return true;
}
bool ret = false;
for(int Mod = 1; Mod < MODIFIER_COUNT; Mod++)
for(int Mod = 1; Mod < MODIFIER_COMBINATION_COUNT; Mod++)
{
if(m_aapKeyBindings[Mod][e.m_Key] && Mask & (1 << Mod)) // always trigger +xxx binds despite any modifier
if(m_aapKeyBindings[Mod][e.m_Key] && (Mask == Mod))
{
if(e.m_Flags&IInput::FLAG_PRESS)
Console()->ExecuteLineStroked(1, m_aapKeyBindings[Mod][e.m_Key]);
@ -153,7 +143,7 @@ bool CBinds::OnInput(IInput::CEvent e)
}
}
if(m_aapKeyBindings[0][e.m_Key] && (!ret || m_aapKeyBindings[0][e.m_Key][0] == '+'))
if(m_aapKeyBindings[0][e.m_Key] && (!ret || m_aapKeyBindings[0][e.m_Key][0] == '+')) // always trigger +xxx binds despite any modifier
{
if(e.m_Flags&IInput::FLAG_PRESS)
Console()->ExecuteLineStroked(1, m_aapKeyBindings[0][e.m_Key]);
@ -167,7 +157,7 @@ bool CBinds::OnInput(IInput::CEvent e)
void CBinds::UnbindAll()
{
for(int i = 0; i < MODIFIER_COUNT; i++)
for(int i = 0; i < MODIFIER_COMBINATION_COUNT; i++)
{
for(int j = 0; j < KEY_LAST; j++)
{
@ -188,7 +178,7 @@ const char *CBinds::Get(int KeyID, int Modifier)
void CBinds::GetKey(const char *pBindStr, char *aBuf, unsigned BufSize)
{
aBuf[0] = 0;
for(int Mod = 0; Mod < MODIFIER_COUNT; Mod++)
for(int Mod = 0; Mod < MODIFIER_COMBINATION_COUNT; Mod++)
{
for(int KeyId = 0; KeyId < KEY_LAST; KeyId++)
{
@ -196,7 +186,8 @@ void CBinds::GetKey(const char *pBindStr, char *aBuf, unsigned BufSize)
if(!pBind[0])
continue;
if(str_comp(pBind, pBindStr) == 0){
if(str_comp(pBind, pBindStr) == 0)
{
if(Mod)
str_format(aBuf, BufSize, "%s+%s", GetModifierName(Mod), Input()->KeyName(KeyId));
else
@ -317,16 +308,14 @@ void CBinds::ConDumpBinds(IConsole::IResult *pResult, void *pUserData)
else if(pResult->NumArguments() == 0)
{
char aBuf[1024];
for(int i = 0; i < MODIFIER_COUNT; i++)
for(int i = 0; i < MODIFIER_COMBINATION_COUNT; i++)
{
for(int j = 0; j < KEY_LAST; j++)
{
if(!pBinds->m_aapKeyBindings[i][j])
continue;
if(i)
str_format(aBuf, sizeof(aBuf), "%s+%s (%d) = %s", pBinds->GetModifierName(i), pBinds->Input()->KeyName(j), j, pBinds->m_aapKeyBindings[i][j]);
else
str_format(aBuf, sizeof(aBuf), "%s (%d) = %s", pBinds->Input()->KeyName(j), j, pBinds->m_aapKeyBindings[i][j]);
str_format(aBuf, sizeof(aBuf), "%s%s (%d) = %s", GetKeyBindModifiersName(i), pBinds->Input()->KeyName(j), j, pBinds->m_aapKeyBindings[i][j]);
pBinds->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "binds", aBuf);
}
}
@ -380,20 +369,25 @@ int CBinds::GetKeyID(const char *pKeyName)
int CBinds::GetBindSlot(const char *pBindString, int *Mod)
{
*Mod = MODIFIER_NONE;
char aMod[32] = { 0 };
char aMod[32];
aMod[0] = '\0';
const char *pKey = str_next_token(pBindString, "+", aMod, sizeof(aMod));
if(aMod[0] && *(pKey))
while(aMod[0] && *(pKey))
{
if(!str_comp(aMod, "shift"))
*Mod = MODIFIER_SHIFT;
else if(!str_comp(aMod, "ctrl"))
*Mod = MODIFIER_CTRL;
else if(!str_comp(aMod, "alt"))
*Mod = MODIFIER_ALT;
if (!str_comp(aMod, "shift"))
*Mod |= (1 << MODIFIER_SHIFT);
else if (!str_comp(aMod, "ctrl"))
*Mod |= (1 << MODIFIER_CTRL);
else if (!str_comp(aMod, "alt"))
*Mod |= (1 << MODIFIER_ALT);
else
return 0;
}
if (str_find(pKey + 1, "+"))
pKey = str_next_token(pKey + 1, "+", aMod, sizeof(aMod));
else
break;
}
return GetKeyID(*Mod == MODIFIER_NONE ? aMod : pKey + 1);
}
@ -413,6 +407,22 @@ const char *CBinds::GetModifierName(int Modifier)
}
}
const char *CBinds::GetKeyBindModifiersName(int Modifier)
{
static char aModifier[256];
aModifier[0] = '\0';
for (int k = 1; k < MODIFIER_COUNT; k++)
{
if (Modifier & (1 << k))
{
str_append(aModifier, GetModifierName(k), sizeof(aModifier));
str_append(aModifier, "+", sizeof(aModifier));
}
}
return aModifier;
}
void CBinds::ConfigSaveCallback(IConfig *pConfig, void *pUserData)
{
CBinds *pSelf = (CBinds *)pUserData;
@ -420,17 +430,14 @@ void CBinds::ConfigSaveCallback(IConfig *pConfig, void *pUserData)
char aBuffer[256];
char *pEnd = aBuffer+sizeof(aBuffer);
pConfig->WriteLine("unbindall");
for(int i = 0; i < MODIFIER_COUNT; i++)
for(int i = 0; i < MODIFIER_COMBINATION_COUNT; i++)
{
for(int j = 0; j < KEY_LAST; j++)
{
if(!pSelf->m_aapKeyBindings[i][j])
continue;
if(i)
str_format(aBuffer, sizeof(aBuffer), "bind %s+%s \"", pSelf->GetModifierName(i), pSelf->Input()->KeyName(j));
else
str_format(aBuffer, sizeof(aBuffer), "bind %s \"", pSelf->Input()->KeyName(j));
str_format(aBuffer, sizeof(aBuffer), "bind %s%s \"", GetKeyBindModifiersName(i), pSelf->Input()->KeyName(j));
// process the string. we need to escape some characters
char *pDst = aBuffer + str_length(aBuffer);
str_escape(&pDst, pSelf->m_aapKeyBindings[i][j], pEnd);

View file

@ -29,11 +29,12 @@ public:
};
enum {
MODIFIER_NONE=0,
MODIFIER_NONE = 0,
MODIFIER_SHIFT,
MODIFIER_CTRL,
MODIFIER_ALT,
MODIFIER_COUNT
MODIFIER_COUNT,
MODIFIER_COMBINATION_COUNT = 1 << MODIFIER_COUNT
};
CBindsSpecial m_SpecialBinds;
@ -48,6 +49,7 @@ public:
static int GetModifierMaskOfKey(int Key);
static bool ModifierMatchesKey(int Modifier, int Key);
static const char *GetModifierName(int Modifier);
static const char *GetKeyBindModifiersName(int Modifier);
virtual void OnConsoleInit();
virtual bool OnInput(IInput::CEvent Event);
@ -57,6 +59,6 @@ public:
void SetDDRaceBinds(bool FreeOnly);
private:
char *m_aapKeyBindings[MODIFIER_COUNT][KEY_LAST];
char *m_aapKeyBindings[MODIFIER_COMBINATION_COUNT][KEY_LAST];
};
#endif