From f59da1956ebd88818cfb955d995323bd632fe90a Mon Sep 17 00:00:00 2001 From: imdongye Date: Mon, 30 Sep 2024 02:42:18 +0900 Subject: [PATCH] Demo: fixed resizing while maintaining aspect constraint in ShowExampleAppConstrainedResize --- imgui_demo.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/imgui_demo.cpp b/imgui_demo.cpp index feaee3be9..ffdcc9f37 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -9117,11 +9117,33 @@ static void ShowExampleAppConstrainedResize(bool* p_open) static void AspectRatio(ImGuiSizeCallbackData* data) { float aspect_ratio = *(float*)data->UserData; - data->DesiredSize.y = (float)(int)(data->DesiredSize.x / aspect_ratio); + int current_cursor = ImGui::GetMouseCursor(); + if(current_cursor == ImGuiMouseCursor_ResizeNWSE || current_cursor == ImGuiMouseCursor_ResizeNESW) + { + if(aspect_ratio > data->DesiredSize.x / data->DesiredSize.y) + data->DesiredSize.x = aspect_ratio * data->DesiredSize.y; + else + data->DesiredSize.y = data->DesiredSize.x / aspect_ratio; + } + else if(current_cursor == ImGuiMouseCursor_ResizeNS) + data->DesiredSize.x = aspect_ratio * data->DesiredSize.y; + else if(current_cursor == ImGuiMouseCursor_ResizeEW) + data->DesiredSize.y = data->DesiredSize.x / aspect_ratio; } static void Square(ImGuiSizeCallbackData* data) { - data->DesiredSize.x = data->DesiredSize.y = IM_MAX(data->DesiredSize.x, data->DesiredSize.y); + int current_cursor = ImGui::GetMouseCursor(); + if(current_cursor == ImGuiMouseCursor_ResizeNWSE || current_cursor == ImGuiMouseCursor_ResizeNESW) + { + if(1.f > data->DesiredSize.x / data->DesiredSize.y) + data->DesiredSize.x = data->DesiredSize.y; + else + data->DesiredSize.y = data->DesiredSize.x; + } + else if(current_cursor == ImGuiMouseCursor_ResizeNS) + data->DesiredSize.x = data->DesiredSize.y; + else if(current_cursor == ImGuiMouseCursor_ResizeEW) + data->DesiredSize.y = data->DesiredSize.x; } static void Step(ImGuiSizeCallbackData* data) {