From 632464a62393f7fb5b8370b5793fe6a31f19bf8d Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sat, 8 May 2021 17:25:43 +0100 Subject: [PATCH] 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); };