diff --git a/Source/ImGui/Private/ImGuiContextProxy.cpp b/Source/ImGui/Private/ImGuiContextProxy.cpp index 0f52e26..ffa6cc9 100644 --- a/Source/ImGui/Private/ImGuiContextProxy.cpp +++ b/Source/ImGui/Private/ImGuiContextProxy.cpp @@ -39,13 +39,26 @@ namespace } } +FImGuiContextProxy::FImGuiContextPtr::~FImGuiContextPtr() +{ + if (Context) + { + // Setting this as a current context. is still required in the current framework version to properly shutdown + // and save data. + ImGui::SetCurrentContext(Context); + + // Save context data and destroy. + ImGui::DestroyContext(Context); + } +} + FImGuiContextProxy::FImGuiContextProxy(const FString& InName, FSimpleMulticastDelegate* InSharedDrawEvent, ImFontAtlas* InFontAtlas) : Name(InName) , SharedDrawEvent(InSharedDrawEvent) , IniFilename(TCHAR_TO_ANSI(*GetIniFile(InName))) { // Create context. - Context = TUniquePtr(ImGui::CreateContext(InFontAtlas)); + Context = FImGuiContextPtr(ImGui::CreateContext(InFontAtlas)); // Set this context in ImGui for initialization (any allocations will be tracked in this context). SetAsCurrent(); @@ -68,19 +81,6 @@ FImGuiContextProxy::FImGuiContextProxy(const FString& InName, FSimpleMulticastDe BeginFrame(); } -FImGuiContextProxy::~FImGuiContextProxy() -{ - if (Context) - { - // Setting this as a current context is still required in the current framework version to properly shutdown - // and save data. - SetAsCurrent(); - - // Save context data and destroy. - ImGui::DestroyContext(Context.Release()); - } -} - void FImGuiContextProxy::Draw() { if (bIsFrameStarted && !bIsDrawCalled) diff --git a/Source/ImGui/Private/ImGuiContextProxy.h b/Source/ImGui/Private/ImGuiContextProxy.h index dfe3e0f..b91f16b 100644 --- a/Source/ImGui/Private/ImGuiContextProxy.h +++ b/Source/ImGui/Private/ImGuiContextProxy.h @@ -17,16 +17,37 @@ class FImGuiInputState; // broadcasts draw events to allow listeners draw their controls. After update it stores draw data. class FImGuiContextProxy { + class FImGuiContextPtr + { + public: + + FImGuiContextPtr() = default; + FImGuiContextPtr(ImGuiContext* InContext) : Context(InContext) {} + + FImGuiContextPtr(const FImGuiContextPtr&) = delete; + FImGuiContextPtr& operator=(const FImGuiContextPtr&) = delete; + + FImGuiContextPtr(FImGuiContextPtr&& Other) : Context(Other.Context) { Other.Context = nullptr; } + FImGuiContextPtr& operator=(FImGuiContextPtr&& Other) { std::swap(Context, Other.Context); return *this; } + + ~FImGuiContextPtr(); + + ImGuiContext* Get() const { return Context; } + + private: + + ImGuiContext* Context = nullptr; + }; + public: FImGuiContextProxy(const FString& Name, FSimpleMulticastDelegate* InSharedDrawEvent, ImFontAtlas* InFontAtlas); - ~FImGuiContextProxy(); FImGuiContextProxy(const FImGuiContextProxy&) = delete; FImGuiContextProxy& operator=(const FImGuiContextProxy&) = delete; - FImGuiContextProxy(FImGuiContextProxy&& Other) = default; - FImGuiContextProxy& operator=(FImGuiContextProxy&& Other) = default; + FImGuiContextProxy(FImGuiContextProxy&&) = default; + FImGuiContextProxy& operator=(FImGuiContextProxy&&) = default; // Get the name of this context. const FString& GetName() const { return Name; } @@ -75,7 +96,7 @@ private: void UpdateDrawData(ImDrawData* DrawData); - TUniquePtr Context; + FImGuiContextPtr Context; FVector2D DisplaySize = FVector2D::ZeroVector; diff --git a/Source/ImGui/Private/Utilities/WorldContextIndex.h b/Source/ImGui/Private/Utilities/WorldContextIndex.h index 7ee26c7..6bf8c8e 100644 --- a/Source/ImGui/Private/Utilities/WorldContextIndex.h +++ b/Source/ImGui/Private/Utilities/WorldContextIndex.h @@ -51,7 +51,7 @@ namespace Utilities return WorldContext ? GetWorldContextIndex(*WorldContext) : INVALID_CONTEXT_INDEX; } - int32 GetWorldContextIndex(const UWorld& World) + FORCEINLINE int32 GetWorldContextIndex(const UWorld& World) { return (World.WorldType == EWorldType::Editor) ? EDITOR_CONTEXT_INDEX : GetWorldContextIndex(World.GetGameInstance()); }