Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demo: fixed resizing while maintaining aspect ratio constraint #8028

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions imgui_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down