Skip to content

Commit

Permalink
PersistentTreeNode/CollapsedHeader saving the "open" state in imgui.ini
Browse files Browse the repository at this point in the history
  • Loading branch information
Benualdo committed Oct 26, 2024
1 parent 43a108d commit f2a7f60
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 39 deletions.
68 changes: 57 additions & 11 deletions imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ Size=930,640
Collapsed=0

[Window][ Inspector]
Pos=2186,344
Size=374,805
Pos=2186,443
Size=374,706
Collapsed=0
DockId=0x00000022,0

Expand Down Expand Up @@ -1381,19 +1381,19 @@ Collapsed=0

[Window][ Physics]
Pos=2186,22
Size=374,322
Size=374,421
Collapsed=0
DockId=0x00000007,3

[Window][ Renderer]
Pos=2186,22
Size=374,322
Size=374,421
Collapsed=0
DockId=0x00000007,1

[Window][ Editor]
Pos=2186,22
Size=374,322
Size=374,421
Collapsed=0
DockId=0x00000007,0

Expand Down Expand Up @@ -1491,7 +1491,7 @@ Collapsed=0

[Window][ Engine]
Pos=2186,22
Size=374,322
Size=374,421
Collapsed=0
DockId=0x00000007,2

Expand Down Expand Up @@ -1781,8 +1781,8 @@ Collapsed=0
DockId=0x00000019,0

[Window][ Statistics]
Pos=2186,344
Size=374,805
Pos=2186,443
Size=374,706
Collapsed=0
DockId=0x00000022,1

Expand Down Expand Up @@ -3077,6 +3077,24 @@ Column 0 Width=96
Column 1 Width=96
Column 2 Weight=1.0000

[Table][0xEC020F3D,3]
RefScale=14
Column 0 Width=96
Column 1 Width=96
Column 2 Weight=1.0000

[Table][0xDD14C29B,3]
RefScale=14
Column 0 Width=96
Column 1 Width=96
Column 2 Weight=1.0000

[Table][0x77F68D54,3]
RefScale=14
Column 0 Width=96
Column 1 Width=96
Column 2 Weight=1.0000

[Docking][Data]
DockNode ID=0x0000001B Pos=517,135 Size=821,613 Selected=0x766639F3
DockSpace ID=0x09EF459F Pos=0,24 Size=1920,1056 Split=X
Expand Down Expand Up @@ -3105,10 +3123,10 @@ DockSpace ID=0x2C5EF6DB Window=0x83199EB2 Pos=0,22 Size=2560,1347
DockNode ID=0x0000001A Parent=0x0000001F SizeRef=916,1104 Selected=0xD5F5E73A
DockNode ID=0x00000017 Parent=0x00000005 SizeRef=1835,219 Selected=0x71A3E48E
DockNode ID=0x00000003 Parent=0x0000000F SizeRef=374,1347 Split=Y Selected=0xFABAEAF4
DockNode ID=0x00000007 Parent=0x00000003 SizeRef=460,322 Selected=0x56AA2FC3
DockNode ID=0x00000004 Parent=0x00000003 SizeRef=460,1023 Split=Y Selected=0x59EB5E58
DockNode ID=0x00000007 Parent=0x00000003 SizeRef=460,421 Selected=0xFABAEAF4
DockNode ID=0x00000004 Parent=0x00000003 SizeRef=460,926 Split=Y Selected=0x59EB5E58
DockNode ID=0x00000020 Parent=0x00000004 SizeRef=374,808 Split=Y Selected=0x59EB5E58
DockNode ID=0x00000022 Parent=0x00000020 SizeRef=374,805 Selected=0x59EB5E58
DockNode ID=0x00000022 Parent=0x00000020 SizeRef=374,706 Selected=0x59EB5E58
DockNode ID=0x00000023 Parent=0x00000020 SizeRef=374,220 Selected=0x0A6C7D00
DockNode ID=0x00000021 Parent=0x00000004 SizeRef=374,217 Selected=0xAD4EBAD1
DockSpace ID=0xCFEB2616 Pos=0,22 Size=2560,1347 Split=X Selected=0xE6E1FC80
Expand All @@ -3131,3 +3149,31 @@ DockSpace ID=0xCFEB2616 Pos=0,22 Size=2560,1347 Split=X Selected=0
DockNode ID=0x0000001E Parent=0x00000006 SizeRef=669,815 Selected=0x5FFA0C6D
DockNode ID=0x0000001C Parent=0xCFEB2616 SizeRef=628,815 Selected=0x7EF55D0E

