Skip to content

Commit f10642a

Browse files
committed
Add functions to get or set the console keys
These will be used to make the console key configurable in the bind menu.
1 parent a83e985 commit f10642a

File tree

4 files changed

+58
-26
lines changed

4 files changed

+58
-26
lines changed

src/engine/client/key_binding.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Maryland 20850 USA.
4343

4444
using Keyboard::Key;
4545

46+
static Cvar::Modified<Cvar::Cvar<std::string>> cl_consoleKeys("cl_consoleKeys", "Keys to open or close the console", 0, "hw:`");
47+
4648
static int ClipTeamNumber(int team)
4749
{
4850
return Math::Clamp(team, 0, Keyboard::NUM_TEAMS - 1);
@@ -271,6 +273,47 @@ void SetBinding(Key key, int team, std::string binding)
271273
bindingsModified = true;
272274
}
273275

276+
const std::vector<Key>& GetConsoleKeys()
277+
{
278+
static std::vector<Keyboard::Key> consoleKeys;
279+
280+
// Only parse the variable when it changes
281+
Util::optional<std::string> modifiedString = cl_consoleKeys.GetModifiedValue();
282+
if ( modifiedString )
283+
{
284+
const char* text_p = modifiedString.value().c_str();
285+
consoleKeys.clear();
286+
while ( true )
287+
{
288+
const char* token = COM_Parse( &text_p );
289+
if ( !token[ 0 ] )
290+
{
291+
break;
292+
}
293+
Keyboard::Key k = Keyboard::StringToKey(token);
294+
if (k.IsBindable()) {
295+
consoleKeys.push_back(k);
296+
}
297+
}
298+
}
299+
300+
return consoleKeys;
301+
}
302+
303+
void SetConsoleKeys(const std::vector<Key>& keys)
304+
{
305+
std::string text;
306+
for (Key key : keys) {
307+
if (key.IsBindable()) {
308+
if (!text.empty()) {
309+
text += ' ';
310+
}
311+
text += Cmd::Escape(KeyToString(key));
312+
}
313+
}
314+
cl_consoleKeys.Set(text);
315+
}
316+
274317
Util::optional<std::string> GetBinding(Key key, BindTeam team, bool useDefault)
275318
{
276319
if (!key.IsBindable() || !IsValidTeamNumber(team)) {

src/engine/client/keys.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ void WriteBindings(fileHandle_t f);
7676
void SetBinding(Key key, int team, std::string binding);
7777
Util::optional<std::string> GetBinding(Key key, BindTeam team, bool useDefault);
7878

79+
// Get/set the keys which toggle (both open and close) the console.
80+
// The source of truth is cl_consoleKeys, but these provide an interface with Key objects.
81+
const std::vector<Key>& GetConsoleKeys();
82+
void SetConsoleKeys(const std::vector<Key>& keys);
83+
7984
// Gets all keys that, if pressed, would execute the given command, based on the current team.
8085
std::vector<Key> GetKeysBoundTo(int team, Str::StringRef command);
8186

src/engine/null/NullKeyboard.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,18 @@ Util::optional<std::string> GetBinding(Key, Keyboard::BindTeam, bool) {
6767
return {};
6868
}
6969

70-
std::vector<Keyboard::Key> GetKeysBoundTo(int, Str::StringRef) {
70+
std::vector<Key> GetKeysBoundTo(int, Str::StringRef) {
7171
return {};
7272
}
7373

74+
const std::vector<Key>& GetConsoleKeys() {
75+
static std::vector<Key> v;
76+
return v;
77+
}
78+
79+
void SetConsoleKeys(const std::vector<Key>&) {
80+
}
81+
7482
void WriteBindings( fileHandle_t ) {
7583
}
7684

src/engine/sys/sdl_input.cpp

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ static void IN_PrintKey( const SDL_Keysym *keysym, Keyboard::Key key, bool down
103103
keysym->scancode, SDL_GetKeyName( keysym->sym ), KeyToString( key ) );
104104
}
105105

106-
static Cvar::Modified<Cvar::Cvar<std::string>> cl_consoleKeys("cl_consoleKeys", "Keys to open or close the console", 0, "hw:`");
107-
108106
/*
109107
===============
110108
IN_IsConsoleKey
@@ -113,29 +111,7 @@ IN_IsConsoleKey
113111
*/
114112
static bool IN_IsConsoleKey( Keyboard::Key key )
115113
{
116-
static std::vector<Keyboard::Key> consoleKeys;
117-
118-
// Only parse the variable when it changes
119-
Util::optional<std::string> modifiedString = cl_consoleKeys.GetModifiedValue();
120-
if ( modifiedString )
121-
{
122-
const char* text_p = modifiedString.value().c_str();
123-
consoleKeys.clear();
124-
while ( true )
125-
{
126-
const char* token = COM_Parse( &text_p );
127-
if ( !token[ 0 ] )
128-
{
129-
break;
130-
}
131-
Keyboard::Key k = Keyboard::StringToKey(token);
132-
if (k.IsBindable()) {
133-
consoleKeys.push_back(k);
134-
}
135-
}
136-
}
137-
138-
for (Keyboard::Key k : consoleKeys)
114+
for (Keyboard::Key k : Keyboard::GetConsoleKeys())
139115
{
140116
if (k == key) {
141117
return true;

0 commit comments

Comments
 (0)