RigsofRods
Soft-body Physics Simulation
GUI_AngelScriptExamples.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 
18 #include "ScriptEngine.h"
19 
20 using namespace RoR;
21 
23 {
24  this->DrawRowSlider("void scaleTruck()", "game.getCurrentTruck().scaleTruck({})", _LC("AngelScript", "Scales the truck"), 1.f, 1.5f, scale);
25  this->DrawRowText("string getTruckName()", "game.log(game.getCurrentTruck().getTruckName())", _LC("AngelScript", "Gets the name of the truck"));
26  this->DrawRowText("string getTruckFileName()", "game.log(game.getCurrentTruck().getTruckFileName())", _LC("AngelScript", "Gets the name of the truck file"));
27  this->DrawRowText("string getSectionConfig()", "game.log(game.getCurrentTruck().getSectionConfig())", _LC("AngelScript", "Gets the name of the loaded section for a truck"));
28  this->DrawRowText("int getTruckType()", "game.log(' ' + game.getCurrentTruck().getTruckType())", _LC("AngelScript", "Gets the type of the truck"));
29  this->DrawRowCheckbox("void reset()", "game.getCurrentTruck().reset({})", _LC("AngelScript", "Resets the truck"), reset, "Keep position");
30  this->DrawRowText("void parkingbrakeToggle()", "game.getCurrentTruck().parkingbrakeToggle()", _LC("AngelScript", "Toggles the parking brake"));
31  this->DrawRowText("void tractioncontrolToggle()", "game.getCurrentTruck().tractioncontrolToggle()", _LC("AngelScript", "Toggles the tracktion control"));
32  this->DrawRowText("void antilockbrakeToggle()", "game.getCurrentTruck().antilockbrakeToggle()", _LC("AngelScript", "Toggles the anti-lock brakes"));
33  this->DrawRowText("void beaconsToggle()", "game.getCurrentTruck().beaconsToggle()", _LC("AngelScript", "Toggles the beacons"));
34  this->DrawRowText("void toggleCustomParticles()", "game.getCurrentTruck().toggleCustomParticles()", _LC("AngelScript", "Toggles the custom particles"));
35  this->DrawRowText("int getNodeCount()", "game.log(' ' + game.getCurrentTruck().getNodeCount())", _LC("AngelScript", "Gets the total amount of nodes of the truck"));
36  this->DrawRowCheckbox("float getTotalMass()", "game.log(' ' + game.getCurrentTruck().getTotalMass({}))", _LC("AngelScript", "Gets the total mass of the truck"), locked, "With locked");
37  this->DrawRowText("int getWheelNodeCount()", "game.log(' ' + game.getCurrentTruck().getWheelNodeCount())", _LC("AngelScript", "Gets the total amount of nodes of the wheels of the truck"));
38  this->DrawRowSlider("void setMass()", "game.getCurrentTruck().setMass({})", _LC("AngelScript", "Sets the mass of the truck"), 1000.f, 10000.f, mass);
39  this->DrawRowText("bool getBrakeLightVisible()", "game.log(' ' + game.getCurrentTruck().getBrakeLightVisible())", _LC("AngelScript", "Returns true if the brake light is enabled"));
40  this->DrawRowInt("bool getCustomLightVisible()", "game.log(' ' + game.getCurrentTruck().getCustomLightVisible({}))", _LC("AngelScript", "Returns true if the custom light with the number is enabled"), light);
41  this->DrawRowIntCheckbox("void setCustomLightVisible()", "game.getCurrentTruck().setCustomLightVisible({}, {})", _LC("AngelScript", "Enables or disables the custom light"), custom_light, visible, "On");
42  this->DrawRowText("bool getBeaconMode()", "game.log(' ' + game.getCurrentTruck().getBeaconMode())", _LC("AngelScript", "Gets the mode of the beacon"));
43  this->DrawRowInt("void setBlinkType()", "game.getCurrentTruck().setBlinkType({})", _LC("AngelScript", "Sets the blinking type"), blink);
44  this->DrawRowText("int getBlinkType()", "game.log(' ' + game.getCurrentTruck().getBlinkType())", _LC("AngelScript", "Gets the blinking type"));
45  this->DrawRowText("bool getCustomParticleMode()", "game.log(' ' + game.getCurrentTruck().getCustomParticleMode())", _LC("AngelScript", "Gets the custom particles mode"));
46  this->DrawRowText("bool getReverseLightVisible()", "game.log(' ' + game.getCurrentTruck().getReverseLightVisible())", _LC("AngelScript", "Returns true if the reverse lights are enabled"));
47  this->DrawRowText("float getHeadingDirectionAngle()", "game.log(' ' + game.getCurrentTruck().getHeadingDirectionAngle())", _LC("AngelScript", "Returns the angle in which the truck is heading"));
48  this->DrawRowText("bool isLocked()", "game.log(' ' + game.getCurrentTruck().isLocked())", _LC("AngelScript", "Returns true if a hook of this truck is locked"));
49  this->DrawRowText("float getWheelSpeed()", "game.log(' ' + game.getCurrentTruck().getWheelSpeed())", _LC("AngelScript", "Gets the current wheel speed of the vehicle"));
50  this->DrawRowText("float getSpeed()", "game.log(' ' + game.getCurrentTruck().getSpeed())", _LC("AngelScript", "Gets the current speed of the vehicle"));
51  this->DrawRowText("vector3 getGForces()", "game.log(' ' + game.getCurrentTruck().getGForces().x + ' ' + game.getCurrentTruck().getGForces().y + ' ' + game.getCurrentTruck().getGForces().z )", _LC("AngelScript", "Gets the G-forces that this truck is currently experiencing"));
52  this->DrawRowText("float getRotation()", "game.log(' ' + game.getCurrentTruck().getRotation())", _LC("AngelScript", "Gets the current rotation of the vehicle"));
53  this->DrawRowText("vector3 getVehiclePosition()", "game.log(' ' + game.getCurrentTruck().getVehiclePosition().x + ' ' + game.getCurrentTruck().getVehiclePosition().y + ' ' + game.getCurrentTruck().getVehiclePosition().z )", _LC("AngelScript", "Gets the current position of the vehicle"));
54  this->DrawRowIntNode("vector3 getNodePosition()", "game.log(' ' + game.getCurrentTruck().getNodePosition({}).x + ' ' + game.getCurrentTruck().getNodePosition({}).y + ' ' + game.getCurrentTruck().getNodePosition({}).z )", _LC("AngelScript", "Gets the node position"), node, node, node, node);
55 }
56 
57 void AngelScriptExamples::DrawRowSlider(const char* nameStr, std::string codeStr, const char* descStr, float min, float max, float &var_ref)
58 {
59  ImGui::PushID(nameStr);
60  ImGui::AlignFirstTextHeightToWidgets();
61  if (ImGui::Selectable(nameStr)) { this->ExecuteString(fmt::format(codeStr, var_ref)); }
62  ImGui::NextColumn();
63  ImGui::AlignFirstTextHeightToWidgets();
64  ImGui::PushItemWidth(-1);
65  ImGui::SliderFloat(_LC("Console", ""), &var_ref, min, max);
66  ImGui::PopItemWidth();
67  ImGui::NextColumn();
68  ImGui::AlignFirstTextHeightToWidgets();
69  ImGui::Text("%s", descStr);
70  ImGui::PopID();
71  ImGui::NextColumn();
72 }
73 
74 void AngelScriptExamples::DrawRowText(const char* nameStr, std::string codeStr, const char* descStr)
75 {
76  ImGui::AlignFirstTextHeightToWidgets();
77  if (ImGui::Selectable(nameStr)) { this->ExecuteString(codeStr); }
78  ImGui::NextColumn();
79  ImGui::AlignFirstTextHeightToWidgets();
80  ImGui::Text("%s", "");
81  ImGui::NextColumn();
82  ImGui::AlignFirstTextHeightToWidgets();
83  ImGui::Text("%s", descStr);
84  ImGui::NextColumn();
85 }
86 
87 void AngelScriptExamples::DrawRowCheckbox(const char* nameStr, std::string codeStr, const char* descStr, bool &var_ref, const char* label)
88 {
89  ImGui::AlignFirstTextHeightToWidgets();
90  if (ImGui::Selectable(nameStr)) { this->ExecuteString(fmt::format(codeStr, var_ref)); }
91  ImGui::NextColumn();
92  ImGui::AlignFirstTextHeightToWidgets();
93  ImGui::Checkbox(label, &var_ref);
94  ImGui::NextColumn();
95  ImGui::AlignFirstTextHeightToWidgets();
96  ImGui::Text("%s", descStr);
97  ImGui::NextColumn();
98 }
99 
100 void AngelScriptExamples::DrawRowInt(const char* nameStr, std::string codeStr, const char* descStr, int &var_ref)
101 {
102  ImGui::PushID(nameStr);
103  ImGui::AlignFirstTextHeightToWidgets();
104  if (ImGui::Selectable(nameStr)) { this->ExecuteString(fmt::format(codeStr, var_ref)); }
105  ImGui::NextColumn();
106  ImGui::AlignFirstTextHeightToWidgets();
107  ImGui::PushItemWidth(-1);
108  ImGui::InputInt("", &var_ref, 1, 1);
109  ImGui::PopItemWidth();
110  ImGui::NextColumn();
111  ImGui::AlignFirstTextHeightToWidgets();
112  ImGui::Text("%s", descStr);
113  ImGui::PopID();
114  ImGui::NextColumn();
115 }
116 
117 void AngelScriptExamples::DrawRowIntNode(const char* nameStr, std::string codeStr, const char* descStr, int &var_ref, int &node_x, int &node_y, int &node_z)
118 {
119  ImGui::PushID(nameStr);
120  ImGui::AlignFirstTextHeightToWidgets();
121  if (ImGui::Selectable(nameStr)) { this->ExecuteString(fmt::format(codeStr, node_x, node_y, node_z)); }
122  ImGui::NextColumn();
123  ImGui::AlignFirstTextHeightToWidgets();
124  ImGui::PushItemWidth(-1);
125  ImGui::InputInt("", &var_ref, 1, 1);
126  ImGui::PopItemWidth();
127  ImGui::NextColumn();
128  ImGui::AlignFirstTextHeightToWidgets();
129  ImGui::Text("%s", descStr);
130  ImGui::PopID();
131  ImGui::NextColumn();
132 }
133 
134 void AngelScriptExamples::DrawRowIntCheckbox(const char* nameStr, std::string codeStr, const char* descStr, int &var_ref, bool &on, const char* label)
135 {
136  ImGui::PushID(nameStr);
137  ImGui::AlignFirstTextHeightToWidgets();
138  if (ImGui::Selectable(nameStr)) { this->ExecuteString(fmt::format(codeStr, var_ref, on)); }
139  ImGui::NextColumn();
140  ImGui::AlignFirstTextHeightToWidgets();
141  ImGui::PushItemWidth(96);
142  ImGui::InputInt("", &var_ref, 1, 1);
143  ImGui::PopItemWidth();
144  ImGui::SameLine();
145  ImGui::Checkbox(label, &on);
146  ImGui::NextColumn();
147  ImGui::AlignFirstTextHeightToWidgets();
148  ImGui::Text("%s", descStr);
149  ImGui::PopID();
150  ImGui::NextColumn();
151 }
152 
153 void AngelScriptExamples::ExecuteString(std::string const& code)
154 {
155  // Use console command 'as' because it echoes the entered code,
156  // which allows the user to see it and learn.
157  // Also reports error when not usable, i.e. in main menu.
158  App::GetConsole()->doCommand(fmt::format("as {}", code));
159 }
RoR::AngelScriptExamples::DrawRowText
void DrawRowText(const char *nameStr, std::string codeStr, const char *descStr)
Definition: GUI_AngelScriptExamples.cpp:74
RoR::AngelScriptExamples::ExecuteString
void ExecuteString(std::string const &code)
Runs code using 'as' console command.
Definition: GUI_AngelScriptExamples.cpp:153
RoR::AngelScriptExamples::DrawRowCheckbox
void DrawRowCheckbox(const char *nameStr, std::string codeStr, const char *descStr, bool &var_ref, const char *label)
Definition: GUI_AngelScriptExamples.cpp:87
format
Truck file format(technical spec)
RoR::AngelScriptExamples::DrawRowIntNode
void DrawRowIntNode(const char *nameStr, std::string codeStr, const char *descStr, int &var_ref, int &node_x, int &node_y, int &node_z)
Definition: GUI_AngelScriptExamples.cpp:117
GUI_AngelScriptExamples.h
RoR::AngelScriptExamples::light
int light
Definition: GUI_AngelScriptExamples.h:44
RoR::AngelScriptExamples::scale
float scale
Definition: GUI_AngelScriptExamples.h:40
RoR::AngelScriptExamples::visible
bool visible
Definition: GUI_AngelScriptExamples.h:47
RoR::AngelScriptExamples::DrawRowInt
void DrawRowInt(const char *nameStr, std::string codeStr, const char *descStr, int &var_ref)
Definition: GUI_AngelScriptExamples.cpp:100
ScriptEngine.h
RoR::AngelScriptExamples::blink
int blink
Definition: GUI_AngelScriptExamples.h:45
RoR::AngelScriptExamples::reset
bool reset
Definition: GUI_AngelScriptExamples.h:42
RoR::AngelScriptExamples::Draw
void Draw()
Definition: GUI_AngelScriptExamples.cpp:22
RoR::App::GetConsole
Console * GetConsole()
Definition: Application.cpp:270
RoR::AngelScriptExamples::mass
float mass
Definition: GUI_AngelScriptExamples.h:41
RoR::Console::doCommand
void doCommand(std::string msg)
Identify and execute any console line.
Definition: ConsoleCmd.cpp:678
RoR::AngelScriptExamples::custom_light
int custom_light
Definition: GUI_AngelScriptExamples.h:48
_LC
#define _LC(ctx, str)
Definition: Language.h:42
RoR::AngelScriptExamples::locked
bool locked
Definition: GUI_AngelScriptExamples.h:43
RoR::AngelScriptExamples::DrawRowIntCheckbox
void DrawRowIntCheckbox(const char *nameStr, std::string codeStr, const char *descStr, int &var_ref, bool &on, const char *label)
Definition: GUI_AngelScriptExamples.cpp:134
RoR::AngelScriptExamples::node
int node
Definition: GUI_AngelScriptExamples.h:46
RoR::AngelScriptExamples::DrawRowSlider
void DrawRowSlider(const char *nameStr, std::string codeStr, const char *descStr, float min, float max, float &var_ref)
Definition: GUI_AngelScriptExamples.cpp:57
RoR
Definition: AppContext.h:36