Skip to content
Permalink
Browse files

Add functions to get or set the console keys

These will be used to make the console key configurable in the bind
menu.
  • Loading branch information
slipher committed Nov 20, 2019
1 parent a83e985 commit f10642a9640640bbc6d2480e55a079407122eeb0
Showing with 58 additions and 26 deletions.
  1. +43 −0 src/engine/client/key_binding.cpp
  2. +5 −0 src/engine/client/keys.h
  3. +9 −1 src/engine/null/NullKeyboard.cpp
  4. +1 −25 src/engine/sys/sdl_input.cpp
@@ -43,6 +43,8 @@ Maryland 20850 USA.

using Keyboard::Key;

static Cvar::Modified<Cvar::Cvar<std::string>> cl_consoleKeys("cl_consoleKeys", "Keys to open or close the console", 0, "hw:`");

static int ClipTeamNumber(int team)
{
return Math::Clamp(team, 0, Keyboard::NUM_TEAMS - 1);
@@ -271,6 +273,47 @@ void SetBinding(Key key, int team, std::string binding)
bindingsModified = true;
}

const std::vector<Key>& GetConsoleKeys()
{
static std::vector<Keyboard::Key> consoleKeys;

// Only parse the variable when it changes
Util::optional<std::string> modifiedString = cl_consoleKeys.GetModifiedValue();
if ( modifiedString )
{
const char* text_p = modifiedString.value().c_str();
consoleKeys.clear();
while ( true )
{
const char* token = COM_Parse( &text_p );
if ( !token[ 0 ] )
{
break;
}
Keyboard::Key k = Keyboard::StringToKey(token);
if (k.IsBindable()) {
consoleKeys.push_back(k);
}
}
}

return consoleKeys;
}

void SetConsoleKeys(const std::vector<Key>& keys)
{
std::string text;
for (Key key : keys) {
if (key.IsBindable()) {
if (!text.empty()) {
text += ' ';
}
text += Cmd::Escape(KeyToString(key));
}
}
cl_consoleKeys.Set(text);
}

Util::optional<std::string> GetBinding(Key key, BindTeam team, bool useDefault)
{
if (!key.IsBindable() || !IsValidTeamNumber(team)) {
@@ -76,6 +76,11 @@ void WriteBindings(fileHandle_t f);
void SetBinding(Key key, int team, std::string binding);
Util::optional<std::string> GetBinding(Key key, BindTeam team, bool useDefault);

// Get/set the keys which toggle (both open and close) the console.
// The source of truth is cl_consoleKeys, but these provide an interface with Key objects.
const std::vector<Key>& GetConsoleKeys();
void SetConsoleKeys(const std::vector<Key>& keys);

// Gets all keys that, if pressed, would execute the given command, based on the current team.
std::vector<Key> GetKeysBoundTo(int team, Str::StringRef command);

@@ -67,10 +67,18 @@ Util::optional<std::string> GetBinding(Key, Keyboard::BindTeam, bool) {
return {};
}

std::vector<Keyboard::Key> GetKeysBoundTo(int, Str::StringRef) {
std::vector<Key> GetKeysBoundTo(int, Str::StringRef) {
return {};
}

const std::vector<Key>& GetConsoleKeys() {
static std::vector<Key> v;
return v;
}

void SetConsoleKeys(const std::vector<Key>&) {
}

void WriteBindings( fileHandle_t ) {
}

@@ -103,8 +103,6 @@ static void IN_PrintKey( const SDL_Keysym *keysym, Keyboard::Key key, bool down
keysym->scancode, SDL_GetKeyName( keysym->sym ), KeyToString( key ) );
}

static Cvar::Modified<Cvar::Cvar<std::string>> cl_consoleKeys("cl_consoleKeys", "Keys to open or close the console", 0, "hw:`");

/*
===============
IN_IsConsoleKey
@@ -113,29 +111,7 @@ IN_IsConsoleKey
*/
static bool IN_IsConsoleKey( Keyboard::Key key )
{
static std::vector<Keyboard::Key> consoleKeys;

// Only parse the variable when it changes
Util::optional<std::string> modifiedString = cl_consoleKeys.GetModifiedValue();
if ( modifiedString )
{
const char* text_p = modifiedString.value().c_str();
consoleKeys.clear();
while ( true )
{
const char* token = COM_Parse( &text_p );
if ( !token[ 0 ] )
{
break;
}
Keyboard::Key k = Keyboard::StringToKey(token);
if (k.IsBindable()) {
consoleKeys.push_back(k);
}
}
}

for (Keyboard::Key k : consoleKeys)
for (Keyboard::Key k : Keyboard::GetConsoleKeys())
{
if (k == key) {
return true;

0 comments on commit f10642a

Please sign in to comment.
You can’t perform that action at this time.