Skip to content

Commit

Permalink
👼Script: Fixed open param of ImGui::Begin()
Browse files Browse the repository at this point in the history
This snowballed into a big grooming of scripts:
- all individual scripts, including all examples, had to be fixed.
- a helper class `CloseWindowPrompt` was added to 'imgui_utils.as'
- Bindings of 'ImGuiCol_' enum were added to the game, copypasted constants were removed from all scripts.
- a copypasted helper `getDummyFullscreenWindow()` was also added to 'imgui_utils.as' - related because it uses ImGuiCol_ constant.
- the Tutorial script in script_editor.as was updated to use `CloseWindowPrompt` from 'imgui_utils.as' - the separate example script was removed.
  • Loading branch information
ohlidalp committed Sep 29, 2024
1 parent 176d391 commit 41060b8
Show file tree
Hide file tree
Showing 30 changed files with 690 additions and 394 deletions.
6 changes: 5 additions & 1 deletion resources/scripts/demo_script.as
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
---------------------------------------------------------------------------
*/

#include "imgui_utils.as"

/*
---------------------------------------------------------------------------
Global variables
Expand All @@ -49,6 +51,7 @@ SoundScriptInstanceClass@ g_playing_soundscript = null;
SoundClass@ g_playing_sound = null;
bool g_sound_follows_player = true;
string g_demofile_data;
imgui_utils::CloseWindowPrompt g_window_closebtn_handler;

// tab settings
bool demotabsReorderable = false;
Expand Down Expand Up @@ -78,7 +81,8 @@ void main()
void frameStep(float dt)
{
// Open demo window
ImGui::Begin("Demo Script", /*open:*/true, ImGuiWindowFlags_AlwaysAutoResize);
ImGui::Begin("Demo Script", g_window_closebtn_handler.windowOpen, ImGuiWindowFlags_AlwaysAutoResize);
g_window_closebtn_handler.draw();

// show some stats
ImGui::Text("Total frames: " + g_total_frames);
Expand Down
57 changes: 32 additions & 25 deletions resources/scripts/example_GenericDocument_editor.as
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// (sorry about the indentation mess, I scribbled this in 'script_editor.as')
// ===================================================

// Window [X] button handler
#include "imgui_utils.as"
imgui_utils::CloseWindowPrompt closeBtnHandler;

class RigEditor
{
// ---- variables -----
Expand All @@ -19,32 +23,35 @@ void drawWindow()
{

string caption = "RigEditor";
if (@m_project_entry != null)
{
caption += " ("+m_project_entry.fname+")";
}
int flags = ImGuiWindowFlags_MenuBar;
ImGui::Begin(caption, /*open:*/true, flags);
if (ImGui::BeginMenuBar())
{
this.drawDocumentControls(); // <- menubar
ImGui::EndMenuBar();
}
if (@g_displayed_document != null)
{
vector2 size = ImGui::GetWindowSize() - vector2(25, 200);
ImGui::BeginChild("docBody", size);
this.drawDocumentBody();
ImGui::EndChild();

if ( m_focused_token_pos > -1)
{
ImGui::Separator();
this.drawTokenEditPanel();
}
}
if (@m_project_entry != null)
{
caption += " ("+m_project_entry.fname+")";
}
int flags = ImGuiWindowFlags_MenuBar;
if (ImGui::Begin(caption, closeBtnHandler.windowOpen, flags))
{
closeBtnHandler.draw();
if (ImGui::BeginMenuBar())
{
this.drawDocumentControls(); // <- menubar
ImGui::EndMenuBar();
}
if (@g_displayed_document != null)
{
vector2 size = ImGui::GetWindowSize() - vector2(25, 200);
ImGui::BeginChild("docBody", size);
this.drawDocumentBody();
ImGui::EndChild();

ImGui::End();
if ( m_focused_token_pos > -1)
{
ImGui::Separator();
this.drawTokenEditPanel();
}
}

ImGui::End();
}

}

Expand Down
11 changes: 10 additions & 1 deletion resources/scripts/example_ImGui_ImHyperlink.as
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@

#include "imgui_utils.as" // ImHyperlink()

// Window [X] button handler
imgui_utils::CloseWindowPrompt closeBtnHandler;

void frameStep(float dt)
{
ImHyperlink("http://developer.rigsofrods.org", "Dev portal");
if (ImGui::Begin("Example", closeBtnHandler.windowOpen, 0))
{
closeBtnHandler.draw();
imgui_utils::ImHyperlink("http://developer.rigsofrods.org", "Dev portal");
ImGui::End();
}

}
74 changes: 34 additions & 40 deletions resources/scripts/example_ImGui_nodeHighlight.as
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
/// * HELPERS - funcs
/// ===================================================

// Window [X] button handler
#include "imgui_utils.as"
imgui_utils::CloseWindowPrompt closeBtnHandler;

//#region GAME CALLBACKS:

// `main()` runs once when script is loaded.
Expand All @@ -20,33 +24,40 @@ void main()
// `frameStep()` runs every frame; `dt` is delta time in seconds.
void frameStep(float dt)
{
ImGui::TextDisabled("::: NODE HIGHLIGHT DEMO:::");
ImGui::Separator();
BeamClass@ actor = game.getCurrentTruck();
if (@actor == null)
if (ImGui::Begin("Example", closeBtnHandler.windowOpen, 0))
{
ImGui::Text("you are on foot");
}
else
{
ImGui::Text("Driving '"+actor.getTruckName()+"' with total "+actor.getNodeCount()+" nodes ("+actor.getWheelNodeCount()+" wheel nodes)");

drawNodeHighlights(actor);

// Left mouse button click = flip the selection state of the
if (mouseHoveredNodeID != -1 && ImGui::IsMouseClicked(0))
closeBtnHandler.draw();

ImGui::TextDisabled("::: NODE HIGHLIGHT DEMO:::");
ImGui::Separator();
BeamClass@ actor = game.getCurrentTruck();
if (@actor == null)
{
nodeSelectedStates[mouseHoveredNodeID] = !nodeSelectedStates[mouseHoveredNodeID];
ImGui::Text("you are on foot");
}


drawNodeSelectionUI();

if (ImGui::CollapsingHeader("Config"))
else
{
drawConfigUI();
ImGui::Text("Driving '"+actor.getTruckName()+"' with total "+actor.getNodeCount()+" nodes ("+actor.getWheelNodeCount()+" wheel nodes)");

drawNodeHighlights(actor);

// Left mouse button click = flip the selection state of the
if (mouseHoveredNodeID != -1 && ImGui::IsMouseClicked(0))
{
nodeSelectedStates[mouseHoveredNodeID] = !nodeSelectedStates[mouseHoveredNodeID];
}


drawNodeSelectionUI();

if (ImGui::CollapsingHeader("Config"))
{
drawConfigUI();
}
}
}

ImGui::End();
}
}

