Changeset - r28348:9ebffeb7fcc0
[Not reviewed]
master
0 9 0
Peter Nelson - 4 months ago 2023-12-29 14:27:04
peter1138@openttd.org
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`.
9 files changed with 48 insertions and 83 deletions:
0 comments (0 inline, 0 general)
src/fios_gui.cpp
Show inline comments
 
@@ -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<NWidgetStacked>(WID_SL_CONTENT_DOWNLOAD_SEL)->SetDisplayedPlane(SZSP_HORIZONTAL);
 
		}
src/network/network_gui.cpp
Show inline comments
 
@@ -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());
 
		}
 

	
src/newgrf_gui.cpp
Show inline comments
 
@@ -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<NewGRFWindow *>(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();
src/smallmap_gui.cpp
Show inline comments
 
@@ -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<SmallMapWindow *>(w);
 
		assert(this->smallmap_window != nullptr);
src/toolbar_gui.cpp
Show inline comments
 
@@ -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 : publ
 
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;
src/widget.cpp
Show inline comments
 
@@ -995,7 +995,7 @@ NWidgetBase::NWidgetBase(WidgetType tp) 
 
/* ~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) 
 
 * 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::AdjustPaddingForZoo
 
	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(W
 
	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(NWi
 
{
 
}
 

	
 
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::SetupSmallestSiz
 
	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(NWidCon
 
{
 
}
 

	
 
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(
 
	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, 
 
	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(Scrollb
 
	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<NWidgetCore *>(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::AdjustPaddingFor
 
	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 ind
 
	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(Widge
 
	}
 
}
 

	
 
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, 
 
	}
 
}
 

	
 
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};
src/widget_type.h
Show inline comments
 
@@ -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 NWidget
 
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 NWidgetPI
 
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 NWidgetResi
 
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 NWidgetCo
 
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<TileIndex, VehicleID> focus, ZoomLevel zoom);
 
@@ -840,7 +840,7 @@ class NWidgetScrollbar : public NWidgetC
 
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);
src/window.cpp
Show inline comments
 
@@ -963,8 +963,8 @@ void Window::ReInit(int rx, int ry, bool
 
	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
 
	this->window_number = window_number;
 

	
 
	this->OnInit();
 
	/* Initialize nested widget tree. */
 
	if (this->nested_array == nullptr) {
 
		this->nested_array = CallocT<NWidgetBase *>(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(con
 
 * @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<NWidgetBase *>(this->nested_array_size);
 
		this->nested_root->FillNestedArray(this->nested_array, this->nested_array_size);
 
	}
 
	this->nested_array = CallocT<NWidgetBase *>(this->nested_array_size);
 
	this->nested_root->FillNestedArray(this->nested_array, this->nested_array_size);
 
}
 

	
 
/**
 
@@ -1763,7 +1756,7 @@ void Window::FinishInitNested(WindowNumb
 
 */
 
void Window::InitNested(WindowNumber window_number)
 
{
 
	this->CreateNestedTree(false);
 
	this->CreateNestedTree();
 
	this->FinishInitNested(window_number);
 
}
 

	
src/window_gui.h
Show inline comments
 
@@ -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<typename T, std::enable_if_t<std::is_base_of<StrongTypedefBase, T>::value, int> = 0>
0 comments (0 inline, 0 general)