Replaced module properties singleton with instance created as part of module manager:

- Removed ImGui Module Properties singleton interface and released construction constraints.
- Added ImGui Module Properties to ImGui Module Manager.
- Moved ImGui Demo to ImGui Module Manager.
- ImGui Demo and ImGui Module Commands keep reference to ImGui Module Manager that gives them access to properties.
This commit is contained in:
Sebastian 2018-11-25 20:36:55 +00:00
parent 70db3c7b20
commit 209a594616
14 changed files with 74 additions and 54 deletions

View File

@ -121,7 +121,7 @@ FImGuiContextManager::FContextData& FImGuiContextManager::GetEditorContextData()
if (UNLIKELY(!Data)) if (UNLIKELY(!Data))
{ {
Data = &Contexts.Emplace(Utilities::EDITOR_CONTEXT_INDEX, FContextData{ GetEditorContextName(), Utilities::EDITOR_CONTEXT_INDEX, DrawMultiContextEvent, FontAtlas, ImGuiDemo, -1 }); Data = &Contexts.Emplace(Utilities::EDITOR_CONTEXT_INDEX, FContextData{ GetEditorContextName(), Utilities::EDITOR_CONTEXT_INDEX, DrawMultiContextEvent, FontAtlas, -1 });
} }
return *Data; return *Data;
@ -135,7 +135,7 @@ FImGuiContextManager::FContextData& FImGuiContextManager::GetStandaloneWorldCont
if (UNLIKELY(!Data)) if (UNLIKELY(!Data))
{ {
Data = &Contexts.Emplace(Utilities::STANDALONE_GAME_CONTEXT_INDEX, FContextData{ GetWorldContextName(), Utilities::STANDALONE_GAME_CONTEXT_INDEX, DrawMultiContextEvent, FontAtlas, ImGuiDemo }); Data = &Contexts.Emplace(Utilities::STANDALONE_GAME_CONTEXT_INDEX, FContextData{ GetWorldContextName(), Utilities::STANDALONE_GAME_CONTEXT_INDEX, DrawMultiContextEvent, FontAtlas });
} }
return *Data; return *Data;
@ -175,7 +175,7 @@ FImGuiContextManager::FContextData& FImGuiContextManager::GetWorldContextData(co
#if WITH_EDITOR #if WITH_EDITOR
if (UNLIKELY(!Data)) if (UNLIKELY(!Data))
{ {
Data = &Contexts.Emplace(Index, FContextData{ GetWorldContextName(World), Index, DrawMultiContextEvent, FontAtlas, ImGuiDemo, WorldContext->PIEInstance }); Data = &Contexts.Emplace(Index, FContextData{ GetWorldContextName(World), Index, DrawMultiContextEvent, FontAtlas, WorldContext->PIEInstance });
} }
else else
{ {
@ -185,7 +185,7 @@ FImGuiContextManager::FContextData& FImGuiContextManager::GetWorldContextData(co
#else #else
if (UNLIKELY(!Data)) if (UNLIKELY(!Data))
{ {
Data = &Contexts.Emplace(Index, FContextData{ GetWorldContextName(World), Index, DrawMultiContextEvent, FontAtlas, ImGuiDemo }); Data = &Contexts.Emplace(Index, FContextData{ GetWorldContextName(World), Index, DrawMultiContextEvent, FontAtlas });
} }
#endif #endif

View File

@ -3,7 +3,6 @@
#pragma once #pragma once
#include "ImGuiContextProxy.h" #include "ImGuiContextProxy.h"
#include "ImGuiDemo.h"
// Manages ImGui context proxies. // Manages ImGui context proxies.
@ -60,11 +59,10 @@ private:
struct FContextData struct FContextData
{ {
FContextData(const FString& ContextName, int32 ContextIndex, FSimpleMulticastDelegate& SharedDrawEvent, ImFontAtlas& FontAtlas, FImGuiDemo& Demo, int32 InPIEInstance = -1) FContextData(const FString& ContextName, int32 ContextIndex, FSimpleMulticastDelegate& SharedDrawEvent, ImFontAtlas& FontAtlas, int32 InPIEInstance = -1)
: PIEInstance(InPIEInstance) : PIEInstance(InPIEInstance)
, ContextProxy(ContextName, &SharedDrawEvent, &FontAtlas) , ContextProxy(ContextName, &SharedDrawEvent, &FontAtlas)
{ {
ContextProxy.OnDraw().AddLambda([&Demo, ContextIndex]() { Demo.DrawControls(ContextIndex); });
} }
FORCEINLINE bool CanTick() const { return PIEInstance < 0 || GEngine->GetWorldContextFromPIEInstance(PIEInstance); } FORCEINLINE bool CanTick() const { return PIEInstance < 0 || GEngine->GetWorldContextFromPIEInstance(PIEInstance); }
@ -77,10 +75,9 @@ private:
struct FContextData struct FContextData
{ {
FContextData(const FString& ContextName, int32 ContextIndex, FSimpleMulticastDelegate& SharedDrawEvent, ImFontAtlas& FontAtlas, FImGuiDemo& Demo) FContextData(const FString& ContextName, int32 ContextIndex, FSimpleMulticastDelegate& SharedDrawEvent, ImFontAtlas& FontAtlas)
: ContextProxy(ContextName, &SharedDrawEvent, &FontAtlas) : ContextProxy(ContextName, &SharedDrawEvent, &FontAtlas)
{ {
ContextProxy.OnDraw().AddLambda([&Demo, ContextIndex]() { Demo.DrawControls(ContextIndex); });
} }
FORCEINLINE bool CanTick() const { return true; } FORCEINLINE bool CanTick() const { return true; }
@ -108,8 +105,6 @@ private:
TMap<int32, FContextData> Contexts; TMap<int32, FContextData> Contexts;
FImGuiDemo ImGuiDemo;
FSimpleMulticastDelegate DrawMultiContextEvent; FSimpleMulticastDelegate DrawMultiContextEvent;
ImFontAtlas FontAtlas; ImFontAtlas FontAtlas;

View File

@ -4,11 +4,13 @@
#include "ImGuiDemo.h" #include "ImGuiDemo.h"
#include "ImGuiModuleManager.h"
// Demo copied (with minor modifications) 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(int32 ContextIndex) void FImGuiDemo::DrawControls(int32 ContextIndex)
{ {
if (FImGuiModuleProperties::Get().ShowDemo()) if (ModuleManager.GetProperties().ShowDemo())
{ {
const int32 ContextBit = ContextIndex < 0 ? 0 : 1 << ContextIndex; const int32 ContextBit = ContextIndex < 0 ? 0 : 1 << ContextIndex;

View File

@ -4,16 +4,24 @@
#include <imgui.h> #include <imgui.h>
class FImGuiModuleManager;
// Widget drawing ImGui demo. // Widget drawing ImGui demo.
class FImGuiDemo class FImGuiDemo
{ {
public: public:
FImGuiDemo(FImGuiModuleManager& InModuleManager)
: ModuleManager(InModuleManager)
{
}
void DrawControls(int32 ContextIndex); void DrawControls(int32 ContextIndex);
private: private:
FImGuiModuleManager& ModuleManager;
ImVec4 ClearColor = ImColor{ 114, 144, 154 }; ImVec4 ClearColor = ImColor{ 114, 144, 154 };
int32 ShowDemoWindowMask = 0; int32 ShowDemoWindowMask = 0;

View File

@ -26,7 +26,7 @@ FImGuiInputResponse UImGuiInputHandler::OnKeyDown(const FKeyEvent& KeyEvent)
// If this is an input mode switch event then handle it here and consume. // If this is an input mode switch event then handle it here and consume.
if (IsToggleInputEvent(KeyEvent)) if (IsToggleInputEvent(KeyEvent))
{ {
FImGuiModuleProperties::Get().ToggleInput(); ModuleManager->GetProperties().ToggleInput();
return FImGuiInputResponse().RequestConsume(); return FImGuiInputResponse().RequestConsume();
} }

View File

@ -170,32 +170,44 @@ ImGuiContext** FImGuiModule::GetImGuiContextHandle()
bool FImGuiModule::IsInputMode() const bool FImGuiModule::IsInputMode() const
{ {
return FImGuiModuleProperties::Get().IsInputEnabled(); return ImGuiModuleManager && ImGuiModuleManager->GetProperties().IsInputEnabled();
} }
void FImGuiModule::SetInputMode(bool bEnabled) void FImGuiModule::SetInputMode(bool bEnabled)
{ {
return FImGuiModuleProperties::Get().SetInputEnabled(bEnabled); if (ImGuiModuleManager)
{
ImGuiModuleManager->GetProperties().SetInputEnabled(bEnabled);
}
} }
void FImGuiModule::ToggleInputMode() void FImGuiModule::ToggleInputMode()
{ {
FImGuiModuleProperties::Get().ToggleInput(); if (ImGuiModuleManager)
{
ImGuiModuleManager->GetProperties().ToggleInput();
}
} }
bool FImGuiModule::IsShowingDemo() const bool FImGuiModule::IsShowingDemo() const
{ {
return FImGuiModuleProperties::Get().ShowDemo(); return ImGuiModuleManager && ImGuiModuleManager->GetProperties().ShowDemo();
} }
void FImGuiModule::SetShowDemo(bool bShow) void FImGuiModule::SetShowDemo(bool bShow)
{ {
return FImGuiModuleProperties::Get().SetShowDemo(bShow); if (ImGuiModuleManager)
{
ImGuiModuleManager->GetProperties().SetShowDemo(bShow);
}
} }
void FImGuiModule::ToggleShowDemo() void FImGuiModule::ToggleShowDemo()
{ {
return FImGuiModuleProperties::Get().ToggleDemo(); if (ImGuiModuleManager)
{
ImGuiModuleManager->GetProperties().ToggleDemo();
}
} }

View File

@ -4,6 +4,7 @@
#include "ImGuiModuleCommands.h" #include "ImGuiModuleCommands.h"
#include "ImGuiModuleManager.h"
#include "ImGuiSettings.h" #include "ImGuiSettings.h"
#include "Utilities/DebugExecBindings.h" #include "Utilities/DebugExecBindings.h"
@ -19,8 +20,9 @@ namespace CommandNames
} }
} }
FImGuiModuleCommands::FImGuiModuleCommands() FImGuiModuleCommands::FImGuiModuleCommands(FImGuiModuleManager& InModuleManager)
: ToggleInputCommand(CommandNames::ToggleInput, : ModuleManager(InModuleManager)
, ToggleInputCommand(CommandNames::ToggleInput,
TEXT("Toggle ImGui input mode."), TEXT("Toggle ImGui input mode."),
FConsoleCommandDelegate::CreateRaw(this, &FImGuiModuleCommands::ToggleInput)) FConsoleCommandDelegate::CreateRaw(this, &FImGuiModuleCommands::ToggleInput))
, ToggleKeyboardNavigationCommand(CommandNames::ToggleKeyboardNavigation, , ToggleKeyboardNavigationCommand(CommandNames::ToggleKeyboardNavigation,
@ -82,20 +84,20 @@ void FImGuiModuleCommands::UpdateToggleInputKeyBinding()
void FImGuiModuleCommands::ToggleInput() void FImGuiModuleCommands::ToggleInput()
{ {
FImGuiModuleProperties::Get().ToggleInput(); ModuleManager.GetProperties().ToggleInput();
} }
void FImGuiModuleCommands::ToggleKeyboardNavigation() void FImGuiModuleCommands::ToggleKeyboardNavigation()
{ {
FImGuiModuleProperties::Get().ToggleKeyboardNavigation(); ModuleManager.GetProperties().ToggleKeyboardNavigation();
} }
void FImGuiModuleCommands::ToggleGamepadNavigation() void FImGuiModuleCommands::ToggleGamepadNavigation()
{ {
FImGuiModuleProperties::Get().ToggleGamepadNavigation(); ModuleManager.GetProperties().ToggleGamepadNavigation();
} }
void FImGuiModuleCommands::ToggleDemo() void FImGuiModuleCommands::ToggleDemo()
{ {
FImGuiModuleProperties::Get().ToggleDemo(); ModuleManager.GetProperties().ToggleDemo();
} }

View File

@ -5,15 +5,18 @@
#include <IConsoleManager.h> #include <IConsoleManager.h>
// Wrapper for ImGui console commands. class FImGuiModuleManager;
// Manges ImGui module console commands.
class FImGuiModuleCommands class FImGuiModuleCommands
{ {
// Allow module manager to control life-cycle of this class. public:
friend class FImGuiModuleManager;
FImGuiModuleCommands(); FImGuiModuleCommands(FImGuiModuleManager& InModuleManager);
~FImGuiModuleCommands(); ~FImGuiModuleCommands();
private:
FImGuiModuleCommands(const FImGuiModuleCommands&) = delete; FImGuiModuleCommands(const FImGuiModuleCommands&) = delete;
FImGuiModuleCommands& operator=(const FImGuiModuleCommands&) = delete; FImGuiModuleCommands& operator=(const FImGuiModuleCommands&) = delete;
@ -32,6 +35,8 @@ class FImGuiModuleCommands
void ToggleGamepadNavigation(); void ToggleGamepadNavigation();
void ToggleDemo(); void ToggleDemo();
FImGuiModuleManager& ModuleManager;
FAutoConsoleCommand ToggleInputCommand; FAutoConsoleCommand ToggleInputCommand;
FAutoConsoleCommand ToggleKeyboardNavigationCommand; FAutoConsoleCommand ToggleKeyboardNavigationCommand;
FAutoConsoleCommand ToggleGamepadNavigationCommand; FAutoConsoleCommand ToggleGamepadNavigationCommand;

View File

@ -13,6 +13,8 @@
FImGuiModuleManager::FImGuiModuleManager() FImGuiModuleManager::FImGuiModuleManager()
: ModuleCommands(*this)
, ImGuiDemo(*this)
{ {
// Typically we will use viewport created events to add widget to new game viewports. // Typically we will use viewport created events to add widget to new game viewports.
ViewportCreatedHandle = UGameViewportClient::OnViewportCreated().AddRaw(this, &FImGuiModuleManager::OnViewportCreated); ViewportCreatedHandle = UGameViewportClient::OnViewportCreated().AddRaw(this, &FImGuiModuleManager::OnViewportCreated);
@ -166,7 +168,8 @@ void FImGuiModuleManager::AddWidgetToViewport(UGameViewportClient* GameViewport)
// Make sure that we have a context for this viewport's world and get its index. // Make sure that we have a context for this viewport's world and get its index.
int32 ContextIndex; int32 ContextIndex;
auto& Proxy = ContextManager.GetWorldContextProxy(*GameViewport->GetWorld(), ContextIndex); auto& ContextProxy = ContextManager.GetWorldContextProxy(*GameViewport->GetWorld(), ContextIndex);
ContextProxy.OnDraw().AddLambda([this, ContextIndex]() { ImGuiDemo.DrawControls(ContextIndex); });
// Make sure that textures are loaded before the first Slate widget is created. // Make sure that textures are loaded before the first Slate widget is created.
LoadTextures(); LoadTextures();

View File

@ -3,7 +3,9 @@
#pragma once #pragma once
#include "ImGuiContextManager.h" #include "ImGuiContextManager.h"
#include "ImGuiDemo.h"
#include "ImGuiModuleCommands.h" #include "ImGuiModuleCommands.h"
#include "ImGuiModuleProperties.h"
#include "SImGuiWidget.h" #include "SImGuiWidget.h"
#include "TextureManager.h" #include "TextureManager.h"
@ -16,6 +18,9 @@ class FImGuiModuleManager
public: public:
// Get interface to module state properties.
FImGuiModuleProperties& GetProperties() { return Properties; }
// Get ImGui contexts manager. // Get ImGui contexts manager.
FImGuiContextManager& GetContextManager() { return ContextManager; } FImGuiContextManager& GetContextManager() { return ContextManager; }
@ -57,8 +62,14 @@ private:
// Event that we call after ImGui is updated. // Event that we call after ImGui is updated.
FSimpleMulticastDelegate PostImGuiUpdateEvent; FSimpleMulticastDelegate PostImGuiUpdateEvent;
// Collection of module state properties.
FImGuiModuleProperties Properties;
// Tying module console commands to life-cycle of this manager and module. // Tying module console commands to life-cycle of this manager and module.
FImGuiModuleCommands Commands; FImGuiModuleCommands ModuleCommands;
// Widget that we add to all created contexts to draw ImGui demo.
FImGuiDemo ImGuiDemo;
// Manager for ImGui contexts. // Manager for ImGui contexts.
FImGuiContextManager ContextManager; FImGuiContextManager ContextManager;

View File

@ -5,8 +5,4 @@
#include "ImGuiModuleProperties.h" #include "ImGuiModuleProperties.h"
FImGuiModuleProperties& FImGuiModuleProperties::Get() // TODO: Initialize relevant properties from settings.
{
static FImGuiModuleProperties Instance;
return Instance;
}

View File

@ -6,15 +6,11 @@
// Collects and give access to module properties. // Collects and give access to module properties.
// TODO: For now singleton instance is initialized on the first use. Try to move it to the ImGui Manager.
class FImGuiModuleProperties class FImGuiModuleProperties
{ {
public: public:
// Get the instance of the ImGui properties. // Check whether input is enabled.
static FImGuiModuleProperties& Get();
// Check whether ImGui input is enabled.
bool IsInputEnabled() const { return bInputEnabled; } bool IsInputEnabled() const { return bInputEnabled; }
// Enable or disable ImGui input. // Enable or disable ImGui input.
@ -52,14 +48,6 @@ public:
private: private:
FImGuiModuleProperties() = default;
FImGuiModuleProperties(const FImGuiModuleProperties&) = delete;
FImGuiModuleProperties& operator=(const FImGuiModuleProperties&) = delete;
FImGuiModuleProperties(FImGuiModuleProperties&&) = delete;
FImGuiModuleProperties& operator=(FImGuiModuleProperties&&) = delete;
bool bInputEnabled = false; bool bInputEnabled = false;
bool bKeyboardNavigationEnabled = false; bool bKeyboardNavigationEnabled = false;

View File

@ -3,7 +3,6 @@
// Module-wide macros // Module-wide macros
#include "VersionCompatibility.h" #include "VersionCompatibility.h"
#include "ImGuiModuleDebug.h" #include "ImGuiModuleDebug.h"
#include "ImGuiModuleProperties.h"
// Module // Module
#include "ImGuiModule.h" #include "ImGuiModule.h"

View File

@ -503,7 +503,7 @@ void SImGuiWidget::SetVisibilityFromInputEnabled()
void SImGuiWidget::UpdateInputEnabled() void SImGuiWidget::UpdateInputEnabled()
{ {
const bool bEnabled = FImGuiModuleProperties::Get().IsInputEnabled(); const bool bEnabled = ModuleManager && ModuleManager->GetProperties().IsInputEnabled();
if (bInputEnabled != bEnabled) if (bInputEnabled != bEnabled)
{ {
bInputEnabled = bEnabled; bInputEnabled = bEnabled;
@ -552,9 +552,8 @@ void SImGuiWidget::UpdateInputEnabled()
if (bInputEnabled) if (bInputEnabled)
{ {
const auto& Properties = FImGuiModuleProperties::Get(); InputState.SetKeyboardNavigationEnabled(ModuleManager && ModuleManager->GetProperties().IsKeyboardNavigationEnabled());
InputState.SetKeyboardNavigationEnabled(Properties.IsKeyboardNavigationEnabled()); InputState.SetGamepadNavigationEnabled(ModuleManager && ModuleManager->GetProperties().IsGamepadNavigationEnabled());
InputState.SetGamepadNavigationEnabled(Properties.IsGamepadNavigationEnabled());
const auto& Application = FSlateApplication::Get().GetPlatformApplication(); const auto& Application = FSlateApplication::Get().GetPlatformApplication();
InputState.SetGamepad(Application.IsValid() && Application->IsGamepadAttached()); InputState.SetGamepad(Application.IsValid() && Application->IsGamepadAttached());
} }