Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added AngelScript shortcuts in console #2780

Merged
merged 6 commits into from
Dec 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ set(SOURCE_FILES
gui/mygui/Dialog.{h,cpp}
gui/mygui/GuiPanelBase.h
gui/mygui/WrapsAttribute.h
gui/panels/GUI_AngelScriptExamples.{h,cpp}
gui/panels/GUI_ConsoleView.{h,cpp}
gui/panels/GUI_ConsoleWindow.{h,cpp}
gui/panels/GUI_DirectionArrow.{h,cpp}
Expand Down
159 changes: 159 additions & 0 deletions source/main/gui/panels/GUI_AngelScriptExamples.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
This source file is part of Rigs of Rods
Copyright 2021 tritonas00
For more information, see http://www.rigsofrods.org/
Rigs of Rods is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3, as
published by the Free Software Foundation.
Rigs of Rods is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
*/


#include "GUI_AngelScriptExamples.h"
#include "ScriptEngine.h"

using namespace RoR;

void AngelScriptExamples::Draw()
{
this->DrawRowSlider("void scaleTruck()", "game.getCurrentTruck().scaleTruck({})", _LC("AngelScript", "Scales the truck"), 1.f, 1.5f, scale);
this->DrawRowText("string getTruckName()", "game.log(game.getCurrentTruck().getTruckName())", _LC("AngelScript", "Gets the name of the truck"));
this->DrawRowText("string getTruckFileName()", "game.log(game.getCurrentTruck().getTruckFileName())", _LC("AngelScript", "Gets the name of the truck file"));
this->DrawRowText("string getSectionConfig()", "game.log(game.getCurrentTruck().getSectionConfig())", _LC("AngelScript", "Gets the name of the loaded section for a truck"));
this->DrawRowText("int getTruckType()", "game.log(' ' + game.getCurrentTruck().getTruckType())", _LC("AngelScript", "Gets the type of the truck"));
this->DrawRowCheckbox("void reset()", "game.getCurrentTruck().reset({})", _LC("AngelScript", "Resets the truck"), reset, "Keep position");
this->DrawRowText("void parkingbrakeToggle()", "game.getCurrentTruck().parkingbrakeToggle()", _LC("AngelScript", "Toggles the parking brake"));
this->DrawRowText("void tractioncontrolToggle()", "game.getCurrentTruck().tractioncontrolToggle()", _LC("AngelScript", "Toggles the tracktion control"));
this->DrawRowText("void antilockbrakeToggle()", "game.getCurrentTruck().antilockbrakeToggle()", _LC("AngelScript", "Toggles the anti-lock brakes"));
this->DrawRowText("void beaconsToggle()", "game.getCurrentTruck().beaconsToggle()", _LC("AngelScript", "Toggles the beacons"));
this->DrawRowText("void toggleCustomParticles()", "game.getCurrentTruck().toggleCustomParticles()", _LC("AngelScript", "Toggles the custom particles"));
this->DrawRowText("int getNodeCount()", "game.log(' ' + game.getCurrentTruck().getNodeCount())", _LC("AngelScript", "Gets the total amount of nodes of the truck"));
this->DrawRowCheckbox("float getTotalMass()", "game.log(' ' + game.getCurrentTruck().getTotalMass({}))", _LC("AngelScript", "Gets the total mass of the truck"), locked, "With locked");
this->DrawRowText("int getWheelNodeCount()", "game.log(' ' + game.getCurrentTruck().getWheelNodeCount())", _LC("AngelScript", "Gets the total amount of nodes of the wheels of the truck"));
this->DrawRowSlider("void setMass()", "game.getCurrentTruck().setMass({})", _LC("AngelScript", "Sets the mass of the truck"), 1000.f, 10000.f, mass);
this->DrawRowText("bool getBrakeLightVisible()", "game.log(' ' + game.getCurrentTruck().getBrakeLightVisible())", _LC("AngelScript", "Returns true if the brake light is enabled"));
this->DrawRowInt("bool getCustomLightVisible()", "game.log(' ' + game.getCurrentTruck().getCustomLightVisible({}))", _LC("AngelScript", "Returns true if the custom light with the number is enabled"), light);
this->DrawRowIntCheckbox("void setCustomLightVisible()", "game.getCurrentTruck().setCustomLightVisible({}, {})", _LC("AngelScript", "Enables or disables the custom light"), custom_light, visible, "On");
this->DrawRowText("bool getBeaconMode()", "game.log(' ' + game.getCurrentTruck().getBeaconMode())", _LC("AngelScript", "Gets the mode of the beacon"));
this->DrawRowInt("void setBlinkType()", "game.getCurrentTruck().setBlinkType({})", _LC("AngelScript", "Sets the blinking type"), blink);
this->DrawRowText("int getBlinkType()", "game.log(' ' + game.getCurrentTruck().getBlinkType())", _LC("AngelScript", "Gets the blinking type"));
this->DrawRowText("bool getCustomParticleMode()", "game.log(' ' + game.getCurrentTruck().getCustomParticleMode())", _LC("AngelScript", "Gets the custom particles mode"));
this->DrawRowText("bool getReverseLightVisible()", "game.log(' ' + game.getCurrentTruck().getReverseLightVisible())", _LC("AngelScript", "Returns true if the reverse lights are enabled"));
this->DrawRowText("float getHeadingDirectionAngle()", "game.log(' ' + game.getCurrentTruck().getHeadingDirectionAngle())", _LC("AngelScript", "Returns the angle in which the truck is heading"));
this->DrawRowText("bool isLocked()", "game.log(' ' + game.getCurrentTruck().isLocked())", _LC("AngelScript", "Returns true if a hook of this truck is locked"));
this->DrawRowText("float getWheelSpeed()", "game.log(' ' + game.getCurrentTruck().getWheelSpeed())", _LC("AngelScript", "Gets the current wheel speed of the vehicle"));
this->DrawRowText("float getSpeed()", "game.log(' ' + game.getCurrentTruck().getSpeed())", _LC("AngelScript", "Gets the current speed of the vehicle"));
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"));
this->DrawRowText("float getRotation()", "game.log(' ' + game.getCurrentTruck().getRotation())", _LC("AngelScript", "Gets the current rotation of the vehicle"));
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"));
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);
}

