RigsofRods
Soft-body Physics Simulation
GUI_ScriptMonitor.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2021 tritonas00
4  For more information, see http://www.rigsofrods.org/
5  Rigs of Rods is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License version 3, as
7  published by the Free Software Foundation.
8  Rigs of Rods is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  You should have received a copy of the GNU General Public License
13  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
14 */
15 
16 
17 #include "GUI_ScriptMonitor.h"
18 
19 #include "Actor.h"
20 #include "ContentManager.h"
21 #include "GameContext.h"
22 #include "ScriptEngine.h"
23 #include "Utils.h"
24 
25 #include <Ogre.h>
26 #include "OgreImGui.h"
27 
28 using namespace RoR;
29 using namespace GUI;
30 using namespace Ogre;
31 
33 {
34  // Table setup
35  ImGui::Columns(3);
36  ImGui::SetColumnWidth(0, 25);
37  ImGui::SetColumnWidth(1, 200);
38  ImGui::SetColumnWidth(2, 200);
39 
40  // Header
41  ImGui::TextDisabled(_LC("ScriptMonitor", "ID"));
42  ImGui::NextColumn();
43  ImGui::TextDisabled(_LC("ScriptMonitor", "File name"));
44  ImGui::NextColumn();
45  ImGui::TextDisabled(_LC("ScriptMonitor", "Options"));
46 
47  this->DrawCommentedSeparator(_LC("ScriptMonitor", "Active"));
48 
49  StringVector autoload = StringUtil::split(App::app_custom_scripts->getStr(), ",");
50  for (auto& pair : App::GetScriptEngine()->getScriptUnits())
51  {
52  ScriptUnitId_t id = pair.first;
53  ImGui::PushID(id);
54 
55  ScriptUnit const& unit = pair.second;
56  ImGui::NextColumn();
57  ImGui::AlignTextToFramePadding();
58  ImGui::TextDisabled("%d", id);
59  ImGui::NextColumn();
60  ImGui::AlignTextToFramePadding();
61  ImGui::Text("%s", unit.scriptName.c_str());
62  ImGui::NextColumn();
63  switch (unit.scriptCategory)
64  {
66  ImGui::Text("({} [%u] '%s')", _LC("ScriptMonitor", "actor"), unit.associatedActor->ar_vector_index, unit.associatedActor->getTruckName().c_str());
67  break;
68 
70  ImGui::Text("%s", _LC("ScriptMonitor", "(terrain)"));
71  break;
72 
74  {
75  if (ImGui::Button(_LC("ScriptMonitor", "Reload")))
76  {
79  req->lsr_category = unit.scriptCategory;
80  req->lsr_filename = unit.scriptName;
82  }
83  ImGui::SameLine();
84  if (ImGui::Button(_LC("ScriptMonitor", "Stop")))
85  {
87  }
88 
89  ImGui::SameLine();
90  bool autoload_set = std::find(autoload.begin(), autoload.end(), unit.scriptName) != autoload.end();
91  if (ImGui::Checkbox(_LC("ScriptMonitor", "Autoload"), &autoload_set))
92  {
93  if (autoload_set)
95  else
97  }
98  break;
99  }
100  default:;
101  }
102 
103  ImGui::PopID(); // ScriptUnitId_t id
104  }
105 
106  if (App::app_recent_scripts->getStr() != "")
107  {
108  // Prepare display list for recent scripts
109  m_recent_displaylist.clear();
110  StringVector recent = StringUtil::split(App::app_recent_scripts->getStr(), ",");
111  for (String& filename : recent)
112  {
113  bool is_running = std::find_if(
114  App::GetScriptEngine()->getScriptUnits().begin(),
115  App::GetScriptEngine()->getScriptUnits().end(),
116  [filename](ScriptUnitMap::const_iterator::value_type pair) { return filename == pair.second.scriptName; })
117  != App::GetScriptEngine()->getScriptUnits().end();
118  if (!is_running)
119  {
120  m_recent_displaylist.push_back(filename);
121  }
122  }
123 
124  // Draw recent scripts from the displaylist
125  if (m_recent_displaylist.size() > 0)
126  {
127  this->DrawCommentedSeparator(_LC("ScriptMonitor", "Recent"));
128 
129  for (String& filename : m_recent_displaylist)
130  {
131  ImGui::PushID(filename.c_str());
132 
133  ImGui::NextColumn();
134  ImGui::NextColumn(); // skip "ID"
135  ImGui::AlignTextToFramePadding();
136  ImGui::Text("%s", filename.c_str());
137  ImGui::NextColumn();
138  float cursorx = ImGui::GetCursorPosX();
139  if (ImGui::Button(_LC("ScriptMonitor", "Load")))
140  {
143  req->lsr_filename = filename;
145  }
146 
147  ImVec2 rem_size = ImGui::CalcTextSize(_LC("ScriptMonitor", "Remove"));
148  ImGui::SameLine();
149  ImGui::SetCursorPosX(((cursorx + 190) - rem_size.x) - 2*ImGui::GetStyle().FramePadding.x);
150  if (ImGui::SmallButton(_LC("ScriptMonitor", "Remove")))
151  {
153  }
154 
155  ImGui::PopID(); // filename.c_str()
156  }
157  }
158  }
159 
160  ImGui::Columns(1); // reset
161 }
162 
164 {
165  ImGui::NextColumn(); // begin new row
166  ImGui::NextColumn(); // skip ID column
167  ImVec2 pos = ImGui::GetCursorScreenPos() + ImVec2(10.f, 0.f);
168  ImGui::Dummy(ImVec2(0.1f, 2.5f));
169  ImGui::Separator();
170  ImGui::Dummy(ImVec2(0.1f, 2.5f));
171  ImDrawList* drawlist = ImGui::GetWindowDrawList();
172  ImVec2 padding(5.f, 0.f);
173  ImVec2 rect_max = pos + padding*2 + ImGui::CalcTextSize(text);
174  drawlist->AddRectFilled(pos, rect_max, ImColor(ImGui::GetStyle().Colors[ImGuiCol_PopupBg]), ImGui::GetStyle().WindowRounding);
175  drawlist->AddText(pos + padding, ImColor(ImGui::GetStyle().Colors[ImGuiCol_TextDisabled]), text);
176  ImGui::NextColumn(); // skip Name column
177 }
RoR::ScriptUnit
Represents a loaded script and all associated resources/handles.
Definition: ScriptEngine.h:69
GameContext.h
Game state manager and message-queue provider.
RoR::ScriptEngine::getScriptUnits
ScriptUnitMap const & getScriptUnits() const
Definition: ScriptEngine.h:235
RoR::LoadScriptRequest::lsr_filename
std::string lsr_filename
Load from resource (file). If buffer is supplied, use this as display name only.
Definition: ScriptEngine.h:92
RoR::GUI::ScriptMonitor::Draw
void Draw()
Definition: GUI_ScriptMonitor.cpp:32
RoR::GUI::ScriptMonitor::DrawCommentedSeparator
void DrawCommentedSeparator(const char *text)
Definition: GUI_ScriptMonitor.cpp:163
RoR::Actor::ar_vector_index
unsigned int ar_vector_index
Sim attr; actor element index in std::vector<m_actors>
Definition: Actor.h:377
ContentManager.h
RoR::MSG_APP_LOAD_SCRIPT_REQUESTED
@ MSG_APP_LOAD_SCRIPT_REQUESTED
Payload = RoR::LoadScriptRequest* (owner)
Definition: Application.h:92
Utils.h
OgreImGui.h
Actor.h
RoR::App::GetScriptEngine
ScriptEngine * GetScriptEngine()
Definition: Application.cpp:279
RoR::LoadScriptRequest::lsr_category
ScriptCategory lsr_category
Definition: ScriptEngine.h:94
RoR::GameContext::ChainMessage
void ChainMessage(Message m)
Add to last pushed message's chain.
Definition: GameContext.cpp:72
RoR::ScriptUnit::scriptName
Ogre::String scriptName
Definition: ScriptEngine.h:83
ScriptEngine.h
RoR::ScriptUnit::scriptCategory
ScriptCategory scriptCategory
Definition: ScriptEngine.h:75
RoR::GameContext::PushMessage
void PushMessage(Message m)
Doesn't guarantee order! Use ChainMessage() if order matters.
Definition: GameContext.cpp:65
RoR::ScriptUnitId_t
int ScriptUnitId_t
Unique sequentially generated ID of a loaded and running scriptin session. Use ScriptEngine::getScrip...
Definition: ForwardDeclarations.h:40
GUI_ScriptMonitor.h
RoR::App::GetGameContext
GameContext * GetGameContext()
Definition: Application.cpp:280
RoR::LoadScriptRequest
Definition: ScriptEngine.h:90
_LC
#define _LC(ctx, str)
Definition: Language.h:42
RoR::CvarAddFileToList
void CvarAddFileToList(CVar *cvar, const std::string &filename)
Definition: Utils.cpp:206
RoR::ScriptCategory::TERRAIN
@ TERRAIN
Defined in terrn2 file under '[Scripts]', receives terrain eventbox notifications.
RoR::ScriptUnit::associatedActor
ActorPtr associatedActor
For ScriptCategory::ACTOR.
Definition: ScriptEngine.h:82
RoR::CvarRemoveFileFromList
void CvarRemoveFileFromList(CVar *cvar, const std::string &filename)
Definition: Utils.cpp:216
RoR::App::app_recent_scripts
CVar * app_recent_scripts
Definition: Application.cpp:93
RoR::Message
Unified game event system - all requests and state changes are reported using a message.
Definition: GameContext.h:51
RoR::ScriptCategory::ACTOR
@ ACTOR
Defined in truck file under 'scripts', contains global variable BeamClass@ thisActor.
Ogre
Definition: ExtinguishableFireAffector.cpp:35
RoR::MSG_APP_UNLOAD_SCRIPT_REQUESTED
@ MSG_APP_UNLOAD_SCRIPT_REQUESTED
Payload = RoR::ScriptUnitId_t* (owner)
Definition: Application.h:93
RoR::Actor::getTruckName
std::string getTruckName()
Definition: Actor.h:226
RoR::App::app_custom_scripts
CVar * app_custom_scripts
Definition: Application.cpp:92
RoR
Definition: AppContext.h:36
RoR::ScriptCategory::CUSTOM
@ CUSTOM
Loaded by user via either: A) ingame console 'loadscript'; B) RoR.cfg 'app_custom_scripts'; C) comman...