Events now only have one handler, remove the neeed for event chaining in CheckBox

This commit is contained in:
Henrik Rydgård
2025-09-15 10:32:38 -06:00
parent c17fb20ac5
commit 730fd4754e
4 changed files with 26 additions and 38 deletions
+17 -24
View File
@@ -65,14 +65,13 @@ void ApplyBoundsBySpec(Bounds &bounds, MeasureSpec horiz, MeasureSpec vert) {
}
void Event::Add(std::function<EventReturn(EventParams&)> func) {
HandlerRegistration reg;
reg.func = func;
handlers_.push_back(reg);
_dbg_assert_(!func_);
func_ = func;
}
// Call this from input thread or whatever, it doesn't matter
void Event::Trigger(EventParams &e) {
if (handlers_.empty()) {
if (!func_) {
return;
}
EventTriggered(this, e);
@@ -80,17 +79,12 @@ void Event::Trigger(EventParams &e) {
// Call this from UI thread
EventReturn Event::Dispatch(EventParams &e) {
for (auto &handler : handlers_) {
if ((handler.func)(e) == UI::EVENT_DONE) {
// Event is handled, stop looping immediately. This event might even have gotten deleted.
return UI::EVENT_DONE;
}
}
if (func_)
func_(e);
return UI::EVENT_DONE;
}
Event::~Event() {
handlers_.clear();
RemoveQueuedEventsByEvent(this);
}
@@ -227,7 +221,7 @@ void Clickable::DrawBG(UIContext &dc, const Style &style) {
}
}
void Clickable::Click() {
void Clickable::ClickInternal() {
UI::EventParams e{};
e.v = this;
OnClick.Trigger(e);
@@ -270,7 +264,7 @@ bool Clickable::Touch(const TouchInput &input) {
}
if (input.flags & TOUCH_UP) {
if ((input.flags & TOUCH_CANCEL) == 0 && dragging_ && bounds_.Contains(input.x, input.y)) {
Click();
ClickInternal();
}
down_ = false;
downCountDown_ = 0;
@@ -376,7 +370,7 @@ bool Clickable::Key(const KeyInput &key) {
if (key.flags & KEY_UP) {
if (IsAcceptKey(key)) {
if (down_) {
Click();
ClickInternal();
down_ = false;
ret = true;
}
@@ -400,7 +394,7 @@ bool StickyChoice::Touch(const TouchInput &touch) {
if (IsFocusMovementEnabled())
SetFocusedView(this);
down_ = true;
Click();
ClickInternal();
return true;
}
}
@@ -417,7 +411,7 @@ bool StickyChoice::Key(const KeyInput &key) {
if (IsAcceptKey(key)) {
down_ = true;
UI::PlayUISound(UI::UISound::TOGGLE_ON);
Click();
ClickInternal();
return true;
}
}
@@ -469,8 +463,8 @@ void ClickableItem::Draw(UIContext &dc) {
DrawBG(dc, style);
}
void Choice::Click() {
ClickableItem::Click();
void Choice::ClickInternal() {
ClickableItem::ClickInternal();
UI::PlayUISound(UI::UISound::CONFIRM);
}
@@ -744,9 +738,8 @@ bool CheckBox::Toggled() const {
return false;
}
EventReturn CheckBox::OnClicked(EventParams &e) {
void CheckBox::ClickInternal() {
Toggle();
return EVENT_CONTINUE; // It's safe to keep processing events.
}
void CheckBox::Draw(UIContext &dc) {
@@ -908,8 +901,8 @@ std::string Button::DescribeText() const {
return ApplySafeSubstitutions(u->T("%1 button"), GetText());
}
void Button::Click() {
Clickable::Click();
void Button::ClickInternal() {
Clickable::ClickInternal();
UI::PlayUISound(UI::UISound::CONFIRM);
}
@@ -970,8 +963,8 @@ std::string RadioButton::DescribeText() const {
return ApplySafeSubstitutions(u->T("%1 radio button"), text_);
}
void RadioButton::Click() {
Clickable::Click();
void RadioButton::ClickInternal() {
Clickable::ClickInternal();
UI::PlayUISound(UI::UISound::CONFIRM);
*value_ = thisButtonValue_;
}
+7 -12
View File
@@ -252,10 +252,6 @@ struct EventParams {
std::string s;
};
struct HandlerRegistration {
std::function<EventReturn(EventParams&)> func;
};
class Event {
public:
Event() {}
@@ -276,7 +272,7 @@ public:
}
private:
std::vector<HandlerRegistration> handlers_;
std::function<EventReturn(EventParams&)> func_;
DISALLOW_COPY_AND_ASSIGN(Event);
};
@@ -541,7 +537,7 @@ protected:
// Internal method that fires on a click. Default behaviour is to trigger
// the event.
// Use it for checking/unchecking checkboxes, etc.
virtual void Click();
virtual void ClickInternal();
void DrawBG(UIContext &dc, const Style &style);
CallbackColorTween *bgColor_ = nullptr;
@@ -560,7 +556,6 @@ public:
Button(std::string_view text, ImageID imageID, LayoutParams *layoutParams = 0)
: Clickable(layoutParams), text_(text), imageID_(imageID) {}
void Click() override;
void Draw(UIContext &dc) override;
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
std::string_view GetText() const { return text_; }
@@ -580,6 +575,7 @@ public:
scale_ = f;
}
private:
void ClickInternal() override;
Style style_;
std::string text_;
ImageID imageID_;
@@ -593,12 +589,12 @@ class RadioButton : public Clickable {
public:
RadioButton(int *value, int thisButtonValue, std::string_view text, LayoutParams *layoutParams = 0)
: Clickable(layoutParams), value_(value), thisButtonValue_(thisButtonValue), text_(text) {}
void Click() override;
void Draw(UIContext &dc) override;
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
std::string DescribeText() const override;
private:
void ClickInternal() override;
int *value_;
int thisButtonValue_;
std::string text_;
@@ -731,7 +727,6 @@ public:
Choice(ImageID image, float imgScale, float imgRot, bool imgFlipH = false, LayoutParams *layoutParams = nullptr)
: ClickableItem(layoutParams), image_(image), rightIconImage_(ImageID::invalid()), imgScale_(imgScale), imgRot_(imgRot), imgFlipH_(imgFlipH) {}
void Click() override;
void GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert, float &w, float &h) const override;
void Draw(UIContext &dc) override;
std::string DescribeText() const override;
@@ -757,6 +752,7 @@ public:
}
protected:
void ClickInternal() override;
// hackery
virtual bool IsSticky() const { return false; }
@@ -890,25 +886,24 @@ class CheckBox : public ClickableItem {
public:
CheckBox(bool *toggle, std::string_view text, std::string_view smallText = "", LayoutParams *layoutParams = nullptr)
: ClickableItem(layoutParams), toggle_(toggle), text_(text), smallText_(smallText) {
OnClick.Handle(this, &CheckBox::OnClicked);
}
// Image-only "checkbox", lights up instead of showing a checkmark.
CheckBox(bool *toggle, ImageID imageID, LayoutParams *layoutParams = nullptr)
: ClickableItem(layoutParams), toggle_(toggle), imageID_(imageID) {
OnClick.Handle(this, &CheckBox::OnClicked);
}
void Draw(UIContext &dc) override;
std::string DescribeText() const override;
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
EventReturn OnClicked(EventParams &e);
//allow external agents to toggle the checkbox
virtual void Toggle();
virtual bool Toggled() const;
protected:
void ClickInternal() override;
bool *toggle_;
std::string text_;
std::string smallText_;
+1 -1
View File
@@ -732,7 +732,7 @@ void AchievementView::GetContentDimensions(const UIContext &dc, float &w, float
MeasureAchievement(dc, achievement_, AchievementRenderStyle::LISTED, &w, &h);
}
void AchievementView::Click() {
void AchievementView::ClickInternal() {
if (!achievement_) {
return;
}
+1 -1
View File
@@ -98,10 +98,10 @@ public:
layoutParams_->height = UI::WRAP_CONTENT; // Override the standard Item fixed height.
}
void Click() override;
void Draw(UIContext &dc) override;
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
private:
void ClickInternal() override;
const rc_client_achievement_t *achievement_;
};