void AngelScriptExamples::DrawRowSlider(const char* nameStr, std::string codeStr, const char* descStr, float min, float max, float &var_ref)
{
ImGui::PushID(nameStr);
ImGui::AlignFirstTextHeightToWidgets();
if (ImGui::Selectable(nameStr)) { this->ExecuteString(fmt::format(codeStr, var_ref)); }
ImGui::NextColumn();
ImGui::AlignFirstTextHeightToWidgets();
ImGui::PushItemWidth(-1);
ImGui::SliderFloat(_LC("Console", ""), &var_ref, min, max);
ImGui::PopItemWidth();
ImGui::NextColumn();
ImGui::AlignFirstTextHeightToWidgets();
ImGui::Text(descStr);
ImGui::PopID();
ImGui::NextColumn();
}

void AngelScriptExamples::DrawRowText(const char* nameStr, std::string codeStr, const char* descStr)
{
ImGui::AlignFirstTextHeightToWidgets();
if (ImGui::Selectable(nameStr)) { this->ExecuteString(codeStr); }
ImGui::NextColumn();
ImGui::AlignFirstTextHeightToWidgets();
ImGui::Text("");
ImGui::NextColumn();
ImGui::AlignFirstTextHeightToWidgets();
ImGui::Text(descStr);
ImGui::NextColumn();
}

void AngelScriptExamples::DrawRowCheckbox(const char* nameStr, std::string codeStr, const char* descStr, bool &var_ref, const char* label)
{
ImGui::AlignFirstTextHeightToWidgets();
if (ImGui::Selectable(nameStr)) { this->ExecuteString(fmt::format(codeStr, var_ref)); }
ImGui::NextColumn();
ImGui::AlignFirstTextHeightToWidgets();
ImGui::Checkbox(label, &var_ref);
ImGui::NextColumn();
ImGui::AlignFirstTextHeightToWidgets();
ImGui::Text(descStr);
ImGui::NextColumn();
}

void AngelScriptExamples::DrawRowInt(const char* nameStr, std::string codeStr, const char* descStr, int &var_ref)
{
ImGui::PushID(nameStr);
ImGui::AlignFirstTextHeightToWidgets();
if (ImGui::Selectable(nameStr)) { this->ExecuteString(fmt::format(codeStr, var_ref)); }
ImGui::NextColumn();
ImGui::AlignFirstTextHeightToWidgets();
ImGui::PushItemWidth(-1);
ImGui::InputInt("", &var_ref, 1, 1);
ImGui::PopItemWidth();
ImGui::NextColumn();
ImGui::AlignFirstTextHeightToWidgets();
ImGui::Text(descStr);
ImGui::PopID();
ImGui::NextColumn();
}

