2013-06-06 12:02:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-06-08 11:16:17 +08:00
|
|
|
#include "CCArmatureAnimation.h"
|
2013-06-06 12:02:54 +08:00
|
|
|
#include "../CCArmature.h"
|
|
|
|
#include "../CCBone.h"
|
|
|
|
#include "../utils/CCArmatureDefine.h"
|
|
|
|
#include "../utils/CCUtilMath.h"
|
|
|
|
#include "../utils/CCConstValue.h"
|
|
|
|
#include "../datas/CCDatas.h"
|
|
|
|
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-08 11:16:17 +08:00
|
|
|
CCArmatureAnimation *CCArmatureAnimation::create(CCArmature *armature)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-08 11:16:17 +08:00
|
|
|
CCArmatureAnimation *pArmatureAnimation = new CCArmatureAnimation();
|
|
|
|
if (pArmatureAnimation && pArmatureAnimation->init(armature))
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-08 11:16:17 +08:00
|
|
|
pArmatureAnimation->autorelease();
|
|
|
|
return pArmatureAnimation;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-08 11:16:17 +08:00
|
|
|
CC_SAFE_DELETE(pArmatureAnimation);
|
2013-06-06 12:02:54 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-08 11:16:17 +08:00
|
|
|
CCArmatureAnimation::CCArmatureAnimation()
|
2013-06-07 16:13:08 +08:00
|
|
|
: m_pAnimationData(NULL)
|
|
|
|
, m_pArmature(NULL)
|
2013-06-07 10:52:32 +08:00
|
|
|
, m_strMovementID("")
|
|
|
|
, m_iToIndex(0)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-08 11:16:17 +08:00
|
|
|
CCArmatureAnimation::~CCArmatureAnimation(void)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(m_pTweenList);
|
|
|
|
CC_SAFE_RELEASE_NULL(m_pAnimationData);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-08 11:16:17 +08:00
|
|
|
bool CCArmatureAnimation::init(CCArmature *armature)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
m_pArmature = armature;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
m_pTweenList = new CCArray();
|
|
|
|
m_pTweenList->init();
|
2013-06-06 12:02:54 +08:00
|
|
|
|
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
while (0);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-08 11:16:17 +08:00
|
|
|
void CCArmatureAnimation:: pause()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
CCObject *object = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pTweenList, object)
|
|
|
|
{
|
|
|
|
((CCTween *)object)->pause();
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
CCProcessBase::pause();
|
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-08 11:16:17 +08:00
|
|
|
void CCArmatureAnimation::resume()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
CCObject *object = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pTweenList, object)
|
|
|
|
{
|
|
|
|
((CCTween *)object)->resume();
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
CCProcessBase::resume();
|
|
|
|
}
|
|
|
|
|
2013-06-08 11:16:17 +08:00
|
|
|
void CCArmatureAnimation::stop()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
CCObject *object = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pTweenList, object)
|
|
|
|
{
|
|
|
|
((CCTween *)object)->stop();
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
m_pTweenList->removeAllObjects();
|
|
|
|
CCProcessBase::stop();
|
|
|
|
}
|
|
|
|
|
2013-06-08 11:16:17 +08:00
|
|
|
void CCArmatureAnimation::setAnimationScale(float animationScale )
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
if(animationScale == m_fAnimationScale)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
m_fAnimationScale = animationScale;
|
|
|
|
|
|
|
|
CCDictElement *element = NULL;
|
|
|
|
CCDictionary *dict = m_pArmature->getBoneDic();
|
|
|
|
CCDICT_FOREACH(dict, element)
|
|
|
|
{
|
|
|
|
CCBone *bone = (CCBone *)element->getObject();
|
|
|
|
bone->getTween()->setAnimationScale(m_fAnimationScale);
|
|
|
|
if (bone->getChildArmature())
|
|
|
|
{
|
|
|
|
bone->getChildArmature()->getAnimation()->setAnimationScale(m_fAnimationScale);
|
|
|
|
}
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-08 11:16:17 +08:00
|
|
|
void CCArmatureAnimation::play(const char *animationName, int durationTo, int durationTween, int loop, int tweenEasing)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
CCAssert(m_pAnimationData, "m_pAnimationData can not be null");
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
m_pMovementData = m_pAnimationData->getMovement(animationName);
|
2013-06-06 12:02:54 +08:00
|
|
|
CCAssert(m_pMovementData, "m_pMovementData can not be null");
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
//! Get key frame count
|
|
|
|
m_iRawDuration = m_pMovementData->duration;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
|
|
|
m_strMovementID = animationName;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
//! Further processing parameters
|
2013-06-06 12:02:54 +08:00
|
|
|
durationTo = (durationTo == -1) ? m_pMovementData->durationTo : durationTo;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
durationTween = (durationTween == -1) ? m_pMovementData->durationTween : durationTween;
|
|
|
|
durationTween = (durationTween == 0) ? m_pMovementData->duration : durationTween;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
tweenEasing = (tweenEasing == TWEEN_EASING_MAX) ? m_pMovementData->tweenEasing : tweenEasing;
|
|
|
|
loop = (loop < 0) ? m_pMovementData->loop : loop;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
CCProcessBase::play((void *)animationName, durationTo, durationTween, loop, tweenEasing);
|
|
|
|
|
|
|
|
|
|
|
|
if (m_iRawDuration == 1)
|
|
|
|
{
|
|
|
|
m_eLoopType = SINGLE_FRAME;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (loop)
|
|
|
|
{
|
|
|
|
m_eLoopType = ANIMATION_TO_LOOP_FRONT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_eLoopType = ANIMATION_NO_LOOP;
|
|
|
|
m_iRawDuration --;
|
|
|
|
}
|
|
|
|
m_iDurationTween = durationTween;
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:22:58 +08:00
|
|
|
CCMovementBoneData *movementBoneData = NULL;
|
2013-06-06 12:02:54 +08:00
|
|
|
m_pTweenList->removeAllObjects();
|
|
|
|
|
|
|
|
CCDictElement *element = NULL;
|
|
|
|
CCDictionary *dict = m_pArmature->getBoneDic();
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
CCDICT_FOREACH(dict, element)
|
|
|
|
{
|
|
|
|
CCBone *bone = (CCBone *)element->getObject();
|
|
|
|
movementBoneData = (CCMovementBoneData *)m_pMovementData->movBoneDataDic.objectForKey(bone->getName());
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
CCTween *tween = bone->getTween();
|
|
|
|
if(movementBoneData && movementBoneData->frameList.count() > 0)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
m_pTweenList->addObject(tween);
|
2013-06-06 12:02:54 +08:00
|
|
|
tween->play(movementBoneData, durationTo, durationTween, loop, tweenEasing);
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
tween->setAnimationScale(m_fAnimationScale);
|
|
|
|
if (bone->getChildArmature())
|
|
|
|
{
|
|
|
|
bone->getChildArmature()->getAnimation()->setAnimationScale(m_fAnimationScale);
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
if(!bone->getIgnoreMovementBoneData())
|
|
|
|
{
|
|
|
|
//! this bone is not include in this movement, so hide it
|
|
|
|
bone->getDisplayManager()->changeDisplayByIndex(-1, false);
|
|
|
|
tween->stop();
|
|
|
|
}
|
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
//m_pArmature->update(0);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-08 11:16:17 +08:00
|
|
|
void CCArmatureAnimation::playByIndex(int animationIndex, int durationTo, int durationTween, int loop, int tweenEasing)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
std::vector<std::string> &movName = m_pAnimationData->movementNames;
|
2013-06-07 17:10:53 +08:00
|
|
|
CC_ASSERT((animationIndex > -1) && ((unsigned int)animationIndex < movName.size()));
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
std::string animationName = movName.at(animationIndex);
|
|
|
|
play(animationName.c_str(), durationTo, durationTween, loop, tweenEasing);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-08 11:16:17 +08:00
|
|
|
int CCArmatureAnimation::getMovementCount()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
return m_pAnimationData->getMovementCount();
|
|
|
|
}
|
|
|
|
|
2013-06-08 11:16:17 +08:00
|
|
|
void CCArmatureAnimation::update(float dt)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
CCProcessBase::update(dt);
|
|
|
|
CCObject *object = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pTweenList, object)
|
|
|
|
{
|
|
|
|
((CCTween *)object)->update(dt);
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-08 11:16:17 +08:00
|
|
|
void CCArmatureAnimation::updateHandler()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
if (m_fCurrentPercent >= 1)
|
|
|
|
{
|
|
|
|
switch(m_eLoopType)
|
|
|
|
{
|
|
|
|
case ANIMATION_NO_LOOP:
|
|
|
|
{
|
|
|
|
m_eLoopType = ANIMATION_MAX;
|
|
|
|
m_fCurrentFrame = (m_fCurrentPercent - 1) * m_iNextFrameIndex;
|
|
|
|
m_fCurrentPercent = m_fCurrentFrame / m_iDurationTween;
|
|
|
|
|
|
|
|
if (m_fCurrentPercent >= 1.0f)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_iNextFrameIndex = m_iDurationTween;
|
|
|
|
|
|
|
|
MovementEventSignal.emit(m_pArmature, START, m_strMovementID.c_str());
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ANIMATION_MAX:
|
|
|
|
case SINGLE_FRAME:
|
|
|
|
{
|
|
|
|
m_fCurrentPercent = 1;
|
|
|
|
m_bIsComplete = true;
|
|
|
|
|
|
|
|
MovementEventSignal.emit(m_pArmature, COMPLETE, m_strMovementID.c_str());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ANIMATION_TO_LOOP_FRONT:
|
|
|
|
{
|
|
|
|
m_eLoopType = ANIMATION_LOOP_FRONT;
|
|
|
|
m_fCurrentPercent = fmodf(m_fCurrentPercent, 1);
|
|
|
|
m_fCurrentFrame = fmodf(m_fCurrentFrame, m_iNextFrameIndex);
|
|
|
|
m_iNextFrameIndex = m_iDurationTween > 0 ? m_iDurationTween : 1;
|
|
|
|
|
|
|
|
MovementEventSignal.emit(m_pArmature, START, m_strMovementID.c_str());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
m_fCurrentPercent = fmodf(m_fCurrentPercent, 1);
|
|
|
|
m_fCurrentFrame = fmodf(m_fCurrentFrame, m_iNextFrameIndex);
|
|
|
|
m_iToIndex = 0;
|
|
|
|
|
|
|
|
MovementEventSignal.emit(m_pArmature, LOOP_COMPLETE, m_strMovementID.c_str());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_eLoopType == ANIMATION_LOOP_FRONT || m_eLoopType == ANIMATION_LOOP_BACK)
|
|
|
|
{
|
|
|
|
updateFrameData(m_fCurrentPercent);
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-08 11:16:17 +08:00
|
|
|
void CCArmatureAnimation::updateFrameData(float currentPercent)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
m_iPrevFrameIndex = m_iCurFrameIndex;
|
|
|
|
m_iCurFrameIndex = m_iRawDuration * currentPercent;
|
|
|
|
m_iCurFrameIndex = m_iCurFrameIndex % m_iRawDuration;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
NS_CC_EXT_END
|