From 29970fb4962ec0426063f5ef99db8d6e6ca63a8e Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Fri, 23 May 2025 17:44:18 +0100 Subject: [PATCH] Codechange: Add DropDownToggle component. --- src/dropdown_common_type.h | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/dropdown_common_type.h b/src/dropdown_common_type.h index 0771386c2c..733e81b75b 100644 --- a/src/dropdown_common_type.h +++ b/src/dropdown_common_type.h @@ -14,6 +14,7 @@ #include "gfx_func.h" #include "gfx_type.h" #include "palette_func.h" +#include "settings_gui.h" #include "string_func.h" #include "strings_func.h" #include "window_gui.h" @@ -182,6 +183,56 @@ public: } }; +/** + * Drop down boolean toggle component. + * @tparam TBase Base component. + * @tparam TEnd Position toggle at end if true, or start if false. + */ +template +class DropDownToggle : public TBase { + bool on; ///< Is item on. + int click; ///< Click result when toggle used. + Colours button_colour; ///< Colour of toggle button. + Colours background_colour; ///< Colour of toggle background. +public: + template + explicit DropDownToggle(bool on, int click, Colours button_colour, Colours background_colour, Args&&... args) + : TBase(std::forward(args)...), on(on), click(click), button_colour(button_colour), background_colour(background_colour) + { + } + + uint Height() const override + { + return std::max(SETTING_BUTTON_HEIGHT + WidgetDimensions::scaled.vsep_normal, this->TBase::Height()); + } + + uint Width() const override + { + return SETTING_BUTTON_WIDTH + WidgetDimensions::scaled.hsep_wide + this->TBase::Width(); + } + + int OnClick(const Rect &r, const Point &pt) const override + { + bool rtl = TEnd ^ (_current_text_dir == TD_RTL); + int w = SETTING_BUTTON_WIDTH; + + if (r.WithWidth(w, rtl).CentreTo(w, SETTING_BUTTON_HEIGHT).Contains(pt)) return this->click; + + return this->TBase::OnClick(r.Indent(w + WidgetDimensions::scaled.hsep_wide, rtl), pt); + } + + void Draw(const Rect &full, const Rect &r, bool sel, int click_result, Colours bg_colour) const override + { + bool rtl = TEnd ^ (_current_text_dir == TD_RTL); + int w = SETTING_BUTTON_WIDTH; + + Rect br = r.WithWidth(w, rtl).CentreTo(w, SETTING_BUTTON_HEIGHT); + DrawBoolButton(br.left, br.top, this->button_colour, this->background_colour, this->on, true); + + this->TBase::Draw(full, r.Indent(w + WidgetDimensions::scaled.hsep_wide, rtl), sel, click_result, bg_colour); + } +}; + /** * Drop down indent component. * @tparam TBase Base component.