mirror of https://github.com/OpenTTD/OpenTTD.git
(svn r16226) -Codechange: Separating generation of a widget array from WindowDesc to the InitializeWidgetArrayFromNestedWidgets function.
This commit is contained in:
parent
d953f3e4c7
commit
efde51e452
|
@ -1724,3 +1724,43 @@ NWidgetContainer *MakeNWidgets(const NWidgetPart *parts, int count)
|
||||||
MakeWidgetTree(parts, count, cont);
|
MakeWidgetTree(parts, count, cont);
|
||||||
return cont;
|
return cont;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a #Widget array from a nested widget parts array, taking care of all the steps and checks.
|
||||||
|
* Also cache the result and use the cache if possible.
|
||||||
|
* @param[in] parts Array with parts of the widgets.
|
||||||
|
* @param parts_length Length of the \a parts array.
|
||||||
|
* @param[in] orig_wid Pointer to original widget array.
|
||||||
|
* @param wid_cache Pointer to the cache for storing the generated widget array (use \c NULL to prevent caching).
|
||||||
|
* @return Cached value if available, otherwise the generated widget array. If \a wid_cache is \c NULL, the caller should free the returned array.
|
||||||
|
*
|
||||||
|
* @pre Before the first call, \c *wid_cache should be \c NULL.
|
||||||
|
* @post The widget array stored in the \c *wid_cache should be free-ed by the caller.
|
||||||
|
*/
|
||||||
|
const Widget *InitializeWidgetArrayFromNestedWidgets(const NWidgetPart *parts, int parts_length, const Widget *orig_wid, Widget **wid_cache)
|
||||||
|
{
|
||||||
|
const bool rtl = false; // Direction of the language is left-to-right
|
||||||
|
|
||||||
|
if (wid_cache != NULL && *wid_cache != NULL) return *wid_cache;
|
||||||
|
|
||||||
|
assert(parts != NULL && parts_length > 0);
|
||||||
|
NWidgetContainer *nwid = MakeNWidgets(parts, parts_length);
|
||||||
|
Widget *gen_wid = InitializeNWidgets(nwid, rtl);
|
||||||
|
|
||||||
|
if (!rtl && orig_wid) {
|
||||||
|
/* There are two descriptions, compare them.
|
||||||
|
* Comparing only makes sense when using a left-to-right language.
|
||||||
|
*/
|
||||||
|
bool ok = CompareWidgetArrays(orig_wid, gen_wid, false);
|
||||||
|
if (ok) {
|
||||||
|
DEBUG(misc, 1, "Nested widgets are equal, min-size(%u, %u)", nwid->min_x, nwid->min_y);
|
||||||
|
} else {
|
||||||
|
DEBUG(misc, 0, "Nested widgets give different results");
|
||||||
|
CompareWidgetArrays(orig_wid, gen_wid, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delete nwid;
|
||||||
|
|
||||||
|
if (wid_cache != NULL) *wid_cache = gen_wid;
|
||||||
|
return gen_wid;
|
||||||
|
}
|
||||||
|
|
|
@ -659,4 +659,6 @@ static inline NWidgetPart NWidgetFunction(NWidgetFunctionType *func_ptr)
|
||||||
|
|
||||||
NWidgetContainer *MakeNWidgets(const NWidgetPart *parts, int count);
|
NWidgetContainer *MakeNWidgets(const NWidgetPart *parts, int count);
|
||||||
|
|
||||||
|
const Widget *InitializeWidgetArrayFromNestedWidgets(const NWidgetPart *parts, int parts_length, const Widget *orig_wid, Widget **wid_cache);
|
||||||
|
|
||||||
#endif /* WIDGET_TYPE_H */
|
#endif /* WIDGET_TYPE_H */
|
||||||
|
|
|
@ -75,28 +75,9 @@ WindowDesc::WindowDesc(int16 left, int16 top, int16 min_width, int16 min_height,
|
||||||
/** Get widget array of the window description. */
|
/** Get widget array of the window description. */
|
||||||
const Widget *WindowDesc::GetWidgets() const
|
const Widget *WindowDesc::GetWidgets() const
|
||||||
{
|
{
|
||||||
const bool rtl = false; // Direction of the language is left-to-right
|
if (this->nwid_parts != NULL) {
|
||||||
|
InitializeWidgetArrayFromNestedWidgets(this->nwid_parts, this->nwid_length, this->widgets, &this->new_widgets);
|
||||||
/* If nested widgets are present, convert them to a widget array. */
|
|
||||||
if (this->nwid_parts != NULL && nwid_length > 0 && this->new_widgets == NULL) {
|
|
||||||
NWidgetContainer *nwid = MakeNWidgets(this->nwid_parts, this->nwid_length);
|
|
||||||
this->new_widgets = InitializeNWidgets(nwid, rtl);
|
|
||||||
|
|
||||||
if (!rtl && this->widgets != NULL) {
|
|
||||||
/* There are two descriptions, compare them.
|
|
||||||
* Comparing only makes sense when using a left-to-right language.
|
|
||||||
*/
|
|
||||||
bool ok = CompareWidgetArrays(this->widgets, this->new_widgets, false);
|
|
||||||
if (ok) {
|
|
||||||
DEBUG(misc, 1, "Nested widgets are equal, min-size(%u, %u)", nwid->min_x, nwid->min_y);
|
|
||||||
} else {
|
|
||||||
DEBUG(misc, 0, "Nested widgets give different results");
|
|
||||||
CompareWidgetArrays(this->widgets, this->new_widgets, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
delete nwid;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const Widget *wids = (this->new_widgets != NULL) ? this->new_widgets : this->widgets;
|
const Widget *wids = (this->new_widgets != NULL) ? this->new_widgets : this->widgets;
|
||||||
assert(wids != NULL);
|
assert(wids != NULL);
|
||||||
return wids;
|
return wids;
|
||||||
|
|
Loading…
Reference in New Issue