1
0
Fork 0

Codechange: Use EnumBitSet for NWidgetDisplayFlags.

pull/13482/head
Peter Nelson 2025-02-05 19:50:23 +00:00 committed by Peter Nelson
parent 28eb5e05c8
commit 693a5f42b9
5 changed files with 47 additions and 55 deletions

View File

@ -132,7 +132,7 @@ struct DropdownWindow : Window {
/* Set flag on parent widget to indicate that we have just closed. */ /* Set flag on parent widget to indicate that we have just closed. */
NWidgetCore *nwc = this->parent->GetWidget<NWidgetCore>(this->parent_button); NWidgetCore *nwc = this->parent->GetWidget<NWidgetCore>(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 void OnFocusLost(bool closing) override
@ -410,7 +410,7 @@ void ShowDropDownList(Window *w, DropDownList &&list, int selected, WidgetID but
Colours wi_colour = nwi->colour; Colours wi_colour = nwi->colour;
if ((nwi->type & WWT_MASK) == NWID_BUTTON_DROPDOWN) { if ((nwi->type & WWT_MASK) == NWID_BUTTON_DROPDOWN) {
nwi->disp_flags |= ND_DROPDOWN_ACTIVE; nwi->disp_flags.Set(NWidgetDisplayFlag::DropdownActive);
} else { } else {
nwi->SetLowered(true); nwi->SetLowered(true);
} }

View File

@ -396,11 +396,11 @@ struct NewsWindow : Window {
} else { } else {
nvp->InitializeViewport(this, GetReferenceTile(ni->reftype1, ni->ref1), ScaleZoomGUI(ZOOM_LVL_NEWS)); 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) { 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) { } else if (this->ni->flags & NF_SHADE) {
nvp->disp_flags |= ND_SHADE_DIMMED; nvp->disp_flags.Set(NWidgetDisplayFlag::ShadeDimmed);
} }
} }

View File

