ddnet/src/game/client/components/binds.cpp

240 lines
5.8 KiB
C++
Raw Normal View History

2010-11-20 10:37:14 +00:00
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
2010-05-29 07:25:38 +00:00
#include <engine/config.h>
#include <engine/shared/config.h>
#include "binds.h"
2010-05-29 07:25:38 +00:00
bool CBinds::CBindsSpecial::OnInput(IInput::CEvent Event)
{
// don't handle invalid events and keys that arn't set to anything
2010-05-29 07:25:38 +00:00
if(Event.m_Key >= KEY_F1 && Event.m_Key <= KEY_F15 && m_pBinds->m_aaKeyBindings[Event.m_Key][0] != 0)
{
2010-05-29 07:25:38 +00:00
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;
}
2010-05-29 07:25:38 +00:00
CBinds::CBinds()
{
2010-05-29 07:25:38 +00:00
mem_zero(m_aaKeyBindings, sizeof(m_aaKeyBindings));
m_SpecialBinds.m_pBinds = this;
}
2010-05-29 07:25:38 +00:00
void CBinds::Bind(int KeyId, const char *pStr)
{
2010-05-29 07:25:38 +00:00
if(KeyId < 0 || KeyId >= KEY_LAST)
return;
2010-05-29 07:25:38 +00:00
str_copy(m_aaKeyBindings[KeyId], pStr, sizeof(m_aaKeyBindings[KeyId]));
char aBuf[256];
2010-05-29 07:25:38 +00:00
if(!m_aaKeyBindings[KeyId][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]);
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "binds", aBuf);
}
2010-05-29 07:25:38 +00:00
bool CBinds::OnInput(IInput::CEvent e)
{
// don't handle invalid events and keys that arn't set to anything
2010-05-29 07:25:38 +00:00
if(e.m_Key <= 0 || e.m_Key >= KEY_LAST || m_aaKeyBindings[e.m_Key][0] == 0)
return false;
2010-05-29 07:25:38 +00:00
int Stroke = 0;
if(e.m_Flags&IInput::FLAG_PRESS)
Stroke = 1;
Console()->ExecuteLineStroked(Stroke, m_aaKeyBindings[e.m_Key]);
return true;
}
2010-05-29 07:25:38 +00:00
void CBinds::UnbindAll()
{
for(int i = 0; i < KEY_LAST; i++)
2010-05-29 07:25:38 +00:00
m_aaKeyBindings[i][0] = 0;
}
2010-05-29 07:25:38 +00:00
const char *CBinds::Get(int KeyId)
{
2010-05-29 07:25:38 +00:00
if(KeyId > 0 && KeyId < KEY_LAST)
return m_aaKeyBindings[KeyId];
return "";
}
2010-05-29 07:25:38 +00:00
const char *CBinds::GetKey(const char *pBindStr)
{
2010-05-29 07:25:38 +00:00
for(int KeyId = 0; KeyId < KEY_LAST; KeyId++)
{
2010-05-29 07:25:38 +00:00
const char *pBind = Get(KeyId);
if(!pBind[0])
continue;
2010-05-29 07:25:38 +00:00
if(str_comp(pBind, pBindStr) == 0)
return Input()->KeyName(KeyId);
}
return "";
}
2010-05-29 07:25:38 +00:00
void CBinds::SetDefaults()
{
// set default key bindings
2010-05-29 07:25:38 +00:00
UnbindAll();
Bind(KEY_F1, "toggle_local_console");
Bind(KEY_F2, "toggle_remote_console");
Bind(KEY_TAB, "+scoreboard");
Bind('u', "+show_chat");
2010-05-29 07:25:38 +00:00
Bind(KEY_F10, "screenshot");
Bind('a', "+left");
Bind('d', "+right");
Bind(KEY_SPACE, "+jump");
Bind(KEY_MOUSE_1, "+fire");
Bind(KEY_MOUSE_2, "+hook");
Bind(KEY_LSHIFT, "+emote");
Bind('1', "+weapon1");
Bind('2', "+weapon2");
Bind('3', "+weapon3");
Bind('4', "+weapon4");
Bind('5', "+weapon5");
2010-05-29 07:25:38 +00:00
Bind(KEY_MOUSE_WHEEL_UP, "+prevweapon");
Bind(KEY_MOUSE_WHEEL_DOWN, "+nextweapon");
2010-05-29 07:25:38 +00:00
Bind('t', "chat all");
Bind('y', "chat team");
2010-05-29 07:25:38 +00:00
Bind(KEY_F3, "vote yes");
Bind(KEY_F4, "vote no");
}
2010-05-29 07:25:38 +00:00
void CBinds::OnConsoleInit()
{
// bindings
2010-05-29 07:25:38 +00:00
IConfig *pConfig = Kernel()->RequestInterface<IConfig>();
if(pConfig)
pConfig->RegisterCallback(ConfigSaveCallback, this);
Console()->Register("bind", "sr", CFGFLAG_CLIENT, ConBind, this, "Bind key to execute the command");
Console()->Register("unbind", "s", CFGFLAG_CLIENT, ConUnbind, this, "Unbind key");
Console()->Register("unbindall", "", CFGFLAG_CLIENT, ConUnbindAll, this, "Unbind all keys");
Console()->Register("dump_binds", "", CFGFLAG_CLIENT, ConDumpBinds, this, "Dump binds");
// default bindings
2010-05-29 07:25:38 +00:00
SetDefaults();
}
2010-05-29 07:25:38 +00:00
void CBinds::ConBind(IConsole::IResult *pResult, void *pUserData)
{
2010-05-29 07:25:38 +00:00
CBinds *pBinds = (CBinds *)pUserData;
const char *pKeyName = pResult->GetString(0);
int id = pBinds->GetKeyId(pKeyName);
if(!id)
{
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "key %s not found", pKeyName);
pBinds->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "binds", aBuf);
return;
}
2010-05-29 07:25:38 +00:00
pBinds->Bind(id, pResult->GetString(1));
}
2010-05-29 07:25:38 +00:00
void CBinds::ConUnbind(IConsole::IResult *pResult, void *pUserData)
{
2010-05-29 07:25:38 +00:00
CBinds *pBinds = (CBinds *)pUserData;
const char *pKeyName = pResult->GetString(0);
int id = pBinds->GetKeyId(pKeyName);
if(!id)
{
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "key %s not found", pKeyName);
pBinds->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "binds", aBuf);
return;
}
2010-05-29 07:25:38 +00:00
pBinds->Bind(id, "");
}
2010-05-29 07:25:38 +00:00
void CBinds::ConUnbindAll(IConsole::IResult *pResult, void *pUserData)
{
2010-05-29 07:25:38 +00:00
CBinds *pBinds = (CBinds *)pUserData;
pBinds->UnbindAll();
}
2010-05-29 07:25:38 +00:00
void CBinds::ConDumpBinds(IConsole::IResult *pResult, void *pUserData)
{
2010-05-29 07:25:38 +00:00
CBinds *pBinds = (CBinds *)pUserData;
2010-05-30 00:15:26 +00:00
char aBuf[1024];
for(int i = 0; i < KEY_LAST; i++)
{
2010-05-29 07:25:38 +00:00
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);
}
}
2010-05-29 07:25:38 +00:00
int CBinds::GetKeyId(const char *pKeyName)
{
// check for numeric
2010-05-29 07:25:38 +00:00
if(pKeyName[0] == '&')
{
2010-05-29 07:25:38 +00:00
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++)
{
2010-05-29 07:25:38 +00:00
if(str_comp(pKeyName, Input()->KeyName(i)) == 0)
return i;
}
return 0;
}
2010-05-29 07:25:38 +00:00
void CBinds::ConfigSaveCallback(IConfig *pConfig, void *pUserData)
{
2010-05-29 07:25:38 +00:00
CBinds *pSelf = (CBinds *)pUserData;
char aBuffer[256];
char *pEnd = aBuffer+sizeof(aBuffer)-8;
pConfig->WriteLine("unbindall");
for(int i = 0; i < KEY_LAST; i++)
{
2010-05-29 07:25:38 +00:00
if(pSelf->m_aaKeyBindings[i][0] == 0)
continue;
2010-05-29 07:25:38 +00:00
str_format(aBuffer, sizeof(aBuffer), "bind %s ", pSelf->Input()->KeyName(i));
// process the string. we need to escape some characters
2010-05-29 07:25:38 +00:00
const char *pSrc = pSelf->m_aaKeyBindings[i];
char *pDst = aBuffer + str_length(aBuffer);
*pDst++ = '"';
while(*pSrc && pDst < pEnd)
{
2010-05-29 07:25:38 +00:00
if(*pSrc == '"' || *pSrc == '\\') // escape \ and "
*pDst++ = '\\';
*pDst++ = *pSrc++;
}
2010-05-29 07:25:38 +00:00
*pDst++ = '"';
*pDst++ = 0;
2010-05-29 07:25:38 +00:00
pConfig->WriteLine(aBuffer);
}
}