EventsReceiver.h 3.8 KB

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