axmol/cocos2dx/actions/CCActionManager.cpp

418 lines
9.9 KiB
C++
Raw Normal View History

2010-11-13 11:34:49 +08:00
/****************************************************************************
2011-03-19 10:34:26 +08:00
Copyright (c) 2010-2011 cocos2d-x.org
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2009 Valentin Milea
Copyright (c) 2011 Zynga Inc.
2011-03-19 10:34:26 +08:00
2010-11-13 11:34:49 +08:00
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 "CCActionManager.h"
#include "CCScheduler.h"
#include "ccMacros.h"
#include "support/data_support/ccCArray.h"
#include "support/data_support/uthash.h"
namespace cocos2d {
2010-08-05 14:37:36 +08:00
//
// singleton stuff
//
2010-11-13 11:34:49 +08:00
static CCActionManager *gSharedManager = NULL;
typedef struct _hashElement
{
struct _ccArray *actions;
CCObject *target;
2010-11-13 11:34:49 +08:00
unsigned int actionIndex;
CCAction *currentAction;
bool currentActionSalvaged;
bool paused;
UT_hash_handle hh;
2010-09-04 11:48:11 +08:00
} tHashElement;
2010-08-05 14:37:36 +08:00
2010-11-16 15:12:09 +08:00
CCActionManager* CCActionManager::sharedManager(void)
2010-08-05 14:37:36 +08:00
{
CCActionManager *pRet = gSharedManager;
if (! gSharedManager)
{
pRet = gSharedManager = new CCActionManager();
if (! gSharedManager->init())
{
// delete CCActionManager if init error
delete gSharedManager;
gSharedManager = NULL;
pRet = NULL;
}
}
return pRet;
}
void CCActionManager::purgeSharedManager(void)
{
2010-11-16 15:12:09 +08:00
CCScheduler::sharedScheduler()->unscheduleUpdateForTarget(this);
CC_SAFE_RELEASE(gSharedManager);
2010-08-05 14:37:36 +08:00
}
2010-08-30 15:45:39 +08:00
void CCActionManager::selectorProtocolRetain()
{
retain();
}
void CCActionManager::selectorProtocolRelease()
{
release();
}
2010-08-05 14:37:36 +08:00
CCActionManager::CCActionManager(void)
2011-06-10 17:51:37 +08:00
: m_pTargets(NULL),
m_pCurrentTarget(NULL),
m_bCurrentTargetSalvaged(false)
2010-08-05 14:37:36 +08:00
{
CCAssert(gSharedManager == NULL, "");
2010-08-05 14:37:36 +08:00
}
CCActionManager::~CCActionManager(void)
{
CCLOGINFO("cocos2d: deallocing %p", this);
removeAllActions();
// ?? do not delete , is it because purgeSharedManager() delete it?
gSharedManager = NULL;
}
bool CCActionManager::init(void)
2010-08-05 14:37:36 +08:00
{
2010-11-16 15:12:09 +08:00
CCScheduler::sharedScheduler()->scheduleUpdateForTarget(this, 0, false);
2010-08-05 14:37:36 +08:00
m_pTargets = NULL;
return true;
2010-08-05 14:37:36 +08:00
}
// private
void CCActionManager::deleteHashElement(tHashElement *pElement)
{
2010-08-09 11:46:35 +08:00
ccArrayFree(pElement->actions);
2010-08-05 14:37:36 +08:00
HASH_DEL(m_pTargets, pElement);
pElement->target->release();
free(pElement);
}
void CCActionManager::actionAllocWithHashElement(tHashElement *pElement)
{
// 4 actions per Node by default
if (pElement->actions == NULL)
{
2010-08-09 11:46:35 +08:00
pElement->actions = ccArrayNew(4);
2011-11-18 21:23:54 +08:00
} else if (pElement->actions->num == pElement->actions->max)
2010-08-09 11:46:35 +08:00
{
ccArrayDoubleCapacity(pElement->actions);
2010-08-05 14:37:36 +08:00
}
2010-08-09 11:46:35 +08:00
2010-08-05 14:37:36 +08:00
}
void CCActionManager::removeActionAtIndex(unsigned int uIndex, tHashElement *pElement)
{
2010-08-09 11:46:35 +08:00
CCAction *pAction = (CCAction*)pElement->actions->arr[uIndex];
2010-08-05 14:37:36 +08:00
if (pAction == pElement->currentAction && (! pElement->currentActionSalvaged))
{
pElement->currentAction->retain();
pElement->currentActionSalvaged = true;
}
2010-08-09 11:46:35 +08:00
ccArrayRemoveObjectAtIndex(pElement->actions, uIndex);
2010-08-05 14:37:36 +08:00
// update actionIndex in case we are in tick. looping over the actions
if (pElement->actionIndex >= uIndex)
{
pElement->actionIndex--;
}
2010-08-09 11:46:35 +08:00
if (pElement->actions->num == 0)
2010-08-05 14:37:36 +08:00
{
if (m_pCurrentTarget == pElement)
{
m_bCurrentTargetSalvaged = true;
}
else
{
deleteHashElement(pElement);
}
}
}
// pause / resume
void CCActionManager::pauseTarget(CCObject *pTarget)
2010-08-05 14:37:36 +08:00
{
tHashElement *pElement = NULL;
HASH_FIND_INT(m_pTargets, &pTarget, pElement);
if (pElement)
{
pElement->paused = true;
}
}
void CCActionManager::resumeTarget(CCObject *pTarget)
2010-08-05 14:37:36 +08:00
{
tHashElement *pElement = NULL;
HASH_FIND_INT(m_pTargets, &pTarget, pElement);
if (pElement)
{
pElement->paused = false;
}
}
// run
void CCActionManager::addAction(CCAction *pAction, CCNode *pTarget, bool paused)
2010-08-05 14:37:36 +08:00
{
CCAssert(pAction != NULL, "");
CCAssert(pTarget != NULL, "");
2010-08-05 14:37:36 +08:00
tHashElement *pElement = NULL;
// we should convert it to CCObject*, because we save it as CCObject*
CCObject *tmp = pTarget;
HASH_FIND_INT(m_pTargets, &tmp, pElement);
2010-08-05 14:37:36 +08:00
if (! pElement)
{
pElement = (tHashElement*)calloc(sizeof(*pElement), 1);
pElement->paused = paused;
pTarget->retain();
pElement->target = pTarget;
HASH_ADD_INT(m_pTargets, target, pElement);
}
2010-11-13 11:34:49 +08:00
actionAllocWithHashElement(pElement);
CCAssert(! ccArrayContainsObject(pElement->actions, pAction), "");
2010-11-13 11:34:49 +08:00
ccArrayAppendObject(pElement->actions, pAction);
pAction->startWithTarget(pTarget);
2010-08-05 14:37:36 +08:00
}
// remove
void CCActionManager::removeAllActions(void)
{
for (tHashElement *pElement = m_pTargets; pElement != NULL; )
{
CCObject *pTarget = pElement->target;
2010-08-05 14:37:36 +08:00
pElement = (tHashElement*)pElement->hh.next;
removeAllActionsFromTarget(pTarget);
}
}
void CCActionManager::removeAllActionsFromTarget(CCObject *pTarget)
2010-08-05 14:37:36 +08:00
{
// explicit null handling
if (pTarget == NULL)
{
return;
}
tHashElement *pElement = NULL;
HASH_FIND_INT(m_pTargets, &pTarget, pElement);
if (pElement)
{
2010-08-09 11:46:35 +08:00
if (ccArrayContainsObject(pElement->actions, pElement->currentAction) && (! pElement->currentActionSalvaged))
2010-08-05 14:37:36 +08:00
{
pElement->currentAction->retain();
pElement->currentActionSalvaged = true;
}
2010-08-09 11:46:35 +08:00
ccArrayRemoveAllObjects(pElement->actions);
2010-08-05 14:37:36 +08:00
if (m_pCurrentTarget == pElement)
{
m_bCurrentTargetSalvaged = true;
}
else
{
deleteHashElement(pElement);
}
}
else
{
2011-11-18 21:23:54 +08:00
// CCLOG("cocos2d: removeAllActionsFromTarget: Target not found");
2010-08-05 14:37:36 +08:00
}
}
void CCActionManager::removeAction(CCAction *pAction)
2010-08-05 14:37:36 +08:00
{
// explicit null handling
if (pAction == NULL)
{
return;
}
tHashElement *pElement = NULL;
CCObject *pTarget = pAction->getOriginalTarget();
2010-08-05 14:37:36 +08:00
HASH_FIND_INT(m_pTargets, &pTarget, pElement);
if (pElement)
{
2010-08-09 11:46:35 +08:00
unsigned int i = ccArrayGetIndexOfObject(pElement->actions, pAction);
if (UINT_MAX != i)
2010-08-05 14:37:36 +08:00
{
removeActionAtIndex(i, pElement);
}
}
else
{
CCLOG("cocos2d: removeAction: Target not found");
}
}
void CCActionManager::removeActionByTag(unsigned int tag, CCObject *pTarget)
2010-08-05 14:37:36 +08:00
{
CCAssert((int)tag != kCCActionTagInvalid, "");
CCAssert(pTarget != NULL, "");
2010-08-05 14:37:36 +08:00
tHashElement *pElement = NULL;
HASH_FIND_INT(m_pTargets, &pTarget, pElement);
if (pElement)
{
2010-08-09 11:46:35 +08:00
unsigned int limit = pElement->actions->num;
for (unsigned int i = 0; i < limit; ++i)
2010-08-05 14:37:36 +08:00
{
2010-08-09 11:46:35 +08:00
CCAction *pAction = (CCAction*)pElement->actions->arr[i];
2011-10-19 15:24:19 +08:00
if (pAction->getTag() == (int)tag && pAction->getOriginalTarget() == pTarget)
2010-08-05 14:37:36 +08:00
{
2011-07-08 15:28:13 +08:00
removeActionAtIndex(i, pElement);
break;
2010-08-05 14:37:36 +08:00
}
}
}
}
// get
CCAction* CCActionManager::getActionByTag(unsigned int tag, CCObject *pTarget)
2010-08-05 14:37:36 +08:00
{
CCAssert((int)tag != kCCActionTagInvalid, "");
2010-08-05 14:37:36 +08:00
tHashElement *pElement = NULL;
HASH_FIND_INT(m_pTargets, &pTarget, pElement);
if (pElement)
{
if (pElement->actions != NULL)
{
2010-08-09 11:46:35 +08:00
unsigned int limit = pElement->actions->num;
for (unsigned int i = 0; i < limit; ++i)
2010-08-05 14:37:36 +08:00
{
2010-08-09 11:46:35 +08:00
CCAction *pAction = (CCAction*)pElement->actions->arr[i];
2010-08-05 14:37:36 +08:00
2011-10-19 15:24:19 +08:00
if (pAction->getTag() == (int)tag)
2010-08-05 14:37:36 +08:00
{
return pAction;
}
}
}
CCLOG("cocos2d : getActionByTag: Action not found");
}
else
{
CCLOG("cocos2d : getActionByTag: Target not found");
}
return NULL;
}
unsigned int CCActionManager::numberOfRunningActionsInTarget(CCObject *pTarget)
2010-08-05 14:37:36 +08:00
{
tHashElement *pElement = NULL;
HASH_FIND_INT(m_pTargets, &pTarget, pElement);
if (pElement)
{
2010-08-09 11:46:35 +08:00
return pElement->actions ? pElement->actions->num : 0;
2010-08-05 14:37:36 +08:00
}
return 0;
}
// main loop
void CCActionManager::update(ccTime dt)
2010-08-05 14:37:36 +08:00
{
for (tHashElement *elt = m_pTargets; elt != NULL; )
{
m_pCurrentTarget = elt;
m_bCurrentTargetSalvaged = false;
if (! m_pCurrentTarget->paused)
{
// The 'actions' CCMutableArray may change while inside this loop.
2010-08-09 11:46:35 +08:00
for (m_pCurrentTarget->actionIndex = 0; m_pCurrentTarget->actionIndex < m_pCurrentTarget->actions->num;
m_pCurrentTarget->actionIndex++)
2010-08-05 14:37:36 +08:00
{
2010-08-09 11:46:35 +08:00
m_pCurrentTarget->currentAction = (CCAction*)m_pCurrentTarget->actions->arr[m_pCurrentTarget->actionIndex];
if (m_pCurrentTarget->currentAction == NULL)
{
continue;
}
2010-08-05 14:37:36 +08:00
m_pCurrentTarget->currentActionSalvaged = false;
m_pCurrentTarget->currentAction->step(dt);
if (m_pCurrentTarget->currentActionSalvaged)
{
// The currentAction told the node to remove it. To prevent the action from
// accidentally deallocating itself before finishing its step, we retained
// it. Now that step is done, it's safe to release it.
m_pCurrentTarget->currentAction->release();
2011-11-18 21:23:54 +08:00
} else if (m_pCurrentTarget->currentAction->isDone())
2010-08-05 14:37:36 +08:00
{
m_pCurrentTarget->currentAction->stop();
CCAction *pAction = m_pCurrentTarget->currentAction;
// Make currentAction nil to prevent removeAction from salvaging it.
m_pCurrentTarget->currentAction = NULL;
removeAction(pAction);
}
m_pCurrentTarget->currentAction = NULL;
}
}
// elt, at this moment, is still valid
// so it is safe to ask this here (issue #490)
elt = (tHashElement*)(elt->hh.next);
2010-08-09 11:46:35 +08:00
// only delete currentTarget if no actions were scheduled during the cycle (issue #481)
if (m_bCurrentTargetSalvaged && m_pCurrentTarget->actions->num == 0)
2010-08-05 14:37:36 +08:00
{
deleteHashElement(m_pCurrentTarget);
}
}
// issue #635
m_pCurrentTarget = NULL;
}
2011-11-18 21:23:54 +08:00
} // namespace cocos2d