From 6e607bbb378db2c39f141528405a62838a625b22 Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Wed, 8 May 2024 20:47:55 +0200 Subject: [PATCH] Optionally align the toolbar buttons horizontally centred --- src/openrct2-ui/windows/TopToolbar.cpp | 48 +++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/openrct2-ui/windows/TopToolbar.cpp b/src/openrct2-ui/windows/TopToolbar.cpp index 8187967583..a6c622905e 100644 --- a/src/openrct2-ui/windows/TopToolbar.cpp +++ b/src/openrct2-ui/windows/TopToolbar.cpp @@ -248,6 +248,19 @@ namespace OpenRCT2::Ui::Windows WIDX_NEWS, }; + static constexpr size_t _totalToolbarElements = kWidgetOrderLeftGroup.size() + kWidgetOrderRightGroup.size(); + + // Make a combined version of both halves of the toolbar, with a separator halfway. + static constexpr std::array kWidgetOrderCombined = []() { + std::array combined; + + auto halfWayPoint = std::copy(kWidgetOrderLeftGroup.begin(), kWidgetOrderLeftGroup.end(), combined.begin()); + *halfWayPoint = WIDX_SEPARATOR; + std::copy(kWidgetOrderRightGroup.begin(), kWidgetOrderRightGroup.end(), halfWayPoint + 1); + + return combined; + }(); + #pragma endregion static Widget _topToolbarWidgets[] = { @@ -3159,6 +3172,36 @@ namespace OpenRCT2::Ui::Windows } } + void AlignButtonsCentre() + { + // First, we figure out how much space we'll be needing + auto totalWidth = 0; + for (auto widgetIndex : kWidgetOrderCombined) + { + auto* widget = &widgets[widgetIndex]; + if (widget->type == WindowWidgetType::Empty && widgetIndex != WIDX_SEPARATOR) + continue; + + totalWidth += widget->width(); + } + + // We'll start from the centre of the UI... + auto xPos = (ContextGetWidth() - totalWidth) / 2; + + for (auto widgetIndex : kWidgetOrderCombined) + { + auto* widget = &widgets[widgetIndex]; + if (widget->type == WindowWidgetType::Empty && widgetIndex != WIDX_SEPARATOR) + continue; + + auto widgetWidth = widget->width(); + widget->left = xPos; + xPos += widgetWidth; + widget->right = xPos; + xPos += 1; + } + } + void OnPrepareDraw() override { ResetWidgetToDefaultState(); @@ -3173,7 +3216,10 @@ namespace OpenRCT2::Ui::Windows ApplyMapRotation(); ApplyFootpathPressed(); - AlignButtonsLeftRight(); + if (false) + AlignButtonsLeftRight(); + else + AlignButtonsCentre(); } void OnDraw(DrawPixelInfo& dpi) override