EventsReceiver.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * EventsReceiver.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. class Rect;
  14. VCMI_LIB_NAMESPACE_END
  15. class EventDispatcher;
  16. enum class EShortcut;
  17. /// Class that is capable of subscribing and receiving input events
  18. /// Acts as base class for all UI elements
  19. class AEventsReceiver
  20. {
  21. friend class EventDispatcher;
  22. ui16 activeState;
  23. bool hoveredState;
  24. bool panningState;
  25. bool mouseClickedState;
  26. protected:
  27. /// Activates particular events for this UI element. Uses unnamed enum from this class
  28. void activateEvents(ui16 what);
  29. /// Deactivates particular events for this UI element. Uses unnamed enum from this class
  30. void deactivateEvents(ui16 what);
  31. /// allows capturing key input so it will be delivered only to this element
  32. virtual bool captureThisKey(EShortcut key) = 0;
  33. /// If true, event of selected type in selected position will be processed by this element
  34. virtual bool receiveEvent(const Point & position, int eventType) const= 0;
  35. virtual const Rect & getPosition() const= 0;
  36. public:
  37. virtual void clickPressed(const Point & cursorPosition) {}
  38. virtual void clickReleased(const Point & cursorPosition) {}
  39. virtual void clickPressed(const Point & cursorPosition, bool lastActivated);
  40. virtual void clickReleased(const Point & cursorPosition, bool lastActivated);
  41. virtual void clickCancel(const Point & cursorPosition) {}
  42. virtual void showPopupWindow(const Point & cursorPosition) {}
  43. virtual void clickDouble(const Point & cursorPosition) {}
  44. /// Called when user pans screen by specified distance
  45. virtual void gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance) {}
  46. /// Called when user pitches screen, requesting scaling by specified factor
  47. virtual void gesturePinch(const Point & centerPosition, double lastUpdateFactor) {}
  48. virtual void wheelScrolled(int distance) {}
  49. virtual void mouseMoved(const Point & cursorPosition, const Point & lastUpdateDistance) {}
  50. virtual void mouseDragged(const Point & cursorPosition, const Point & lastUpdateDistance) {}
  51. /// Called when UI element hover status changes
  52. virtual void hover(bool on) {}
  53. /// Called when UI element gesture status changes
  54. virtual void gesture(bool on, const Point & initialPosition, const Point & finalPosition) {}
  55. virtual void textInputed(const std::string & enteredText) {}
  56. virtual void textEdited(const std::string & enteredText) {}
  57. virtual void keyPressed(EShortcut key) {}
  58. virtual void keyReleased(EShortcut key) {}
  59. virtual void tick(uint32_t msPassed) {}
  60. public:
  61. AEventsReceiver();
  62. virtual ~AEventsReceiver() = default;
  63. /// These are the arguments that can be used to determine what kind of input UI element will receive
  64. enum
  65. {
  66. LCLICK = 1,
  67. SHOW_POPUP = 2,
  68. HOVER = 4,
  69. MOVE = 8,
  70. KEYBOARD = 16,
  71. TIME = 32,
  72. GENERAL = 64,
  73. WHEEL = 128,
  74. DOUBLECLICK = 256,
  75. TEXTINPUT = 512,
  76. GESTURE = 1024,
  77. DRAG = 2048,
  78. };
  79. /// Returns true if element is currently hovered by mouse
  80. bool isHovered() const;
  81. /// Returns true if panning/swiping gesture is currently active
  82. bool isGesturing() const;
  83. /// Returns true if element is currently active and may receive events
  84. bool isActive() const;
  85. };