UnrealImGui/Source/ImGui/Private/Editor/ImGuiEditor.cpp
Sebastian 09572a8ef9 Refactorization of ImGui settings:
- Divided ImGui settings into UImGuiSettings persistent object and FImGuiModuleSettings proxy that provides interface for other classes and handles delayed loading of UImGuiSettings.
- Removed from FImGuiModuleProperties and FImGuiModuleCommands direct dependencies on UImGuiSettings.
- Simplified FImGuiModuleProperties making it more robust when moving data after hot-reloading.
- Inverted binding logic by injecting FImGuiModuleProperties and FImGuiModuleCommands into FImGuiModuleSettings and letting it take care of synchronization. Dependencies are setup by module manager.
- Added to module manager FImGuiModuleSettings and interface to access it.
- Cleaned interface of FImGuiInputHandlerFactory and removed direct dependency on settings.
2018-12-08 21:03:18 +00:00

134 lines
2.9 KiB
C++

// Distributed under the MIT License (MIT) (see accompanying LICENSE file)
#include "ImGuiPrivatePCH.h"
#if WITH_EDITOR
#include "ImGuiEditor.h"
#include "ImGuiKeyInfoCustomization.h"
#include "ImGuiSettings.h"
#include <ISettingsModule.h>
#define LOCTEXT_NAMESPACE "ImGuiEditor"
#define SETTINGS_CONTAINER TEXT("Project"), TEXT("Plugins"), TEXT("ImGui")
namespace
{
ISettingsModule* GetSettingsModule()
{
return FModuleManager::GetModulePtr<ISettingsModule>("Settings");
}
FPropertyEditorModule* GetPropertyEditorModule()
{
return FModuleManager::GetModulePtr<FPropertyEditorModule>("PropertyEditor");
}
}
FImGuiEditor::FImGuiEditor()
{
Register();
// As a side effect of being part of the ImGui module, we need to support deferred registration (only executed if
// module is loaded manually at the very early stage).
if (!IsRegistrationCompleted())
{
CreateRegistrator();
}
}
FImGuiEditor::~FImGuiEditor()
{
Unregister();
}
void FImGuiEditor::Register()
{
// Only register after UImGuiSettings class is initialized (necessary to check in early loading stages).
if (!bSettingsRegistered && UImGuiSettings::Get())
{
if (ISettingsModule* SettingsModule = GetSettingsModule())
{
bSettingsRegistered = true;
SettingsModule->RegisterSettings(SETTINGS_CONTAINER,
LOCTEXT("ImGuiSettingsName", "ImGui"),
LOCTEXT("ImGuiSettingsDescription", "Configure the Unreal ImGui plugin."),
UImGuiSettings::Get());
}
}
if (!bCustomPropertyTypeLayoutsRegistered)
{
if (FPropertyEditorModule* PropertyModule = GetPropertyEditorModule())
{
bCustomPropertyTypeLayoutsRegistered = true;
PropertyModule->RegisterCustomPropertyTypeLayout("ImGuiKeyInfo",
FOnGetPropertyTypeCustomizationInstance::CreateStatic(&FImGuiKeyInfoCustomization::MakeInstance));
}
}
}
void FImGuiEditor::Unregister()
{
if (bSettingsRegistered)
{
bSettingsRegistered = false;
if (ISettingsModule* SettingsModule = GetSettingsModule())
{
SettingsModule->UnregisterSettings(SETTINGS_CONTAINER);
}
}
if (bCustomPropertyTypeLayoutsRegistered)
{
bCustomPropertyTypeLayoutsRegistered = false;
if (FPropertyEditorModule* PropertyModule = GetPropertyEditorModule())
{
PropertyModule->UnregisterCustomPropertyTypeLayout("ImGuiKeyInfo");
}
}
}
void FImGuiEditor::CreateRegistrator()
{
if (!RegistratorHandle.IsValid())
{
RegistratorHandle = FModuleManager::Get().OnModulesChanged().AddLambda([this](FName Name, EModuleChangeReason Reason)
{
if (Reason == EModuleChangeReason::ModuleLoaded)
{
Register();
}
if (IsRegistrationCompleted())
{
ReleaseRegistrator();
}
});
}
}
void FImGuiEditor::ReleaseRegistrator()
{
if (RegistratorHandle.IsValid())
{
FModuleManager::Get().OnModulesChanged().Remove(RegistratorHandle);
RegistratorHandle.Reset();
}
}
#undef SETTINGS_CONTAINER
#undef LOCTEXT_NAMESPACE
#endif // WITH_EDITOR