Window: Add accessors for cached NWidgetLeaf dimensions
This commit is contained in:
@@ -3019,7 +3019,7 @@ struct BuildVehicleWindowTrainAdvanced final : BuildVehicleWindowBase {
|
||||
}
|
||||
|
||||
case WID_BV_RENAME_LOCO: {
|
||||
*size = maxdim(*size, NWidgetLeaf::resizebox_dimension);
|
||||
*size = maxdim(*size, NWidgetLeaf::GetResizeBoxDimension());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@@ -194,7 +194,7 @@ public:
|
||||
(*size).width = std::max((*size).width, WidgetDimensions::scaled.framerect.left + this->CARGO_ICON_WIDTH + WidgetDimensions::scaled.framerect.Horizontal() + this->max_cargo_name_width + padding.width);
|
||||
(*size).height = std::max((*size).height, (uint) FONT_HEIGHT_NORMAL + WidgetDimensions::scaled.framerect.Vertical());
|
||||
} else if ((WID_CTO_CARGO_DROPDOWN_FIRST <= widget && widget <= WID_CTO_CARGO_DROPDOWN_LAST) || widget == WID_CTO_SET_TO_ALL_DROPDOWN) {
|
||||
(*size).width = std::max((*size).width, WidgetDimensions::scaled.dropdowntext.Horizontal() + this->max_cargo_dropdown_width + NWidgetLeaf::dropdown_dimension.width);
|
||||
(*size).width = std::max((*size).width, WidgetDimensions::scaled.dropdowntext.Horizontal() + this->max_cargo_dropdown_width + NWidgetLeaf::GetDropdownBoxDimension().width);
|
||||
(*size).height = std::max((*size).height, (uint) WidgetDimensions::scaled.dropdowntext.Vertical() + FONT_HEIGHT_NORMAL);
|
||||
} else if (widget == WID_CTO_SET_TO_ALL_LABEL) {
|
||||
(*size).width = std::max((*size).width, this->max_cargo_name_width + WidgetDimensions::scaled.framerect.right + padding.width);
|
||||
|
@@ -839,7 +839,7 @@ void DrawCaption(const Rect &r, Colours colour, Owner owner, TextColour text_col
|
||||
*/
|
||||
static inline void DrawButtonDropdown(const Rect &r, Colours colour, bool clicked_button, bool clicked_dropdown, StringID str, StringAlignment align)
|
||||
{
|
||||
int dd_width = NWidgetLeaf::dropdown_dimension.width;
|
||||
int dd_width = NWidgetLeaf::GetDropdownBoxDimension().width;
|
||||
|
||||
if (_current_text_dir == TD_LTR) {
|
||||
DrawFrameRect(r.left, r.top, r.right - dd_width, r.bottom, colour, clicked_button ? FR_LOWERED : FR_NONE);
|
||||
@@ -2739,6 +2739,27 @@ NWidgetLeaf::NWidgetLeaf(WidgetType tp, Colours colour, int index, uint32 data,
|
||||
}
|
||||
}
|
||||
|
||||
/* static */ void NWidgetLeaf::UpdateDropdownBoxDimension()
|
||||
{
|
||||
NWidgetLeaf::dropdown_dimension = GetScaledSpriteSize(SPR_ARROW_DOWN);
|
||||
NWidgetLeaf::dropdown_dimension.width += WidgetDimensions::scaled.vscrollbar.Horizontal();
|
||||
NWidgetLeaf::dropdown_dimension.height += WidgetDimensions::scaled.vscrollbar.Vertical();
|
||||
}
|
||||
|
||||
/* static */ void NWidgetLeaf::UpdateResizeBoxDimension()
|
||||
{
|
||||
NWidgetLeaf::resizebox_dimension = maxdim(GetScaledSpriteSize(SPR_WINDOW_RESIZE_LEFT), GetScaledSpriteSize(SPR_WINDOW_RESIZE_RIGHT));
|
||||
NWidgetLeaf::resizebox_dimension.width += WidgetDimensions::scaled.resizebox.Horizontal();
|
||||
NWidgetLeaf::resizebox_dimension.height += WidgetDimensions::scaled.resizebox.Vertical();
|
||||
}
|
||||
|
||||
/* static */ void NWidgetLeaf::UpdateCloseBoxDimension()
|
||||
{
|
||||
NWidgetLeaf::closebox_dimension = GetScaledSpriteSize(SPR_CLOSEBOX);
|
||||
NWidgetLeaf::closebox_dimension.width += WidgetDimensions::scaled.closebox.Horizontal();
|
||||
NWidgetLeaf::closebox_dimension.height += WidgetDimensions::scaled.closebox.Vertical();
|
||||
}
|
||||
|
||||
void NWidgetLeaf::SetupSmallestSize(Window *w, bool init_array)
|
||||
{
|
||||
if (this->index >= 0 && init_array) { // Fill w->nested_array[]
|
||||
@@ -2809,12 +2830,7 @@ void NWidgetLeaf::SetupSmallestSize(Window *w, bool init_array)
|
||||
|
||||
case WWT_RESIZEBOX: {
|
||||
padding = {WidgetDimensions::scaled.resizebox.Horizontal(), WidgetDimensions::scaled.resizebox.Vertical()};
|
||||
if (NWidgetLeaf::resizebox_dimension.width == 0) {
|
||||
NWidgetLeaf::resizebox_dimension = maxdim(GetScaledSpriteSize(SPR_WINDOW_RESIZE_LEFT), GetScaledSpriteSize(SPR_WINDOW_RESIZE_RIGHT));
|
||||
NWidgetLeaf::resizebox_dimension.width += padding.width;
|
||||
NWidgetLeaf::resizebox_dimension.height += padding.height;
|
||||
}
|
||||
size = maxdim(size, NWidgetLeaf::resizebox_dimension);
|
||||
size = maxdim(size, NWidgetLeaf::GetResizeBoxDimension());
|
||||
break;
|
||||
}
|
||||
case WWT_EDITBOX: {
|
||||
@@ -2850,12 +2866,7 @@ void NWidgetLeaf::SetupSmallestSize(Window *w, bool init_array)
|
||||
|
||||
case WWT_CLOSEBOX: {
|
||||
padding = {WidgetDimensions::scaled.closebox.Horizontal(), WidgetDimensions::scaled.closebox.Vertical()};
|
||||
if (NWidgetLeaf::closebox_dimension.width == 0) {
|
||||
NWidgetLeaf::closebox_dimension = GetScaledSpriteSize(SPR_CLOSEBOX);
|
||||
NWidgetLeaf::closebox_dimension.width += padding.width;
|
||||
NWidgetLeaf::closebox_dimension.height += padding.height;
|
||||
}
|
||||
size = maxdim(size, NWidgetLeaf::closebox_dimension);
|
||||
size = maxdim(size, NWidgetLeaf::GetCloseBoxDimension());
|
||||
break;
|
||||
}
|
||||
case WWT_TEXTBTN:
|
||||
@@ -2887,16 +2898,12 @@ void NWidgetLeaf::SetupSmallestSize(Window *w, bool init_array)
|
||||
case WWT_DROPDOWN:
|
||||
case NWID_BUTTON_DROPDOWN:
|
||||
case NWID_PUSHBUTTON_DROPDOWN: {
|
||||
if (NWidgetLeaf::dropdown_dimension.width == 0) {
|
||||
NWidgetLeaf::dropdown_dimension = GetScaledSpriteSize(SPR_ARROW_DOWN);
|
||||
NWidgetLeaf::dropdown_dimension.width += WidgetDimensions::scaled.vscrollbar.Horizontal();
|
||||
NWidgetLeaf::dropdown_dimension.height += WidgetDimensions::scaled.vscrollbar.Vertical();
|
||||
}
|
||||
padding = {WidgetDimensions::scaled.dropdowntext.Horizontal() + NWidgetLeaf::dropdown_dimension.width + WidgetDimensions::scaled.fullbevel.Horizontal(), WidgetDimensions::scaled.dropdowntext.Vertical()};
|
||||
const Dimension &dropdown_dim = NWidgetLeaf::GetDropdownBoxDimension();
|
||||
padding = {WidgetDimensions::scaled.dropdowntext.Horizontal() + dropdown_dim.width + WidgetDimensions::scaled.fullbevel.Horizontal(), WidgetDimensions::scaled.dropdowntext.Vertical()};
|
||||
if (this->index >= 0) w->SetStringParameters(this->index);
|
||||
Dimension d2 = GetStringBoundingBox(this->widget_data, this->text_size);
|
||||
d2.width += padding.width;
|
||||
d2.height = std::max(d2.height + padding.height, NWidgetLeaf::dropdown_dimension.height);
|
||||
d2.height = std::max(d2.height + padding.height, dropdown_dim.height);
|
||||
size = maxdim(size, d2);
|
||||
break;
|
||||
}
|
||||
@@ -3054,11 +3061,12 @@ void NWidgetLeaf::Draw(const Window *w)
|
||||
*/
|
||||
bool NWidgetLeaf::ButtonHit(const Point &pt)
|
||||
{
|
||||
const Dimension &dimension = NWidgetLeaf::GetDropdownBoxDimension();
|
||||
if (_current_text_dir == TD_LTR) {
|
||||
int button_width = this->pos_x + this->current_x - NWidgetLeaf::dropdown_dimension.width;
|
||||
int button_width = this->pos_x + this->current_x - dimension.width;
|
||||
return pt.x < button_width;
|
||||
} else {
|
||||
int button_left = this->pos_x + NWidgetLeaf::dropdown_dimension.width;
|
||||
int button_left = this->pos_x + dimension.width;
|
||||
return pt.x >= button_left;
|
||||
}
|
||||
}
|
||||
|
@@ -877,10 +877,32 @@ public:
|
||||
|
||||
static void InvalidateDimensionCache();
|
||||
|
||||
static const Dimension &GetDropdownBoxDimension()
|
||||
{
|
||||
if (dropdown_dimension.width == 0) UpdateDropdownBoxDimension();
|
||||
return dropdown_dimension;
|
||||
}
|
||||
|
||||
static const Dimension &GetResizeBoxDimension()
|
||||
{
|
||||
if (resizebox_dimension.width == 0) UpdateResizeBoxDimension();
|
||||
return resizebox_dimension;
|
||||
}
|
||||
|
||||
static const Dimension &GetCloseBoxDimension()
|
||||
{
|
||||
if (closebox_dimension.width == 0) UpdateCloseBoxDimension();
|
||||
return closebox_dimension;
|
||||
}
|
||||
|
||||
private:
|
||||
static void UpdateDropdownBoxDimension();
|
||||
static void UpdateResizeBoxDimension();
|
||||
static void UpdateCloseBoxDimension();
|
||||
|
||||
static Dimension dropdown_dimension; ///< Cached size of a dropdown widget.
|
||||
static Dimension resizebox_dimension; ///< Cached size of a resizebox widget.
|
||||
static Dimension closebox_dimension; ///< Cached size of a closebox widget.
|
||||
private:
|
||||
static Dimension shadebox_dimension; ///< Cached size of a shadebox widget.
|
||||
static Dimension debugbox_dimension; ///< Cached size of a debugbox widget.
|
||||
static Dimension defsizebox_dimension; ///< Cached size of a defsizebox widget.
|
||||
|
@@ -1791,8 +1791,9 @@ static Point GetAutoPlacePosition(int width, int height)
|
||||
* of the closebox
|
||||
*/
|
||||
int left = rtl ? _screen.width - width : 0, top = toolbar_y;
|
||||
int offset_x = rtl ? -(int)NWidgetLeaf::closebox_dimension.width : (int)NWidgetLeaf::closebox_dimension.width;
|
||||
int offset_y = std::max<int>(NWidgetLeaf::closebox_dimension.height, FONT_HEIGHT_NORMAL + WidgetDimensions::scaled.captiontext.Vertical());
|
||||
const Dimension &closebox_dimension = NWidgetLeaf::GetCloseBoxDimension();
|
||||
int offset_x = rtl ? -(int)closebox_dimension.width : (int)closebox_dimension.width;
|
||||
int offset_y = std::max<int>(closebox_dimension.height, FONT_HEIGHT_NORMAL + WidgetDimensions::scaled.captiontext.Vertical());
|
||||
|
||||
restart:
|
||||
for (const Window *w : Window::IterateFromBack()) {
|
||||
@@ -1858,11 +1859,12 @@ static Point LocalGetWindowPlacement(const WindowDesc *desc, int16 sm_width, int
|
||||
* - Y position: closebox of parent + closebox of child + statusbar
|
||||
* - X position: closebox on left/right, resizebox on right/left (depending on ltr/rtl)
|
||||
*/
|
||||
int indent_y = std::max<int>(NWidgetLeaf::closebox_dimension.height, FONT_HEIGHT_NORMAL + WidgetDimensions::scaled.captiontext.Vertical());
|
||||
const Dimension &closebox_dimension = NWidgetLeaf::GetCloseBoxDimension();
|
||||
int indent_y = std::max<int>(closebox_dimension.height, FONT_HEIGHT_NORMAL + WidgetDimensions::scaled.captiontext.Vertical());
|
||||
if (w->top + 3 * indent_y < _screen.height) {
|
||||
pt.y = w->top + indent_y;
|
||||
int indent_close = NWidgetLeaf::closebox_dimension.width;
|
||||
int indent_resize = NWidgetLeaf::resizebox_dimension.width;
|
||||
int indent_close = closebox_dimension.width;
|
||||
int indent_resize = NWidgetLeaf::GetResizeBoxDimension().width;
|
||||
if (_current_text_dir == TD_RTL) {
|
||||
pt.x = std::max(w->left + w->width - default_width - indent_close, 0);
|
||||
if (pt.x + default_width >= indent_close && pt.x + indent_resize <= _screen.width) return pt;
|
||||
|
Reference in New Issue
Block a user