From ab55d3e1fa5fc898c5a4dfab30cffc5a4f7fb09e Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sun, 9 Oct 2022 17:42:47 +0100 Subject: [PATCH 1/9] Change: Rearrange layouts of station/depot picker windows. --- src/dock_gui.cpp | 15 +++++------ src/road_gui.cpp | 68 +++++++++++++++++++++++------------------------- 2 files changed, 38 insertions(+), 45 deletions(-) diff --git a/src/dock_gui.cpp b/src/dock_gui.cpp index f5149b8b07..87a49969ba 100644 --- a/src/dock_gui.cpp +++ b/src/dock_gui.cpp @@ -574,17 +574,14 @@ static const NWidgetPart _nested_build_docks_depot_widgets[] = { NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_DEPOT_BUILD_SHIP_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS), EndContainer(), NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BDD_BACKGROUND), - NWidget(NWID_SPACER), SetMinimalSize(0, 3), - NWidget(NWID_HORIZONTAL_LTR), - NWidget(NWID_SPACER), SetMinimalSize(3, 0), - NWidget(WWT_PANEL, COLOUR_GREY, WID_BDD_X), SetMinimalSize(98, 66), SetDataTip(0x0, STR_DEPOT_BUILD_SHIP_ORIENTATION_TOOLTIP), + NWidget(NWID_HORIZONTAL), SetPadding(3), + NWidget(NWID_SPACER), SetFill(1, 0), + NWidget(NWID_HORIZONTAL_LTR), SetPIP(0, 2, 0), + NWidget(WWT_PANEL, COLOUR_GREY, WID_BDD_X), SetMinimalSize(98, 66), SetDataTip(0x0, STR_DEPOT_BUILD_SHIP_ORIENTATION_TOOLTIP), EndContainer(), + NWidget(WWT_PANEL, COLOUR_GREY, WID_BDD_Y), SetMinimalSize(98, 66), SetDataTip(0x0, STR_DEPOT_BUILD_SHIP_ORIENTATION_TOOLTIP), EndContainer(), EndContainer(), - NWidget(NWID_SPACER), SetMinimalSize(2, 0), - NWidget(WWT_PANEL, COLOUR_GREY, WID_BDD_Y), SetMinimalSize(98, 66), SetDataTip(0x0, STR_DEPOT_BUILD_SHIP_ORIENTATION_TOOLTIP), - EndContainer(), - NWidget(NWID_SPACER), SetMinimalSize(3, 0), + NWidget(NWID_SPACER), SetFill(1, 0), EndContainer(), - NWidget(NWID_SPACER), SetMinimalSize(0, 3), EndContainer(), }; diff --git a/src/road_gui.cpp b/src/road_gui.cpp index cb49688d5d..86af0fb8c6 100644 --- a/src/road_gui.cpp +++ b/src/road_gui.cpp @@ -1011,27 +1011,20 @@ static const NWidgetPart _nested_build_road_depot_widgets[] = { NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_BROD_CAPTION), SetDataTip(STR_BUILD_DEPOT_ROAD_ORIENTATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS), EndContainer(), NWidget(WWT_PANEL, COLOUR_DARK_GREEN), - NWidget(NWID_SPACER), SetMinimalSize(0, 3), - NWidget(NWID_HORIZONTAL_LTR), - NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0), - NWidget(NWID_VERTICAL), - NWidget(WWT_PANEL, COLOUR_GREY, WID_BROD_DEPOT_NW), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_ROAD_ORIENTATION_SELECT_TOOLTIP), + NWidget(NWID_HORIZONTAL), SetPadding(3), + NWidget(NWID_SPACER), SetFill(1, 0), + NWidget(NWID_VERTICAL), SetPIP(0, 2, 0), + NWidget(NWID_HORIZONTAL_LTR), SetPIP(0, 2, 0), + NWidget(WWT_PANEL, COLOUR_GREY, WID_BROD_DEPOT_NW), SetMinimalSize(66, 50), SetFill(0, 0), SetDataTip(0x0, STR_BUILD_DEPOT_ROAD_ORIENTATION_SELECT_TOOLTIP), EndContainer(), + NWidget(WWT_PANEL, COLOUR_GREY, WID_BROD_DEPOT_NE), SetMinimalSize(66, 50), SetFill(0, 0), SetDataTip(0x0, STR_BUILD_DEPOT_ROAD_ORIENTATION_SELECT_TOOLTIP), EndContainer(), EndContainer(), - NWidget(NWID_SPACER), SetMinimalSize(0, 2), - NWidget(WWT_PANEL, COLOUR_GREY, WID_BROD_DEPOT_SW), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_ROAD_ORIENTATION_SELECT_TOOLTIP), + NWidget(NWID_HORIZONTAL_LTR), SetPIP(0, 2, 0), + NWidget(WWT_PANEL, COLOUR_GREY, WID_BROD_DEPOT_SW), SetMinimalSize(66, 50), SetFill(0, 0), SetDataTip(0x0, STR_BUILD_DEPOT_ROAD_ORIENTATION_SELECT_TOOLTIP), EndContainer(), + NWidget(WWT_PANEL, COLOUR_GREY, WID_BROD_DEPOT_SE), SetMinimalSize(66, 50), SetFill(0, 0), SetDataTip(0x0, STR_BUILD_DEPOT_ROAD_ORIENTATION_SELECT_TOOLTIP), EndContainer(), EndContainer(), EndContainer(), - NWidget(NWID_SPACER), SetMinimalSize(2, 0), - NWidget(NWID_VERTICAL), - NWidget(WWT_PANEL, COLOUR_GREY, WID_BROD_DEPOT_NE), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_ROAD_ORIENTATION_SELECT_TOOLTIP), - EndContainer(), - NWidget(NWID_SPACER), SetMinimalSize(0, 2), - NWidget(WWT_PANEL, COLOUR_GREY, WID_BROD_DEPOT_SE), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_ROAD_ORIENTATION_SELECT_TOOLTIP), - EndContainer(), - EndContainer(), - NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0), + NWidget(NWID_SPACER), SetFill(1, 0), EndContainer(), - NWidget(NWID_SPACER), SetMinimalSize(0, 3), EndContainer(), }; @@ -1163,24 +1156,24 @@ static const NWidgetPart _nested_road_station_picker_widgets[] = { NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_BROS_CAPTION), EndContainer(), NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BROS_BACKGROUND), - NWidget(NWID_SPACER), SetMinimalSize(0, 3), - NWidget(NWID_HORIZONTAL), SetPIP(0, 2, 0), + NWidget(NWID_HORIZONTAL), SetPadding(3), NWidget(NWID_SPACER), SetFill(1, 0), - NWidget(WWT_PANEL, COLOUR_GREY, WID_BROS_STATION_NW), SetMinimalSize(66, 50), SetFill(0, 0), EndContainer(), - NWidget(WWT_PANEL, COLOUR_GREY, WID_BROS_STATION_NE), SetMinimalSize(66, 50), SetFill(0, 0), EndContainer(), - NWidget(WWT_PANEL, COLOUR_GREY, WID_BROS_STATION_X), SetMinimalSize(66, 50), SetFill(0, 0), EndContainer(), - NWidget(NWID_SPACER), SetFill(1, 0), - EndContainer(), - NWidget(NWID_SPACER), SetMinimalSize(0, 2), - NWidget(NWID_HORIZONTAL), SetPIP(0, 2, 0), - NWidget(NWID_SPACER), SetFill(1, 0), - NWidget(WWT_PANEL, COLOUR_GREY, WID_BROS_STATION_SW), SetMinimalSize(66, 50), SetFill(0, 0), EndContainer(), - NWidget(WWT_PANEL, COLOUR_GREY, WID_BROS_STATION_SE), SetMinimalSize(66, 50), SetFill(0, 0), EndContainer(), - NWidget(WWT_PANEL, COLOUR_GREY, WID_BROS_STATION_Y), SetMinimalSize(66, 50), SetFill(0, 0), EndContainer(), + NWidget(NWID_VERTICAL), SetPIP(0, 2, 0), + NWidget(NWID_HORIZONTAL), SetPIP(0, 2, 0), + NWidget(WWT_PANEL, COLOUR_GREY, WID_BROS_STATION_NW), SetMinimalSize(66, 50), SetFill(0, 0), EndContainer(), + NWidget(WWT_PANEL, COLOUR_GREY, WID_BROS_STATION_NE), SetMinimalSize(66, 50), SetFill(0, 0), EndContainer(), + NWidget(WWT_PANEL, COLOUR_GREY, WID_BROS_STATION_X), SetMinimalSize(66, 50), SetFill(0, 0), EndContainer(), + EndContainer(), + NWidget(NWID_HORIZONTAL), SetPIP(0, 2, 0), + NWidget(WWT_PANEL, COLOUR_GREY, WID_BROS_STATION_SW), SetMinimalSize(66, 50), SetFill(0, 0), EndContainer(), + NWidget(WWT_PANEL, COLOUR_GREY, WID_BROS_STATION_SE), SetMinimalSize(66, 50), SetFill(0, 0), EndContainer(), + NWidget(WWT_PANEL, COLOUR_GREY, WID_BROS_STATION_Y), SetMinimalSize(66, 50), SetFill(0, 0), EndContainer(), + EndContainer(), + EndContainer(), NWidget(NWID_SPACER), SetFill(1, 0), EndContainer(), NWidget(WWT_LABEL, COLOUR_DARK_GREEN, WID_BROS_INFO), SetPadding(WD_FRAMERECT_TOP, WD_FRAMERECT_RIGHT, WD_FRAMERECT_BOTTOM, WD_FRAMERECT_LEFT), SetDataTip(STR_STATION_BUILD_COVERAGE_AREA_TITLE, STR_NULL), SetFill(1, 0), - NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2), + NWidget(NWID_HORIZONTAL), SetPadding(3), NWidget(NWID_SPACER), SetFill(1, 0), NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BROS_LT_OFF), SetMinimalSize(60, 12), SetDataTip(STR_STATION_BUILD_COVERAGE_OFF, STR_STATION_BUILD_COVERAGE_AREA_OFF_TOOLTIP), @@ -1206,15 +1199,18 @@ static const NWidgetPart _nested_tram_station_picker_widgets[] = { NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_BROS_CAPTION), EndContainer(), NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BROS_BACKGROUND), - NWidget(NWID_SPACER), SetMinimalSize(0, 3), - NWidget(NWID_HORIZONTAL), SetPIP(0, 2, 0), + NWidget(NWID_HORIZONTAL), SetPadding(3), NWidget(NWID_SPACER), SetFill(1, 0), - NWidget(WWT_PANEL, COLOUR_GREY, WID_BROS_STATION_X), SetMinimalSize(66, 50), SetFill(0, 0), EndContainer(), - NWidget(WWT_PANEL, COLOUR_GREY, WID_BROS_STATION_Y), SetMinimalSize(66, 50), SetFill(0, 0), EndContainer(), + NWidget(NWID_VERTICAL), SetPIP(0, 2, 0), + NWidget(NWID_HORIZONTAL), SetPIP(0, 2, 0), + NWidget(WWT_PANEL, COLOUR_GREY, WID_BROS_STATION_X), SetMinimalSize(66, 50), SetFill(0, 0), EndContainer(), + NWidget(WWT_PANEL, COLOUR_GREY, WID_BROS_STATION_Y), SetMinimalSize(66, 50), SetFill(0, 0), EndContainer(), + EndContainer(), + EndContainer(), NWidget(NWID_SPACER), SetFill(1, 0), EndContainer(), NWidget(WWT_LABEL, COLOUR_DARK_GREEN, WID_BROS_INFO), SetPadding(WD_FRAMERECT_TOP, WD_FRAMERECT_RIGHT, WD_FRAMERECT_BOTTOM, WD_FRAMERECT_LEFT), SetMinimalSize(140, 14), SetDataTip(STR_STATION_BUILD_COVERAGE_AREA_TITLE, STR_NULL), SetFill(1, 0), - NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2), + NWidget(NWID_HORIZONTAL), SetPadding(3), NWidget(NWID_SPACER), SetFill(1, 0), NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BROS_LT_OFF), SetMinimalSize(60, 12), SetDataTip(STR_STATION_BUILD_COVERAGE_OFF, STR_STATION_BUILD_COVERAGE_AREA_OFF_TOOLTIP), From 18201dfbc329c09d1bed9a296422f3448b82bbde Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 30 Aug 2022 08:57:19 +0100 Subject: [PATCH 2/9] Cleanup: Simplify CenterBounds() --- src/gfx_func.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gfx_func.h b/src/gfx_func.h index 4ebb85aed2..c5bf12914b 100644 --- a/src/gfx_func.h +++ b/src/gfx_func.h @@ -139,7 +139,7 @@ bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int heigh */ static inline int CenterBounds(int min, int max, int size) { - return min + (max - min - size + 1) / 2; + return (min + max - size + 1) / 2; } /* window.cpp */ From eb4ba1991b9911101329f0730cb6688670d7d9a7 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Wed, 7 Sep 2022 08:31:02 +0100 Subject: [PATCH 3/9] Change: Use CenterBounds function in more places. CenterBounds may have a 1 pixel difference so the result is not exactly the same. --- src/airport_gui.cpp | 2 +- src/company_gui.cpp | 2 +- src/graph_gui.cpp | 2 +- src/linkgraph/linkgraph_gui.cpp | 6 +++--- src/misc_gui.cpp | 2 +- src/newgrf_debug_gui.cpp | 2 +- src/newgrf_gui.cpp | 2 +- src/news_gui.cpp | 2 +- src/rail_gui.cpp | 12 +++++------- src/smallmap_gui.cpp | 4 ++-- src/station_gui.cpp | 2 +- src/statusbar_gui.cpp | 2 +- src/tree_gui.cpp | 2 +- src/vehicle_gui.cpp | 7 +++---- 14 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/airport_gui.cpp b/src/airport_gui.cpp index f8ef410d85..665f778161 100644 --- a/src/airport_gui.cpp +++ b/src/airport_gui.cpp @@ -404,7 +404,7 @@ public: case WID_AP_AIRPORT_SPRITE: if (this->preview_sprite != 0) { Dimension d = GetSpriteSize(this->preview_sprite); - DrawSprite(this->preview_sprite, COMPANY_SPRITE_COLOUR(_local_company), (r.left + r.right - d.width) / 2, (r.top + r.bottom - d.height) / 2); + DrawSprite(this->preview_sprite, COMPANY_SPRITE_COLOUR(_local_company), CenterBounds(r.left, r.right, d.width), CenterBounds(r.top, r.bottom, d.height)); } break; diff --git a/src/company_gui.cpp b/src/company_gui.cpp index fdcc7309df..2a9b06a6b0 100644 --- a/src/company_gui.cpp +++ b/src/company_gui.cpp @@ -2466,7 +2466,7 @@ struct CompanyWindow : Window Point offset; Dimension d = GetSpriteSize(SPR_VEH_BUS_SW_VIEW, &offset); d.height -= offset.y; - DrawSprite(SPR_VEH_BUS_SW_VIEW, COMPANY_SPRITE_COLOUR(c->index), r.left - offset.x, (r.top + r.bottom - d.height) / 2 - offset.y); + DrawSprite(SPR_VEH_BUS_SW_VIEW, COMPANY_SPRITE_COLOUR(c->index), r.left - offset.x, CenterBounds(r.top, r.bottom, d.height) - offset.y); break; } diff --git a/src/graph_gui.cpp b/src/graph_gui.cpp index e5e09aa66b..cb4e793077 100644 --- a/src/graph_gui.cpp +++ b/src/graph_gui.cpp @@ -1389,7 +1389,7 @@ struct PerformanceRatingDetailWindow : Window { CompanyID cid = (CompanyID)(widget - WID_PRD_COMPANY_FIRST); int offset = (cid == this->company) ? 1 : 0; Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON); - DrawCompanyIcon(cid, (r.left + r.right - sprite_size.width) / 2 + offset, (r.top + r.bottom - sprite_size.height) / 2 + offset); + DrawCompanyIcon(cid, CenterBounds(r.left, r.right, sprite_size.width) + offset, CenterBounds(r.top, r.bottom, sprite_size.height) + offset); return; } diff --git a/src/linkgraph/linkgraph_gui.cpp b/src/linkgraph/linkgraph_gui.cpp index a5366b55e3..1eff55e65e 100644 --- a/src/linkgraph/linkgraph_gui.cpp +++ b/src/linkgraph/linkgraph_gui.cpp @@ -614,7 +614,7 @@ void LinkGraphLegendWindow::DrawWidget(const Rect &r, int widget) const if (this->IsWidgetDisabled(widget)) return; CompanyID cid = (CompanyID)(widget - WID_LGL_COMPANY_FIRST); Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON); - DrawCompanyIcon(cid, (r.left + r.right + 1 - sprite_size.width) / 2, (r.top + r.bottom + 1 - sprite_size.height) / 2); + DrawCompanyIcon(cid, CenterBounds(r.left, r.right, sprite_size.width), CenterBounds(r.top, r.bottom, sprite_size.height)); } if (IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) { uint8 colour = LinkGraphOverlay::LINK_COLOURS[_settings_client.gui.linkgraph_colours][widget - WID_LGL_SATURATION_FIRST]; @@ -628,14 +628,14 @@ void LinkGraphLegendWindow::DrawWidget(const Rect &r, int widget) const str = STR_LINKGRAPH_LEGEND_SATURATED; } if (str != STR_NULL) { - DrawString(r.left, r.right, (r.top + r.bottom + 1 - FONT_HEIGHT_SMALL) / 2, str, GetContrastColour(colour) | TC_FORCED, SA_HOR_CENTER); + DrawString(r.left, r.right, CenterBounds(r.top, r.bottom, FONT_HEIGHT_SMALL), str, GetContrastColour(colour) | TC_FORCED, SA_HOR_CENTER); } } if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) { if (this->IsWidgetDisabled(widget)) return; CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST); GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, cargo->legend_colour); - DrawString(r.left, r.right, (r.top + r.bottom + 1 - FONT_HEIGHT_SMALL) / 2, cargo->abbrev, GetContrastColour(cargo->legend_colour, 73), SA_HOR_CENTER); + DrawString(r.left, r.right, CenterBounds(r.top, r.bottom, FONT_HEIGHT_SMALL), cargo->abbrev, GetContrastColour(cargo->legend_colour, 73), SA_HOR_CENTER); } } diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp index 1a3ba2b05c..5017f885b5 100644 --- a/src/misc_gui.cpp +++ b/src/misc_gui.cpp @@ -809,7 +809,7 @@ void QueryString::DrawEditBox(const Window *w, int wid) const int bottom = wi->pos_y + wi->current_y - 1; DrawFrameRect(clearbtn_left, top, clearbtn_right, bottom, wi->colour, wi->IsLowered() ? FR_LOWERED : FR_NONE); - DrawSprite(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT, PAL_NONE, clearbtn_left + WD_IMGBTN_LEFT + (wi->IsLowered() ? 1 : 0), (top + bottom - sprite_size.height) / 2 + (wi->IsLowered() ? 1 : 0)); + DrawSprite(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT, PAL_NONE, clearbtn_left + WD_IMGBTN_LEFT + (wi->IsLowered() ? 1 : 0), CenterBounds(top, bottom, sprite_size.height) + (wi->IsLowered() ? 1 : 0)); if (this->text.bytes == 1) GfxFillRect(clearbtn_left + 1, top + 1, clearbtn_right - 1, bottom - 1, _colour_gradient[wi->colour & 0xF][2], FILLRECT_CHECKER); DrawFrameRect(left, top, right, bottom, wi->colour, FR_LOWERED | FR_DARKENED); diff --git a/src/newgrf_debug_gui.cpp b/src/newgrf_debug_gui.cpp index ec221e2f33..cad9cf2467 100644 --- a/src/newgrf_debug_gui.cpp +++ b/src/newgrf_debug_gui.cpp @@ -437,7 +437,7 @@ struct NewGRFInspectWindow : Window { GrfSpecFeature f = GetFeatureNum(this->window_number); int h = GetVehicleImageCellSize((VehicleType)(VEH_TRAIN + (f - GSF_TRAINS)), EIT_IN_DEPOT).height; - int y = (r.top + r.bottom - h) / 2; + int y = CenterBounds(r.top, r.bottom, h); DrawVehicleImage(v->First(), r.left + WD_BEVEL_LEFT, r.right - WD_BEVEL_RIGHT, y + 1, INVALID_VEHICLE, EIT_IN_DETAILS, skip); /* Highlight the articulated part (this is different to the whole-vehicle highlighting of DrawVehicleImage */ diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 1b7ab44f6c..687c5e95b5 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -904,7 +904,7 @@ struct NewGRFWindow : public Window, NewGRFScanCallback { case WID_NS_NEWGRF_INFO_TITLE: /* Create the nice grayish rectangle at the details top. */ GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_DARK_BLUE); - DrawString(r.left, r.right, (r.top + r.bottom - FONT_HEIGHT_NORMAL) / 2, STR_NEWGRF_SETTINGS_INFO_TITLE, TC_FROMSTRING, SA_HOR_CENTER); + DrawString(r.left, r.right, CenterBounds(r.top, r.bottom, FONT_HEIGHT_NORMAL), STR_NEWGRF_SETTINGS_INFO_TITLE, TC_FROMSTRING, SA_HOR_CENTER); break; case WID_NS_NEWGRF_INFO: { diff --git a/src/news_gui.cpp b/src/news_gui.cpp index 1a12dad803..57b745b211 100644 --- a/src/news_gui.cpp +++ b/src/news_gui.cpp @@ -466,7 +466,7 @@ struct NewsWindow : Window { case WID_N_VEH_SPR: { assert(this->ni->reftype1 == NR_ENGINE); EngineID engine = this->ni->ref1; - DrawVehicleEngine(r.left, r.right, (r.left + r.right) / 2, (r.top + r.bottom) / 2, engine, GetEnginePalette(engine, _local_company), EIT_PREVIEW); + DrawVehicleEngine(r.left, r.right, CenterBounds(r.left, r.right, 0), CenterBounds(r.top, r.bottom, 0), engine, GetEnginePalette(engine, _local_company), EIT_PREVIEW); GfxFillRect(r.left, r.top, r.right, r.bottom, PALETTE_NEWSPAPER, FILLRECT_RECOLOUR); break; } diff --git a/src/rail_gui.cpp b/src/rail_gui.cpp index 8089c939cb..703a271db8 100644 --- a/src/rail_gui.cpp +++ b/src/rail_gui.cpp @@ -1673,15 +1673,13 @@ private: * @param widget_index index of this widget in the window * @param image the sprite to draw */ - void DrawSignalSprite(byte widget_index, SpriteID image) const + void DrawSignalSprite(const Rect &r, int widget_index, SpriteID image) const { Point offset; Dimension sprite_size = GetSpriteSize(image, &offset); - const NWidgetBase *widget = this->GetWidget(widget_index); - int x = widget->pos_x - offset.x + - (widget->current_x - sprite_size.width + offset.x) / 2; // centered - int y = widget->pos_y - sig_sprite_bottom_offset + WD_IMGBTN_TOP + - (widget->current_y - WD_IMGBTN_TOP - WD_IMGBTN_BOTTOM + sig_sprite_size.height) / 2; // aligned to bottom + int x = CenterBounds(r.left, r.right, sprite_size.width - offset.x); + int y = r.top - sig_sprite_bottom_offset + WD_IMGBTN_TOP + + (r.bottom - r.top - WD_IMGBTN_TOP - WD_IMGBTN_BOTTOM + sig_sprite_size.height) / 2; // aligned to bottom DrawSprite(image, PAL_NONE, x + this->IsWidgetLowered(widget_index), @@ -1768,7 +1766,7 @@ public: int var = SIG_SEMAPHORE - (widget - WID_BS_SEMAPHORE_NORM) / SIGTYPE_END; // SignalVariant order is reversed compared to the widgets. SpriteID sprite = GetRailTypeInfo(_cur_railtype)->gui_sprites.signals[type][var][this->IsWidgetLowered(widget)]; - this->DrawSignalSprite(widget, sprite); + this->DrawSignalSprite(r, widget, sprite); } } diff --git a/src/smallmap_gui.cpp b/src/smallmap_gui.cpp index e0045efb0a..e5c6a3bf8a 100644 --- a/src/smallmap_gui.cpp +++ b/src/smallmap_gui.cpp @@ -1683,8 +1683,8 @@ void SmallMapWindow::SmallMapCenterOnCurrentPos() */ Point SmallMapWindow::GetStationMiddle(const Station *st) const { - int x = (st->rect.right + st->rect.left + 1) / 2; - int y = (st->rect.bottom + st->rect.top + 1) / 2; + int x = CenterBounds(st->rect.left, st->rect.right, 0); + int y = CenterBounds(st->rect.top, st->rect.bottom, 0); Point ret = this->RemapTile(x, y); /* Same magic 3 as in DrawVehicles; that's where I got it from. diff --git a/src/station_gui.cpp b/src/station_gui.cpp index 965b7e04f2..17f8a43c97 100644 --- a/src/station_gui.cpp +++ b/src/station_gui.cpp @@ -488,7 +488,7 @@ public: int cg_ofst = HasBit(this->cargo_filter, cs->Index()) ? 1 : 0; GfxFillRect(r.left + cg_ofst + 1, r.top + cg_ofst + 1, r.right - 1 + cg_ofst, r.bottom - 1 + cg_ofst, cs->rating_colour); TextColour tc = GetContrastColour(cs->rating_colour); - DrawString(r.left + cg_ofst, r.right + cg_ofst, r.top + (r.bottom - r.top - FONT_HEIGHT_SMALL) / 2 + cg_ofst, cs->abbrev, tc, SA_HOR_CENTER); + DrawString(r.left + cg_ofst, r.right + cg_ofst, CenterBounds(r.top, r.bottom, FONT_HEIGHT_SMALL) + cg_ofst, cs->abbrev, tc, SA_HOR_CENTER); } break; } diff --git a/src/statusbar_gui.cpp b/src/statusbar_gui.cpp index 9031ccf303..7567c351d7 100644 --- a/src/statusbar_gui.cpp +++ b/src/statusbar_gui.cpp @@ -189,7 +189,7 @@ struct StatusBarWindow : Window { if (!this->reminder_timeout.HasElapsed()) { Dimension icon_size = GetSpriteSize(SPR_UNREAD_NEWS); - DrawSprite(SPR_UNREAD_NEWS, PAL_NONE, r.right - WD_FRAMERECT_RIGHT - icon_size.width, r.top + std::max(0, ((int)(r.bottom - r.top + 1) - (int)icon_size.height) / 2)); + DrawSprite(SPR_UNREAD_NEWS, PAL_NONE, r.right - WD_FRAMERECT_RIGHT - icon_size.width, CenterBounds(r.top, r.bottom, icon_size.height)); } break; } diff --git a/src/tree_gui.cpp b/src/tree_gui.cpp index 0b0eeda22b..cb1a760502 100644 --- a/src/tree_gui.cpp +++ b/src/tree_gui.cpp @@ -169,7 +169,7 @@ public: if (widget >= WID_BT_TYPE_BUTTON_FIRST) { const int index = widget - WID_BT_TYPE_BUTTON_FIRST; /* Trees "grow" in the centre on the bottom line of the buttons */ - DrawSprite(tree_sprites[index].sprite, tree_sprites[index].pal, (r.left + r.right) / 2 + WD_FRAMERECT_LEFT, r.bottom - ScaleGUITrad(BUTTON_BOTTOM_OFFSET)); + DrawSprite(tree_sprites[index].sprite, tree_sprites[index].pal, CenterBounds(r.left, r.right, 0) + WD_FRAMERECT_LEFT, r.bottom - ScaleGUITrad(BUTTON_BOTTOM_OFFSET)); } } diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 7ccb7e6c2f..02fc085eeb 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -2524,7 +2524,7 @@ struct VehicleDetailsWindow : Window { /* Draw service interval text */ SetDParam(0, v->GetServiceInterval()); SetDParam(1, v->date_of_last_service); - DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + (r.bottom - r.top + 1 - FONT_HEIGHT_NORMAL) / 2, + DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, CenterBounds(r.top, r.bottom, FONT_HEIGHT_NORMAL), v->ServiceIntervalIsPercent() ? STR_VEHICLE_DETAILS_SERVICING_INTERVAL_PERCENT : STR_VEHICLE_DETAILS_SERVICING_INTERVAL_DAYS); break; } @@ -3033,13 +3033,12 @@ public: /* Draw the flag plus orders. */ bool rtl = (_current_text_dir == TD_RTL); uint text_offset = std::max({GetSpriteSize(SPR_WARNING_SIGN).width, GetSpriteSize(SPR_FLAG_VEH_STOPPED).width, GetSpriteSize(SPR_FLAG_VEH_RUNNING).width}) + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT; - int height = r.bottom - r.top; int text_left = r.left + (rtl ? (uint)WD_FRAMERECT_LEFT : text_offset); int text_right = r.right - (rtl ? text_offset : (uint)WD_FRAMERECT_RIGHT); - int text_top = r.top + WD_FRAMERECT_TOP + (height - WD_FRAMERECT_TOP - WD_FRAMERECT_BOTTOM - FONT_HEIGHT_NORMAL) / 2; + int text_top = CenterBounds(r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM, FONT_HEIGHT_NORMAL); int image = ((v->vehstatus & VS_STOPPED) != 0) ? SPR_FLAG_VEH_STOPPED : (HasBit(v->vehicle_flags, VF_PATHFINDER_LOST)) ? SPR_WARNING_SIGN : SPR_FLAG_VEH_RUNNING; int image_left = (rtl ? text_right + 1 : r.left) + WD_IMGBTN_LEFT; - int image_top = r.top + WD_IMGBTN_TOP + (height - WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM - GetSpriteSize(image).height) / 2; + int image_top = CenterBounds(r.top + WD_IMGBTN_TOP, r.bottom - WD_IMGBTN_BOTTOM, GetSpriteSize(image).height); int lowered = this->IsWidgetLowered(WID_VV_START_STOP) ? 1 : 0; DrawSprite(image, PAL_NONE, image_left + lowered, image_top + lowered); DrawString(text_left + lowered, text_right + lowered, text_top + lowered, str, text_colour, SA_HOR_CENTER); From 17d1fe26c237f3608018396acb360db598170063 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Wed, 28 Sep 2022 17:16:46 +0100 Subject: [PATCH 4/9] Codechange: Helpers to allow passing a Rect to some functions. --- src/gfx_func.h | 36 ++++++++++++++++++++++++++++++++++++ src/window_gui.h | 6 ++++++ 2 files changed, 42 insertions(+) diff --git a/src/gfx_func.h b/src/gfx_func.h index c5bf12914b..c4e21792bd 100644 --- a/src/gfx_func.h +++ b/src/gfx_func.h @@ -108,6 +108,42 @@ void GfxFillPolygon(const std::vector &shape, int colour, FillRectMode mo void GfxDrawLine(int left, int top, int right, int bottom, int colour, int width = 1, int dash = 0); void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3); +/* Versions of DrawString/DrawStringMultiLine that accept a Rect instead of separate left, right, top and bottom parameters. */ +static inline int DrawString(const Rect &r, const char *str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL) +{ + return DrawString(r.left, r.right, r.top, str, colour, align, underline, fontsize); +} + +static inline int DrawString(const Rect &r, const std::string &str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL) +{ + return DrawString(r.left, r.right, r.top, str, colour, align, underline, fontsize); +} + +static inline int DrawString(const Rect &r, StringID str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL) +{ + return DrawString(r.left, r.right, r.top, str, colour, align, underline, fontsize); +} + +static inline int DrawStringMultiLine(const Rect &r, const char *str, TextColour colour = TC_FROMSTRING, StringAlignment align = (SA_TOP | SA_LEFT), bool underline = false, FontSize fontsize = FS_NORMAL) +{ + return DrawStringMultiLine(r.left, r.right, r.top, r.bottom, str, colour, align, underline, fontsize); +} + +static inline int DrawStringMultiLine(const Rect &r, const std::string &str, TextColour colour = TC_FROMSTRING, StringAlignment align = (SA_TOP | SA_LEFT), bool underline = false, FontSize fontsize = FS_NORMAL) +{ + return DrawStringMultiLine(r.left, r.right, r.top, r.bottom, str, colour, align, underline, fontsize); +} + +static inline int DrawStringMultiLine(const Rect &r, StringID str, TextColour colour = TC_FROMSTRING, StringAlignment align = (SA_TOP | SA_LEFT), bool underline = false, FontSize fontsize = FS_NORMAL) +{ + return DrawStringMultiLine(r.left, r.right, r.top, r.bottom, str, colour, align, underline, fontsize); +} + +static inline void GfxFillRect(const Rect &r, int colour, FillRectMode mode = FILLRECT_OPAQUE) +{ + GfxFillRect(r.left, r.top, r.right, r.bottom, colour, mode); +} + Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize = FS_NORMAL); Dimension GetStringBoundingBox(const std::string &str, FontSize start_fontsize = FS_NORMAL); Dimension GetStringBoundingBox(StringID strid); diff --git a/src/window_gui.h b/src/window_gui.h index 58d14a54e7..25b962f411 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -142,6 +142,12 @@ enum WidgetDrawDistances { /* widget.cpp */ void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags); + +static inline void DrawFrameRect(const Rect &r, Colours colour, FrameFlags flags) +{ + DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, flags); +} + void DrawCaption(const Rect &r, Colours colour, Owner owner, TextColour text_colour, StringID str, StringAlignment align); /* window.cpp */ From d8e01f8aa5bb7bc00da3388706201f2696793a7b Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Mon, 3 May 2021 19:27:03 +0100 Subject: [PATCH 5/9] Codechange: Add some Rect helpers. Shrink/Expand/Translate are marked nodiscard as they provide a new Rect rather than modifying the original Rect. --- src/core/geometry_type.hpp | 106 +++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/src/core/geometry_type.hpp b/src/core/geometry_type.hpp index cba944fb6f..d5fdd2a944 100644 --- a/src/core/geometry_type.hpp +++ b/src/core/geometry_type.hpp @@ -49,6 +49,112 @@ struct Rect { int top; int right; int bottom; + + /** + * Get width of Rect. + * @return width of Rect. + */ + inline int Width() const { return this->right - this->left + 1; } + + /** + * Get height of Rect. + * @return height of Rect. + */ + inline int Height() const { return this->bottom - this->top + 1; } + + /** + * Copy and shrink Rect by s pixels. + * @param s number of pixels to remove from each side of Rect. + * @return the new smaller Rect. + */ + [[nodiscard]] inline Rect Shrink(int s) const + { + return {this->left + s, this->top + s, this->right - s, this->bottom - s}; + } + + /** + * Copy and shrink Rect by h horizontal and v vertical pixels. + * @param h number of pixels to remove from left and right sides. + * @param v number of pixels to remove from top and bottom sides. + * @return the new smaller Rect. + */ + [[nodiscard]] inline Rect Shrink(int h, int v) const + { + return {this->left + h, this->top + v, this->right - h, this->bottom - v}; + } + + /** + * Copy and shrink Rect by pixels. + * @param left number of pixels to remove from left side. + * @param top number of pixels to remove from top side. + * @param right number of pixels to remove from right side. + * @param bottom number of pixels to remove from bottom side. + * @return the new smaller Rect. + */ + [[nodiscard]] inline Rect Shrink(int left, int top, int right, int bottom) const + { + return {this->left + left, this->top + top, this->right - right, this->bottom - bottom}; + } + + /** + * Copy and expand Rect by s pixels. + * @param s number of pixels to add to each side of Rect. + * @return the new larger Rect. + */ + [[nodiscard]] inline Rect Expand(int s) const + { + return this->Shrink(-s); + } + + /** + * Copy and translate Rect by x,y pixels. + * @param x number of pixels to move horizontally. + * @param y number of pixels to move vertically. + * @return the new translated Rect. + */ + [[nodiscard]] inline Rect Translate(int x, int y) const + { + return {this->left + x, this->top + y, this->right + x, this->bottom + y}; + } + + /** + * Copy Rect and set its width. + * @param width width in pixels for new Rect. + * @param end if set, set width at end of Rect, i.e. on right. + * @return the new resized Rect. + */ + [[nodiscard]] inline Rect WithWidth(int width, bool end) const + { + return end + ? Rect {this->right - width + 1, this->top, this->right, this->bottom} + : Rect {this->left, this->top, this->left + width - 1, this->bottom}; + } + + /** + * Copy Rect and indent it from its position. + * @param indent offset in pixels for new Rect. + * @param end if set, set indent at end of Rect, i.e. on right. + * @return the new resized Rect. + */ + [[nodiscard]] inline Rect Indent(int indent, bool end) const + { + return end + ? Rect {this->left, this->top, this->right - indent, this->bottom} + : Rect {this->left + indent, this->top, this->right, this->bottom}; + } + + /** + * Copy Rect and set its height. + * @param width height in pixels for new Rect. + * @param end if set, set height at end of Rect, i.e. at bottom. + * @return the new resized Rect. + */ + [[nodiscard]] inline Rect WithHeight(int height, bool end = false) const + { + return end + ? Rect {this->left, this->bottom - height + 1, this->right, this->bottom} + : Rect {this->left, this->top, this->right, this->top + height - 1}; + } }; /** From 632464a62393f7fb5b8370b5793fe6a31f19bf8d Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sat, 8 May 2021 17:25:43 +0100 Subject: [PATCH 6/9] Codechange: Use Rect in DropDownListItem::Draw(). --- src/company_gui.cpp | 17 ++++++++--------- src/toolbar_gui.cpp | 22 +++++++++++----------- src/widgets/dropdown.cpp | 30 +++++++++++++++--------------- src/widgets/dropdown_type.h | 6 +++--- 4 files changed, 37 insertions(+), 38 deletions(-) diff --git a/src/company_gui.cpp b/src/company_gui.cpp index 2a9b06a6b0..203f9972f6 100644 --- a/src/company_gui.cpp +++ b/src/company_gui.cpp @@ -603,18 +603,17 @@ public: return true; } - void Draw(int left, int right, int top, int bottom, bool sel, Colours bg_colour) const override + void Draw(const Rect &r, bool sel, Colours bg_colour) const override { bool rtl = _current_text_dir == TD_RTL; - int height = bottom - top; - int icon_y_offset = height / 2; - int text_y_offset = (height - FONT_HEIGHT_NORMAL) / 2 + 1; + int icon_y = CenterBounds(r.top, r.bottom, 0); + int text_y = CenterBounds(r.top, r.bottom, FONT_HEIGHT_NORMAL); DrawSprite(SPR_VEH_BUS_SIDE_VIEW, PALETTE_RECOLOUR_START + (this->result % COLOUR_END), - rtl ? right - 2 - ScaleGUITrad(14) : left + ScaleGUITrad(14) + 2, - top + icon_y_offset); - DrawString(rtl ? left + 2 : left + ScaleGUITrad(28) + 4, - rtl ? right - ScaleGUITrad(28) - 4 : right - 2, - top + text_y_offset, this->String(), sel ? TC_WHITE : TC_BLACK); + rtl ? r.right - 2 - ScaleGUITrad(14) : r.left + ScaleGUITrad(14) + 2, + icon_y); + DrawString(rtl ? r.left + 2 : r.left + ScaleGUITrad(28) + 4, + rtl ? r.right - ScaleGUITrad(28) - 4 : r.right - 2, + text_y, this->String(), sel ? TC_WHITE : TC_BLACK); } }; diff --git a/src/toolbar_gui.cpp b/src/toolbar_gui.cpp index 7f04511636..07376a3f56 100644 --- a/src/toolbar_gui.cpp +++ b/src/toolbar_gui.cpp @@ -98,18 +98,18 @@ public: this->checkmark_width = GetStringBoundingBox(STR_JUST_CHECKMARK).width + 3; } - uint Width() const + uint Width() const override { return DropDownListStringItem::Width() + this->checkmark_width; } - void Draw(int left, int right, int top, int bottom, bool sel, Colours bg_colour) const + void Draw(const Rect &r, bool sel, Colours bg_colour) const override { bool rtl = _current_text_dir == TD_RTL; if (this->checked) { - DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, top, STR_JUST_CHECKMARK, sel ? TC_WHITE : TC_BLACK); + DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top, STR_JUST_CHECKMARK, sel ? TC_WHITE : TC_BLACK); } - DrawString(left + WD_FRAMERECT_LEFT + (rtl ? 0 : this->checkmark_width), right - WD_FRAMERECT_RIGHT - (rtl ? this->checkmark_width : 0), top, this->String(), sel ? TC_WHITE : TC_BLACK); + DrawString(r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : this->checkmark_width), r.right - WD_FRAMERECT_RIGHT - (rtl ? this->checkmark_width : 0), r.top, this->String(), sel ? TC_WHITE : TC_BLACK); } }; @@ -146,7 +146,7 @@ public: return std::max(std::max(this->icon_size.height, this->lock_size.height) + 2U, (uint)FONT_HEIGHT_NORMAL); } - void Draw(int left, int right, int top, int bottom, bool sel, Colours bg_colour) const override + void Draw(const Rect &r, bool sel, Colours bg_colour) const override { CompanyID company = (CompanyID)this->result; bool rtl = _current_text_dir == TD_RTL; @@ -154,13 +154,13 @@ public: /* It's possible the company is deleted while the dropdown is open */ if (!Company::IsValidID(company)) return; - int icon_offset = (bottom - top - icon_size.height) / 2; - int text_offset = (bottom - top - FONT_HEIGHT_NORMAL) / 2; - int lock_offset = (bottom - top - lock_size.height) / 2; + int icon_y = CenterBounds(r.top, r.bottom, icon_size.height); + int text_y = CenterBounds(r.top, r.bottom, FONT_HEIGHT_NORMAL); + int lock_y = CenterBounds(r.top, r.bottom, lock_size.height); - DrawCompanyIcon(company, rtl ? right - this->icon_size.width - WD_FRAMERECT_RIGHT : left + WD_FRAMERECT_LEFT, top + icon_offset); + DrawCompanyIcon(company, rtl ? r.right - this->icon_size.width - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT, icon_y); if (NetworkCompanyIsPassworded(company)) { - DrawSprite(SPR_LOCK, PAL_NONE, rtl ? left + WD_FRAMERECT_LEFT : right - this->lock_size.width - WD_FRAMERECT_RIGHT, top + lock_offset); + DrawSprite(SPR_LOCK, PAL_NONE, rtl ? r.left + WD_FRAMERECT_LEFT : r.right - this->lock_size.width - WD_FRAMERECT_RIGHT, lock_y); } SetDParam(0, company); @@ -171,7 +171,7 @@ public: } else { col = sel ? TC_WHITE : TC_BLACK; } - DrawString(left + WD_FRAMERECT_LEFT + (rtl ? 3 + this->lock_size.width : 3 + this->icon_size.width), right - WD_FRAMERECT_RIGHT - (rtl ? 3 + this->icon_size.width : 3 + this->lock_size.width), top + text_offset, STR_COMPANY_NAME_COMPANY_NUM, col); + DrawString(r.left + WD_FRAMERECT_LEFT + (rtl ? 3 + this->lock_size.width : 3 + this->icon_size.width), r.right - WD_FRAMERECT_RIGHT - (rtl ? 3 + this->icon_size.width : 3 + this->lock_size.width), text_y, STR_COMPANY_NAME_COMPANY_NUM, col); } }; diff --git a/src/widgets/dropdown.cpp b/src/widgets/dropdown.cpp index f104be68c8..3e0365a0bf 100644 --- a/src/widgets/dropdown.cpp +++ b/src/widgets/dropdown.cpp @@ -20,14 +20,14 @@ #include "../safeguards.h" -void DropDownListItem::Draw(int left, int right, int top, int bottom, bool sel, Colours bg_colour) const +void DropDownListItem::Draw(const Rect &r, bool sel, Colours bg_colour) const { int c1 = _colour_gradient[bg_colour][3]; int c2 = _colour_gradient[bg_colour][7]; - int mid = top + this->Height(0) / 2; - GfxFillRect(left + 1, mid - 2, right - 1, mid - 2, c1); - GfxFillRect(left + 1, mid - 1, right - 1, mid - 1, c2); + int mid = (r.top + r.bottom) / 2; + GfxFillRect(r.left + 1, mid - 2, r.right - 1, mid - 2, c1); + GfxFillRect(r.left + 1, mid - 1, r.right - 1, mid - 1, c2); } uint DropDownListStringItem::Width() const @@ -37,9 +37,9 @@ uint DropDownListStringItem::Width() const return GetStringBoundingBox(buffer).width; } -void DropDownListStringItem::Draw(int left, int right, int top, int bottom, bool sel, Colours bg_colour) const +void DropDownListStringItem::Draw(const Rect &r, bool sel, Colours bg_colour) const { - DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, top, this->String(), sel ? TC_WHITE : TC_BLACK); + DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top, this->String(), sel ? TC_WHITE : TC_BLACK); } /** @@ -85,11 +85,11 @@ uint DropDownListIconItem::Width() const return DropDownListStringItem::Width() + this->dim.width + WD_FRAMERECT_LEFT; } -void DropDownListIconItem::Draw(int left, int right, int top, int bottom, bool sel, Colours bg_colour) const +void DropDownListIconItem::Draw(const Rect &r, bool sel, Colours bg_colour) const { bool rtl = _current_text_dir == TD_RTL; - DrawSprite(this->sprite, this->pal, rtl ? right - this->dim.width - WD_FRAMERECT_RIGHT : left + WD_FRAMERECT_LEFT, CenterBounds(top, bottom, this->sprite_y)); - DrawString(left + WD_FRAMERECT_LEFT + (rtl ? 0 : (this->dim.width + WD_FRAMERECT_LEFT)), right - WD_FRAMERECT_RIGHT - (rtl ? (this->dim.width + WD_FRAMERECT_RIGHT) : 0), CenterBounds(top, bottom, FONT_HEIGHT_NORMAL), this->String(), sel ? TC_WHITE : TC_BLACK); + DrawSprite(this->sprite, this->pal, rtl ? r.right - this->dim.width - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT, CenterBounds(r.top, r.bottom, this->sprite_y)); + DrawString(r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : (this->dim.width + WD_FRAMERECT_LEFT)), r.right - WD_FRAMERECT_RIGHT - (rtl ? (this->dim.width + WD_FRAMERECT_RIGHT) : 0), CenterBounds(r.top, r.bottom, FONT_HEIGHT_NORMAL), this->String(), sel ? TC_WHITE : TC_BLACK); } void DropDownListIconItem::SetDimension(Dimension d) @@ -213,9 +213,9 @@ struct DropdownWindow : Window { { if (GetWidgetFromPos(this, _cursor.pos.x - this->left, _cursor.pos.y - this->top) < 0) return false; - NWidgetBase *nwi = this->GetWidget(WID_DM_ITEMS); - int y = _cursor.pos.y - this->top - nwi->pos_y - 2; - int width = nwi->current_x - 4; + const Rect &r = this->GetWidget(WID_DM_ITEMS)->GetCurrentRect(); + int y = _cursor.pos.y - this->top - r.top - 2; + int width = r.Width(); int pos = this->vscroll->GetPosition(); for (const auto &item : this->list) { @@ -245,7 +245,7 @@ struct DropdownWindow : Window { int y = r.top + 2; int pos = this->vscroll->GetPosition(); for (const auto &item : this->list) { - int item_height = item->Height(r.right - r.left + 1); + int item_height = item->Height(r.Width()); /* Skip items that are scrolled up */ if (--pos >= 0) continue; @@ -254,7 +254,7 @@ struct DropdownWindow : Window { bool selected = (this->selected_index == item->result); if (selected) GfxFillRect(r.left + 2, y, r.right - 1, y + item_height - 1, PC_BLACK); - item->Draw(r.left, r.right, y, y + item_height, selected, colour); + item->Draw({r.left, y, r.right, y + item_height - 1}, selected, colour); if (item->masked) { GfxFillRect(r.left + 1, y, r.right - 1, y + item_height - 1, _colour_gradient[colour][5], FILLRECT_CHECKER); @@ -361,7 +361,7 @@ void ShowDropDownListAt(Window *w, DropDownList &&list, int selected, int button int top = w->top + wi_rect.bottom + 1; /* The preferred width equals the calling widget */ - uint width = wi_rect.right - wi_rect.left + 1; + uint width = wi_rect.Width(); /* Longest item in the list, if auto_width is enabled */ uint max_item_width = 0; diff --git a/src/widgets/dropdown_type.h b/src/widgets/dropdown_type.h index 5dfa9ed58b..2e95b407d7 100644 --- a/src/widgets/dropdown_type.h +++ b/src/widgets/dropdown_type.h @@ -30,7 +30,7 @@ public: virtual bool Selectable() const { return false; } virtual uint Height(uint width) const { return FONT_HEIGHT_NORMAL; } virtual uint Width() const { return 0; } - virtual void Draw(int left, int right, int top, int bottom, bool sel, Colours bg_colour) const; + virtual void Draw(const Rect &r, bool sel, Colours bg_colour) const; }; /** @@ -44,7 +44,7 @@ public: bool Selectable() const override { return true; } uint Width() const override; - void Draw(int left, int right, int top, int bottom, bool sel, Colours bg_colour) const override; + void Draw(const Rect &r, bool sel, Colours bg_colour) const override; virtual StringID String() const { return this->string; } static bool NatSortFunc(std::unique_ptr const &first, std::unique_ptr const &second); @@ -89,7 +89,7 @@ public: uint Height(uint width) const override; uint Width() const override; - void Draw(int left, int right, int top, int bottom, bool sel, Colours bg_colour) const override; + void Draw(const Rect &r, bool sel, Colours bg_colour) const override; void SetDimension(Dimension d); }; From f5f035a22b16dba3b47ba031bbce8ab0c12512b3 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Thu, 29 Sep 2022 00:10:41 +0100 Subject: [PATCH 7/9] 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); From cd0d5a771d0921896687e4417710d3684a23cf4d Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sat, 15 Oct 2022 18:06:55 +0100 Subject: [PATCH 8/9] Fix: Engine preview text overly padded. WIP_EP_QUESTION is already padded at the widget level, so no need to apply extra padding to left/right. --- src/engine_gui.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/engine_gui.cpp b/src/engine_gui.cpp index ff5a67ab57..8455dfc1d2 100644 --- a/src/engine_gui.cpp +++ b/src/engine_gui.cpp @@ -109,17 +109,16 @@ struct EnginePreviewWindow : Window { EngineID engine = this->window_number; SetDParam(0, GetEngineCategoryName(engine)); - 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; + int y = DrawStringMultiLine(r, STR_ENGINE_PREVIEW_MESSAGE, TC_FROMSTRING, SA_HOR_CENTER | SA_TOP) + WD_PAR_VSEP_WIDE; SetDParam(0, engine); - DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_ENGINE_NAME, TC_BLACK, SA_HOR_CENTER); + DrawString(r.left, r.right, y, STR_ENGINE_NAME, TC_BLACK, SA_HOR_CENTER); y += FONT_HEIGHT_NORMAL; - DrawVehicleEngine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, this->width >> 1, y + this->vehicle_space / 2, engine, GetEnginePalette(engine, _local_company), EIT_PREVIEW); + DrawVehicleEngine(r.left, r.right, this->width >> 1, y + this->vehicle_space / 2, engine, GetEnginePalette(engine, _local_company), EIT_PREVIEW); y += this->vehicle_space; - DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, r.bottom, GetEngineInfoString(engine), TC_FROMSTRING, SA_CENTER); + DrawStringMultiLine(r.left, r.right, y, r.bottom, GetEngineInfoString(engine), TC_FROMSTRING, SA_CENTER); } void OnClick(Point pt, int widget, int click_count) override From cb10ed15099f8fbb6a07a515d242b343cd82cd7b Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sat, 15 Oct 2022 18:39:08 +0100 Subject: [PATCH 9/9] Change: Move error window padding to widget level. This simplifies calculation during drawing. --- src/error_gui.cpp | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/error_gui.cpp b/src/error_gui.cpp index 366664a5a7..004fac0796 100644 --- a/src/error_gui.cpp +++ b/src/error_gui.cpp @@ -35,7 +35,7 @@ static const NWidgetPart _nested_errmsg_widgets[] = { NWidget(WWT_CAPTION, COLOUR_RED, WID_EM_CAPTION), SetDataTip(STR_ERROR_MESSAGE_CAPTION, STR_NULL), EndContainer(), NWidget(WWT_PANEL, COLOUR_RED), - NWidget(WWT_EMPTY, COLOUR_RED, WID_EM_MESSAGE), SetPadding(0, 2, 0, 2), SetMinimalSize(236, 32), + NWidget(WWT_EMPTY, COLOUR_RED, WID_EM_MESSAGE), SetPadding(WD_FRAMETEXT_TOP, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_BOTTOM, WD_FRAMETEXT_LEFT), SetFill(1, 0), SetMinimalSize(236, 0), EndContainer(), }; @@ -52,9 +52,9 @@ static const NWidgetPart _nested_errmsg_face_widgets[] = { NWidget(WWT_CAPTION, COLOUR_RED, WID_EM_CAPTION), SetDataTip(STR_ERROR_MESSAGE_CAPTION_OTHER_COMPANY, STR_NULL), EndContainer(), NWidget(WWT_PANEL, COLOUR_RED), - NWidget(NWID_HORIZONTAL), SetPIP(2, 1, 2), - NWidget(WWT_EMPTY, COLOUR_RED, WID_EM_FACE), SetMinimalSize(92, 119), SetFill(0, 1), SetPadding(2, 0, 1, 0), - NWidget(WWT_EMPTY, COLOUR_RED, WID_EM_MESSAGE), SetFill(0, 1), SetMinimalSize(238, 123), + NWidget(NWID_HORIZONTAL), + NWidget(WWT_EMPTY, COLOUR_RED, WID_EM_FACE), SetPadding(2, 0, 2, 2), SetFill(0, 1), SetMinimalSize(92, 119), + NWidget(WWT_EMPTY, COLOUR_RED, WID_EM_MESSAGE), SetPadding(WD_FRAMETEXT_TOP, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_BOTTOM, WD_FRAMETEXT_LEFT), SetFill(1, 1), SetMinimalSize(236, 0), EndContainer(), EndContainer(), }; @@ -200,13 +200,12 @@ public: CopyInDParam(0, this->decode_params, lengthof(this->decode_params)); if (this->textref_stack_size > 0) StartTextRefStackUsage(this->textref_stack_grffile, this->textref_stack_size, this->textref_stack); - int text_width = std::max(0, (int)size->width - WD_FRAMETEXT_LEFT - WD_FRAMETEXT_RIGHT); - this->height_summary = GetStringHeight(this->summary_msg, text_width); - this->height_detailed = (this->detailed_msg == INVALID_STRING_ID) ? 0 : GetStringHeight(this->detailed_msg, text_width); + this->height_summary = GetStringHeight(this->summary_msg, size->width); + this->height_detailed = (this->detailed_msg == INVALID_STRING_ID) ? 0 : GetStringHeight(this->detailed_msg, size->width); if (this->textref_stack_size > 0) StopTextRefStackUsage(); - uint panel_height = WD_FRAMERECT_TOP + this->height_summary + WD_FRAMERECT_BOTTOM; + uint panel_height = this->height_summary; if (this->detailed_msg != INVALID_STRING_ID) panel_height += this->height_detailed + WD_PAR_VSEP_WIDE; size->height = std::max(size->height, panel_height); @@ -282,19 +281,14 @@ public: if (this->textref_stack_size > 0) StartTextRefStackUsage(this->textref_stack_grffile, this->textref_stack_size, this->textref_stack); if (this->detailed_msg == INVALID_STRING_ID) { - 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); + DrawStringMultiLine(r, this->summary_msg, TC_FROMSTRING, SA_CENTER); } else { + /* Extra space when message is shorter than company face window */ 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; - int bottom = top + this->height_summary + extra; - DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, top, bottom, this->summary_msg, TC_WHITE, SA_CENTER); - - bottom = r.bottom - WD_FRAMERECT_BOTTOM; - top = bottom - this->height_detailed - extra; - DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, top, bottom, this->detailed_msg, TC_WHITE, SA_CENTER); + DrawStringMultiLine(r.SetHeight(this->height_summary + extra, false), this->summary_msg, TC_WHITE, SA_CENTER); + DrawStringMultiLine(r.SetHeight(this->height_detailed + extra, true), this->detailed_msg, TC_WHITE, SA_CENTER); } if (this->textref_stack_size > 0) StopTextRefStackUsage();