From 5bb04213ec65927c61c70e0df6201ea5e776662a Mon Sep 17 00:00:00 2001 From: Pascal Thomet Date: Mon, 1 Jan 2024 15:11:03 +0100 Subject: [PATCH] Wip / borderless resizable --- .../backend_impls/abstract_runner.cpp | 4 + .../internal/borderless_resizable.cpp | 88 +++++++++++++++++++ .../internal/borderless_resizable.h | 10 +++ .../hello_imgui_demodocking.main.cpp | 1 + 4 files changed, 103 insertions(+) create mode 100644 src/hello_imgui/internal/borderless_resizable.cpp create mode 100644 src/hello_imgui/internal/borderless_resizable.h diff --git a/src/hello_imgui/internal/backend_impls/abstract_runner.cpp b/src/hello_imgui/internal/backend_impls/abstract_runner.cpp index 2eb31eac..3108a5fb 100644 --- a/src/hello_imgui/internal/backend_impls/abstract_runner.cpp +++ b/src/hello_imgui/internal/backend_impls/abstract_runner.cpp @@ -6,6 +6,7 @@ #include "hello_imgui/internal/clock_seconds.h" #include "hello_imgui/internal/platform/ini_folder_locations.h" #include "imgui.h" +#include "hello_imgui/internal/borderless_resizable.h" #include "hello_imgui/internal/imgui_global_context.h" // must be included before imgui_internal.h #include "imgui_internal.h" @@ -504,6 +505,9 @@ void AbstractRunner::RenderGui() if (params.imGuiWindowParams.showMenuBar) Menu_StatusBar::ShowMenu(params); + if (params.appWindowParams.borderless) // Need to add params.appWindowParams.borderlessResizable + HandleBorderlessResizable(mWindow, mBackendWindowHelper.get()); + if (params.callbacks.ShowGui) { bool wantAutoSize = WantAutoSize(); diff --git a/src/hello_imgui/internal/borderless_resizable.cpp b/src/hello_imgui/internal/borderless_resizable.cpp new file mode 100644 index 00000000..07be6035 --- /dev/null +++ b/src/hello_imgui/internal/borderless_resizable.cpp @@ -0,0 +1,88 @@ +#define IMGUI_DEFINE_MATH_OPERATORS +#include "hello_imgui/internal/borderless_resizable.h" +#include "hello_imgui/hello_imgui.h" +#include "imgui_internal.h" + + +namespace HelloImGui +{ + void HandleBorderlessResizable( + BackendApi::WindowPointer window, + BackendApi::IBackendWindowHelper * backendWindowHelper) + { + ImVec2 winSize = ImGui::GetWindowSize(); + auto highlightColor = ImGui::GetColorU32(ImGuiCol_FrameBgHovered, 0.8f); + + // Drag zone at the top of the window + { + ImVec2 topLeftCorner = ImGui::GetMainViewport()->Pos; + ImVec2 bottomRightCorner = topLeftCorner + ImVec2(winSize.x, HelloImGui::EmSize(1.5f)); + ImRect dragRect = ImRect(topLeftCorner, bottomRightCorner); + + ImVec2 mousePos = ImGui::GetIO().MousePos; + bool isMouseInDragZone = dragRect.Contains(mousePos); + + // highlight drag zone if hovering + if (isMouseInDragZone) + { + ImGui::GetForegroundDrawList()->AddRectFilled( + dragRect.Min, + dragRect.Max, + highlightColor + ); + } + + static bool isDragging = false; + if (ImGui::IsMouseDragging(0) && isMouseInDragZone) + isDragging = true; + if (ImGui::IsMouseReleased(0)) + isDragging = false; + + if (isDragging) + { + ImVec2 dragDelta = ImGui::GetMouseDragDelta(0); + ImGui::ResetMouseDragDelta(0); + + // move window + { + auto windowBounds = backendWindowHelper->GetWindowBounds(window); + windowBounds.position[0] += (int)dragDelta.x; + windowBounds.position[1] += (int)dragDelta.y; + backendWindowHelper->SetWindowBounds(window, windowBounds); + } + } + } + + // Drag zone at the bottom-right of the window + // This is still a draft, which needs to be completed + // and probably refactored: there is too much repetition between the two blocks of code + { + ImVec2 bottomRightCorner = ImGui::GetMainViewport()->Pos + ImGui::GetMainViewport()->Size; + ImVec2 topLeftCorner = bottomRightCorner - HelloImGui::EmToVec2(1.5f, 1.5f); + + // Small triangle in the bottom-right corner + ImVec2 t1 = bottomRightCorner; + ImVec2 t2 = bottomRightCorner - HelloImGui::EmToVec2(1.5f, 0.f); + ImVec2 t3 = bottomRightCorner - HelloImGui::EmToVec2(0.f, 1.5f); + + ImRect dragRect = ImRect(topLeftCorner, bottomRightCorner); + + ImVec2 mousePos = ImGui::GetIO().MousePos; + bool isMouseInDragZone = dragRect.Contains(mousePos); + + // highlight drag zone if hovering + if (isMouseInDragZone) + { + ImGui::GetForegroundDrawList()->AddTriangleFilled( + t1, t2, t3, + highlightColor + ); + } + + // To be completed... + + } + } +} + + diff --git a/src/hello_imgui/internal/borderless_resizable.h b/src/hello_imgui/internal/borderless_resizable.h new file mode 100644 index 00000000..08865369 --- /dev/null +++ b/src/hello_imgui/internal/borderless_resizable.h @@ -0,0 +1,10 @@ +#pragma once +#include "hello_imgui/runner_params.h" +#include "hello_imgui/internal/backend_impls/backend_window_helper/backend_window_helper.h" + +namespace HelloImGui +{ + void HandleBorderlessResizable( + BackendApi::WindowPointer window, + BackendApi::IBackendWindowHelper * backendWindowHelper); +} \ No newline at end of file diff --git a/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp b/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp index 9b7937ff..71a79bab 100644 --- a/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp +++ b/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp @@ -498,6 +498,7 @@ int main(int, char**) runnerParams.imGuiWindowParams.menuAppTitle = "Docking Demo"; runnerParams.appWindowParams.windowGeometry.size = {1000, 900}; runnerParams.appWindowParams.restorePreviousGeometry = true; + runnerParams.appWindowParams.borderless = true; // Load additional font runnerParams.callbacks.LoadAdditionalFonts = LoadFonts;