From 51b4bd6c38ac69319461a5ba6a582a5e7517cc02 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Mon, 19 Apr 2021 16:37:10 +0100 Subject: Codechange: Add widget text colour override property. --- src/news_gui.cpp | 2 +- src/widget.cpp | 76 +++++++++++++++++++++++++++++++++++-------------------- src/widget_type.h | 27 ++++++++++++++++++++ src/window_gui.h | 2 +- 4 files changed, 78 insertions(+), 29 deletions(-) diff --git a/src/news_gui.cpp b/src/news_gui.cpp index fa601eb56..8d0531682 100644 --- a/src/news_gui.cpp +++ b/src/news_gui.cpp @@ -422,7 +422,7 @@ struct NewsWindow : Window { { switch (widget) { case WID_N_CAPTION: - DrawCaption(r, COLOUR_LIGHT_BLUE, this->owner, STR_NEWS_MESSAGE_CAPTION, SA_HOR_CENTER); + DrawCaption(r, COLOUR_LIGHT_BLUE, this->owner, TC_FROMSTRING, STR_NEWS_MESSAGE_CAPTION, SA_HOR_CENTER); break; case WID_N_PANEL: diff --git a/src/widget.cpp b/src/widget.cpp index feaf036b2..1e3afb155 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -257,16 +257,17 @@ static inline void DrawImageButtons(const Rect &r, WidgetType type, Colours colo * @param r Rectangle of the label background. * @param type Widget type (#WWT_TEXTBTN, #WWT_TEXTBTN_2, or #WWT_LABEL). * @param clicked Label is rendered lowered. + * @param colour Colour of the text. * @param str Text to draw. * @param align Alignment of the text. */ -static inline void DrawLabel(const Rect &r, WidgetType type, bool clicked, StringID str, StringAlignment align) +static inline void DrawLabel(const Rect &r, WidgetType type, bool clicked, TextColour colour, StringID str, StringAlignment align) { if (str == STR_NULL) return; if ((type & WWT_MASK) == WWT_TEXTBTN_2 && clicked) str++; Dimension d = GetStringBoundingBox(str); Point p = GetAlignedPosition(r, d, align); - DrawString(r.left + clicked, r.right + clicked, p.y + clicked, str, TC_FROMSTRING, align); + DrawString(r.left + clicked, r.right + clicked, p.y + clicked, str, colour, align); } /** @@ -285,15 +286,16 @@ static inline void DrawText(const Rect &r, TextColour colour, StringID str, Stri /** * Draw an inset widget. - * @param r Rectangle of the background. - * @param colour Colour of the inset. - * @param str Text to draw. - * @param align Alignment of the text. + * @param r Rectangle of the background. + * @param colour Colour of the inset. + * @param text_colour Colour of the text. + * @param str Text to draw. + * @param align Alignment of the text. */ -static inline void DrawInset(const Rect &r, Colours colour, StringID str, StringAlignment align) +static inline void DrawInset(const Rect &r, Colours colour, TextColour text_colour, StringID str, StringAlignment align) { DrawFrameRect(r.left, r.top, r.right, r.bottom, colour, FR_LOWERED | FR_DARKENED); - if (str != STR_NULL) DrawString(r.left + WD_INSET_LEFT, r.right - WD_INSET_RIGHT, r.top + WD_INSET_TOP, str, TC_FROMSTRING, align); + if (str != STR_NULL) DrawString(r.left + WD_INSET_LEFT, r.right - WD_INSET_RIGHT, r.top + WD_INSET_TOP, str, text_colour, align); } /** @@ -434,16 +436,17 @@ static inline void DrawHorizontalScrollbar(const Rect &r, Colours colour, bool l /** * Draw a frame widget. - * @param r Rectangle of the frame. - * @param colour Colour of the frame. - * @param str Text of the frame. - * @param align Alignment of the text in the frame. + * @param r Rectangle of the frame. + * @param colour Colour of the frame. + * @param text_colour Colour of the text. + * @param str Text of the frame. + * @param align Alignment of the text in the frame. */ -static inline void DrawFrame(const Rect &r, Colours colour, StringID str, StringAlignment align) +static inline void DrawFrame(const Rect &r, Colours colour, TextColour text_colour, StringID str, StringAlignment align) { int x2 = r.left; // by default the left side is the left side of the widget - if (str != STR_NULL) x2 = DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top, str, TC_FROMSTRING, align); + if (str != STR_NULL) x2 = DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top, str, text_colour, align); int c1 = _colour_gradient[colour][3]; int c2 = _colour_gradient[colour][7]; @@ -563,13 +566,14 @@ static inline void DrawCloseBox(const Rect &r, Colours colour) /** * Draw a caption bar. - * @param r Rectangle of the bar. - * @param colour Colour of the window. - * @param owner 'Owner' of the window. - * @param str Text to draw in the bar. - * @param align Alignment of the text. + * @param r Rectangle of the bar. + * @param colour Colour of the window. + * @param owner 'Owner' of the window. + * @param text_colour Colour of the text. + * @param str Text to draw in the bar. + * @param align Alignment of the text. */ -void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str, StringAlignment align) +void DrawCaption(const Rect &r, Colours colour, Owner owner, TextColour text_colour, StringID str, StringAlignment align) { bool company_owned = owner < MAX_COMPANIES; @@ -583,7 +587,7 @@ void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str, Strin if (str != STR_NULL) { Dimension d = GetStringBoundingBox(str); Point p = GetAlignedPosition(r, d, align); - DrawString(r.left + WD_CAPTIONTEXT_LEFT, r.right - WD_CAPTIONTEXT_RIGHT, p.y, str, TC_FROMSTRING, align); + DrawString(r.left + WD_CAPTIONTEXT_LEFT, r.right - WD_CAPTIONTEXT_RIGHT, p.y, str, text_colour, align); } } @@ -908,6 +912,7 @@ NWidgetCore::NWidgetCore(WidgetType tp, Colours colour, uint fill_x, uint fill_y this->widget_data = widget_data; this->tool_tip = tool_tip; this->scrollbar_index = -1; + this->text_colour = TC_FROMSTRING; this->align = SA_CENTER; } @@ -932,6 +937,15 @@ void NWidgetCore::SetDataTip(uint32 widget_data, StringID tool_tip) this->tool_tip = tool_tip; } +/** + * Set the text colour of the nested widget. + * @param colour TextColour to use. + */ +void NWidgetCore::SetTextColour(TextColour colour) +{ + this->text_colour = colour; +} + /** * Set the tool tip of the nested widget. * @param tool_tip Tool tip string to use. @@ -1905,12 +1919,12 @@ void NWidgetBackground::Draw(const Window *w) case WWT_FRAME: if (this->index >= 0) w->SetStringParameters(this->index); - DrawFrame(r, this->colour, this->widget_data, this->align); + DrawFrame(r, this->colour, this->text_colour, this->widget_data, this->align); break; case WWT_INSET: if (this->index >= 0) w->SetStringParameters(this->index); - DrawInset(r, this->colour, this->widget_data, this->align); + DrawInset(r, this->colour, this->text_colour, this->widget_data, this->align); break; default: @@ -2553,7 +2567,7 @@ void NWidgetLeaf::Draw(const Window *w) case WWT_TEXTBTN_2: if (this->index >= 0) w->SetStringParameters(this->index); DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FR_LOWERED : FR_NONE); - DrawLabel(r, this->type, clicked, this->widget_data, this->align); + DrawLabel(r, this->type, clicked, this->text_colour, this->widget_data, this->align); break; case WWT_ARROWBTN: @@ -2572,12 +2586,12 @@ void NWidgetLeaf::Draw(const Window *w) case WWT_LABEL: if (this->index >= 0) w->SetStringParameters(this->index); - DrawLabel(r, this->type, clicked, this->widget_data, this->align); + DrawLabel(r, this->type, clicked, this->text_colour, this->widget_data, this->align); break; case WWT_TEXT: if (this->index >= 0) w->SetStringParameters(this->index); - DrawText(r, (TextColour)this->colour, this->widget_data, this->align); + DrawText(r, this->text_colour, this->widget_data, this->align); break; case WWT_MATRIX: @@ -2592,7 +2606,7 @@ void NWidgetLeaf::Draw(const Window *w) case WWT_CAPTION: if (this->index >= 0) w->SetStringParameters(this->index); - DrawCaption(r, this->colour, w->owner, this->widget_data, this->align); + DrawCaption(r, this->colour, w->owner, this->text_colour, this->widget_data, this->align); break; case WWT_SHADEBOX: @@ -2770,6 +2784,14 @@ static int MakeNWidget(const NWidgetPart *parts, int count, NWidgetBase **dest, break; } + case WPT_TEXTCOLOUR: { + NWidgetCore *nwc = dynamic_cast(*dest); + if (nwc != nullptr) { + nwc->SetTextColour(parts->u.colour.colour); + } + break; + } + case WPT_ALIGNMENT: { NWidgetCore *nwc = dynamic_cast(*dest); if (nwc != nullptr) { diff --git a/src/widget_type.h b/src/widget_type.h index 97f64ba1b..e1fd92f63 100644 --- a/src/widget_type.h +++ b/src/widget_type.h @@ -89,6 +89,7 @@ enum WidgetType { WPT_DATATIP, ///< Widget part for specifying data and tooltip. WPT_PADDING, ///< Widget part for specifying a padding. WPT_PIPSPACE, ///< Widget part for specifying pre/inter/post space for containers. + WPT_TEXTCOLOUR, ///< Widget part for specifying text colour. WPT_ALIGNMENT, ///< Widget part for specifying text/image alignment. WPT_ENDCONTAINER, ///< Widget part to denote end of a container. WPT_FUNCTION, ///< Widget part for calling a user function. @@ -297,6 +298,7 @@ public: void SetIndex(int index); void SetDataTip(uint32 widget_data, StringID tool_tip); void SetToolTip(StringID tool_tip); + void SetTextColour(TextColour colour); void SetAlignment(StringAlignment align); inline void SetLowered(bool lowered); @@ -317,6 +319,7 @@ public: StringID tool_tip; ///< Tooltip of the widget. @see Widget::tootips int scrollbar_index; ///< Index of an attached scrollbar. TextColour highlight_colour; ///< Colour of highlight. + TextColour text_colour; ///< Colour of text within widget. StringAlignment align; ///< Alignment of text/image within widget. }; @@ -908,6 +911,14 @@ struct NWidgetPartTextLines { FontSize size; ///< Font size of text lines. }; +/** + * Widget part for storing text colour. + * @ingroup NestedWidgetParts + */ +struct NWidgetPartTextColour { + TextColour colour; ///< TextColour for DrawString. +}; + /** * Widget part for setting text/image alignment within a widget. * @ingroup NestedWidgetParts @@ -937,6 +948,7 @@ struct NWidgetPart { NWidgetPartPaddings padding; ///< Part with paddings. NWidgetPartPIP pip; ///< Part with pre/inter/post spaces. NWidgetPartTextLines text_lines; ///< Part with text line data. + NWidgetPartTextColour colour; ///< Part with text colour data. NWidgetPartAlignment align; ///< Part with internal alignment. NWidgetFunctionType *func_ptr; ///< Part with a function call. NWidContainerFlags cont_flags; ///< Part with container flags. @@ -996,6 +1008,21 @@ static inline NWidgetPart SetMinimalTextLines(uint8 lines, uint8 spacing, FontSi return part; } +/** + * Widget part function for setting the text colour. + * @param colour Colour to draw string within widget. + * @ingroup NestedWidgetParts + */ +static inline NWidgetPart SetTextColour(TextColour colour) +{ + NWidgetPart part; + + part.type = WPT_TEXTCOLOUR; + part.u.colour.colour = colour; + + return part; +} + /** * Widget part function for setting the alignment of text/images. * @param align Alignment of text/image within widget. diff --git a/src/window_gui.h b/src/window_gui.h index 1799001c9..67a799c3d 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -140,7 +140,7 @@ enum WidgetDrawDistances { /* widget.cpp */ void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags); -void DrawCaption(const Rect &r, Colours colour, Owner owner, StringID str, StringAlignment align); +void DrawCaption(const Rect &r, Colours colour, Owner owner, TextColour text_colour, StringID str, StringAlignment align); /* window.cpp */ extern Window *_z_front_window; -- cgit v1.2.3-70-g09d2