[VimontGames][Animation###Animation_1488493778]
Open=0
[VimontGames][Console###Console_3465924071]
Open=0
[VimontGames][Debug###Debug_3465924071]
Open=0
[VimontGames][Framegraph###Framegraph_4100248654]
Open=0
[VimontGames][Gizmo###Gizmo_3465924071]
Open=0
[VimontGames][Lighting###Lighting_4100248654]
Open=0
[VimontGames][Materials###Materials_4100248654]
Open=0
[VimontGames][Misc###Misc_4100248654]
Open=0
[VimontGames][Physics###Physics_1488493778]
Open=0
[VimontGames][Post-process###Post-process_4100248654]
Open=1
[VimontGames][Presentation###Presentation_4100248654]
Open=1
[VimontGames][Project###Project_1488493778]
Open=0
[VimontGames][Raytracing###Raytracing_4100248654]
Open=0
[VimontGames][Time###Time_1488493778]
Open=0
25 changes: 25 additions & 0 deletions src/editor/ImGui/Extensions/ImGuiExtensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ using namespace vg::renderer;

namespace ImGui
{
//--------------------------------------------------------------------------------------
bool PersistentTreeNode(vg::core::IObject * _object, const vg::core::IProperty * _prop, ImGuiTreeNodeFlags _flags)
{
const auto label = ImGui::getObjectPropertyLabel(_object, _prop);

auto * imGuiAdapter = Editor::get()->getRenderer()->GetImGuiAdapter();
auto & customData = imGuiAdapter->GetCustomData(label);

customData.isOpen = ImGui::TreeNodeEx(label.c_str(), _flags | (customData.isOpen ? ImGuiTreeNodeFlags_DefaultOpen : 0));

return customData.isOpen;
}

//--------------------------------------------------------------------------------------
bool PersistentCollapsingHeader(vg::core::IObject * _object, const vg::core::IProperty * _prop)
{
return PersistentTreeNode(_object, _prop, ImGuiTreeNodeFlags_CollapsingHeader);
}

//--------------------------------------------------------------------------------------
bool IconBegin(const char * icon, const char * name, bool * p_open, ImGuiWindowFlags flags)
{
Expand Down Expand Up @@ -324,6 +343,12 @@ namespace ImGui
return fmt::sprintf("%s###%s_%p", _label.c_str(), _subLabel.c_str(), _object);
}

//--------------------------------------------------------------------------------------
vg::core::string getObjectPropertyLabel(const vg::core::IObject * _object, const vg::core::IProperty * _prop)
{
return fmt::sprintf("%s###%s_%u", _prop->GetDisplayName(), _prop->GetName(), (uint_ptr)_object->GetUID());
}

//--------------------------------------------------------------------------------------
ImVec2 GetWindowContentRegionSize()
{
Expand Down
8 changes: 8 additions & 0 deletions src/editor/ImGui/Extensions/imGuiExtensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace vg::core
{
class IObject;
class IProperty;
}

namespace vg::renderer
Expand Down Expand Up @@ -37,6 +38,13 @@ namespace ImGui
vg::core::string getObjectLabel(const vg::core::string & _label, const void * _object);
vg::core::string getObjectLabel(const vg::core::string & _label, const vg::core::string & _subLabel, const void * _object);

// This version uses UID to generate a persistent ID
vg::core::string getObjectPropertyLabel(const vg::core::IObject * _object, const vg::core::IProperty * _prop);

// These versions save the "opened" state in the imgui.ini file
bool PersistentTreeNode(vg::core::IObject * _object, const vg::core::IProperty * _prop, ImGuiTreeNodeFlags _flags = 0);
bool PersistentCollapsingHeader(vg::core::IObject * _object, const vg::core::IProperty * _prop);

ImVec2 GetWindowContentRegionSize();

void CollapsingHeaderLabel(const ImVec2 & _headerPos, const vg::core::string & _label, bool _enabled);
Expand Down
11 changes: 7 additions & 4 deletions src/editor/ImGui/Window/ImGuiWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1208,16 +1208,19 @@ namespace vg::editor
{
if (_objectContext.m_treeNodes.size() > 0 || dynamic_cast<IComponent *>(_object) || dynamic_cast<IComponent *>(_object->GetParent()))
{
auto & newInfo = _objectContext.m_treeNodes.emplace_back();
if (_objectContext.m_treeNodes.back().treeNodeOpen)
{
auto & newInfo = _objectContext.m_treeNodes.emplace_back();

newInfo.treeNodeOpen = ImGui::TreeNodeEx(ImGui::getObjectLabel(_prop->GetDisplayName(), _prop).c_str(), ImGuiTreeNodeFlags_DefaultOpen);
newInfo.treeNodeIsCollapsingHeader = false;
newInfo.treeNodeOpen = ImGui::PersistentTreeNode(_object, _prop);
newInfo.treeNodeIsCollapsingHeader = false;
}
}
else
{
auto & newInfo = _objectContext.m_treeNodes.emplace_back();

newInfo.treeNodeOpen = ImGui::CollapsingHeader(ImGui::getObjectLabel(_prop->GetDisplayName(), _prop).c_str(), ImGuiTreeNodeFlags_DefaultOpen);
newInfo.treeNodeOpen = ImGui::PersistentCollapsingHeader(_object, _prop);
newInfo.treeNodeIsCollapsingHeader = true;
}
}
Expand Down
53 changes: 30 additions & 23 deletions src/renderer/IImGuiAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,50 @@ namespace vg

namespace renderer
{
struct CustomImGuiData
{
bool isOpen;
};

class IImGuiAdapter
{
public:
using BeginFrameCallback = void(*)(void);

virtual ~IImGuiAdapter () = default;
virtual ~IImGuiAdapter () = default;

virtual void SetGUITheme (Theme _theme) = 0;
virtual bool IsCurrentThemeDark () const = 0;

virtual void SetGUITheme (Theme _theme) = 0;
virtual bool IsCurrentThemeDark () const = 0;
virtual const ImVec4 & GetTextColor () const = 0;
virtual const ImVec4 & GetUnsavedPropertyColor () const = 0;
virtual const ImVec4 & GetPrefabOverridePropertyColor () const = 0;

virtual const ImVec4 & GetTextColor () const = 0;
virtual const ImVec4 & GetUnsavedPropertyColor () const = 0;
virtual const ImVec4 & GetPrefabOverridePropertyColor () const = 0;
virtual const ImVec4 & GetRowColorEven () const = 0;
virtual const ImVec4 & GetRowColorOdd () const = 0;

virtual const ImVec4 & GetRowColorEven () const = 0;
virtual const ImVec4 & GetRowColorOdd () const = 0;
virtual const ImVec4 & GetWarningColor () const = 0;
virtual const ImVec4 & GetErrorColor () const = 0;

virtual const ImVec4 & GetWarningColor () const = 0;
virtual const ImVec4 & GetErrorColor () const = 0;
virtual bool IsKeyboardFocused () const = 0;
virtual bool IsMouseFocused () const = 0;

virtual bool IsKeyboardFocused () const = 0;
virtual bool IsMouseFocused () const = 0;
virtual ImTextureID GetTextureID (const gfx::ITexture * _texture) = 0;
virtual void ReleaseTextureID (const gfx::ITexture * _texture) = 0;

virtual ImTextureID GetTextureID (const gfx::ITexture * _texture) = 0;
virtual void ReleaseTextureID (const gfx::ITexture * _texture) = 0;
virtual void AddBeginFrameCallback (BeginFrameCallback _func) = 0;
virtual ImFont * GetFont (Font _font, FontStyle _style = FontStyle::Regular) = 0;
virtual const char * GetFontPath (Font _font, FontStyle _style) const = 0;

virtual void AddBeginFrameCallback (BeginFrameCallback _func) = 0;
virtual ImFont * GetFont (Font _font, FontStyle _style = FontStyle::Regular) = 0;
virtual const char * GetFontPath (Font _font, FontStyle _style) const = 0;
virtual void PushDefaultFont () = 0;
virtual void PushFont (vg::renderer::Font _font) = 0;
virtual void PushFont (vg::renderer::Font _font, vg::renderer::FontStyle _style) = 0;
virtual void PopFont () = 0;

virtual void PushDefaultFont () = 0;
virtual void PushFont (vg::renderer::Font _font) = 0;
virtual void PushFont (vg::renderer::Font _font, vg::renderer::FontStyle _style) = 0;
virtual void PopFont () = 0;
virtual void PushFontStyle (vg::renderer::FontStyle _style) = 0;
virtual void PopFontStyle () = 0;

virtual void PushFontStyle (vg::renderer::FontStyle _style) = 0;
virtual void PopFontStyle () = 0;
virtual CustomImGuiData & GetCustomData (const core::string & _name) = 0;
};
}
}
10 changes: 9 additions & 1 deletion src/renderer/RenderPass/ImGui/ImGuiAdapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ namespace vg::renderer
m_currentFont(s_defaultFont),
m_currentFontStyle(s_defaultFontStyle),
m_currentFontSize(s_defaultFontSize)
{
{
ImGui::CreateContext();
ImGuiIO & io = ImGui::GetIO();
io.ConfigWindowsMoveFromTitleBarOnly = true;

m_settingsHandler.init();

io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.ConfigDockingTransparentPayload = true;

Expand Down Expand Up @@ -950,4 +952,10 @@ namespace vg::renderer
fileDialog->SetFileStyle(IGFD_FileStyleByTypeDir, "", textColor, editor::style::icon::Folder);
fileDialog->SetFileStyle(IGFD_FileStyleByTypeFile, "", textColor, editor::style::icon::File);
}

//--------------------------------------------------------------------------------------
CustomImGuiData & ImGuiAdapter::GetCustomData(const core::string & _name)
{
return m_settingsHandler.getCustomData(_name);
}
}
1 change: 1 addition & 0 deletions src/renderer/RenderPass/ImGui/ImGuiPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "editor/IEditor.h"

#include "ImGui.hpp"
#include "ImGuiSettings.hpp"
#include "ImGuiAdapter.hpp"

using namespace vg::core;
Expand Down
22 changes: 22 additions & 0 deletions src/renderer/RenderPass/ImGui/ImGuiSettings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "core/Singleton/Singleton.h"

namespace vg::renderer
{
class ImGuiSettings : public core::Singleton<ImGuiSettings>
{
public:
void init();

CustomImGuiData & getCustomData(const core::string & _name);

private:
static void * onReadOpen(ImGuiContext * _imGuiContext, ImGuiSettingsHandler * _imGuiSettingsHandler, const char * _name);
static void onReadLine(ImGuiContext * _imGuiContext, ImGuiSettingsHandler * _imGuiSettingsHandler, void * _entry, const char * _line);
static void onWriteAll(ImGuiContext * _imGuiContext, ImGuiSettingsHandler * _imGuiSettingsHandler, ImGuiTextBuffer * _outputTextBuffer);

private:
core::map<core::string, CustomImGuiData> m_customImGuiData;
};
}
54 changes: 54 additions & 0 deletions src/renderer/RenderPass/ImGui/ImGuiSettings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "ImGuiSettings.h"

namespace vg::renderer
{
//--------------------------------------------------------------------------------------
void ImGuiSettings::init()
{
ImGuiSettingsHandler ini_handler;
ini_handler.TypeName = "VimontGames";
ini_handler.TypeHash = ImHashStr("VimontGames");
ini_handler.ReadOpenFn = onReadOpen;
ini_handler.ReadLineFn = onReadLine;
ini_handler.WriteAllFn = onWriteAll;
ImGui::GetCurrentContext()->SettingsHandlers.push_back(ini_handler);

ImGui::LoadIniSettingsFromDisk(ImGui::GetIO().IniFilename);
}

//--------------------------------------------------------------------------------------
void * ImGuiSettings::onReadOpen(ImGuiContext * _imGuiContext, ImGuiSettingsHandler * _imGuiSettingsHandler, const char * _name)
{
auto & customState = ImGuiSettings::get()->m_customImGuiData;
customState[_name] = CustomImGuiData();
return &customState[_name];
}

//--------------------------------------------------------------------------------------
void ImGuiSettings::onReadLine(ImGuiContext * _imGuiContext, ImGuiSettingsHandler * _imGuiSettingsHandler, void * _entry, const char * _line)
{
CustomImGuiData * data = (CustomImGuiData *)_entry;
int isOpen;
if (sscanf(_line, "Open=%d", &isOpen) == 1)
data->isOpen = (isOpen != 0);
}

//--------------------------------------------------------------------------------------
// Function to write data to `imgui.ini`
//--------------------------------------------------------------------------------------
void ImGuiSettings::onWriteAll(ImGuiContext * _imGuiContext, ImGuiSettingsHandler * _imGuiSettingsHandler, ImGuiTextBuffer * _outputTextBuffer)
{
const auto & customState = ImGuiSettings::get()->m_customImGuiData;
for (const auto & pair : customState)
{
_outputTextBuffer->appendf("[%s][%s]\n", _imGuiSettingsHandler->TypeName, pair.first.c_str());
_outputTextBuffer->appendf("Open=%d\n", pair.second.isOpen ? 1 : 0);
}
}

//--------------------------------------------------------------------------------------
CustomImGuiData & ImGuiSettings::getCustomData(const core::string & _name)
{
return m_customImGuiData[_name];
}
}
Loading

0 comments on commit f2a7f60

Please sign in to comment.