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

345 lines
8.9 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)
{
2018-07-10 09:29:02 +00:00
// don't handle invalid events and keys that aren't set to anything
2017-04-11 23:36:48 +00:00
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])
{
2010-05-29 07:25:38 +00:00
int Stroke = 0;
if(Event.m_Flags&IInput::FLAG_PRESS)
Stroke = 1;
2017-04-11 23:36:48 +00:00
m_pBinds->GetConsole()->ExecuteLineStroked(Stroke, m_pBinds->m_apKeyBindings[Event.m_Key]);
return true;
}
return false;
}
2010-05-29 07:25:38 +00:00
CBinds::CBinds()
{
2017-04-11 23:36:48 +00:00
mem_zero(m_apKeyBindings, sizeof(m_apKeyBindings));
2010-05-29 07:25:38 +00:00
m_SpecialBinds.m_pBinds = this;
}
2017-04-11 23:36:48 +00:00
CBinds::~CBinds()
{
for(int i = 0; i < KEY_LAST; i++)
if(m_apKeyBindings[i])
free(m_apKeyBindings[i]);
2017-04-11 23:36:48 +00:00
}
2017-07-21 17:32:32 +00:00
void CBinds::Bind(int KeyID, const char *pStr, bool FreeOnly)
{
if(KeyID < 0 || KeyID >= KEY_LAST)
return;
2017-07-21 17:32:32 +00:00
if(FreeOnly && Get(KeyID)[0])
return;
2017-04-11 23:36:48 +00:00
if(m_apKeyBindings[KeyID])
2017-04-12 23:47:17 +00:00
{
free(m_apKeyBindings[KeyID]);
2017-04-12 23:47:17 +00:00
m_apKeyBindings[KeyID] = 0;
}
2017-04-11 23:36:48 +00:00
char aBuf[256];
2017-04-11 23:36:48 +00:00
if(!pStr[0])
{
str_format(aBuf, sizeof(aBuf), "unbound %s (%d)", Input()->KeyName(KeyID), KeyID);
2017-04-11 23:36:48 +00:00
}
else
2017-04-11 23:36:48 +00:00
{
int Size = str_length(pStr) + 1;
m_apKeyBindings[KeyID] = (char *)malloc(Size);
str_copy(m_apKeyBindings[KeyID], pStr, Size);
str_format(aBuf, sizeof(aBuf), "bound %s (%d) = %s", Input()->KeyName(KeyID), KeyID, m_apKeyBindings[KeyID]);
2017-04-11 23:36:48 +00:00
}
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "binds", aBuf);
}
2010-05-29 07:25:38 +00:00
bool CBinds::OnInput(IInput::CEvent e)
{
2018-07-10 09:29:02 +00:00
// don't handle invalid events and keys that aren't set to anything
2017-04-11 23:36:48 +00:00
if(e.m_Key <= 0 || e.m_Key >= KEY_LAST || !m_apKeyBindings[e.m_Key])
return false;
2010-05-29 07:25:38 +00:00
if(e.m_Flags&IInput::FLAG_PRESS)
2017-04-11 23:36:48 +00:00
Console()->ExecuteLineStroked(1, m_apKeyBindings[e.m_Key]);
if(e.m_Flags&IInput::FLAG_RELEASE)
2017-04-11 23:36:48 +00:00
Console()->ExecuteLineStroked(0, m_apKeyBindings[e.m_Key]);
return true;
}
2010-05-29 07:25:38 +00:00
void CBinds::UnbindAll()
{
for(int i = 0; i < KEY_LAST; i++)
2017-04-11 23:36:48 +00:00
{
2017-06-02 17:44:01 +00:00
if(m_apKeyBindings[i])
free(m_apKeyBindings[i]);
2017-04-11 23:36:48 +00:00
m_apKeyBindings[i] = 0;
}
}
const char *CBinds::Get(int KeyID)
{
if(KeyID > 0 && KeyID < KEY_LAST && m_apKeyBindings[KeyID])
2017-04-11 23:36:48 +00:00
return m_apKeyBindings[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);
2017-04-12 23:49:21 +00:00
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(KEY_EQUALS, "+statboard");
2010-05-29 07:25:38 +00:00
Bind(KEY_F10, "screenshot");
2017-07-21 17:32:32 +00:00
Bind(KEY_A, "+left");
Bind(KEY_D, "+right");
2010-05-29 07:25:38 +00:00
Bind(KEY_SPACE, "+jump");
Bind(KEY_MOUSE_1, "+fire");
Bind(KEY_MOUSE_2, "+hook");
Bind(KEY_LSHIFT, "+emote");
2014-06-16 11:29:18 +00:00
#if defined(__ANDROID__)
Bind(KEY_RCTRL, "+fire");
Bind(KEY_RETURN, "+hook");
Bind(KEY_RIGHT, "+right");
Bind(KEY_LEFT, "+left");
Bind(KEY_UP, "+jump");
Bind(KEY_DOWN, "+hook");
Bind(KEY_PAGEUP, "+prevweapon");
Bind(KEY_PAGEDOWN, "+nextweapon");
2014-06-16 12:44:00 +00:00
Bind(KEY_F5, "spectate_previous");
Bind(KEY_F6, "spectate_next");
2014-06-16 11:29:18 +00:00
#else
Bind(KEY_RETURN, "+show_chat; chat all");
Bind(KEY_RIGHT, "spectate_next");
Bind(KEY_LEFT, "spectate_previous");
2014-06-16 12:44:00 +00:00
Bind(KEY_RSHIFT, "+spectate");
2014-06-16 11:29:18 +00:00
#endif
2010-05-29 07:25:38 +00:00
2017-07-21 17:32:32 +00:00
Bind(KEY_1, "+weapon1");
Bind(KEY_2, "+weapon2");
Bind(KEY_3, "+weapon3");
Bind(KEY_4, "+weapon4");
Bind(KEY_5, "+weapon5");
2010-05-29 07:25:38 +00:00
Bind(KEY_MOUSE_WHEEL_UP, "+prevweapon");
Bind(KEY_MOUSE_WHEEL_DOWN, "+nextweapon");
2017-07-21 17:32:32 +00:00
Bind(KEY_T, "+show_chat; chat all");
Bind(KEY_Y, "+show_chat; chat team");
Bind(KEY_U, "+show_chat");
Bind(KEY_I, "+show_chat; chat all /c ");
2010-05-29 07:25:38 +00:00
Bind(KEY_F3, "vote yes");
Bind(KEY_F4, "vote no");
2017-07-21 17:32:32 +00:00
Bind(KEY_K, "kill");
Bind(KEY_Q, "say /pause");
Bind(KEY_P, "say /pause");
2013-08-18 01:33:55 +00:00
// DDRace
2011-04-17 17:14:49 +00:00
if(g_Config.m_ClDDRaceBinds)
SetDDRaceBinds(false);
}
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", "s[key] r[command]", CFGFLAG_CLIENT, ConBind, this, "Bind key to execute the command");
2017-03-06 17:06:55 +00:00
Console()->Register("dump_binds", "?s[key]", CFGFLAG_CLIENT, ConDumpBinds, this, "Print command executed by this keybindind or all binds");
Console()->Register("unbind", "s[key]", CFGFLAG_CLIENT, ConUnbind, this, "Unbind key");
Console()->Register("unbindall", "", CFGFLAG_CLIENT, ConUnbindAll, this, "Unbind all keys");
// default bindings
2010-05-29 07:25:38 +00:00
SetDefaults();
}
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));
}
2017-03-06 17:06:55 +00:00
void CBinds::ConDumpBinds(IConsole::IResult *pResult, void *pUserData)
2017-01-28 15:29:03 +00:00
{
CBinds *pBinds = (CBinds *)pUserData;
if(pResult->NumArguments() == 1)
2017-01-28 17:34:24 +00:00
{
2017-01-28 15:29:03 +00:00
char aBuf[256];
const char *pKeyName = pResult->GetString(0);
2017-01-28 16:02:39 +00:00
int id = pBinds->GetKeyID(pKeyName);
if (!id)
2017-01-28 16:02:39 +00:00
{
2017-03-06 17:06:55 +00:00
str_format(aBuf, sizeof(aBuf), "key '%s' not found", pKeyName);
pBinds->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "binds", aBuf);
2017-01-28 16:02:39 +00:00
}
else
2017-01-28 17:34:24 +00:00
{
2017-04-11 23:36:48 +00:00
if (!pBinds->m_apKeyBindings[id])
str_format(aBuf, sizeof(aBuf), "%s (%d) is not bound", pKeyName, id);
else
2017-04-11 23:36:48 +00:00
str_format(aBuf, sizeof(aBuf), "%s (%d) = %s", pKeyName, id, pBinds->m_apKeyBindings[id]);
pBinds->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "binds", aBuf);
}
}
else if(pResult->NumArguments() == 0)
{
char aBuf[1024];
for(int i = 0; i < KEY_LAST; i++)
{
2017-04-11 23:36:48 +00:00
if(!pBinds->m_apKeyBindings[i])
continue;
2017-04-11 23:36:48 +00:00
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);
2017-01-28 16:02:39 +00:00
}
2017-01-28 15:29:03 +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, "");
}
void CBinds::ConUnbindAll(IConsole::IResult *pResult, void *pUserData)
{
2010-05-29 07:25:38 +00:00
CBinds *pBinds = (CBinds *)pUserData;
pBinds->UnbindAll();
}
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;
2010-05-29 07:25:38 +00:00
char aBuffer[256];
char *pEnd = aBuffer+sizeof(aBuffer);
2010-05-29 07:25:38 +00:00
pConfig->WriteLine("unbindall");
for(int i = 0; i < KEY_LAST; i++)
{
2017-04-11 23:36:48 +00:00
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
2010-05-29 07:25:38 +00:00
char *pDst = aBuffer + str_length(aBuffer);
str_escape(&pDst, pSelf->m_apKeyBindings[i], pEnd);
str_append(aBuffer, "\"", sizeof(aBuffer));
2010-05-29 07:25:38 +00:00
pConfig->WriteLine(aBuffer);
}
}
2011-04-17 17:14:49 +00:00
// DDRace
void CBinds::SetDDRaceBinds(bool FreeOnly)
{
2017-07-21 17:32:32 +00:00
Bind(KEY_KP_PLUS, "zoom+", FreeOnly);
Bind(KEY_KP_MINUS, "zoom-", FreeOnly);
Bind(KEY_KP_MULTIPLY, "zoom", FreeOnly);
Bind(KEY_PAUSE, "say /pause", FreeOnly);
Bind(KEY_UP, "+jump", FreeOnly);
Bind(KEY_LEFT, "+left", FreeOnly);
Bind(KEY_RIGHT, "+right", FreeOnly);
Bind(KEY_LEFTBRACKET, "+prevweapon", FreeOnly);
Bind(KEY_RIGHTBRACKET, "+nextweapon", FreeOnly);
Bind(KEY_C, "say /rank", FreeOnly);
Bind(KEY_V, "say /info", FreeOnly);
Bind(KEY_B, "say /top5", FreeOnly);
Bind(KEY_X, "emote 14", FreeOnly);
Bind(KEY_H, "emote 2", FreeOnly);
Bind(KEY_M, "emote 5", FreeOnly);
Bind(KEY_S, "+showhookcoll", FreeOnly);
Bind(KEY_X, "toggle cl_dummy 0 1", FreeOnly);
2014-06-16 11:29:18 +00:00
#if !defined(__ANDROID__)
2017-07-21 17:32:32 +00:00
Bind(KEY_PAGEDOWN, "toggle cl_show_quads 0 1", FreeOnly);
Bind(KEY_PAGEUP, "toggle cl_overlay_entities 0 100", FreeOnly);
2014-06-16 11:29:18 +00:00
#endif
2017-07-21 17:32:32 +00:00
Bind(KEY_KP_0, "say /emote normal 999999", FreeOnly);
Bind(KEY_KP_1, "say /emote happy 999999", FreeOnly);
Bind(KEY_KP_2, "say /emote angry 999999", FreeOnly);
Bind(KEY_KP_3, "say /emote pain 999999", FreeOnly);
Bind(KEY_KP_4, "say /emote surprise 999999", FreeOnly);
Bind(KEY_KP_5, "say /emote blink 999999", FreeOnly);
Bind(KEY_MOUSE_3, "+spectate", FreeOnly);
Bind(KEY_MINUS, "spectate_previous", FreeOnly);
Bind(KEY_EQUALS, "spectate_next", FreeOnly);
2014-01-14 20:47:54 +00:00
g_Config.m_ClDDRaceBindsSet = 1;
2013-08-18 01:33:55 +00:00
}