Rigs of Rods 2026.01
Soft-body Physics Simulation
Loading...
Searching...
No Matches
GUI_ConsoleWindow.cpp
Go to the documentation of this file.
1/*
2 This source file is part of Rigs of Rods
3 Copyright 2005-2012 Pierre-Michel Ricordel
4 Copyright 2007-2012 Thomas Fischer
5 Copyright 2013-2014 Petr Ohlidal
6
7 For more information, see http://www.rigsofrods.org/
8
9 Rigs of Rods is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License version 3, as
11 published by the Free Software Foundation.
12
13 Rigs of Rods is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22
23#include "GUI_ConsoleWindow.h"
24
25#include "Actor.h"
26#include "GUIManager.h"
28
29#include "Language.h"
30
31using namespace RoR;
32using namespace GUI;
33using namespace Ogre;
34
39
41{
42 ImGuiWindowFlags win_flags = ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_MenuBar;
43 ImGui::SetNextWindowPosCenter(ImGuiCond_FirstUseEver);
44 ImGui::SetNextWindowSize(ImVec2((ImGui::GetIO().DisplaySize.x / 1.6), (ImGui::GetIO().DisplaySize.y / 1.3)), ImGuiCond_FirstUseEver);
45 bool keep_open = true;
46 ImGui::Begin("Console", &keep_open, win_flags);
47
48 if (ImGui::BeginMenuBar())
49 {
50 if (ImGui::BeginMenu(_LC("Console", "Filter options")))
51 {
53 ImGui::EndMenu();
54 }
55 if (ImGui::BeginMenu(_LC("Console", "Commands")))
56 {
57 ImGui::Dummy(ImVec2(ImGui::GetFontSize() * 43.75f, 1.f)); // Manually resize width (DearIMGUI bug workaround)
58 ImGui::Columns(3);
59 ImGui::SetColumnWidth(0, ImGui::GetFontSize() * 6.25f); // TODO: Calculate dynamically
60 ImGui::SetColumnWidth(1, ImGui::GetFontSize() * 10.625f); // TODO: Calculate dynamically
61 ImGui::SetColumnWidth(2, ImGui::GetFontSize() * 31.25f); // TODO: Calculate dynamically
62
63 for (auto& cmd_pair: App::GetConsole()->getCommands())
64 {
65 if (ImGui::Selectable(cmd_pair.second->getName().c_str()))
66 {
67 cmd_pair.second->Run(Ogre::StringVector{cmd_pair.second->getName()});
68 }
69 ImGui::NextColumn();
70 ImGui::Text("%s", cmd_pair.second->GetUsage().c_str());
71 ImGui::NextColumn();
72 ImGui::Text("%s", cmd_pair.second->GetDoc().c_str());
73 ImGui::NextColumn();
74 }
75
76 ImGui::Columns(1); // reset
77 ImGui::EndMenu();
78 }
79#ifdef USE_ANGELSCRIPT
80 if (ImGui::BeginMenu(_LC("Console", "AngelScript")))
81 {
82 ImGui::Dummy(ImVec2(ImGui::GetFontSize() * 49.f, 1.f)); // Manually resize width (DearIMGUI bug workaround)
83 ImGui::Columns(3);
84 ImGui::SetColumnWidth(0, ImGui::GetFontSize() * 14.375f);
85 ImGui::SetColumnWidth(1, ImGui::GetFontSize() * 10.f);
86 ImGui::SetColumnWidth(2, ImGui::GetFontSize() * 25.f);
87
89
90 ImGui::Columns(1); // reset
91 ImGui::EndMenu();
92 }
93
94 if (ImGui::BeginMenu(_LC("Console", "Script Monitor")))
95 {
96 ImGui::Dummy(ImVec2(ImGui::GetFontSize() * 27.5f, 1.f)); // Manually resize width (DearIMGUI bug workaround)
98 ImGui::EndMenu();
99 }
100#endif
101 ImGui::EndMenuBar();
102 }
103
104 const float footer_height_to_reserve = ImGui::GetFrameHeightWithSpacing(); // 1 input text
105 ImGui::BeginChild("ScrollingRegion", ImVec2(0, -footer_height_to_reserve), false, ImGuiWindowFlags_HorizontalScrollbar); // Leave room for 1 separator + 1 InputText
106
108
109 ImGui::EndChild();
110
111 const ImGuiInputTextFlags cmd_flags = ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackHistory;
112 if (ImGui::InputText(_LC("Console", "Command"), m_cmd_buffer.GetBuffer(), m_cmd_buffer.GetCapacity(), cmd_flags, &ConsoleWindow::TextEditCallback, this))
113 {
116 }
117
118 m_is_hovered = ImGui::IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows);
120
121 ImGui::End();
122
123 if (!keep_open)
124 {
125 this->SetVisible(false);
126 }
127}
128
129void ConsoleWindow::doCommand(std::string msg) // All commands are processed here
130{
131 Ogre::StringUtil::trim(msg);
132 if (msg.empty())
133 {
134 // discard the empty message
135 return;
136 }
137
138 m_cmd_history.push_back(msg);
139 if (m_cmd_history.size() > HISTORY_CAP)
140 {
141 m_cmd_history.erase(m_cmd_history.begin());
142 }
144
146}
147
148int ConsoleWindow::TextEditCallback(ImGuiTextEditCallbackData *data)
149{
150 ConsoleWindow* c = static_cast<ConsoleWindow*>(data->UserData);
151 c->TextEditCallbackProc(data);
152 return 0;
153}
154
155void ConsoleWindow::TextEditCallbackProc(ImGuiTextEditCallbackData *data)
156{
157 if (data->EventFlag == ImGuiInputTextFlags_CallbackHistory)
158 {
159 const int prev_cursor = m_cmd_history_cursor;
160 if (data->EventKey == ImGuiKey_UpArrow)
161 {
162 if (m_cmd_history_cursor == -1)
163 {
164 m_cmd_history_cursor = static_cast<int>(m_cmd_history.size()) - 1;
165 }
166 else if (m_cmd_history_cursor > 0)
167 {
169 }
170 }
171 else if (data->EventKey == ImGuiKey_DownArrow)
172 {
173 if (m_cmd_history_cursor != -1)
174 {
176
177 if (m_cmd_history_cursor >= static_cast<int>(m_cmd_history.size()))
178 {
180 }
181 }
182 }
183
184 if (m_cmd_history_cursor != prev_cursor)
185 {
186 const char* text = (m_cmd_history_cursor >= 0) ? m_cmd_history.at(m_cmd_history_cursor).c_str() : "";
187 data->DeleteChars(0, data->BufTextLen);
188 data->InsertChars(0, text);
189 }
190 }
191}
192
#define _LC(ctx, str)
Definition Language.h:38
CommandPtrMap & getCommands()
Definition Console.h:134
std::vector< std::string > m_cmd_history
AngelScriptExamples m_angelscript_examples
void SetVisible(bool visible)
void TextEditCallbackProc(ImGuiTextEditCallbackData *data)
void doCommand(std::string msg)
static const size_t HISTORY_CAP
static int TextEditCallback(ImGuiTextEditCallbackData *data)
void RequestGuiCaptureKeyboard(bool val)
Pass true during frame to prevent input passing to application.
const char * ToCStr() const
Definition Str.h:46
Str & Clear()
Definition Str.h:54
char * GetBuffer()
Definition Str.h:48
size_t GetCapacity() const
Definition Str.h:49
void doCommand(std::string msg)
Identify and execute any console line.
GUIManager * GetGuiManager()
Console * GetConsole()