Skip to content

Commit

Permalink
Scale C++ menus down to fit the window (#14690)
Browse files Browse the repository at this point in the history
  • Loading branch information
grorp authored May 24, 2024
1 parent d5fc040 commit 728f643
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
10 changes: 3 additions & 7 deletions src/gui/guiKeyChangeMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,9 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
removeAllChildren();
key_used_text = nullptr;

const float s = m_gui_scale;
DesiredRect = core::rect<s32>(
screensize.X / 2 - 835 * s / 2,
screensize.Y / 2 - 430 * s / 2,
screensize.X / 2 + 835 * s / 2,
screensize.Y / 2 + 430 * s / 2
);
ScalingInfo info = getScalingInfo(screensize, v2u32(835, 430));
const float s = info.scale;
DesiredRect = info.rect;
recalculateAbsolutePosition(false);

v2s32 size = DesiredRect.getSize();
Expand Down
10 changes: 3 additions & 7 deletions src/gui/guiOpenURL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,9 @@ void GUIOpenURLMenu::regenerateGui(v2u32 screensize)
/*
Calculate new sizes and positions
*/
const float s = m_gui_scale;
DesiredRect = core::rect<s32>(
screensize.X / 2 - 580 * s / 2,
screensize.Y / 2 - 250 * s / 2,
screensize.X / 2 + 580 * s / 2,
screensize.Y / 2 + 250 * s / 2
);
ScalingInfo info = getScalingInfo(screensize, v2u32(580, 250));
const float s = info.scale;
DesiredRect = info.rect;
recalculateAbsolutePosition(false);

v2s32 size = DesiredRect.getSize();
Expand Down
11 changes: 3 additions & 8 deletions src/gui/guiPasswordChange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,9 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize)
/*
Calculate new sizes and positions
*/
const float s = m_gui_scale;

DesiredRect = core::rect<s32>(
screensize.X / 2 - 580 * s / 2,
screensize.Y / 2 - 300 * s / 2,
screensize.X / 2 + 580 * s / 2,
screensize.Y / 2 + 300 * s / 2
);
ScalingInfo info = getScalingInfo(screensize, v2u32(580, 300));
const float s = info.scale;
DesiredRect = info.rect;
recalculateAbsolutePosition(false);

v2s32 size = DesiredRect.getSize();
Expand Down
10 changes: 3 additions & 7 deletions src/gui/guiVolumeChange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,9 @@ void GUIVolumeChange::regenerateGui(v2u32 screensize)
/*
Calculate new sizes and positions
*/
const float s = m_gui_scale;
DesiredRect = core::rect<s32>(
screensize.X / 2 - 380 * s / 2,
screensize.Y / 2 - 200 * s / 2,
screensize.X / 2 + 380 * s / 2,
screensize.Y / 2 + 200 * s / 2
);
ScalingInfo info = getScalingInfo(screensize, v2u32(380, 200));
const float s = info.scale;
DesiredRect = info.rect;
recalculateAbsolutePosition(false);

v2s32 size = DesiredRect.getSize();
Expand Down
14 changes: 14 additions & 0 deletions src/gui/modalMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,17 @@ porting::AndroidDialogState GUIModalMenu::getAndroidUIInputState()
return porting::getInputDialogState();
}
#endif

GUIModalMenu::ScalingInfo GUIModalMenu::getScalingInfo(v2u32 screensize, v2u32 base_size)
{
f32 scale = m_gui_scale;
scale = std::min(scale, (f32)screensize.X / (f32)base_size.X);
scale = std::min(scale, (f32)screensize.Y / (f32)base_size.Y);
s32 w = base_size.X * scale, h = base_size.Y * scale;
return {scale, core::rect<s32>(
screensize.X / 2 - w / 2,
screensize.Y / 2 - h / 2,
screensize.X / 2 + w / 2,
screensize.Y / 2 + h / 2
)};
}
6 changes: 6 additions & 0 deletions src/gui/modalMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ class GUIModalMenu : public gui::IGUIElement
std::string m_jni_field_name;
#endif

struct ScalingInfo {
f32 scale;
core::rect<s32> rect;
};
ScalingInfo getScalingInfo(v2u32 screensize, v2u32 base_size);

// This is set to true if the menu is currently processing a second-touch event.
bool m_second_touch = false;
// This is set to true if the menu is currently processing a mouse event
Expand Down

0 comments on commit 728f643

Please sign in to comment.