From fb55ab0742625eb54bad0c18279a083f781fca12 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sun, 2 Feb 2025 17:15:47 +0000 Subject: [PATCH] Codechange: Pass rect to DrawStationCoverageAreaText. (#13442) This moves the overflow behaviour to the callers, making it clearer why that is desired. --- src/airport_gui.cpp | 19 ++++++++++--------- src/dock_gui.cpp | 11 ++++++----- src/rail_gui.cpp | 11 ++++++----- src/road_gui.cpp | 11 ++++++----- src/station_gui.cpp | 8 +++----- src/station_gui.h | 3 ++- 6 files changed, 33 insertions(+), 30 deletions(-) diff --git a/src/airport_gui.cpp b/src/airport_gui.cpp index 1927513e36..381939db08 100644 --- a/src/airport_gui.cpp +++ b/src/airport_gui.cpp @@ -422,7 +422,8 @@ public: this->DrawWidgets(); Rect r = this->GetWidget(WID_AP_ACCEPTANCE)->GetCurrentRect(); - int top = r.top; + const int bottom = r.bottom; + r.bottom = INT_MAX; // Allow overflow as we want to know the required height. if (_selected_airport_index != -1) { const AirportSpec *as = AirportClass::Get(_selected_airport_class)->GetSpec(_selected_airport_index); @@ -432,27 +433,27 @@ public: if (_settings_game.economy.station_noise_level) { /* show the noise of the selected airport */ SetDParam(0, as->noise_level); - DrawString(r.left, r.right, top, STR_STATION_BUILD_NOISE); - top += GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal; + DrawString(r, STR_STATION_BUILD_NOISE); + r.top += GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal; } if (_settings_game.economy.infrastructure_maintenance) { Money monthly = _price[PR_INFRASTRUCTURE_AIRPORT] * as->maintenance_cost >> 3; SetDParam(0, monthly * 12); - DrawString(r.left, r.right, top, TimerGameEconomy::UsingWallclockUnits() ? STR_STATION_BUILD_INFRASTRUCTURE_COST_PERIOD : STR_STATION_BUILD_INFRASTRUCTURE_COST_YEAR); - top += GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal; + DrawString(r, TimerGameEconomy::UsingWallclockUnits() ? STR_STATION_BUILD_INFRASTRUCTURE_COST_PERIOD : STR_STATION_BUILD_INFRASTRUCTURE_COST_YEAR); + r.top += GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal; } /* strings such as 'Size' and 'Coverage Area' */ - top = DrawStationCoverageAreaText(r.left, r.right, top, SCT_ALL, rad, false) + WidgetDimensions::scaled.vsep_normal; - top = DrawStationCoverageAreaText(r.left, r.right, top, SCT_ALL, rad, true); + r.top = DrawStationCoverageAreaText(r, SCT_ALL, rad, false) + WidgetDimensions::scaled.vsep_normal; + r.top = DrawStationCoverageAreaText(r, SCT_ALL, rad, true); } /* Resize background if the window is too small. * Never make the window smaller to avoid oscillating if the size change affects the acceptance. * (This is the case, if making the window bigger moves the mouse into the window.) */ - if (top > r.bottom) { - ResizeWindow(this, 0, top - r.bottom, false); + if (r.top > bottom) { + ResizeWindow(this, 0, r.top - bottom, false); } } diff --git a/src/dock_gui.cpp b/src/dock_gui.cpp index ac2c004cea..017be23bba 100644 --- a/src/dock_gui.cpp +++ b/src/dock_gui.cpp @@ -442,14 +442,15 @@ public: /* strings such as 'Size' and 'Coverage Area' */ Rect r = this->GetWidget(BDSW_ACCEPTANCE)->GetCurrentRect(); - int top = r.top; - top = DrawStationCoverageAreaText(r.left, r.right, top, SCT_ALL, rad, false) + WidgetDimensions::scaled.vsep_normal; - top = DrawStationCoverageAreaText(r.left, r.right, top, SCT_ALL, rad, true); + const int bottom = r.bottom; + r.bottom = INT_MAX; // Allow overflow as we want to know the required height. + r.top = DrawStationCoverageAreaText(r, SCT_ALL, rad, false) + WidgetDimensions::scaled.vsep_normal; + r.top = DrawStationCoverageAreaText(r, SCT_ALL, rad, true); /* Resize background if the window is too small. * Never make the window smaller to avoid oscillating if the size change affects the acceptance. * (This is the case, if making the window bigger moves the mouse into the window.) */ - if (top > r.bottom) { - ResizeWindow(this, 0, top - r.bottom, false); + if (r.top > bottom) { + ResizeWindow(this, 0, r.top - bottom, false); } } diff --git a/src/rail_gui.cpp b/src/rail_gui.cpp index b28d0fbf28..37090da1c9 100644 --- a/src/rail_gui.cpp +++ b/src/rail_gui.cpp @@ -1150,14 +1150,15 @@ public: if (this->IsShaded()) return; /* 'Accepts' and 'Supplies' texts. */ Rect r = this->GetWidget(WID_BRAS_COVERAGE_TEXTS)->GetCurrentRect(); - int top = r.top; - top = DrawStationCoverageAreaText(r.left, r.right, top, SCT_ALL, rad, false) + WidgetDimensions::scaled.vsep_normal; - top = DrawStationCoverageAreaText(r.left, r.right, top, SCT_ALL, rad, true); + const int bottom = r.bottom; + r.bottom = INT_MAX; // Allow overflow as we want to know the required height. + r.top = DrawStationCoverageAreaText(r, SCT_ALL, rad, false) + WidgetDimensions::scaled.vsep_normal; + r.top = DrawStationCoverageAreaText(r, SCT_ALL, rad, true); /* Resize background if the window is too small. * Never make the window smaller to avoid oscillating if the size change affects the acceptance. * (This is the case, if making the window bigger moves the mouse into the window.) */ - if (top > r.bottom) { - this->coverage_height += top - r.bottom; + if (r.top > bottom) { + this->coverage_height += r.top - bottom; this->ReInit(); } } diff --git a/src/road_gui.cpp b/src/road_gui.cpp index b51d56b74e..d250ad1921 100644 --- a/src/road_gui.cpp +++ b/src/road_gui.cpp @@ -1361,14 +1361,15 @@ public: /* 'Accepts' and 'Supplies' texts. */ StationCoverageType sct = (this->window_class == WC_BUS_STATION) ? SCT_PASSENGERS_ONLY : SCT_NON_PASSENGERS_ONLY; Rect r = this->GetWidget(WID_BROS_ACCEPTANCE)->GetCurrentRect(); - int top = r.top; - top = DrawStationCoverageAreaText(r.left, r.right, top, sct, rad, false) + WidgetDimensions::scaled.vsep_normal; - top = DrawStationCoverageAreaText(r.left, r.right, top, sct, rad, true); + const int bottom = r.bottom; + r.bottom = INT_MAX; // Allow overflow as we want to know the required height. + r.top = DrawStationCoverageAreaText(r, sct, rad, false) + WidgetDimensions::scaled.vsep_normal; + r.top = DrawStationCoverageAreaText(r, sct, rad, true); /* Resize background if the window is too small. * Never make the window smaller to avoid oscillating if the size change affects the acceptance. * (This is the case, if making the window bigger moves the mouse into the window.) */ - if (top > r.bottom) { - this->coverage_height += top - r.bottom; + if (r.top > bottom) { + this->coverage_height += r.top - bottom; this->ReInit(); } } diff --git a/src/station_gui.cpp b/src/station_gui.cpp index 2e336e86fa..e32a95b41b 100644 --- a/src/station_gui.cpp +++ b/src/station_gui.cpp @@ -66,15 +66,13 @@ using RoadWaypointTypeFilter = GenericWaypointTypeFilter; /** * Calculates and draws the accepted or supplied cargo around the selected tile(s) - * @param left x position where the string is to be drawn - * @param right the right most position to draw on - * @param top y position where the string is to be drawn + * @param r Rect where the string is to be drawn. * @param sct which type of cargo is to be displayed (passengers/non-passengers) * @param rad radius around selected tile(s) to be searched * @param supplies if supplied cargoes should be drawn, else accepted cargoes * @return Returns the y value below the string that was drawn */ -int DrawStationCoverageAreaText(int left, int right, int top, StationCoverageType sct, int rad, bool supplies) +int DrawStationCoverageAreaText(const Rect &r, StationCoverageType sct, int rad, bool supplies) { TileIndex tile = TileVirtXY(_thd.pos.x, _thd.pos.y); CargoTypes cargo_mask = 0; @@ -98,7 +96,7 @@ int DrawStationCoverageAreaText(int left, int right, int top, StationCoverageTyp } } SetDParam(0, cargo_mask); - return DrawStringMultiLine(left, right, top, INT32_MAX, supplies ? STR_STATION_BUILD_SUPPLIES_CARGO : STR_STATION_BUILD_ACCEPTS_CARGO); + return DrawStringMultiLine(r, supplies ? STR_STATION_BUILD_SUPPLIES_CARGO : STR_STATION_BUILD_ACCEPTS_CARGO); } /** diff --git a/src/station_gui.h b/src/station_gui.h index f2ad490684..39a98cc729 100644 --- a/src/station_gui.h +++ b/src/station_gui.h @@ -10,6 +10,7 @@ #ifndef STATION_GUI_H #define STATION_GUI_H +#include "core/geometry_type.hpp" #include "command_type.h" #include "tilearea_type.h" #include "window_type.h" @@ -23,7 +24,7 @@ enum StationCoverageType : uint8_t { SCT_ALL, ///< Draw all cargoes. }; -int DrawStationCoverageAreaText(int left, int right, int top, StationCoverageType sct, int rad, bool supplies); +int DrawStationCoverageAreaText(const Rect &r, StationCoverageType sct, int rad, bool supplies); void CheckRedrawStationCoverage(const Window *w); void CheckRedrawRailWaypointCoverage(const Window *w); void CheckRedrawRoadWaypointCoverage(const Window *w);