diff --git a/src/dropdown.cpp b/src/dropdown.cpp index a328c69896..e424d79ad6 100644 --- a/src/dropdown.cpp +++ b/src/dropdown.cpp @@ -132,7 +132,7 @@ struct DropdownWindow : Window { /* Set flag on parent widget to indicate that we have just closed. */ NWidgetCore *nwc = this->parent->GetWidget(this->parent_button); - if (nwc != nullptr) SetBit(nwc->disp_flags, NDB_DROPDOWN_CLOSED); + if (nwc != nullptr) nwc->disp_flags.Set(NWidgetDisplayFlag::DropdownClosed); } void OnFocusLost(bool closing) override @@ -410,7 +410,7 @@ void ShowDropDownList(Window *w, DropDownList &&list, int selected, WidgetID but Colours wi_colour = nwi->colour; if ((nwi->type & WWT_MASK) == NWID_BUTTON_DROPDOWN) { - nwi->disp_flags |= ND_DROPDOWN_ACTIVE; + nwi->disp_flags.Set(NWidgetDisplayFlag::DropdownActive); } else { nwi->SetLowered(true); } diff --git a/src/news_gui.cpp b/src/news_gui.cpp index bef04fe83a..1c29471c77 100644 --- a/src/news_gui.cpp +++ b/src/news_gui.cpp @@ -396,11 +396,11 @@ struct NewsWindow : Window { } else { nvp->InitializeViewport(this, GetReferenceTile(ni->reftype1, ni->ref1), ScaleZoomGUI(ZOOM_LVL_NEWS)); } - if (this->ni->flags & NF_NO_TRANSPARENT) nvp->disp_flags |= ND_NO_TRANSPARENCY; + if (this->ni->flags & NF_NO_TRANSPARENT) nvp->disp_flags.Set(NWidgetDisplayFlag::NoTransparency); if ((this->ni->flags & NF_INCOLOUR) == 0) { - nvp->disp_flags |= ND_SHADE_GREY; + nvp->disp_flags.Set(NWidgetDisplayFlag::ShadeGrey); } else if (this->ni->flags & NF_SHADE) { - nvp->disp_flags |= ND_SHADE_DIMMED; + nvp->disp_flags.Set(NWidgetDisplayFlag::ShadeDimmed); } } diff --git a/src/widget.cpp b/src/widget.cpp index 4ea993f7c6..5149181d1d 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -194,7 +194,7 @@ static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, in } if (pos < mi + button_size) { /* Pressing the upper button? */ - SetBit(sb->disp_flags, NDB_SCROLLBAR_UP); + sb->disp_flags.Set(NWidgetDisplayFlag::ScrollbarUp); if (_scroller_click_timeout <= 1) { _scroller_click_timeout = 3; changed = sb->UpdatePosition(rtl ? 1 : -1); @@ -202,7 +202,7 @@ static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, in w->mouse_capture_widget = sb->GetIndex(); } else if (pos >= ma - button_size) { /* Pressing the lower button? */ - SetBit(sb->disp_flags, NDB_SCROLLBAR_DOWN); + sb->disp_flags.Set(NWidgetDisplayFlag::ScrollbarDown); if (_scroller_click_timeout <= 1) { _scroller_click_timeout = 3; @@ -2401,7 +2401,7 @@ void NWidgetViewport::Draw(const Window *w) { if (this->current_x == 0 || this->current_y == 0) return; - if (this->disp_flags & ND_NO_TRANSPARENCY) { + if (this->disp_flags.Test(NWidgetDisplayFlag::NoTransparency)) { TransparencyOptionBits to_backup = _transparency_opt; _transparency_opt &= (1 << TO_SIGNS) | (1 << TO_TEXT); // Disable all transparency, except textual stuff w->DrawViewport(); @@ -2411,8 +2411,8 @@ void NWidgetViewport::Draw(const Window *w) } /* Optionally shade the viewport. */ - if (this->disp_flags & (ND_SHADE_GREY | ND_SHADE_DIMMED)) { - GfxFillRect(this->GetCurrentRect(), (this->disp_flags & ND_SHADE_DIMMED) ? PALETTE_TO_TRANSPARENT : PALETTE_NEWSPAPER, FILLRECT_RECOLOUR); + if (this->disp_flags.Any({NWidgetDisplayFlag::ShadeGrey, NWidgetDisplayFlag::ShadeDimmed})) { + GfxFillRect(this->GetCurrentRect(), this->disp_flags.Test(NWidgetDisplayFlag::ShadeDimmed) ? PALETTE_TO_TRANSPARENT : PALETTE_NEWSPAPER, FILLRECT_RECOLOUR); } DrawOutline(w, this); @@ -2626,9 +2626,9 @@ void NWidgetScrollbar::Draw(const Window *w) const DrawPixelInfo *dpi = _cur_dpi; if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return; - bool up_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_UP); - bool down_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_DOWN); - bool middle_lowered = !(this->disp_flags & ND_SCROLLBAR_BTN) && w->mouse_capture_widget == this->index; + bool up_lowered = this->disp_flags.Test(NWidgetDisplayFlag::ScrollbarUp); + bool down_lowered = this->disp_flags.Test(NWidgetDisplayFlag::ScrollbarDown); + bool middle_lowered = !this->disp_flags.Any({NWidgetDisplayFlag::ScrollbarUp, NWidgetDisplayFlag::ScrollbarDown}) && w->mouse_capture_widget == this->index; if (this->type == NWID_HSCROLLBAR) { DrawHorizontalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this); @@ -3091,7 +3091,7 @@ void NWidgetLeaf::Draw(const Window *w) case NWID_BUTTON_DROPDOWN: case NWID_PUSHBUTTON_DROPDOWN: if (this->index >= 0) w->SetStringParameters(this->index); - DrawButtonDropdown(r, this->colour, clicked, (this->disp_flags & ND_DROPDOWN_ACTIVE) != 0, this->GetString(), this->align); + DrawButtonDropdown(r, this->colour, clicked, this->disp_flags.Test(NWidgetDisplayFlag::DropdownActive), this->GetString(), this->align); break; default: diff --git a/src/widget_type.h b/src/widget_type.h index 79296cc929..0394abec1e 100644 --- a/src/widget_type.h +++ b/src/widget_type.h @@ -323,36 +323,28 @@ public: }; /** Nested widget flags that affect display and interaction with 'real' widgets. */ -enum NWidgetDisplay : uint16_t { +enum NWidgetDisplayFlag : uint8_t { /* Generic. */ - NDB_LOWERED = 0, ///< Widget is lowered (pressed down) bit. - NDB_DISABLED = 1, ///< Widget is disabled (greyed out) bit. - /* Viewport widget. */ - NDB_NO_TRANSPARENCY = 2, ///< Viewport is never transparent. - NDB_SHADE_GREY = 3, ///< Shade viewport to grey-scale. - NDB_SHADE_DIMMED = 4, ///< Display dimmed colours in the viewport. - /* Button dropdown widget. */ - NDB_DROPDOWN_ACTIVE = 5, ///< Dropdown menu of the button dropdown widget is active. @see #NWID_BUTTON_DROPDOWN - /* Scrollbar widget. */ - NDB_SCROLLBAR_UP = 6, ///< Up-button is lowered bit. - NDB_SCROLLBAR_DOWN = 7, ///< Down-button is lowered bit. - /* Generic. */ - NDB_HIGHLIGHT = 8, ///< Highlight of widget is on. - NDB_DROPDOWN_CLOSED = 9, ///< Dropdown menu of the dropdown widget has closed. + Lowered, ///< Widget is lowered (pressed down) bit. + Disabled, ///< Widget is disabled (greyed out) bit. - ND_LOWERED = 1 << NDB_LOWERED, ///< Bit value of the lowered flag. - ND_DISABLED = 1 << NDB_DISABLED, ///< Bit value of the disabled flag. - ND_HIGHLIGHT = 1 << NDB_HIGHLIGHT, ///< Bit value of the highlight flag. - ND_NO_TRANSPARENCY = 1 << NDB_NO_TRANSPARENCY, ///< Bit value of the 'no transparency' flag. - ND_SHADE_GREY = 1 << NDB_SHADE_GREY, ///< Bit value of the 'shade to grey' flag. - ND_SHADE_DIMMED = 1 << NDB_SHADE_DIMMED, ///< Bit value of the 'dimmed colours' flag. - ND_DROPDOWN_ACTIVE = 1 << NDB_DROPDOWN_ACTIVE, ///< Bit value of the 'dropdown active' flag. - ND_SCROLLBAR_UP = 1 << NDB_SCROLLBAR_UP, ///< Bit value of the 'scrollbar up' flag. - ND_SCROLLBAR_DOWN = 1 << NDB_SCROLLBAR_DOWN, ///< Bit value of the 'scrollbar down' flag. - ND_SCROLLBAR_BTN = ND_SCROLLBAR_UP | ND_SCROLLBAR_DOWN, ///< Bit value of the 'scrollbar up' or 'scrollbar down' flag. - ND_DROPDOWN_CLOSED = 1 << NDB_DROPDOWN_CLOSED, ///< Bit value of the 'dropdown closed' flag. + /* Viewport widget. */ + NoTransparency, ///< Viewport is never transparent. + ShadeGrey, ///< Shade viewport to grey-scale. + ShadeDimmed, ///< Display dimmed colours in the viewport. + + /* Button dropdown widget. */ + DropdownActive, ///< Dropdown menu of the button dropdown widget is active. @see #NWID_BUTTON_DROPDOWN + + /* Scrollbar widget. */ + ScrollbarUp, ///< Up-button is lowered bit. + ScrollbarDown, ///< Down-button is lowered bit. + + /* Generic. */ + Highlight, ///< Highlight of widget is on. + DropdownClosed, ///< Dropdown menu of the dropdown widget has closed. }; -DECLARE_ENUM_AS_BIT_SET(NWidgetDisplay) +using NWidgetDisplayFlags = EnumBitSet; /** Container with the data associated to a single widget. */ struct WidgetData { @@ -397,7 +389,7 @@ public: TextColour GetHighlightColour() const override; void SetHighlighted(TextColour highlight_colour) override; - NWidgetDisplay disp_flags; ///< Flags that affect display and interaction with the widget. + NWidgetDisplayFlags disp_flags; ///< Flags that affect display and interaction with the widget. Colours colour; ///< Colour of this widget. protected: const WidgetID index; ///< Index of the nested widget (\c -1 means 'not used'). @@ -419,14 +411,14 @@ protected: */ inline void NWidgetCore::SetHighlighted(TextColour highlight_colour) { - this->disp_flags = highlight_colour != TC_INVALID ? SETBITS(this->disp_flags, ND_HIGHLIGHT) : CLRBITS(this->disp_flags, ND_HIGHLIGHT); + highlight_colour != TC_INVALID ? this->disp_flags.Set(NWidgetDisplayFlag::Highlight) : this->disp_flags.Reset(NWidgetDisplayFlag::Highlight); this->highlight_colour = highlight_colour; } /** Return whether the widget is highlighted. */ inline bool NWidgetCore::IsHighlighted() const { - return HasBit(this->disp_flags, NDB_HIGHLIGHT); + return this->disp_flags.Test(NWidgetDisplayFlag::Highlight); } /** Return the colour of the highlight. */ @@ -441,13 +433,13 @@ inline TextColour NWidgetCore::GetHighlightColour() const */ inline void NWidgetCore::SetLowered(bool lowered) { - this->disp_flags = lowered ? SETBITS(this->disp_flags, ND_LOWERED) : CLRBITS(this->disp_flags, ND_LOWERED); + lowered ? this->disp_flags.Set(NWidgetDisplayFlag::Lowered) : this->disp_flags.Reset(NWidgetDisplayFlag::Lowered); } /** Return whether the widget is lowered. */ inline bool NWidgetCore::IsLowered() const { - return HasBit(this->disp_flags, NDB_LOWERED); + return this->disp_flags.Test(NWidgetDisplayFlag::Lowered); } /** @@ -456,13 +448,13 @@ inline bool NWidgetCore::IsLowered() const */ inline void NWidgetCore::SetDisabled(bool disabled) { - this->disp_flags = disabled ? SETBITS(this->disp_flags, ND_DISABLED) : CLRBITS(this->disp_flags, ND_DISABLED); + this->disp_flags = disabled ? this->disp_flags.Set(NWidgetDisplayFlag::Disabled) : this->disp_flags.Reset(NWidgetDisplayFlag::Disabled); } /** Return whether the widget is disabled. */ inline bool NWidgetCore::IsDisabled() const { - return HasBit(this->disp_flags, NDB_DISABLED); + return this->disp_flags.Test(NWidgetDisplayFlag::Disabled); } diff --git a/src/window.cpp b/src/window.cpp index ad61dce045..63f63f3c2d 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -296,7 +296,7 @@ void Window::OnDropdownClose(Point pt, WidgetID widget, int index, bool instant_ /* Raise the dropdown button */ NWidgetCore *nwi2 = this->GetWidget(widget); if ((nwi2->type & WWT_MASK) == NWID_BUTTON_DROPDOWN) { - nwi2->disp_flags &= ~ND_DROPDOWN_ACTIVE; + nwi2->disp_flags.Reset(NWidgetDisplayFlag::DropdownActive); } else { this->RaiseWidget(widget); } @@ -607,7 +607,7 @@ static void DispatchLeftClickEvent(Window *w, int x, int y, int click_count) WidgetType widget_type = (nw != nullptr) ? nw->type : WWT_EMPTY; /* Allow dropdown close flag detection to work. */ - if (nw != nullptr) ClrBit(nw->disp_flags, NDB_DROPDOWN_CLOSED); + if (nw != nullptr) nw->disp_flags.Reset(NWidgetDisplayFlag::DropdownClosed); bool focused_widget_changed = false; /* If clicked on a window that previously did not have focus */ @@ -642,7 +642,7 @@ static void DispatchLeftClickEvent(Window *w, int x, int y, int click_count) } /* Dropdown window of this widget was closed so don't process click this time. */ - if (HasBit(nw->disp_flags, NDB_DROPDOWN_CLOSED)) return; + if (nw->disp_flags.Test(NWidgetDisplayFlag::DropdownClosed)) return; if ((widget_type & ~WWB_PUSHBUTTON) < WWT_LAST && (widget_type & WWB_PUSHBUTTON)) w->HandleButtonClick(widget_index); @@ -1830,8 +1830,8 @@ static void DecreaseWindowCounters() NWidgetBase *nwid = pair.second; if (nwid->type == NWID_HSCROLLBAR || nwid->type == NWID_VSCROLLBAR) { NWidgetScrollbar *sb = static_cast(nwid); - if (sb->disp_flags & (ND_SCROLLBAR_UP | ND_SCROLLBAR_DOWN)) { - sb->disp_flags &= ~(ND_SCROLLBAR_UP | ND_SCROLLBAR_DOWN); + if (sb->disp_flags.Any({NWidgetDisplayFlag::ScrollbarUp, NWidgetDisplayFlag::ScrollbarDown})) { + sb->disp_flags.Reset(NWidgetDisplayFlag::ScrollbarUp).Reset(NWidgetDisplayFlag::ScrollbarDown); w->mouse_capture_widget = -1; sb->SetDirty(w); } @@ -2297,10 +2297,10 @@ static void HandleScrollbarScrolling(Window *w) i = _cursor.pos.y - _cursorpos_drag_start.y; } - if (sb->disp_flags & ND_SCROLLBAR_BTN) { + if (sb->disp_flags.Any({NWidgetDisplayFlag::ScrollbarUp, NWidgetDisplayFlag::ScrollbarDown})) { if (_scroller_click_timeout == 1) { _scroller_click_timeout = 3; - if (sb->UpdatePosition(rtl == HasBit(sb->disp_flags, NDB_SCROLLBAR_UP) ? 1 : -1)) w->SetDirty(); + if (sb->UpdatePosition(rtl == sb->disp_flags.Test(NWidgetDisplayFlag::ScrollbarUp) ? 1 : -1)) w->SetDirty(); } return; }