Docking: Fix to allow basic reload of non-docking .ini data (following d33021d8) + moved settings blocks

This commit is contained in:
omar 2020-05-07 22:46:06 +02:00
parent 582f4db59b
commit 51e568f9dc
2 changed files with 55 additions and 60 deletions

101
imgui.cpp
View File

@ -5206,6 +5206,8 @@ ImGuiWindow* ImGui::FindWindowByName(const char* name)
static void ApplyWindowSettings(ImGuiWindow* window, ImGuiWindowSettings* settings) static void ApplyWindowSettings(ImGuiWindow* window, ImGuiWindowSettings* settings)
{ {
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
window->ViewportPos = main_viewport->Pos;
if (settings->ViewportId) if (settings->ViewportId)
{ {
window->ViewportId = settings->ViewportId; window->ViewportId = settings->ViewportId;
@ -5217,7 +5219,6 @@ static void ApplyWindowSettings(ImGuiWindow* window, ImGuiWindowSettings* settin
window->Collapsed = settings->Collapsed; window->Collapsed = settings->Collapsed;
window->DockId = settings->DockId; window->DockId = settings->DockId;
window->DockOrder = settings->DockOrder; window->DockOrder = settings->DockOrder;
} }
static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags) static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags)
@ -5233,6 +5234,7 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags)
// Default/arbitrary window position. Use SetNextWindowPos() with the appropriate condition flag to change the initial position of a window. // Default/arbitrary window position. Use SetNextWindowPos() with the appropriate condition flag to change the initial position of a window.
ImGuiViewport* main_viewport = ImGui::GetMainViewport(); ImGuiViewport* main_viewport = ImGui::GetMainViewport();
window->Pos = main_viewport->Pos + ImVec2(60, 60); window->Pos = main_viewport->Pos + ImVec2(60, 60);
window->ViewportPos = main_viewport->Pos;
// User can disable loading and saving of settings. Tooltip and child windows also don't store settings. // User can disable loading and saving of settings. Tooltip and child windows also don't store settings.
if (!(flags & ImGuiWindowFlags_NoSavedSettings)) if (!(flags & ImGuiWindowFlags_NoSavedSettings))
@ -5241,7 +5243,6 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags)
// Retrieve settings from .ini file // Retrieve settings from .ini file
window->SettingsOffset = g.SettingsWindows.offset_from_ptr(settings); window->SettingsOffset = g.SettingsWindows.offset_from_ptr(settings);
SetWindowConditionAllowFlags(window, ImGuiCond_FirstUseEver, false); SetWindowConditionAllowFlags(window, ImGuiCond_FirstUseEver, false);
window->ViewportPos = main_viewport->Pos;
ApplyWindowSettings(window, settings); ApplyWindowSettings(window, settings);
} }
window->DC.CursorStartPos = window->DC.CursorMaxPos = window->Pos; // So first call to CalcContentSize() doesn't return crazy values window->DC.CursorStartPos = window->DC.CursorMaxPos = window->Pos; // So first call to CalcContentSize() doesn't return crazy values
@ -10564,8 +10565,6 @@ void ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size)
for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++) for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++)
if (g.SettingsHandlers[handler_n].ApplyAllFn) if (g.SettingsHandlers[handler_n].ApplyAllFn)
g.SettingsHandlers[handler_n].ApplyAllFn(&g, &g.SettingsHandlers[handler_n]); g.SettingsHandlers[handler_n].ApplyAllFn(&g, &g.SettingsHandlers[handler_n]);
DockContextOnLoadSettings(&g);
} }
void ImGui::SaveIniSettingsToDisk(const char* ini_filename) void ImGui::SaveIniSettingsToDisk(const char* ini_filename)
@ -10624,9 +10623,10 @@ static void WindowSettingsHandler_ApplyAll(ImGuiContext* ctx, ImGuiSettingsHandl
static void* WindowSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name) static void* WindowSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name)
{ {
ImGuiWindowSettings* settings = ImGui::FindWindowSettings(ImHashStr(name)); ImGuiWindowSettings* settings = ImGui::FindOrCreateWindowSettings(name);
if (!settings) ImGuiID id = settings->ID;
settings = ImGui::CreateNewWindowSettings(name); *settings = ImGuiWindowSettings();
settings->ID = id;
settings->WantApply = true; settings->WantApply = true;
return (void*)settings; return (void*)settings;
} }
@ -11683,6 +11683,7 @@ namespace ImGui
static void DockSettingsRenameNodeReferences(ImGuiID old_node_id, ImGuiID new_node_id); static void DockSettingsRenameNodeReferences(ImGuiID old_node_id, ImGuiID new_node_id);
static void DockSettingsRemoveNodeReferences(ImGuiID* node_ids, int node_ids_count); static void DockSettingsRemoveNodeReferences(ImGuiID* node_ids, int node_ids_count);
static ImGuiDockNodeSettings* DockSettingsFindNodeSettings(ImGuiContext* ctx, ImGuiID node_id); static ImGuiDockNodeSettings* DockSettingsFindNodeSettings(ImGuiContext* ctx, ImGuiID node_id);
static void DockSettingsHandler_ApplyAll(ImGuiContext*, ImGuiSettingsHandler*);
static void* DockSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name); static void* DockSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name);
static void DockSettingsHandler_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line); static void DockSettingsHandler_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line);
static void DockSettingsHandler_WriteAll(ImGuiContext* imgui_ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf); static void DockSettingsHandler_WriteAll(ImGuiContext* imgui_ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf);
@ -11700,7 +11701,6 @@ namespace ImGui
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// - DockContextInitialize() // - DockContextInitialize()
// - DockContextShutdown() // - DockContextShutdown()
// - DockContextOnLoadSettings()
// - DockContextClearNodes() // - DockContextClearNodes()
// - DockContextRebuildNodes() // - DockContextRebuildNodes()
// - DockContextUpdateUndocking() // - DockContextUpdateUndocking()
@ -11726,6 +11726,7 @@ void ImGui::DockContextInitialize(ImGuiContext* ctx)
ImGuiSettingsHandler ini_handler; ImGuiSettingsHandler ini_handler;
ini_handler.TypeName = "Docking"; ini_handler.TypeName = "Docking";
ini_handler.TypeHash = ImHashStr("Docking"); ini_handler.TypeHash = ImHashStr("Docking");
ini_handler.ApplyAllFn = DockSettingsHandler_ApplyAll;
ini_handler.ReadOpenFn = DockSettingsHandler_ReadOpen; ini_handler.ReadOpenFn = DockSettingsHandler_ReadOpen;
ini_handler.ReadLineFn = DockSettingsHandler_ReadLine; ini_handler.ReadLineFn = DockSettingsHandler_ReadLine;
ini_handler.WriteAllFn = DockSettingsHandler_WriteAll; ini_handler.WriteAllFn = DockSettingsHandler_WriteAll;
@ -11743,13 +11744,6 @@ void ImGui::DockContextShutdown(ImGuiContext* ctx)
g.DockContext = NULL; g.DockContext = NULL;
} }
void ImGui::DockContextOnLoadSettings(ImGuiContext* ctx)
{
ImGuiDockContext* dc = ctx->DockContext;
DockContextPruneUnusedSettingsNodes(ctx);
DockContextBuildNodesFromSettings(ctx, dc->SettingsNodes.Data, dc->SettingsNodes.Size);
}
void ImGui::DockContextClearNodes(ImGuiContext* ctx, ImGuiID root_id, bool clear_persistent_docking_references) void ImGui::DockContextClearNodes(ImGuiContext* ctx, ImGuiID root_id, bool clear_persistent_docking_references)
{ {
IM_UNUSED(ctx); IM_UNUSED(ctx);
@ -14251,6 +14245,7 @@ void ImGui::DockBuilderRemoveNode(ImGuiID node_id)
DockContextRemoveNode(ctx, node, true); DockContextRemoveNode(ctx, node, true);
} }
// root_id = 0 to remove all, root_id != 0 to remove child of given node.
void ImGui::DockBuilderRemoveNodeChildNodes(ImGuiID root_id) void ImGui::DockBuilderRemoveNodeChildNodes(ImGuiID root_id)
{ {
ImGuiContext* ctx = GImGui; ImGuiContext* ctx = GImGui;
@ -14821,6 +14816,7 @@ void ImGui::BeginDockableDragDropTarget(ImGuiWindow* window)
// - DockSettingsRenameNodeReferences() // - DockSettingsRenameNodeReferences()
// - DockSettingsRemoveNodeReferences() // - DockSettingsRemoveNodeReferences()
// - DockSettingsFindNodeSettings() // - DockSettingsFindNodeSettings()
// - DockSettingsHandler_ApplyAll()
// - DockSettingsHandler_ReadOpen() // - DockSettingsHandler_ReadOpen()
// - DockSettingsHandler_ReadLine() // - DockSettingsHandler_ReadLine()
// - DockSettingsHandler_DockNodeToSettings() // - DockSettingsHandler_DockNodeToSettings()
@ -14871,6 +14867,15 @@ static ImGuiDockNodeSettings* ImGui::DockSettingsFindNodeSettings(ImGuiContext*
return NULL; return NULL;
} }
static void ImGui::DockSettingsHandler_ApplyAll(ImGuiContext* ctx, ImGuiSettingsHandler*)
{
// Prune settings at boot time only
ImGuiDockContext* dc = ctx->DockContext;
if (ctx->Windows.Size == 0)
DockContextPruneUnusedSettingsNodes(ctx);
DockContextBuildNodesFromSettings(ctx, dc->SettingsNodes.Data, dc->SettingsNodes.Size);
}
static void* ImGui::DockSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name) static void* ImGui::DockSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name)
{ {
if (strcmp(name, "Data") != 0) if (strcmp(name, "Data") != 0)
@ -15690,14 +15695,11 @@ void ImGui::ShowMetricsWindow(bool* p_open)
// Details for Docking // Details for Docking
#ifdef IMGUI_HAS_DOCK #ifdef IMGUI_HAS_DOCK
if (ImGui::TreeNode("Docking")) if (ImGui::TreeNode("Dock nodes"))
{ {
ImGuiDockContext* dc = g.DockContext; ImGuiDockContext* dc = g.DockContext;
ImGui::Checkbox("Ctrl shows window dock info", &show_docking_nodes); ImGui::Checkbox("Ctrl shows window dock info", &show_docking_nodes);
if (ImGui::SmallButton("Clear nodes")) { DockContextClearNodes(&g, 0, true); }
if (ImGui::TreeNode("Dock nodes"))
{
if (ImGui::SmallButton("Clear settings")) { DockContextClearNodes(&g, 0, true); }
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::SmallButton("Rebuild all")) { dc->WantFullRebuild = true; } if (ImGui::SmallButton("Rebuild all")) { dc->WantFullRebuild = true; }
for (int n = 0; n < dc->Nodes.Data.Size; n++) for (int n = 0; n < dc->Nodes.Data.Size; n++)
@ -15706,38 +15708,6 @@ void ImGui::ShowMetricsWindow(bool* p_open)
Funcs::NodeDockNode(node, "Node"); Funcs::NodeDockNode(node, "Node");
ImGui::TreePop(); ImGui::TreePop();
} }
if (ImGui::TreeNode("Settings"))
{
if (ImGui::SmallButton("Refresh"))
SaveIniSettingsToMemory();
ImGui::SameLine();
if (ImGui::SmallButton("Save to disk"))
SaveIniSettingsToDisk(g.IO.IniFilename);
ImGui::Separator();
ImGui::Text("Docked Windows:");
for (ImGuiWindowSettings* settings = g.SettingsWindows.begin(); settings != NULL; settings = g.SettingsWindows.next_chunk(settings))
if (settings->DockId != 0)
ImGui::BulletText("Window '%s' -> DockId %08X", settings->GetName(), settings->DockId);
ImGui::Separator();
ImGui::Text("Dock Nodes:");
for (int n = 0; n < dc->SettingsNodes.Size; n++)
{
ImGuiDockNodeSettings* settings = &dc->SettingsNodes[n];
const char* selected_tab_name = NULL;
if (settings->SelectedWindowId)
{
if (ImGuiWindow* window = FindWindowByID(settings->SelectedWindowId))
selected_tab_name = window->Name;
else if (ImGuiWindowSettings* window_settings = FindWindowSettings(settings->SelectedWindowId))
selected_tab_name = window_settings->GetName();
}
ImGui::BulletText("Node %08X, Parent %08X, SelectedTab %08X ('%s')", settings->ID, settings->ParentNodeId, settings->SelectedWindowId, selected_tab_name ? selected_tab_name : settings->SelectedWindowId ? "N/A" : "");
}
ImGui::TreePop();
}
ImGui::TreePop();
}
#endif // #define IMGUI_HAS_DOCK #endif // #define IMGUI_HAS_DOCK
// Settings // Settings
@ -15746,6 +15716,9 @@ void ImGui::ShowMetricsWindow(bool* p_open)
if (ImGui::SmallButton("Clear")) if (ImGui::SmallButton("Clear"))
ImGui::ClearIniSettings(); ImGui::ClearIniSettings();
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::SmallButton("Save to memory"))
ImGui::SaveIniSettingsToMemory();
ImGui::SameLine();
if (ImGui::SmallButton("Save to disk")) if (ImGui::SmallButton("Save to disk"))
ImGui::SaveIniSettingsToDisk(g.IO.IniFilename); ImGui::SaveIniSettingsToDisk(g.IO.IniFilename);
ImGui::SameLine(); ImGui::SameLine();
@ -15757,7 +15730,7 @@ void ImGui::ShowMetricsWindow(bool* p_open)
if (ImGui::TreeNode("SettingsHandlers", "Settings handlers: (%d)", g.SettingsHandlers.Size)) if (ImGui::TreeNode("SettingsHandlers", "Settings handlers: (%d)", g.SettingsHandlers.Size))
{ {
for (int n = 0; n < g.SettingsHandlers.Size; n++) for (int n = 0; n < g.SettingsHandlers.Size; n++)
ImGui::TextUnformatted(g.SettingsHandlers[n].TypeName); ImGui::BulletText("%s", g.SettingsHandlers[n].TypeName);
ImGui::TreePop(); ImGui::TreePop();
} }
if (ImGui::TreeNode("SettingsWindows", "Settings packed data: Windows: %d bytes", g.SettingsWindows.size())) if (ImGui::TreeNode("SettingsWindows", "Settings packed data: Windows: %d bytes", g.SettingsWindows.size()))
@ -15766,6 +15739,28 @@ void ImGui::ShowMetricsWindow(bool* p_open)
Funcs::NodeWindowSettings(settings); Funcs::NodeWindowSettings(settings);
ImGui::TreePop(); ImGui::TreePop();
} }
if (ImGui::TreeNode("SettingsDocking", "Settings packed data: Docking"))
{
ImGui::Text("In SettingsWindows:");
for (ImGuiWindowSettings* settings = g.SettingsWindows.begin(); settings != NULL; settings = g.SettingsWindows.next_chunk(settings))
if (settings->DockId != 0)
ImGui::BulletText("Window '%s' -> DockId %08X", settings->GetName(), settings->DockId);
ImGui::Text("In SettingsNodes:");
for (int n = 0; n < g.DockContext->SettingsNodes.Size; n++)
{
ImGuiDockNodeSettings* settings = &g.DockContext->SettingsNodes[n];
const char* selected_tab_name = NULL;
if (settings->SelectedWindowId)
{
if (ImGuiWindow* window = FindWindowByID(settings->SelectedWindowId))
selected_tab_name = window->Name;
else if (ImGuiWindowSettings* window_settings = FindWindowSettings(settings->SelectedWindowId))
selected_tab_name = window_settings->GetName();
}
ImGui::BulletText("Node %08X, Parent %08X, SelectedTab %08X ('%s')", settings->ID, settings->ParentNodeId, settings->SelectedWindowId, selected_tab_name ? selected_tab_name : settings->SelectedWindowId ? "N/A" : "");
}
ImGui::TreePop();
}
if (ImGui::TreeNode("SettingsIniData", "Settings unpacked data (.ini): %d bytes", g.SettingsIniData.size())) if (ImGui::TreeNode("SettingsIniData", "Settings unpacked data (.ini): %d bytes", g.SettingsIniData.size()))
{ {
char* buf = (char*)(void*)(g.SettingsIniData.Buf.Data ? g.SettingsIniData.Buf.Data : ""); char* buf = (char*)(void*)(g.SettingsIniData.Buf.Data ? g.SettingsIniData.Buf.Data : "");

View File

@ -2058,7 +2058,7 @@ namespace ImGui
IMGUI_API ImGuiID DockBuilderAddNode(ImGuiID node_id = 0, ImGuiDockNodeFlags flags = 0); IMGUI_API ImGuiID DockBuilderAddNode(ImGuiID node_id = 0, ImGuiDockNodeFlags flags = 0);
IMGUI_API void DockBuilderRemoveNode(ImGuiID node_id); // Remove node and all its child, undock all windows IMGUI_API void DockBuilderRemoveNode(ImGuiID node_id); // Remove node and all its child, undock all windows
IMGUI_API void DockBuilderRemoveNodeDockedWindows(ImGuiID node_id, bool clear_persistent_docking_references = true); IMGUI_API void DockBuilderRemoveNodeDockedWindows(ImGuiID node_id, bool clear_persistent_docking_references = true);
IMGUI_API void DockBuilderRemoveNodeChildNodes(ImGuiID node_id); // Remove all split/hierarchy. All remaining docked windows will be re-docked to the root. IMGUI_API void DockBuilderRemoveNodeChildNodes(ImGuiID node_id); // Remove all split/hierarchy. All remaining docked windows will be re-docked to the remaining root node (node_id).
IMGUI_API void DockBuilderSetNodePos(ImGuiID node_id, ImVec2 pos); IMGUI_API void DockBuilderSetNodePos(ImGuiID node_id, ImVec2 pos);
IMGUI_API void DockBuilderSetNodeSize(ImGuiID node_id, ImVec2 size); IMGUI_API void DockBuilderSetNodeSize(ImGuiID node_id, ImVec2 size);
IMGUI_API ImGuiID DockBuilderSplitNode(ImGuiID node_id, ImGuiDir split_dir, float size_ratio_for_node_at_dir, ImGuiID* out_id_at_dir, ImGuiID* out_id_at_opposite_dir); // Create 2 child nodes in this parent node. IMGUI_API ImGuiID DockBuilderSplitNode(ImGuiID node_id, ImGuiDir split_dir, float size_ratio_for_node_at_dir, ImGuiID* out_id_at_dir, ImGuiID* out_id_at_opposite_dir); // Create 2 child nodes in this parent node.