EventDispatcher.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * EventDispatcher.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. VCMI_LIB_NAMESPACE_BEGIN
  12. class Point;
  13. VCMI_LIB_NAMESPACE_END
  14. class AEventsReceiver;
  15. enum class MouseButton;
  16. enum class EShortcut;
  17. /// Class that receives events from event producers and dispatches it to UI elements that are interested in this event
  18. class EventDispatcher
  19. {
  20. using EventReceiversList = std::list<AEventsReceiver *>;
  21. /// list of UI elements that are interested in particular event
  22. EventReceiversList lclickable;
  23. EventReceiversList rclickable;
  24. EventReceiversList hoverable;
  25. EventReceiversList keyinterested;
  26. EventReceiversList motioninterested;
  27. EventReceiversList draginterested;
  28. EventReceiversList timeinterested;
  29. EventReceiversList wheelInterested;
  30. EventReceiversList doubleClickInterested;
  31. EventReceiversList textInterested;
  32. EventReceiversList panningInterested;
  33. void handleLeftButtonClick(const Point & position, int tolerance, bool isPressed);
  34. void handleDoubleButtonClick(const Point & position, int tolerance);
  35. AEventsReceiver * findElementInToleranceRange(const EventReceiversList & list, const Point & position, int eventToTest, int tolerance);
  36. template<typename Functor>
  37. void processLists(ui16 activityFlag, const Functor & cb);
  38. public:
  39. /// add specified UI element as interested. Uses unnamed enum from AEventsReceiver for activity flags
  40. void activateElement(AEventsReceiver * elem, ui16 activityFlag);
  41. /// removes specified UI element as interested for specified activities
  42. void deactivateElement(AEventsReceiver * elem, ui16 activityFlag);
  43. /// Regular timer event
  44. void dispatchTimer(uint32_t msPassed);
  45. /// Shortcut events (e.g. keyboard keys)
  46. void dispatchShortcutPressed(const std::vector<EShortcut> & shortcuts);
  47. void dispatchShortcutReleased(const std::vector<EShortcut> & shortcuts);
  48. /// Mouse events
  49. void dispatchMouseLeftButtonPressed(const Point & position, int tolerance);
  50. void dispatchMouseLeftButtonReleased(const Point & position, int tolerance);
  51. void dispatchMouseScrolled(const Point & distance, const Point & position);
  52. void dispatchMouseDoubleClick(const Point & position, int tolerance);
  53. void dispatchMouseMoved(const Point & distance, const Point & position);
  54. void dispatchMouseDragged(const Point & currentPosition, const Point & lastUpdateDistance);
  55. void dispatchShowPopup(const Point & position, int tolerance);
  56. void dispatchClosePopup(const Point & position);
  57. void dispatchGesturePanningStarted(const Point & initialPosition);
  58. void dispatchGesturePanningEnded(const Point & initialPosition, const Point & finalPosition);
  59. void dispatchGesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance);
  60. void dispatchGesturePinch(const Point & initialPosition, double distance);
  61. /// Text input events
  62. void dispatchTextInput(const std::string & text);
  63. void dispatchTextEditing(const std::string & text);
  64. };