1
0
Fork 0

Codechange: Return update nwid_begin instead of count.

This avoids needing to keep track of how many widget parts have been consumed, instead we only to ensure nwid_begin < nwid_end.
pull/11262/head
Peter Nelson 2023-09-03 21:55:30 +01:00 committed by PeterN
parent e8015e497d
commit 152b0cac34
1 changed files with 23 additions and 32 deletions

View File

@ -3013,31 +3013,29 @@ bool NWidgetLeaf::ButtonHit(const Point &pt)
* @param dest Address of pointer to use for returning the composed widget. * @param dest Address of pointer to use for returning the composed widget.
* @param fill_dest Fill the composed widget with child widgets. * @param fill_dest Fill the composed widget with child widgets.
* @param biggest_index Pointer to biggest nested widget index in the tree encountered so far. * @param biggest_index Pointer to biggest nested widget index in the tree encountered so far.
* @return Number of widget part elements used to compose the widget. * @return Pointer to remaining nested widget parts.
* @pre \c biggest_index != nullptr. * @pre \c biggest_index != nullptr.
*/ */
static int MakeNWidget(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_end, NWidgetBase **dest, bool *fill_dest, int *biggest_index) static const NWidgetPart *MakeNWidget(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_end, NWidgetBase **dest, bool *fill_dest, int *biggest_index)
{ {
int num_used = 0;
*dest = nullptr; *dest = nullptr;
*fill_dest = false; *fill_dest = false;
while (nwid_begin < nwid_end) { while (nwid_begin < nwid_end) {
switch (nwid_begin->type) { switch (nwid_begin->type) {
case NWID_SPACER: case NWID_SPACER:
if (*dest != nullptr) return num_used; if (*dest != nullptr) return nwid_begin;
*dest = new NWidgetSpacer(0, 0); *dest = new NWidgetSpacer(0, 0);
break; break;
case NWID_HORIZONTAL: case NWID_HORIZONTAL:
if (*dest != nullptr) return num_used; if (*dest != nullptr) return nwid_begin;
*dest = new NWidgetHorizontal(nwid_begin->u.cont_flags); *dest = new NWidgetHorizontal(nwid_begin->u.cont_flags);
*fill_dest = true; *fill_dest = true;
break; break;
case NWID_HORIZONTAL_LTR: case NWID_HORIZONTAL_LTR:
if (*dest != nullptr) return num_used; if (*dest != nullptr) return nwid_begin;
*dest = new NWidgetHorizontalLTR(nwid_begin->u.cont_flags); *dest = new NWidgetHorizontalLTR(nwid_begin->u.cont_flags);
*fill_dest = true; *fill_dest = true;
break; break;
@ -3045,20 +3043,20 @@ static int MakeNWidget(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_en
case WWT_PANEL: case WWT_PANEL:
case WWT_INSET: case WWT_INSET:
case WWT_FRAME: case WWT_FRAME:
if (*dest != nullptr) return num_used; if (*dest != nullptr) return nwid_begin;
*dest = new NWidgetBackground(nwid_begin->type, nwid_begin->u.widget.colour, nwid_begin->u.widget.index); *dest = new NWidgetBackground(nwid_begin->type, nwid_begin->u.widget.colour, nwid_begin->u.widget.index);
*biggest_index = std::max(*biggest_index, (int)nwid_begin->u.widget.index); *biggest_index = std::max(*biggest_index, (int)nwid_begin->u.widget.index);
*fill_dest = true; *fill_dest = true;
break; break;
case NWID_VERTICAL: case NWID_VERTICAL:
if (*dest != nullptr) return num_used; if (*dest != nullptr) return nwid_begin;
*dest = new NWidgetVertical(nwid_begin->u.cont_flags); *dest = new NWidgetVertical(nwid_begin->u.cont_flags);
*fill_dest = true; *fill_dest = true;
break; break;
case NWID_MATRIX: { case NWID_MATRIX: {
if (*dest != nullptr) return num_used; if (*dest != nullptr) return nwid_begin;
NWidgetMatrix *nwm = new NWidgetMatrix(); NWidgetMatrix *nwm = new NWidgetMatrix();
*dest = nwm; *dest = nwm;
*fill_dest = true; *fill_dest = true;
@ -3069,7 +3067,7 @@ static int MakeNWidget(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_en
} }
case WPT_FUNCTION: { case WPT_FUNCTION: {
if (*dest != nullptr) return num_used; if (*dest != nullptr) return nwid_begin;
/* Ensure proper functioning even when the called code simply writes its largest index. */ /* Ensure proper functioning even when the called code simply writes its largest index. */
int biggest = -1; int biggest = -1;
*dest = nwid_begin->u.func_ptr(&biggest); *dest = nwid_begin->u.func_ptr(&biggest);
@ -3158,23 +3156,23 @@ static int MakeNWidget(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_en
} }
case WPT_ENDCONTAINER: case WPT_ENDCONTAINER:
return num_used; return nwid_begin;
case NWID_VIEWPORT: case NWID_VIEWPORT:
if (*dest != nullptr) return num_used; if (*dest != nullptr) return nwid_begin;
*dest = new NWidgetViewport(nwid_begin->u.widget.index); *dest = new NWidgetViewport(nwid_begin->u.widget.index);
*biggest_index = std::max(*biggest_index, (int)nwid_begin->u.widget.index); *biggest_index = std::max(*biggest_index, (int)nwid_begin->u.widget.index);
break; break;
case NWID_HSCROLLBAR: case NWID_HSCROLLBAR:
case NWID_VSCROLLBAR: case NWID_VSCROLLBAR:
if (*dest != nullptr) return num_used; if (*dest != nullptr) return nwid_begin;
*dest = new NWidgetScrollbar(nwid_begin->type, nwid_begin->u.widget.colour, nwid_begin->u.widget.index); *dest = new NWidgetScrollbar(nwid_begin->type, nwid_begin->u.widget.colour, nwid_begin->u.widget.index);
*biggest_index = std::max(*biggest_index, (int)nwid_begin->u.widget.index); *biggest_index = std::max(*biggest_index, (int)nwid_begin->u.widget.index);
break; break;
case NWID_SELECTION: { case NWID_SELECTION: {
if (*dest != nullptr) return num_used; if (*dest != nullptr) return nwid_begin;
NWidgetStacked *nws = new NWidgetStacked(); NWidgetStacked *nws = new NWidgetStacked();
*dest = nws; *dest = nws;
*fill_dest = true; *fill_dest = true;
@ -3184,17 +3182,16 @@ static int MakeNWidget(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_en
} }
default: default:
if (*dest != nullptr) return num_used; if (*dest != nullptr) return nwid_begin;
assert((nwid_begin->type & WWT_MASK) < WWT_LAST || (nwid_begin->type & WWT_MASK) == NWID_BUTTON_DROPDOWN); assert((nwid_begin->type & WWT_MASK) < WWT_LAST || (nwid_begin->type & WWT_MASK) == NWID_BUTTON_DROPDOWN);
*dest = new NWidgetLeaf(nwid_begin->type, nwid_begin->u.widget.colour, nwid_begin->u.widget.index, 0x0, STR_NULL); *dest = new NWidgetLeaf(nwid_begin->type, nwid_begin->u.widget.colour, nwid_begin->u.widget.index, 0x0, STR_NULL);
*biggest_index = std::max(*biggest_index, (int)nwid_begin->u.widget.index); *biggest_index = std::max(*biggest_index, (int)nwid_begin->u.widget.index);
break; break;
} }
num_used++;
nwid_begin++; nwid_begin++;
} }
return num_used; return nwid_begin;
} }
/** /**
@ -3203,10 +3200,10 @@ static int MakeNWidget(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_en
* @param nwid_end Pointer to ending of nested widget parts. * @param nwid_end Pointer to ending of nested widget parts.
* @param parent Pointer or container to use for storing the child widgets (*parent == nullptr or *parent == container or background widget). * @param parent Pointer or container to use for storing the child widgets (*parent == nullptr or *parent == container or background widget).
* @param biggest_index Pointer to biggest nested widget index in the tree. * @param biggest_index Pointer to biggest nested widget index in the tree.
* @return Number of widget part elements used to fill the container. * @return Pointer to remaining nested widget parts.
* @post \c *biggest_index contains the largest widget index of the tree and \c -1 if no index is used. * @post \c *biggest_index contains the largest widget index of the tree and \c -1 if no index is used.
*/ */
static int MakeWidgetTree(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_end, NWidgetBase **parent, int *biggest_index) static const NWidgetPart *MakeWidgetTree(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_end, NWidgetBase **parent, int *biggest_index)
{ {
/* If *parent == nullptr, only the first widget is read and returned. Otherwise, *parent must point to either /* If *parent == nullptr, only the first widget is read and returned. Otherwise, *parent must point to either
* a #NWidgetContainer or a #NWidgetBackground object, and parts are added as much as possible. */ * a #NWidgetContainer or a #NWidgetBackground object, and parts are added as much as possible. */
@ -3214,13 +3211,10 @@ static int MakeWidgetTree(const NWidgetPart *nwid_begin, const NWidgetPart *nwid
NWidgetBackground *nwid_parent = dynamic_cast<NWidgetBackground *>(*parent); NWidgetBackground *nwid_parent = dynamic_cast<NWidgetBackground *>(*parent);
assert(*parent == nullptr || (nwid_cont != nullptr && nwid_parent == nullptr) || (nwid_cont == nullptr && nwid_parent != nullptr)); assert(*parent == nullptr || (nwid_cont != nullptr && nwid_parent == nullptr) || (nwid_cont == nullptr && nwid_parent != nullptr));
int total_used = 0;
for (;;) { for (;;) {
NWidgetBase *sub_widget = nullptr; NWidgetBase *sub_widget = nullptr;
bool fill_sub = false; bool fill_sub = false;
int num_used = MakeNWidget(nwid_begin, nwid_end, &sub_widget, &fill_sub, biggest_index); nwid_begin = MakeNWidget(nwid_begin, nwid_end, &sub_widget, &fill_sub, biggest_index);
nwid_begin += num_used;
total_used += num_used;
/* Break out of loop when end reached */ /* Break out of loop when end reached */
if (sub_widget == nullptr) break; if (sub_widget == nullptr) break;
@ -3230,9 +3224,7 @@ static int MakeWidgetTree(const NWidgetPart *nwid_begin, const NWidgetPart *nwid
if (fill_sub && (tp == NWID_HORIZONTAL || tp == NWID_HORIZONTAL_LTR || tp == NWID_VERTICAL || tp == NWID_MATRIX if (fill_sub && (tp == NWID_HORIZONTAL || tp == NWID_HORIZONTAL_LTR || tp == NWID_VERTICAL || tp == NWID_MATRIX
|| tp == WWT_PANEL || tp == WWT_FRAME || tp == WWT_INSET || tp == NWID_SELECTION)) { || tp == WWT_PANEL || tp == WWT_FRAME || tp == WWT_INSET || tp == NWID_SELECTION)) {
NWidgetBase *sub_ptr = sub_widget; NWidgetBase *sub_ptr = sub_widget;
num_used = MakeWidgetTree(nwid_begin, nwid_end, &sub_ptr, biggest_index); nwid_begin = MakeWidgetTree(nwid_begin, nwid_end, &sub_ptr, biggest_index);
nwid_begin += num_used;
total_used += num_used;
} }
/* Add sub_widget to parent container if available, otherwise return the widget to the caller. */ /* Add sub_widget to parent container if available, otherwise return the widget to the caller. */
@ -3240,15 +3232,15 @@ static int MakeWidgetTree(const NWidgetPart *nwid_begin, const NWidgetPart *nwid
if (nwid_parent != nullptr) nwid_parent->Add(sub_widget); if (nwid_parent != nullptr) nwid_parent->Add(sub_widget);
if (nwid_cont == nullptr && nwid_parent == nullptr) { if (nwid_cont == nullptr && nwid_parent == nullptr) {
*parent = sub_widget; *parent = sub_widget;
return total_used; return nwid_begin;
} }
} }
if (nwid_begin == nwid_end) return total_used; // Reached the end of the array of parts? if (nwid_begin == nwid_end) return nwid_begin; // Reached the end of the array of parts?
assert(nwid_begin < nwid_end); assert(nwid_begin < nwid_end);
assert(nwid_begin->type == WPT_ENDCONTAINER); assert(nwid_begin->type == WPT_ENDCONTAINER);
return total_used + 1; // *nwid_begin is also 'used' return nwid_begin + 1; // *nwid_begin is also 'used'
} }
/** /**
@ -3290,9 +3282,8 @@ NWidgetContainer *MakeWindowNWidgetTree(const NWidgetPart *nwid_begin, const NWi
/* Read the first widget recursively from the array. */ /* Read the first widget recursively from the array. */
NWidgetBase *nwid = nullptr; NWidgetBase *nwid = nullptr;
int num_used = MakeWidgetTree(nwid_begin, nwid_end, &nwid, biggest_index); nwid_begin = MakeWidgetTree(nwid_begin, nwid_end, &nwid, biggest_index);
assert(nwid != nullptr); assert(nwid != nullptr);
nwid_begin += num_used;
NWidgetContainer *root = new NWidgetVertical; NWidgetContainer *root = new NWidgetVertical;
root->Add(nwid); root->Add(nwid);