From d89753b88dd0b465211531460393014e2c5df6fe Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sun, 20 Apr 2025 11:19:01 +0100 Subject: [PATCH] Codechange: Add dedicated widget for boolean toggle buttons. --- src/widget.cpp | 17 ++++++++++++++++- src/widget_type.h | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/widget.cpp b/src/widget.cpp index e8198a8bca..e62d1ba63e 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -10,6 +10,7 @@ #include "stdafx.h" #include "core/backup_type.hpp" #include "company_func.h" +#include "settings_gui.h" #include "strings_type.h" #include "window_gui.h" #include "viewport_func.h" @@ -2719,6 +2720,7 @@ NWidgetLeaf::NWidgetLeaf(WidgetType tp, Colours colour, WidgetID index, const Wi case WWT_TEXTBTN: case WWT_PUSHTXTBTN: case WWT_TEXTBTN_2: + case WWT_BOOLBTN: case WWT_MATRIX: case NWID_BUTTON_DROPDOWN: case NWID_PUSHBUTTON_DROPDOWN: @@ -2879,6 +2881,12 @@ void NWidgetLeaf::SetupSmallestSize(Window *w) padding = {WidgetDimensions::scaled.frametext.Horizontal(), WidgetDimensions::scaled.framerect.Vertical()}; break; } + + case WWT_BOOLBTN: + size.width = SETTING_BUTTON_WIDTH; + size.height = SETTING_BUTTON_HEIGHT; + break; + case WWT_IMGBTN: case WWT_IMGBTN_2: case WWT_PUSHIMGBTN: { @@ -2992,6 +3000,12 @@ void NWidgetLeaf::Draw(const Window *w) DrawFrameRect(r.left, r.top, r.right, r.bottom, this->colour, (clicked) ? FrameFlag::Lowered : FrameFlags{}); break; + case WWT_BOOLBTN: { + Point pt = GetAlignedPosition(r, Dimension(SETTING_BUTTON_WIDTH, SETTING_BUTTON_HEIGHT), this->align); + DrawBoolButton(pt.x, pt.y, this->colour, this->colour, clicked, !this->IsDisabled()); + break; + } + case WWT_IMGBTN: case WWT_PUSHIMGBTN: case WWT_IMGBTN_2: @@ -3079,7 +3093,8 @@ void NWidgetLeaf::Draw(const Window *w) } if (this->index >= 0) w->DrawWidget(r, this->index); - if (this->IsDisabled()) { + if (this->IsDisabled() && this->type != WWT_BOOLBTN) { + /* WWT_BOOLBTN is excluded as it draws its own disabled state. */ GfxFillRect(r.Shrink(WidgetDimensions::scaled.bevel), GetColourGradient(this->colour, SHADE_DARKER), FILLRECT_CHECKER); } diff --git a/src/widget_type.h b/src/widget_type.h index 52abde5565..cb5c8b8787 100644 --- a/src/widget_type.h +++ b/src/widget_type.h @@ -44,6 +44,7 @@ enum WidgetType : uint8_t { WWT_ARROWBTN, ///< (Toggle) Button with an arrow WWT_TEXTBTN, ///< (Toggle) Button with text WWT_TEXTBTN_2, ///< (Toggle) Button with diff text when clicked + WWT_BOOLBTN, ///< Standard boolean toggle button. WWT_LABEL, ///< Centered label WWT_TEXT, ///< Pure simple text WWT_MATRIX, ///< Grid of rows and columns. @see MatrixWidgetValues