Changed ImGui Demo to allow selectively show demo window in chosen contexts and to fix problems with some of the examples. Added relevant warning message and description displayed when demo is opened in more than one context.

This commit is contained in:
Sebastian 2018-01-09 19:36:52 +00:00
parent 068ac2ebd0
commit f01cefc30f
3 changed files with 43 additions and 10 deletions

View File

@ -5,12 +5,18 @@
#include "ImGuiContextManager.h" #include "ImGuiContextManager.h"
#include "ImGuiImplementation.h" #include "ImGuiImplementation.h"
#include "Utilities/ScopeGuards.h"
#include "Utilities/WorldContext.h" #include "Utilities/WorldContext.h"
#include "Utilities/WorldContextIndex.h" #include "Utilities/WorldContextIndex.h"
#include <imgui.h> #include <imgui.h>
// 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 namespace
{ {
#if WITH_EDITOR #if WITH_EDITOR
@ -79,6 +85,8 @@ void FImGuiContextManager::Tick(float DeltaSeconds)
for (auto& Pair : Contexts) for (auto& Pair : Contexts)
{ {
auto ContextIndexSave = ScopeGuards::MakeStateSaver(CurrentContextIndex);
CurrentContextIndex = Pair.Key;
auto& ContextData = Pair.Value; auto& ContextData = Pair.Value;
if (ContextData.CanTick()) if (ContextData.CanTick())
{ {

View File

@ -4,6 +4,7 @@
#include "ImGuiDemo.h" #include "ImGuiDemo.h"
#include "ImGuiModuleManager.h" #include "ImGuiModuleManager.h"
#include "Utilities/ScopeGuards.h"
namespace CVars namespace CVars
@ -15,11 +16,15 @@ namespace CVars
ECVF_Default); 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() void FImGuiDemo::DrawControls()
{ {
if (CVars::ShowDemo.GetValueOnGameThread() > 0) 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 // 1. Show a simple window
// Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug" // 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::Text("Hello, world!");
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
ImGui::ColorEdit3("clear color", (float*)&ClearColor); 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); 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 // 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::SetNextWindowSize(ImVec2(200, 100), ImGuiSetCond_FirstUseEver);
ImGui::Begin("Another Window", &bDemoShowAnotherTestWindow); ImGui::Begin("Another Window");
ImGui::Text("Hello"); ImGui::Text("Hello");
ImGui::End(); ImGui::End();
} }
// 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow() // 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::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCond_FirstUseEver);
ImGui::ShowTestWindow(); ImGui::ShowDemoWindow();
} }
} }
} }

View File

@ -16,7 +16,6 @@ private:
ImVec4 ClearColor = ImColor{ 114, 144, 154 }; ImVec4 ClearColor = ImColor{ 114, 144, 154 };
bool bShowDemo = false; int32 ShowDemoWindowMask = 0;
bool bDemoShowTestWindow = true; int32 ShowAnotherWindowMask = 0;
bool bDemoShowAnotherTestWindow = false;
}; };