mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 10:08:18 +00:00
Merge pull request #728 from Learath2/dd_pr_bindlen
Allow arbitrary length binds
This commit is contained in:
commit
d146b51189
|
@ -7,13 +7,13 @@
|
|||
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)
|
||||
if(((Event.m_Key >= KEY_F1 && Event.m_Key <= KEY_F12) || (Event.m_Key >= KEY_F13 && Event.m_Key <= KEY_F24)) && m_pBinds->m_apKeyBindings[Event.m_Key])
|
||||
{
|
||||
int Stroke = 0;
|
||||
if(Event.m_Flags&IInput::FLAG_PRESS)
|
||||
Stroke = 1;
|
||||
|
||||
m_pBinds->GetConsole()->ExecuteLineStroked(Stroke, m_pBinds->m_aaKeyBindings[Event.m_Key]);
|
||||
m_pBinds->GetConsole()->ExecuteLineStroked(Stroke, m_pBinds->m_apKeyBindings[Event.m_Key]);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -22,48 +22,74 @@ bool CBinds::CBindsSpecial::OnInput(IInput::CEvent Event)
|
|||
|
||||
CBinds::CBinds()
|
||||
{
|
||||
mem_zero(m_aaKeyBindings, sizeof(m_aaKeyBindings));
|
||||
mem_zero(m_apKeyBindings, sizeof(m_apKeyBindings));
|
||||
m_SpecialBinds.m_pBinds = this;
|
||||
}
|
||||
|
||||
CBinds::~CBinds()
|
||||
{
|
||||
for(int i = 0; i < KEY_LAST; i++)
|
||||
if(m_apKeyBindings[i])
|
||||
mem_free(m_apKeyBindings[i]);
|
||||
}
|
||||
|
||||
void CBinds::Bind(int KeyID, const char *pStr)
|
||||
{
|
||||
if(KeyID < 0 || KeyID >= KEY_LAST)
|
||||
return;
|
||||
|
||||
str_copy(m_aaKeyBindings[KeyID], pStr, sizeof(m_aaKeyBindings[KeyID]));
|
||||
if(m_apKeyBindings[KeyID])
|
||||
mem_free(m_apKeyBindings[KeyID]);
|
||||
|
||||
char aBuf[256];
|
||||
if(!m_aaKeyBindings[KeyID][0])
|
||||
if(!pStr[0])
|
||||
{
|
||||
str_format(aBuf, sizeof(aBuf), "unbound %s (%d)", Input()->KeyName(KeyID), KeyID);
|
||||
}
|
||||
else
|
||||
str_format(aBuf, sizeof(aBuf), "bound %s (%d) = %s", Input()->KeyName(KeyID), KeyID, m_aaKeyBindings[KeyID]);
|
||||
{
|
||||
int size = str_length(pStr) + 1;
|
||||
m_apKeyBindings[KeyID] = (char *)mem_alloc(size, 1);
|
||||
if(!m_apKeyBindings[KeyID])
|
||||
{
|
||||
str_format(aBuf, sizeof(aBuf), "couldn't bind %s (%d) (bind might be too long)", Input()->KeyName(KeyID), KeyID);
|
||||
}
|
||||
else
|
||||
{
|
||||
str_copy(m_apKeyBindings[KeyID], pStr, size);
|
||||
str_format(aBuf, sizeof(aBuf), "bound %s (%d) = %s", Input()->KeyName(KeyID), KeyID, m_apKeyBindings[KeyID]);
|
||||
}
|
||||
}
|
||||
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "binds", aBuf);
|
||||
}
|
||||
|
||||
|
||||
bool CBinds::OnInput(IInput::CEvent e)
|
||||
{
|
||||
// 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)
|
||||
if(e.m_Key <= 0 || e.m_Key >= KEY_LAST || !m_apKeyBindings[e.m_Key])
|
||||
return false;
|
||||
|
||||
if(e.m_Flags&IInput::FLAG_PRESS)
|
||||
Console()->ExecuteLineStroked(1, m_aaKeyBindings[e.m_Key]);
|
||||
Console()->ExecuteLineStroked(1, m_apKeyBindings[e.m_Key]);
|
||||
if(e.m_Flags&IInput::FLAG_RELEASE)
|
||||
Console()->ExecuteLineStroked(0, m_aaKeyBindings[e.m_Key]);
|
||||
Console()->ExecuteLineStroked(0, m_apKeyBindings[e.m_Key]);
|
||||
return true;
|
||||
}
|
||||
|
||||
void CBinds::UnbindAll()
|
||||
{
|
||||
for(int i = 0; i < KEY_LAST; i++)
|
||||
m_aaKeyBindings[i][0] = 0;
|
||||
{
|
||||
if(m_apKeyBindings)
|
||||
mem_free(m_apKeyBindings[i]);
|
||||
m_apKeyBindings[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
const char *CBinds::Get(int KeyID)
|
||||
{
|
||||
if(KeyID > 0 && KeyID < KEY_LAST)
|
||||
return m_aaKeyBindings[KeyID];
|
||||
return m_apKeyBindings[KeyID];
|
||||
return "";
|
||||
}
|
||||
|
||||
|
@ -195,10 +221,10 @@ void CBinds::ConDumpBinds(IConsole::IResult *pResult, void *pUserData)
|
|||
}
|
||||
else
|
||||
{
|
||||
if (pBinds->m_aaKeyBindings[id][0] == '\0')
|
||||
if (!pBinds->m_apKeyBindings[id])
|
||||
str_format(aBuf, sizeof(aBuf), "%s (%d) is not bound", pKeyName, id);
|
||||
else
|
||||
str_format(aBuf, sizeof(aBuf), "%s (%d) = %s", pKeyName, id, pBinds->m_aaKeyBindings[id]);
|
||||
str_format(aBuf, sizeof(aBuf), "%s (%d) = %s", pKeyName, id, pBinds->m_apKeyBindings[id]);
|
||||
|
||||
pBinds->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "binds", aBuf);
|
||||
}
|
||||
|
@ -208,9 +234,9 @@ 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)
|
||||
if(!pBinds->m_apKeyBindings[i])
|
||||
continue;
|
||||
str_format(aBuf, sizeof(aBuf), "%s (%d) = %s", pBinds->Input()->KeyName(i), i, pBinds->m_aaKeyBindings[i]);
|
||||
str_format(aBuf, sizeof(aBuf), "%s (%d) = %s", pBinds->Input()->KeyName(i), i, pBinds->m_apKeyBindings[i]);
|
||||
pBinds->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "binds", aBuf);
|
||||
}
|
||||
}
|
||||
|
@ -268,12 +294,12 @@ void CBinds::ConfigSaveCallback(IConfig *pConfig, void *pUserData)
|
|||
pConfig->WriteLine("unbindall");
|
||||
for(int i = 0; i < KEY_LAST; i++)
|
||||
{
|
||||
if(pSelf->m_aaKeyBindings[i][0] == 0)
|
||||
if(!pSelf->m_apKeyBindings[i])
|
||||
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];
|
||||
const char *pSrc = pSelf->m_apKeyBindings[i];
|
||||
char *pDst = aBuffer + str_length(aBuffer);
|
||||
*pDst++ = '"';
|
||||
while(*pSrc && pDst < pEnd)
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
class CBinds : public CComponent
|
||||
{
|
||||
char m_aaKeyBindings[KEY_LAST][128];
|
||||
char *m_apKeyBindings[KEY_LAST];
|
||||
|
||||
int GetKeyID(const char *pKeyName);
|
||||
|
||||
|
@ -21,6 +21,7 @@ class CBinds : public CComponent
|
|||
|
||||
public:
|
||||
CBinds();
|
||||
~CBinds();
|
||||
|
||||
class CBindsSpecial : public CComponent
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue