2012-07-31 17:41:53 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2014-04-03 17:02:17 +08:00
|
|
|
|
2012-07-31 17:41:53 +08:00
|
|
|
http://www.cocos2d-x.org
|
2014-04-03 17:02:17 +08:00
|
|
|
|
2012-07-31 17:41:53 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2014-04-03 17:02:17 +08:00
|
|
|
|
2012-07-31 17:41:53 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2014-04-03 17:02:17 +08:00
|
|
|
|
2012-07-31 17:41:53 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-03 17:02:17 +08:00
|
|
|
#ifndef __CCEVENTLISTENER_H__
|
|
|
|
#define __CCEVENTLISTENER_H__
|
2012-07-31 17:41:53 +08:00
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "base/CCPlatformMacros.h"
|
|
|
|
#include "base/CCRef.h"
|
2013-03-04 20:08:19 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
2013-10-23 11:27:24 +08:00
|
|
|
#include <set>
|
2012-07-31 17:41:53 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
NS_CC_BEGIN
|
2012-07-31 17:41:53 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
class Event;
|
2013-10-23 11:27:24 +08:00
|
|
|
class Node;
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2013-09-16 14:43:57 +08:00
|
|
|
/**
|
|
|
|
* The base class of event listener.
|
|
|
|
* If you need custom listener which with different callback, you need to inherit this class.
|
2013-10-21 17:22:42 +08:00
|
|
|
* For instance, you could refer to EventListenerAcceleration, EventListenerKeyboard, EventListenerTouchOneByOne, EventListenerCustom.
|
2013-09-16 14:43:57 +08:00
|
|
|
*/
|
2014-02-20 10:53:49 +08:00
|
|
|
class EventListener : public Ref
|
2013-10-25 10:35:48 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-10-28 16:00:01 +08:00
|
|
|
enum class Type
|
2013-10-25 10:35:48 +08:00
|
|
|
{
|
2013-10-28 16:00:01 +08:00
|
|
|
UNKNOWN,
|
|
|
|
TOUCH_ONE_BY_ONE,
|
|
|
|
TOUCH_ALL_AT_ONCE,
|
|
|
|
KEYBOARD,
|
2013-10-31 14:19:36 +08:00
|
|
|
MOUSE,
|
2013-10-28 16:00:01 +08:00
|
|
|
ACCELERATION,
|
|
|
|
CUSTOM
|
2013-10-25 10:35:48 +08:00
|
|
|
};
|
2014-04-03 17:02:17 +08:00
|
|
|
|
2013-12-20 19:31:08 +08:00
|
|
|
typedef std::string ListenerID;
|
2014-04-03 17:02:17 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
protected:
|
2013-09-16 14:43:57 +08:00
|
|
|
/** Constructor */
|
2013-09-13 18:00:56 +08:00
|
|
|
EventListener();
|
2014-04-03 17:02:17 +08:00
|
|
|
|
2013-09-16 14:43:57 +08:00
|
|
|
/** Initializes event with type and callback function */
|
2014-02-25 14:31:24 +08:00
|
|
|
bool init(Type t, const ListenerID& listenerID, const std::function<void(Event*)>& callback);
|
2013-09-03 18:22:03 +08:00
|
|
|
public:
|
2013-09-16 14:43:57 +08:00
|
|
|
/** Destructor */
|
2013-09-03 18:22:03 +08:00
|
|
|
virtual ~EventListener();
|
2014-04-03 17:02:17 +08:00
|
|
|
|
2013-09-16 14:43:57 +08:00
|
|
|
/** Checks whether the listener is available. */
|
2013-10-05 01:07:39 +08:00
|
|
|
virtual bool checkAvailable() = 0;
|
2014-04-03 17:02:17 +08:00
|
|
|
|
2013-09-16 14:43:57 +08:00
|
|
|
/** Clones the listener, its subclasses have to override this method. */
|
2013-09-19 09:14:51 +08:00
|
|
|
virtual EventListener* clone() = 0;
|
2014-04-03 15:32:16 +08:00
|
|
|
|
|
|
|
/** Enables or disables the listener
|
|
|
|
* @note Only listeners with `enabled` state will be able to receive events.
|
|
|
|
* When an listener was initialized, it's enabled by default.
|
2014-04-03 17:02:17 +08:00
|
|
|
* An event listener can receive events when it is enabled and is not paused.
|
|
|
|
* paused state is always false when it is a fixed priority listener.
|
2014-04-03 15:32:16 +08:00
|
|
|
*/
|
|
|
|
inline void setEnabled(bool enabled) { _isEnabled = enabled; };
|
|
|
|
|
2014-04-03 15:35:18 +08:00
|
|
|
/** Checks whether the listener is enabled */
|
2014-04-03 16:46:16 +08:00
|
|
|
inline bool isEnabled() const { return _isEnabled; };
|
2014-04-03 15:32:16 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
protected:
|
2014-04-03 15:32:16 +08:00
|
|
|
|
2014-04-03 17:02:17 +08:00
|
|
|
/** Sets paused state for the listener
|
2014-04-03 15:32:16 +08:00
|
|
|
* The paused state is only used for scene graph priority listeners.
|
|
|
|
* `EventDispatcher::resumeAllEventListenersForTarget(node)` will set the paused state to `true`,
|
|
|
|
* while `EventDispatcher::pauseAllEventListenersForTarget(node)` will set it to `false`.
|
2014-04-03 17:02:17 +08:00
|
|
|
* @note 1) Fixed priority listeners will never get paused. If a fixed priority doesn't want to receive events,
|
|
|
|
* call `setEnabled(false)` instead.
|
2014-04-03 17:20:16 +08:00
|
|
|
* 2) In `Node`'s onEnter and onExit, the `paused state` of the listeners which associated with that node will be automatically updated.
|
2014-04-03 15:32:16 +08:00
|
|
|
*/
|
2013-10-28 16:00:01 +08:00
|
|
|
inline void setPaused(bool paused) { _paused = paused; };
|
2014-04-03 15:32:16 +08:00
|
|
|
|
2014-04-03 15:35:18 +08:00
|
|
|
/** Checks whether the listener is paused */
|
2013-10-23 11:27:24 +08:00
|
|
|
inline bool isPaused() const { return _paused; };
|
2014-04-03 15:32:16 +08:00
|
|
|
|
|
|
|
/** Marks the listener was registered by EventDispatcher */
|
2013-10-28 16:00:01 +08:00
|
|
|
inline void setRegistered(bool registered) { _isRegistered = registered; };
|
2014-04-03 15:32:16 +08:00
|
|
|
|
|
|
|
/** Checks whether the listener was registered by EventDispatcher */
|
2013-10-24 11:17:29 +08:00
|
|
|
inline bool isRegistered() const { return _isRegistered; };
|
2014-04-03 15:32:16 +08:00
|
|
|
|
|
|
|
/** Gets the type of this listener
|
|
|
|
* @note It's different from `EventType`, e.g. TouchEvent has two kinds of event listeners - EventListenerOneByOne, EventListenerAllAtOnce
|
|
|
|
*/
|
2013-10-28 16:00:01 +08:00
|
|
|
inline Type getType() const { return _type; };
|
2014-04-03 15:32:16 +08:00
|
|
|
|
|
|
|
/** Gets the listener ID of this listener
|
|
|
|
* When event is being dispatched, listener ID is used as key for searching listeners according to event type.
|
|
|
|
*/
|
2013-12-20 19:31:08 +08:00
|
|
|
inline const ListenerID& getListenerID() const { return _listenerID; };
|
2014-04-03 15:32:16 +08:00
|
|
|
|
|
|
|
/** Sets the fixed priority for this listener
|
|
|
|
* @note This method is only used for `fixed priority listeners`, it needs to access a non-zero value.
|
|
|
|
* 0 is reserved for scene graph priority listeners
|
|
|
|
*/
|
2013-10-28 16:00:01 +08:00
|
|
|
inline void setFixedPriority(int fixedPriority) { _fixedPriority = fixedPriority; };
|
2014-04-03 15:32:16 +08:00
|
|
|
|
|
|
|
/** Gets the fixed priority of this listener
|
|
|
|
* @return 0 if it's a scene graph priority listener, non-zero for fixed priority listener
|
|
|
|
*/
|
2013-10-28 16:00:01 +08:00
|
|
|
inline int getFixedPriority() const { return _fixedPriority; };
|
2014-04-03 15:32:16 +08:00
|
|
|
|
2014-04-15 12:50:33 +08:00
|
|
|
/** Sets the node associated with this listener */
|
|
|
|
inline void setAssociatedNode(Node* node) { _node = node; };
|
2014-04-03 15:32:16 +08:00
|
|
|
|
2014-04-15 12:50:33 +08:00
|
|
|
/** Gets the node associated with this listener
|
|
|
|
* @return nullptr if it's a fixed priority listener, otherwise return non-nullptr
|
2014-04-03 15:32:16 +08:00
|
|
|
*/
|
2014-04-15 12:50:33 +08:00
|
|
|
inline Node* getAssociatedNode() const { return _node; };
|
2014-04-03 15:32:16 +08:00
|
|
|
|
|
|
|
///////////////
|
|
|
|
// Properties
|
|
|
|
//////////////
|
2013-09-19 09:14:51 +08:00
|
|
|
std::function<void(Event*)> _onEvent; /// Event callback function
|
2014-04-03 17:02:17 +08:00
|
|
|
|
2013-10-28 16:00:01 +08:00
|
|
|
Type _type; /// Event listener type
|
2014-04-03 15:32:16 +08:00
|
|
|
ListenerID _listenerID; /// Event listener ID
|
2013-10-28 16:00:01 +08:00
|
|
|
bool _isRegistered; /// Whether the listener has been added to dispatcher.
|
2014-04-03 17:02:17 +08:00
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
int _fixedPriority; // The higher the number, the higher the priority, 0 is for scene graph base priority.
|
2013-10-29 14:58:47 +08:00
|
|
|
Node* _node; // scene graph based priority
|
|
|
|
bool _paused; // Whether the listener is paused
|
2014-04-03 15:32:16 +08:00
|
|
|
bool _isEnabled; // Whether the listener is enabled
|
2013-09-03 18:22:03 +08:00
|
|
|
friend class EventDispatcher;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
2014-04-03 17:02:17 +08:00
|
|
|
#endif // __CCEVENTLISTENER_H__
|