From feb94d233d8fdceff193a4c59298960d8148d470 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Fri, 29 Dec 2023 14:27:04 +0000 Subject: [PATCH] Codechange: Remove deferred nested_array initialization path. (#11640) Having two ways (`FillNestedArray` and `SetupSmallestSize`) to initialize `Window::nested_array` introduces confusion. Instead, make `FillNestedArray` the canonical way, always call it, and remove init_array from `SetupSmallestSize`. --- src/fios_gui.cpp | 2 +- src/network/network_gui.cpp | 4 +-- src/newgrf_gui.cpp | 8 ++--- src/smallmap_gui.cpp | 6 ++-- src/toolbar_gui.cpp | 8 ++--- src/widget.cpp | 58 ++++++++++--------------------------- src/widget_type.h | 20 ++++++------- src/window.cpp | 23 +++++---------- src/window_gui.h | 2 +- 9 files changed, 48 insertions(+), 83 deletions(-) diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index c4bbb7aef5..fb140bb138 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -326,7 +326,7 @@ public: this->querystrings[WID_SL_SAVE_OSK_TITLE] = &this->filename_editbox; this->filename_editbox.ok_button = WID_SL_SAVE_GAME; - this->CreateNestedTree(true); + this->CreateNestedTree(); if (this->fop == SLO_LOAD && this->abstract_filetype == FT_SAVEGAME) { this->GetWidget(WID_SL_CONTENT_DOWNLOAD_SEL)->SetDisplayedPlane(SZSP_HORIZONTAL); } diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index 669de4954c..4f0890f6ad 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -106,7 +106,7 @@ public: this->Add(leaf); } - void SetupSmallestSize(Window *w, bool init_array) override + void SetupSmallestSize(Window *w) override { this->smallest_y = 0; // Biggest child. this->fill_x = 1; @@ -116,7 +116,7 @@ public: /* First initialise some variables... */ for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) { - child_wid->SetupSmallestSize(w, init_array); + child_wid->SetupSmallestSize(w); this->smallest_y = std::max(this->smallest_y, child_wid->smallest_y + child_wid->padding.Vertical()); } diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index f3f72fa80c..472dfca745 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -1623,16 +1623,16 @@ public: this->editable = true; // Temporary setting, 'real' value is set in SetupSmallestSize(). } - void SetupSmallestSize(Window *w, bool init_array) override + void SetupSmallestSize(Window *w) override { /* Copy state flag from the window. */ assert(dynamic_cast(w) != nullptr); NewGRFWindow *ngw = (NewGRFWindow *)w; this->editable = ngw->editable; - this->avs->SetupSmallestSize(w, init_array); - this->acs->SetupSmallestSize(w, init_array); - this->inf->SetupSmallestSize(w, init_array); + this->avs->SetupSmallestSize(w); + this->acs->SetupSmallestSize(w); + this->inf->SetupSmallestSize(w); uint min_avs_width = this->avs->smallest_x + this->avs->padding.Horizontal(); uint min_acs_width = this->acs->smallest_x + this->acs->padding.Horizontal(); diff --git a/src/smallmap_gui.cpp b/src/smallmap_gui.cpp index 3d0c76ca7b..e1a1ffb2d5 100644 --- a/src/smallmap_gui.cpp +++ b/src/smallmap_gui.cpp @@ -1850,13 +1850,13 @@ public: this->smallmap_window = nullptr; } - void SetupSmallestSize(Window *w, bool init_array) override + void SetupSmallestSize(Window *w) override { NWidgetBase *display = this->head; NWidgetBase *bar = display->next; - display->SetupSmallestSize(w, init_array); - bar->SetupSmallestSize(w, init_array); + display->SetupSmallestSize(w); + bar->SetupSmallestSize(w); this->smallmap_window = dynamic_cast(w); assert(this->smallmap_window != nullptr); diff --git a/src/toolbar_gui.cpp b/src/toolbar_gui.cpp index b4e3f4ed77..4a9081e22a 100644 --- a/src/toolbar_gui.cpp +++ b/src/toolbar_gui.cpp @@ -1335,7 +1335,7 @@ public: return type == WWT_IMGBTN || type == WWT_IMGBTN_2 || type == WWT_PUSHIMGBTN; } - void SetupSmallestSize(Window *w, bool init_array) override + void SetupSmallestSize(Window *w) override { this->smallest_x = 0; // Biggest child this->smallest_y = 0; // Biggest child @@ -1348,7 +1348,7 @@ public: uint nbuttons = 0; /* First initialise some variables... */ for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) { - child_wid->SetupSmallestSize(w, init_array); + child_wid->SetupSmallestSize(w); this->smallest_y = std::max(this->smallest_y, child_wid->smallest_y + child_wid->padding.Vertical()); if (this->IsButton(child_wid->type)) { nbuttons++; @@ -1777,9 +1777,9 @@ class NWidgetMainToolbarContainer : public NWidgetToolbarContainer { class NWidgetScenarioToolbarContainer : public NWidgetToolbarContainer { uint panel_widths[2]; ///< The width of the two panels (the text panel and date panel) - void SetupSmallestSize(Window *w, bool init_array) override + void SetupSmallestSize(Window *w) override { - this->NWidgetToolbarContainer::SetupSmallestSize(w, init_array); + this->NWidgetToolbarContainer::SetupSmallestSize(w); /* Find the size of panel_widths */ uint i = 0; diff --git a/src/widget.cpp b/src/widget.cpp index 84176fa837..71f367181a 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -995,7 +995,7 @@ NWidgetBase::NWidgetBase(WidgetType tp) : ZeroedMemoryAllocator() /* ~NWidgetContainer() takes care of #next and #prev data members. */ /** - * @fn void NWidgetBase::SetupSmallestSize(Window *w, bool init_array) + * @fn void NWidgetBase::SetupSmallestSize(Window *w) * Compute smallest size needed by the widget. * * The smallest size of a widget is the smallest size that a widget needs to @@ -1004,7 +1004,6 @@ NWidgetBase::NWidgetBase(WidgetType tp) : ZeroedMemoryAllocator() * background widget without child with a non-negative index. * * @param w Window owning the widget. - * @param init_array Initialize the \c w->nested_array. * * @note After the computation, the results can be queried by accessing the #smallest_x and #smallest_y data members of the widget. */ @@ -1394,13 +1393,8 @@ void NWidgetStacked::AdjustPaddingForZoom() NWidgetContainer::AdjustPaddingForZoom(); } -void NWidgetStacked::SetupSmallestSize(Window *w, bool init_array) +void NWidgetStacked::SetupSmallestSize(Window *w) { - if (this->index >= 0 && init_array) { // Fill w->nested_array[] - assert(w->nested_array_size > (uint)this->index); - w->nested_array[this->index] = this; - } - /* Zero size plane selected */ if (this->shown_plane >= SZSP_BEGIN) { Dimension size = {0, 0}; @@ -1427,7 +1421,7 @@ void NWidgetStacked::SetupSmallestSize(Window *w, bool init_array) this->resize_x = (this->head != nullptr) ? 1 : 0; this->resize_y = (this->head != nullptr) ? 1 : 0; for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) { - child_wid->SetupSmallestSize(w, init_array); + child_wid->SetupSmallestSize(w); this->smallest_x = std::max(this->smallest_x, child_wid->smallest_x + child_wid->padding.Horizontal()); this->smallest_y = std::max(this->smallest_y, child_wid->smallest_y + child_wid->padding.Vertical()); @@ -1560,7 +1554,7 @@ NWidgetHorizontal::NWidgetHorizontal(NWidContainerFlags flags) : NWidgetPIPConta { } -void NWidgetHorizontal::SetupSmallestSize(Window *w, bool init_array) +void NWidgetHorizontal::SetupSmallestSize(Window *w) { this->smallest_x = 0; // Sum of minimal size of all children. this->smallest_y = 0; // Biggest child. @@ -1574,7 +1568,7 @@ void NWidgetHorizontal::SetupSmallestSize(Window *w, bool init_array) uint longest = 0; // Longest child found. uint max_vert_fill = 0; // Biggest vertical fill step. for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) { - child_wid->SetupSmallestSize(w, init_array); + child_wid->SetupSmallestSize(w); longest = std::max(longest, child_wid->smallest_x); max_vert_fill = std::max(max_vert_fill, child_wid->GetVerticalStepSize(ST_SMALLEST)); this->smallest_y = std::max(this->smallest_y, child_wid->smallest_y + child_wid->padding.Vertical()); @@ -1752,7 +1746,7 @@ NWidgetVertical::NWidgetVertical(NWidContainerFlags flags) : NWidgetPIPContainer { } -void NWidgetVertical::SetupSmallestSize(Window *w, bool init_array) +void NWidgetVertical::SetupSmallestSize(Window *w) { this->smallest_x = 0; // Biggest child. this->smallest_y = 0; // Sum of minimal size of all children. @@ -1766,7 +1760,7 @@ void NWidgetVertical::SetupSmallestSize(Window *w, bool init_array) uint highest = 0; // Highest child found. uint max_hor_fill = 0; // Biggest horizontal fill step. for (NWidgetBase *child_wid = this->head; child_wid != nullptr; child_wid = child_wid->next) { - child_wid->SetupSmallestSize(w, init_array); + child_wid->SetupSmallestSize(w); highest = std::max(highest, child_wid->smallest_y); max_hor_fill = std::max(max_hor_fill, child_wid->GetHorizontalStepSize(ST_SMALLEST)); this->smallest_x = std::max(this->smallest_x, child_wid->smallest_x + child_wid->padding.Horizontal()); @@ -1925,7 +1919,7 @@ NWidgetSpacer::NWidgetSpacer(int width, int height) : NWidgetResizeBase(NWID_SPA this->SetResize(0, 0); } -void NWidgetSpacer::SetupSmallestSize(Window *, bool) +void NWidgetSpacer::SetupSmallestSize(Window *) { this->smallest_x = this->min_x; this->smallest_y = this->min_y; @@ -2021,21 +2015,16 @@ void NWidgetMatrix::SetScrollbar(Scrollbar *sb) this->sb = sb; } -void NWidgetMatrix::SetupSmallestSize(Window *w, bool init_array) +void NWidgetMatrix::SetupSmallestSize(Window *w) { assert(this->head != nullptr); assert(this->head->next == nullptr); - if (this->index >= 0 && init_array) { // Fill w->nested_array[] - assert(w->nested_array_size > (uint)this->index); - w->nested_array[this->index] = this; - } - /* Reset the widget number. */ NWidgetCore *nw = dynamic_cast(this->head); assert(nw != nullptr); SB(nw->index, 16, 16, 0); - this->head->SetupSmallestSize(w, init_array); + this->head->SetupSmallestSize(w); Dimension padding = { (uint)this->pip_pre + this->pip_post, (uint)this->pip_pre + this->pip_post}; Dimension size = {this->head->smallest_x + padding.width, this->head->smallest_y + padding.height}; @@ -2271,14 +2260,10 @@ void NWidgetBackground::AdjustPaddingForZoom() NWidgetCore::AdjustPaddingForZoom(); } -void NWidgetBackground::SetupSmallestSize(Window *w, bool init_array) +void NWidgetBackground::SetupSmallestSize(Window *w) { - if (init_array && this->index >= 0) { - assert(w->nested_array_size > (uint)this->index); - w->nested_array[this->index] = this; - } if (this->child != nullptr) { - this->child->SetupSmallestSize(w, init_array); + this->child->SetupSmallestSize(w); this->smallest_x = this->child->smallest_x; this->smallest_y = this->child->smallest_y; @@ -2418,12 +2403,8 @@ NWidgetViewport::NWidgetViewport(int index) : NWidgetCore(NWID_VIEWPORT, INVALID this->SetIndex(index); } -void NWidgetViewport::SetupSmallestSize(Window *w, bool init_array) +void NWidgetViewport::SetupSmallestSize(Window *) { - if (init_array && this->index >= 0) { - assert(w->nested_array_size > (uint)this->index); - w->nested_array[this->index] = this; - } this->smallest_x = this->min_x; this->smallest_y = this->min_y; } @@ -2599,12 +2580,8 @@ NWidgetScrollbar::NWidgetScrollbar(WidgetType tp, Colours colour, int index) : N } } -void NWidgetScrollbar::SetupSmallestSize(Window *w, bool init_array) +void NWidgetScrollbar::SetupSmallestSize(Window *) { - if (init_array && this->index >= 0) { - assert(w->nested_array_size > (uint)this->index); - w->nested_array[this->index] = this; - } this->min_x = 0; this->min_y = 0; @@ -2799,13 +2776,8 @@ NWidgetLeaf::NWidgetLeaf(WidgetType tp, Colours colour, int index, uint32_t data } } -void NWidgetLeaf::SetupSmallestSize(Window *w, bool init_array) +void NWidgetLeaf::SetupSmallestSize(Window *w) { - if (this->index >= 0 && init_array) { // Fill w->nested_array[] - assert(w->nested_array_size > (uint)this->index); - w->nested_array[this->index] = this; - } - Dimension padding = {0, 0}; Dimension size = {this->min_x, this->min_y}; Dimension fill = {this->fill_x, this->fill_y}; diff --git a/src/widget_type.h b/src/widget_type.h index 2280b09cbc..b4bf6da505 100644 --- a/src/widget_type.h +++ b/src/widget_type.h @@ -133,7 +133,7 @@ public: NWidgetBase(WidgetType tp); virtual void AdjustPaddingForZoom(); - virtual void SetupSmallestSize(Window *w, bool init_array) = 0; + 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 FillNestedArray(NWidgetBase **array, uint length) = 0; @@ -460,7 +460,7 @@ public: void SetIndex(int index); void AdjustPaddingForZoom() override; - void SetupSmallestSize(Window *w, bool init_array) override; + void SetupSmallestSize(Window *w) override; void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override; void FillNestedArray(NWidgetBase **array, uint length) override; @@ -517,7 +517,7 @@ class NWidgetHorizontal : public NWidgetPIPContainer { public: NWidgetHorizontal(NWidContainerFlags flags = NC_NONE); - void SetupSmallestSize(Window *w, bool init_array) override; + void SetupSmallestSize(Window *w) override; void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override; }; @@ -540,7 +540,7 @@ class NWidgetVertical : public NWidgetPIPContainer { public: NWidgetVertical(NWidContainerFlags flags = NC_NONE); - void SetupSmallestSize(Window *w, bool init_array) override; + void SetupSmallestSize(Window *w) override; void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override; }; @@ -562,7 +562,7 @@ public: void SetCount(int count); void SetScrollbar(Scrollbar *sb); - void SetupSmallestSize(Window *w, bool init_array) override; + void SetupSmallestSize(Window *w) override; void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override; void FillNestedArray(NWidgetBase **array, uint length) override; @@ -592,7 +592,7 @@ class NWidgetSpacer : public NWidgetResizeBase { public: NWidgetSpacer(int width, int height); - void SetupSmallestSize(Window *w, bool init_array) override; + void SetupSmallestSize(Window *w) override; void FillNestedArray(NWidgetBase **array, uint length) override; void Draw(const Window *w) override; @@ -614,7 +614,7 @@ public: void SetPIPRatio(uint8_t pip_ratio_pre, uint8_t pip_ratio_inter, uint8_t pip_ratio_post); void AdjustPaddingForZoom() override; - void SetupSmallestSize(Window *w, bool init_array) override; + void SetupSmallestSize(Window *w) override; void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override; void FillNestedArray(NWidgetBase **array, uint length) override; @@ -640,7 +640,7 @@ class NWidgetViewport : public NWidgetCore { public: NWidgetViewport(int index); - void SetupSmallestSize(Window *w, bool init_array) override; + void SetupSmallestSize(Window *w) override; void Draw(const Window *w) override; void InitializeViewport(Window *w, std::variant focus, ZoomLevel zoom); @@ -840,7 +840,7 @@ class NWidgetScrollbar : public NWidgetCore, public Scrollbar { public: NWidgetScrollbar(WidgetType tp, Colours colour, int index); - void SetupSmallestSize(Window *w, bool init_array) override; + void SetupSmallestSize(Window *w) override; void Draw(const Window *w) override; static void InvalidateDimensionCache(); @@ -860,7 +860,7 @@ class NWidgetLeaf : public NWidgetCore { public: NWidgetLeaf(WidgetType tp, Colours colour, int index, uint32_t data, StringID tip); - void SetupSmallestSize(Window *w, bool init_array) override; + void SetupSmallestSize(Window *w) override; void Draw(const Window *w) override; bool ButtonHit(const Point &pt); diff --git a/src/window.cpp b/src/window.cpp index 31e9f7fccf..41a55d41f3 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -963,8 +963,8 @@ void Window::ReInit(int rx, int ry, bool reposition) this->scale = _gui_scale; this->OnInit(); - /* Re-initialize the window from the ground up. No need to change the nested_array, as all widgets stay where they are. */ - this->nested_root->SetupSmallestSize(this, false); + /* Re-initialize window smallest size. */ + this->nested_root->SetupSmallestSize(this); this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL); this->width = this->nested_root->smallest_x; this->height = this->nested_root->smallest_y; @@ -1379,13 +1379,8 @@ void Window::InitializeData(WindowNumber window_number) this->window_number = window_number; this->OnInit(); - /* Initialize nested widget tree. */ - if (this->nested_array == nullptr) { - this->nested_array = CallocT(this->nested_array_size); - this->nested_root->SetupSmallestSize(this, true); - } else { - this->nested_root->SetupSmallestSize(this, false); - } + /* Initialize smallest size. */ + this->nested_root->SetupSmallestSize(this); /* Initialize to smallest size. */ this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL); @@ -1732,16 +1727,14 @@ static Point LocalGetWindowPlacement(const WindowDesc *desc, int16_t sm_width, i * @param fill_nested Fill the #nested_array (enabling is expensive!). * @note Filling the nested array requires an additional traversal through the nested widget tree, and is best performed by #FinishInitNested rather than here. */ -void Window::CreateNestedTree(bool fill_nested) +void Window::CreateNestedTree() { int biggest_index = -1; this->nested_root = MakeWindowNWidgetTree(this->window_desc->nwid_begin, this->window_desc->nwid_end, &biggest_index, &this->shade_select); this->nested_array_size = (uint)(biggest_index + 1); - if (fill_nested) { - this->nested_array = CallocT(this->nested_array_size); - this->nested_root->FillNestedArray(this->nested_array, this->nested_array_size); - } + this->nested_array = CallocT(this->nested_array_size); + this->nested_root->FillNestedArray(this->nested_array, this->nested_array_size); } /** @@ -1763,7 +1756,7 @@ void Window::FinishInitNested(WindowNumber window_number) */ void Window::InitNested(WindowNumber window_number) { - this->CreateNestedTree(false); + this->CreateNestedTree(); this->FinishInitNested(window_number); } diff --git a/src/window_gui.h b/src/window_gui.h index 5b9a3e3433..df6b47f027 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -288,7 +288,7 @@ public: virtual ptrdiff_t GetTextCharacterAtPosition(const Point &pt) const; void InitNested(WindowNumber number = 0); - void CreateNestedTree(bool fill_nested = true); + void CreateNestedTree(); void FinishInitNested(WindowNumber window_number = 0); template::value, int> = 0>