diff --git a/src/intro_gui.cpp b/src/intro_gui.cpp index 662442ad8a..35de114e34 100644 --- a/src/intro_gui.cpp +++ b/src/intro_gui.cpp @@ -72,10 +72,17 @@ struct SelectGameWindow : public Window { virtual void OnPaint() { - SetDParam(0, STR_DIFFICULTY_LEVEL_EASY + _settings_newgame.difficulty.diff_level); this->DrawWidgets(); } + virtual void DrawWidget(const Rect &r, int widget) const + { + if (widget == SGI_DIFFICULTIES) { + SetDParam(0, STR_DIFFICULTY_LEVEL_EASY + _settings_newgame.difficulty.diff_level); + DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_INTRO_DIFFICULTY, TC_FROMSTRING, SA_CENTER); + } + } + virtual void OnClick(Point pt, int widget) { #ifdef ENABLE_NETWORK @@ -189,7 +196,7 @@ static const NWidgetPart _nested_select_game_widgets[] = { NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_OPTIONS), SetMinimalSize(158, 12), SetDataTip(STR_INTRO_GAME_OPTIONS, STR_INTRO_TOOLTIP_GAME_OPTIONS), SetPadding(0, 0, 0, 10), NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_DIFFICULTIES), SetMinimalSize(158, 12), - SetDataTip(STR_INTRO_DIFFICULTY, STR_INTRO_TOOLTIP_DIFFICULTY_OPTIONS), SetPadding(0, 10, 0, 0), + SetDataTip(STR_NULL, STR_INTRO_TOOLTIP_DIFFICULTY_OPTIONS), SetPadding(0, 10, 0, 0), EndContainer(), NWidget(NWID_SPACER), SetMinimalSize(0, 6), diff --git a/src/town_gui.cpp b/src/town_gui.cpp index 9731d35cd1..84ca240c7c 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -613,29 +613,40 @@ public: SetVScrollCount(this, this->towns.Length()); this->DrawWidgets(); - this->DrawSortButtonState(this->towns.SortType() == 0 ? TDW_SORTNAME : TDW_SORTPOPULATION, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP); + } - { - int n = 0; - uint16 i = this->vscroll.pos; - int y = this->nested_array[TDW_CENTERTOWN]->pos_y + 2; + virtual void DrawWidget(const Rect &r, int widget) const + { + switch(widget) { + case TDW_SORTNAME: + if (this->towns.SortType() == 0) this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP); + break; - while (i < this->towns.Length()) { - const Town *t = this->towns[i]; + case TDW_SORTPOPULATION: + if (this->towns.SortType() != 0) this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP); + break; - assert(t->xy != INVALID_TILE); + case TDW_CENTERTOWN: { + int n = 0; + int y = r.top + 2; + for (uint i = this->vscroll.pos; i < this->towns.Length(); i++) { + const Town *t = this->towns[i]; - SetDParam(0, t->index); - SetDParam(1, t->population); - DrawString(2, this->width - 2, y, STR_TOWN_DIRECTORY_TOWN); + assert(t->xy != INVALID_TILE); - y += this->townline_height; - i++; - if (++n == this->vscroll.cap) break; // max number of towns in 1 window - } + SetDParam(0, t->index); + SetDParam(1, t->population); + DrawString(r.left + 2, r.right - 2, y, STR_TOWN_DIRECTORY_TOWN); - SetDParam(0, GetWorldPopulation()); - DrawString(3, this->width - 3, this->height - 12 + 2, STR_TOWN_POPULATION); + y += this->townline_height; + if (++n == this->vscroll.cap) break; // max number of towns in 1 window + } + } break; + + case TDW_EMPTYBOTTOM: + SetDParam(0, GetWorldPopulation()); + DrawString(r.left + 3, r.right - 3, r.top + 2, STR_TOWN_POPULATION); + break; } } diff --git a/src/widget.cpp b/src/widget.cpp index dc8a3598e2..390012879e 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -223,43 +223,6 @@ int GetWidgetFromPos(const Window *w, int x, int y) return found_index; } -/** Distances used in drawing widgets. */ -enum WidgetDrawDistances { - WD_IMGBTN_LEFT = 1, ///< Left offset of the image in the button. - WD_IMGBTN_TOP = 1, ///< Top offset of image in the button. - - WD_INSET_LEFT = 2, ///< Left offset of string. - WD_INSET_RIGHT = 2, ///< Right offset of string. - WD_INSET_TOP = 1, ///< Top offset of string. - - WD_VSCROLLBAR_WIDTH = 12, ///< Width of a vertical scrollbar. - - WD_HSCROLLBAR_HEIGHT = 12, ///< Height of a horizontal scrollbar. - - WD_FRAMETEXT_LEFT = 6, ///< Left offset of the text of the frame. - WD_FRAMETEXT_RIGHT = 6, ///< Right offset of the text of the frame. - - WD_STICKY_WIDTH = 12, ///< Width of a sticky box widget. - WD_STICKY_LEFT = 2, ///< Left offset of sticky sprite. - WD_STICKY_TOP = 3, ///< Top offset of sticky sprite. - - WD_RESIZE_WIDTH = 12, ///< Width of a resize box widget. - WD_RESIZE_TOP = 3, ///< Top offset of resize sprite. - - WD_CLOSEBOX_WIDTH = 11, ///< Width of a close box widget. - WD_CLOSEBOX_TOP = 2, ///< Distance between the top of the close box widget, and the string. - - WD_CAPTION_HEIGHT = 14, ///< Height of a title bar. - WD_CAPTIONTEXT_LEFT = 2, ///< Offset of the caption text at the left. - WD_CAPTIONTEXT_RIGHT = 2, ///< Offset of the caption text at the right. - WD_CAPTIONTEXT_TOP = 2, ///< Offset of the caption text at the top. - - WD_DROPDOWN_HEIGHT = 12, ///< Height of a drop down widget. - WD_DROPDOWNTEXT_LEFT = 2, ///< Left offset of the dropdown widget string. - WD_DROPDOWNTEXT_RIGHT = 14, ///< Right offset of the dropdown widget string. - WD_DROPDOWNTEXT_TOP = 1, ///< Top offset of the dropdown widget string. -}; - /** * Draw frame rectangle. * @param left Left edge of the frame @@ -327,6 +290,7 @@ static inline void DrawImageButtons(const Rect &r, WidgetType type, Colours colo */ static inline void DrawLabel(const Rect &r, WidgetType type, bool clicked, StringID str) { + if (str == STR_NULL) return; if ((type & WWT_MASK) == WWT_TEXTBTN_2 && clicked) str++; DrawString(r.left + clicked, r.right + clicked, ((r.top + r.bottom + 1) >> 1) - (FONT_HEIGHT_NORMAL / 2) + clicked, str, TC_FROMSTRING, SA_CENTER); } @@ -582,7 +546,7 @@ static inline void DrawCaption(const Rect &r, Colours colour, Owner owner, Strin GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, _colour_gradient[_company_colours[owner]][4]); } - DrawString(r.left + WD_CAPTIONTEXT_LEFT, r.right - WD_CAPTIONTEXT_RIGHT, r.top + WD_CAPTIONTEXT_TOP, str, TC_FROMSTRING, SA_CENTER); + if (str != STR_NULL) DrawString(r.left + WD_CAPTIONTEXT_LEFT, r.right - WD_CAPTIONTEXT_RIGHT, r.top + WD_CAPTIONTEXT_TOP, str, TC_FROMSTRING, SA_CENTER); } static inline void DrawDropdown(const Rect &r, Colours colour, bool clicked, StringID str) @@ -1001,6 +965,7 @@ inline void NWidgetBase::StoreSizePosition(SizingType sizing, uint x, uint y, ui /** * @fn void Draw(const Window *w) * Draw the widgets of the tree. + * The function calls #Window::DrawWidget for each widget with a non-negative index, after the widget itself is painted. * @param w Window that owns the tree. */ @@ -1831,6 +1796,7 @@ void NWidgetBackground::Draw(const Window *w) } if (this->child != NULL) this->child->Draw(w); + if (this->index >= 0) w->DrawWidget(r, this->index); if (this->IsDisabled()) { GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER); @@ -2050,6 +2016,7 @@ void NWidgetLeaf::Draw(const Window *w) default: NOT_REACHED(); } + if (this->index >= 0) w->DrawWidget(r, this->index); if (this->IsDisabled()) { GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, _colour_gradient[this->colour & 0xF][2], FILLRECT_CHECKER); diff --git a/src/window_gui.h b/src/window_gui.h index bc3237ce4b..2847bad485 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -27,6 +27,57 @@ enum FrameFlags { DECLARE_ENUM_AS_BIT_SET(FrameFlags); +/** Distances used in drawing widgets. */ +enum WidgetDrawDistances { + /* WWT_IMGBTN, WWT_IMGBTN_2 */ + WD_IMGBTN_LEFT = 1, ///< Left offset of the image in the button. + WD_IMGBTN_TOP = 1, ///< Top offset of image in the button. + + /* WWT_INSET */ + WD_INSET_LEFT = 2, ///< Left offset of string. + WD_INSET_RIGHT = 2, ///< Right offset of string. + WD_INSET_TOP = 1, ///< Top offset of string. + + WD_VSCROLLBAR_WIDTH = 12, ///< Width of a vertical scrollbar. + + WD_HSCROLLBAR_HEIGHT = 12, ///< Height of a horizontal scrollbar. + + /* FrameRect widgets, all text buttons, panel, editbox */ + WD_FRAMERECT_LEFT = 2, ///< Offset at left to draw the frame rectangular area + WD_FRAMERECT_RIGHT = 2, ///< Offset at right to draw the frame rectangular area + WD_FRAMERECT_TOP = 1, ///< Offset at top to draw the frame rectangular area + WD_FRAMERECT_BOTTOM = 1, ///< Offset at bottom to draw the frame rectangular area + + /* WWT_FRAME */ + WD_FRAMETEXT_LEFT = 6, ///< Left offset of the text of the frame. + WD_FRAMETEXT_RIGHT = 6, ///< Right offset of the text of the frame. + + /* WWT_STICKYBOX */ + WD_STICKY_WIDTH = 12, ///< Width of a sticky box widget. + WD_STICKY_LEFT = 2, ///< Left offset of sticky sprite. + WD_STICKY_TOP = 3, ///< Top offset of sticky sprite. + + /* WWT_RESIZEBOX */ + WD_RESIZE_WIDTH = 12, ///< Width of a resize box widget. + WD_RESIZE_TOP = 3, ///< Top offset of resize sprite. + + /* WWT_CLOSEBOX */ + WD_CLOSEBOX_WIDTH = 11, ///< Width of a close box widget. + WD_CLOSEBOX_TOP = 2, ///< Distance between the top of the close box widget, and the string. + + /* WWT_CAPTION */ + WD_CAPTION_HEIGHT = 14, ///< Height of a title bar. + WD_CAPTIONTEXT_LEFT = 2, ///< Offset of the caption text at the left. + WD_CAPTIONTEXT_RIGHT = 2, ///< Offset of the caption text at the right. + WD_CAPTIONTEXT_TOP = 2, ///< Offset of the caption text at the top. + + /* Dropdown widget. */ + WD_DROPDOWN_HEIGHT = 12, ///< Height of a drop down widget. + WD_DROPDOWNTEXT_LEFT = 2, ///< Left offset of the dropdown widget string. + WD_DROPDOWNTEXT_RIGHT = 14, ///< Right offset of the dropdown widget string. + WD_DROPDOWNTEXT_TOP = 1, ///< Top offset of the dropdown widget string. +}; + /* wiget.cpp */ void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags); @@ -434,10 +485,19 @@ public: /*** Event handling ***/ /** - * This window is currently being repainted. + * The window must be repainted. + * @note This method should not change any state, it should only use drawing functions. */ virtual void OnPaint() {} + /** + * Draw the contents of a nested widget. + * @param r Rectangle occupied by the widget. + * @param widget Number of the widget to draw. + * @note This method may not change any state, it may only use drawing functions. + */ + virtual void DrawWidget(const Rect &r, int widget) const {} + /** * Called when window gains focus */