Skip to content

Commit dbf0e06

Browse files
committed
Added commands for managing key binds.
1 parent 145468e commit dbf0e06

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

Runtime/DevConsole.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public static bool IsOpen
7272
/// </summary>
7373
public static bool IsOpenAndFocused => _console.ConsoleIsShowingAndFocused;
7474

75+
/// <summary>
76+
/// Whether the dev console user-defined key bindings are enabled.
77+
/// </summary>
78+
public static bool IsKeyBindingsEnabled => _console.BindingsIsEnabled;
79+
7580
/// <summary>
7681
/// The key used to toggle the dev console window, NULL if no key.
7782
/// </summary>

Runtime/DevConsoleMono.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ internal sealed class DevConsoleMono : MonoBehaviour
7777
"DevConsole.newConsoleToggleKey";
7878
#else
7979
"DevConsole.legacyConsoleToggleKey";
80+
#endif
81+
private const string PrefBindings =
82+
#if USE_NEW_INPUT_SYSTEM
83+
"DevConsole.newBindings";
84+
#else
85+
"DevConsole.legacyBindings";
8086
#endif
8187
private const string PrefDisplayUnityLogs = "DevConsole.displayUnityLogs";
8288
private const string PrefDisplayUnityErrors = "DevConsole.displayUnityErrors";
@@ -121,6 +127,7 @@ internal sealed class DevConsoleMono : MonoBehaviour
121127
#region Input fields
122128

123129
private bool _focusInputField = false;
130+
private Dictionary<InputKey, string> _bindings = new Dictionary<InputKey, string>();
124131

125132
#endregion
126133

@@ -178,6 +185,8 @@ internal sealed class DevConsoleMono : MonoBehaviour
178185

179186
internal bool ConsoleIsShowingAndFocused => ConsoleIsShowing && _inputField.isFocused;
180187

188+
internal bool BindingsIsEnabled { get; set; } = true;
189+
181190
private string InputText
182191
{
183192
get => _inputField.text;
@@ -684,6 +693,25 @@ private void LateUpdate()
684693
return;
685694
}
686695

696+
// Check bindings (as long as the input field isn't focused!)
697+
if (BindingsIsEnabled && !_inputField.isFocused)
698+
{
699+
try
700+
{
701+
foreach (InputKey key in _bindings.Keys)
702+
{
703+
if (GetKeyDown(key))
704+
{
705+
RunCommand(_bindings[key]);
706+
}
707+
}
708+
}
709+
catch (Exception e)
710+
{
711+
LogError($"Checking bindings failed with an exception: {e.Message}");
712+
}
713+
}
714+
687715
// Process the stored logs, displaying them to the console
688716
if (_logTextStore != string.Empty)
689717
{
@@ -933,6 +961,68 @@ private void InitBuiltInCommands()
933961
() => Log($"Developer console version: {_version}.")
934962
));
935963

964+
AddCommand(Command.Create<InputKey, string>(
965+
"bind",
966+
"addbind",
967+
"Add a key binding for a command",
968+
Parameter.Create("Key", "Key to bind the command to"),
969+
Parameter.Create("Command", "Command to execute when the key bind is pressed"),
970+
(key, command) =>
971+
{
972+
if (_bindings.ContainsKey(key))
973+
{
974+
LogError($"A key binding already exists for <i>{key}</i>. Use {GetCommand("unbind").ToFormattedString()} to remove the key binding.");
975+
return;
976+
}
977+
978+
_bindings[key] = command;
979+
LogSuccess($"Successfully added a key binding for <i>{key}</i>.");
980+
}
981+
));
982+
983+
AddCommand(Command.Create<InputKey>(
984+
"unbind",
985+
"removebind",
986+
"Remove a key binding",
987+
Parameter.Create("Key", "Key binding to remove"),
988+
key =>
989+
{
990+
if (!_bindings.ContainsKey(key))
991+
{
992+
LogError($"A key binding doesn't exist for <i>{key}</i>.");
993+
return;
994+
}
995+
996+
_bindings.Remove(key);
997+
LogSuccess($"Successfully removed a key binding for <i>{key}</i>.");
998+
}
999+
));
1000+
1001+
AddCommand(Command.Create(
1002+
"binds",
1003+
"",
1004+
"List all the key bindings",
1005+
() =>
1006+
{
1007+
if (_bindings.Count == 0)
1008+
{
1009+
Log($"There are no key bindings. Use {GetCommand("bind").GetFormattedName()} to add a key binding.");
1010+
return;
1011+
}
1012+
1013+
string result = "";
1014+
foreach (InputKey key in _bindings.Keys)
1015+
{
1016+
result += $"<i>{key}</i>: \"{_bindings[key]}\"\n";
1017+
}
1018+
result = result.Remove(result.Length - 2);
1019+
1020+
LogSeperator($"Key bindings ({_bindings.Count})");
1021+
Log(result);
1022+
LogSeperator();
1023+
}
1024+
));
1025+
9361026
#endregion
9371027

9381028
#region Player commands

0 commit comments

Comments
 (0)