From 3e950d2b71058b60c229117252f17ec68f38d14a Mon Sep 17 00:00:00 2001 From: Xottab-DUTY Date: Wed, 15 Jan 2025 20:54:44 +0300 Subject: [PATCH] Implement custom ImGui settings support --- src/xrEngine/Environment.h | 2 +- src/xrEngine/editor_base.h | 8 +++- src/xrEngine/editor_base_input.cpp | 70 ++++++++++++++++++++++++++++++ src/xrGame/player_hud_tune.h | 2 +- src/xrUICore/ui_debug.h | 2 +- 5 files changed, 80 insertions(+), 4 deletions(-) diff --git a/src/xrEngine/Environment.h b/src/xrEngine/Environment.h index b62e6c7ecb4..a375f8c4580 100644 --- a/src/xrEngine/Environment.h +++ b/src/xrEngine/Environment.h @@ -370,7 +370,7 @@ class ENGINE_API CEnvironment : public xray::editor::ide_tool void save_weather_effects(CInifile* environment_config = nullptr) const; private: - pcstr tool_name() override { return "Weather Editor"; } + pcstr tool_name() const override { return "Weather Editor"; } }; ENGINE_API extern Flags32 psEnvFlags; diff --git a/src/xrEngine/editor_base.h b/src/xrEngine/editor_base.h index dbb664d4ad0..0b31f1eb541 100644 --- a/src/xrEngine/editor_base.h +++ b/src/xrEngine/editor_base.h @@ -19,13 +19,19 @@ class XR_NOVTABLE ENGINE_API ide_tool virtual void on_tool_frame() = 0; - virtual pcstr tool_name() = 0; + virtual pcstr tool_name() const = 0; bool& get_open_state() { return is_opened; } bool is_open() const { return is_opened; } virtual bool is_active() const { return is_opened; } ImGuiWindowFlags get_default_window_flags() const; + + virtual void reset_settings() {} + virtual void apply_setting(pcstr /*line*/) {} + virtual void apply_settings() {} + virtual void save_settings(ImGuiTextBuffer* /*buffer*/) const {} + virtual size_t estimate_settings_size() const { return 0; } }; class ENGINE_API ide final : diff --git a/src/xrEngine/editor_base_input.cpp b/src/xrEngine/editor_base_input.cpp index 87a41b6e278..ab65563748f 100644 --- a/src/xrEngine/editor_base_input.cpp +++ b/src/xrEngine/editor_base_input.cpp @@ -4,6 +4,8 @@ #include "editor_helper.h" #include "XR_IOConsole.h" +#include + namespace { bool mouse_can_use_global_state() @@ -44,6 +46,74 @@ void ide::InitBackend() m_imgui_backend.mouse_can_report_hovered_viewport = true; #endif } + + ImGuiSettingsHandler ini_handler; + ini_handler.TypeName = "OpenXRay"; + ini_handler.TypeHash = ImHashStr("OpenXRay"); + ini_handler.UserData = this; + + ini_handler.ClearAllFn = [](ImGuiContext*, ImGuiSettingsHandler* handler) + { + ide& self = *static_cast(handler->UserData); + for (ide_tool* tool : self.m_tools) + { + tool->reset_settings(); + } + }; + + ini_handler.ReadOpenFn = [](ImGuiContext*, ImGuiSettingsHandler* handler, pcstr name) -> void* + { + ide& self = *static_cast(handler->UserData); + for (ide_tool* tool : self.m_tools) + { + if (xr_strcmp(tool->tool_name(), name) == 0) + { + tool->reset_settings(); // Clear existing if recycling previous entry + return tool; + } + } + return nullptr; + }; + + ini_handler.ReadLineFn = [](ImGuiContext*, ImGuiSettingsHandler*, void* entry, pcstr line) + { + if (!entry) + return; + ide_tool& self = *static_cast(entry); + self.apply_setting(line); + + }; + + // We don't store separate copy of settings and + // intended workflow is to apply settings immediately in apply_setting, + // so this isn't much useful, but who knows + ini_handler.ApplyAllFn = [](ImGuiContext*, ImGuiSettingsHandler* handler) + { + ide& self = *static_cast(handler->UserData); + for (ide_tool* tool : self.m_tools) + { + tool->apply_settings(); + } + }; + + ini_handler.WriteAllFn = [](ImGuiContext*, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buffer) + { + ide& self = *static_cast(handler->UserData); + + size_t estimated_buffer_size = 0; + for (const ide_tool* tool : self.m_tools) + { + estimated_buffer_size += tool->estimate_settings_size(); + } + buffer->reserve(estimated_buffer_size); + + for (const ide_tool* tool : self.m_tools) + { + buffer->appendf("[%s][%s]\n", handler->TypeName, tool->tool_name()); + tool->save_settings(buffer); + } + }; + ImGui::AddSettingsHandler(&ini_handler); } void ide::ProcessEvent(const SDL_Event& event) diff --git a/src/xrGame/player_hud_tune.h b/src/xrGame/player_hud_tune.h index e66e50f82ac..70d7c2a83da 100644 --- a/src/xrGame/player_hud_tune.h +++ b/src/xrGame/player_hud_tune.h @@ -10,7 +10,7 @@ class CHudTuner final : public xray::editor::ide_tool bool is_active() const override; private: - pcstr tool_name() override { return "Hud Tuner"; } + pcstr tool_name() const override { return "Hud Tuner"; } void ResetToDefaultValues(); void UpdateValues(); diff --git a/src/xrUICore/ui_debug.h b/src/xrUICore/ui_debug.h index fcc076a27a9..5e0ed84050e 100644 --- a/src/xrUICore/ui_debug.h +++ b/src/xrUICore/ui_debug.h @@ -55,5 +55,5 @@ class XRUICORE_API CUIDebugger final : public xray::editor::ide_tool bool ShouldDrawRects() const { return m_state.drawWndRects; } private: - pcstr tool_name() override { return "UI Debugger"; } + pcstr tool_name() const override { return "UI Debugger"; } };