Skip to content

Commit

Permalink
Implement custom ImGui settings support
Browse files Browse the repository at this point in the history
  • Loading branch information
Xottab-DUTY committed Jan 15, 2025
1 parent 74ce1f8 commit 3e950d2
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/xrEngine/Environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion src/xrEngine/editor_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down
70 changes: 70 additions & 0 deletions src/xrEngine/editor_base_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "editor_helper.h"
#include "XR_IOConsole.h"

#include <imgui_internal.h>

namespace
{
bool mouse_can_use_global_state()
Expand Down Expand Up @@ -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<ide*>(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<ide*>(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<ide_tool*>(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<ide*>(handler->UserData);
for (ide_tool* tool : self.m_tools)
{
tool->apply_settings();
}
};

ini_handler.WriteAllFn = [](ImGuiContext*, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buffer)
{
ide& self = *static_cast<ide*>(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)
Expand Down
2 changes: 1 addition & 1 deletion src/xrGame/player_hud_tune.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/xrUICore/ui_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"; }
};

0 comments on commit 3e950d2

Please sign in to comment.