Skip to content

Commit

Permalink
👼 script: added ImGui tab bar API + demo_script.as example.
Browse files Browse the repository at this point in the history
  • Loading branch information
ohlidalp committed Jul 22, 2023
1 parent 05ca921 commit dd752ca
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 2 deletions.
91 changes: 90 additions & 1 deletion resources/scripts/demo_script.as
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ SoundClass@ g_playing_sound = null;
bool g_sound_follows_player = true;
string g_demofile_data;

// tab settings
bool demotabsReorderable = false;
bool demotabsNoTooltip = true;
// closable tabs
bool demotabOpentabChecks = true;
bool demotabOpentabEmpty = true;

/*
---------------------------------------------------------------------------
Script setup function - invoked once when script is loaded.
Expand Down Expand Up @@ -85,7 +92,8 @@ void frameStep(float dt)
drawAudioButtons();

ImGui::Separator();
drawTextResourceButtons();
drawTextResourceButtons();
drawExampleTabs();
}
else if (g_app_state.getInt() == 2) // simulation
{
Expand Down Expand Up @@ -708,3 +716,84 @@ void drawActorAngles(BeamClass@ actor)
+ " (degrees: " + formatFloat(actor.getOrientation().getRoll().valueDegrees(), "", 6,2) + ")");
}
}



void drawExampleTabs()
{
ImGui::TextDisabled("* * E X A M P L E T A B B A R * *");
// TABS, TAB BARS:
//bool BeginTabBar(const string&in, int TabBarFlags = 0)
//void EndTabBar()
//bool BeginTabItem(const string&in, int = 0) // BeginTabItem() without X close button.
//bool BeginTabItem(const string&in, bool&out , int TabItemFlags = 0) // BeginTabItem() with X close button.
//void EndTabItem()
//bool TabItemButton(const string&in, int TabItemFlags = 0)
//void SetTabItemClosed(const string&in)

// IMGUI TAB BAR FLAGS:
//ImGuiTabBarFlags_Reorderable); // // Allow manually dragging tabs to re-order them + New tabs are appended at the end of list
//ImGuiTabBarFlags_AutoSelectNewTabs); // // Automatically select new tabs when they appear
//ImGuiTabBarFlags_TabListPopupButton); // // Disable buttons to open the tab list popup
//ImGuiTabBarFlags_NoCloseWithMiddleMouseButton); // // Disable behavior of closing tabs (that are submitted with p_open != NULL) with middle mouse button. You can still repro this behavior on user's side with if (IsItemHovered() && IsMouseClicked(2)) *p_open = false.
//ImGuiTabBarFlags_NoTabListScrollingButtons); // // Disable scrolling buttons (apply when fitting policy is ImGuiTabBarFlags_FittingPolicyScroll)
//ImGuiTabBarFlags_NoTooltip); // // Disable tooltips when hovering a tab
//ImGuiTabBarFlags_FittingPolicyResizeDown); // // Resize tabs when they don't fit
//ImGuiTabBarFlags_FittingPolicyScroll); // // Add scroll buttons when tabs don't fit
// // ============================================================================

int barflags =
ImGuiTabBarFlags_NoCloseWithMiddleMouseButton
| ImGuiTabBarFlags_NoTabListScrollingButtons
| ImGuiTabBarFlags_FittingPolicyResizeDown;
if (demotabsReorderable)
barflags = barflags | ImGuiTabBarFlags_Reorderable;
if (demotabsNoTooltip)
barflags = barflags |= ImGuiTabBarFlags_NoTooltip;

if (ImGui::BeginTabBar("demotabs", barflags))
{

if (ImGui::BeginTabItem("Text"))
{
// just text
ImGui::TextDisabled(">1");
ImGui::TextDisabled(">>2");
ImGui::TextDisabled(">>>3");
ImGui::TextDisabled(">>>>4\n>>>>>5\n>>>>>>6");

ImGui::EndTabItem();
}


if (ImGui::BeginTabItem("Reset"))
{
// boring text
if (ImGui::Button("Reset closed tabs"))
{
demotabOpentabChecks=true;
demotabOpentabEmpty=true;
}

ImGui::EndTabItem();
}
if ( ImGui::BeginTabItem("Checks", /*inout:*/demotabOpentabChecks))
{
// chekcboxes
ImGui::Checkbox("reorderable", /*inout:*/demotabsReorderable);
ImGui::Checkbox("no tooltip", /*inout:*/demotabsNoTooltip);
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Empty", /*inout:*/demotabOpentabEmpty))
{
// empty
ImGui::EndTabItem();
}
ImGui::EndTabBar();
}

ImGui::Separator();
ImGui::Text("nothing interesting down here...");
}


