From e9788e2904f00c60c1580f32c5f4dc2e3eb642a0 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Sun, 11 Jun 2023 18:27:42 +0300 Subject: [PATCH] Cleaned up EventDispatcher code --- client/gui/EventDispatcher.cpp | 67 ++++++++++++++++------------------ client/gui/EventDispatcher.h | 5 +-- 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/client/gui/EventDispatcher.cpp b/client/gui/EventDispatcher.cpp index 21e8f9dd3..21c73dc5b 100644 --- a/client/gui/EventDispatcher.cpp +++ b/client/gui/EventDispatcher.cpp @@ -112,18 +112,6 @@ void EventDispatcher::dispatchShortcutReleased(const std::vector & sh } } -EventDispatcher::EventReceiversList & EventDispatcher::getListForMouseButton(MouseButton button) -{ - switch (button) - { - case MouseButton::LEFT: - return lclickable; - case MouseButton::RIGHT: - return rclickable; - } - throw std::runtime_error("Invalid mouse button in getListForMouseButton"); -} - void EventDispatcher::dispatchMouseDoubleClick(const Point & position) { bool doubleClicked = false; @@ -147,49 +135,58 @@ void EventDispatcher::dispatchMouseDoubleClick(const Point & position) void EventDispatcher::dispatchMouseButtonPressed(const MouseButton & button, const Point & position) { - handleMouseButtonClick(getListForMouseButton(button), button, true); + if (button == MouseButton::LEFT) + handleLeftButtonClick(true); + if (button == MouseButton::RIGHT) + handleRightButtonClick(true); } void EventDispatcher::dispatchMouseButtonReleased(const MouseButton & button, const Point & position) { - handleMouseButtonClick(getListForMouseButton(button), button, false); + if (button == MouseButton::LEFT) + handleLeftButtonClick(false); + if (button == MouseButton::RIGHT) + handleRightButtonClick(false); } -void EventDispatcher::handleMouseButtonClick(EventReceiversList & interestedObjs, MouseButton btn, bool isPressed) +void EventDispatcher::handleRightButtonClick(bool isPressed) { - auto hlp = interestedObjs; + auto hlp = rclickable; for(auto & i : hlp) { - if(!vstd::contains(interestedObjs, i)) + if(!vstd::contains(rclickable, i)) continue; - auto prev = i->isMouseButtonPressed(btn); + if( isPressed && i->receiveEvent(GH.getCursorPosition(), AEventsReceiver::LCLICK)) + i->showPopupWindow(); if(!isPressed) - i->currentMouseState[btn] = isPressed; + i->closePopupWindow(); + } +} - if( btn == MouseButton::LEFT && i->receiveEvent(GH.getCursorPosition(), AEventsReceiver::LCLICK)) +void EventDispatcher::handleLeftButtonClick(bool isPressed) +{ + auto hlp = lclickable; + for(auto & i : hlp) + { + if(!vstd::contains(lclickable, i)) + continue; + + auto prev = i->isMouseButtonPressed(MouseButton::LEFT); + + if(!isPressed) + i->currentMouseState[MouseButton::LEFT] = isPressed; + + if( i->receiveEvent(GH.getCursorPosition(), AEventsReceiver::LCLICK)) { if(isPressed) - i->currentMouseState[btn] = isPressed; + i->currentMouseState[MouseButton::LEFT] = isPressed; i->clickLeft(isPressed, prev); } - else if( btn == MouseButton::RIGHT && i->receiveEvent(GH.getCursorPosition(), AEventsReceiver::RCLICK)) - { - if(isPressed) - i->currentMouseState[btn] = isPressed; - - if (isPressed) - i->showPopupWindow(); - else - i->closePopupWindow(); - } else if(!isPressed) { - if (btn == MouseButton::LEFT) - i->clickLeft(boost::logic::indeterminate, prev); - if (btn == MouseButton::RIGHT) - i->closePopupWindow(); + i->clickLeft(boost::logic::indeterminate, prev); } } } diff --git a/client/gui/EventDispatcher.h b/client/gui/EventDispatcher.h index b93a3c529..e73f5e4b9 100644 --- a/client/gui/EventDispatcher.h +++ b/client/gui/EventDispatcher.h @@ -34,9 +34,8 @@ class EventDispatcher EventReceiversList textInterested; EventReceiversList panningInterested; - EventReceiversList & getListForMouseButton(MouseButton button); - - void handleMouseButtonClick(EventReceiversList & interestedObjs, MouseButton btn, bool isPressed); + void handleLeftButtonClick(bool isPressed); + void handleRightButtonClick(bool isPressed); template void processLists(ui16 activityFlag, const Functor & cb);