CCEventListener.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __CCEVENTLISTENER_H__
  22. #define __CCEVENTLISTENER_H__
  23. #include <functional>
  24. #include <string>
  25. #include <memory>
  26. #include "platform/CCPlatformMacros.h"
  27. #include "base/CCRef.h"
  28. /**
  29. * @addtogroup base
  30. * @{
  31. */
  32. NS_CC_BEGIN
  33. class Event;
  34. class Node;
  35. /** @class EventListener
  36. * @brief The base class of event listener.
  37. * If you need custom listener which with different callback, you need to inherit this class.
  38. * For instance, you could refer to EventListenerAcceleration, EventListenerKeyboard, EventListenerTouchOneByOne, EventListenerCustom.
  39. */
  40. class CC_DLL EventListener : public Ref
  41. {
  42. public:
  43. /** Type Event type.*/
  44. enum class Type
  45. {
  46. UNKNOWN,
  47. TOUCH_ONE_BY_ONE,
  48. TOUCH_ALL_AT_ONCE,
  49. KEYBOARD,
  50. MOUSE,
  51. ACCELERATION,
  52. FOCUS,
  53. GAME_CONTROLLER,
  54. CUSTOM
  55. };
  56. typedef std::string ListenerID;
  57. CC_CONSTRUCTOR_ACCESS:
  58. /**
  59. * Constructor
  60. * @js ctor
  61. */
  62. EventListener();
  63. /**
  64. * Initializes event with type and callback function
  65. * @js NA
  66. */
  67. bool init(Type t, const ListenerID& listenerID, const std::function<void(Event*)>& callback);
  68. public:
  69. /** Destructor.
  70. * @js NA
  71. */
  72. virtual ~EventListener();
  73. /** Checks whether the listener is available.
  74. *
  75. * @return True if the listener is available.
  76. */
  77. virtual bool checkAvailable() = 0;
  78. /** Clones the listener, its subclasses have to override this method.
  79. */
  80. virtual EventListener* clone() = 0;
  81. /** Enables or disables the listener.
  82. * @note Only listeners with `enabled` state will be able to receive events.
  83. * When an listener was initialized, it's enabled by default.
  84. * An event listener can receive events when it is enabled and is not paused.
  85. * paused state is always false when it is a fixed priority listener.
  86. *
  87. * @param enabled True if enables the listener.
  88. */
  89. void setEnabled(bool enabled) { _isEnabled = enabled; }
  90. /** Checks whether the listener is enabled.
  91. *
  92. * @return True if the listener is enabled.
  93. */
  94. bool isEnabled() const { return _isEnabled; }
  95. protected:
  96. /** Sets paused state for the listener
  97. * The paused state is only used for scene graph priority listeners.
  98. * `EventDispatcher::resumeAllEventListenersForTarget(node)` will set the paused state to `true`,
  99. * while `EventDispatcher::pauseAllEventListenersForTarget(node)` will set it to `false`.
  100. * @note 1) Fixed priority listeners will never get paused. If a fixed priority doesn't want to receive events,
  101. * call `setEnabled(false)` instead.
  102. * 2) In `Node`'s onEnter and onExit, the `paused state` of the listeners which associated with that node will be automatically updated.
  103. */
  104. void setPaused(bool paused) { _paused = paused; }
  105. /** Checks whether the listener is paused */
  106. bool isPaused() const { return _paused; }
  107. /** Marks the listener was registered by EventDispatcher */
  108. void setRegistered(bool registered) { _isRegistered = registered; }
  109. /** Checks whether the listener was registered by EventDispatcher */
  110. bool isRegistered() const { return _isRegistered; }
  111. /** Gets the type of this listener
  112. * @note It's different from `EventType`, e.g. TouchEvent has two kinds of event listeners - EventListenerOneByOne, EventListenerAllAtOnce
  113. */
  114. Type getType() const { return _type; }
  115. /** Gets the listener ID of this listener
  116. * When event is being dispatched, listener ID is used as key for searching listeners according to event type.
  117. */
  118. const ListenerID& getListenerID() const { return _listenerID; }
  119. /** Sets the fixed priority for this listener
  120. * @note This method is only used for `fixed priority listeners`, it needs to access a non-zero value.
  121. * 0 is reserved for scene graph priority listeners
  122. */
  123. void setFixedPriority(int fixedPriority) { _fixedPriority = fixedPriority; }
  124. /** Gets the fixed priority of this listener
  125. * @return 0 if it's a scene graph priority listener, non-zero for fixed priority listener
  126. */
  127. int getFixedPriority() const { return _fixedPriority; }
  128. /** Sets the node associated with this listener */
  129. void setAssociatedNode(Node* node) { _node = node; }
  130. /** Gets the node associated with this listener
  131. * @return nullptr if it's a fixed priority listener, otherwise return non-nullptr
  132. */
  133. Node* getAssociatedNode() const { return _node; }
  134. ///////////////
  135. // Properties
  136. //////////////
  137. std::function<void(Event*)> _onEvent; /// Event callback function
  138. Type _type; /// Event listener type
  139. ListenerID _listenerID; /// Event listener ID
  140. bool _isRegistered; /// Whether the listener has been added to dispatcher.
  141. int _fixedPriority; // The higher the number, the higher the priority, 0 is for scene graph base priority.
  142. Node* _node; // scene graph based priority
  143. bool _paused; // Whether the listener is paused
  144. bool _isEnabled; // Whether the listener is enabled
  145. friend class EventDispatcher;
  146. };
  147. NS_CC_END
  148. // end of base group
  149. /// @}
  150. #endif // __CCEVENTLISTENER_H__