void AngelScriptExamples::DrawRowIntNode(const char* nameStr, std::string codeStr, const char* descStr, int &var_ref, int &node_x, int &node_y, int &node_z)
{
ImGui::PushID(nameStr);
ImGui::AlignFirstTextHeightToWidgets();
if (ImGui::Selectable(nameStr)) { this->ExecuteString(fmt::format(codeStr, node_x, node_y, node_z)); }
ImGui::NextColumn();
ImGui::AlignFirstTextHeightToWidgets();
ImGui::PushItemWidth(-1);
ImGui::InputInt("", &var_ref, 1, 1);
ImGui::PopItemWidth();
ImGui::NextColumn();
ImGui::AlignFirstTextHeightToWidgets();
ImGui::Text(descStr);
ImGui::PopID();
ImGui::NextColumn();
}

void AngelScriptExamples::DrawRowIntCheckbox(const char* nameStr, std::string codeStr, const char* descStr, int &var_ref, bool &on, const char* label)
{
ImGui::PushID(nameStr);
ImGui::AlignFirstTextHeightToWidgets();
if (ImGui::Selectable(nameStr)) { this->ExecuteString(fmt::format(codeStr, var_ref, on)); }
ImGui::NextColumn();
ImGui::AlignFirstTextHeightToWidgets();
ImGui::PushItemWidth(96);
ImGui::InputInt("", &var_ref, 1, 1);
ImGui::PopItemWidth();
ImGui::SameLine();
ImGui::Checkbox(label, &on);
ImGui::NextColumn();
ImGui::AlignFirstTextHeightToWidgets();
ImGui::Text(descStr);
ImGui::PopID();
ImGui::NextColumn();
}

void AngelScriptExamples::ExecuteString(std::string const& code)
{
// Use console command 'as' because it echoes the entered code,
// which allows the user to see it and learn.
// Also reports error when not usable, i.e. in main menu.
App::GetConsole()->doCommand(fmt::format("as {}", code));
}
51 changes: 51 additions & 0 deletions source/main/gui/panels/GUI_AngelScriptExamples.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
This source file is part of Rigs of Rods
Copyright 2021 tritonas00
For more information, see http://www.rigsofrods.org/
Rigs of Rods is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3, as
published by the Free Software Foundation.
Rigs of Rods is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
*/

/// @file

#pragma once

#include "Application.h"
#include "GUI_ConsoleView.h"


namespace RoR {

class AngelScriptExamples
{
public:
void Draw();
private:
void DrawRowSlider(const char* nameStr, std::string codeStr, const char* descStr, float min, float max, float &var_ref);
void DrawRowText(const char* nameStr, std::string codeStr, const char* descStr);
void DrawRowCheckbox(const char* nameStr, std::string codeStr, const char* descStr, bool &var_ref, const char* label);
void DrawRowInt(const char* nameStr, std::string codeStr, const char* descStr, int &var_ref);
void DrawRowIntCheckbox(const char* nameStr, std::string codeStr, const char* descStr, int &var_ref, bool &on, const char* label);
void DrawRowIntNode(const char* nameStr, std::string codeStr, const char* descStr, int &var_ref, int &node_x, int &node_y, int &node_z);

void ExecuteString(std::string const& code); //!< Runs code using 'as' console command.

float scale = 1.f;
float mass = 1000.f;
bool reset = false;
bool locked = false;
int light = 1;
int blink = 1;
int node = 1;
bool visible = false;
int custom_light = 1;
};

} // namespace RoR
13 changes: 10 additions & 3 deletions source/main/gui/panels/GUI_ConsoleView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,28 @@ void ConsoleView::DrawConsoleMessages()
ImGui::SetScrollY(ImGui::GetScrollMaxY() + (num_incoming * line_h) + 100.f);
}

// Calculate visible area height
float view_height = ImGui::GetWindowHeight();
if (ImGui::GetScrollMaxX() > 0) // Account for horizontal scrollbar, if visible.
{
view_height -= ImGui::GetStyle().ScrollbarSize;
}

