diff --git a/Source/ImGui/Private/ImGuiContextManager.cpp b/Source/ImGui/Private/ImGuiContextManager.cpp index 4bf74f5..570c29b 100644 --- a/Source/ImGui/Private/ImGuiContextManager.cpp +++ b/Source/ImGui/Private/ImGuiContextManager.cpp @@ -5,12 +5,18 @@ #include "ImGuiContextManager.h" #include "ImGuiImplementation.h" +#include "Utilities/ScopeGuards.h" #include "Utilities/WorldContext.h" #include "Utilities/WorldContextIndex.h" #include +// Index of the currently updated context. Only valid during context manager tick. +// TODO: Move to public interface (but probably as a current world/viewport etc.) +int32 CurrentContextIndex = Utilities::INVALID_CONTEXT_INDEX; + + namespace { #if WITH_EDITOR @@ -79,6 +85,8 @@ void FImGuiContextManager::Tick(float DeltaSeconds) for (auto& Pair : Contexts) { + auto ContextIndexSave = ScopeGuards::MakeStateSaver(CurrentContextIndex); + CurrentContextIndex = Pair.Key; auto& ContextData = Pair.Value; if (ContextData.CanTick()) { diff --git a/Source/ImGui/Private/ImGuiDemo.cpp b/Source/ImGui/Private/ImGuiDemo.cpp index 6b4694d..02a93eb 100644 --- a/Source/ImGui/Private/ImGuiDemo.cpp +++ b/Source/ImGui/Private/ImGuiDemo.cpp @@ -4,6 +4,7 @@ #include "ImGuiDemo.h" #include "ImGuiModuleManager.h" +#include "Utilities/ScopeGuards.h" namespace CVars @@ -15,11 +16,15 @@ namespace CVars ECVF_Default); } -// Demo copied from ImGui examples. See https://github.com/ocornut/imgui. +// Demo copied (with minor modifications) from ImGui examples. See https://github.com/ocornut/imgui. void FImGuiDemo::DrawControls() { if (CVars::ShowDemo.GetValueOnGameThread() > 0) { + // TODO: This should be part of a public interface. + extern int32 CurrentContextIndex; + const int32 ContextBit = CurrentContextIndex < 0 ? 0 : 1 << CurrentContextIndex; + // 1. Show a simple window // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug" { @@ -27,25 +32,46 @@ void FImGuiDemo::DrawControls() ImGui::Text("Hello, world!"); ImGui::SliderFloat("float", &f, 0.0f, 1.0f); ImGui::ColorEdit3("clear color", (float*)&ClearColor); - if (ImGui::Button("Test Window")) bDemoShowTestWindow = !bDemoShowTestWindow; - if (ImGui::Button("Another Window")) bDemoShowAnotherTestWindow = !bDemoShowAnotherTestWindow; + + if (ContextBit) + { + if (ImGui::Button("Demo Window")) ShowDemoWindowMask ^= ContextBit; + if (ImGui::Button("Another Window")) ShowAnotherWindowMask ^= ContextBit; + } ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); } // 2. Show another simple window, this time using an explicit Begin/End pair - if (bDemoShowAnotherTestWindow) + if (ShowAnotherWindowMask & ContextBit) { ImGui::SetNextWindowSize(ImVec2(200, 100), ImGuiSetCond_FirstUseEver); - ImGui::Begin("Another Window", &bDemoShowAnotherTestWindow); + ImGui::Begin("Another Window"); ImGui::Text("Hello"); ImGui::End(); } // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow() - if (bDemoShowTestWindow) + if (ShowDemoWindowMask & ContextBit) { + // Display warning about running ImGui examples in multiple contexts. + if (ShowDemoWindowMask != ContextBit) + { + ImGui::Spacing(); + + ImGui::PushStyleColor(ImGuiCol_Text, { 1.f, 1.f, 0.5f, 1.f }); + ImGui::TextWrapped("Demo Window is opend in more than one context, some of the ImGui examples may not work correctly."); + ImGui::PopStyleColor(); + + if (ImGui::IsItemHovered()) + { + ImGui::SetTooltip( + "Some of the ImGui examples that use static variables may not work correctly\n" + "when run concurrently in multiple contexts.\n" + "If you have a problem with an example try to run it in one context only."); + } + } ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCond_FirstUseEver); - ImGui::ShowTestWindow(); + ImGui::ShowDemoWindow(); } } } diff --git a/Source/ImGui/Private/ImGuiDemo.h b/Source/ImGui/Private/ImGuiDemo.h index 4aa0b90..f620990 100644 --- a/Source/ImGui/Private/ImGuiDemo.h +++ b/Source/ImGui/Private/ImGuiDemo.h @@ -16,7 +16,6 @@ private: ImVec4 ClearColor = ImColor{ 114, 144, 154 }; - bool bShowDemo = false; - bool bDemoShowTestWindow = true; - bool bDemoShowAnotherTestWindow = false; + int32 ShowDemoWindowMask = 0; + int32 ShowAnotherWindowMask = 0; };