mirror of https://github.com/axmolengine/axmol.git
[dispatcher] Renaming 'registerEventListenerXXX' to 'addEventListenerXXX'.
This commit is contained in:
parent
dd7bfd7b60
commit
997a5a36c1
|
@ -176,9 +176,9 @@ Node::~Node()
|
|||
|
||||
CC_SAFE_DELETE(_componentContainer);
|
||||
|
||||
for (auto iter = _eventIds.begin(); iter != _eventIds.end(); ++iter)
|
||||
for (auto iter = _eventlisteners.begin(); iter != _eventlisteners.end(); ++iter)
|
||||
{
|
||||
EventDispatcher::getInstance()->unregisterEventListener(*iter);
|
||||
EventDispatcher::getInstance()->removeEventListener(*iter);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1301,14 +1301,9 @@ void Node::updateEventPriorityIndex()
|
|||
_eventPriority = ++_globalEventPriorityIndex;
|
||||
}
|
||||
|
||||
void Node::addEventId(int eventId)
|
||||
void Node::associateEventListener(EventListener* listener)
|
||||
{
|
||||
_eventIds.insert(eventId);
|
||||
}
|
||||
|
||||
void Node::removeEventId(int eventId)
|
||||
{
|
||||
_eventIds.erase(eventId);
|
||||
_eventlisteners.insert(listener);
|
||||
}
|
||||
|
||||
// NodeRGBA
|
||||
|
|
|
@ -76,6 +76,8 @@ bool nodeComparisonLess(const RCPtr<Object>& pp1, const RCPtr<Object>& pp2);
|
|||
bool nodeComparisonLess(Object* p1, Object* p2);
|
||||
#endif
|
||||
|
||||
class EventListener;
|
||||
|
||||
/** @brief Node is the main element. Anything that gets drawn or contains things that get drawn is a Node.
|
||||
The most popular Nodes are: Scene, Layer, Sprite, Menu.
|
||||
|
||||
|
@ -1372,11 +1374,10 @@ private:
|
|||
friend class Director;
|
||||
friend class EventDispatcher;
|
||||
|
||||
void addEventId(int eventId);
|
||||
void removeEventId(int eventId);
|
||||
void associateEventListener(EventListener* listener);
|
||||
|
||||
static void resetEventPriorityIndex();
|
||||
std::set<int> _eventIds;
|
||||
std::set<EventListener*> _eventlisteners;
|
||||
|
||||
protected:
|
||||
|
||||
|
|
|
@ -51,8 +51,6 @@ private:
|
|||
int& _count;
|
||||
};
|
||||
|
||||
int _eventId = 0;
|
||||
|
||||
}
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
@ -94,15 +92,14 @@ void EventDispatcher::registerEventListenerWithItem(EventListenerItem* item)
|
|||
listenerList->push_front(item);
|
||||
}
|
||||
|
||||
int EventDispatcher::registerEventListenerWithSceneGraphPriority(EventListener* listener, Node* node)
|
||||
void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node)
|
||||
{
|
||||
CCASSERT(!listener->_isRegistered, "The listener has been registered.");
|
||||
|
||||
if (!listener->checkAvaiable())
|
||||
return 0;
|
||||
|
||||
return;
|
||||
|
||||
EventListenerItem* item = new EventListenerItem();
|
||||
item->id = ++_eventId;
|
||||
item->node = node;
|
||||
item->fixedPriority = 0;
|
||||
item->listener = listener;
|
||||
|
@ -112,20 +109,17 @@ int EventDispatcher::registerEventListenerWithSceneGraphPriority(EventListener*
|
|||
registerEventListenerWithItem(item);
|
||||
|
||||
_eventNodes.push_back(node);
|
||||
node->addEventId(item->id);
|
||||
|
||||
return item->id;
|
||||
node->associateEventListener(listener);
|
||||
}
|
||||
|
||||
int EventDispatcher::registerEventListenerWithFixedPriority(EventListener* listener, int fixedPriority)
|
||||
void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority)
|
||||
{
|
||||
CCASSERT(!listener->_isRegistered, "The listener has been registered.");
|
||||
|
||||
if (!listener->checkAvaiable())
|
||||
return 0;
|
||||
|
||||
return;
|
||||
|
||||
EventListenerItem* item = new EventListenerItem();
|
||||
item->id = ++_eventId;
|
||||
item->node = nullptr;
|
||||
item->fixedPriority = fixedPriority;
|
||||
item->listener = listener;
|
||||
|
@ -133,29 +127,29 @@ int EventDispatcher::registerEventListenerWithFixedPriority(EventListener* liste
|
|||
item->listener->_isRegistered = true;
|
||||
|
||||
registerEventListenerWithItem(item);
|
||||
|
||||
return item->id;
|
||||
}
|
||||
|
||||
void EventDispatcher::unregisterEventListener(int listenerId)
|
||||
void EventDispatcher::removeEventListener(EventListener* listener)
|
||||
{
|
||||
if (_listeners == nullptr || listenerId <= 0)
|
||||
if (_listeners == nullptr || listener == nullptr)
|
||||
return;
|
||||
|
||||
for (auto iter = _listeners->begin(); iter != _listeners->end();)
|
||||
{
|
||||
for (auto itemIter = iter->second->begin(); itemIter != iter->second->end(); ++itemIter)
|
||||
{
|
||||
if ((*itemIter)->id == listenerId)
|
||||
if ((*itemIter)->listener == listener)
|
||||
{
|
||||
(*itemIter)->id = 0;
|
||||
|
||||
if (_inDispatch == 0)
|
||||
{
|
||||
(*itemIter)->listener->release();
|
||||
delete (*itemIter);
|
||||
iter->second->remove(*itemIter);
|
||||
}
|
||||
else
|
||||
{
|
||||
(*itemIter)->listener = nullptr;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
@ -179,9 +173,9 @@ void EventDispatcher::unregisterEventListener(int listenerId)
|
|||
}
|
||||
}
|
||||
|
||||
void EventDispatcher::setPriorityWithSceneGraph(int listenerId, Node* node)
|
||||
void EventDispatcher::setPriorityWithSceneGraph(EventListener* listener, Node* node)
|
||||
{
|
||||
if (_listeners == nullptr || listenerId <= 0)
|
||||
if (_listeners == nullptr || listener == nullptr || node == nullptr)
|
||||
return;
|
||||
|
||||
for (auto iter = _listeners->begin(); iter != _listeners->end(); ++iter)
|
||||
|
@ -189,7 +183,7 @@ void EventDispatcher::setPriorityWithSceneGraph(int listenerId, Node* node)
|
|||
for (auto itemIter = iter->second->begin(); itemIter != iter->second->end(); ++itemIter)
|
||||
{
|
||||
auto item = *itemIter;
|
||||
if (item->id == listenerId)
|
||||
if (item->listener == listener)
|
||||
{
|
||||
// FIXME: fixed priority --> scene graph's priority.
|
||||
item->fixedPriority = 0;
|
||||
|
@ -200,9 +194,9 @@ void EventDispatcher::setPriorityWithSceneGraph(int listenerId, Node* node)
|
|||
}
|
||||
}
|
||||
|
||||
void EventDispatcher::setPriorityWithFixedValue(int listenerId, int fixedPriority)
|
||||
void EventDispatcher::setPriorityWithFixedValue(EventListener* listener, int fixedPriority)
|
||||
{
|
||||
if (_listeners == nullptr || listenerId <= 0)
|
||||
if (_listeners == nullptr || listener == nullptr)
|
||||
return;
|
||||
|
||||
for (auto iter = _listeners->begin(); iter != _listeners->end(); ++iter)
|
||||
|
@ -210,7 +204,7 @@ void EventDispatcher::setPriorityWithFixedValue(int listenerId, int fixedPriorit
|
|||
for (auto itemIter = iter->second->begin(); itemIter != iter->second->end(); ++itemIter)
|
||||
{
|
||||
auto item = *itemIter;
|
||||
if (item->id == listenerId)
|
||||
if (item->listener == listener)
|
||||
{
|
||||
// FIXME: scene graph's priority --> fixed priority.
|
||||
item->fixedPriority = fixedPriority;
|
||||
|
@ -308,7 +302,7 @@ void EventDispatcher::dispatchTouchEvent(TouchEvent* event)
|
|||
for (; oneByOneIter != oneByOnelisteners.end(); ++oneByOneIter)
|
||||
{
|
||||
// Skip if the listener was removed.
|
||||
if ((*oneByOneIter)->id == 0)
|
||||
if ((*oneByOneIter)->listener == nullptr)
|
||||
continue;
|
||||
|
||||
event->setCurrentTarget((*oneByOneIter)->node);
|
||||
|
@ -396,7 +390,7 @@ void EventDispatcher::dispatchTouchEvent(TouchEvent* event)
|
|||
for (auto allInOneIter = allInOnelisteners.begin(); allInOneIter != allInOnelisteners.end(); ++allInOneIter)
|
||||
{
|
||||
// Skip if the listener was removed.
|
||||
if ((*allInOneIter)->id == 0)
|
||||
if ((*allInOneIter)->listener == nullptr)
|
||||
continue;
|
||||
|
||||
event->setCurrentTarget((*allInOneIter)->node);
|
||||
|
@ -455,7 +449,7 @@ void EventDispatcher::removeUnregisteredListeners()
|
|||
while ( listenerItemIter != _listeners->end())
|
||||
{
|
||||
auto removeIterBegin = std::remove_if(listenerItemIter->second->begin(), listenerItemIter->second->end(), [](const EventListenerItem* item){
|
||||
return item->id == 0;
|
||||
return item->listener == nullptr;
|
||||
});
|
||||
|
||||
for (auto iter = removeIterBegin; iter != listenerItemIter->second->end(); ++iter)
|
||||
|
@ -577,4 +571,15 @@ void EventDispatcher::removeAllListeners()
|
|||
_listeners = nullptr;
|
||||
}
|
||||
|
||||
void EventDispatcher::setEnabled(bool isEnabled)
|
||||
{
|
||||
_isEnabled = isEnabled;
|
||||
}
|
||||
|
||||
|
||||
bool EventDispatcher::isEnabled() const
|
||||
{
|
||||
return _isEnabled;
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
|
|
|
@ -55,41 +55,39 @@ public:
|
|||
static EventDispatcher* getInstance();
|
||||
|
||||
/** Registers a callback function for an specified event with the priority of scene graph.
|
||||
* @return The unique ID for the listener.
|
||||
*/
|
||||
int registerEventListenerWithSceneGraphPriority(EventListener* listener, Node* node);
|
||||
void addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node);
|
||||
|
||||
/** Registers a callback function for an specified event with the fixed priority.
|
||||
* @return The unique ID for the listener.
|
||||
*/
|
||||
int registerEventListenerWithFixedPriority(EventListener* listener, int fixedPriority);
|
||||
void addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority);
|
||||
|
||||
/** Unregisters a callback function by the unique ID. */
|
||||
void unregisterEventListener(int listenerId);
|
||||
void removeEventListener(EventListener* listener);
|
||||
|
||||
/** Removes listeners by event type */
|
||||
void removeListenersForEventType(const std::string& eventType);
|
||||
|
||||
/** Removes all listeners */
|
||||
void removeAllListeners();
|
||||
|
||||
/** Sets listener's priority with node's draw order. */
|
||||
void setPriorityWithSceneGraph(int listenerId, Node* node);
|
||||
void setPriorityWithSceneGraph(EventListener* listener, Node* node);
|
||||
|
||||
/** Sets listener's priority with fixed value. */
|
||||
void setPriorityWithFixedValue(int listenerId, int fixedPriority);
|
||||
void setPriorityWithFixedValue(EventListener* listener, int fixedPriority);
|
||||
|
||||
/** Whether to enable dispatching events */
|
||||
void setEnabled(bool isEnabled) { _isEnabled = isEnabled; };
|
||||
void setEnabled(bool isEnabled);
|
||||
|
||||
/** Checks whether dispatching events is enabled */
|
||||
bool isEnabled() const { return _isEnabled; };
|
||||
bool isEnabled() const;
|
||||
|
||||
/** Dispatches the event
|
||||
* Also removes all EventListeners marked for deletion from the
|
||||
* event dispatcher list.
|
||||
*/
|
||||
void dispatchEvent(Event* event);
|
||||
|
||||
/** Removes listeners by event type */
|
||||
void removeListenersForEventType(const std::string& eventType);
|
||||
|
||||
/** Removes all listeners */
|
||||
void removeAllListeners();
|
||||
|
||||
public:
|
||||
/** Destructor of EventDispatcher */
|
||||
|
@ -98,7 +96,6 @@ public:
|
|||
private:
|
||||
struct EventListenerItem
|
||||
{
|
||||
int id;
|
||||
int fixedPriority; // The higher the number, the higher the priority
|
||||
Node* node; // Weak reference.
|
||||
EventListener* listener;
|
||||
|
|
|
@ -52,9 +52,9 @@ Layer::Layer()
|
|||
, _keyboardEnabled(false)
|
||||
, _touchMode(Touch::DispatchMode::ALL_AT_ONCE)
|
||||
, _swallowsTouches(true)
|
||||
, _touchListenerId(0)
|
||||
, _keyboardListenerId(0)
|
||||
, _accelerationId(0)
|
||||
, _touchListener(nullptr)
|
||||
, _keyboardListener(nullptr)
|
||||
, _accelerationListener(nullptr)
|
||||
{
|
||||
_ignoreAnchorPointForPosition = true;
|
||||
setAnchorPoint(Point(0.5f, 0.5f));
|
||||
|
@ -97,22 +97,9 @@ Layer *Layer::create()
|
|||
}
|
||||
|
||||
/// Touch and Accelerometer related
|
||||
//
|
||||
//void Layer::registerWithTouchDispatcher()
|
||||
//{
|
||||
// TouchDispatcher* pDispatcher = Director::getInstance()->getTouchDispatcher();
|
||||
//
|
||||
// if( _touchMode == Touch::DispatchMode::ALL_AT_ONCE ) {
|
||||
// pDispatcher->addStandardDelegate(this, 0);
|
||||
// } else {
|
||||
// pDispatcher->addTargetedDelegate(this, _touchPriority, _swallowsTouches);
|
||||
// }
|
||||
//}
|
||||
|
||||
void Layer::onRegisterTouchListener()
|
||||
{
|
||||
EventDispatcher::getInstance()->unregisterEventListener(_touchListenerId);
|
||||
|
||||
{
|
||||
if( _touchMode == Touch::DispatchMode::ALL_AT_ONCE ) {
|
||||
// Register Touch Event
|
||||
auto listener = TouchEventListener::create(Touch::DispatchMode::ALL_AT_ONCE);
|
||||
|
@ -122,7 +109,8 @@ void Layer::onRegisterTouchListener()
|
|||
listener->onTouchesEnded = CC_CALLBACK_2(Layer::onTouchesEnded, this);
|
||||
listener->onTouchesCancelled = CC_CALLBACK_2(Layer::onTouchesCancelled, this);
|
||||
|
||||
_touchListenerId = EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this);
|
||||
EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this);
|
||||
_touchListener = listener;
|
||||
} else {
|
||||
// Register Touch Event
|
||||
auto listener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE);
|
||||
|
@ -133,7 +121,8 @@ void Layer::onRegisterTouchListener()
|
|||
listener->onTouchEnded = CC_CALLBACK_2(Layer::onTouchEnded, this);
|
||||
listener->onTouchCancelled = CC_CALLBACK_2(Layer::onTouchCancelled, this);
|
||||
|
||||
_touchListenerId = EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this);
|
||||
EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this);
|
||||
_touchListener = listener;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -182,10 +171,8 @@ void Layer::setTouchEnabled(bool enabled)
|
|||
}
|
||||
else
|
||||
{
|
||||
// have problems?
|
||||
// Director::getInstance()->getTouchDispatcher()->removeDelegate(this);
|
||||
EventDispatcher::getInstance()->unregisterEventListener(_touchListenerId);
|
||||
_touchListenerId = 0;
|
||||
EventDispatcher::getInstance()->removeEventListener(_touchListener);
|
||||
_touchListener = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -248,12 +235,13 @@ void Layer::setAccelerometerEnabled(bool enabled)
|
|||
if (_running)
|
||||
{
|
||||
auto dispatcher = EventDispatcher::getInstance();
|
||||
dispatcher->unregisterEventListener(_accelerationId);
|
||||
dispatcher->removeEventListener(_accelerationListener);
|
||||
_accelerationListener = nullptr;
|
||||
|
||||
if (enabled)
|
||||
{
|
||||
auto listener = AccelerationEventListener::create(CC_CALLBACK_2(Layer::onAcceleration, this));
|
||||
dispatcher->registerEventListenerWithSceneGraphPriority(listener, this);
|
||||
_accelerationListener = AccelerationEventListener::create(CC_CALLBACK_2(Layer::onAcceleration, this));
|
||||
dispatcher->addEventListenerWithSceneGraphPriority(_accelerationListener, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -295,8 +283,9 @@ void Layer::setKeyboardEnabled(bool enabled)
|
|||
{
|
||||
_keyboardEnabled = enabled;
|
||||
|
||||
EventDispatcher::getInstance()->unregisterEventListener(_keyboardListenerId);
|
||||
_keyboardListenerId = 0;
|
||||
auto dispatcher = EventDispatcher::getInstance();
|
||||
dispatcher->removeEventListener(_keyboardListener);
|
||||
_keyboardListener = nullptr;
|
||||
|
||||
if (enabled)
|
||||
{
|
||||
|
@ -304,7 +293,8 @@ void Layer::setKeyboardEnabled(bool enabled)
|
|||
listener->onKeyPressed = CC_CALLBACK_2(Layer::onKeyPressed, this);
|
||||
listener->onKeyReleased = CC_CALLBACK_2(Layer::onKeyReleased, this);
|
||||
|
||||
_keyboardListenerId = EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this);
|
||||
dispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
||||
_keyboardListener = listener;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -326,9 +316,9 @@ void Layer::onEnter()
|
|||
if (_accelerometerEnabled)
|
||||
{
|
||||
auto dispatcher = EventDispatcher::getInstance();
|
||||
dispatcher->unregisterEventListener(_accelerationId);
|
||||
dispatcher->removeEventListener(_accelerationListener);
|
||||
auto listener = AccelerationEventListener::create(CC_CALLBACK_2(Layer::onAcceleration, this));
|
||||
dispatcher->registerEventListenerWithSceneGraphPriority(listener, this);
|
||||
dispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -336,16 +326,16 @@ void Layer::onExit()
|
|||
{
|
||||
auto dispatcher = EventDispatcher::getInstance();
|
||||
|
||||
dispatcher->unregisterEventListener(_touchListenerId);
|
||||
_touchListenerId = 0;
|
||||
dispatcher->removeEventListener(_touchListener);
|
||||
_touchListener = nullptr;
|
||||
|
||||
// remove this layer from the delegates who concern Accelerometer Sensor
|
||||
dispatcher->unregisterEventListener(_accelerationId);
|
||||
_accelerationId = 0;
|
||||
dispatcher->removeEventListener(_accelerationListener);
|
||||
_accelerationListener = nullptr;
|
||||
|
||||
// remove this layer from the delegates who concern the keypad msg
|
||||
dispatcher->unregisterEventListener(_keyboardListenerId);
|
||||
_keyboardListenerId = 0;
|
||||
dispatcher->removeEventListener(_keyboardListener);
|
||||
_keyboardListener = nullptr;
|
||||
|
||||
Node::onExit();
|
||||
}
|
||||
|
@ -355,9 +345,8 @@ void Layer::onEnterTransitionDidFinish()
|
|||
if (_accelerometerEnabled)
|
||||
{
|
||||
auto dispatcher = EventDispatcher::getInstance();
|
||||
dispatcher->unregisterEventListener(_accelerationId);
|
||||
auto listener = AccelerationEventListener::create(CC_CALLBACK_2(Layer::onAcceleration, this));
|
||||
dispatcher->registerEventListenerWithSceneGraphPriority(listener, this);
|
||||
_accelerationListener = AccelerationEventListener::create(CC_CALLBACK_2(Layer::onAcceleration, this));
|
||||
dispatcher->addEventListenerWithSceneGraphPriority(_accelerationListener, this);
|
||||
}
|
||||
|
||||
Node::onEnterTransitionDidFinish();
|
||||
|
|
|
@ -120,8 +120,6 @@ public:
|
|||
virtual bool isTouchEnabled() const;
|
||||
virtual void setTouchEnabled(bool value);
|
||||
|
||||
int getTouchListenerId() const { return _touchListenerId; };
|
||||
|
||||
virtual void setTouchMode(Touch::DispatchMode mode);
|
||||
virtual Touch::DispatchMode getTouchMode() const;
|
||||
|
||||
|
@ -182,9 +180,9 @@ protected:
|
|||
bool _touchEnabled;
|
||||
bool _accelerometerEnabled;
|
||||
bool _keyboardEnabled;
|
||||
int _touchListenerId;
|
||||
int _keyboardListenerId;
|
||||
int _accelerationId;
|
||||
EventListener* _touchListener;
|
||||
EventListener* _keyboardListener;
|
||||
EventListener* _accelerationListener;
|
||||
private:
|
||||
Touch::DispatchMode _touchMode;
|
||||
bool _swallowsTouches;
|
||||
|
|
|
@ -28,11 +28,11 @@ NS_CC_EXT_BEGIN
|
|||
|
||||
InputDelegate::InputDelegate(void)
|
||||
: _touchEnabled(false)
|
||||
, _touchListenerId(0)
|
||||
, _touchListener(nullptr)
|
||||
, _accelerometerEnabled(false)
|
||||
, _accelerometerListenerId(0)
|
||||
, _accelerometerListener(nullptr)
|
||||
, _keypadEnabled(false)
|
||||
, _keyboardListenerId(0)
|
||||
, _keyboardListener(nullptr)
|
||||
, _touchPriority(0)
|
||||
, _touchMode(Touch::DispatchMode::ALL_AT_ONCE)
|
||||
{
|
||||
|
@ -42,9 +42,9 @@ InputDelegate::InputDelegate(void)
|
|||
InputDelegate::~InputDelegate(void)
|
||||
{
|
||||
auto dispatcher = EventDispatcher::getInstance();
|
||||
dispatcher->unregisterEventListener(_touchListenerId);
|
||||
dispatcher->unregisterEventListener(_keyboardListenerId);
|
||||
dispatcher->unregisterEventListener(_accelerometerListenerId);
|
||||
dispatcher->removeEventListener(_touchListener);
|
||||
dispatcher->removeEventListener(_keyboardListener);
|
||||
dispatcher->removeEventListener(_accelerometerListener);
|
||||
}
|
||||
|
||||
bool InputDelegate::onTouchBegan(Touch *pTouch, Event *pEvent)
|
||||
|
@ -107,9 +107,7 @@ void InputDelegate::setTouchEnabled(bool enabled)
|
|||
{
|
||||
_touchEnabled = enabled;
|
||||
if (enabled)
|
||||
{
|
||||
EventDispatcher::getInstance()->unregisterEventListener(_touchListenerId);
|
||||
|
||||
{
|
||||
if( _touchMode == Touch::DispatchMode::ALL_AT_ONCE ) {
|
||||
// Register Touch Event
|
||||
auto listener = TouchEventListener::create(Touch::DispatchMode::ALL_AT_ONCE);
|
||||
|
@ -119,7 +117,8 @@ void InputDelegate::setTouchEnabled(bool enabled)
|
|||
listener->onTouchesEnded = CC_CALLBACK_2(InputDelegate::onTouchesEnded, this);
|
||||
listener->onTouchesCancelled = CC_CALLBACK_2(InputDelegate::onTouchesCancelled, this);
|
||||
|
||||
_touchListenerId = EventDispatcher::getInstance()->registerEventListenerWithFixedPriority(listener, 0);
|
||||
EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, 0);
|
||||
_touchListener = listener;
|
||||
} else {
|
||||
// Register Touch Event
|
||||
auto listener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE);
|
||||
|
@ -130,12 +129,13 @@ void InputDelegate::setTouchEnabled(bool enabled)
|
|||
listener->onTouchEnded = CC_CALLBACK_2(InputDelegate::onTouchEnded, this);
|
||||
listener->onTouchCancelled = CC_CALLBACK_2(InputDelegate::onTouchCancelled, this);
|
||||
|
||||
_touchListenerId = EventDispatcher::getInstance()->registerEventListenerWithFixedPriority(listener, _touchPriority);
|
||||
EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, _touchPriority);
|
||||
_touchListener = listener;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
EventDispatcher::getInstance()->unregisterEventListener(_touchListenerId);
|
||||
EventDispatcher::getInstance()->removeEventListener(_touchListener);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -190,11 +190,14 @@ void InputDelegate::setAccelerometerEnabled(bool enabled)
|
|||
_accelerometerEnabled = enabled;
|
||||
|
||||
auto dispatcher = EventDispatcher::getInstance();
|
||||
dispatcher->unregisterEventListener(_accelerometerListenerId);
|
||||
dispatcher->removeEventListener(_accelerometerListener);
|
||||
_accelerometerListener = nullptr;
|
||||
|
||||
if (enabled)
|
||||
{
|
||||
auto listener = AccelerationEventListener::create(CC_CALLBACK_2(InputDelegate::onAcceleration, this));
|
||||
_accelerometerListenerId = dispatcher->registerEventListenerWithFixedPriority(listener, 0);
|
||||
dispatcher->addEventListenerWithFixedPriority(listener, 0);
|
||||
_accelerometerListener = listener;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -210,13 +213,16 @@ void InputDelegate::setKeypadEnabled(bool enabled)
|
|||
{
|
||||
_keypadEnabled = enabled;
|
||||
|
||||
EventDispatcher::getInstance()->removeEventListener(_keyboardListener);
|
||||
|
||||
if (enabled)
|
||||
{
|
||||
auto listener = KeyboardEventListener::create();
|
||||
listener->onKeyPressed = CC_CALLBACK_2(InputDelegate::onKeyPressed, this);
|
||||
listener->onKeyReleased = CC_CALLBACK_2(InputDelegate::onKeyReleased, this);
|
||||
|
||||
_keyboardListenerId = EventDispatcher::getInstance()->registerEventListenerWithFixedPriority(listener, 0);
|
||||
EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, 0);
|
||||
_keyboardListener = listener;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -124,11 +124,11 @@ public:
|
|||
|
||||
protected:
|
||||
bool _touchEnabled;
|
||||
int _touchListenerId;
|
||||
EventListener* _touchListener;
|
||||
bool _accelerometerEnabled;
|
||||
int _accelerometerListenerId;
|
||||
EventListener* _accelerometerListener;
|
||||
bool _keypadEnabled;
|
||||
int _keyboardListenerId;
|
||||
EventListener* _keyboardListener;
|
||||
private:
|
||||
int _touchPriority;
|
||||
Touch::DispatchMode _touchMode;
|
||||
|
|
|
@ -98,21 +98,21 @@ bool HelloWorld::init()
|
|||
// dispatcher->removeAllListeners();
|
||||
};
|
||||
|
||||
|
||||
int layerTouchId = dispatcher->registerEventListenerWithSceneGraphPriority(layerTouchListener, this);
|
||||
int layerTouchId2 = dispatcher->registerEventListenerWithSceneGraphPriority(layerTouchListener->clone(), this);
|
||||
auto layerTouchListener2 = layerTouchListener->clone();
|
||||
dispatcher->addEventListenerWithSceneGraphPriority(layerTouchListener, this);
|
||||
dispatcher->addEventListenerWithSceneGraphPriority(layerTouchListener2, this);
|
||||
|
||||
auto spriteTouchListener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE);
|
||||
spriteTouchListener->setSwallowTouches(true);
|
||||
|
||||
spriteTouchListener->onTouchBegan = [=](Touch* touch, Event* evt){
|
||||
CCLOG("Touch sprite.... began... %d, drawOrder = %d", sprite->getZOrder(), sprite->getEventPriority());
|
||||
dispatcher->unregisterEventListener(layerTouchId);
|
||||
dispatcher->unregisterEventListener(layerTouchId2);
|
||||
dispatcher->removeEventListener(layerTouchListener);
|
||||
dispatcher->removeEventListener(layerTouchListener2);
|
||||
return false;
|
||||
};
|
||||
|
||||
dispatcher->registerEventListenerWithSceneGraphPriority(spriteTouchListener, sprite);
|
||||
dispatcher->addEventListenerWithSceneGraphPriority(spriteTouchListener, sprite);
|
||||
|
||||
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
|
@ -160,7 +160,7 @@ bool HelloWorld::init()
|
|||
|
||||
spriteItemTouchListener->setSwallowTouches(true);
|
||||
|
||||
dispatcher->registerEventListenerWithSceneGraphPriority(spriteItemTouchListener, sprite1);
|
||||
dispatcher->addEventListenerWithSceneGraphPriority(spriteItemTouchListener, sprite1);
|
||||
|
||||
|
||||
// EventDispatcher::getInstance()->registerEventListener(TouchEvent::EVENT_TYPE, [=](Event* evt){
|
||||
|
|
|
@ -87,8 +87,8 @@ bool MenuLayer::initWithEntryID(int entryId)
|
|||
listener->onTouchBegan = CC_CALLBACK_2(MenuLayer::onTouchBegan, this);
|
||||
listener->onTouchMoved = CC_CALLBACK_2(MenuLayer::onTouchMoved, this);
|
||||
|
||||
_touchListenerId = EventDispatcher::getInstance()->registerEventListenerWithFixedPriority(listener, 0);
|
||||
|
||||
EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, 0);
|
||||
_touchListener = listener;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -185,8 +185,6 @@ bool Box2DView::initWithEntryID(int entryId)
|
|||
|
||||
// Register Touch Event
|
||||
|
||||
// EventDispatcher::getInstance()->unregisterEventListener(this->getTouchEventId());
|
||||
|
||||
auto listener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE);
|
||||
listener->setSwallowTouches(true);
|
||||
|
||||
|
@ -194,7 +192,8 @@ bool Box2DView::initWithEntryID(int entryId)
|
|||
listener->onTouchMoved = CC_CALLBACK_2(Box2DView::onTouchMoved, this);
|
||||
listener->onTouchEnded = CC_CALLBACK_2(Box2DView::onTouchEnded, this);
|
||||
|
||||
_touchListenerId = EventDispatcher::getInstance()->registerEventListenerWithFixedPriority(listener, 10);
|
||||
EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, 10);
|
||||
_touchListener = listener;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -18,7 +18,6 @@ public:
|
|||
void nextCallback(Object* sender);
|
||||
void backCallback(Object* sender);
|
||||
|
||||
// virtual void registerWithTouchDispatcher();
|
||||
|
||||
virtual bool onTouchBegan(Touch* touch, Event* event);
|
||||
virtual void onTouchMoved(Touch* touch, Event* event);
|
||||
|
|
|
@ -72,7 +72,7 @@ private:
|
|||
listener->onTouchEnded = CC_CALLBACK_2(Button::onTouchEnded, this);
|
||||
listener->onTouchCancelled = CC_CALLBACK_2(Button::onTouchCancelled, this);
|
||||
|
||||
EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this);
|
||||
EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -448,7 +448,7 @@ void TestParticleDisplay::onEnter()
|
|||
|
||||
listener->onTouchBegan = CC_CALLBACK_2(TestParticleDisplay::onTouchBegan, this);
|
||||
|
||||
EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this);
|
||||
EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this);
|
||||
|
||||
}
|
||||
std::string TestParticleDisplay::title()
|
||||
|
@ -503,7 +503,7 @@ void TestUseMutiplePicture::onEnter()
|
|||
|
||||
listener->onTouchBegan = CC_CALLBACK_2(TestUseMutiplePicture::onTouchBegan, this);
|
||||
|
||||
EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this);
|
||||
EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this);
|
||||
|
||||
}
|
||||
std::string TestUseMutiplePicture::title()
|
||||
|
@ -651,7 +651,7 @@ void TestArmatureNesting::onEnter()
|
|||
|
||||
listener->onTouchBegan = CC_CALLBACK_2(TestArmatureNesting::onTouchBegan, this);
|
||||
|
||||
EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this);
|
||||
EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -577,9 +577,6 @@ RemoveMenuItemWhenMove::RemoveMenuItemWhenMove()
|
|||
|
||||
void RemoveMenuItemWhenMove::onRegisterTouchListener()
|
||||
{
|
||||
// Unregister touch event
|
||||
EventDispatcher::getInstance()->unregisterEventListener(_touchListenerId);
|
||||
|
||||
// Register Touch Event
|
||||
auto listener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE);
|
||||
listener->setSwallowTouches(false);
|
||||
|
@ -587,7 +584,8 @@ void RemoveMenuItemWhenMove::onRegisterTouchListener()
|
|||
listener->onTouchBegan = CC_CALLBACK_2(RemoveMenuItemWhenMove::onTouchBegan, this);
|
||||
listener->onTouchMoved = CC_CALLBACK_2(RemoveMenuItemWhenMove::onTouchMoved, this);
|
||||
|
||||
_touchListenerId = EventDispatcher::getInstance()->registerEventListenerWithFixedPriority(listener, -100);
|
||||
EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, -100);
|
||||
_touchListener = listener;
|
||||
}
|
||||
|
||||
void RemoveMenuItemWhenMove::goBack(Object *pSender)
|
||||
|
|
|
@ -177,9 +177,9 @@ void TouchableSpriteTest::onEnter()
|
|||
}
|
||||
};
|
||||
|
||||
dispatcher->registerEventListenerWithSceneGraphPriority(listener1, sprite1);
|
||||
dispatcher->registerEventListenerWithSceneGraphPriority(listener1->clone(), sprite2);
|
||||
dispatcher->registerEventListenerWithSceneGraphPriority(listener1->clone(), sprite3);
|
||||
dispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1);
|
||||
dispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2);
|
||||
dispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite3);
|
||||
}
|
||||
|
||||
std::string TouchableSpriteTest::title()
|
||||
|
|
|
@ -100,7 +100,8 @@ void TouchesPerformTest1::onEnter()
|
|||
listener->onTouchEnded = CC_CALLBACK_2(TouchesPerformTest1::onTouchEnded, this);
|
||||
listener->onTouchCancelled = CC_CALLBACK_2(TouchesPerformTest1::onTouchCancelled, this);
|
||||
|
||||
EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this);
|
||||
EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this);
|
||||
_touchListener = listener;
|
||||
}
|
||||
|
||||
std::string TouchesPerformTest1::title()
|
||||
|
@ -108,12 +109,6 @@ std::string TouchesPerformTest1::title()
|
|||
return "Targeted touches";
|
||||
}
|
||||
|
||||
//void TouchesPerformTest1::registerWithTouchDispatcher()
|
||||
//{
|
||||
// auto director = Director::getInstance();
|
||||
// director->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
|
||||
//}
|
||||
|
||||
bool TouchesPerformTest1::onTouchBegan(Touch* touch, Event* event)
|
||||
{
|
||||
numberOfTouchesB++;
|
||||
|
|
|
@ -134,7 +134,7 @@ KeyboardNotificationLayer::KeyboardNotificationLayer()
|
|||
listener->onTouchBegan = CC_CALLBACK_2(KeyboardNotificationLayer::onTouchBegan, this);
|
||||
listener->onTouchEnded = CC_CALLBACK_2(KeyboardNotificationLayer::onTouchEnded, this);
|
||||
|
||||
EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this);
|
||||
EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this);
|
||||
}
|
||||
|
||||
//void KeyboardNotificationLayer::registerWithTouchDispatcher()
|
||||
|
|
|
@ -37,9 +37,6 @@ void Paddle::onEnter()
|
|||
{
|
||||
Sprite::onEnter();
|
||||
|
||||
// auto director = Director::getInstance();
|
||||
// director->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
|
||||
|
||||
// Register Touch Event
|
||||
auto listener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE);
|
||||
listener->setSwallowTouches(true);
|
||||
|
@ -48,7 +45,7 @@ void Paddle::onEnter()
|
|||
listener->onTouchMoved = CC_CALLBACK_2(Paddle::onTouchMoved, this);
|
||||
listener->onTouchEnded = CC_CALLBACK_2(Paddle::onTouchEnded, this);
|
||||
|
||||
EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this);
|
||||
EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this);
|
||||
}
|
||||
|
||||
void Paddle::onExit()
|
||||
|
|
|
@ -133,7 +133,7 @@ TestController::TestController()
|
|||
listener->onTouchBegan = CC_CALLBACK_2(TestController::onTouchBegan, this);
|
||||
listener->onTouchMoved = CC_CALLBACK_2(TestController::onTouchMoved, this);
|
||||
|
||||
EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this);
|
||||
EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this);
|
||||
}
|
||||
|
||||
TestController::~TestController()
|
||||
|
|
|
@ -1 +1 @@
|
|||
b81aba469a4fa7d6ef527533671627541d195e73
|
||||
60d103435762fea41e137a0d126bc9a94ec71dc3
|
|
@ -218,7 +218,7 @@ private:
|
|||
typedef std::pair<JSObject*, JSTouchDelegate*> TouchDelegatePair;
|
||||
static TouchDelegateMap sTouchDelegateMap;
|
||||
bool _needUnroot;
|
||||
int _listenerId;
|
||||
TouchEventListener* _touchListener;
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue