From 79534d691ba5ec65c735214ed6fc62217c8475f0 Mon Sep 17 00:00:00 2001 From: Arnaud Jamin Date: Wed, 14 May 2025 15:20:29 -0400 Subject: [PATCH] Fix saving of cog window visible state --- .../Cog/Source/Cog/Private/CogSubsystem.cpp | 32 +++++++++++-------- Plugins/Cog/Source/Cog/Public/CogSubsystem.h | 11 ++++--- .../CogImgui/Private/CogImguiContext.cpp | 10 ++++++ .../Source/CogImgui/Public/CogImguiContext.h | 2 ++ 4 files changed, 38 insertions(+), 17 deletions(-) diff --git a/Plugins/Cog/Source/Cog/Private/CogSubsystem.cpp b/Plugins/Cog/Source/Cog/Private/CogSubsystem.cpp index 0188bdb..d38a487 100644 --- a/Plugins/Cog/Source/Cog/Private/CogSubsystem.cpp +++ b/Plugins/Cog/Source/Cog/Private/CogSubsystem.cpp @@ -13,7 +13,6 @@ #include "CogWidgets.h" #include "Engine/Engine.h" #include "GameFramework/PlayerInput.h" -#include "HAL/IConsoleManager.h" #include "imgui_internal.h" #include "Misc/CoreMisc.h" #include "NetImgui_Api.h" @@ -43,6 +42,12 @@ TStatId UCogSubsystem::GetStatId() const RETURN_QUICK_DECLARE_CYCLE_STAT(UCogSubsystemBase, STATGROUP_Tickables); } +//-------------------------------------------------------------------------------------------------------------------------- +void UCogSubsystem::PostInitialize() +{ + Super::PostInitialize(); +} + //-------------------------------------------------------------------------------------------------------------------------- void UCogSubsystem::Deinitialize() { @@ -54,7 +59,7 @@ void UCogSubsystem::Deinitialize() //-------------------------------------------------------------------------------------------------------------------------- void UCogSubsystem::TryInitialize(UWorld& World) { - if (IsInitialized) + if (bIsInitialized) { return; } FWorldContext* WorldContext = GEngine->GetWorldContextFromWorld(&World); @@ -64,6 +69,8 @@ void UCogSubsystem::TryInitialize(UWorld& World) if (WorldContext->GameViewport == nullptr && IsRunningDedicatedServer() == false) { return; } + UE_LOG(LogCogImGui, Verbose, TEXT("UCogSubsystem::TryInitialize | World:%s %p"), *World.GetName(), &World); + Context.Initialize(WorldContext->GameViewport.Get()); FCogImGuiContextScope ImGuiContextScope(Context); @@ -161,7 +168,7 @@ void UCogSubsystem::TryInitialize(UWorld& World) })); - IsInitialized = true; + bIsInitialized = true; } //-------------------------------------------------------------------------------------------------------------------------- @@ -169,11 +176,10 @@ void UCogSubsystem::Shutdown() { FCogImGuiContextScope ImGuiContextScope(Context); - //------------------------------------------------------------------ - // Destroy ImGui before destroying the windows to make sure - // imgui serialize their visibility state in imgui.ini - // It also save the Cog Settings, so they are saved regularly. - //------------------------------------------------------------------ + if (bIsInitialized) + { + Context.SaveSettings(); + } for (FCogWindow* Window : Windows) { @@ -182,7 +188,7 @@ void UCogSubsystem::Shutdown() } Windows.Empty(); - if (IsInitialized) + if (bIsInitialized) { Context.Shutdown(); } @@ -233,8 +239,8 @@ void UCogSubsystem::Tick(float InDeltaTime) if (World == nullptr) { return; } - - if (IsInitialized == false) + + if (bIsInitialized == false) { TryInitialize(*World); return; @@ -538,7 +544,7 @@ UCogSubsystem::FMenu* UCogSubsystem::AddMenu(const FString& Name) //-------------------------------------------------------------------------------------------------------------------------- void UCogSubsystem::RenderMainMenu() { - IsRenderingInMainMenu = true; + bIsRenderingInMainMenu = true; //----------------------------------------------------------------------------------------------- // Prevent having a small gap on the right of the main menu, where some widgets are displayed @@ -588,7 +594,7 @@ void UCogSubsystem::RenderMainMenu() ImGui::EndMainMenuBar(); } - IsRenderingInMainMenu = false; + bIsRenderingInMainMenu = false; } //-------------------------------------------------------------------------------------------------------------------------- diff --git a/Plugins/Cog/Source/Cog/Public/CogSubsystem.h b/Plugins/Cog/Source/Cog/Public/CogSubsystem.h index 6974ace..ebd691d 100644 --- a/Plugins/Cog/Source/Cog/Public/CogSubsystem.h +++ b/Plugins/Cog/Source/Cog/Public/CogSubsystem.h @@ -32,10 +32,14 @@ public: virtual TStatId GetStatId() const override; + virtual void PostInitialize() override; + virtual void Deinitialize() override; virtual void Tick(float DeltaTime) override; + virtual void Activate(); + virtual void AddWindow(FCogWindow* Window, const FString& Name); template @@ -82,7 +86,7 @@ public: FCogImguiContext& GetContext() { return Context; } - bool IsRenderingMainMenu() const { return IsRenderingInMainMenu; } + bool IsRenderingMainMenu() const { return bIsRenderingInMainMenu; } static void AddCommand(UPlayerInput* PlayerInput, const FString& Command, const FKey& Key); @@ -215,12 +219,11 @@ protected: bool bIsInputEnabledBeforeEnteringSelectionMode = false; bool bEnable = false; - bool bIsSelectionModeActive = false; - bool IsInitialized = false; + bool bIsInitialized = false; - bool IsRenderingInMainMenu = false; + bool bIsRenderingInMainMenu = false; int32 NumExecBindingsChecked = 0; diff --git a/Plugins/Cog/Source/CogImgui/Private/CogImguiContext.cpp b/Plugins/Cog/Source/CogImgui/Private/CogImguiContext.cpp index 2a340e7..78558d6 100644 --- a/Plugins/Cog/Source/CogImgui/Private/CogImguiContext.cpp +++ b/Plugins/Cog/Source/CogImgui/Private/CogImguiContext.cpp @@ -202,11 +202,21 @@ void FCogImguiContext::Shutdown() if (Context) { + Context->IO.IniFilename = nullptr; ImGui::DestroyContext(Context); Context = nullptr; } } +//-------------------------------------------------------------------------------------------------------------------------- +void FCogImguiContext::SaveSettings() const +{ + if (Context && Context->SettingsLoaded && Context->IO.IniFilename != nullptr) + { + ImGui::SaveIniSettingsToDisk(Context->IO.IniFilename); + } +} + //-------------------------------------------------------------------------------------------------------------------------- void FCogImguiContext::OnImGuiWidgetFocusLost() { diff --git a/Plugins/Cog/Source/CogImgui/Public/CogImguiContext.h b/Plugins/Cog/Source/CogImgui/Public/CogImguiContext.h index 8c47e6e..30b896e 100644 --- a/Plugins/Cog/Source/CogImgui/Public/CogImguiContext.h +++ b/Plugins/Cog/Source/CogImgui/Public/CogImguiContext.h @@ -45,6 +45,8 @@ public: void Shutdown(); + void SaveSettings() const; + bool GetEnableInput() const { return bEnableInput; } void SetEnableInput(bool InValue);