2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2009 Valentin Milea
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCTouchDispatcher.h"
|
|
|
|
#include "CCTouchHandler.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "cocoa/CCArray.h"
|
|
|
|
#include "cocoa/CCSet.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCTouch.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "textures/CCTexture2D.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "support/data_support/ccCArray.h"
|
|
|
|
#include "ccMacros.h"
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used for sort
|
|
|
|
*/
|
2013-08-20 08:09:28 +08:00
|
|
|
#if 0
|
|
|
|
static int less(const RCPtr<Object>& p1, const RCPtr<Object>& p2)
|
|
|
|
{
|
|
|
|
Object *o1, *o2;
|
|
|
|
o1 = static_cast<Object*>(p1);
|
|
|
|
o2 = static_cast<Object*>(p2);
|
|
|
|
|
|
|
|
TouchHandler *h1, *h2;
|
|
|
|
h1 = static_cast<TouchHandler*>( o1 );
|
|
|
|
h2 = static_cast<TouchHandler*>( o2 );
|
|
|
|
return ( h1->getPriority() < h2->getPriority() );
|
|
|
|
}
|
|
|
|
#else
|
2013-06-20 14:13:12 +08:00
|
|
|
static int less(const Object* p1, const Object* p2)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return ((TouchHandler*)p1)->getPriority() < ((TouchHandler*)p2)->getPriority();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-08-20 08:09:28 +08:00
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool TouchDispatcher::isDispatchEvents(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _dispatchEvents;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TouchDispatcher::setDispatchEvents(bool bDispatchEvents)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_dispatchEvents = bDispatchEvents;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-06-20 14:13:12 +08:00
|
|
|
+(id) allocWithZone:(Zone *)zone
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
@synchronized(self) {
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(sharedDispatcher == nil, @"Attempted to allocate a second instance of a singleton.");
|
2012-04-19 14:35:52 +08:00
|
|
|
return [super allocWithZone:zone];
|
|
|
|
}
|
|
|
|
return nil; // on subsequent allocation attempts return nil
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool TouchDispatcher::init(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_dispatchEvents = true;
|
2013-06-20 14:13:12 +08:00
|
|
|
_targetedHandlers = Array::createWithCapacity(8);
|
2013-06-15 14:03:30 +08:00
|
|
|
_targetedHandlers->retain();
|
2013-06-20 14:13:12 +08:00
|
|
|
_standardHandlers = Array::createWithCapacity(4);
|
2013-06-15 14:03:30 +08:00
|
|
|
_standardHandlers->retain();
|
2013-06-20 14:13:12 +08:00
|
|
|
_handlersToAdd = Array::createWithCapacity(8);
|
2013-06-15 14:03:30 +08:00
|
|
|
_handlersToAdd->retain();
|
|
|
|
_handlersToRemove = ccCArrayNew(8);
|
|
|
|
|
|
|
|
_toRemove = false;
|
|
|
|
_toAdd = false;
|
|
|
|
_toQuit = false;
|
|
|
|
_locked = false;
|
|
|
|
|
|
|
|
_handlerHelperData[CCTOUCHBEGAN]._type = CCTOUCHBEGAN;
|
|
|
|
_handlerHelperData[CCTOUCHMOVED]._type = CCTOUCHMOVED;
|
|
|
|
_handlerHelperData[CCTOUCHENDED]._type = CCTOUCHENDED;
|
|
|
|
_handlerHelperData[CCTOUCHCANCELLED]._type = CCTOUCHCANCELLED;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TouchDispatcher::~TouchDispatcher(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_targetedHandlers);
|
|
|
|
CC_SAFE_RELEASE(_standardHandlers);
|
|
|
|
CC_SAFE_RELEASE(_handlersToAdd);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
ccCArrayFree(_handlersToRemove);
|
|
|
|
_handlersToRemove = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// handlers management
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
void TouchDispatcher::forceAddHandler(TouchHandler *pHandler, Array *pArray)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
unsigned int u = 0;
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObj = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
CCARRAY_FOREACH(pArray, pObj)
|
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
TouchHandler *h = static_cast<TouchHandler *>(pObj);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (h)
|
|
|
|
{
|
|
|
|
if (h->getPriority() < pHandler->getPriority())
|
|
|
|
{
|
|
|
|
++u;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (h->getDelegate() == pHandler->getDelegate())
|
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(0, "");
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pArray->insertObject(pHandler, u);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TouchDispatcher::addStandardDelegate(TouchDelegate *pDelegate, int nPriority)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
TouchHandler *pHandler = StandardTouchHandler::handlerWithDelegate(pDelegate, nPriority);
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _locked)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
forceAddHandler(pHandler, _standardHandlers);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
/* If pHandler is contained in _handlersToRemove, if so remove it from _handlersToRemove and return.
|
2012-04-19 14:35:52 +08:00
|
|
|
* Refer issue #752(cocos2d-x)
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
if (ccCArrayContainsValue(_handlersToRemove, pDelegate))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ccCArrayRemoveValue(_handlersToRemove, pDelegate);
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_handlersToAdd->addObject(pHandler);
|
|
|
|
_toAdd = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TouchDispatcher::addTargetedDelegate(TouchDelegate *pDelegate, int nPriority, bool bSwallowsTouches)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
TouchHandler *pHandler = TargetedTouchHandler::handlerWithDelegate(pDelegate, nPriority, bSwallowsTouches);
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _locked)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
forceAddHandler(pHandler, _targetedHandlers);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
/* If pHandler is contained in _handlersToRemove, if so remove it from _handlersToRemove and return.
|
2012-04-19 14:35:52 +08:00
|
|
|
* Refer issue #752(cocos2d-x)
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
if (ccCArrayContainsValue(_handlersToRemove, pDelegate))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ccCArrayRemoveValue(_handlersToRemove, pDelegate);
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_handlersToAdd->addObject(pHandler);
|
|
|
|
_toAdd = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TouchDispatcher::forceRemoveDelegate(TouchDelegate *pDelegate)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
TouchHandler *pHandler;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// XXX: remove it from both handlers ???
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
// remove handler from _standardHandlers
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_standardHandlers, pObj)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
pHandler = static_cast<TouchHandler*>(pObj);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pHandler && pHandler->getDelegate() == pDelegate)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_standardHandlers->removeObject(pHandler);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
// remove handler from _targetedHandlers
|
|
|
|
CCARRAY_FOREACH(_targetedHandlers, pObj)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
pHandler = static_cast<TouchHandler*>(pObj);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pHandler && pHandler->getDelegate() == pDelegate)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_targetedHandlers->removeObject(pHandler);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TouchDispatcher::removeDelegate(TouchDelegate *pDelegate)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if (pDelegate == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _locked)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
forceRemoveDelegate(pDelegate);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
/* If pHandler is contained in _handlersToAdd, if so remove it from _handlersToAdd and return.
|
2012-04-19 14:35:52 +08:00
|
|
|
* Refer issue #752(cocos2d-x)
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
TouchHandler *pHandler = findHandler(_handlersToAdd, pDelegate);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pHandler)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_handlersToAdd->removeObject(pHandler);
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
ccCArrayAppendValue(_handlersToRemove, pDelegate);
|
|
|
|
_toRemove = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TouchDispatcher::forceRemoveAllDelegates(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_standardHandlers->removeAllObjects();
|
|
|
|
_targetedHandlers->removeAllObjects();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TouchDispatcher::removeAllDelegates(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _locked)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
forceRemoveAllDelegates();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_toQuit = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TouchHandler* TouchDispatcher::findHandler(TouchDelegate *pDelegate)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_targetedHandlers, pObj)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
TouchHandler* pHandler = static_cast<TouchHandler*>(pObj);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pHandler->getDelegate() == pDelegate)
|
|
|
|
{
|
|
|
|
return pHandler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_standardHandlers, pObj)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
TouchHandler* pHandler = static_cast<TouchHandler*>(pObj);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pHandler->getDelegate() == pDelegate)
|
|
|
|
{
|
|
|
|
return pHandler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TouchHandler* TouchDispatcher::findHandler(Array* pArray, TouchDelegate *pDelegate)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(pArray != NULL && pDelegate != NULL, "");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObj = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
CCARRAY_FOREACH(pArray, pObj)
|
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
TouchHandler* pHandle = static_cast<TouchHandler*>(pObj);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pHandle->getDelegate() == pDelegate)
|
|
|
|
{
|
|
|
|
return pHandle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-08-20 08:09:28 +08:00
|
|
|
void TouchDispatcher::rearrangeHandlers(Array *array)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-08-22 11:12:09 +08:00
|
|
|
// std::sort(array->data->arr, array->data->arr + array->data->num, less);
|
|
|
|
std::sort( std::begin(*array), std::end(*array), less);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TouchDispatcher::setPriority(int nPriority, TouchDelegate *pDelegate)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(pDelegate != NULL, "");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TouchHandler *handler = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
handler = this->findHandler(pDelegate);
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(handler != NULL, "");
|
2012-06-07 11:38:59 +08:00
|
|
|
|
|
|
|
if (handler->getPriority() != nPriority)
|
|
|
|
{
|
|
|
|
handler->setPriority(nPriority);
|
2013-06-15 14:03:30 +08:00
|
|
|
this->rearrangeHandlers(_targetedHandlers);
|
|
|
|
this->rearrangeHandlers(_standardHandlers);
|
2012-06-07 11:38:59 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// dispatch events
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
void TouchDispatcher::touches(Set *pTouches, Event *pEvent, unsigned int uIndex)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(uIndex >= 0 && uIndex < 4, "");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Set *pMutableTouches;
|
2013-06-15 14:03:30 +08:00
|
|
|
_locked = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// optimization to prevent a mutable copy when it is not necessary
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned int uTargetedHandlersCount = _targetedHandlers->count();
|
|
|
|
unsigned int uStandardHandlersCount = _standardHandlers->count();
|
2012-04-19 14:35:52 +08:00
|
|
|
bool bNeedsMutableSet = (uTargetedHandlersCount && uStandardHandlersCount);
|
|
|
|
|
|
|
|
pMutableTouches = (bNeedsMutableSet ? pTouches->mutableCopy() : pTouches);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
struct ccTouchHandlerHelperData sHelper = _handlerHelperData[uIndex];
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// process the target handlers 1st
|
|
|
|
//
|
|
|
|
if (uTargetedHandlersCount > 0)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Touch *pTouch;
|
|
|
|
SetIterator setIter;
|
2012-04-19 14:35:52 +08:00
|
|
|
for (setIter = pTouches->begin(); setIter != pTouches->end(); ++setIter)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pTouch = (Touch *)(*setIter);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TargetedTouchHandler *pHandler = NULL;
|
|
|
|
Object* pObj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_targetedHandlers, pObj)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
pHandler = static_cast<TargetedTouchHandler*>(pObj);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (! pHandler)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bClaimed = false;
|
|
|
|
if (uIndex == CCTOUCHBEGAN)
|
|
|
|
{
|
|
|
|
bClaimed = pHandler->getDelegate()->ccTouchBegan(pTouch, pEvent);
|
|
|
|
|
|
|
|
if (bClaimed)
|
|
|
|
{
|
|
|
|
pHandler->getClaimedTouches()->addObject(pTouch);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
if (pHandler->getClaimedTouches()->containsObject(pTouch))
|
|
|
|
{
|
2012-09-17 15:02:24 +08:00
|
|
|
// moved ended canceled
|
2012-04-19 14:35:52 +08:00
|
|
|
bClaimed = true;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
switch (sHelper._type)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
case CCTOUCHMOVED:
|
|
|
|
pHandler->getDelegate()->ccTouchMoved(pTouch, pEvent);
|
|
|
|
break;
|
|
|
|
case CCTOUCHENDED:
|
|
|
|
pHandler->getDelegate()->ccTouchEnded(pTouch, pEvent);
|
|
|
|
pHandler->getClaimedTouches()->removeObject(pTouch);
|
|
|
|
break;
|
|
|
|
case CCTOUCHCANCELLED:
|
|
|
|
pHandler->getDelegate()->ccTouchCancelled(pTouch, pEvent);
|
|
|
|
pHandler->getClaimedTouches()->removeObject(pTouch);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bClaimed && pHandler->isSwallowsTouches())
|
|
|
|
{
|
|
|
|
if (bNeedsMutableSet)
|
|
|
|
{
|
|
|
|
pMutableTouches->removeObject(pTouch);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// process standard handlers 2nd
|
|
|
|
//
|
|
|
|
if (uStandardHandlersCount > 0 && pMutableTouches->count() > 0)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
StandardTouchHandler *pHandler = NULL;
|
2013-07-09 14:29:51 +08:00
|
|
|
Object* pObj = nullptr;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_standardHandlers, pObj)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
pHandler = static_cast<StandardTouchHandler*>(pObj);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (! pHandler)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
switch (sHelper._type)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
case CCTOUCHBEGAN:
|
|
|
|
pHandler->getDelegate()->ccTouchesBegan(pMutableTouches, pEvent);
|
|
|
|
break;
|
|
|
|
case CCTOUCHMOVED:
|
|
|
|
pHandler->getDelegate()->ccTouchesMoved(pMutableTouches, pEvent);
|
|
|
|
break;
|
|
|
|
case CCTOUCHENDED:
|
|
|
|
pHandler->getDelegate()->ccTouchesEnded(pMutableTouches, pEvent);
|
|
|
|
break;
|
|
|
|
case CCTOUCHCANCELLED:
|
|
|
|
pHandler->getDelegate()->ccTouchesCancelled(pMutableTouches, pEvent);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bNeedsMutableSet)
|
|
|
|
{
|
|
|
|
pMutableTouches->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Optimization. To prevent a [handlers copy] which is expensive
|
|
|
|
// the add/removes/quit is done after the iterations
|
|
|
|
//
|
2013-06-15 14:03:30 +08:00
|
|
|
_locked = false;
|
|
|
|
if (_toRemove)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_toRemove = false;
|
|
|
|
for (unsigned int i = 0; i < _handlersToRemove->num; ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
forceRemoveDelegate((TouchDelegate*)_handlersToRemove->arr[i]);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
ccCArrayRemoveAllValues(_handlersToRemove);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_toAdd)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_toAdd = false;
|
2013-06-20 14:13:12 +08:00
|
|
|
TouchHandler* pHandler = NULL;
|
2013-07-09 14:29:51 +08:00
|
|
|
Object* pObj = nullptr;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_handlersToAdd, pObj)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
pHandler = static_cast<TouchHandler*>(pObj);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (! pHandler)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
if (dynamic_cast<TargetedTouchHandler*>(pHandler) != NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
forceAddHandler(pHandler, _targetedHandlers);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
forceAddHandler(pHandler, _standardHandlers);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_handlersToAdd->removeAllObjects();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_toQuit)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_toQuit = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
forceRemoveAllDelegates();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TouchDispatcher::touchesBegan(Set *touches, Event *pEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_dispatchEvents)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
this->touches(touches, pEvent, CCTOUCHBEGAN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TouchDispatcher::touchesMoved(Set *touches, Event *pEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_dispatchEvents)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
this->touches(touches, pEvent, CCTOUCHMOVED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TouchDispatcher::touchesEnded(Set *touches, Event *pEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_dispatchEvents)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
this->touches(touches, pEvent, CCTOUCHENDED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TouchDispatcher::touchesCancelled(Set *touches, Event *pEvent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_dispatchEvents)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
this->touches(touches, pEvent, CCTOUCHCANCELLED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|