RigsofRods
Soft-body Physics Simulation
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 
31 using namespace RoR;
32 using namespace GUI;
33 using namespace Ogre;
34 
36 {
37  m_console_view.cvw_enable_scrolling = true;
38 }
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  {
52  m_console_view.DrawFilteringOptions();
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 
89  m_angelscript_examples.Draw();
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)
99  m_script_monitor.Draw();
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 
109  m_console_view.DrawConsoleMessages();
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  {
116  this->doCommand(m_cmd_buffer.ToCStr());
117  m_cmd_buffer.Clear();
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 
131 void 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  }
145  m_cmd_history_cursor = -1;
146 
147  App::GetConsole()->doCommand(msg);
148 }
149 
150 int ConsoleWindow::TextEditCallback(ImGuiTextEditCallbackData *data)
151 {
152  ConsoleWindow* c = static_cast<ConsoleWindow*>(data->UserData);
153  c->TextEditCallbackProc(data);
154  return 0;
155 }
156 
157 void 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  {
170  m_cmd_history_cursor--;
171  }
172  }
173  else if (data->EventKey == ImGuiKey_DownArrow)
174  {
175  if (m_cmd_history_cursor != -1)
176  {
177  ++m_cmd_history_cursor;
178 
179  if (m_cmd_history_cursor >= static_cast<int>(m_cmd_history.size()))
180  {
181  m_cmd_history_cursor = -1;
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 
RoR::App::GetGuiManager
GUIManager * GetGuiManager()
Definition: Application.cpp:269
GUI_AngelScriptExamples.h
GUI_ConsoleWindow.h
Language.h
GUIManager.h
Actor.h
RoR::GUI::ConsoleWindow::ConsoleWindow
ConsoleWindow()
Definition: GUI_ConsoleWindow.cpp:35
RoR::GUI::ConsoleWindow::Draw
void Draw()
Definition: GUI_ConsoleWindow.cpp:40
RoR::GUI::ConsoleWindow::doCommand
void doCommand(std::string msg)
Definition: GUI_ConsoleWindow.cpp:131
RoR::App::GetConsole
Console * GetConsole()
Definition: Application.cpp:270
RoR::Console::doCommand
void doCommand(std::string msg)
Identify and execute any console line.
Definition: ConsoleCmd.cpp:678
_LC
#define _LC(ctx, str)
Definition: Language.h:42
RoR::GUI::ConsoleWindow::TextEditCallback
static int TextEditCallback(ImGuiTextEditCallbackData *data)
Definition: GUI_ConsoleWindow.cpp:150
Ogre
Definition: ExtinguishableFireAffector.cpp:35
RoR::GUI::ConsoleWindow::TextEditCallbackProc
void TextEditCallbackProc(ImGuiTextEditCallbackData *data)
Definition: GUI_ConsoleWindow.cpp:157
RoR::GUI::ConsoleWindow
Definition: GUI_ConsoleWindow.h:40
RoR::GUIManager::RequestGuiCaptureKeyboard
void RequestGuiCaptureKeyboard(bool val)
Pass true during frame to prevent input passing to application.
Definition: GUIManager.cpp:439
RoR
Definition: AppContext.h:36