From f5f035a22b16dba3b47ba031bbce8ab0c12512b3 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Thu, 29 Sep 2022 00:10:41 +0100 Subject: [PATCH] Codechange: Make use of Rect Width/Height helpers. --- src/aircraft_cmd.cpp | 4 ++-- src/aircraft_gui.cpp | 4 ++-- src/bitmap_type.h | 4 ++-- src/engine_gui.cpp | 2 +- src/error_gui.cpp | 2 +- src/industry_gui.cpp | 4 ++-- src/newgrf_debug_gui.cpp | 6 +++--- src/object_gui.cpp | 4 ++-- src/rail_gui.cpp | 6 +++--- src/roadveh_cmd.cpp | 4 ++-- src/script/api/script_tilelist.cpp | 2 +- src/ship_cmd.cpp | 4 ++-- src/ship_gui.cpp | 4 ++-- src/smallmap_gui.cpp | 4 ++-- src/station.cpp | 4 ++-- src/statusbar_gui.cpp | 2 +- src/train_cmd.cpp | 6 +++--- src/vehicle_gui.cpp | 2 +- src/widget.cpp | 12 ++++++------ 19 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index d67969c006..fc10660801 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -252,8 +252,8 @@ void GetAircraftSpriteSize(EngineID engine, uint &width, uint &height, int &xoff Rect rect; seq.GetBounds(&rect); - width = UnScaleGUI(rect.right - rect.left + 1); - height = UnScaleGUI(rect.bottom - rect.top + 1); + width = UnScaleGUI(rect.Width()); + height = UnScaleGUI(rect.Height()); xoffs = UnScaleGUI(rect.left); yoffs = UnScaleGUI(rect.top); } diff --git a/src/aircraft_gui.cpp b/src/aircraft_gui.cpp index 422f422002..a9e0a68224 100644 --- a/src/aircraft_gui.cpp +++ b/src/aircraft_gui.cpp @@ -87,7 +87,7 @@ void DrawAircraftImage(const Vehicle *v, int left, int right, int y, VehicleID s Rect rect; seq.GetBounds(&rect); - int width = UnScaleGUI(rect.right - rect.left + 1); + int width = UnScaleGUI(rect.Width()); int x_offs = UnScaleGUI(rect.left); int x = rtl ? right - width - x_offs : left - x_offs; bool helicopter = v->subtype == AIR_HELICOPTER; @@ -108,6 +108,6 @@ void DrawAircraftImage(const Vehicle *v, int left, int right, int y, VehicleID s if (v->index == selection) { x += x_offs; y += UnScaleGUI(rect.top) + y_offs - heli_offs; - DrawFrameRect(x - 1, y - 1, x + width + 1, y + UnScaleGUI(rect.bottom - rect.top + 1) + heli_offs + 1, COLOUR_WHITE, FR_BORDERONLY); + DrawFrameRect(x - 1, y - 1, x + width + 1, y + UnScaleGUI(rect.Height()) + heli_offs + 1, COLOUR_WHITE, FR_BORDERONLY); } } diff --git a/src/bitmap_type.h b/src/bitmap_type.h index 99c29bf181..75f9ab6642 100644 --- a/src/bitmap_type.h +++ b/src/bitmap_type.h @@ -58,8 +58,8 @@ public: void Initialize(const Rect &r) { this->tile = TileXY(r.left, r.top); - this->w = r.right - r.left + 1; - this->h = r.bottom - r.top + 1; + this->w = r.Width(); + this->h = r.Height(); this->data.clear(); this->data.resize(Index(w, h)); } diff --git a/src/engine_gui.cpp b/src/engine_gui.cpp index a14bc2652c..ff5a67ab57 100644 --- a/src/engine_gui.cpp +++ b/src/engine_gui.cpp @@ -109,7 +109,7 @@ struct EnginePreviewWindow : Window { EngineID engine = this->window_number; SetDParam(0, GetEngineCategoryName(engine)); - int y = r.top + GetStringHeight(STR_ENGINE_PREVIEW_MESSAGE, r.right - r.left + 1); + int y = r.top + GetStringHeight(STR_ENGINE_PREVIEW_MESSAGE, r.Width()); y = DrawStringMultiLine(r.left, r.right, r.top, y, STR_ENGINE_PREVIEW_MESSAGE, TC_FROMSTRING, SA_CENTER) + WD_PAR_VSEP_WIDE; SetDParam(0, engine); diff --git a/src/error_gui.cpp b/src/error_gui.cpp index d23589683a..366664a5a7 100644 --- a/src/error_gui.cpp +++ b/src/error_gui.cpp @@ -285,7 +285,7 @@ public: DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM, this->summary_msg, TC_FROMSTRING, SA_CENTER); } else { - int extra = (r.bottom - r.top + 1 - this->height_summary - this->height_detailed - WD_PAR_VSEP_WIDE) / 2; + int extra = (r.Height() - this->height_summary - this->height_detailed - WD_PAR_VSEP_WIDE) / 2; /* Note: NewGRF supplied error message often do not start with a colour code, so default to white. */ int top = r.top + WD_FRAMERECT_TOP; diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index cf78f4db90..08c4b48410 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -2921,8 +2921,8 @@ struct IndustryCargoesWindow : public Window { if (widget != WID_IC_PANEL) return; DrawPixelInfo tmp_dpi, *old_dpi; - int width = r.right - r.left + 1; - int height = r.bottom - r.top + 1 - WD_FRAMERECT_TOP - WD_FRAMERECT_BOTTOM; + int width = r.Width(); + int height = r.Height() - WD_FRAMERECT_TOP - WD_FRAMERECT_BOTTOM; if (!FillDrawPixelInfo(&tmp_dpi, r.left + WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, width, height)) return; old_dpi = _cur_dpi; _cur_dpi = &tmp_dpi; diff --git a/src/newgrf_debug_gui.cpp b/src/newgrf_debug_gui.cpp index cad9cf2467..9aa370aaf5 100644 --- a/src/newgrf_debug_gui.cpp +++ b/src/newgrf_debug_gui.cpp @@ -428,7 +428,7 @@ struct NewGRFInspectWindow : Window { if (u == v) sel_end = total_width; } - int width = r.right + 1 - r.left - WD_BEVEL_LEFT - WD_BEVEL_RIGHT; + int width = r.Width() - WD_BEVEL_LEFT - WD_BEVEL_RIGHT; int skip = 0; if (total_width > width) { int sel_center = (sel_start + sel_end) / 2; @@ -878,8 +878,8 @@ struct SpriteAlignerWindow : Window { case WID_SA_SPRITE: { /* Center the sprite ourselves */ const Sprite *spr = GetSprite(this->current_sprite, ST_NORMAL); - int width = r.right - r.left + 1 - WD_BEVEL_LEFT - WD_BEVEL_RIGHT; - int height = r.bottom - r.top + 1 - WD_BEVEL_TOP - WD_BEVEL_BOTTOM; + int width = r.Width() - WD_BEVEL_LEFT - WD_BEVEL_RIGHT; + int height = r.Height() - WD_BEVEL_TOP - WD_BEVEL_BOTTOM; int x = -UnScaleGUI(spr->x_offs) + (width - UnScaleGUI(spr->width) ) / 2; int y = -UnScaleGUI(spr->y_offs) + (height - UnScaleGUI(spr->height)) / 2; diff --git a/src/object_gui.cpp b/src/object_gui.cpp index 53fb47323e..2afd51e348 100644 --- a/src/object_gui.cpp +++ b/src/object_gui.cpp @@ -354,7 +354,7 @@ public: DrawPixelInfo tmp_dpi; /* Set up a clipping area for the preview. */ - if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) { + if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.Width(), r.Height())) { DrawPixelInfo *old_dpi = _cur_dpi; _cur_dpi = &tmp_dpi; if (spec->grf_prop.grffile == nullptr) { @@ -381,7 +381,7 @@ public: } DrawPixelInfo tmp_dpi; /* Set up a clipping area for the preview. */ - if (FillDrawPixelInfo(&tmp_dpi, r.left + 1, r.top, (r.right - 1) - (r.left + 1) + 1, r.bottom - r.top + 1)) { + if (FillDrawPixelInfo(&tmp_dpi, r.left + 1, r.top, (r.right - 1) - (r.left + 1) + 1, r.Height())) { DrawPixelInfo *old_dpi = _cur_dpi; _cur_dpi = &tmp_dpi; if (spec->grf_prop.grffile == nullptr) { diff --git a/src/rail_gui.cpp b/src/rail_gui.cpp index 703a271db8..8c59336811 100644 --- a/src/rail_gui.cpp +++ b/src/rail_gui.cpp @@ -1254,7 +1254,7 @@ public: switch (GB(widget, 0, 16)) { case WID_BRAS_PLATFORM_DIR_X: /* Set up a clipping area for the '/' station preview */ - if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) { + if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.Width(), r.Height())) { DrawPixelInfo *old_dpi = _cur_dpi; _cur_dpi = &tmp_dpi; int x = ScaleGUITrad(31) + 1; @@ -1268,7 +1268,7 @@ public: case WID_BRAS_PLATFORM_DIR_Y: /* Set up a clipping area for the '\' station preview */ - if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) { + if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.Width(), r.Height())) { DrawPixelInfo *old_dpi = _cur_dpi; _cur_dpi = &tmp_dpi; int x = ScaleGUITrad(31) + 1; @@ -1305,7 +1305,7 @@ public: } /* Set up a clipping area for the station preview. */ - if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) { + if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.Width(), r.Height())) { DrawPixelInfo *old_dpi = _cur_dpi; _cur_dpi = &tmp_dpi; int x = ScaleGUITrad(31) + 1; diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index ae09fda403..86271283ce 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -175,8 +175,8 @@ void GetRoadVehSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs Rect rect; seq.GetBounds(&rect); - width = UnScaleGUI(rect.right - rect.left + 1); - height = UnScaleGUI(rect.bottom - rect.top + 1); + width = UnScaleGUI(rect.Width()); + height = UnScaleGUI(rect.Height()); xoffs = UnScaleGUI(rect.left); yoffs = UnScaleGUI(rect.top); } diff --git a/src/script/api/script_tilelist.cpp b/src/script/api/script_tilelist.cpp index 8266f46ff9..2050644ff5 100644 --- a/src/script/api/script_tilelist.cpp +++ b/src/script/api/script_tilelist.cpp @@ -155,7 +155,7 @@ ScriptTileList_StationType::ScriptTileList_StationType(StationID station_id, Scr if ((station_type & ScriptStation::STATION_AIRPORT) != 0) station_type_value |= (1 << ::STATION_AIRPORT) | (1 << ::STATION_OILRIG); if ((station_type & ScriptStation::STATION_DOCK) != 0) station_type_value |= (1 << ::STATION_DOCK) | (1 << ::STATION_OILRIG); - TileArea ta(::TileXY(rect->left, rect->top), rect->right - rect->left + 1, rect->bottom - rect->top + 1); + TileArea ta(::TileXY(rect->left, rect->top), rect->Width(), rect->Height()); for (TileIndex cur_tile : ta) { if (!::IsTileType(cur_tile, MP_STATION)) continue; if (::GetStationIndex(cur_tile) != station_id) continue; diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index ec90258b69..e1b05835bb 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -119,8 +119,8 @@ void GetShipSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, i Rect rect; seq.GetBounds(&rect); - width = UnScaleGUI(rect.right - rect.left + 1); - height = UnScaleGUI(rect.bottom - rect.top + 1); + width = UnScaleGUI(rect.Width()); + height = UnScaleGUI(rect.Height()); xoffs = UnScaleGUI(rect.left); yoffs = UnScaleGUI(rect.top); } diff --git a/src/ship_gui.cpp b/src/ship_gui.cpp index aff7077f07..7525eb0da5 100644 --- a/src/ship_gui.cpp +++ b/src/ship_gui.cpp @@ -39,7 +39,7 @@ void DrawShipImage(const Vehicle *v, int left, int right, int y, VehicleID selec Rect rect; seq.GetBounds(&rect); - int width = UnScaleGUI(rect.right - rect.left + 1); + int width = UnScaleGUI(rect.Width()); int x_offs = UnScaleGUI(rect.left); int x = rtl ? right - width - x_offs : left - x_offs; @@ -49,7 +49,7 @@ void DrawShipImage(const Vehicle *v, int left, int right, int y, VehicleID selec if (v->index == selection) { x += x_offs; y += UnScaleGUI(rect.top); - DrawFrameRect(x - 1, y - 1, x + width + 1, y + UnScaleGUI(rect.bottom - rect.top + 1) + 1, COLOUR_WHITE, FR_BORDERONLY); + DrawFrameRect(x - 1, y - 1, x + width + 1, y + UnScaleGUI(rect.Height()) + 1, COLOUR_WHITE, FR_BORDERONLY); } } diff --git a/src/smallmap_gui.cpp b/src/smallmap_gui.cpp index e5c6a3bf8a..b8550a4e18 100644 --- a/src/smallmap_gui.cpp +++ b/src/smallmap_gui.cpp @@ -1215,13 +1215,13 @@ void SmallMapWindow::RebuildColourIndexIfNecessary() switch (widget) { case WID_SM_MAP: { DrawPixelInfo new_dpi; - if (!FillDrawPixelInfo(&new_dpi, r.left + 1, r.top + 1, r.right - r.left - 1, r.bottom - r.top - 1)) return; + if (!FillDrawPixelInfo(&new_dpi, r.left + 1, r.top + 1, r.Width(), r.Height())) return; this->DrawSmallMap(&new_dpi); break; } case WID_SM_LEGEND: { - uint columns = this->GetNumberColumnsLegend(r.right - r.left + 1); + uint columns = this->GetNumberColumnsLegend(r.Width()); uint number_of_rows = this->GetNumberRowsLegend(columns); bool rtl = _current_text_dir == TD_RTL; uint y_org = r.top + WD_FRAMERECT_TOP; diff --git a/src/station.cpp b/src/station.cpp index b73f566561..9c96b9b002 100644 --- a/src/station.cpp +++ b/src/station.cpp @@ -549,8 +549,8 @@ CommandCost StationRect::BeforeAddTile(TileIndex tile, StationRectMode mode) Rect new_rect = {std::min(x, this->left), std::min(y, this->top), std::max(x, this->right), std::max(y, this->bottom)}; /* check new rect dimensions against preset max */ - int w = new_rect.right - new_rect.left + 1; - int h = new_rect.bottom - new_rect.top + 1; + int w = new_rect.Width(); + int h = new_rect.Height(); if (mode != ADD_FORCE && (w > _settings_game.station.station_spread || h > _settings_game.station.station_spread)) { assert(mode != ADD_TRY); return_cmd_error(STR_ERROR_STATION_TOO_SPREAD_OUT); diff --git a/src/statusbar_gui.cpp b/src/statusbar_gui.cpp index 7567c351d7..b2500e609a 100644 --- a/src/statusbar_gui.cpp +++ b/src/statusbar_gui.cpp @@ -137,7 +137,7 @@ struct StatusBarWindow : Window { void DrawWidget(const Rect &r, int widget) const override { - int text_offset = std::max(0, ((int)(r.bottom - r.top + 1) - FONT_HEIGHT_NORMAL) / 2); // Offset for rendering the text vertically centered + int text_offset = std::max(0, (r.Height() - FONT_HEIGHT_NORMAL) / 2); // Offset for rendering the text vertically centered int text_top = r.top + text_offset; switch (widget) { case WID_S_LEFT: diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 7d3a1fb361..3659af7bad 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -566,8 +566,8 @@ void GetTrainSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, Rect rect; seq.GetBounds(&rect); - width = UnScaleGUI(rect.right - rect.left + 1); - height = UnScaleGUI(rect.bottom - rect.top + 1); + width = UnScaleGUI(rect.Width()); + height = UnScaleGUI(rect.Height()); xoffs = UnScaleGUI(rect.left); yoffs = UnScaleGUI(rect.top); @@ -577,7 +577,7 @@ void GetTrainSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, /* Calculate values relative to an imaginary center between the two sprites. */ width = ScaleGUITrad(TRAININFO_DEFAULT_VEHICLE_WIDTH) + UnScaleGUI(rect.right) - xoffs; - height = std::max(height, UnScaleGUI(rect.bottom - rect.top + 1)); + height = std::max(height, UnScaleGUI(rect.Height())); xoffs = xoffs - ScaleGUITrad(TRAININFO_DEFAULT_VEHICLE_WIDTH) / 2; yoffs = std::min(yoffs, UnScaleGUI(rect.top)); } diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 02fc085eeb..d0bb7910e9 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -3316,7 +3316,7 @@ int GetSingleVehicleWidth(const Vehicle *v, EngineImageType image_type) v->GetImage(rtl ? DIR_E : DIR_W, image_type, &seq); Rect rec; seq.GetBounds(&rec); - return UnScaleGUI(rec.right - rec.left + 1); + return UnScaleGUI(rec.Width()); } } diff --git a/src/widget.cpp b/src/widget.cpp index 0a47659611..10ae2f239c 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -322,18 +322,18 @@ static inline void DrawMatrix(const Rect &r, Colours colour, bool clicked, uint1 int column_width; // Width of a single column in the matrix. if (num_columns == 0) { column_width = resize_x; - num_columns = (r.right - r.left + 1) / column_width; + num_columns = r.Width() / column_width; } else { - column_width = (r.right - r.left + 1) / num_columns; + column_width = r.Width() / num_columns; } int num_rows = GB(data, MAT_ROW_START, MAT_ROW_BITS); // Upper 8 bits of the widget data: Number of rows in the matrix. int row_height; // Height of a single row in the matrix. if (num_rows == 0) { row_height = resize_y; - num_rows = (r.bottom - r.top + 1) / row_height; + num_rows = r.Height() / row_height; } else { - row_height = (r.bottom - r.top + 1) / num_rows; + row_height = r.Height() / num_rows; } int col = _colour_gradient[colour & 0xF][6]; @@ -611,11 +611,11 @@ void DrawCaption(const Rect &r, Colours colour, Owner owner, TextColour text_col */ static inline void DrawButtonDropdown(const Rect &r, Colours colour, bool clicked_button, bool clicked_dropdown, StringID str, StringAlignment align) { - int text_offset = std::max(0, ((int)(r.bottom - r.top + 1) - FONT_HEIGHT_NORMAL) / 2); // Offset for rendering the text vertically centered + int text_offset = std::max(0, (r.Height() - FONT_HEIGHT_NORMAL) / 2); // Offset for rendering the text vertically centered int dd_width = NWidgetLeaf::dropdown_dimension.width; int dd_height = NWidgetLeaf::dropdown_dimension.height; - int image_offset = std::max(0, ((int)(r.bottom - r.top + 1) - dd_height) / 2); + int image_offset = std::max(0, (r.Height() - dd_height) / 2); if (_current_text_dir == TD_LTR) { DrawFrameRect(r.left, r.top, r.right - dd_width, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);