-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathGameConsole.cpp
More file actions
437 lines (361 loc) · 16.2 KB
/
GameConsole.cpp
File metadata and controls
437 lines (361 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include "GameConsole.h"
#include "GameConsoleCommandHandler.h"
#include "Game-Lib/Util/ServiceLocator.h"
#include <imgui/imgui.h>
#include <imgui/backends/imgui_impl_vulkan.h>
#include <imgui/backends/imgui_impl_glfw.h>
#include <imgui/misc/cpp/imgui_stdlib.h>
#include <imgui/implot.h>
#include <Input/InputManager.h>
#include <Renderer/RenderSettings.h>
#include <GLFW/glfw3.h>
#include <imgui_internal.h>
#include <tracy/Tracy.hpp>
AutoCVar_Int CVAR_GameConsoleEnabled(CVarCategory::Client, "consoleEnabled", "enable game console", 1, CVarFlags::Hidden | CVarFlags::DoNotSave);
AutoCVar_Int CVAR_GameConsoleDuplicateToTerminal(CVarCategory::Client, "consoleDuplicateToTerminal", "enable printing to terminal", 1, CVarFlags::EditCheckbox);
GameConsole::GameConsole()
{
_lines.push_back("Welcome to Game Console. You may enter 'help' for more information about usage.");
InputManager* inputManager = ServiceLocator::GetInputManager();
KeybindGroup* keybindGroup = inputManager->GetKeybindGroupByHash("Debug"_h);
keybindGroup->AddKeyboardCallback("Enable Game Console", GLFW_KEY_BACKSLASH, KeybindAction::Release, KeybindModifier::Any, [this](i32 key, KeybindAction action, KeybindModifier modifier) -> bool
{
Toggle();
return true;
});
Disable();
_commandHandler = new GameConsoleCommandHandler();
_commandHistory.reserve(CommandHistoryMaxSize);
}
GameConsole::~GameConsole()
{
delete _commandHandler;
}
bool MatchesCommand(const std::string& command, const std::string& _searchText)
{
if (_searchText.empty())
return true; // Show all if empty
// Case-insensitive comparison
auto caseInsensitiveFind = [](const std::string& str, const std::string& sub) -> bool
{
return std::search(str.begin(), str.end(), sub.begin(), sub.end(),
[](char ch1, char ch2) { return std::tolower(ch1) == std::tolower(ch2); }) != str.end();
};
// Prioritize prefix match
if (command.size() >= _searchText.size() &&
std::equal(_searchText.begin(), _searchText.end(), command.begin(),
[](char ch1, char ch2) { return std::tolower(ch1) == std::tolower(ch2); }))
{
return true;
}
// Fallback to case-insensitive substring match
return caseInsensitiveFind(command, _searchText);
}
void GameConsole::Render(f32 deltaTime)
{
ZoneScoped;
i32* isGameConsoleEnabled = CVAR_GameConsoleEnabled.GetPtr();
if (*isGameConsoleEnabled == 0)
return;
_visibleProgressTimer = glm::min(_visibleProgressTimer + (5.0f * deltaTime), 1.0f);
std::string lineToAppend;
while (_linesToAppend.try_dequeue(lineToAppend))
{
_lines.push_back(lineToAppend);
}
f32 height = glm::mix(0.0f, 300.f, _visibleProgressTimer);
const ImGuiViewport* viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(ImVec2(viewport->Pos.x, viewport->Pos.y));
ImGui::SetNextWindowSize(ImVec2(viewport->WorkSize.x, height));
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0.14f, 0.14f, 0.14f, 0.85f));
if (ImGui::Begin("GameConsole", reinterpret_cast<bool*>(isGameConsoleEnabled), ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove))
{
ImGuiWindow* gameConsoleWindow = ImGui::GetCurrentWindow();
if (ImGui::IsKeyReleased(ImGuiKey_Escape))
{
Disable();
}
else if (ImGui::IsKeyReleased(ImGuiKey_Tab))
{
ImGuiID inputFieldID = gameConsoleWindow->GetID("##ConsoleInputField");
ImGui::ActivateItemByID(inputFieldID);
}
ImGui::SetWindowFontScale(1.0f);
const f32 reservedHeight = ImGui::GetStyle().ItemSpacing.y + ImGui::GetFrameHeightWithSpacing();
ImGui::BeginChild("TextRegion", ImVec2(0, -reservedHeight), false, ImGuiWindowFlags_HorizontalScrollbar);
{
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 1));
for (u32 i = 0; i < _lines.size(); i++)
{
const std::string& line = _lines[i];
ImGui::TextWrapped("%s", line.c_str());
}
ImGui::PopStyleVar();
}
ImGui::EndChild();
ImGui::Separator();
ImGui::PushItemWidth(Renderer::Settings::SCREEN_WIDTH);
ImGui::PushStyleColor(ImGuiCol_FrameBg, ImVec4(0.50f, 0.50f, 0.50f, 0.50f));
if (_textFieldHasFocus && !_commandHistory.empty())
{
bool wasModified = false;
if (ImGui::IsKeyDown(ImGuiKey_LeftCtrl))
{
if (ImGui::IsKeyReleased(ImGuiKey_UpArrow))
{
i32 numCommandsInHistory = static_cast<i32>(_commandHistory.size());
if (_commandHistoryIndex == -1)
{
_commandHistoryIndex = _commandHistoryDefaultIndex;
}
else if (--_commandHistoryIndex < 0)
{
_commandHistoryIndex = numCommandsInHistory - 1;
}
_searchText = _commandHistory[_commandHistoryIndex];
wasModified = true;
}
else if (ImGui::IsKeyReleased(ImGuiKey_DownArrow))
{
if (_commandHistoryIndex == -1)
_commandHistoryIndex = _commandHistoryDefaultIndex;
i32 numCommandsInHistory = static_cast<i32>(_commandHistory.size());
if (++_commandHistoryIndex >= numCommandsInHistory)
_commandHistoryIndex = 0;
_searchText = _commandHistory[_commandHistoryIndex];
wasModified = true;
}
}
if (wasModified)
{
ImGuiWindow* window = ImGui::GetCurrentWindow();
ImGuiID inputFieldID = window->GetID("##ConsoleInputField");
ImGui::GetInputTextState(inputFieldID)->ReloadUserBufAndMoveToEnd();
}
}
u32 searchTextLength = static_cast<u32>(_searchText.length());
bool handleInput = ImGui::InputText("##ConsoleInputField", &_searchText, ImGuiInputTextFlags_EnterReturnsTrue);
ImGui::PopStyleColor();
ImGui::PopItemWidth();
if (handleInput && searchTextLength > 0)
{
bool searchTextIsOnlySpaces = std::all_of(_searchText.begin(), _searchText.end(), [](unsigned char ch) { return std::isspace(ch); });
if (!searchTextIsOnlySpaces)
{
u32 numCommandsInHistory = static_cast<u32>(_commandHistory.size());
if (numCommandsInHistory < CommandHistoryMaxSize)
{
if (numCommandsInHistory == 0 || _commandHistory.back() != _searchText)
{
_commandHistoryIndex = -1;
_commandHistoryDefaultIndex = numCommandsInHistory;
_commandHistory.push_back(_searchText);
}
}
else
{
_commandHistoryIndex = -1;
_commandHistoryDefaultIndex = _commandHistoryUpdateIndex;
_commandHistory[_commandHistoryUpdateIndex++] = _searchText;
if (_commandHistoryUpdateIndex >= CommandHistoryMaxSize)
_commandHistoryUpdateIndex = 0;
}
_commandHandler->HandleCommand(this, _searchText);
_lines.push_back(_searchText);
}
_searchText = "";
ImGuiID inputFieldID = gameConsoleWindow->GetID("##ConsoleInputField");
ImGui::GetInputTextState(inputFieldID)->ReloadUserBufAndMoveToEnd();
}
// Auto-completion suggestions based on the current input
if (_searchText.empty())
{
_lastSearchTextHash = std::numeric_limits<u32>().max();
_suggestionSelectedIndex = 0;
_commandHashSuggestions.clear();
}
else
{
const auto& commands = _commandHandler->GetCommandEntries();
u32 searchHash = StringUtils::fnv1a_32(_searchText.c_str(), _searchText.length());
if (searchHash != _lastSearchTextHash)
{
_commandHashSuggestions.clear();
if (!commands.contains(searchHash))
{
for (const auto& command : commands)
{
if (MatchesCommand(command.second.nameWithAliases, _searchText))
{
_commandHashSuggestions.push_back(command.first);
}
}
auto EditDistance = [&](const std::string_view a, const std::string_view b) -> i32
{
size_t n = a.size(), m = b.size();
std::vector<i32> dp(m + 1);
std::iota(dp.begin(), dp.end(), 0);
for (size_t i = 1; i <= n; ++i)
{
i32 prev = dp[0];
dp[0] = static_cast<i32>(i);
for (size_t j = 1; j <= m; ++j)
{
i32 temp = dp[j];
if (std::tolower(a[i - 1]) == std::tolower(b[j - 1]))
dp[j] = prev;
else
dp[j] = std::min({ dp[j - 1], dp[j], prev }) + 1;
prev = temp;
}
}
return dp[m];
};
auto scoreOne = [&](std::string_view text)
{
// exact match
if (text == _searchText)
return std::tuple{ 0, 0, static_cast<i32>(text.size()) };
// prefix match
if (text.size() >= _searchText.size() &&
std::equal(_searchText.begin(), _searchText.end(), text.begin(),
[](char a, char b) { return a == b; }))
{
return std::tuple{ 1, 0, static_cast<int>(text.size()) };
}
// substring match
auto pos = text.find(_searchText);
if (pos != std::string::npos)
{
bool leftBoundary = (pos == 0) || (text[pos - 1] == ' ');
bool rightBoundary = (pos + _searchText.size() == text.size()) ||
(text[pos + _searchText.size()] == ' ');
i32 boundaryScore = (leftBoundary && rightBoundary) ? 0 : 1;
return std::tuple{ 2, boundaryScore * 1000 + static_cast<i32>(pos), static_cast<i32>(text.size()) };
}
// fuzzy fallback
i32 dist = EditDistance(text, _searchText);
return std::tuple{ 3, dist, static_cast<i32>(text.size()) };
};
auto score = [&](const GameConsoleCommandEntry& cmd)
{
auto best = scoreOne(cmd.name);
for (auto alias : cmd.aliases)
{
auto s = scoreOne(alias);
if (s < best)
best = s;
}
return best;
};
std::sort(_commandHashSuggestions.begin(), _commandHashSuggestions.end(), [&](u32 a, u32 b)
{
const auto& commandA = commands.at(a);
const auto& commandB = commands.at(b);
return score(commandA) < score(commandB);
});
}
_lastSearchTextHash = searchHash;
_suggestionSelectedIndex = 0;
}
if (!_commandHashSuggestions.empty())
{
// Position the popup right below the input field.
ImVec2 inputPos = ImGui::GetItemRectMin();
ImVec2 inputSize = ImGui::GetItemRectSize();
ImVec2 popupPos = ImVec2(inputPos.x, (inputPos.y + inputSize.y));
ImGui::SetNextWindowPos(popupPos, ImGuiCond_Always);
bool tabReleased = ImGui::IsKeyReleased(ImGuiKey_Tab);
if (ImGui::BeginPopup("SuggestionsPopup",
ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize))
{
u32 numSuggestions = static_cast<u32>(_commandHashSuggestions.size());
if (!ImGui::IsKeyDown(ImGuiKey_LeftCtrl))
{
if (ImGui::IsKeyReleased(ImGuiKey_UpArrow))
{
if (--_suggestionSelectedIndex < 0)
_suggestionSelectedIndex = static_cast<i32>(numSuggestions) - 1;
}
else if (ImGui::IsKeyReleased(ImGuiKey_DownArrow))
{
if (++_suggestionSelectedIndex >= static_cast<i32>(numSuggestions))
_suggestionSelectedIndex = 0;
}
}
for (u32 suggestionIndex = 0; suggestionIndex < numSuggestions; suggestionIndex++)
{
const auto suggestion = _commandHashSuggestions[suggestionIndex];
const auto& command = commands.at(suggestion);
bool isSelected = suggestionIndex == _suggestionSelectedIndex;
if (ImGui::Selectable(command.nameWithAliases.data()) || (isSelected && tabReleased))
{
_searchText = command.name;
if (command.hasParameters)
_searchText += " ";
ImGuiID inputFieldID = gameConsoleWindow->GetID("##ConsoleInputField");
ImGui::GetInputTextState(inputFieldID)->ReloadUserBufAndMoveToEnd();
ImGui::CloseCurrentPopup();
break;
}
ImVec2 min = ImGui::GetItemRectMin();
if (command.help.length() > 0)
{
ImGui::SameLine();
ImGui::TextDisabled("- %s", command.help.data());
}
if (isSelected)
{
static u32 hoveredColor = ImGui::GetColorU32(ImVec4(0.7f, 0.8f, 1.0f, 0.3f));
ImVec2 max = ImGui::GetItemRectMax();
ImGui::GetWindowDrawList()->AddRectFilled(min, max, hoveredColor);
}
// Insert a tiny line under each suggestion except the last
if (suggestionIndex < numSuggestions - 1)
ImGui::Separator();
}
ImGui::EndPopup();
}
ImGui::OpenPopup("SuggestionsPopup");
}
}
if (handleInput)
{
ImGui::SetKeyboardFocusHere(-1);
}
_textFieldHasFocus = ImGui::IsItemActive();
}
ImGui::End();
ImGui::PopStyleColor();
}
void GameConsole::Clear()
{
_lines.clear();
std::string dummy;
while (_linesToAppend.try_dequeue(dummy)) {}
}
void GameConsole::Toggle()
{
if (IsEnabled())
{
Disable();
}
else
{
Enable();
}
}
bool GameConsole::IsEnabled()
{
return CVAR_GameConsoleEnabled.Get();
}
void GameConsole::Enable()
{
CVAR_GameConsoleEnabled.Set(1);
_visibleProgressTimer = 0.0f;
}
void GameConsole::Disable()
{
CVAR_GameConsoleEnabled.Set(0);
}