1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-19 13:03:11 +01:00

Centralise ResizeFrame in WindowBase and call when setting widgets or resizing

This commit is contained in:
Aaron van Geffen
2025-04-11 20:37:54 +02:00
parent 6d7b312e7a
commit 3f87e2b67a
75 changed files with 251 additions and 440 deletions

View File

@@ -1,5 +1,6 @@
#include "WindowBase.h"
#include "../config/Config.h"
#include "../entity/EntityList.h"
#include "../entity/EntityRegistry.h"
#include "Cursors.h"
@@ -31,10 +32,98 @@ namespace OpenRCT2
{
widgets.clear();
widgets.insert(widgets.end(), newWidgets.begin(), newWidgets.end());
ResizeFrame();
}
CursorID WindowBase::OnCursor(WidgetIndex, const ScreenCoordsXY&, CursorID)
{
return CursorID::Arrow;
}
static inline void repositionCloseButton(Widget& closeButton, int32_t windowWidth)
{
auto closeButtonSize = Config::Get().interface.EnlargedUi ? kCloseButtonSizeTouch : kCloseButtonSize;
if (Config::Get().interface.WindowButtonsOnTheLeft)
{
closeButton.left = 2;
closeButton.right = 2 + closeButtonSize;
}
else
{
closeButton.left = windowWidth - 3 - closeButtonSize;
closeButton.right = windowWidth - 3;
}
}
void WindowBase::ResizeFrame()
{
if (widgets.size() < 3)
return;
// Frame
auto& frameWidget = widgets[0];
if (frameWidget.type == WindowWidgetType::Frame)
{
frameWidget.right = width - 1;
frameWidget.bottom = height - 1;
}
// Title/caption
auto& titleWidget = widgets[1];
bool hasTitleWidget = titleWidget.type == WindowWidgetType::Caption;
if (hasTitleWidget)
titleWidget.right = width - 2;
// Close button
auto& closeButton = widgets[2];
if (closeButton.type == WindowWidgetType::CloseBox || closeButton.type == WindowWidgetType::Empty)
repositionCloseButton(closeButton, width);
// Page/resize widget
if (widgets.size() >= 4)
{
auto& pageWidget = widgets[3];
if (pageWidget.type == WindowWidgetType::Resize)
{
pageWidget.right = width - 1;
pageWidget.bottom = height - 1;
}
}
// Figure out if we need to push the other widgets down to accommodate a resized title/caption
auto preferredHeight = getTitleBarHeight();
auto currentHeight = titleWidget.height();
auto heightDifference = preferredHeight - currentHeight;
if (!hasTitleWidget || heightDifference == 0)
return;
Invalidate();
// Offset title and close button
titleWidget.bottom += heightDifference;
closeButton.bottom += heightDifference;
height += heightDifference;
min_height += heightDifference;
max_height += heightDifference;
Invalidate();
// Resize frame again to match new height
frameWidget.bottom = height - 1;
// Offset body widgets
// NB: we're offsetting page widget as well!
for (WidgetIndex i = 3; i < widgets.size(); i++)
{
widgets[i].top += heightDifference;
widgets[i].bottom += heightDifference;
}
// Offset viewport
if (viewport != nullptr)
viewport->pos.y += heightDifference;
}
} // namespace OpenRCT2