2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
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 "CCKeypadDispatcher.h"
|
|
|
|
#include "support/data_support/ccCArray.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// CCKeypadDispatcher
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
CCKeypadDispatcher::CCKeypadDispatcher()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _locked(false)
|
|
|
|
, _toAdd(false)
|
|
|
|
, _toRemove(false)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_delegates = CCArray::create();
|
|
|
|
_delegates->retain();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_handlersToAdd = ccCArrayNew(8);
|
|
|
|
_handlersToRemove = ccCArrayNew(8);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCKeypadDispatcher::~CCKeypadDispatcher()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_delegates);
|
|
|
|
if (_handlersToAdd)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ccCArrayFree(_handlersToAdd);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_handlersToRemove)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ccCArrayFree(_handlersToRemove);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCKeypadDispatcher::removeDelegate(CCKeypadDelegate* pDelegate)
|
|
|
|
{
|
|
|
|
if (!pDelegate)
|
|
|
|
{
|
|
|
|
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
|
|
|
ccCArrayAppendValue(_handlersToRemove, pDelegate);
|
|
|
|
_toRemove = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCKeypadDispatcher::addDelegate(CCKeypadDelegate* pDelegate)
|
|
|
|
{
|
|
|
|
if (!pDelegate)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _locked)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
forceAddDelegate(pDelegate);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ccCArrayAppendValue(_handlersToAdd, pDelegate);
|
|
|
|
_toAdd = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCKeypadDispatcher::forceAddDelegate(CCKeypadDelegate* pDelegate)
|
|
|
|
{
|
|
|
|
CCKeypadHandler* pHandler = CCKeypadHandler::handlerWithDelegate(pDelegate);
|
|
|
|
|
|
|
|
if (pHandler)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_delegates->addObject(pHandler);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCKeypadDispatcher::forceRemoveDelegate(CCKeypadDelegate* pDelegate)
|
|
|
|
{
|
|
|
|
CCKeypadHandler* pHandler = NULL;
|
|
|
|
CCObject* pObj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_delegates, pObj)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pHandler = (CCKeypadHandler*)pObj;
|
|
|
|
if (pHandler && pHandler->getDelegate() == pDelegate)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_delegates->removeObject(pHandler);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCKeypadDispatcher::dispatchKeypadMSG(ccKeypadMSGType nMsgType)
|
|
|
|
{
|
|
|
|
CCKeypadHandler* pHandler = NULL;
|
|
|
|
CCKeypadDelegate* pDelegate = NULL;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_locked = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_delegates->count() > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCObject* pObj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_delegates, pObj)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CC_BREAK_IF(!pObj);
|
|
|
|
|
|
|
|
pHandler = (CCKeypadHandler*)pObj;
|
|
|
|
pDelegate = pHandler->getDelegate();
|
|
|
|
|
|
|
|
switch (nMsgType)
|
|
|
|
{
|
|
|
|
case kTypeBackClicked:
|
|
|
|
pDelegate->keyBackClicked();
|
|
|
|
break;
|
|
|
|
case kTypeMenuClicked:
|
|
|
|
pDelegate->keyMenuClicked();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-15 14:03:30 +08:00
|
|
|
forceRemoveDelegate((CCKeypadDelegate*)_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;
|
|
|
|
for (unsigned int i = 0; i < _handlersToAdd->num; ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
forceAddDelegate((CCKeypadDelegate*)_handlersToAdd->arr[i]);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
ccCArrayRemoveAllValues(_handlersToAdd);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|