1
0
Fork 0

Codechange: Allow all widget types to have a WidgetID index.

pull/14305/head
Peter Nelson 2025-05-25 09:51:46 +01:00 committed by Peter Nelson
parent 31fbb17c5b
commit 82f5af7ecd
4 changed files with 41 additions and 93 deletions

View File

@ -1713,6 +1713,7 @@ public:
void FillWidgetLookup(WidgetLookup &widget_lookup) override
{
this->NWidgetBase::FillWidgetLookup(widget_lookup);
this->avs->FillWidgetLookup(widget_lookup);
this->acs->FillWidgetLookup(widget_lookup);
this->inf->FillWidgetLookup(widget_lookup);

View File

@ -227,7 +227,7 @@ static constexpr NWidgetPart _nested_social_plugins_none_widgets[] = {
class NWidgetSocialPlugins : public NWidgetVertical {
public:
NWidgetSocialPlugins()
NWidgetSocialPlugins() : NWidgetVertical({}, WID_GO_SOCIAL_PLUGINS)
{
this->plugins = SocialIntegration::GetPlugins();
@ -244,12 +244,6 @@ public:
this->SetPIP(0, WidgetDimensions::unscaled.vsep_wide, 0);
}
void FillWidgetLookup(WidgetLookup &widget_lookup) override
{
widget_lookup[WID_GO_SOCIAL_PLUGINS] = this;
NWidgetVertical::FillWidgetLookup(widget_lookup);
}
void SetupSmallestSize(Window *w) override
{
this->current_index = -1;

View File

@ -923,6 +923,10 @@ static void DrawOutline(const Window *, const NWidgetBase *wid)
* Fill the Window::widget_lookup with pointers to nested widgets in the tree.
* @param widget_lookup The WidgetLookup.
*/
void NWidgetBase::FillWidgetLookup(WidgetLookup &widget_lookup)
{
if (this->index >= 0) widget_lookup[this->index] = this;
}
/**
* @fn void NWidgetBase::Draw(const Window *w)
@ -985,7 +989,7 @@ void NWidgetBase::AdjustPaddingForZoom()
* @param fill_x Horizontal fill step size, \c 0 means no filling is allowed.
* @param fill_y Vertical fill step size, \c 0 means no filling is allowed.
*/
NWidgetResizeBase::NWidgetResizeBase(WidgetType tp, uint fill_x, uint fill_y) : NWidgetBase(tp)
NWidgetResizeBase::NWidgetResizeBase(WidgetType tp, WidgetID index, uint fill_x, uint fill_y) : NWidgetBase(tp, index)
{
this->fill_x = fill_x;
this->fill_y = fill_y;
@ -1146,7 +1150,7 @@ void NWidgetResizeBase::AssignSizePosition(SizingType sizing, int x, int y, uint
* @param widget_data Data component of the widget. @see Widget::data
* @param tool_tip Tool tip of the widget. @see Widget::tooltips
*/
NWidgetCore::NWidgetCore(WidgetType tp, Colours colour, WidgetID index, uint fill_x, uint fill_y, const WidgetData &widget_data, StringID tool_tip) : NWidgetResizeBase(tp, fill_x, fill_y), index(index)
NWidgetCore::NWidgetCore(WidgetType tp, Colours colour, WidgetID index, uint fill_x, uint fill_y, const WidgetData &widget_data, StringID tool_tip) : NWidgetResizeBase(tp, index, fill_x, fill_y)
{
this->colour = colour;
this->widget_data = widget_data;
@ -1260,15 +1264,6 @@ StringID NWidgetCore::GetString() const
return this->widget_data.string;
}
/**
* Get the \c WidgetID of this nested widget.
* @return The \c WidgetID.
*/
WidgetID NWidgetCore::GetIndex() const
{
return this->index;
}
/**
* Get the \c WidgetID of this nested widget's scrollbar.
* @return The \c WidgetID.
@ -1278,11 +1273,6 @@ WidgetID NWidgetCore::GetScrollbarIndex() const
return this->scrollbar_index;
}
void NWidgetCore::FillWidgetLookup(WidgetLookup &widget_lookup)
{
if (this->index >= 0) widget_lookup[this->index] = this;
}
NWidgetCore *NWidgetCore::GetWidgetFromPos(int x, int y)
{
return (IsInsideBS(x, this->pos_x, this->current_x) && IsInsideBS(y, this->pos_y, this->current_y)) ? this : nullptr;
@ -1319,6 +1309,7 @@ void NWidgetContainer::Add(std::unique_ptr<NWidgetBase> &&wid)
void NWidgetContainer::FillWidgetLookup(WidgetLookup &widget_lookup)
{
this->NWidgetBase::FillWidgetLookup(widget_lookup);
for (const auto &child_wid : this->children) {
child_wid->FillWidgetLookup(widget_lookup);
}
@ -1410,8 +1401,7 @@ void NWidgetStacked::FillWidgetLookup(WidgetLookup &widget_lookup)
/* We need to update widget_lookup later. */
this->widget_lookup = &widget_lookup;
if (this->index >= 0) widget_lookup[this->index] = this;
NWidgetContainer::FillWidgetLookup(widget_lookup);
this->NWidgetContainer::FillWidgetLookup(widget_lookup);
/* In case widget IDs are repeated, make sure Window::GetWidget works on displayed widgets. */
if (static_cast<size_t>(this->shown_plane) < this->children.size()) this->children[shown_plane]->FillWidgetLookup(widget_lookup);
}
@ -1451,14 +1441,12 @@ bool NWidgetStacked::SetDisplayedPlane(int plane)
class NWidgetLayer : public NWidgetContainer {
public:
NWidgetLayer(WidgetID index) : NWidgetContainer(NWID_LAYER), index(index) {}
NWidgetLayer(WidgetID index) : NWidgetContainer(NWID_LAYER, index) {}
void SetupSmallestSize(Window *w) override;
void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override;
void Draw(const Window *w) override;
const WidgetID index; ///< If non-negative, index in the #Window::widget_lookup.
};
void NWidgetLayer::SetupSmallestSize(Window *w)
@ -1511,11 +1499,6 @@ void NWidgetLayer::Draw(const Window *w)
DrawOutline(w, this);
}
NWidgetPIPContainer::NWidgetPIPContainer(WidgetType tp, NWidContainerFlags flags) : NWidgetContainer(tp)
{
this->flags = flags;
}
void NWidgetPIPContainer::AdjustPaddingForZoom()
{
this->pip_pre = ScaleGUITrad(this->uz_pip_pre);
@ -1560,11 +1543,6 @@ void NWidgetPIPContainer::SetPIPRatio(uint8_t pip_ratio_pre, uint8_t pip_ratio_i
this->pip_ratio_post = pip_ratio_post;
}
/** Horizontal container widget. */
NWidgetHorizontal::NWidgetHorizontal(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_HORIZONTAL, flags)
{
}
void NWidgetHorizontal::SetupSmallestSize(Window *w)
{
this->smallest_x = 0; // Sum of minimal size of all children.
@ -1743,22 +1721,11 @@ void NWidgetHorizontal::AssignSizePosition(SizingType sizing, int x, int y, uint
}
}
/** Horizontal left-to-right container widget. */
NWidgetHorizontalLTR::NWidgetHorizontalLTR(NWidContainerFlags flags) : NWidgetHorizontal(flags)
{
this->type = NWID_HORIZONTAL_LTR;
}
void NWidgetHorizontalLTR::AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool)
{
NWidgetHorizontal::AssignSizePosition(sizing, x, y, given_width, given_height, false);
}
/** Vertical container widget. */
NWidgetVertical::NWidgetVertical(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_VERTICAL, flags)
{
}
void NWidgetVertical::SetupSmallestSize(Window *w)
{
this->smallest_x = 0; // Biggest child.
@ -1931,7 +1898,7 @@ void NWidgetVertical::AssignSizePosition(SizingType sizing, int x, int y, uint g
* @param width Horizontal size of the spacer widget.
* @param height Vertical size of the spacer widget.
*/
NWidgetSpacer::NWidgetSpacer(int width, int height) : NWidgetResizeBase(NWID_SPACER, 0, 0)
NWidgetSpacer::NWidgetSpacer(int width, int height) : NWidgetResizeBase(NWID_SPACER, -1, 0, 0)
{
this->SetMinimalSize(width, height);
this->SetResize(0, 0);
@ -1944,10 +1911,6 @@ void NWidgetSpacer::SetupSmallestSize(Window *)
this->ApplyAspectRatio();
}
void NWidgetSpacer::FillWidgetLookup(WidgetLookup &)
{
}
void NWidgetSpacer::Draw(const Window *w)
{
/* Spacer widget is never normally visible. */
@ -1970,11 +1933,6 @@ NWidgetCore *NWidgetSpacer::GetWidgetFromPos(int, int)
return nullptr;
}
NWidgetMatrix::NWidgetMatrix(Colours colour, WidgetID index) : NWidgetPIPContainer(NWID_MATRIX, NWidContainerFlag::EqualSize), index(index)
{
this->colour = colour;
}
/**
* Sets the clicked element in the matrix.
* @param clicked The clicked element.
@ -2080,12 +2038,6 @@ void NWidgetMatrix::AssignSizePosition(SizingType, int x, int y, uint given_widt
this->SetCount(this->count);
}
void NWidgetMatrix::FillWidgetLookup(WidgetLookup &widget_lookup)
{
if (this->index >= 0) widget_lookup[this->index] = this;
NWidgetContainer::FillWidgetLookup(widget_lookup);
}
NWidgetCore *NWidgetMatrix::GetWidgetFromPos(int x, int y)
{
/* Falls outside of the matrix widget. */
@ -2351,7 +2303,7 @@ void NWidgetBackground::AssignSizePosition(SizingType sizing, int x, int y, uint
void NWidgetBackground::FillWidgetLookup(WidgetLookup &widget_lookup)
{
if (this->index >= 0) widget_lookup[this->index] = this;
this->NWidgetCore::FillWidgetLookup(widget_lookup);
if (this->child != nullptr) this->child->FillWidgetLookup(widget_lookup);
}
@ -3312,9 +3264,9 @@ static std::unique_ptr<NWidgetBase> MakeNWidget(const NWidgetPart &nwid)
case WWT_INSET: [[fallthrough]];
case WWT_FRAME: return std::make_unique<NWidgetBackground>(nwid.type, nwid.u.widget.colour, nwid.u.widget.index);
case NWID_HORIZONTAL: return std::make_unique<NWidgetHorizontal>(nwid.u.cont_flags);
case NWID_HORIZONTAL_LTR: return std::make_unique<NWidgetHorizontalLTR>(nwid.u.cont_flags);
case NWID_VERTICAL: return std::make_unique<NWidgetVertical>(nwid.u.cont_flags);
case NWID_HORIZONTAL: return std::make_unique<NWidgetHorizontal>(nwid.u.container.flags, nwid.u.container.index);
case NWID_HORIZONTAL_LTR: return std::make_unique<NWidgetHorizontalLTR>(nwid.u.container.flags, nwid.u.container.index);
case NWID_VERTICAL: return std::make_unique<NWidgetVertical>(nwid.u.container.flags, nwid.u.container.index);
case NWID_SELECTION: return std::make_unique<NWidgetStacked>(nwid.u.widget.index);
case NWID_MATRIX: return std::make_unique<NWidgetMatrix>(nwid.u.widget.colour, nwid.u.widget.index);
case NWID_VIEWPORT: return std::make_unique<NWidgetViewport>(nwid.u.widget.index);

View File

@ -136,7 +136,7 @@ using WidgetLookup = std::map<WidgetID, class NWidgetBase *>;
*/
class NWidgetBase {
public:
NWidgetBase(WidgetType tp) : type(tp) {}
NWidgetBase(WidgetType tp, WidgetID index = -1) : type(tp), index(index) {}
virtual ~NWidgetBase() = default;
void ApplyAspectRatio();
@ -144,7 +144,7 @@ public:
virtual void SetupSmallestSize(Window *w) = 0;
virtual void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) = 0;
virtual void FillWidgetLookup(WidgetLookup &widget_lookup) = 0;
virtual void FillWidgetLookup(WidgetLookup &widget_lookup);
virtual NWidgetCore *GetWidgetFromPos(int x, int y) = 0;
virtual NWidgetBase *GetWidgetOfType(WidgetType tp);
@ -177,6 +177,7 @@ public:
return nullptr;
}
inline WidgetID GetIndex() const { return this->index; }
virtual bool IsHighlighted() const { return false; }
virtual TextColour GetHighlightColour() const { return TC_INVALID; }
virtual void SetHighlighted([[maybe_unused]] TextColour highlight_colour) {}
@ -248,6 +249,8 @@ public:
NWidgetBase *parent = nullptr; ///< Parent widget of this widget, automatically filled in when added to container.
protected:
const WidgetID index = -1; ///< Index of the nested widget (\c -1 means 'not used').
inline void StoreSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height);
};
@ -296,7 +299,7 @@ inline void NWidgetBase::StoreSizePosition(SizingType sizing, int x, int y, uint
*/
class NWidgetResizeBase : public NWidgetBase {
public:
NWidgetResizeBase(WidgetType tp, uint fill_x, uint fill_y);
NWidgetResizeBase(WidgetType tp, WidgetID index, uint fill_x, uint fill_y);
void AdjustPaddingForZoom() override;
void SetMinimalSize(uint min_x, uint min_y);
@ -379,7 +382,6 @@ public:
void SetAlignment(StringAlignment align);
StringID GetString() const;
WidgetID GetIndex() const;
WidgetID GetScrollbarIndex() const;
inline void SetLowered(bool lowered);
@ -390,7 +392,6 @@ public:
inline TextColour GetTextColour() const { return this->text_colour; }
inline FontSize GetFontSize() const { return this->text_size; }
void FillWidgetLookup(WidgetLookup &widget_lookup) override;
NWidgetCore *GetWidgetFromPos(int x, int y) override;
bool IsHighlighted() const override;
TextColour GetHighlightColour() const override;
@ -399,7 +400,6 @@ public:
NWidgetDisplayFlags disp_flags; ///< Flags that affect display and interaction with the widget.
Colours colour; ///< Colour of this widget.
protected:
const WidgetID index = -1; ///< Index of the nested widget (\c -1 means 'not used').
WidgetData widget_data{}; ///< Data of the widget. @see Widget::data
StringID tool_tip{}; ///< Tooltip of the widget. @see Widget::tool_tips
WidgetID scrollbar_index = -1; ///< Index of an attached scrollbar.
@ -471,7 +471,7 @@ inline bool NWidgetCore::IsDisabled() const
*/
class NWidgetContainer : public NWidgetBase {
public:
NWidgetContainer(WidgetType tp) : NWidgetBase(tp) { }
NWidgetContainer(WidgetType tp, WidgetID index = -1) : NWidgetBase(tp, index) {}
void AdjustPaddingForZoom() override;
void Add(std::unique_ptr<NWidgetBase> &&wid);
@ -510,7 +510,7 @@ enum StackedZeroSizePlanes : int {
*/
class NWidgetStacked : public NWidgetContainer {
public:
NWidgetStacked(WidgetID index) : NWidgetContainer(NWID_SELECTION), index(index) {}
NWidgetStacked(WidgetID index) : NWidgetContainer(NWID_SELECTION, index) {}
void SetupSmallestSize(Window *w) override;
void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override;
@ -522,7 +522,6 @@ public:
bool SetDisplayedPlane(int plane);
int shown_plane = 0; ///< Plane being displayed (for #NWID_SELECTION only).
const WidgetID index = -1; ///< If non-negative, index in the #Window::widget_lookup.
private:
WidgetLookup *widget_lookup = nullptr; ///< Window's widget lookup, updated in SetDisplayedPlane().
};
@ -537,7 +536,7 @@ using NWidContainerFlags = EnumBitSet<NWidContainerFlag, uint8_t>;
/** Container with pre/inter/post child space. */
class NWidgetPIPContainer : public NWidgetContainer {
public:
NWidgetPIPContainer(WidgetType tp, NWidContainerFlags flags = {});
NWidgetPIPContainer(WidgetType tp, NWidContainerFlags flags = {}, WidgetID index = -1) : NWidgetContainer(tp, index), flags(flags) {}
void AdjustPaddingForZoom() override;
void SetPIP(uint8_t pip_pre, uint8_t pip_inter, uint8_t pip_post);
@ -565,7 +564,7 @@ protected:
*/
class NWidgetHorizontal : public NWidgetPIPContainer {
public:
NWidgetHorizontal(NWidContainerFlags flags = {});
NWidgetHorizontal(NWidContainerFlags flags = {}, WidgetID index = -1, WidgetType type = NWID_HORIZONTAL) : NWidgetPIPContainer(type, flags, index) {}
void SetupSmallestSize(Window *w) override;
void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override;
@ -577,7 +576,7 @@ public:
*/
class NWidgetHorizontalLTR : public NWidgetHorizontal {
public:
NWidgetHorizontalLTR(NWidContainerFlags flags = {});
NWidgetHorizontalLTR(NWidContainerFlags flags = {}, WidgetID index = -1) : NWidgetHorizontal(flags, index, NWID_HORIZONTAL_LTR) {}
void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override;
};
@ -588,7 +587,7 @@ public:
*/
class NWidgetVertical : public NWidgetPIPContainer {
public:
NWidgetVertical(NWidContainerFlags flags = {});
NWidgetVertical(NWidContainerFlags flags = {}, WidgetID index = -1) : NWidgetPIPContainer(NWID_VERTICAL, flags, index) {}
void SetupSmallestSize(Window *w) override;
void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override;
@ -604,7 +603,7 @@ public:
*/
class NWidgetMatrix : public NWidgetPIPContainer {
public:
NWidgetMatrix(Colours colour, WidgetID index);
NWidgetMatrix(Colours colour, WidgetID index) : NWidgetPIPContainer(NWID_MATRIX, NWidContainerFlag::EqualSize, index), colour(colour) {}
void SetClicked(int clicked);
void SetCount(int count);
@ -613,15 +612,13 @@ public:
void SetupSmallestSize(Window *w) override;
void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override;
void FillWidgetLookup(WidgetLookup &widget_lookup) override;
NWidgetCore *GetWidgetFromPos(int x, int y) override;
void Draw(const Window *w) override;
protected:
const WidgetID index = -1; ///< If non-negative, index in the #Window::widget_lookup.
Colours colour{}; ///< Colour of this widget.
int clicked = -1; ///< The currently clicked element.
int count = -1; ///< Amount of valid elements.
int count = 0; ///< Amount of valid elements.
int current_element = 0; ///< The element currently being processed.
Scrollbar *sb = nullptr; ///< The scrollbar we're associated with.
private:
@ -643,7 +640,6 @@ public:
NWidgetSpacer(int width, int height);
void SetupSmallestSize(Window *w) override;
void FillWidgetLookup(WidgetLookup &widget_lookup) override;
void Draw(const Window *w) override;
void SetDirty(const Window *w) const override;
@ -1023,6 +1019,11 @@ struct NWidgetPartWidget {
WidgetID index; ///< Index of the widget.
};
struct NWidgetPartContainer {
NWidContainerFlags flags;
WidgetID index;
};
/**
* Widget part for storing padding.
* @ingroup NestedWidgetParts
@ -1074,7 +1075,7 @@ struct NWidgetPartAspect {
* Pointer to function returning a nested widget.
* @return Nested widget (tree).
*/
typedef std::unique_ptr<NWidgetBase> NWidgetFunctionType();
using NWidgetFunctionType = std::unique_ptr<NWidgetBase>();
/**
* Partial widget specification to allow NWidgets to be written nested.
@ -1092,7 +1093,7 @@ struct NWidgetPart {
NWidgetPartTextStyle text_style; ///< Part with text style data.
NWidgetPartAlignment align; ///< Part with internal alignment.
NWidgetFunctionType *func_ptr; ///< Part with a function call.
NWidContainerFlags cont_flags; ///< Part with container flags.
NWidgetPartContainer container; ///< Part with container flags.
NWidgetPartAspect aspect; ///< Part to set aspect ratio.
/* Constructors for each NWidgetPartUnion data type. */
@ -1106,7 +1107,7 @@ struct NWidgetPart {
constexpr NWidgetPartUnion(NWidgetPartTextStyle text_style) : text_style(text_style) {}
constexpr NWidgetPartUnion(NWidgetPartAlignment align) : align(align) {}
constexpr NWidgetPartUnion(NWidgetFunctionType *func_ptr) : func_ptr(func_ptr) {}
constexpr NWidgetPartUnion(NWidContainerFlags cont_flags) : cont_flags(cont_flags) {}
constexpr NWidgetPartUnion(NWidgetPartContainer container) : container(container) {}
constexpr NWidgetPartUnion(NWidgetPartAspect aspect) : aspect(aspect) {}
} u;
@ -1121,7 +1122,7 @@ struct NWidgetPart {
constexpr NWidgetPart(WidgetType type, NWidgetPartTextStyle text_style) : type(type), u(text_style) {}
constexpr NWidgetPart(WidgetType type, NWidgetPartAlignment align) : type(type), u(align) {}
constexpr NWidgetPart(WidgetType type, NWidgetFunctionType *func_ptr) : type(type), u(func_ptr) {}
constexpr NWidgetPart(WidgetType type, NWidContainerFlags cont_flags) : type(type), u(cont_flags) {}
constexpr NWidgetPart(WidgetType type, NWidgetPartContainer container) : type(type), u(container) {}
constexpr NWidgetPart(WidgetType type, NWidgetPartAspect aspect) : type(type), u(aspect) {}
};
@ -1402,9 +1403,9 @@ constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx = -1)
* @param cont_flags Flags for the containers (#NWID_HORIZONTAL and #NWID_VERTICAL).
* @ingroup NestedWidgetParts
*/
constexpr NWidgetPart NWidget(WidgetType tp, NWidContainerFlags cont_flags = {})
constexpr NWidgetPart NWidget(WidgetType tp, NWidContainerFlags cont_flags = {}, WidgetID idx = -1)
{
return NWidgetPart{tp, NWidContainerFlags{cont_flags}};
return NWidgetPart{tp, NWidgetPartContainer{cont_flags, idx}};
}
/**