From e38c236cf44eae33a0c9c4238f6dd9b6dac1486a Mon Sep 17 00:00:00 2001 From: praydog Date: Tue, 2 Apr 2024 23:09:21 -0700 Subject: [PATCH] Lua: Add imgui.begin/end_disabled --- src/mods/ScriptRunner.cpp | 1 + src/mods/bindings/ImGui.cpp | 30 ++++++++++++++++++++++++++++++ src/mods/bindings/ImGui.hpp | 4 ++++ 3 files changed, 35 insertions(+) diff --git a/src/mods/ScriptRunner.cpp b/src/mods/ScriptRunner.cpp index 0af109e5b..87afdf638 100644 --- a/src/mods/ScriptRunner.cpp +++ b/src/mods/ScriptRunner.cpp @@ -439,6 +439,7 @@ void ScriptState::on_frame() { ScriptRunner::get()->spew_error("Unknown error in on_frame"); } + api::imgui::cleanup(); api::imnodes::cleanup(); } diff --git a/src/mods/bindings/ImGui.cpp b/src/mods/bindings/ImGui.cpp index 69ae61693..a740806c2 100644 --- a/src/mods/bindings/ImGui.cpp +++ b/src/mods/bindings/ImGui.cpp @@ -10,6 +10,16 @@ #include "ImGui.hpp" namespace api::imgui { +int32_t g_disabled_counts{0}; + +void cleanup() { + for (auto i = 0; i < g_disabled_counts; ++i) { + ImGui::EndDisabled(); + } + + g_disabled_counts = 0; +} + ImVec2 create_imvec2(sol::object obj) { ImVec2 out{ 0.0f, 0.0f }; @@ -495,6 +505,24 @@ void end_rect(sol::object additional_size_obj, sol::object rounding_obj) { ImGui::GetWindowDrawList()->AddRect(mins, maxs, ImGui::GetColorU32(ImGuiCol_Border), ImGui::GetStyle().FrameRounding, ImDrawCornerFlags_All, 1.0f); } +void begin_disabled(sol::object disabled_obj) { + bool disabled{true}; + + if (disabled_obj.is()) { + disabled = disabled_obj.as(); + } + + ++g_disabled_counts; + ImGui::BeginDisabled(disabled); +} + +void end_disabled() { + if (g_disabled_counts > 0) { + --g_disabled_counts; + ImGui::EndDisabled(); + } +} + void separator() { ImGui::Separator(); } @@ -1962,6 +1990,8 @@ void bindings::open_imgui(ScriptState* s) { imgui["end_group"] = api::imgui::end_group; imgui["begin_rect"] = api::imgui::begin_rect; imgui["end_rect"] = api::imgui::end_rect; + imgui["begin_disabled"] = api::imgui::begin_disabled; + imgui["end_disabled"] = api::imgui::end_disabled; imgui["separator"] = api::imgui::separator; imgui["spacing"] = api::imgui::spacing; imgui["new_line"] = api::imgui::new_line; diff --git a/src/mods/bindings/ImGui.hpp b/src/mods/bindings/ImGui.hpp index f1b4f8976..b2d899730 100644 --- a/src/mods/bindings/ImGui.hpp +++ b/src/mods/bindings/ImGui.hpp @@ -6,6 +6,10 @@ namespace bindings { void open_imgui(ScriptState* s); } +namespace api::imgui { +void cleanup(); +} + namespace api::imnodes { // pop the active nodes/editors/attributes etc so we dont crash. void cleanup();