// Handles events registered using `game.registerForEvent()`
Expand Down Expand Up @@ -168,7 +179,7 @@ void drawNodeHighlights(BeamClass@ actor)

int mouseClosestNodeDist = 999999;

ImDrawList@ drawlist = getDummyFullscreenWindow("nodeHighlights");
ImDrawList@ drawlist = imgui_utils::ImGetDummyFullscreenWindow("nodeHighlights");
for (int i = 0; i < actor.getNodeCount(); i++)
{
vector3 posWorld = actor.getNodePosition(i);
Expand Down Expand Up @@ -229,23 +240,6 @@ void drawNodeHighlights(BeamClass@ actor)

// #region HELPERS

// shamelessly copypasted from 'road_editor.as', line 787
ImDrawList@ getDummyFullscreenWindow(const string&in name)
{
// Dummy fullscreen window to draw to
int window_flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar| ImGuiWindowFlags_NoInputs
| ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoBringToFrontOnFocus;
ImGui::SetNextWindowPos(vector2(0,0));
ImGui::SetNextWindowSize(game.getDisplaySize());
ImGui::PushStyleColor(/*ImGuiCol_WindowBg*/2, color(0.f,0.f,0.f,0.f)); // Fully transparent background!
ImGui::Begin(name, /*open:*/true, window_flags);
ImDrawList@ drawlist = ImGui::GetWindowDrawList();
ImGui::End();
ImGui::PopStyleColor(1); // WindowBg

return drawlist;
}

// shamelessly copypasted from 'road_editor.as', line 803
int getMouseShortestDistance(vector2 mouse, vector2 target)
{
Expand Down
17 changes: 16 additions & 1 deletion resources/scripts/example_ImGui_tabs.as
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@

void frameStep(float dt) { drawExampleTabs(); }
// Window [X] button handler
#include "imgui_utils.as"
imgui_utils::CloseWindowPrompt closeBtnHandler;

void frameStep(float dt)
{
if (ImGui::Begin("Example", closeBtnHandler.windowOpen, 0))
{
closeBtnHandler.draw();

drawExampleTabs();

ImGui::End();
}

}

// settings
bool demotabsReorderable = false;
Expand Down
6 changes: 4 additions & 2 deletions resources/scripts/example_RigEditor_alpha.as
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/// Written and auto-indented using script_editor.as!
// ===================================================

#include "gridviewer_utils.as"
#include "imgui_utils.as"

class RigEditor
{
Expand All @@ -27,6 +27,7 @@ class RigEditor
CacheEntryClass@ m_awaiting_load_bundle_entry = null;
color node_color = color(0.8, 0.9, 0.2, 1.0);
float node_radius = 1.f;
imgui_utils::CloseWindowPrompt closeBtnHandler; // Window [X] button handler

// ---- functions ----

Expand All @@ -43,7 +44,8 @@ class RigEditor
caption += " ("+m_project_entry.fname+")";
}
int flags = ImGuiWindowFlags_MenuBar;
ImGui::Begin(caption, /*open:*/true, flags);
ImGui::Begin(caption, closeBtnHandler.windowOpen, flags);
closeBtnHandler.draw();

// Draw menu bar

Expand Down
54 changes: 32 additions & 22 deletions resources/scripts/example_angelscript_exceptionEvent.as
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
// based on Overlays code because those throw a lot.
// ===================================================

// Window [X] button handler
#include "imgui_utils.as"
imgui_utils::CloseWindowPrompt closeBtnHandler;

dictionary exception_stats; // fullDesc -> num occurences

//SETUP
// SETUP
void main() { game.registerForEvent(SE_GENERIC_EXCEPTION_CAUGHT); }

// EVENT HANDLING
Expand All @@ -22,27 +26,33 @@ void eventCallbackEx(scriptEvents ev,

void frameStep(float dt)
{
Ogre::Overlay@ ov;
Ogre::Overlay@ ov;
Ogre::OverlayElement@ pa;

// CAUTION: attempt to create the same overlay again will throw an exception,
@ ov = Ogre::OverlayManager::getSingleton().create("ov");

// CAUTION: getOverlayElement() will throw exception if not found,
@pa = Ogre::OverlayManager::getSingleton().getOverlayElement("pa");

// CAUTION: using the same name twice will throw an exception,
@ pa = Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "pa");

// ---- the exceptions ----
array<string>@tag_keys = exception_stats.getKeys();
ImGui::Text("total exception types:"+tag_keys.length());
for (uint i=0; i < tag_keys.length(); i++)
{
int tagcount = int(exception_stats[tag_keys[i]]);
ImGui::Bullet();
ImGui::SameLine(); ImGui::TextDisabled('['+tagcount+']');
ImGui::SameLine(); ImGui::Text(tag_keys[i]+": "+tagcount);
}
// ------ END exceptions ----
// CAUTION: attempt to create the same overlay again will throw an exception,
@ ov = Ogre::OverlayManager::getSingleton().create("ov");

// CAUTION: getOverlayElement() will throw exception if not found,
@pa = Ogre::OverlayManager::getSingleton().getOverlayElement("pa");

// CAUTION: using the same name twice will throw an exception,
@ pa = Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "pa");

// ---- the exceptions ----
if (ImGui::Begin("Exception events", closeBtnHandler.windowOpen, 0))
{
closeBtnHandler.draw();

array<string>@tag_keys = exception_stats.getKeys();
ImGui::Text("total exception types:"+tag_keys.length());
for (uint i=0; i < tag_keys.length(); i++)
{
int tagcount = int(exception_stats[tag_keys[i]]);
ImGui::Bullet();
ImGui::SameLine(); ImGui::TextDisabled('['+tagcount+']');
ImGui::SameLine(); ImGui::Text(tag_keys[i]+": "+tagcount);
}
ImGui::End();
}
// ------ END exceptions ----
}
41 changes: 27 additions & 14 deletions resources/scripts/example_angelscript_getScriptDetails.as
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,35 @@
// * getRunningScripts()
// ===================================

// Window [X] button handler
#include "imgui_utils.as"
imgui_utils::CloseWindowPrompt closeBtnHandler;

// `frameStep()` runs every frame; `dt` is delta time in seconds.
void frameStep(float dt)
{
array<int> nids = game.getRunningScripts();
ImGui::TextDisabled("Running scripts ("+nids.length()+', our NID:'+thisScript+'):');
for (uint i=0; i<nids.length(); i++)
{
ImGui::Bullet(); ImGui::SameLine(); ImGui::TextDisabled('['+i+'] ');
dictionary@ info = game.getScriptDetails(nids[i]);
ImGui::SameLine();
if (@info == null) { ImGui::Text('null'); }
else {
ImGui::Text(''+int( info['uniqueId'] ) +' >> '
+ string(info['scriptName']) + ' (' + ScriptCategory(info['scriptCategory']) + ')');
}
if (nids[i] == thisScript) { ImGui::SameLine(); ImGui::Text('<------- THIS SCRIPT!'); }
}
if (ImGui::Begin("Example - getScriptDetails", closeBtnHandler.windowOpen, 0))
{
closeBtnHandler.draw();

array<int> nids = game.getRunningScripts();
ImGui::TextDisabled("Running scripts ("+nids.length()+', our NID:'+thisScript+'):');
for (uint i=0; i<nids.length(); i++)
{
ImGui::Bullet(); ImGui::SameLine(); ImGui::TextDisabled('['+i+'] ');
dictionary@ info = game.getScriptDetails(nids[i]);
ImGui::SameLine();
if (@info == null)
{
ImGui::Text('null');
}
else
{
ImGui::Text(''+int( info['uniqueId'] ) +' >> '
+ string(info['scriptName']) + ' (' + ScriptCategory(info['scriptCategory']) + ')');
}
if (nids[i] == thisScript) { ImGui::SameLine(); ImGui::Text('<------- THIS SCRIPT!'); }
}
ImGui::End();
}
}
Loading

0 comments on commit 41060b8

Please sign in to comment.