From b2ec0741f101493d76967b2a869a01382a74b72f Mon Sep 17 00:00:00 2001 From: omar Date: Fri, 15 Dec 2017 20:59:00 +0100 Subject: [PATCH 1/3] Internals: Settings api tweaks --- imgui.cpp | 26 +++++++++++++++----------- imgui_internal.h | 11 +++++++---- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index d764d60c9..336c43f78 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -2525,7 +2525,7 @@ void ImGui::NewFrame() Begin("Debug##Default"); } -static void* SettingsHandlerWindow_ReadOpen(ImGuiContext&, const char* name) +static void* SettingsHandlerWindow_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name) { ImGuiWindowSettings* settings = ImGui::FindWindowSettings(ImHash(name, 0)); if (!settings) @@ -2533,7 +2533,7 @@ static void* SettingsHandlerWindow_ReadOpen(ImGuiContext&, const char* name) return (void*)settings; } -static void SettingsHandlerWindow_ReadLine(ImGuiContext&, void* entry, const char* line) +static void SettingsHandlerWindow_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line) { ImGuiWindowSettings* settings = (ImGuiWindowSettings*)entry; float x, y; @@ -2543,9 +2543,10 @@ static void SettingsHandlerWindow_ReadLine(ImGuiContext&, void* entry, const cha else if (sscanf(line, "Collapsed=%d", &i) == 1) settings->Collapsed = (i != 0); } -static void SettingsHandlerWindow_WriteAll(ImGuiContext& g, ImGuiTextBuffer* buf) +static void SettingsHandlerWindow_WriteAll(ImGuiContext* imgui_ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf) { // Gather data from windows that were active during this session + ImGuiContext& g = *imgui_ctx; for (int i = 0; i != g.Windows.Size; i++) { ImGuiWindow* window = g.Windows[i]; @@ -2570,7 +2571,7 @@ static void SettingsHandlerWindow_WriteAll(ImGuiContext& g, ImGuiTextBuffer* buf const char* name = settings->Name; if (const char* p = strstr(name, "###")) // Skip to the "###" marker if any. We don't skip past to match the behavior of GetID() name = p; - buf->appendf("[Window][%s]\n", name); + buf->appendf("[%s][%s]\n", handler->TypeName, name); buf->appendf("Pos=%d,%d\n", (int)settings->Pos.x, (int)settings->Pos.y); buf->appendf("Size=%d,%d\n", (int)settings->Size.x, (int)settings->Size.y); buf->appendf("Collapsed=%d\n", settings->Collapsed); @@ -2684,9 +2685,10 @@ static void LoadIniSettingsFromDisk(const char* ini_filename) ImGui::MemFree(file_data); } -ImGuiSettingsHandler* ImGui::FindSettingsHandler(ImGuiID type_hash) +ImGuiSettingsHandler* ImGui::FindSettingsHandler(const char* type_name) { ImGuiContext& g = *GImGui; + const ImGuiID type_hash = ImHash(type_name, 0, 0); for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++) if (g.SettingsHandlers[handler_n].TypeHash == type_hash) return &g.SettingsHandlers[handler_n]; @@ -2702,7 +2704,7 @@ static void LoadIniSettingsFromMemory(const char* buf_readonly) ImGuiContext& g = *GImGui; void* entry_data = NULL; - const ImGuiSettingsHandler* entry_handler = NULL; + ImGuiSettingsHandler* entry_handler = NULL; char* line_end = NULL; for (char* line = buf; line < buf_end; line = line_end + 1) @@ -2733,14 +2735,13 @@ static void LoadIniSettingsFromMemory(const char* buf_readonly) *type_end = 0; // Overwrite first ']' name_start++; // Skip second '[' } - const ImGuiID type_hash = ImHash(type_start, 0, 0); - entry_handler = ImGui::FindSettingsHandler(type_hash); - entry_data = entry_handler ? entry_handler->ReadOpenFn(g, name_start) : NULL; + entry_handler = ImGui::FindSettingsHandler(type_start); + entry_data = entry_handler ? entry_handler->ReadOpenFn(&g, entry_handler, name_start) : NULL; } else if (entry_handler != NULL && entry_data != NULL) { // Let type handler parse the line - entry_handler->ReadLineFn(g, entry_data, line); + entry_handler->ReadLineFn(&g, entry_handler, entry_data, line); } } ImGui::MemFree(buf); @@ -2770,7 +2771,10 @@ static void SaveIniSettingsToMemory(ImVector& out_buf) ImGuiTextBuffer buf; for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++) - g.SettingsHandlers[handler_n].WriteAllFn(g, &buf); + { + ImGuiSettingsHandler* handler = &g.SettingsHandlers[handler_n]; + handler->WriteAllFn(&g, handler, &buf); + } buf.Buf.pop_back(); // Remove extra zero-terminator used by ImGuiTextBuffer out_buf.swap(buf.Buf); diff --git a/imgui_internal.h b/imgui_internal.h index babda0b5d..6a2513d28 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -376,9 +376,12 @@ struct ImGuiSettingsHandler { const char* TypeName; // Short description stored in .ini file. Disallowed characters: '[' ']' ImGuiID TypeHash; // == ImHash(TypeName, 0, 0) - void* (*ReadOpenFn)(ImGuiContext& ctx, const char* name); - void (*ReadLineFn)(ImGuiContext& ctx, void* entry, const char* line); - void (*WriteAllFn)(ImGuiContext& ctx, ImGuiTextBuffer* out_buf); + void* (*ReadOpenFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, const char* name); + void (*ReadLineFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, void* entry, const char* line); + void (*WriteAllFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* out_buf); + void* UserData; + + ImGuiSettingsHandler() { memset(this, 0, sizeof(*this)); } }; // Mouse cursor data (used when io.MouseDrawCursor is set) @@ -870,7 +873,7 @@ namespace ImGui IMGUI_API void Initialize(); IMGUI_API void MarkIniSettingsDirty(); - IMGUI_API ImGuiSettingsHandler* FindSettingsHandler(ImGuiID type_id); + IMGUI_API ImGuiSettingsHandler* FindSettingsHandler(const char* type_name); IMGUI_API ImGuiWindowSettings* FindWindowSettings(ImGuiID id); IMGUI_API void SetActiveID(ImGuiID id, ImGuiWindow* window); From 4b1240b2e1371ce76de0e6cd30f53bfd88df4ecc Mon Sep 17 00:00:00 2001 From: omar Date: Thu, 23 Nov 2017 18:18:56 +0100 Subject: [PATCH 2/3] Fixed non-popup child windows not honoring the HiddenFrames flag (can't see a reason). Docking relies on this. --- imgui.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imgui.cpp b/imgui.cpp index 336c43f78..b0564cba5 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -2867,7 +2867,7 @@ static void AddWindowToRenderList(ImVector& out_render_list, ImGuiW ImGuiWindow* child = window->DC.ChildWindows[i]; if (!child->Active) // clipped children may have been marked not active continue; - if ((child->Flags & ImGuiWindowFlags_Popup) && child->HiddenFrames > 0) + if (child->HiddenFrames > 0) continue; AddWindowToRenderList(out_render_list, child); } From 4fc9f440732d8a5694550b98cb4030135457c230 Mon Sep 17 00:00:00 2001 From: omar Date: Wed, 3 Jan 2018 20:45:06 +0100 Subject: [PATCH 3/3] Merge of minor left-overs from private work branch to reduce drifts. Should be functionally a no-op. --- imgui.cpp | 25 +++++++++++++------------ imgui_internal.h | 2 +- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index b0564cba5..3728a890e 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -4339,9 +4339,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) IM_ASSERT(g.Initialized); // Forgot to call ImGui::NewFrame() IM_ASSERT(g.FrameCountEnded != g.FrameCount); // Called ImGui::Render() or ImGui::EndFrame() and haven't called ImGui::NewFrame() again yet - if (flags & ImGuiWindowFlags_NoInputs) - flags |= ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize; - // Find or create ImGuiWindow* window = FindWindowByName(name); if (!window) @@ -4350,6 +4347,12 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window = CreateNewWindow(name, size_on_first_use, flags); } + // Automatically disable manual moving/resizing when NoInputs is set + if (flags & ImGuiWindowFlags_NoInputs) + flags |= ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize; + //if (flags & ImGuiWindowFlags_NavFlattened) + // IM_ASSERT(flags & ImGuiWindowFlags_ChildWindow); + const int current_frame = g.FrameCount; const bool first_begin_of_the_frame = (window->LastFrameActive != current_frame); if (first_begin_of_the_frame) @@ -4877,18 +4880,16 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) } // Title text (FIXME: refactor text alignment facilities along with RenderText helpers) - const ImVec2 text_size = CalcTextSize(name, NULL, true); - ImVec2 text_min = window->Pos; - ImVec2 text_max = window->Pos + ImVec2(window->Size.x, style.FramePadding.y*2 + text_size.y); - ImRect clip_rect; - clip_rect.Max = ImVec2(window->Pos.x + window->Size.x - (p_open ? title_bar_rect.GetHeight() - 3 : style.FramePadding.x), text_max.y); // Match the size of CloseWindowButton() + ImVec2 text_size = CalcTextSize(name, NULL, true); + ImRect text_r = title_bar_rect; float pad_left = (flags & ImGuiWindowFlags_NoCollapse) == 0 ? (style.FramePadding.x + g.FontSize + style.ItemInnerSpacing.x) : style.FramePadding.x; float pad_right = (p_open != NULL) ? (style.FramePadding.x + g.FontSize + style.ItemInnerSpacing.x) : style.FramePadding.x; if (style.WindowTitleAlign.x > 0.0f) pad_right = ImLerp(pad_right, pad_left, style.WindowTitleAlign.x); - text_min.x += pad_left; - text_max.x -= pad_right; - clip_rect.Min = ImVec2(text_min.x, window->Pos.y); - RenderTextClipped(text_min, text_max, name, NULL, &text_size, style.WindowTitleAlign, &clip_rect); + text_r.Min.x += pad_left; + text_r.Max.x -= pad_right; + ImRect clip_rect = text_r; + clip_rect.Max.x = window->Pos.x + window->Size.x - (p_open ? title_bar_rect.GetHeight() - 3 : style.FramePadding.x); // Match the size of CloseButton() + RenderTextClipped(text_r.Min, text_r.Max, name, NULL, &text_size, style.WindowTitleAlign, &clip_rect); } // Save clipped aabb so we can access it in constant-time in FindHoveredWindow() diff --git a/imgui_internal.h b/imgui_internal.h index 6a2513d28..c80970250 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -775,7 +775,7 @@ struct IMGUI_API ImGuiWindow ImVec2 ScrollTargetCenterRatio; // 0.0f = scroll so that target position is at top, 0.5f = scroll so that target position is centered bool ScrollbarX, ScrollbarY; ImVec2 ScrollbarSizes; - bool Active; // Set to true on Begin() + bool Active; // Set to true on Begin(), unless Collapsed bool WasActive; bool WriteAccessed; // Set to true when any widget access the current window bool Collapsed; // Set when collapsing window to become only title-bar