From e3d2d68bd47293e56f37d0751912ed79ce25c0b2 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sun, 20 Apr 2025 20:36:28 +0100 Subject: [PATCH] Change: Draw boolean toggle as a slider widget. This improves usability as the slider position indicates the state instead of a red/green colour change. --- src/cheat_gui.cpp | 4 ++-- src/game/game_gui.cpp | 2 +- src/newgrf_gui.cpp | 2 +- src/script/script_gui.cpp | 2 +- src/settingentry_gui.cpp | 2 +- src/settings_gui.cpp | 13 +++++++++---- src/settings_gui.h | 2 +- 7 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/cheat_gui.cpp b/src/cheat_gui.cpp index b2994d5eee..7583da680b 100644 --- a/src/cheat_gui.cpp +++ b/src/cheat_gui.cpp @@ -286,7 +286,7 @@ struct CheatWindow : Window { case SLE_BOOL: { bool on = (*(bool*)ce->variable); - DrawBoolButton(button_left, y + button_y_offset, on, true); + DrawBoolButton(button_left, y + button_y_offset, COLOUR_YELLOW, COLOUR_GREY, on, true); str = GetString(ce->str, on ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF); break; } @@ -354,7 +354,7 @@ struct CheatWindow : Window { int32_t value = sd->Read(&GetGameSettings()); if (sd->IsBoolSetting()) { /* Draw checkbox for boolean-value either on/off */ - DrawBoolButton(buttons.left, buttons.top, value != 0, editable); + DrawBoolButton(buttons.left, buttons.top, COLOUR_YELLOW, COLOUR_GREY, value != 0, editable); } else if (sd->flags.Test(SettingFlag::GuiDropdown)) { /* Draw [v] button for settings of an enum-type */ DrawDropDownButton(buttons.left, buttons.top, COLOUR_YELLOW, state != 0, editable); diff --git a/src/game/game_gui.cpp b/src/game/game_gui.cpp index cc042246fa..0fe11b1eba 100644 --- a/src/game/game_gui.cpp +++ b/src/game/game_gui.cpp @@ -195,7 +195,7 @@ struct GSConfigWindow : public Window { bool editable = this->IsEditableItem(config_item); if (config_item.flags.Test(ScriptConfigFlag::Boolean)) { - DrawBoolButton(br.left, y + button_y_offset, current_value != 0, editable); + DrawBoolButton(br.left, y + button_y_offset, COLOUR_YELLOW, COLOUR_MAUVE, current_value != 0, editable); } else { int i = static_cast(std::distance(std::begin(this->visible_settings), it)); if (config_item.complete_labels) { diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index b66f226697..e074163fba 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -301,7 +301,7 @@ struct NewGRFParametersWindow : public Window { bool selected = (i == this->clicked_row); if (par_info.type == PTYPE_BOOL) { - DrawBoolButton(buttons_left, ir.top + button_y_offset, current_value != 0, this->editable); + DrawBoolButton(buttons_left, ir.top + button_y_offset, COLOUR_YELLOW, COLOUR_MAUVE, current_value != 0, this->editable); } else if (par_info.type == PTYPE_UINT_ENUM) { if (par_info.complete_labels) { DrawDropDownButton(buttons_left, ir.top + button_y_offset, COLOUR_YELLOW, this->clicked_row == i && this->clicked_dropdown, this->editable); diff --git a/src/script/script_gui.cpp b/src/script/script_gui.cpp index d5353dfa82..c2f4a02a74 100644 --- a/src/script/script_gui.cpp +++ b/src/script/script_gui.cpp @@ -375,7 +375,7 @@ struct ScriptSettingsWindow : public Window { bool editable = this->IsEditableItem(config_item); if (config_item.flags.Test(ScriptConfigFlag::Boolean)) { - DrawBoolButton(br.left, y + button_y_offset, current_value != 0, editable); + DrawBoolButton(br.left, y + button_y_offset, COLOUR_YELLOW, COLOUR_MAUVE, current_value != 0, editable); } else { int i = static_cast(std::distance(std::begin(this->visible_settings), it)); if (config_item.complete_labels) { diff --git a/src/settingentry_gui.cpp b/src/settingentry_gui.cpp index e48572e13f..3346a345eb 100644 --- a/src/settingentry_gui.cpp +++ b/src/settingentry_gui.cpp @@ -293,7 +293,7 @@ void SettingEntry::DrawSetting(GameSettings *settings_ptr, int left, int right, int32_t value = sd->Read(ResolveObject(settings_ptr, sd)); if (sd->IsBoolSetting()) { /* Draw checkbox for boolean-value either on/off */ - DrawBoolButton(buttons_left, button_y, value != 0, editable); + DrawBoolButton(buttons_left, button_y, COLOUR_YELLOW, COLOUR_MAUVE, value != 0, editable); } else if (sd->flags.Test(SettingFlag::GuiDropdown)) { /* Draw [v] button for settings of an enum-type */ DrawDropDownButton(buttons_left, button_y, COLOUR_YELLOW, state != 0, editable); diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index 1337f298e7..079835ea71 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -1870,15 +1870,20 @@ void DrawDropDownButton(int x, int y, Colours button_colour, bool state, bool cl * Draw a toggle button. * @param x the x position to draw * @param y the y position to draw + * @param button_colour the colour of the button. + * @param background background colour. * @param state true = lowered * @param clickable is the button clickable? */ -void DrawBoolButton(int x, int y, bool state, bool clickable) +void DrawBoolButton(int x, int y, Colours button_colour, Colours background, 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}; - DrawFrameRect(r, _bool_ctabs[state][clickable], state ? FrameFlag::Lowered : FrameFlags{}); + DrawFrameRect(r, state ? COLOUR_GREEN : background, state ? FrameFlags{FrameFlag::Lowered} : FrameFlags{FrameFlag::Lowered, FrameFlag::BorderOnly}); + Rect button_rect = r.WithWidth(SETTING_BUTTON_WIDTH / 3, state ^ (_current_text_dir == TD_RTL)); + DrawFrameRect(button_rect, button_colour, {}); + if (!clickable) { + GfxFillRect(button_rect.Shrink(WidgetDimensions::scaled.bevel), GetColourGradient(button_colour, SHADE_DARKER), FILLRECT_CHECKER); + } } struct CustomCurrencyWindow : Window { diff --git a/src/settings_gui.h b/src/settings_gui.h index bca6888ac7..936d9b6de7 100644 --- a/src/settings_gui.h +++ b/src/settings_gui.h @@ -20,7 +20,7 @@ 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); -void DrawBoolButton(int x, int y, bool state, bool clickable); +void DrawBoolButton(int x, int y, Colours button_colour, Colours background, bool state, bool clickable); template DropDownList BuildSetDropDownList(int *selected_index);