From 514d30d8cdd659b6af3c93512583a00ee18ecbaf Mon Sep 17 00:00:00 2001 From: omar Date: Sun, 14 Jan 2018 14:13:54 +0100 Subject: [PATCH] MovingWindow: Track click offset based on MovingWindow and not its RootWindow. Followup to 3849bb4470aebb4ff276113170f26cc82f990f49. Comments + adding a local to ease patch #1345. --- imgui.cpp | 15 +++++++++++---- imgui_internal.h | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index ff6b871d..fee26e2f 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -2392,10 +2392,17 @@ void ImGui::NewFrame() IM_ASSERT(g.MovingWindow->MoveId == g.MovingWindowMoveId); if (g.IO.MouseDown[0]) { + // MovingWindow = window we clicked on, could be a child window. We track it to preserve Focus and so that ActiveIdWindow == MovingWindow and ActiveId == MovingWindow->MoveId for consistency. + // actually_moving_window = MovingWindow->RootWindow. + ImGuiWindow* actually_moving_window = g.MovingWindow->RootWindow; ImVec2 pos = g.IO.MousePos - g.ActiveIdClickOffset; - if (g.MovingWindow->RootWindow->PosFloat.x != pos.x || g.MovingWindow->RootWindow->PosFloat.y != pos.y) - MarkIniSettingsDirty(g.MovingWindow->RootWindow); - g.MovingWindow->RootWindow->PosFloat = pos; + if (actually_moving_window != g.MovingWindow) + pos += actually_moving_window->PosFloat - g.MovingWindow->PosFloat; + if (actually_moving_window->PosFloat.x != pos.x || actually_moving_window->PosFloat.y != pos.y) + { + MarkIniSettingsDirty(actually_moving_window); + actually_moving_window->PosFloat = pos; + } FocusWindow(g.MovingWindow); } else @@ -2950,7 +2957,7 @@ void ImGui::EndFrame() // Set ActiveId even if the _NoMove flag is set, without it dragging away from a window with _NoMove would activate hover on other windows. FocusWindow(g.HoveredWindow); SetActiveID(g.HoveredWindow->MoveId, g.HoveredWindow); - g.ActiveIdClickOffset = g.IO.MousePos - g.HoveredRootWindow->Pos; + g.ActiveIdClickOffset = g.IO.MousePos - g.HoveredWindow->Pos; if (!(g.HoveredWindow->Flags & ImGuiWindowFlags_NoMove) && !(g.HoveredRootWindow->Flags & ImGuiWindowFlags_NoMove)) { g.MovingWindow = g.HoveredWindow; diff --git a/imgui_internal.h b/imgui_internal.h index 87a4a131..797f7075 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -534,7 +534,7 @@ struct ImGuiContext bool ActiveIdAllowOverlap; // Active widget allows another widget to steal active id (generally for overlapping widgets, but not always) ImVec2 ActiveIdClickOffset; // Clicked offset from upper-left corner, if applicable (currently only set by ButtonBehavior) ImGuiWindow* ActiveIdWindow; - ImGuiWindow* MovingWindow; // Track the child window we clicked on to move a window. + ImGuiWindow* MovingWindow; // Track the window we clicked on (in order to preserve focus). The actually window that is moved is generally MovingWindow->RootWindow. ImGuiID MovingWindowMoveId; // == MovingWindow->MoveId ImVector ColorModifiers; // Stack for PushStyleColor()/PopStyleColor() ImVector StyleModifiers; // Stack for PushStyleVar()/PopStyleVar()