EventsReceiver.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. VCMI_LIB_NAMESPACE_END
  14. class EventDispatcher;
  15. enum class MouseButton;
  16. enum class EShortcut;
  17. using boost::logic::tribool;
  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. std::map<MouseButton, bool> currentMouseState;
  24. ui16 activeState;
  25. bool hoveredState;
  26. bool panningState;
  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. virtual void clickLeft(tribool down, bool previousState) {}
  33. virtual void clickRight(tribool down, bool previousState) {}
  34. virtual void clickDouble() {}
  35. /// Called when user pans screen by specified distance
  36. virtual void gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance) {}
  37. virtual void wheelScrolled(int distance) {}
  38. virtual void mouseMoved(const Point & cursorPosition) {}
  39. /// Called when UI element hover status changes
  40. virtual void hover(bool on) {}
  41. /// Called when UI element panning gesture status changes
  42. virtual void panning(bool on, const Point & initialPosition, const Point & finalPosition) {}
  43. virtual void textInputed(const std::string & enteredText) {}
  44. virtual void textEdited(const std::string & enteredText) {}
  45. virtual void keyPressed(EShortcut key) {}
  46. virtual void keyReleased(EShortcut key) {}
  47. virtual void tick(uint32_t msPassed) {}
  48. virtual bool captureThisKey(EShortcut key) = 0;
  49. /// If true, event of selected type in selected position will be processed by this element
  50. virtual bool receiveEvent(const Point & position, int eventType) const= 0;
  51. public:
  52. AEventsReceiver();
  53. virtual ~AEventsReceiver() = default;
  54. /// These are the arguments that can be used to determine what kind of input UI element will receive
  55. enum {LCLICK=1, RCLICK=2, HOVER=4, MOVE=8, KEYBOARD=16, TIME=32, GENERAL=64, WHEEL=128, DOUBLECLICK=256, TEXTINPUT=512, GESTURE_PANNING=1024, ALL=0xffff};
  56. /// Returns true if element is currently hovered by mouse
  57. bool isHovered() const;
  58. /// Returns true if panning/swiping gesture is currently active
  59. bool isPanning() const;
  60. /// Returns true if element is currently active and may receive events
  61. bool isActive() const;
  62. /// Returns true if particular mouse button was pressed when inside this element
  63. bool isMouseButtonPressed(MouseButton btn) const;
  64. };