Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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(700.f, 1.f)); // Manually resize width (DearIMGUI bug workaround)
58 ImGui::Columns(3);
59 ImGui::SetColumnWidth(0, 100); // TODO: Calculate dynamically
60 ImGui::SetColumnWidth(1, 170); // TODO: Calculate dynamically
61 ImGui::SetColumnWidth(2, 500); // 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 ImGui::SetNextWindowSize(ImVec2((ImGui::GetIO().DisplaySize.x / 2), (ImGui::GetIO().DisplaySize.y / 1.5)));
81 if (ImGui::BeginMenu(_LC("Console", "AngelScript")))
82 {
83 ImGui::Dummy(ImVec2(720.f, 1.f)); // Manually resize width (DearIMGUI bug workaround)
84 ImGui::Columns(3);
85 ImGui::SetColumnWidth(0, 230);
86 ImGui::SetColumnWidth(1, 160);
87 ImGui::SetColumnWidth(2, 400);
88
90
91 ImGui::Columns(1); // reset
92 ImGui::EndMenu();
93 }
94 ImGui::SetNextWindowSize(ImVec2(0.f, 0.f)); // reset to auto-fit
95
96 if (ImGui::BeginMenu(_LC("Console", "Script Monitor")))
97 {
98 ImGui::Dummy(ImVec2(440.f, 1.f)); // Manually resize width (DearIMGUI bug workaround)
100 ImGui::EndMenu();
101 }
102#endif
103 ImGui::EndMenuBar();
104 }
105
106 const float footer_height_to_reserve = ImGui::GetFrameHeightWithSpacing(); // 1 input text
107 ImGui::BeginChild("ScrollingRegion", ImVec2(0, -footer_height_to_reserve), false, ImGuiWindowFlags_HorizontalScrollbar); // Leave room for 1 separator + 1 InputText
108
110
111 ImGui::EndChild();
112
113 const ImGuiInputTextFlags cmd_flags = ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackHistory;
114 if (ImGui::InputText(_LC("Console", "Command"), m_cmd_buffer.GetBuffer(), m_cmd_buffer.GetCapacity(), cmd_flags, &ConsoleWindow::TextEditCallback, this))
115 {
118 }
119
120 m_is_hovered = ImGui::IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows);
122
123 ImGui::End();
124
125 if (!keep_open)
126 {
127 this->SetVisible(false);
128 }
129}
130
131void ConsoleWindow::doCommand(std::string msg) // All commands are processed here
132{
133 Ogre::StringUtil::trim(msg);
134 if (msg.empty())
135 {
136 // discard the empty message
137 return;
138 }
139
140 m_cmd_history.push_back(msg);
141 if (m_cmd_history.size() > HISTORY_CAP)
142 {
143 m_cmd_history.erase(m_cmd_history.begin());
144 }
146
148}
149
150int ConsoleWindow::TextEditCallback(ImGuiTextEditCallbackData *data)
151{
152 ConsoleWindow* c = static_cast<ConsoleWindow*>(data->UserData);
153 c->TextEditCallbackProc(data);
154 return 0;
155}
156
157void ConsoleWindow::TextEditCallbackProc(ImGuiTextEditCallbackData *data)
158{
159 if (data->EventFlag == ImGuiInputTextFlags_CallbackHistory)
160 {
161 const int prev_cursor = m_cmd_history_cursor;
162 if (data->EventKey == ImGuiKey_UpArrow)
163 {
164 if (m_cmd_history_cursor == -1)
165 {
166 m_cmd_history_cursor = static_cast<int>(m_cmd_history.size()) - 1;
167 }
168 else if (m_cmd_history_cursor > 0)
169 {
171 }
172 }
173 else if (data->EventKey == ImGuiKey_DownArrow)
174 {
175 if (m_cmd_history_cursor != -1)
176 {
178
179 if (m_cmd_history_cursor >= static_cast<int>(m_cmd_history.size()))
180 {
182 }
183 }
184 }
185
186 if (m_cmd_history_cursor != prev_cursor)
187 {
188 const char* text = (m_cmd_history_cursor >= 0) ? m_cmd_history.at(m_cmd_history_cursor).c_str() : "";
189 data->DeleteChars(0, data->BufTextLen);
190 data->InsertChars(0, text);
191 }
192 }
193}
194
#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()