EventDispatcher.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 timeinterested;
  28. EventReceiversList wheelInterested;
  29. EventReceiversList doubleClickInterested;
  30. EventReceiversList textInterested;
  31. EventReceiversList panningInterested;
  32. EventReceiversList & getListForMouseButton(MouseButton button);
  33. void handleMouseButtonClick(EventReceiversList & interestedObjs, MouseButton btn, bool isPressed);
  34. template<typename Functor>
  35. void processLists(ui16 activityFlag, const Functor & cb);
  36. public:
  37. /// add specified UI element as interested. Uses unnamed enum from AEventsReceiver for activity flags
  38. void activateElement(AEventsReceiver * elem, ui16 activityFlag);
  39. /// removes specified UI element as interested for specified activities
  40. void deactivateElement(AEventsReceiver * elem, ui16 activityFlag);
  41. /// Regular timer event
  42. void dispatchTimer(uint32_t msPassed);
  43. /// Shortcut events (e.g. keyboard keys)
  44. void dispatchShortcutPressed(const std::vector<EShortcut> & shortcuts);
  45. void dispatchShortcutReleased(const std::vector<EShortcut> & shortcuts);
  46. /// Mouse events
  47. void dispatchMouseButtonPressed(const MouseButton & button, const Point & position);
  48. void dispatchMouseButtonReleased(const MouseButton & button, const Point & position);
  49. void dispatchMouseScrolled(const Point & distance, const Point & position);
  50. void dispatchMouseDoubleClick(const Point & position);
  51. void dispatchMouseMoved(const Point & distance);
  52. void dispatchGesturePanningStarted(const Point & initialPosition);
  53. void dispatchGesturePanningEnded(const Point & initialPosition, const Point & finalPosition);
  54. void dispatchGesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance);
  55. void dispatchGesturePinch(const Point & initialPosition, double distance);
  56. /// Text input events
  57. void dispatchTextInput(const std::string & text);
  58. void dispatchTextEditing(const std::string & text);
  59. };