// Calculate message range and cursor pos
int msg_start = 0, msg_count = 0;
ImVec2 cursor = ImGui::GetWindowPos();
if (ImGui::GetScrollMaxY() == 0) // No scrolling
{
msg_count = (int)m_display_messages.size();
cursor += ImVec2(0, ImGui::GetWindowHeight() - (msg_count * line_h)); // Align to bottom
cursor += ImVec2(0, view_height - (msg_count * line_h)); // Align to bottom
}
else // Scrolling
{
const float scroll_rel = ImGui::GetScrollY()/ImGui::GetScrollMaxY();
const float scroll_offset = ((dummy_h - ImGui::GetWindowHeight()) *scroll_rel);
const float scroll_offset = ((dummy_h - view_height) * scroll_rel);
msg_start = std::max(0, (int)(scroll_offset/line_h));

msg_count = std::min((int)(ImGui::GetWindowHeight() / line_h)+2, // Bias (2) for partially visible messages (1 top, 1 bottom)
msg_count = std::min((int)(view_height / line_h)+2, // Bias (2) for partially visible messages (1 top, 1 bottom)
(int)m_display_messages.size() - msg_start);

const float line_offset = scroll_offset/line_h;
Expand Down
20 changes: 19 additions & 1 deletion source/main/gui/panels/GUI_ConsoleWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "GUI_ConsoleWindow.h"
#include "GUIManager.h"
#include "GUI_AngelScriptExamples.h"

#include "Language.h"

Expand Down Expand Up @@ -51,10 +52,11 @@ void ConsoleWindow::Draw()
}
if (ImGui::BeginMenu(_LC("Console", "Commands")))
{
ImGui::Dummy(ImVec2(550.f, 1.f)); // Manually resize width (DearIMGUI bug workaround)
ImGui::Dummy(ImVec2(700.f, 1.f)); // Manually resize width (DearIMGUI bug workaround)
ImGui::Columns(3);
ImGui::SetColumnWidth(0, 100); // TODO: Calculate dynamically
ImGui::SetColumnWidth(1, 170); // TODO: Calculate dynamically
ImGui::SetColumnWidth(2, 500); // TODO: Calculate dynamically

for (auto& cmd_pair: App::GetConsole()->getCommands())
{
Expand All @@ -72,6 +74,22 @@ void ConsoleWindow::Draw()
ImGui::Columns(1); // reset
ImGui::EndMenu();
}
#ifdef USE_ANGELSCRIPT
ImGui::SetNextWindowSize(ImVec2((ImGui::GetIO().DisplaySize.x / 2), (ImGui::GetIO().DisplaySize.y / 1.5)));
if (ImGui::BeginMenu(_LC("Console", "AngelScript")))
{
ImGui::Dummy(ImVec2(720.f, 1.f)); // Manually resize width (DearIMGUI bug workaround)
ImGui::Columns(3);
ImGui::SetColumnWidth(0, 230);
ImGui::SetColumnWidth(1, 160);
ImGui::SetColumnWidth(2, 400);

m_angelscript_examples.Draw();

ImGui::Columns(1); // reset
ImGui::EndMenu();
}
#endif
ImGui::EndMenuBar();
}

Expand Down
3 changes: 3 additions & 0 deletions source/main/gui/panels/GUI_ConsoleWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "Application.h"
#include "GUI_ConsoleView.h"
#include "OgreImGui.h"
#include "GUI_AngelScriptExamples.h"

#include <vector>
#include <string>
Expand All @@ -49,6 +50,8 @@ class ConsoleWindow

private:

AngelScriptExamples m_angelscript_examples;

static int TextEditCallback(ImGuiTextEditCallbackData *data);
void TextEditCallbackProc(ImGuiTextEditCallbackData *data);

Expand Down
12 changes: 9 additions & 3 deletions source/main/system/ConsoleCmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,19 +320,25 @@ class AsCmd: public ConsoleCmd
// we want to notify any running scripts that we might change something (prevent cheating)
App::GetScriptEngine()->triggerEvent(SE_ANGELSCRIPT_MANIPULATIONS);

Str<1000> code; // Re-compose the code snippet
// Re-compose the code snippet
Str<1000> code;
for (int i = 1; i < args.size(); ++i)
{
code << " " << args[i];
}
App::GetScriptEngine()->executeString(code.ToCStr());

// Echo the code back to console user.
reply_type = Console::CONSOLE_SYSTEM_REPLY;
reply << " >>> " << code.ToCStr();
App::GetConsole()->putMessage(Console::CONSOLE_MSGTYPE_INFO, reply_type, reply.ToCStr());

// Run the code - will output script messages/AngelScript errors.
App::GetScriptEngine()->executeString(code.ToCStr());
#else
reply_type = Console::CONSOLE_SYSTEM_ERROR;
reply << _L("Scripting disabled in this build");
#endif
App::GetConsole()->putMessage(Console::CONSOLE_MSGTYPE_INFO, reply_type, reply.ToCStr());
#endif
}
};

Expand Down