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_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
28using namespace RoR;
29using namespace GUI;
30using 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; })
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}
Game state manager and message-queue provider.
#define _LC(ctx, str)
Definition Language.h:38
unsigned int ar_vector_index
Sim attr; actor element index in std::vector<m_actors>
Definition Actor.h:430
std::string getTruckName()
Definition Actor.h:266
Ogre::String fname
filename
Definition CacheSystem.h:67
void DrawCommentedSeparator(const char *text)
Ogre::StringVector m_recent_displaylist
void PushMessage(Message m)
Doesn't guarantee order! Use ChainMessage() if order matters.
void ChainMessage(Message m)
Add to last pushed message's chain.
ScriptUnitMap const & getScriptUnits() const
@ MSG_APP_UNLOAD_SCRIPT_REQUESTED
Payload = RoR::ScriptUnitId_t* (owner)
Definition Application.h:94
@ MSG_APP_LOAD_SCRIPT_REQUESTED
Payload = RoR::LoadScriptRequest* (owner)
Definition Application.h:93
@ CUSTOM
Loaded by user via either: A) ingame console 'loadscript'; B) RoR.cfg 'app_custom_scripts'; C) comman...
@ GADGET
Associated with a .gadget mod file, launched via UI or any method given below for CUSTOM scripts (use...
@ TERRAIN
Defined in terrn2 file under '[Scripts]', receives terrain eventbox notifications.
@ ACTOR
Defined in truck file under 'scripts', contains global variable BeamClass@ thisActor.
CVar * app_recent_scripts
GameContext * GetGameContext()
ScriptEngine * GetScriptEngine()
CVar * app_custom_scripts
void CvarAddFileToList(CVar *cvar, const std::string &filename)
Definition Utils.cpp:217
int ScriptUnitID_t
Unique sequentially generated ID of a loaded and running scriptin session. Use ScriptEngine::getScrip...
void CvarRemoveFileFromList(CVar *cvar, const std::string &filename)
Definition Utils.cpp:227
std::string lsr_filename
Load from resource ('.as' file or '.gadget' file); If buffer is supplied, use this as display name on...
ScriptCategory lsr_category
Unified game event system - all requests and state changes are reported using a message.
Definition GameContext.h:52
Represents a loaded script and all associated resources/handles.
CacheEntryPtr originatingGadget
For ScriptCategory::GADGET ~ determines resource group.
ScriptCategory scriptCategory
Ogre::String scriptName
Name of the '.as' file exclusively.
ActorPtr associatedActor
For ScriptCategory::ACTOR.