@ -194,7 +194,7 @@ static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, in
} }
if (pos < mi + button_size) { if (pos < mi + button_size) {
/* Pressing the upper button? */ /* Pressing the upper button? */
SetBit(sb->disp_flags, NDB_SCROLLBAR_UP); sb->disp_flags.Set(NWidgetDisplayFlag::ScrollbarUp);
if (_scroller_click_timeout <= 1) { if (_scroller_click_timeout <= 1) {
_scroller_click_timeout = 3; _scroller_click_timeout = 3;
changed = sb->UpdatePosition(rtl ? 1 : -1); 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(); w->mouse_capture_widget = sb->GetIndex();
} else if (pos >= ma - button_size) { } else if (pos >= ma - button_size) {
/* Pressing the lower button? */ /* Pressing the lower button? */
SetBit(sb->disp_flags, NDB_SCROLLBAR_DOWN); sb->disp_flags.Set(NWidgetDisplayFlag::ScrollbarDown);
if (_scroller_click_timeout <= 1) { if (_scroller_click_timeout <= 1) {
_scroller_click_timeout = 3; _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->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; TransparencyOptionBits to_backup = _transparency_opt;
_transparency_opt &= (1 << TO_SIGNS) | (1 << TO_TEXT); // Disable all transparency, except textual stuff _transparency_opt &= (1 << TO_SIGNS) | (1 << TO_TEXT); // Disable all transparency, except textual stuff
w->DrawViewport(); w->DrawViewport();
@ -2411,8 +2411,8 @@ void NWidgetViewport::Draw(const Window *w)
} }
/* Optionally shade the viewport. */ /* Optionally shade the viewport. */
if (this->disp_flags & (ND_SHADE_GREY | ND_SHADE_DIMMED)) { if (this->disp_flags.Any({NWidgetDisplayFlag::ShadeGrey, NWidgetDisplayFlag::ShadeDimmed})) {
GfxFillRect(this->GetCurrentRect(), (this->disp_flags & ND_SHADE_DIMMED) ? PALETTE_TO_TRANSPARENT : PALETTE_NEWSPAPER, FILLRECT_RECOLOUR); GfxFillRect(this->GetCurrentRect(), this->disp_flags.Test(NWidgetDisplayFlag::ShadeDimmed) ? PALETTE_TO_TRANSPARENT : PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
} }
DrawOutline(w, this); DrawOutline(w, this);
@ -2626,9 +2626,9 @@ void NWidgetScrollbar::Draw(const Window *w)
const DrawPixelInfo *dpi = _cur_dpi; 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; 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 up_lowered = this->disp_flags.Test(NWidgetDisplayFlag::ScrollbarUp);
bool down_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_DOWN); bool down_lowered = this->disp_flags.Test(NWidgetDisplayFlag::ScrollbarDown);
bool middle_lowered = !(this->disp_flags & ND_SCROLLBAR_BTN) && w->mouse_capture_widget == this->index; bool middle_lowered = !this->disp_flags.Any({NWidgetDisplayFlag::ScrollbarUp, NWidgetDisplayFlag::ScrollbarDown}) && w->mouse_capture_widget == this->index;
if (this->type == NWID_HSCROLLBAR) { if (this->type == NWID_HSCROLLBAR) {
DrawHorizontalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this); 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_BUTTON_DROPDOWN:
case NWID_PUSHBUTTON_DROPDOWN: case NWID_PUSHBUTTON_DROPDOWN:
if (this->index >= 0) w->SetStringParameters(this->index); 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; break;
default: default:

View File

@ -323,36 +323,28 @@ public:
}; };
/** Nested widget flags that affect display and interaction with 'real' widgets. */ /** Nested widget flags that affect display and interaction with 'real' widgets. */
enum NWidgetDisplay : uint16_t { enum NWidgetDisplayFlag : uint8_t {
/* Generic. */ /* Generic. */
NDB_LOWERED = 0, ///< Widget is lowered (pressed down) bit. Lowered, ///< Widget is lowered (pressed down) bit.
NDB_DISABLED = 1, ///< Widget is disabled (greyed out) bit. Disabled, ///< 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.
ND_LOWERED = 1 << NDB_LOWERED, ///< Bit value of the lowered flag. /* Viewport widget. */
ND_DISABLED = 1 << NDB_DISABLED, ///< Bit value of the disabled flag. NoTransparency, ///< Viewport is never transparent.
ND_HIGHLIGHT = 1 << NDB_HIGHLIGHT, ///< Bit value of the highlight flag. ShadeGrey, ///< Shade viewport to grey-scale.
ND_NO_TRANSPARENCY = 1 << NDB_NO_TRANSPARENCY, ///< Bit value of the 'no transparency' flag. ShadeDimmed, ///< Display dimmed colours in the viewport.
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. /* Button dropdown widget. */
ND_DROPDOWN_ACTIVE = 1 << NDB_DROPDOWN_ACTIVE, ///< Bit value of the 'dropdown active' flag. DropdownActive, ///< Dropdown menu of the button dropdown widget is active. @see #NWID_BUTTON_DROPDOWN
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. /* Scrollbar widget. */
ND_SCROLLBAR_BTN = ND_SCROLLBAR_UP | ND_SCROLLBAR_DOWN, ///< Bit value of the 'scrollbar up' or 'scrollbar down' flag. ScrollbarUp, ///< Up-button is lowered bit.
ND_DROPDOWN_CLOSED = 1 << NDB_DROPDOWN_CLOSED, ///< Bit value of the 'dropdown closed' flag. 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<NWidgetDisplayFlag, uint16_t>;
/** Container with the data associated to a single widget. */ /** Container with the data associated to a single widget. */
struct WidgetData { struct WidgetData {
@ -397,7 +389,7 @@ public:
TextColour GetHighlightColour() const override; TextColour GetHighlightColour() const override;
void SetHighlighted(TextColour highlight_colour) 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. Colours colour; ///< Colour of this widget.
protected: protected:
const WidgetID index; ///< Index of the nested widget (\c -1 means 'not used'). 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) 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; this->highlight_colour = highlight_colour;
} }
/** Return whether the widget is highlighted. */ /** Return whether the widget is highlighted. */
inline bool NWidgetCore::IsHighlighted() const 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. */ /** Return the colour of the highlight. */
@ -441,13 +433,13 @@ inline TextColour NWidgetCore::GetHighlightColour() const
*/ */
inline void NWidgetCore::SetLowered(bool lowered) 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. */ /** Return whether the widget is lowered. */
inline bool NWidgetCore::IsLowered() const 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) 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. */ /** Return whether the widget is disabled. */
inline bool NWidgetCore::IsDisabled() const inline bool NWidgetCore::IsDisabled() const
{ {
return HasBit(this->disp_flags, NDB_DISABLED); return this->disp_flags.Test(NWidgetDisplayFlag::Disabled);
} }

View File

@ -296,7 +296,7 @@ void Window::OnDropdownClose(Point pt, WidgetID widget, int index, bool instant_
/* Raise the dropdown button */ /* Raise the dropdown button */
NWidgetCore *nwi2 = this->GetWidget<NWidgetCore>(widget); NWidgetCore *nwi2 = this->GetWidget<NWidgetCore>(widget);
if ((nwi2->type & WWT_MASK) == NWID_BUTTON_DROPDOWN) { if ((nwi2->type & WWT_MASK) == NWID_BUTTON_DROPDOWN) {
nwi2->disp_flags &= ~ND_DROPDOWN_ACTIVE; nwi2->disp_flags.Reset(NWidgetDisplayFlag::DropdownActive);
} else { } else {
this->RaiseWidget(widget); 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; WidgetType widget_type = (nw != nullptr) ? nw->type : WWT_EMPTY;
/* Allow dropdown close flag detection to work. */ /* 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; bool focused_widget_changed = false;
/* If clicked on a window that previously did not have focus */ /* 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. */ /* 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); 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; NWidgetBase *nwid = pair.second;
if (nwid->type == NWID_HSCROLLBAR || nwid->type == NWID_VSCROLLBAR) { if (nwid->type == NWID_HSCROLLBAR || nwid->type == NWID_VSCROLLBAR) {
NWidgetScrollbar *sb = static_cast<NWidgetScrollbar*>(nwid); NWidgetScrollbar *sb = static_cast<NWidgetScrollbar*>(nwid);
if (sb->disp_flags & (ND_SCROLLBAR_UP | ND_SCROLLBAR_DOWN)) { if (sb->disp_flags.Any({NWidgetDisplayFlag::ScrollbarUp, NWidgetDisplayFlag::ScrollbarDown})) {
sb->disp_flags &= ~(ND_SCROLLBAR_UP | ND_SCROLLBAR_DOWN); sb->disp_flags.Reset(NWidgetDisplayFlag::ScrollbarUp).Reset(NWidgetDisplayFlag::ScrollbarDown);
w->mouse_capture_widget = -1; w->mouse_capture_widget = -1;
sb->SetDirty(w); sb->SetDirty(w);
} }
@ -2297,10 +2297,10 @@ static void HandleScrollbarScrolling(Window *w)
i = _cursor.pos.y - _cursorpos_drag_start.y; 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) { if (_scroller_click_timeout == 1) {
_scroller_click_timeout = 3; _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; return;
} }