From b1b8c1c14c6a05e844f388bdf3cb84780f7a6d1d Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sat, 19 Apr 2025 19:32:22 +0100 Subject: [PATCH] Codechange: Replace SETTING_BUTTON_WIDTH/_HEIGHT macros with a function. --- src/cheat_gui.cpp | 44 ++++++++++++++++++++++---------------- src/core/geometry_type.hpp | 4 ++++ src/game/game_gui.cpp | 28 +++++++++++++----------- src/industry_gui.cpp | 23 +++++++++++--------- src/newgrf_gui.cpp | 33 ++++++++++++++++------------ src/script/script_gui.cpp | 25 ++++++++++++---------- src/settingentry_gui.cpp | 9 ++++---- src/settings_gui.cpp | 39 +++++++++++++++++++-------------- src/settings_gui.h | 13 +++++++---- 9 files changed, 129 insertions(+), 89 deletions(-) diff --git a/src/cheat_gui.cpp b/src/cheat_gui.cpp index b2994d5eee..8ab258b93c 100644 --- a/src/cheat_gui.cpp +++ b/src/cheat_gui.cpp @@ -266,16 +266,17 @@ struct CheatWindow : Window { void DrawCheatWidget(const Rect &r) const { + const Dimension setting_button = GetSettingButtonSize(); const Rect ir = r; int y = ir.top; bool rtl = _current_text_dir == TD_RTL; - uint button_left = rtl ? ir.right - SETTING_BUTTON_WIDTH : ir.left; - uint text_left = ir.left + (rtl ? 0 : WidgetDimensions::scaled.hsep_wide + SETTING_BUTTON_WIDTH); - uint text_right = ir.right - (rtl ? WidgetDimensions::scaled.hsep_wide + SETTING_BUTTON_WIDTH : 0); + uint button_left = rtl ? ir.right - setting_button.width : ir.left; + uint text_left = ir.left + (rtl ? 0 : WidgetDimensions::scaled.hsep_wide + setting_button.width); + uint text_right = ir.right - (rtl ? WidgetDimensions::scaled.hsep_wide + setting_button.width : 0); int text_y_offset = (this->line_height - GetCharacterHeight(FS_NORMAL)) / 2; - int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2; + int button_y_offset = (this->line_height - setting_button.height) / 2; int icon_y_offset = (this->line_height - this->icon.height) / 2; for (int i = 0; i != lengthof(_cheats_ui); i++) { @@ -337,14 +338,15 @@ struct CheatWindow : Window { void DrawSetting(const Rect r, const SettingDesc *desc) const { + const Dimension setting_button = GetSettingButtonSize(); const IntSettingDesc *sd = desc->AsIntSetting(); int state = this->clicked_setting == sd ? this->clicked : 0; bool rtl = _current_text_dir == TD_RTL; - Rect buttons = r.WithWidth(SETTING_BUTTON_WIDTH, rtl); - Rect text = r.Indent(SETTING_BUTTON_WIDTH + WidgetDimensions::scaled.hsep_wide, rtl); - buttons.top += (r.Height() - SETTING_BUTTON_HEIGHT) / 2; + Rect buttons = r.WithWidth(setting_button.width, rtl); + Rect text = r.Indent(setting_button.width + WidgetDimensions::scaled.hsep_wide, rtl); + buttons.top += (r.Height() - setting_button.height) / 2; text.top += (r.Height() - GetCharacterHeight(FS_NORMAL)) / 2; /* We do not allow changes of some items when we are a client in a network game */ @@ -405,10 +407,11 @@ struct CheatWindow : Window { } } - this->line_height = std::max(this->icon.height, SETTING_BUTTON_HEIGHT); + const Dimension setting_button = GetSettingButtonSize(); + this->line_height = std::max(this->icon.height, setting_button.height); this->line_height = std::max(this->line_height, GetCharacterHeight(FS_NORMAL)) + WidgetDimensions::scaled.framerect.Vertical(); - size.width = width + WidgetDimensions::scaled.hsep_wide * 2 + SETTING_BUTTON_WIDTH; + size.width = width + WidgetDimensions::scaled.hsep_wide * 2 + setting_button.width; size.height = this->line_height * lengthof(_cheats_ui); } @@ -422,7 +425,8 @@ struct CheatWindow : Window { width = std::max(width, GetStringBoundingBox(GetString(sd->GetTitle(), STR_CONFIG_SETTING_VALUE, param1, param2)).width); } - size.width = width + WidgetDimensions::scaled.hsep_wide * 2 + SETTING_BUTTON_WIDTH; + const Dimension setting_button = GetSettingButtonSize(); + size.width = width + WidgetDimensions::scaled.hsep_wide * 2 + setting_button.width; size.height = this->line_height * static_cast(std::size(this->sandbox_settings)); } @@ -436,6 +440,7 @@ struct CheatWindow : Window { void CheatPanelClick(Point pt) { + const Dimension setting_button = GetSettingButtonSize(); Rect r = this->GetWidget(WID_C_PANEL)->GetCurrentRect().Shrink(WidgetDimensions::scaled.framerect); uint btn = (pt.y - r.top) / this->line_height; int x = pt.x - r.left; @@ -448,19 +453,19 @@ struct CheatWindow : Window { int value = static_cast(ReadValue(ce->variable, ce->type)); int oldvalue = value; - if (btn == CHT_CHANGE_DATE && x >= SETTING_BUTTON_WIDTH) { + if (btn == CHT_CHANGE_DATE && !IsInsideMM(x, 0, setting_button.width)) { /* Click at the date text directly. */ clicked_cheat = CHT_CHANGE_DATE; ShowQueryString(GetString(STR_JUST_INT, value), STR_CHEAT_CHANGE_DATE_QUERY_CAPT, 8, this, CS_NUMERAL, QueryStringFlag::AcceptUnchanged); return; - } else if (btn == CHT_EDIT_MAX_HL && x >= SETTING_BUTTON_WIDTH) { + } else if (btn == CHT_EDIT_MAX_HL && !IsInsideMM(x, 0, setting_button.width)) { clicked_cheat = CHT_EDIT_MAX_HL; ShowQueryString(GetString(STR_JUST_INT, value), STR_CHEAT_EDIT_MAX_HL_QUERY_CAPT, 8, this, CS_NUMERAL, QueryStringFlag::AcceptUnchanged); return; } /* Not clicking a button? */ - if (!IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) return; + if (!IsInsideMM(x, 0, setting_button.width)) return; this->clicked_setting = nullptr; *ce->been_used = true; @@ -473,10 +478,10 @@ struct CheatWindow : Window { default: /* Take whatever the function returns */ - value = ce->proc(value + ((x >= SETTING_BUTTON_WIDTH / 2) ? 1 : -1), (x >= SETTING_BUTTON_WIDTH / 2) ? 1 : -1); + value = ce->proc(value + ((x >= static_cast(setting_button.width) / 2) ? 1 : -1), (x >= static_cast(setting_button.width) / 2) ? 1 : -1); /* The first cheat (money), doesn't return a different value. */ - if (value != oldvalue || btn == CHT_MONEY) this->clicked = btn * 2 + 1 + ((x >= SETTING_BUTTON_WIDTH / 2) != rtl ? 1 : 0); + if (value != oldvalue || btn == CHT_MONEY) this->clicked = btn * 2 + 1 + ((x >= static_cast(setting_button.width) / 2) != rtl ? 1 : 0); break; } @@ -502,7 +507,8 @@ struct CheatWindow : Window { bool rtl = _current_text_dir == TD_RTL; if (rtl) x = r.Width() - 1 - x; - if (x < SETTING_BUTTON_WIDTH) { + const Dimension setting_button = GetSettingButtonSize(); + if (IsInsideMM(x, 0, setting_button.width)) { ChangeSettingValue(sd, x); } else { /* Only open editbox if clicked for the second time, and only for types where it is sensible for. */ @@ -538,6 +544,8 @@ struct CheatWindow : Window { return; } + const Dimension setting_button = GetSettingButtonSize(); + /* Add a dynamic step-size to the scroller. In a maximum of * 50-steps you should be able to get from min to max, * unless specified otherwise in the 'interval' variable @@ -546,7 +554,7 @@ struct CheatWindow : Window { if (step == 0) step = 1; /* Increase or decrease the value and clamp it to extremes */ - if (x >= SETTING_BUTTON_WIDTH / 2) { + if (x >= static_cast(setting_button.width) / 2) { value += step; if (sd->min < 0) { assert(static_cast(sd->max) >= 0); @@ -564,7 +572,7 @@ struct CheatWindow : Window { if (value != oldvalue) { this->last_clicked_setting = nullptr; this->clicked_setting = sd; - this->clicked = (x >= SETTING_BUTTON_WIDTH / 2) != (_current_text_dir == TD_RTL) ? 2 : 1; + this->clicked = (x >= static_cast(setting_button.width) / 2) != (_current_text_dir == TD_RTL) ? 2 : 1; this->SetTimeout(); _left_button_clicked = false; } diff --git a/src/core/geometry_type.hpp b/src/core/geometry_type.hpp index ddb3398ade..6830e34839 100644 --- a/src/core/geometry_type.hpp +++ b/src/core/geometry_type.hpp @@ -89,6 +89,10 @@ struct Rect { int right = 0; int bottom = 0; + constexpr Rect() {} + constexpr Rect(int left, int top, int right, int bottom) : left(left), top(top), right(right), bottom(bottom) {} + constexpr Rect(int left, int top, const Dimension &dim) : left(left), top(top), right(left + dim.width - 1), bottom(top + dim.height - 1) {} + /** * Get width of Rect. * @return width of Rect. diff --git a/src/game/game_gui.cpp b/src/game/game_gui.cpp index cc042246fa..6451a1dfc8 100644 --- a/src/game/game_gui.cpp +++ b/src/game/game_gui.cpp @@ -137,12 +137,14 @@ struct GSConfigWindow : public Window { void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override { switch (widget) { - case WID_GSC_SETTINGS: - this->line_height = std::max(SETTING_BUTTON_HEIGHT, GetCharacterHeight(FS_NORMAL)) + padding.height; + case WID_GSC_SETTINGS: { + const Dimension setting_button = GetSettingButtonSize(); + this->line_height = std::max(setting_button.height, GetCharacterHeight(FS_NORMAL)) + padding.height; resize.width = 1; resize.height = this->line_height; size.height = 5 * this->line_height; break; + } case WID_GSC_GSLIST: this->line_height = GetCharacterHeight(FS_NORMAL) + padding.height; @@ -179,13 +181,14 @@ struct GSConfigWindow : public Window { break; } case WID_GSC_SETTINGS: { + const Dimension setting_button = GetSettingButtonSize(); Rect ir = r.Shrink(WidgetDimensions::scaled.framerect); bool rtl = _current_text_dir == TD_RTL; - Rect br = ir.WithWidth(SETTING_BUTTON_WIDTH, rtl); - Rect tr = ir.Indent(SETTING_BUTTON_WIDTH + WidgetDimensions::scaled.hsep_wide, rtl); + Rect br = ir.WithWidth(setting_button.width, rtl); + Rect tr = ir.Indent(setting_button.width + WidgetDimensions::scaled.hsep_wide, rtl); int y = r.top; - int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2; + int button_y_offset = (this->line_height - setting_button.height) / 2; int text_y_offset = (this->line_height - GetCharacterHeight(FS_NORMAL)) / 2; const auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->visible_settings); @@ -267,13 +270,14 @@ struct GSConfigWindow : public Window { bool bool_item = config_item.flags.Test(ScriptConfigFlag::Boolean); + const Dimension setting_button = GetSettingButtonSize(); Rect r = this->GetWidget(widget)->GetCurrentRect().Shrink(WidgetDimensions::scaled.matrix, RectPadding::zero); int x = pt.x - r.left; if (_current_text_dir == TD_RTL) x = r.Width() - 1 - x; /* One of the arrows is clicked (or green/red rect in case of bool value) */ int old_val = this->gs_config->GetSetting(config_item.name); - if (!bool_item && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && config_item.complete_labels) { + if (!bool_item && IsInsideMM(x, 0, setting_button.width) && config_item.complete_labels) { if (this->clicked_dropdown) { /* unclick the dropdown */ this->CloseChildWindows(WC_DROPDOWN_MENU); @@ -283,10 +287,10 @@ struct GSConfigWindow : public Window { int rel_y = (pt.y - r.top) % this->line_height; Rect wi_rect; - wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x); - wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1; - wi_rect.top = pt.y - rel_y + (this->line_height - SETTING_BUTTON_HEIGHT) / 2; - wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1; + wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? setting_button.width - 1 - x : x); + wi_rect.right = wi_rect.left + setting_button.width - 1; + wi_rect.top = pt.y - rel_y + (this->line_height - setting_button.height) / 2; + wi_rect.bottom = wi_rect.top + setting_button.height - 1; /* If the mouse is still held but dragged outside of the dropdown list, keep the dropdown open */ if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) { @@ -301,11 +305,11 @@ struct GSConfigWindow : public Window { ShowDropDownListAt(this, std::move(list), old_val, WID_GSC_SETTING_DROPDOWN, wi_rect, COLOUR_ORANGE); } } - } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) { + } else if (IsInsideMM(x, 0, setting_button.width)) { int new_val = old_val; if (bool_item) { new_val = !new_val; - } else if (x >= SETTING_BUTTON_WIDTH / 2) { + } else if (x >= static_cast(setting_button.width) / 2) { /* Increase button clicked */ new_val += config_item.step_size; if (new_val > config_item.max_value) new_val = config_item.max_value; diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index f1693e265a..df5b039556 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -839,8 +839,9 @@ public: void OnInit() override { + const Dimension setting_button = GetSettingButtonSize(); /* This only used when the cheat to alter industry production is enabled */ - this->cheat_line_height = std::max(SETTING_BUTTON_HEIGHT + WidgetDimensions::scaled.vsep_normal, GetCharacterHeight(FS_NORMAL)); + this->cheat_line_height = std::max(setting_button.height + WidgetDimensions::scaled.vsep_normal, GetCharacterHeight(FS_NORMAL)); this->cargo_icon_size = GetLargestCargoIconSize(); } @@ -926,9 +927,10 @@ public: ir.top += GetCharacterHeight(FS_NORMAL); } + const Dimension setting_button = GetSettingButtonSize(); int line_height = this->editable == EA_RATE ? this->cheat_line_height : GetCharacterHeight(FS_NORMAL); int text_y_offset = (line_height - GetCharacterHeight(FS_NORMAL)) / 2; - int button_y_offset = (line_height - SETTING_BUTTON_HEIGHT) / 2; + int button_y_offset = (line_height - setting_button.height) / 2; first = true; for (const auto &p : i->produced) { if (!IsValidCargoType(p.cargo)) continue; @@ -945,11 +947,11 @@ public: CargoSuffix suffix; GetCargoSuffix(CARGOSUFFIX_OUT, CST_VIEW, i, i->type, ind, p.cargo, &p - i->produced.data(), suffix); - DrawString(ir.Indent(label_indent + (this->editable == EA_RATE ? SETTING_BUTTON_WIDTH + WidgetDimensions::scaled.hsep_normal : 0), rtl).Translate(0, text_y_offset), + DrawString(ir.Indent(label_indent + (this->editable == EA_RATE ? setting_button.width + WidgetDimensions::scaled.hsep_normal : 0), rtl).Translate(0, text_y_offset), GetString(STR_INDUSTRY_VIEW_TRANSPORTED, p.cargo, p.history[LAST_MONTH].production, suffix.text, ToPercent8(p.history[LAST_MONTH].PctTransported()))); /* Let's put out those buttons.. */ if (this->editable == EA_RATE) { - DrawArrowButtons(ir.Indent(label_indent, rtl).WithWidth(SETTING_BUTTON_WIDTH, rtl).left, ir.top + button_y_offset, COLOUR_YELLOW, (this->clicked_line == IL_RATE1 + (&p - i->produced.data())) ? this->clicked_button : 0, + DrawArrowButtons(ir.Indent(label_indent, rtl).WithWidth(setting_button.width, rtl).left, ir.top + button_y_offset, COLOUR_YELLOW, (this->clicked_line == IL_RATE1 + (&p - i->produced.data())) ? this->clicked_button : 0, p.rate > 0, p.rate < 255); } ir.top += line_height; @@ -959,12 +961,12 @@ public: if (this->editable == EA_MULTIPLIER) { line_height = this->cheat_line_height; text_y_offset = (line_height - GetCharacterHeight(FS_NORMAL)) / 2; - button_y_offset = (line_height - SETTING_BUTTON_HEIGHT) / 2; + button_y_offset = (line_height - setting_button.height) / 2; ir.top += WidgetDimensions::scaled.vsep_wide; this->production_offset_y = ir.top; - DrawString(ir.Indent(label_indent + SETTING_BUTTON_WIDTH + WidgetDimensions::scaled.hsep_normal, rtl).Translate(0, text_y_offset), + DrawString(ir.Indent(label_indent + setting_button.width + WidgetDimensions::scaled.hsep_normal, rtl).Translate(0, text_y_offset), GetString(STR_INDUSTRY_VIEW_PRODUCTION_LEVEL, RoundDivSU(i->prod_level * 100, PRODLEVEL_DEFAULT))); - DrawArrowButtons(ir.Indent(label_indent, rtl).WithWidth(SETTING_BUTTON_WIDTH, rtl).left, ir.top + button_y_offset, COLOUR_YELLOW, (this->clicked_line == IL_MULTIPLIER) ? this->clicked_button : 0, + DrawArrowButtons(ir.Indent(label_indent, rtl).WithWidth(setting_button.width, rtl).left, ir.top + button_y_offset, COLOUR_YELLOW, (this->clicked_line == IL_MULTIPLIER) ? this->clicked_button : 0, i->prod_level > PRODLEVEL_MINIMUM, i->prod_level < PRODLEVEL_MAXIMUM); ir.top += line_height; } @@ -1039,9 +1041,10 @@ public: bool rtl = _current_text_dir == TD_RTL; Rect r = this->GetWidget(widget)->GetCurrentRect().Shrink(WidgetDimensions::scaled.framerect).Indent(this->cargo_icon_size.width + WidgetDimensions::scaled.hsep_normal, rtl); - if (r.WithWidth(SETTING_BUTTON_WIDTH, rtl).Contains(pt)) { + const Dimension setting_button = GetSettingButtonSize(); + if (r.WithWidth(setting_button.width, rtl).Contains(pt)) { /* Clicked buttons, decrease or increase production */ - bool decrease = r.WithWidth(SETTING_BUTTON_WIDTH / 2, rtl).Contains(pt); + bool decrease = r.WithWidth(setting_button.width / 2, rtl).Contains(pt); switch (this->editable) { case EA_MULTIPLIER: if (decrease) { @@ -1073,7 +1076,7 @@ public: this->SetTimeout(); this->clicked_line = line; this->clicked_button = (decrease ^ rtl) ? 1 : 2; - } else if (r.Indent(SETTING_BUTTON_WIDTH + WidgetDimensions::scaled.hsep_normal, rtl).Contains(pt)) { + } else if (r.Indent(setting_button.width + WidgetDimensions::scaled.hsep_normal, rtl).Contains(pt)) { /* clicked the text */ this->editbox_line = line; switch (this->editable) { diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 6c206a93fe..3414e1311e 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -206,8 +206,9 @@ struct NewGRFParametersWindow : public Window { switch (widget) { case WID_NP_NUMPAR_DEC: case WID_NP_NUMPAR_INC: { - size.width = std::max(SETTING_BUTTON_WIDTH / 2, GetCharacterHeight(FS_NORMAL)); - size.height = std::max(SETTING_BUTTON_HEIGHT, GetCharacterHeight(FS_NORMAL)); + const Dimension setting_button = GetSettingButtonSize(); + size.width = std::max(setting_button.width / 2, GetCharacterHeight(FS_NORMAL)); + size.height = std::max(setting_button.height, GetCharacterHeight(FS_NORMAL)); break; } @@ -219,13 +220,15 @@ struct NewGRFParametersWindow : public Window { break; } - case WID_NP_BACKGROUND: - this->line_height = std::max(SETTING_BUTTON_HEIGHT, GetCharacterHeight(FS_NORMAL)) + padding.height; + case WID_NP_BACKGROUND: { + const Dimension setting_button = GetSettingButtonSize(); + this->line_height = std::max(setting_button.height, GetCharacterHeight(FS_NORMAL)) + padding.height; resize.width = 1; resize.height = this->line_height; size.height = 5 * this->line_height; break; + } case WID_NP_DESCRIPTION: /* Minimum size of 4 lines. The 500 is the default size of the window. */ @@ -288,12 +291,13 @@ struct NewGRFParametersWindow : public Window { return; } + const Dimension setting_button = GetSettingButtonSize(); Rect ir = r.Shrink(WidgetDimensions::scaled.frametext, RectPadding::zero); bool rtl = _current_text_dir == TD_RTL; - uint buttons_left = rtl ? ir.right - SETTING_BUTTON_WIDTH : ir.left; - Rect tr = ir.Indent(SETTING_BUTTON_WIDTH + WidgetDimensions::scaled.hsep_wide, rtl); + uint buttons_left = rtl ? ir.right - setting_button.width : ir.left; + Rect tr = ir.Indent(setting_button.width + WidgetDimensions::scaled.hsep_wide, rtl); - int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2; + int button_y_offset = (this->line_height - setting_button.height) / 2; int text_y_offset = (this->line_height - GetCharacterHeight(FS_NORMAL)) / 2; for (int32_t i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) { GRFParameterInfo &par_info = this->GetParameterInfo(i); @@ -355,6 +359,7 @@ struct NewGRFParametersWindow : public Window { this->clicked_dropdown = false; } + const Dimension setting_button = GetSettingButtonSize(); Rect r = this->GetWidget(widget)->GetCurrentRect().Shrink(WidgetDimensions::scaled.frametext, RectPadding::zero); int x = pt.x - r.left; if (_current_text_dir == TD_RTL) x = r.Width() - 1 - x; @@ -363,7 +368,7 @@ struct NewGRFParametersWindow : public Window { /* One of the arrows is clicked */ uint32_t old_val = this->grf_config.GetValue(par_info); - if (par_info.type != PTYPE_BOOL && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && par_info.complete_labels) { + if (par_info.type != PTYPE_BOOL && IsInsideMM(x, 0, setting_button.width) && par_info.complete_labels) { if (this->clicked_dropdown) { /* unclick the dropdown */ this->CloseChildWindows(WC_DROPDOWN_MENU); @@ -373,10 +378,10 @@ struct NewGRFParametersWindow : public Window { int rel_y = (pt.y - r.top) % this->line_height; Rect wi_rect; - wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x);; - wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1; - wi_rect.top = pt.y - rel_y + (this->line_height - SETTING_BUTTON_HEIGHT) / 2; - wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1; + wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? setting_button.width - 1 - x : x);; + wi_rect.right = wi_rect.left + setting_button.width - 1; + wi_rect.top = pt.y - rel_y + (this->line_height - setting_button.height) / 2; + wi_rect.bottom = wi_rect.top + setting_button.height - 1; /* For dropdowns we also have to check the y position thoroughly, the mouse may not above the just opening dropdown */ if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) { @@ -393,12 +398,12 @@ struct NewGRFParametersWindow : public Window { ShowDropDownListAt(this, std::move(list), old_val, WID_NP_SETTING_DROPDOWN, wi_rect, COLOUR_ORANGE); } } - } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) { + } else if (IsInsideMM(x, 0, setting_button.width)) { uint32_t val = old_val; if (par_info.type == PTYPE_BOOL) { val = !val; } else { - if (x >= SETTING_BUTTON_WIDTH / 2) { + if (x >= static_cast(setting_button.width) / 2) { /* Increase button clicked */ if (val < par_info.max_value) val++; this->clicked_increase = true; diff --git a/src/script/script_gui.cpp b/src/script/script_gui.cpp index d5353dfa82..3f6c859812 100644 --- a/src/script/script_gui.cpp +++ b/src/script/script_gui.cpp @@ -348,7 +348,8 @@ struct ScriptSettingsWindow : public Window { { if (widget != WID_SCRS_BACKGROUND) return; - this->line_height = std::max(SETTING_BUTTON_HEIGHT, GetCharacterHeight(FS_NORMAL)) + padding.height; + const Dimension setting_button = GetSettingButtonSize(); + this->line_height = std::max(setting_button.height, GetCharacterHeight(FS_NORMAL)) + padding.height; resize.width = 1; resize.height = this->line_height; @@ -359,13 +360,14 @@ struct ScriptSettingsWindow : public Window { { if (widget != WID_SCRS_BACKGROUND) return; + const Dimension setting_button = GetSettingButtonSize(); Rect ir = r.Shrink(WidgetDimensions::scaled.framerect); bool rtl = _current_text_dir == TD_RTL; - Rect br = ir.WithWidth(SETTING_BUTTON_WIDTH, rtl); - Rect tr = ir.Indent(SETTING_BUTTON_WIDTH + WidgetDimensions::scaled.hsep_wide, rtl); + Rect br = ir.WithWidth(setting_button.width, rtl); + Rect tr = ir.Indent(setting_button.width + WidgetDimensions::scaled.hsep_wide, rtl); int y = r.top; - int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2; + int button_y_offset = (this->line_height - setting_button.height) / 2; int text_y_offset = (this->line_height - GetCharacterHeight(FS_NORMAL)) / 2; const auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->visible_settings); @@ -419,13 +421,14 @@ struct ScriptSettingsWindow : public Window { bool bool_item = config_item.flags.Test(ScriptConfigFlag::Boolean); + const Dimension setting_button = GetSettingButtonSize(); Rect r = this->GetWidget(widget)->GetCurrentRect().Shrink(WidgetDimensions::scaled.matrix, RectPadding::zero); int x = pt.x - r.left; if (_current_text_dir == TD_RTL) x = r.Width() - 1 - x; /* One of the arrows is clicked (or green/red rect in case of bool value) */ int old_val = this->script_config->GetSetting(config_item.name); - if (!bool_item && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && config_item.complete_labels) { + if (!bool_item && IsInsideMM(x, 0, setting_button.width) && config_item.complete_labels) { if (this->clicked_dropdown) { /* unclick the dropdown */ this->CloseChildWindows(WC_DROPDOWN_MENU); @@ -435,10 +438,10 @@ struct ScriptSettingsWindow : public Window { int rel_y = (pt.y - r.top) % this->line_height; Rect wi_rect; - wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x); - wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1; - wi_rect.top = pt.y - rel_y + (this->line_height - SETTING_BUTTON_HEIGHT) / 2; - wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1; + wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? setting_button.width - 1 - x : x); + wi_rect.right = wi_rect.left + setting_button.width - 1; + wi_rect.top = pt.y - rel_y + (this->line_height - setting_button.height) / 2; + wi_rect.bottom = wi_rect.top + setting_button.height - 1; /* If the mouse is still held but dragged outside of the dropdown list, keep the dropdown open */ if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) { @@ -453,11 +456,11 @@ struct ScriptSettingsWindow : public Window { ShowDropDownListAt(this, std::move(list), old_val, WID_SCRS_SETTING_DROPDOWN, wi_rect, COLOUR_ORANGE); } } - } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) { + } else if (IsInsideMM(x, 0, setting_button.width)) { int new_val = old_val; if (bool_item) { new_val = !new_val; - } else if (x >= SETTING_BUTTON_WIDTH / 2) { + } else if (x >= static_cast(setting_button.width) / 2) { /* Increase button clicked */ new_val += config_item.step_size; if (new_val > config_item.max_value) new_val = config_item.max_value; diff --git a/src/settingentry_gui.cpp b/src/settingentry_gui.cpp index e48572e13f..b19b08d5fa 100644 --- a/src/settingentry_gui.cpp +++ b/src/settingentry_gui.cpp @@ -277,14 +277,15 @@ const void *ResolveObject(const GameSettings *settings_ptr, const IntSettingDesc */ void SettingEntry::DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const { + const Dimension setting_button = GetSettingButtonSize(); const IntSettingDesc *sd = this->setting; int state = (this->flags & SEF_BUTTONS_MASK).base(); bool rtl = _current_text_dir == TD_RTL; - uint buttons_left = rtl ? right + 1 - SETTING_BUTTON_WIDTH : left; - uint text_left = left + (rtl ? 0 : SETTING_BUTTON_WIDTH + WidgetDimensions::scaled.hsep_wide); - uint text_right = right - (rtl ? SETTING_BUTTON_WIDTH + WidgetDimensions::scaled.hsep_wide : 0); - uint button_y = y + (SETTING_HEIGHT - SETTING_BUTTON_HEIGHT) / 2; + uint buttons_left = rtl ? right + 1 - setting_button.width : left; + uint text_left = left + (rtl ? 0 : setting_button.width + WidgetDimensions::scaled.hsep_wide); + uint text_right = right - (rtl ? setting_button.width + WidgetDimensions::scaled.hsep_wide : 0); + uint button_y = y + (SETTING_HEIGHT - setting_button.height) / 2; /* We do not allow changes of some items when we are a client in a networkgame */ bool editable = sd->IsEditable(); diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index 1337f298e7..a98e0e104a 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -1273,12 +1273,14 @@ struct GameSettingsWindow : Window { void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override { switch (widget) { - case WID_GS_OPTIONSPANEL: - resize.height = SETTING_HEIGHT = std::max({(int)_setting_circle_size.height, SETTING_BUTTON_HEIGHT, GetCharacterHeight(FS_NORMAL)}) + WidgetDimensions::scaled.vsep_normal; + case WID_GS_OPTIONSPANEL: { + const Dimension setting_button = GetSettingButtonSize(); + resize.height = SETTING_HEIGHT = std::max({_setting_circle_size.height, setting_button.height, static_cast(GetCharacterHeight(FS_NORMAL))}) + WidgetDimensions::scaled.vsep_normal; resize.width = 1; size.height = 5 * resize.height + WidgetDimensions::scaled.framerect.Vertical(); break; + } case WID_GS_HELP_TEXT: { static const StringID setting_types[] = { @@ -1507,11 +1509,12 @@ struct GameSettingsWindow : Window { return; } + const Dimension setting_button = GetSettingButtonSize(); auto [min_val, max_val] = sd->GetRange(); int32_t value = sd->Read(ResolveObject(settings_ptr, sd)); /* clicked on the icon on the left side. Either scroller, bool on/off or dropdown */ - if (x < SETTING_BUTTON_WIDTH && sd->flags.Test(SettingFlag::GuiDropdown)) { + if (IsInsideMM(x, 0, setting_button.width) && sd->flags.Test(SettingFlag::GuiDropdown)) { this->SetDisplayedHelpText(pe); if (this->valuedropdown_entry == pe) { @@ -1528,10 +1531,10 @@ struct GameSettingsWindow : Window { int rel_y = (pt.y - wid->pos_y - WidgetDimensions::scaled.framerect.top) % wid->resize_y; Rect wi_rect; - wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x); - wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1; - wi_rect.top = pt.y - rel_y + (SETTING_HEIGHT - SETTING_BUTTON_HEIGHT) / 2; - wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1; + wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? setting_button.width - 1 - x : x); + wi_rect.right = wi_rect.left + setting_button.width - 1; + wi_rect.top = pt.y - rel_y + (SETTING_HEIGHT - setting_button.height) / 2; + wi_rect.bottom = wi_rect.top + setting_button.height - 1; /* For dropdowns we also have to check the y position thoroughly, the mouse may not above the just opening dropdown */ if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) { @@ -1548,7 +1551,7 @@ struct GameSettingsWindow : Window { } } this->SetDirty(); - } else if (x < SETTING_BUTTON_WIDTH) { + } else if (IsInsideMM(x, 0, setting_button.width)) { this->SetDisplayedHelpText(pe); int32_t oldvalue = value; @@ -1569,7 +1572,7 @@ struct GameSettingsWindow : Window { } /* Increase or decrease the value and clamp it to extremes */ - if (x >= SETTING_BUTTON_WIDTH / 2) { + if (IsInsideMM(x, setting_button.width / 2, setting_button.width)) { value += step; if (min_val < 0) { assert(static_cast(max_val) >= 0); @@ -1589,7 +1592,7 @@ struct GameSettingsWindow : Window { this->clicked_entry->SetButtons({}); } this->clicked_entry = pe; - this->clicked_entry->SetButtons((x >= SETTING_BUTTON_WIDTH / 2) != (_current_text_dir == TD_RTL) ? SettingEntryFlag::RightDepressed : SettingEntryFlag::LeftDepressed); + this->clicked_entry->SetButtons(IsInsideMM(x, setting_button.width / 2, setting_button.width) != (_current_text_dir == TD_RTL) ? SettingEntryFlag::RightDepressed : SettingEntryFlag::LeftDepressed); this->SetTimeout(); _left_button_clicked = false; } @@ -1856,7 +1859,7 @@ void DrawDropDownButton(int x, int y, Colours button_colour, bool state, bool cl { int colour = GetColourGradient(button_colour, SHADE_DARKER); - Rect r = {x, y, x + SETTING_BUTTON_WIDTH - 1, y + SETTING_BUTTON_HEIGHT - 1}; + Rect r{x, y, GetSettingButtonSize()}; DrawFrameRect(r, button_colour, state ? FrameFlag::Lowered : FrameFlags{}); DrawSpriteIgnorePadding(SPR_ARROW_DOWN, PAL_NONE, r, SA_CENTER); @@ -1877,7 +1880,7 @@ void DrawBoolButton(int x, int y, bool state, bool clickable) { static const Colours _bool_ctabs[2][2] = {{COLOUR_CREAM, COLOUR_RED}, {COLOUR_DARK_GREEN, COLOUR_GREEN}}; - Rect r = {x, y, x + SETTING_BUTTON_WIDTH - 1, y + SETTING_BUTTON_HEIGHT - 1}; + Rect r{x, y, GetSettingButtonSize()}; DrawFrameRect(r, _bool_ctabs[state][clickable], state ? FrameFlag::Lowered : FrameFlags{}); } @@ -1924,16 +1927,20 @@ struct CustomCurrencyWindow : Window { case WID_CC_RATE_DOWN: case WID_CC_RATE_UP: case WID_CC_YEAR_DOWN: - case WID_CC_YEAR_UP: - size = maxdim(size, {(uint)SETTING_BUTTON_WIDTH / 2, (uint)SETTING_BUTTON_HEIGHT}); + case WID_CC_YEAR_UP: { + const Dimension setting_button = GetSettingButtonSize(); + size = maxdim(size, {setting_button.width / 2, setting_button.height}); break; + } /* Set the appropriate width for the edit buttons. */ case WID_CC_SEPARATOR_EDIT: case WID_CC_PREFIX_EDIT: - case WID_CC_SUFFIX_EDIT: - size = maxdim(size, {(uint)SETTING_BUTTON_WIDTH, (uint)SETTING_BUTTON_HEIGHT}); + case WID_CC_SUFFIX_EDIT: { + const Dimension setting_button = GetSettingButtonSize(); + size = maxdim(size, setting_button); break; + } /* Make sure the window is wide enough for the widest exchange rate */ case WID_CC_RATE: diff --git a/src/settings_gui.h b/src/settings_gui.h index bca6888ac7..3a94151d83 100644 --- a/src/settings_gui.h +++ b/src/settings_gui.h @@ -13,10 +13,15 @@ #include "gfx_type.h" #include "dropdown_type.h" -/** Width of setting buttons */ -#define SETTING_BUTTON_WIDTH ((int)NWidgetScrollbar::GetHorizontalDimension().width * 2) -/** Height of setting buttons */ -#define SETTING_BUTTON_HEIGHT ((int)NWidgetScrollbar::GetHorizontalDimension().height) +/** + * Get dimension of setting buttons. + * @return Dimension of setting buttons. + * @note returns pair instead of Dimension due to signed comparisons. + */ +static inline Dimension GetSettingButtonSize() +{ + return {NWidgetScrollbar::GetHorizontalDimension().width * 2, NWidgetScrollbar::GetHorizontalDimension().height}; +} void DrawArrowButtons(int x, int y, Colours button_colour, uint8_t state, bool clickable_left, bool clickable_right); void DrawDropDownButton(int x, int y, Colours button_colour, bool state, bool clickable);