axmol/cocos2dx/actions/CCActionManager.cpp

399 lines
11 KiB
C++
Raw Normal View History

2010-11-13 11:34:49 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
2011-03-19 10:34:26 +08:00
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 "base_nodes/CCNode.h"
2010-11-13 11:34:49 +08:00
#include "CCScheduler.h"
#include "ccMacros.h"
#include "support/data_support/ccCArray.h"
#include "support/data_support/uthash.h"
#include "cocoa/CCSet.h"
2010-11-13 11:34:49 +08:00
2012-04-18 18:43:45 +08:00
NS_CC_BEGIN
2010-08-05 14:37:36 +08:00
//
// singleton stuff
//
2010-11-13 11:34:49 +08:00
typedef struct _hashElement
{
struct _ccArray *actions;
CCObject *target;
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
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
{
2010-08-05 14:37:36 +08:00
}
CCActionManager::~CCActionManager(void)
{
CCLOGINFO("cocos2d: deallocing %p", this);
2010-08-05 14:37:36 +08:00
removeAllActions();
2010-08-05 14:37:36 +08:00
}
// private
void CCActionManager::deleteHashElement(tHashElement *pElement)
{
ccArrayFree(pElement->actions);
HASH_DEL(m_pTargets, pElement);
pElement->target->release();
free(pElement);
2010-08-05 14:37:36 +08:00
}
void CCActionManager::actionAllocWithHashElement(tHashElement *pElement)
{
// 4 actions per Node by default
if (pElement->actions == NULL)
{
pElement->actions = ccArrayNew(4);
}else
if (pElement->actions->num == pElement->actions->max)
{
ccArrayDoubleCapacity(pElement->actions);
}
2010-08-09 11:46:35 +08:00
2010-08-05 14:37:36 +08:00
}
void CCActionManager::removeActionAtIndex(unsigned int uIndex, tHashElement *pElement)
{
CCAction *pAction = (CCAction*)pElement->actions->arr[uIndex];
if (pAction == pElement->currentAction && (! pElement->currentActionSalvaged))
{
pElement->currentAction->retain();
pElement->currentActionSalvaged = true;
}
ccArrayRemoveObjectAtIndex(pElement->actions, uIndex, true);
// update actionIndex in case we are in tick. looping over the actions
if (pElement->actionIndex >= uIndex)
{
pElement->actionIndex--;
}
if (pElement->actions->num == 0)
{
if (m_pCurrentTarget == pElement)
{
m_bCurrentTargetSalvaged = true;
}
else
{
deleteHashElement(pElement);
}
}
2010-08-05 14:37:36 +08:00
}
// 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;
}
2010-08-05 14:37:36 +08:00
}
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;
}
2010-08-05 14:37:36 +08:00
}
CCSet* CCActionManager::pauseAllRunningActions()
2012-06-08 13:55:28 +08:00
{
CCSet *idsWithActions = new CCSet();
idsWithActions->autorelease();
for (tHashElement *element=m_pTargets; element != NULL; element = (tHashElement *)element->hh.next)
{
if (! element->paused)
{
element->paused = true;
idsWithActions->addObject(element->target);
}
}
return idsWithActions;
}
void CCActionManager::resumeTargets(cocos2d::CCSet *targetsToResume)
{
CCSetIterator iter;
for (iter = targetsToResume->begin(); iter != targetsToResume->end(); ++iter)
{
resumeTarget(*iter);
}
}
2010-08-05 14:37:36 +08:00
// run
void CCActionManager::addAction(CCAction *pAction, CCNode *pTarget, bool paused)
2010-08-05 14:37:36 +08:00
{
CCAssert(pAction != NULL, "");
CCAssert(pTarget != NULL, "");
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);
if (! pElement)
{
pElement = (tHashElement*)calloc(sizeof(*pElement), 1);
pElement->paused = paused;
pTarget->retain();
pElement->target = pTarget;
HASH_ADD_INT(m_pTargets, target, pElement);
}
actionAllocWithHashElement(pElement);
2010-11-13 11:34:49 +08:00
CCAssert(! ccArrayContainsObject(pElement->actions, pAction), "");
ccArrayAppendObject(pElement->actions, pAction);
2010-11-13 11:34:49 +08:00
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;
pElement = (tHashElement*)pElement->hh.next;
removeAllActionsFromTarget(pTarget);
}
2010-08-05 14:37:36 +08:00
}
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)
{
if (ccArrayContainsObject(pElement->actions, pElement->currentAction) && (! pElement->currentActionSalvaged))
{
pElement->currentAction->retain();
pElement->currentActionSalvaged = true;
}
ccArrayRemoveAllObjects(pElement->actions);
if (m_pCurrentTarget == pElement)
{
m_bCurrentTargetSalvaged = true;
}
else
{
deleteHashElement(pElement);
}
}
else
{
// 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();
HASH_FIND_INT(m_pTargets, &pTarget, pElement);
if (pElement)
{
unsigned int i = ccArrayGetIndexOfObject(pElement->actions, pAction);
if (UINT_MAX != i)
{
removeActionAtIndex(i, pElement);
}
}
else
{
CCLOG("cocos2d: removeAction: Target not found");
}
2010-08-05 14:37:36 +08:00
}
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);
2010-08-05 14:37:36 +08:00
if (pElement)
{
unsigned int limit = pElement->actions->num;
for (unsigned int i = 0; i < limit; ++i)
{
CCAction *pAction = (CCAction*)pElement->actions->arr[i];
2010-08-09 11:46:35 +08:00
2011-10-19 15:24:19 +08:00
if (pAction->getTag() == (int)tag && pAction->getOriginalTarget() == pTarget)
{
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);
2010-08-05 14:37:36 +08:00
if (pElement)
{
if (pElement->actions != NULL)
{
unsigned int limit = pElement->actions->num;
for (unsigned int i = 0; i < limit; ++i)
{
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)
{
return pAction;
}
}
}
CCLOG("cocos2d : getActionByTag: Action not found");
}
else
{
// CCLOG("cocos2d : getActionByTag: Target not found");
}
2010-08-05 14:37:36 +08:00
return NULL;
2010-08-05 14:37:36 +08:00
}
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)
{
return pElement->actions ? pElement->actions->num : 0;
}
return 0;
2010-08-05 14:37:36 +08:00
}
// main loop
2012-06-08 13:55:28 +08:00
void CCActionManager::update(float 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.
for (m_pCurrentTarget->actionIndex = 0; m_pCurrentTarget->actionIndex < m_pCurrentTarget->actions->num;
m_pCurrentTarget->actionIndex++)
{
m_pCurrentTarget->currentAction = (CCAction*)m_pCurrentTarget->actions->arr[m_pCurrentTarget->actionIndex];
if (m_pCurrentTarget->currentAction == NULL)
{
continue;
}
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();
} else
if (m_pCurrentTarget->currentAction->isDone())
{
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);
// only delete currentTarget if no actions were scheduled during the cycle (issue #481)
if (m_bCurrentTargetSalvaged && m_pCurrentTarget->actions->num == 0)
{
deleteHashElement(m_pCurrentTarget);
}
}
// issue #635
m_pCurrentTarget = NULL;
2010-08-05 14:37:36 +08:00
}
2012-04-18 18:43:45 +08:00
NS_CC_END