EventsReceiver.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. ui16 activeState;
  24. bool hoveredState;
  25. bool strongInterestState;
  26. std::map<MouseButton, bool> currentMouseState;
  27. void click(MouseButton btn, tribool down, bool previousState);
  28. protected:
  29. /// If set, UI element will receive all mouse movement events, even those outside this element
  30. void setMoveEventStrongInterest(bool on);
  31. /// Activates particular events for this UI element. Uses unnamed enum from this class
  32. void activateEvents(ui16 what);
  33. /// Deactivates particular events for this UI element. Uses unnamed enum from this class
  34. void deactivateEvents(ui16 what);
  35. virtual void clickLeft(tribool down, bool previousState) {}
  36. virtual void clickRight(tribool down, bool previousState) {}
  37. virtual void clickMiddle(tribool down, bool previousState) {}
  38. virtual void textInputed(const std::string & enteredText) {}
  39. virtual void textEdited(const std::string & enteredText) {}
  40. virtual void tick(uint32_t msPassed) {}
  41. virtual void wheelScrolled(bool down, bool in) {}
  42. virtual void mouseMoved(const Point & cursorPosition) {}
  43. virtual void hover(bool on) {}
  44. virtual void onDoubleClick() {}
  45. virtual void keyPressed(EShortcut key) {}
  46. virtual void keyReleased(EShortcut key) {}
  47. virtual bool captureThisKey(EShortcut key) = 0;
  48. virtual bool isInside(const Point & position) = 0;
  49. public:
  50. AEventsReceiver();
  51. virtual ~AEventsReceiver() = default;
  52. /// These are the arguments that can be used to determine what kind of input UI element will receive
  53. enum {LCLICK=1, RCLICK=2, HOVER=4, MOVE=8, KEYBOARD=16, TIME=32, GENERAL=64, WHEEL=128, DOUBLECLICK=256, TEXTINPUT=512, MCLICK=1024, ALL=0xffff};
  54. /// Returns true if element is currently hovered by mouse
  55. bool isHovered() const;
  56. /// Returns true if element is currently active and may receive events
  57. bool isActive() const;
  58. /// Returns true if particular event(s) is active for this element
  59. bool isActive(int flags) const;
  60. /// Returns true if particular mouse button was pressed when inside this element
  61. bool isMouseButtonPressed(MouseButton btn) const;
  62. };