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
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
// KeypadDispatcher
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2013-06-20 14:13:12 +08:00
|
|
|
KeypadDispatcher::KeypadDispatcher()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _locked(false)
|
|
|
|
, _toAdd(false)
|
|
|
|
, _toRemove(false)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
_delegates = Array::create();
|
2013-06-15 14:03:30 +08:00
|
|
|
_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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
KeypadDispatcher::~KeypadDispatcher()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void KeypadDispatcher::removeDelegate(KeypadDelegate* pDelegate)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void KeypadDispatcher::addDelegate(KeypadDelegate* pDelegate)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void KeypadDispatcher::forceAddDelegate(KeypadDelegate* pDelegate)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
KeypadHandler* pHandler = KeypadHandler::handlerWithDelegate(pDelegate);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (pHandler)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_delegates->addObject(pHandler);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void KeypadDispatcher::forceRemoveDelegate(KeypadDelegate* pDelegate)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
KeypadHandler* pHandler = NULL;
|
|
|
|
Object* pObj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_delegates, pObj)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pHandler = (KeypadHandler*)pObj;
|
2012-04-19 14:35:52 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool KeypadDispatcher::dispatchKeypadMSG(ccKeypadMSGType nMsgType)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
KeypadHandler* pHandler = NULL;
|
|
|
|
KeypadDelegate* pDelegate = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
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
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* 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);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
pHandler = (KeypadHandler*)pObj;
|
2012-04-19 14:35:52 +08:00
|
|
|
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-20 14:13:12 +08:00
|
|
|
forceRemoveDelegate((KeypadDelegate*)_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-20 14:13:12 +08:00
|
|
|
forceAddDelegate((KeypadDelegate*)_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
|