From 39f3784318dd134d13ac723ec6fb47adbd7943fe Mon Sep 17 00:00:00 2001 From: past-due <30942300+past-due@users.noreply.github.com> Date: Wed, 20 Mar 2024 15:53:11 -0400 Subject: [PATCH] Move ClickableScrollableList to lib/widget --- lib/widget/scrollablelist.cpp | 56 +++++++++++++++++++++++++++++++++ lib/widget/scrollablelist.h | 19 +++++++++++ src/titleui/campaign.cpp | 59 ----------------------------------- 3 files changed, 75 insertions(+), 59 deletions(-) diff --git a/lib/widget/scrollablelist.cpp b/lib/widget/scrollablelist.cpp index b7c1bf204ef..73219e767e3 100644 --- a/lib/widget/scrollablelist.cpp +++ b/lib/widget/scrollablelist.cpp @@ -285,3 +285,59 @@ void ScrollableListWidget::setListTransparentToMouse(bool hasMouseTransparency) { listView->setTransparentToMouse(hasMouseTransparency); } + +// MARK: - ClickableScrollableList + +std::shared_ptr ClickableScrollableList::make() +{ + class make_shared_enabler: public ClickableScrollableList {}; + auto widget = std::make_shared(); + widget->initialize(); // calls ScrollableListWidget::initialize + widget->setListTransparentToMouse(true); + return widget; +} + +void ClickableScrollableList::setOnClickHandler(const ClickableScrollableList_OnClick_Func& _onClickFunc) +{ + onClickFunc = _onClickFunc; +} + +void ClickableScrollableList::setOnHighlightHandler(const ClickableScrollableList_OnHighlight_Func& _onHighlightFunc) +{ + onHighlightFunc = _onHighlightFunc; +} + +void ClickableScrollableList::clicked(W_CONTEXT *, WIDGET_KEY) +{ + mouseDownOnList = true; +} + +void ClickableScrollableList::released(W_CONTEXT *, WIDGET_KEY) +{ + bool wasFullClick = mouseDownOnList; + mouseDownOnList = false; + if (wasFullClick) + { + if (onClickFunc) + { + onClickFunc(*this); + } + } +} + +void ClickableScrollableList::highlight(W_CONTEXT *) +{ + if (onHighlightFunc) + { + onHighlightFunc(*this, true); + } +} + +void ClickableScrollableList::highlightLost() +{ + mouseDownOnList = false; + if (onHighlightFunc) + { + onHighlightFunc(*this, false); + } +} diff --git a/lib/widget/scrollablelist.h b/lib/widget/scrollablelist.h index fc647864471..8f20bb97ac4 100644 --- a/lib/widget/scrollablelist.h +++ b/lib/widget/scrollablelist.h @@ -92,4 +92,23 @@ class ScrollableListWidget : public WIDGET uint32_t getScrollPositionForItem(size_t itemNum); }; +class ClickableScrollableList : public ScrollableListWidget +{ +public: + static std::shared_ptr make(); + typedef std::function ClickableScrollableList_OnClick_Func; + void setOnClickHandler(const ClickableScrollableList_OnClick_Func& _onClickFunc); + typedef std::function ClickableScrollableList_OnHighlight_Func; + void setOnHighlightHandler(const ClickableScrollableList_OnHighlight_Func& _onHighlightFunc); +protected: + void clicked(W_CONTEXT *, WIDGET_KEY) override; + void released(W_CONTEXT *, WIDGET_KEY) override; + void highlight(W_CONTEXT *) override; + void highlightLost() override; +private: + ClickableScrollableList_OnClick_Func onClickFunc; + ClickableScrollableList_OnHighlight_Func onHighlightFunc; + bool mouseDownOnList = false; +}; + #endif // __INCLUDED_LIB_WIDGET_SCROLLABLELIST_H__ diff --git a/src/titleui/campaign.cpp b/src/titleui/campaign.cpp index f876199001d..ddbb792f688 100644 --- a/src/titleui/campaign.cpp +++ b/src/titleui/campaign.cpp @@ -760,65 +760,6 @@ void WzCampaignOptionButton::display(int xOffset, int yOffset) } } -class ClickableScrollableList : public ScrollableListWidget -{ -public: - static std::shared_ptr make() - { - class make_shared_enabler: public ClickableScrollableList {}; - auto widget = std::make_shared(); - widget->initialize(); // calls ScrollableListWidget::initialize - widget->setListTransparentToMouse(true); - return widget; - } - typedef std::function ClickableScrollableList_OnClick_Func; - void setOnClickHandler(const ClickableScrollableList_OnClick_Func& _onClickFunc) - { - onClickFunc = _onClickFunc; - } - typedef std::function ClickableScrollableList_OnHighlight_Func; - void setOnHighlightHandler(const ClickableScrollableList_OnHighlight_Func& _onHighlightFunc) - { - onHighlightFunc = _onHighlightFunc; - } -protected: - void clicked(W_CONTEXT *, WIDGET_KEY) override - { - mouseDownOnList = true; - } - void released(W_CONTEXT *, WIDGET_KEY) override - { - bool wasFullClick = mouseDownOnList; - mouseDownOnList = false; - if (wasFullClick) - { - if (onClickFunc) - { - onClickFunc(*this); - } - } - } - void highlight(W_CONTEXT *) override - { - if (onHighlightFunc) - { - onHighlightFunc(*this, true); - } - } - void highlightLost() override - { - mouseDownOnList = false; - if (onHighlightFunc) - { - onHighlightFunc(*this, false); - } - } -private: - ClickableScrollableList_OnClick_Func onClickFunc; - ClickableScrollableList_OnHighlight_Func onHighlightFunc; - bool mouseDownOnList = false; -}; - class WzCampaignTweakOptionSetting { public: