mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-19 14:38:18 +00:00
Merge pull request #1589 from Dune-jr/feature-complexbinds
Implement composed binds (Ctrl, Shift, Alt); adapt controls menus.
This commit is contained in:
commit
bb544ca3ae
|
@ -4,7 +4,7 @@
|
|||
#include <engine/shared/config.h>
|
||||
#include "binds.h"
|
||||
|
||||
const int CBinds::s_aDefaultBindKeys[] = {
|
||||
const int CBinds::s_aDefaultBindKeys[] = { // only simple binds
|
||||
KEY_F1, KEY_F2, KEY_TAB, 'u', KEY_F10,
|
||||
'a', 'd',
|
||||
KEY_SPACE, KEY_MOUSE_1, KEY_MOUSE_2, KEY_LSHIFT, KEY_RSHIFT, KEY_RIGHT, KEY_LEFT,
|
||||
|
@ -25,82 +25,156 @@ const char CBinds::s_aaDefaultBindValues[][32] = {
|
|||
"ready_change",
|
||||
};
|
||||
|
||||
bool CBinds::CBindsSpecial::OnInput(IInput::CEvent Event)
|
||||
{
|
||||
// don't handle invalid events and keys that arn't set to anything
|
||||
if(((Event.m_Key >= KEY_F1 && Event.m_Key <= KEY_F12) || (Event.m_Key >= KEY_F13 && Event.m_Key <= KEY_F24)) && m_pBinds->m_aaKeyBindings[Event.m_Key][0] != 0)
|
||||
{
|
||||
int Stroke = 0;
|
||||
if(Event.m_Flags&IInput::FLAG_PRESS)
|
||||
Stroke = 1;
|
||||
|
||||
m_pBinds->GetConsole()->ExecuteLineStroked(Stroke, m_pBinds->m_aaKeyBindings[Event.m_Key]);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
CBinds::CBinds()
|
||||
{
|
||||
mem_zero(m_aaKeyBindings, sizeof(m_aaKeyBindings));
|
||||
mem_zero(m_aaaKeyBindings, sizeof(m_aaaKeyBindings));
|
||||
m_SpecialBinds.m_pBinds = this;
|
||||
}
|
||||
|
||||
void CBinds::Bind(int KeyID, const char *pStr)
|
||||
void CBinds::Bind(int KeyID, int Modifier, const char *pStr)
|
||||
{
|
||||
if(KeyID < 0 || KeyID >= KEY_LAST)
|
||||
return;
|
||||
|
||||
str_copy(m_aaKeyBindings[KeyID], pStr, sizeof(m_aaKeyBindings[KeyID]));
|
||||
str_copy(m_aaaKeyBindings[KeyID][Modifier], pStr, sizeof(m_aaaKeyBindings[KeyID][Modifier]));
|
||||
char aBuf[256];
|
||||
if(!m_aaKeyBindings[KeyID][0])
|
||||
str_format(aBuf, sizeof(aBuf), "unbound %s (%d)", Input()->KeyName(KeyID), KeyID);
|
||||
if(!m_aaaKeyBindings[KeyID][Modifier][0])
|
||||
str_format(aBuf, sizeof(aBuf), "unbound %s%s (%d)", GetModifierName(Modifier),Input()->KeyName(KeyID), KeyID);
|
||||
else
|
||||
str_format(aBuf, sizeof(aBuf), "bound %s (%d) = %s", Input()->KeyName(KeyID), KeyID, m_aaKeyBindings[KeyID]);
|
||||
str_format(aBuf, sizeof(aBuf), "bound %s%s (%d) = %s", GetModifierName(Modifier),Input()->KeyName(KeyID), KeyID, m_aaaKeyBindings[KeyID][Modifier]);
|
||||
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "binds", aBuf);
|
||||
}
|
||||
|
||||
|
||||
bool CBinds::OnInput(IInput::CEvent e)
|
||||
int CBinds::GetModifierMask(IInput *i)
|
||||
{
|
||||
// don't handle invalid events and keys that arn't set to anything
|
||||
if(e.m_Key <= 0 || e.m_Key >= KEY_LAST || m_aaKeyBindings[e.m_Key][0] == 0)
|
||||
int Mask = 0;
|
||||
// since we only handle one modifier, when doing ctrl+q and shift+q, execute both
|
||||
Mask |= i->KeyIsPressed(KEY_LSHIFT) << CBinds::MODIFIER_SHIFT;
|
||||
Mask |= i->KeyIsPressed(KEY_RSHIFT) << CBinds::MODIFIER_SHIFT;
|
||||
Mask |= i->KeyIsPressed(KEY_LCTRL) << CBinds::MODIFIER_CTRL;
|
||||
Mask |= i->KeyIsPressed(KEY_RCTRL) << CBinds::MODIFIER_CTRL;
|
||||
Mask |= i->KeyIsPressed(KEY_LALT) << CBinds::MODIFIER_ALT;
|
||||
if(Mask == 0)
|
||||
return 1; // if no modifier, flag with MODIFIER_NONE
|
||||
return Mask;
|
||||
}
|
||||
|
||||
int CBinds::GetModifierMaskOfKey(int Key)
|
||||
{
|
||||
switch(Key)
|
||||
{
|
||||
case KEY_LSHIFT:
|
||||
case KEY_RSHIFT:
|
||||
return 1 << CBinds::MODIFIER_SHIFT;
|
||||
case KEY_LCTRL:
|
||||
case KEY_RCTRL:
|
||||
return 1 << CBinds::MODIFIER_CTRL;
|
||||
case KEY_LALT:
|
||||
return 1 << CBinds::MODIFIER_ALT;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool CBinds::ModifierMatchesKey(int Modifier, int Key)
|
||||
{
|
||||
switch(Modifier)
|
||||
{
|
||||
case MODIFIER_SHIFT:
|
||||
return Key == KEY_LSHIFT || Key == KEY_RSHIFT;
|
||||
case MODIFIER_CTRL:
|
||||
return Key == KEY_LCTRL || Key == KEY_RCTRL;
|
||||
case MODIFIER_ALT:
|
||||
return Key == KEY_LALT;
|
||||
case MODIFIER_NONE:
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool CBinds::CBindsSpecial::OnInput(IInput::CEvent Event)
|
||||
{
|
||||
int Mask = GetModifierMask(Input());
|
||||
|
||||
// don't handle anything but FX and composed FX binds
|
||||
if(/*Mask == 1 && */!(Event.m_Key >= KEY_F1 && Event.m_Key <= KEY_F12) && !(Event.m_Key >= KEY_F13 && Event.m_Key <= KEY_F24))
|
||||
return false;
|
||||
|
||||
if(e.m_Flags&IInput::FLAG_PRESS)
|
||||
Console()->ExecuteLineStroked(1, m_aaKeyBindings[e.m_Key]);
|
||||
if(e.m_Flags&IInput::FLAG_RELEASE)
|
||||
Console()->ExecuteLineStroked(0, m_aaKeyBindings[e.m_Key]);
|
||||
return true;
|
||||
bool rtn = false;
|
||||
for(int m = 0; m < MODIFIER_COUNT; m++)
|
||||
{
|
||||
if((Mask&(1 << m)) && m_pBinds->m_aaaKeyBindings[Event.m_Key][m][0] != 0)
|
||||
{
|
||||
if(Event.m_Flags&IInput::FLAG_PRESS)
|
||||
m_pBinds->GetConsole()->ExecuteLineStroked(1, m_pBinds->m_aaaKeyBindings[Event.m_Key][m]);
|
||||
if(Event.m_Flags&IInput::FLAG_RELEASE)
|
||||
m_pBinds->GetConsole()->ExecuteLineStroked(0, m_pBinds->m_aaaKeyBindings[Event.m_Key][m]);
|
||||
rtn = true;
|
||||
}
|
||||
}
|
||||
return rtn;
|
||||
}
|
||||
|
||||
bool CBinds::OnInput(IInput::CEvent Event)
|
||||
{
|
||||
// don't handle invalid events and keys that aren't set to anything
|
||||
if(Event.m_Key <= 0 || Event.m_Key >= KEY_LAST)
|
||||
return false;
|
||||
|
||||
int Mask = GetModifierMask(Input());
|
||||
int KeyModifierMask = GetModifierMaskOfKey(Event.m_Key); // returns 0 if m_Key is not a modifier
|
||||
if(KeyModifierMask)
|
||||
{
|
||||
// avoid to have e.g. key press "lshift" be treated as "shift+lshift"
|
||||
Mask -= KeyModifierMask;
|
||||
if(!Mask)
|
||||
Mask = 1 << MODIFIER_NONE;
|
||||
}
|
||||
|
||||
bool rtn = false;
|
||||
for(int m = 0; m < MODIFIER_COUNT; m++)
|
||||
{
|
||||
if((Mask&(1 << m)) && m_aaaKeyBindings[Event.m_Key][m][0] != 0)
|
||||
{
|
||||
if(Event.m_Flags&IInput::FLAG_PRESS)
|
||||
Console()->ExecuteLineStroked(1, m_aaaKeyBindings[Event.m_Key][m]);
|
||||
if(Event.m_Flags&IInput::FLAG_RELEASE)
|
||||
Console()->ExecuteLineStroked(0, m_aaaKeyBindings[Event.m_Key][m]);
|
||||
rtn = true;
|
||||
}
|
||||
}
|
||||
return rtn;
|
||||
}
|
||||
|
||||
void CBinds::UnbindAll()
|
||||
{
|
||||
for(int i = 0; i < KEY_LAST; i++)
|
||||
m_aaKeyBindings[i][0] = 0;
|
||||
for(int m = 0; m < MODIFIER_COUNT; m++)
|
||||
m_aaaKeyBindings[i][m][0] = 0;
|
||||
}
|
||||
|
||||
const char *CBinds::Get(int KeyID)
|
||||
const char *CBinds::Get(int KeyID, int Modifier)
|
||||
{
|
||||
if(KeyID > 0 && KeyID < KEY_LAST)
|
||||
return m_aaKeyBindings[KeyID];
|
||||
return m_aaaKeyBindings[KeyID][Modifier];
|
||||
return "";
|
||||
}
|
||||
|
||||
const char *CBinds::GetKey(const char *pBindStr)
|
||||
void CBinds::GetKey(const char *pBindStr, char aBuf[64], unsigned BufSize)
|
||||
{
|
||||
for(int KeyId = 0; KeyId < KEY_LAST; KeyId++)
|
||||
aBuf[0] = 0;
|
||||
for(int KeyID = 0; KeyID < KEY_LAST; KeyID++)
|
||||
{
|
||||
const char *pBind = Get(KeyId);
|
||||
if(!pBind[0])
|
||||
continue;
|
||||
for(int m = 0; m < MODIFIER_COUNT; m++)
|
||||
{
|
||||
const char *pBind = Get(KeyID, m);
|
||||
if(!pBind[0])
|
||||
continue;
|
||||
|
||||
if(str_comp(pBind, pBindStr) == 0)
|
||||
return Input()->KeyName(KeyId);
|
||||
if(str_comp(pBind, pBindStr) == 0)
|
||||
str_format(aBuf, BufSize, "key %s%s not found", Input()->KeyName(KeyID), GetModifierName(m));
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
void CBinds::SetDefaults()
|
||||
|
@ -110,7 +184,7 @@ void CBinds::SetDefaults()
|
|||
const int count = sizeof(s_aDefaultBindKeys)/sizeof(int);
|
||||
dbg_assert(count == sizeof(s_aaDefaultBindValues)/32, "the count of bind keys differs from that of bind values!");
|
||||
for(int i = 0; i < count; i++)
|
||||
Bind(s_aDefaultBindKeys[i], s_aaDefaultBindValues[i]);
|
||||
Bind(s_aDefaultBindKeys[i], MODIFIER_NONE, s_aaDefaultBindValues[i]);
|
||||
}
|
||||
|
||||
void CBinds::OnConsoleInit()
|
||||
|
@ -133,7 +207,8 @@ void CBinds::ConBind(IConsole::IResult *pResult, void *pUserData)
|
|||
{
|
||||
CBinds *pBinds = (CBinds *)pUserData;
|
||||
const char *pKeyName = pResult->GetString(0);
|
||||
int id = pBinds->GetKeyID(pKeyName);
|
||||
int Modifier;
|
||||
int id = pBinds->DecodeBindString(pKeyName, &Modifier);
|
||||
|
||||
if(!id)
|
||||
{
|
||||
|
@ -143,7 +218,7 @@ void CBinds::ConBind(IConsole::IResult *pResult, void *pUserData)
|
|||
return;
|
||||
}
|
||||
|
||||
pBinds->Bind(id, pResult->GetString(1));
|
||||
pBinds->Bind(id, Modifier, pResult->GetString(1));
|
||||
}
|
||||
|
||||
|
||||
|
@ -151,7 +226,8 @@ void CBinds::ConUnbind(IConsole::IResult *pResult, void *pUserData)
|
|||
{
|
||||
CBinds *pBinds = (CBinds *)pUserData;
|
||||
const char *pKeyName = pResult->GetString(0);
|
||||
int id = pBinds->GetKeyID(pKeyName);
|
||||
int Modifier;
|
||||
int id = pBinds->DecodeBindString(pKeyName, &Modifier);
|
||||
|
||||
if(!id)
|
||||
{
|
||||
|
@ -161,7 +237,7 @@ void CBinds::ConUnbind(IConsole::IResult *pResult, void *pUserData)
|
|||
return;
|
||||
}
|
||||
|
||||
pBinds->Bind(id, "");
|
||||
pBinds->Bind(id, Modifier, "");
|
||||
}
|
||||
|
||||
|
||||
|
@ -178,15 +254,40 @@ void CBinds::ConDumpBinds(IConsole::IResult *pResult, void *pUserData)
|
|||
char aBuf[1024];
|
||||
for(int i = 0; i < KEY_LAST; i++)
|
||||
{
|
||||
if(pBinds->m_aaKeyBindings[i][0] == 0)
|
||||
continue;
|
||||
str_format(aBuf, sizeof(aBuf), "%s (%d) = %s", pBinds->Input()->KeyName(i), i, pBinds->m_aaKeyBindings[i]);
|
||||
pBinds->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "binds", aBuf);
|
||||
for(int m = 0; m < MODIFIER_COUNT; m++)
|
||||
{
|
||||
if(pBinds->m_aaaKeyBindings[i][m][0] == 0)
|
||||
continue;
|
||||
str_format(aBuf, sizeof(aBuf), "%s%s (%d) = %s", GetModifierName(m), pBinds->Input()->KeyName(i), i, pBinds->m_aaaKeyBindings[i][m]);
|
||||
pBinds->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "binds", aBuf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int CBinds::GetKeyID(const char *pKeyName)
|
||||
int CBinds::DecodeBindString(const char *pKeyName, int* pModifier)
|
||||
{
|
||||
// check for modifier of type xxx+xxx
|
||||
char aBuf[64];
|
||||
str_copy(aBuf, pKeyName, sizeof(aBuf));
|
||||
const char* pStr = str_find(aBuf, "+");
|
||||
*pModifier = 0;
|
||||
if(pStr && *(pStr+1)) // make sure the '+' isn't the last character
|
||||
{
|
||||
aBuf[pStr-aBuf] = 0; // *pStr=0 (split the string where the + is)
|
||||
char* pModifierStr = aBuf;
|
||||
if(str_comp(pModifierStr, "shift") == 0)
|
||||
*pModifier = MODIFIER_SHIFT;
|
||||
else if(str_comp(pModifierStr, "ctrl") == 0)
|
||||
*pModifier = MODIFIER_CTRL;
|
||||
else if(str_comp(pModifierStr, "alt") == 0)
|
||||
*pModifier = MODIFIER_ALT;
|
||||
else
|
||||
return 0;
|
||||
pKeyName = pStr + 1;
|
||||
if(!pKeyName)
|
||||
return 0;
|
||||
}
|
||||
|
||||
// check for numeric
|
||||
if(pKeyName[0] == '&')
|
||||
{
|
||||
|
@ -205,6 +306,24 @@ int CBinds::GetKeyID(const char *pKeyName)
|
|||
return 0;
|
||||
}
|
||||
|
||||
|
||||
const char *CBinds::GetModifierName(int m)
|
||||
{
|
||||
switch(m)
|
||||
{
|
||||
case 0:
|
||||
return "";
|
||||
case MODIFIER_SHIFT:
|
||||
return "shift+";
|
||||
case MODIFIER_CTRL:
|
||||
return "ctrl+";
|
||||
case MODIFIER_ALT:
|
||||
return "alt+";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
void CBinds::ConfigSaveCallback(IConfig *pConfig, void *pUserData)
|
||||
{
|
||||
CBinds *pSelf = (CBinds *)pUserData;
|
||||
|
@ -213,31 +332,35 @@ void CBinds::ConfigSaveCallback(IConfig *pConfig, void *pUserData)
|
|||
char *pEnd = aBuffer+sizeof(aBuffer)-8;
|
||||
for(int i = 0; i < KEY_LAST; i++)
|
||||
{
|
||||
if(pSelf->m_aaKeyBindings[i][0] == 0)
|
||||
continue;
|
||||
|
||||
str_format(aBuffer, sizeof(aBuffer), "bind %s ", pSelf->Input()->KeyName(i));
|
||||
|
||||
// process the string. we need to escape some characters
|
||||
const char *pSrc = pSelf->m_aaKeyBindings[i];
|
||||
char *pDst = aBuffer + str_length(aBuffer);
|
||||
*pDst++ = '"';
|
||||
while(*pSrc && pDst < pEnd)
|
||||
for(int m = 0; m < MODIFIER_COUNT; m++)
|
||||
{
|
||||
if(*pSrc == '"' || *pSrc == '\\') // escape \ and "
|
||||
*pDst++ = '\\';
|
||||
*pDst++ = *pSrc++;
|
||||
}
|
||||
*pDst++ = '"';
|
||||
*pDst++ = 0;
|
||||
if(pSelf->m_aaaKeyBindings[i][m][0] == 0)
|
||||
continue;
|
||||
|
||||
pConfig->WriteLine(aBuffer);
|
||||
str_format(aBuffer, sizeof(aBuffer), "bind %s%s ", GetModifierName(m), pSelf->Input()->KeyName(i));
|
||||
|
||||
// process the string. we need to escape some characters
|
||||
const char *pSrc = pSelf->m_aaaKeyBindings[i][m];
|
||||
char *pDst = aBuffer + str_length(aBuffer);
|
||||
*pDst++ = '"';
|
||||
while(*pSrc && pDst < pEnd)
|
||||
{
|
||||
if(*pSrc == '"' || *pSrc == '\\') // escape \ and "
|
||||
*pDst++ = '\\';
|
||||
*pDst++ = *pSrc++;
|
||||
}
|
||||
*pDst++ = '"';
|
||||
*pDst++ = 0;
|
||||
|
||||
pConfig->WriteLine(aBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
// default binds can only be non-composed right now, so only check for that
|
||||
for(unsigned j = 0; j < sizeof(s_aDefaultBindKeys)/sizeof(int); j++)
|
||||
{
|
||||
const int i = s_aDefaultBindKeys[j];
|
||||
if(pSelf->m_aaKeyBindings[i][0] == 0)
|
||||
if(pSelf->m_aaaKeyBindings[i][0][0] == 0)
|
||||
{
|
||||
// explicitly unbind keys that were unbound by the user
|
||||
str_format(aBuffer, sizeof(aBuffer), "unbind %s ", pSelf->Input()->KeyName(i));
|
||||
|
|
|
@ -7,9 +7,7 @@
|
|||
|
||||
class CBinds : public CComponent
|
||||
{
|
||||
char m_aaKeyBindings[KEY_LAST][128];
|
||||
|
||||
int GetKeyID(const char *pKeyName);
|
||||
int DecodeBindString(const char *pKeyName, int* pModifier);
|
||||
|
||||
static void ConBind(IConsole::IResult *pResult, void *pUserData);
|
||||
static void ConUnbind(IConsole::IResult *pResult, void *pUserData);
|
||||
|
@ -29,18 +27,32 @@ public:
|
|||
virtual bool OnInput(IInput::CEvent Event);
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
MODIFIER_NONE=0,
|
||||
MODIFIER_SHIFT,
|
||||
MODIFIER_CTRL,
|
||||
MODIFIER_ALT,
|
||||
MODIFIER_COUNT
|
||||
};
|
||||
|
||||
CBindsSpecial m_SpecialBinds;
|
||||
|
||||
void Bind(int KeyID, const char *pStr);
|
||||
void Bind(int KeyID, int Modifier, const char *pStr);
|
||||
void SetDefaults();
|
||||
void UnbindAll();
|
||||
const char *Get(int KeyID);
|
||||
const char *GetKey(const char *pBindStr);
|
||||
const char *Get(int KeyID, int Modifier);
|
||||
void GetKey(const char *pBindStr, char aBuf[64], unsigned BufSize);
|
||||
static const char *GetModifierName(int m);
|
||||
static int GetModifierMask(IInput *i);
|
||||
static int GetModifierMaskOfKey(int Key);
|
||||
static bool ModifierMatchesKey(int Modifier, int Key);
|
||||
|
||||
virtual void OnConsoleInit();
|
||||
virtual bool OnInput(IInput::CEvent Event);
|
||||
|
||||
private:
|
||||
private:
|
||||
char m_aaaKeyBindings[KEY_LAST][MODIFIER_COUNT][128];
|
||||
static const int s_aDefaultBindKeys[];
|
||||
static const char s_aaDefaultBindValues[][32];
|
||||
};
|
||||
|
|
|
@ -450,13 +450,14 @@ void CHud::RenderVoting()
|
|||
CUIRect Base = {5, 88, 100, 4};
|
||||
m_pClient->m_pVoting->RenderBars(Base, false);
|
||||
|
||||
const char *pYesKey = m_pClient->m_pBinds->GetKey("vote yes");
|
||||
const char *pNoKey = m_pClient->m_pBinds->GetKey("vote no");
|
||||
str_format(aBuf, sizeof(aBuf), "%s - %s", pYesKey, Localize("Vote yes"));
|
||||
char aBufYes[64], aBufNo[64];
|
||||
m_pClient->m_pBinds->GetKey("vote yes", aBufYes, sizeof(aBufYes));
|
||||
m_pClient->m_pBinds->GetKey("vote no", aBufNo, sizeof(aBufNo));
|
||||
str_format(aBuf, sizeof(aBuf), "%s - %s", aBufYes, Localize("Vote yes"));
|
||||
Base.y += Base.h+1;
|
||||
UI()->DoLabel(&Base, aBuf, 6.0f, CUI::ALIGN_LEFT);
|
||||
|
||||
str_format(aBuf, sizeof(aBuf), "%s - %s", Localize("Vote no"), pNoKey);
|
||||
str_format(aBuf, sizeof(aBuf), "%s - %s", Localize("Vote no"), aBufNo);
|
||||
UI()->DoLabel(&Base, aBuf, 6.0f, CUI::ALIGN_RIGHT);
|
||||
}
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include <generated/protocol.h>
|
||||
|
||||
#include <generated/client_data.h>
|
||||
#include <game/client/components/binds.h>
|
||||
#include <game/client/components/camera.h>
|
||||
#include <game/client/components/console.h>
|
||||
#include <game/client/components/sounds.h>
|
||||
|
@ -1021,7 +1022,7 @@ int CMenus::UiDoListboxEnd(CListBoxState* pState, bool *pItemActivated)
|
|||
return pState->m_ListBoxNewSelected;
|
||||
}
|
||||
|
||||
int CMenus::DoKeyReader(CButtonContainer *pBC, const CUIRect *pRect, int Key)
|
||||
int CMenus::DoKeyReader(CButtonContainer *pBC, const CUIRect *pRect, int Key, int Modifier, int* NewModifier)
|
||||
{
|
||||
// process
|
||||
static const void *pGrabbedID = 0;
|
||||
|
@ -1029,6 +1030,7 @@ int CMenus::DoKeyReader(CButtonContainer *pBC, const CUIRect *pRect, int Key)
|
|||
static int ButtonUsed = 0;
|
||||
int Inside = UI()->MouseInside(pRect) && UI()->MouseInsideClip();
|
||||
int NewKey = Key;
|
||||
*NewModifier = Modifier;
|
||||
|
||||
if(!UI()->MouseButton(0) && !UI()->MouseButton(1) && pGrabbedID == pBC->GetID())
|
||||
MouseReleased = true;
|
||||
|
@ -1039,7 +1041,10 @@ int CMenus::DoKeyReader(CButtonContainer *pBC, const CUIRect *pRect, int Key)
|
|||
{
|
||||
// abort with escape key
|
||||
if(m_Binder.m_Key.m_Key != KEY_ESCAPE)
|
||||
{
|
||||
NewKey = m_Binder.m_Key.m_Key;
|
||||
*NewModifier = m_Binder.m_Modifier;
|
||||
}
|
||||
m_Binder.m_GotKey = false;
|
||||
UI()->SetActiveItem(0);
|
||||
MouseReleased = false;
|
||||
|
@ -1084,7 +1089,11 @@ int CMenus::DoKeyReader(CButtonContainer *pBC, const CUIRect *pRect, int Key)
|
|||
if(Key == 0)
|
||||
DoButton_KeySelect(pBC, "", 0, pRect);
|
||||
else
|
||||
DoButton_KeySelect(pBC, Input()->KeyName(Key), 0, pRect);
|
||||
{
|
||||
char aBuf[64];
|
||||
str_format(aBuf, sizeof(aBuf), "%s%s", CBinds::GetModifierName(*NewModifier), Input()->KeyName(Key));
|
||||
DoButton_KeySelect(pBC, aBuf, 0, pRect);
|
||||
}
|
||||
}
|
||||
return NewKey;
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ class CMenusKeyBinder : public CComponent
|
|||
public:
|
||||
bool m_TakeKey;
|
||||
bool m_GotKey;
|
||||
int m_Modifier;
|
||||
IInput::CEvent m_Key;
|
||||
CMenusKeyBinder();
|
||||
virtual bool OnInput(IInput::CEvent Event);
|
||||
|
@ -91,7 +92,7 @@ private:
|
|||
float DoScrollbarV(const void *pID, const CUIRect *pRect, float Current);
|
||||
float DoScrollbarH(const void *pID, const CUIRect *pRect, float Current);
|
||||
void DoButton_KeySelect(CButtonContainer *pBC, const char *pText, int Checked, const CUIRect *pRect);
|
||||
int DoKeyReader(CButtonContainer *pPC, const CUIRect *pRect, int Key);
|
||||
int DoKeyReader(CButtonContainer *pPC, const CUIRect *pRect, int Key, int Modifier, int* NewModifier);
|
||||
|
||||
//static int ui_do_key_reader(void *id, const CUIRect *rect, int key);
|
||||
void UiDoGetButtons(int Start, int Stop, CUIRect View, float ButtonHeight, float Spacing);
|
||||
|
|
|
@ -10,40 +10,41 @@ typedef struct
|
|||
CLocConstString m_Name;
|
||||
const char *m_pCommand;
|
||||
int m_KeyId;
|
||||
int m_Modifier;
|
||||
CMenus::CButtonContainer m_BC;
|
||||
} CKeyInfo;
|
||||
|
||||
static CKeyInfo gs_aKeys[] =
|
||||
{
|
||||
{ "Move left", "+left", 0}, // Localize - these strings are localized within CLocConstString
|
||||
{ "Move right", "+right", 0 },
|
||||
{ "Jump", "+jump", 0 },
|
||||
{ "Fire", "+fire", 0 },
|
||||
{ "Hook", "+hook", 0 },
|
||||
{ "Hammer", "+weapon1", 0 },
|
||||
{ "Pistol", "+weapon2", 0 },
|
||||
{ "Shotgun", "+weapon3", 0 },
|
||||
{ "Grenade", "+weapon4", 0 },
|
||||
{ "Laser", "+weapon5", 0 },
|
||||
{ "Next weapon", "+nextweapon", 0 },
|
||||
{ "Prev. weapon", "+prevweapon", 0 },
|
||||
{ "Vote yes", "vote yes", 0 },
|
||||
{ "Vote no", "vote no", 0 },
|
||||
{ "Chat", "chat all", 0 },
|
||||
{ "Team chat", "chat team", 0 },
|
||||
{ "Whisper", "chat whisper", 0 },
|
||||
{ "Show chat", "+show_chat", 0 },
|
||||
{ "Emoticon", "+emote", 0 },
|
||||
{ "Spectator mode", "+spectate", 0 },
|
||||
{ "Spectate next", "spectate_next", 0 },
|
||||
{ "Spectate previous", "spectate_previous", 0 },
|
||||
{ "Console", "toggle_local_console", 0 },
|
||||
{ "Remote console", "toggle_remote_console", 0 },
|
||||
{ "Screenshot", "screenshot", 0 },
|
||||
{ "Scoreboard", "+scoreboard", 0 },
|
||||
{ "Respawn", "kill", 0 },
|
||||
{ "Ready", "ready_change", 0 },
|
||||
{ "Add demo marker", "add_demomarker", 0},
|
||||
{ "Move left", "+left", 0, 0}, // Localize - these strings are localized within CLocConstString
|
||||
{ "Move right", "+right", 0, 0},
|
||||
{ "Jump", "+jump", 0, 0},
|
||||
{ "Fire", "+fire", 0, 0},
|
||||
{ "Hook", "+hook", 0, 0},
|
||||
{ "Hammer", "+weapon1", 0, 0},
|
||||
{ "Pistol", "+weapon2", 0, 0},
|
||||
{ "Shotgun", "+weapon3", 0, 0},
|
||||
{ "Grenade", "+weapon4", 0, 0},
|
||||
{ "Laser", "+weapon5", 0, 0},
|
||||
{ "Next weapon", "+nextweapon", 0, 0},
|
||||
{ "Prev. weapon", "+prevweapon", 0, 0},
|
||||
{ "Vote yes", "vote yes", 0, 0},
|
||||
{ "Vote no", "vote no", 0, 0},
|
||||
{ "Chat", "chat all", 0, 0},
|
||||
{ "Team chat", "chat team", 0, 0},
|
||||
{ "Whisper", "chat whisper", 0, 0},
|
||||
{ "Show chat", "+show_chat", 0, 0},
|
||||
{ "Emoticon", "+emote", 0, 0},
|
||||
{ "Spectator mode", "+spectate", 0, 0},
|
||||
{ "Spectate next", "spectate_next", 0, 0},
|
||||
{ "Spectate previous", "spectate_previous", 0, 0},
|
||||
{ "Console", "toggle_local_console", 0, 0},
|
||||
{ "Remote console", "toggle_remote_console", 0, 0},
|
||||
{ "Screenshot", "screenshot", 0, 0},
|
||||
{ "Scoreboard", "+scoreboard", 0, 0},
|
||||
{ "Respawn", "kill", 0, 0},
|
||||
{ "Ready", "ready_change", 0, 0},
|
||||
{ "Add demo marker", "add_demomarker", 0, 0},
|
||||
};
|
||||
|
||||
/* This is for scripts/update_localization.py to work, don't remove!
|
||||
|
@ -74,14 +75,14 @@ void CMenus::UiDoGetButtons(int Start, int Stop, CUIRect View, float ButtonHeigh
|
|||
|
||||
Label.y += 2.0f;
|
||||
UI()->DoLabelScaled(&Label, aBuf, 13.0f, CUI::ALIGN_CENTER);
|
||||
int OldId = Key.m_KeyId;
|
||||
int NewId = DoKeyReader(&gs_aKeys[i].m_BC, &Button, OldId);
|
||||
if(NewId != OldId)
|
||||
int OldId = Key.m_KeyId, OldModifier = Key.m_Modifier, NewModifier;
|
||||
int NewId = DoKeyReader(&gs_aKeys[i].m_BC, &Button, OldId, OldModifier, &NewModifier);
|
||||
if(NewId != OldId || NewModifier != OldModifier)
|
||||
{
|
||||
if(OldId != 0 || NewId == 0)
|
||||
m_pClient->m_pBinds->Bind(OldId, "");
|
||||
m_pClient->m_pBinds->Bind(OldId, OldModifier, "");
|
||||
if(NewId != 0)
|
||||
m_pClient->m_pBinds->Bind(NewId, gs_aKeys[i].m_pCommand);
|
||||
m_pClient->m_pBinds->Bind(NewId, NewModifier, gs_aKeys[i].m_pCommand);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -92,20 +93,27 @@ float CMenus::RenderSettingsControlsMovement(CUIRect View, void *pUser)
|
|||
|
||||
// this is kinda slow, but whatever
|
||||
for(int i = 0; i < g_KeyCount; i++)
|
||||
{
|
||||
gs_aKeys[i].m_KeyId = 0;
|
||||
gs_aKeys[i].m_Modifier = 0;
|
||||
}
|
||||
|
||||
for(int KeyId = 0; KeyId < KEY_LAST; KeyId++)
|
||||
{
|
||||
const char *pBind = pSelf->m_pClient->m_pBinds->Get(KeyId);
|
||||
if(!pBind[0])
|
||||
continue;
|
||||
for(int m = 0; m < CBinds::MODIFIER_COUNT; m++)
|
||||
{
|
||||
const char *pBind = pSelf->m_pClient->m_pBinds->Get(KeyId, m);
|
||||
if(!pBind[0])
|
||||
continue;
|
||||
|
||||
for(int i = 0; i < g_KeyCount; i++)
|
||||
if(str_comp(pBind, gs_aKeys[i].m_pCommand) == 0)
|
||||
{
|
||||
gs_aKeys[i].m_KeyId = KeyId;
|
||||
break;
|
||||
}
|
||||
for(int i = 0; i < g_KeyCount; i++)
|
||||
if(str_comp(pBind, gs_aKeys[i].m_pCommand) == 0)
|
||||
{
|
||||
gs_aKeys[i].m_KeyId = KeyId;
|
||||
gs_aKeys[i].m_Modifier = m;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int NumOptions = 6;
|
||||
|
@ -132,20 +140,27 @@ float CMenus::RenderSettingsControlsWeapon(CUIRect View, void *pUser)
|
|||
|
||||
// this is kinda slow, but whatever
|
||||
for(int i = 0; i < g_KeyCount; i++)
|
||||
{
|
||||
gs_aKeys[i].m_KeyId = 0;
|
||||
gs_aKeys[i].m_Modifier = 0;
|
||||
}
|
||||
|
||||
for(int KeyId = 0; KeyId < KEY_LAST; KeyId++)
|
||||
{
|
||||
const char *pBind = pSelf->m_pClient->m_pBinds->Get(KeyId);
|
||||
if(!pBind[0])
|
||||
continue;
|
||||
for(int m = 0; m < CBinds::MODIFIER_COUNT; m++)
|
||||
{
|
||||
const char *pBind = pSelf->m_pClient->m_pBinds->Get(KeyId, m);
|
||||
if(!pBind[0])
|
||||
continue;
|
||||
|
||||
for(int i = 0; i < g_KeyCount; i++)
|
||||
if(str_comp(pBind, gs_aKeys[i].m_pCommand) == 0)
|
||||
{
|
||||
gs_aKeys[i].m_KeyId = KeyId;
|
||||
break;
|
||||
}
|
||||
for(int i = 0; i < g_KeyCount; i++)
|
||||
if(str_comp(pBind, gs_aKeys[i].m_pCommand) == 0)
|
||||
{
|
||||
gs_aKeys[i].m_KeyId = KeyId;
|
||||
gs_aKeys[i].m_Modifier = m;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int NumOptions = 7;
|
||||
|
@ -167,20 +182,27 @@ float CMenus::RenderSettingsControlsVoting(CUIRect View, void *pUser)
|
|||
|
||||
// this is kinda slow, but whatever
|
||||
for(int i = 0; i < g_KeyCount; i++)
|
||||
{
|
||||
gs_aKeys[i].m_KeyId = 0;
|
||||
gs_aKeys[i].m_Modifier = 0;
|
||||
}
|
||||
|
||||
for(int KeyId = 0; KeyId < KEY_LAST; KeyId++)
|
||||
{
|
||||
const char *pBind = pSelf->m_pClient->m_pBinds->Get(KeyId);
|
||||
if(!pBind[0])
|
||||
continue;
|
||||
for(int m = 0; m < CBinds::MODIFIER_COUNT; m++)
|
||||
{
|
||||
const char *pBind = pSelf->m_pClient->m_pBinds->Get(KeyId, m);
|
||||
if(!pBind[0])
|
||||
continue;
|
||||
|
||||
for(int i = 0; i < g_KeyCount; i++)
|
||||
if(str_comp(pBind, gs_aKeys[i].m_pCommand) == 0)
|
||||
{
|
||||
gs_aKeys[i].m_KeyId = KeyId;
|
||||
break;
|
||||
}
|
||||
for(int i = 0; i < g_KeyCount; i++)
|
||||
if(str_comp(pBind, gs_aKeys[i].m_pCommand) == 0)
|
||||
{
|
||||
gs_aKeys[i].m_KeyId = KeyId;
|
||||
gs_aKeys[i].m_Modifier = m;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int NumOptions = 2;
|
||||
|
@ -202,20 +224,27 @@ float CMenus::RenderSettingsControlsChat(CUIRect View, void *pUser)
|
|||
|
||||
// this is kinda slow, but whatever
|
||||
for(int i = 0; i < g_KeyCount; i++)
|
||||
{
|
||||
gs_aKeys[i].m_KeyId = 0;
|
||||
gs_aKeys[i].m_Modifier = 0;
|
||||
}
|
||||
|
||||
for(int KeyId = 0; KeyId < KEY_LAST; KeyId++)
|
||||
{
|
||||
const char *pBind = pSelf->m_pClient->m_pBinds->Get(KeyId);
|
||||
if(!pBind[0])
|
||||
continue;
|
||||
for(int m = 0; m < CBinds::MODIFIER_COUNT; m++)
|
||||
{
|
||||
const char *pBind = pSelf->m_pClient->m_pBinds->Get(KeyId, m);
|
||||
if(!pBind[0])
|
||||
continue;
|
||||
|
||||
for(int i = 0; i < g_KeyCount; i++)
|
||||
if(str_comp(pBind, gs_aKeys[i].m_pCommand) == 0)
|
||||
{
|
||||
gs_aKeys[i].m_KeyId = KeyId;
|
||||
break;
|
||||
}
|
||||
for(int i = 0; i < g_KeyCount; i++)
|
||||
if(str_comp(pBind, gs_aKeys[i].m_pCommand) == 0)
|
||||
{
|
||||
gs_aKeys[i].m_KeyId = KeyId;
|
||||
gs_aKeys[i].m_Modifier = m;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int NumOptions = 4;
|
||||
|
@ -237,20 +266,27 @@ float CMenus::RenderSettingsControlsMisc(CUIRect View, void *pUser)
|
|||
|
||||
// this is kinda slow, but whatever
|
||||
for(int i = 0; i < g_KeyCount; i++)
|
||||
{
|
||||
gs_aKeys[i].m_KeyId = 0;
|
||||
gs_aKeys[i].m_Modifier = 0;
|
||||
}
|
||||
|
||||
for(int KeyId = 0; KeyId < KEY_LAST; KeyId++)
|
||||
{
|
||||
const char *pBind = pSelf->m_pClient->m_pBinds->Get(KeyId);
|
||||
if(!pBind[0])
|
||||
continue;
|
||||
for(int m = 0; m < CBinds::MODIFIER_COUNT; m++)
|
||||
{
|
||||
const char *pBind = pSelf->m_pClient->m_pBinds->Get(KeyId, m);
|
||||
if(!pBind[0])
|
||||
continue;
|
||||
|
||||
for(int i = 0; i < g_KeyCount; i++)
|
||||
if(str_comp(pBind, gs_aKeys[i].m_pCommand) == 0)
|
||||
{
|
||||
gs_aKeys[i].m_KeyId = KeyId;
|
||||
break;
|
||||
}
|
||||
for(int i = 0; i < g_KeyCount; i++)
|
||||
if(str_comp(pBind, gs_aKeys[i].m_pCommand) == 0)
|
||||
{
|
||||
gs_aKeys[i].m_KeyId = KeyId;
|
||||
gs_aKeys[i].m_Modifier = m;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int NumOptions = 11;
|
||||
|
|
|
@ -37,11 +37,23 @@ bool CMenusKeyBinder::OnInput(IInput::CEvent Event)
|
|||
{
|
||||
if(m_TakeKey)
|
||||
{
|
||||
if(Event.m_Flags&IInput::FLAG_PRESS)
|
||||
int TriggeringEvent = (Event.m_Key == KEY_MOUSE_1) ? IInput::FLAG_PRESS : IInput::FLAG_RELEASE;
|
||||
if(Event.m_Flags&TriggeringEvent) // delay to RELEASE to support composed binds
|
||||
{
|
||||
m_Key = Event;
|
||||
m_GotKey = true;
|
||||
m_TakeKey = false;
|
||||
|
||||
int Mask = CBinds::GetModifierMask(Input()); // always > 0
|
||||
m_Modifier = 0;
|
||||
while(!(Mask&1)) // this computes a log2, we take the first modifier flag in mask.
|
||||
{
|
||||
Mask >>= 1;
|
||||
m_Modifier++;
|
||||
}
|
||||
// prevent from adding e.g. a control modifier to lctrl
|
||||
if(CBinds::ModifierMatchesKey(m_Modifier, Event.m_Key))
|
||||
m_Modifier = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -184,6 +184,7 @@ void CGameClient::OnConsoleInit()
|
|||
m_All.Add(m_pEffects); // doesn't render anything, just updates effects
|
||||
m_All.Add(m_pParticles); // doesn't render anything, just updates all the particles
|
||||
m_All.Add(m_pBinds);
|
||||
m_All.Add(&m_pBinds->m_SpecialBinds);
|
||||
m_All.Add(m_pControls);
|
||||
m_All.Add(m_pCamera);
|
||||
m_All.Add(m_pSounds);
|
||||
|
@ -208,6 +209,7 @@ void CGameClient::OnConsoleInit()
|
|||
m_All.Add(&gs_Scoreboard);
|
||||
m_All.Add(m_pMotd);
|
||||
m_All.Add(m_pMenus);
|
||||
m_All.Add(&m_pMenus->m_Binder);
|
||||
m_All.Add(m_pGameConsole);
|
||||
|
||||
// build the input stack
|
||||
|
|
Loading…
Reference in a new issue