Skip to content

Commit

Permalink
Move ClickableScrollableList to lib/widget
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due committed Mar 20, 2024
1 parent 3628188 commit 39f3784
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 59 deletions.
56 changes: 56 additions & 0 deletions lib/widget/scrollablelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,59 @@ void ScrollableListWidget::setListTransparentToMouse(bool hasMouseTransparency)
{
listView->setTransparentToMouse(hasMouseTransparency);
}

// MARK: - ClickableScrollableList

std::shared_ptr<ClickableScrollableList> ClickableScrollableList::make()
{
class make_shared_enabler: public ClickableScrollableList {};
auto widget = std::make_shared<make_shared_enabler>();
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);
}
}
19 changes: 19 additions & 0 deletions lib/widget/scrollablelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,23 @@ class ScrollableListWidget : public WIDGET
uint32_t getScrollPositionForItem(size_t itemNum);
};

class ClickableScrollableList : public ScrollableListWidget
{
public:
static std::shared_ptr<ClickableScrollableList> make();
typedef std::function<void (ClickableScrollableList& list)> ClickableScrollableList_OnClick_Func;
void setOnClickHandler(const ClickableScrollableList_OnClick_Func& _onClickFunc);
typedef std::function<void (ClickableScrollableList& list, bool isHighlighted)> 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__
59 changes: 0 additions & 59 deletions src/titleui/campaign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,65 +760,6 @@ void WzCampaignOptionButton::display(int xOffset, int yOffset)
}
}

class ClickableScrollableList : public ScrollableListWidget
{
public:
static std::shared_ptr<ClickableScrollableList> make()
{
class make_shared_enabler: public ClickableScrollableList {};
auto widget = std::make_shared<make_shared_enabler>();
widget->initialize(); // calls ScrollableListWidget::initialize
widget->setListTransparentToMouse(true);
return widget;
}
typedef std::function<void (ClickableScrollableList& list)> ClickableScrollableList_OnClick_Func;
void setOnClickHandler(const ClickableScrollableList_OnClick_Func& _onClickFunc)
{
onClickFunc = _onClickFunc;
}
typedef std::function<void (ClickableScrollableList& list, bool isHighlighted)> 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:
Expand Down

0 comments on commit 39f3784

Please sign in to comment.