36 changes: 35 additions & 1 deletion source/main/scripting/bindings/ImGuiAngelscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,27 @@ void RoR::RegisterImGuiBindings(AngelScript::asIScriptEngine* engine)
engine->RegisterEnumValue("ImGuiWindowFlags", "ImGuiWindowFlags_NoDecoration", ImGuiWindowFlags_NoDecoration);
engine->RegisterEnumValue("ImGuiWindowFlags", "ImGuiWindowFlags_NoInputs", ImGuiWindowFlags_NoInputs);

engine->RegisterEnum("ImGuiTabBarFlags");
engine->RegisterEnumValue("ImGuiTabBarFlags", "ImGuiTabBarFlags_None", ImGuiTabBarFlags_None);
engine->RegisterEnumValue("ImGuiTabBarFlags", "ImGuiTabBarFlags_Reorderable", ImGuiTabBarFlags_Reorderable); // // Allow manually dragging tabs to re-order them + New tabs are appended at the end of list
engine->RegisterEnumValue("ImGuiTabBarFlags", "ImGuiTabBarFlags_AutoSelectNewTabs", ImGuiTabBarFlags_AutoSelectNewTabs); // // Automatically select new tabs when they appear
engine->RegisterEnumValue("ImGuiTabBarFlags", "ImGuiTabBarFlags_TabListPopupButton", ImGuiTabBarFlags_TabListPopupButton); // // Disable buttons to open the tab list popup
engine->RegisterEnumValue("ImGuiTabBarFlags", "ImGuiTabBarFlags_NoCloseWithMiddleMouseButton", ImGuiTabBarFlags_NoCloseWithMiddleMouseButton); // // Disable behavior of closing tabs (that are submitted with p_open != NULL) with middle mouse button. You can still repro this behavior on user's side with if (IsItemHovered() && IsMouseClicked(2)) *p_open = false.
engine->RegisterEnumValue("ImGuiTabBarFlags", "ImGuiTabBarFlags_NoTabListScrollingButtons", ImGuiTabBarFlags_NoTabListScrollingButtons); // // Disable scrolling buttons (apply when fitting policy is ImGuiTabBarFlags_FittingPolicyScroll)
engine->RegisterEnumValue("ImGuiTabBarFlags", "ImGuiTabBarFlags_NoTooltip", ImGuiTabBarFlags_NoTooltip); // // Disable tooltips when hovering a tab
engine->RegisterEnumValue("ImGuiTabBarFlags", "ImGuiTabBarFlags_FittingPolicyResizeDown", ImGuiTabBarFlags_FittingPolicyResizeDown); // // Resize tabs when they don't fit
engine->RegisterEnumValue("ImGuiTabBarFlags", "ImGuiTabBarFlags_FittingPolicyScroll", ImGuiTabBarFlags_FittingPolicyScroll); // // Add scroll buttons when tabs don't fit
engine->RegisterEnumValue("ImGuiTabBarFlags", "ImGuiTabBarFlags_FittingPolicyMask_", ImGuiTabBarFlags_FittingPolicyMask_); // ImGuiTabBarFlags_FittingPolicyResizeDown | ImGuiTabBarFlags_FittingPolicyScroll,
engine->RegisterEnumValue("ImGuiTabBarFlags", "ImGuiTabBarFlags_FittingPolicyDefault_", ImGuiTabBarFlags_FittingPolicyDefault_); // ImGuiTabBarFlags_FittingPolicyResizeDown

engine->RegisterEnum("ImGuiTabItemFlags");
engine->RegisterEnumValue("ImGuiTabItemFlags", "ImGuiTabItemFlags_None", ImGuiTabItemFlags_None);
engine->RegisterEnumValue("ImGuiTabItemFlags", "ImGuiTabItemFlags_UnsavedDocument", ImGuiTabItemFlags_UnsavedDocument); // // Append '*' to title without affecting the ID, as a convenience to avoid using the ### operator. Also: tab is selected on closure and closure is deferred by one frame to allow code to undo it without flicker.
engine->RegisterEnumValue("ImGuiTabItemFlags", "ImGuiTabItemFlags_SetSelected", ImGuiTabItemFlags_SetSelected); // // Trigger flag to programmatically make the tab selected when calling BeginTabItem()
engine->RegisterEnumValue("ImGuiTabItemFlags", "ImGuiTabItemFlags_NoCloseWithMiddleMouseButton", ImGuiTabItemFlags_NoCloseWithMiddleMouseButton); // // Disable behavior of closing tabs (that are submitted with p_open != NULL) with middle mouse button. You can still repro this behavior on user's side with if (IsItemHovered() && IsMouseClicked(2)) *p_open = false.
engine->RegisterEnumValue("ImGuiTabItemFlags", "ImGuiTabItemFlags_NoPushId", ImGuiTabItemFlags_NoPushId); // // Don't call PushID(tab->ID)/PopID() on BeginTabItem()/EndTabItem()


// ImDrawList object (global namespace)
engine->RegisterObjectType("ImDrawList", sizeof(ImDrawList), asOBJ_REF | asOBJ_NOCOUNT);
engine->RegisterObjectMethod("ImDrawList", "void AddLine(const vector2&in p1, const vector2&in p2, const color&in col, float thickness = 1.f)", asFUNCTIONPR([](ImDrawList* drawlist, Ogre::Vector2 const& p1, Ogre::Vector2 const& p2, Ogre::ColourValue const& col, float thickness) { drawlist->AddLine(ImVec2(p1.x, p1.y), ImVec2(p2.x, p2.y), ImColor(col.r, col.g, col.b, col.a), thickness); }, (ImDrawList * , Ogre::Vector2 const& , Ogre::Vector2 const& , Ogre::ColourValue const& , float ), void), asCALL_CDECL_OBJFIRST);
Expand Down Expand Up @@ -185,7 +206,7 @@ void RoR::RegisterImGuiBindings(AngelScript::asIScriptEngine* engine)
// engine->RegisterGlobalFunction("float GetFrameHeight()", asFUNCTIONPR(ImGui::GetFrameHeight, (), float), asCALL_CDECL);
// engine->RegisterGlobalFunction("float GetFrameHeightWithSpacing()", asFUNCTIONPR(ImGui::GetFrameHeightWithSpacing, (), float), asCALL_CDECL);

// Columns
// Columns (considered legacy in latest versions - superseded by Tables!)
engine->RegisterGlobalFunction("void Columns(int = 1, const string&in = string(), bool = true)", asFUNCTIONPR([](int a, const string& b, bool c) {
ImGui::Columns(a, b.c_str(), c); }, (int, const string&, bool), void), asCALL_CDECL);
engine->RegisterGlobalFunction("void NextColumn()", asFUNCTIONPR([]() { ImGui::NextColumn(); }, (), void), asCALL_CDECL);
Expand All @@ -196,6 +217,19 @@ void RoR::RegisterImGuiBindings(AngelScript::asIScriptEngine* engine)
engine->RegisterGlobalFunction("void SetColumnOffset(int, float)", asFUNCTIONPR([](int a, float b) { ImGui::SetColumnOffset(a, b); }, (int,float), void), asCALL_CDECL);
engine->RegisterGlobalFunction("int GetColumnsCount()", asFUNCTIONPR([]() { return ImGui::GetColumnsCount(); }, (), int), asCALL_CDECL);


// Tab bars, tabs
engine->RegisterGlobalFunction("bool BeginTabBar(const string&in, int = 0)", asFUNCTIONPR([](const string& str_id, ImGuiTabBarFlags flags) { return ImGui::BeginTabBar(str_id.c_str(), flags); }, (const string&, ImGuiTabBarFlags), bool), asCALL_CDECL);
engine->RegisterGlobalFunction("void EndTabBar()", asFUNCTION(ImGui::EndTabBar), asCALL_CDECL);
// BeginTabItem() without X close button.
engine->RegisterGlobalFunction("bool BeginTabItem(const string&in, int = 0)", asFUNCTIONPR([](const string& label, ImGuiTabItemFlags flags) { return ImGui::BeginTabItem(label.c_str(), nullptr, flags); }, (const string&, ImGuiTabItemFlags), bool), asCALL_CDECL);
// BeginTabItem() with X close button.
engine->RegisterGlobalFunction("bool BeginTabItem(const string&in, bool&inout, int = 0)", asFUNCTIONPR([](const string& label, bool& p_open, ImGuiTabItemFlags flags) { return ImGui::BeginTabItem(label.c_str(), &p_open, flags); }, (const string&, bool&, ImGuiTabItemFlags), bool), asCALL_CDECL);
engine->RegisterGlobalFunction("void EndTabItem()", asFUNCTION(ImGui::EndTabItem), asCALL_CDECL);
//engine->RegisterGlobalFunction("bool TabItemButton(const string&in, ImGuiTabItemFlags = 0)", asFUNCTIONPR([](const string& label, ImGuiTabItemFlags flags) { return ImGui::TabItemButton(label.c_str(), flags); }, (const string&, ImGuiTabItemFlags), bool), asCALL_CDECL);
engine->RegisterGlobalFunction("void SetTabItemClosed(const string&in)", asFUNCTIONPR([](const string& tab_or_docked_window_label) { ImGui::SetTabItemClosed(tab_or_docked_window_label.c_str()); }, (const string&), void), asCALL_CDECL);


// ID scopes
engine->RegisterGlobalFunction("void PushID(const string&in)", asFUNCTIONPR([](const string& n) { ImGui::PushID(n.c_str()); }, (const string&), void), asCALL_CDECL);
engine->RegisterGlobalFunction("void PushID(int int_id)", asFUNCTIONPR([](int id) { ImGui::PushID(id); }, (int), void), asCALL_CDECL);
Expand Down

0 comments on commit dd752ca

Please sign in to comment.