OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
Console.cpp
Go to the documentation of this file.
1 
8 #include <cstring>
9 
10 #include "imgui/imgui.h"
11 
12 #include "global.h"
13 #include "Log.h"
14 #include "commands/Command.h"
15 #include "Console.h"
16 
17 bool Console::visible = false;
18 char Console::buffer[bufferLength + 1] = "";
19 unsigned long Console::lastLogLength = 0;
20 std::vector<std::string> Console::lastCommands;
22 std::string Console::bufferedCommand;
23 
25  bool update = false;
26  std::string completion;
27 
28  switch (data->EventKey) {
29  case ImGuiKey_Tab:
30  completion = Command::autoComplete(data->Buf);
31  if (completion.size() > 0) {
32  data->DeleteChars(0, data->BufSize);
33  data->InsertChars(0, completion.c_str());
34  data->CursorPos = strlen(data->Buf);
35  }
36  break;
37 
38  case ImGuiKey_UpArrow:
39  update = true;
40  if (lastCommandIndex < (long)(lastCommands.size() - 1)) {
42  if (lastCommandIndex == 0)
43  bufferedCommand = data->Buf;
44  }
45  break;
46 
47  case ImGuiKey_DownArrow:
48  update = true;
49  if (lastCommandIndex > -1)
51  break;
52  }
53 
54  if (update) {
55  if ((lastCommandIndex >= 0) && (lastCommandIndex < lastCommands.size())) {
56  if (lastCommands.at(lastCommands.size() - 1 - lastCommandIndex) != buffer) {
57  data->DeleteChars(0, data->BufSize);
58  data->InsertChars(0, lastCommands.at(lastCommands.size() - 1 - lastCommandIndex).c_str());
59  }
60  } else {
61  data->DeleteChars(0, data->BufSize);
62  if (bufferedCommand.size() > 0) {
63  data->InsertChars(0, bufferedCommand.c_str());
64  }
65  }
66 
67  data->CursorPos = strlen(data->Buf);
68  }
69 
70  return 0;
71 }
72 
74  if (!visible)
75  return;
76 
77  if (ImGui::Begin("Console", &visible, ImVec2(600, 400))) {
78  static bool scrollToBottom = false;
79  if (lastLogLength != Log::size()) {
81  scrollToBottom = true;
82  }
83 
84  static bool visibleLogs[LOG_COUNT] = { true, true, true, true, true };
85  ImGui::Checkbox("Error##log", &visibleLogs[1]);
87  ImGui::Checkbox("Warning##log", &visibleLogs[2]);
89  ImGui::Checkbox("User##log", &visibleLogs[0]);
91  ImGui::Checkbox("Info##log", &visibleLogs[3]);
93  ImGui::Checkbox("Debug##log", &visibleLogs[4]);
94 
95  static bool logToTTY = false, logToClipboard = false, logToFile = false;
96  if (ImGui::Button("Log to TTY")) { logToTTY = true; }
98  if (ImGui::Button("Log to Clipboard")) { logToClipboard = true; }
100  if (ImGui::Button("Log to File")) { logToFile = true; }
102 
104  if (logToTTY)
105  ImGui::LogToTTY();
106  else if (logToClipboard)
108  else if (logToFile)
110  for (unsigned long i = 0; i < Log::size(); i++) {
111  auto& entry = Log::getEntry(i);
112 
113  assertLessThan(entry.level, LOG_COUNT);
114  if (!visibleLogs[entry.level]) {
115  continue;
116  }
117 
118  ImVec4 col(1.0f, 1.0f, 1.0f, 1.0f);
119  if (entry.level == LOG_ERROR) {
120  col = ImVec4(1.0f, 0.0f, 0.0f, 1.0f);
121  } else if (entry.level == LOG_WARNING) {
122  col = ImVec4(1.0f, 1.0f, 0.0f, 1.0f);
123  } else if (entry.level == LOG_DEBUG) {
124  col = ImVec4(0.0f, 1.0f, 0.0f, 1.0f);
125  } else if (entry.level == LOG_USER) {
126  col = ImVec4(0.5f, 0.75f, 1.0f, 1.0f);
127  }
128 
130  ImGui::TextWrapped("%s", entry.text.c_str());
132  }
133  if (logToTTY || logToClipboard || logToFile) {
135  logToTTY = logToClipboard = logToFile = false;
136  }
137  if (scrollToBottom) {
139  scrollToBottom = false;
140  }
141  ImGui::EndChild();
142 
143  bool focusInput = false;
144  if (ImGui::InputText("Command", buffer, bufferLength,
148  &Console::callback)) {
149  Log::get(LOG_USER) << "> " << buffer << Log::endl;
150  if (strlen(buffer) > 0) {
152  if (error != 0) {
153  Log::get(LOG_USER) << "Error code: " << error << Log::endl;
154  }
155 
156  if ((lastCommands.size() == 0) || (lastCommands[lastCommands.size() - 1] != buffer))
157  lastCommands.push_back(std::string(buffer));
158  }
159 
160  lastCommandIndex = -1;
161  buffer[0] = '\0';
162  scrollToBottom = true;
163  focusInput = true;
164  bufferedCommand.clear();
165  }
166 
167  if (ImGui::IsItemHovered() || focusInput) {
169  }
170  }
171  ImGui::End();
172 }
173 
#define LOG_WARNING
Definition: Log.h:20
static LogEntry & getEntry(unsigned long i)
Definition: Log.h:39
static long lastCommandIndex
Definition: Console.h:32
static unsigned long lastLogLength
Definition: Console.h:30
static void display()
Definition: Console.cpp:73
static int callback(ImGuiTextEditCallbackData *data)
Definition: Console.cpp:24
IMGUI_API bool Checkbox(const char *label, bool *v)
Definition: imgui.cpp:5128
static std::string bufferedCommand
Definition: Console.h:33
#define LOG_USER
Definition: Log.h:18
static const int bufferLength
Definition: Console.h:26
static std::string autoComplete(std::string begin)
Definition: Command.cpp:122
IMGUI_API void PopStyleColor(int count=1)
Definition: imgui.cpp:3345
static std::vector< std::string > lastCommands
Definition: Console.h:31
IMGUI_API void LogToTTY(int max_depth=-1)
Definition: imgui.cpp:4228
IMGUI_API void TextWrapped(const char *fmt,...)
Definition: imgui.cpp:3832
IMGUI_API void End()
Definition: imgui.cpp:3137
static int command(std::string c)
Definition: Command.cpp:38
Commands.
Definition: imgui.h:61
Included everywhere.
static LogLevel & get(int level)
Definition: Log.cpp:14
IMGUI_API float GetTextLineHeightWithSpacing()
Definition: imgui.cpp:3672
Definition: imgui.h:50
Global Logging Utility.
IMGUI_API bool InputText(const char *label, char *buf, size_t buf_size, ImGuiInputTextFlags flags=0, ImGuiTextEditCallback callback=NULL, void *user_data=NULL)
Definition: imgui.cpp:5577
void DeleteChars(int pos, int bytes_count)
Definition: imgui.cpp:5490
Console Window.
IMGUI_API bool Button(const char *label, const ImVec2 &size=ImVec2(0, 0), bool repeat_when_held=false)
Definition: imgui.cpp:4055
IMGUI_API void SetKeyboardFocusHere(int offset=0)
Definition: imgui.cpp:3772
static void error(char *msg)
Definition: commander.c:19
#define LOG_DEBUG
Definition: Log.h:22
#define LOG_COUNT
Definition: Log.h:23
static const char endl
Definition: Log.h:35
IMGUI_API void SameLine(int column_x=0, int spacing_w=-1)
Definition: imgui.cpp:6551
IMGUI_API void EndChild()
Definition: imgui.cpp:2623
#define LOG_ERROR
Definition: Log.h:19
IMGUI_API bool IsItemHovered()
Definition: imgui.cpp:2504
IMGUI_API void SetScrollPosHere()
Definition: imgui.cpp:3766
void InsertChars(int pos, const char *text, const char *text_end=NULL)
Definition: imgui.cpp:5506
IMGUI_API void Separator()
Definition: imgui.cpp:6448
static char buffer[bufferLength+1]
Definition: Console.h:29
IMGUI_API void LogFinish()
Definition: imgui.cpp:4279
#define assertLessThan(x, y)
Definition: global.h:146
static unsigned long size()
Definition: Log.h:38
IMGUI_API void LogToClipboard(int max_depth=-1)
Definition: imgui.cpp:4265
IMGUI_API void LogToFile(int max_depth=-1, const char *filename=NULL)
Definition: imgui.cpp:4243
IMGUI_API bool BeginChild(const char *str_id, const ImVec2 &size=ImVec2(0, 0), bool border=false, ImGuiWindowFlags extra_flags=0)
Definition: imgui.cpp:2577
IMGUI_API bool Begin(const char *name="Debug", bool *p_opened=NULL, const ImVec2 &initial_size=ImVec2(0, 0), float bg_alpha=-1.0f, ImGuiWindowFlags flags=0)
Definition: imgui.cpp:2735
static bool visible
Definition: Console.h:28
IMGUI_API void PushStyleColor(ImGuiCol idx, const ImVec4 &col)
Definition: imgui.cpp:3334