RigsofRods
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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();
62  {
63  ImGui::Text("%s", unit.originatingGadget->fname.c_str());
64  }
65  else
66  {
67  ImGui::Text("%s", unit.scriptName.c_str());
68  }
69  ImGui::NextColumn();
70  switch (unit.scriptCategory)
71  {
73  ImGui::Text("({} [%u] '%s')", _LC("ScriptMonitor", "actor"), unit.associatedActor->ar_vector_index, unit.associatedActor->getTruckName().c_str());
74  break;
75 
77  ImGui::Text("%s", _LC("ScriptMonitor", "(terrain)"));
78  break;
79 
82  {
83  std::string filename = unit.scriptName;
85  {
86  filename = unit.originatingGadget->fname;
87  }
88 
89  if (ImGui::Button(_LC("ScriptMonitor", "Reload")))
90  {
93  req->lsr_category = unit.scriptCategory;
94  req->lsr_filename = filename;
96  }
97  ImGui::SameLine();
98  if (ImGui::Button(_LC("ScriptMonitor", "Stop")))
99  {
101  }
102 
103  ImGui::SameLine();
104  bool autoload_set = std::find(autoload.begin(), autoload.end(), filename) != autoload.end();
105  if (ImGui::Checkbox(_LC("ScriptMonitor", "Autoload"), &autoload_set))
106  {
107  if (autoload_set)
109  else
111  }
112  break;
113  }
114  default:;
115  }
116 
117  ImGui::PopID(); // ScriptUnitID_t id
118  }
119 
120  if (App::app_recent_scripts->getStr() != "")
121  {
122  // Prepare display list for recent scripts
123  m_recent_displaylist.clear();
124  StringVector recent = StringUtil::split(App::app_recent_scripts->getStr(), ",");
125  for (String& filename : recent)
126  {
127  bool is_running = std::find_if(
128  App::GetScriptEngine()->getScriptUnits().begin(),
129  App::GetScriptEngine()->getScriptUnits().end(),
130  [filename](ScriptUnitMap::const_iterator::value_type pair) { return filename == pair.second.scriptName; })
131  != App::GetScriptEngine()->getScriptUnits().end();
132  if (!is_running)
133  {
134  m_recent_displaylist.push_back(filename);
135  }
136  }
137 
138  // Draw recent scripts from the displaylist
139  if (m_recent_displaylist.size() > 0)
140  {
141  this->DrawCommentedSeparator(_LC("ScriptMonitor", "Recent"));
142 
143  for (String& filename : m_recent_displaylist)
144  {
145  ImGui::PushID(filename.c_str());
146 
147  ImGui::NextColumn();
148  ImGui::NextColumn(); // skip "ID"
149  ImGui::AlignTextToFramePadding();
150  ImGui::Text("%s", filename.c_str());
151  ImGui::NextColumn();
152  float cursorx = ImGui::GetCursorPosX();
153  if (ImGui::Button(_LC("ScriptMonitor", "Load")))
154  {
157  req->lsr_filename = filename;
159  }
160 
161  ImVec2 rem_size = ImGui::CalcTextSize(_LC("ScriptMonitor", "Remove"));
162  ImGui::SameLine();
163  ImGui::SetCursorPosX(((cursorx + 190) - rem_size.x) - 2*ImGui::GetStyle().FramePadding.x);
164  if (ImGui::SmallButton(_LC("ScriptMonitor", "Remove")))
165  {
167  }
168 
169  ImGui::PopID(); // filename.c_str()
170  }
171  }
172  }
173 
174  ImGui::Columns(1); // reset
175 }
176 
178 {
179  ImGui::NextColumn(); // begin new row
180  ImGui::NextColumn(); // skip ID column
181  ImVec2 pos = ImGui::GetCursorScreenPos() + ImVec2(10.f, 0.f);
182  ImGui::Dummy(ImVec2(0.1f, 2.5f));
183  ImGui::Separator();
184  ImGui::Dummy(ImVec2(0.1f, 2.5f));
185  ImDrawList* drawlist = ImGui::GetWindowDrawList();
186  ImVec2 padding(5.f, 0.f);
187  ImVec2 rect_max = pos + padding*2 + ImGui::CalcTextSize(text);
188  drawlist->AddRectFilled(pos, rect_max, ImColor(ImGui::GetStyle().Colors[ImGuiCol_PopupBg]), ImGui::GetStyle().WindowRounding);
189  drawlist->AddText(pos + padding, ImColor(ImGui::GetStyle().Colors[ImGuiCol_TextDisabled]), text);
190  ImGui::NextColumn(); // skip Name column
191 }
RoR::ScriptUnit
Represents a loaded script and all associated resources/handles.
Definition: ScriptEngine.h:70
GameContext.h
Game state manager and message-queue provider.
RoR::ScriptEngine::getScriptUnits
ScriptUnitMap const & getScriptUnits() const
Definition: ScriptEngine.h:303
RoR::LoadScriptRequest::lsr_filename
std::string lsr_filename
Load from resource ('.as' file or '.gadget' file); If buffer is supplied, use this as display name on...
Definition: ScriptEngine.h:94
RoR::GUI::ScriptMonitor::Draw
void Draw()
Definition: GUI_ScriptMonitor.cpp:32
RoR::ScriptUnit::originatingGadget
CacheEntryPtr originatingGadget
For ScriptCategory::GADGET ~ determines resource group.
Definition: ScriptEngine.h:84
RoR::GUI::ScriptMonitor::DrawCommentedSeparator
void DrawCommentedSeparator(const char *text)
Definition: GUI_ScriptMonitor.cpp:177
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:283
RoR::LoadScriptRequest::lsr_category
ScriptCategory lsr_category
Definition: ScriptEngine.h:96
RoR::GameContext::ChainMessage
void ChainMessage(Message m)
Add to last pushed message's chain.
Definition: GameContext.cpp:73
RoR::ScriptUnit::scriptName
Ogre::String scriptName
Name of the '.as' file exclusively.
Definition: ScriptEngine.h:85
ScriptEngine.h
RoR::ScriptUnit::scriptCategory
ScriptCategory scriptCategory
Definition: ScriptEngine.h:76
RoR::GameContext::PushMessage
void PushMessage(Message m)
Doesn't guarantee order! Use ChainMessage() if order matters.
Definition: GameContext.cpp:66
GUI_ScriptMonitor.h
RoR::App::GetGameContext
GameContext * GetGameContext()
Definition: Application.cpp:284
RoR::LoadScriptRequest
Definition: ScriptEngine.h:92
_LC
#define _LC(ctx, str)
Definition: Language.h:38
RoR::CvarAddFileToList
void CvarAddFileToList(CVar *cvar, const std::string &filename)
Definition: Utils.cpp:217
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:83
RoR::CvarRemoveFileFromList
void CvarRemoveFileFromList(CVar *cvar, const std::string &filename)
Definition: Utils.cpp:227
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::ScriptUnitID_t
int ScriptUnitID_t
Unique sequentially generated ID of a loaded and running scriptin session. Use ScriptEngine::getScrip...
Definition: ForwardDeclarations.h:41
RoR::ScriptCategory::GADGET
@ GADGET
Associated with a .gadget mod file, launched via UI or any method given below for CUSTOM scripts (use...
RoR::Actor::getTruckName
std::string getTruckName()
Definition: Actor.h:236
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...
RoR::CacheEntry::fname
Ogre::String fname
filename
Definition: CacheSystem.h:67