/**************************************************************************** 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. ****************************************************************************/ #include "CCAnimation.h" #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 CCAnimation *CCAnimation::create(CCArmature *armature) { CCAnimation *pARmatureAnimation = new CCAnimation(); if (pARmatureAnimation && pARmatureAnimation->init(armature)) { pARmatureAnimation->autorelease(); return pARmatureAnimation; } CC_SAFE_DELETE(pARmatureAnimation); return NULL; } CCAnimation::CCAnimation() : m_pAnimationData(NULL) , m_pArmature(NULL) , m_strMovementID("") , m_iToIndex(0) { } CCAnimation::~CCAnimation(void) { CC_SAFE_RELEASE_NULL(m_pTweenList); CC_SAFE_RELEASE_NULL(m_pAnimationData); } bool CCAnimation::init(CCArmature *armature) { bool bRet = false; do { m_pArmature = armature; m_pTweenList = new CCArray(); m_pTweenList->init(); bRet = true; } while (0); return bRet; } void CCAnimation:: pause() { CCObject *object = NULL; CCARRAY_FOREACH(m_pTweenList, object) { ((CCTween *)object)->pause(); } CCProcessBase::pause(); } void CCAnimation::resume() { CCObject *object = NULL; CCARRAY_FOREACH(m_pTweenList, object) { ((CCTween *)object)->resume(); } CCProcessBase::resume(); } void CCAnimation::stop() { CCObject *object = NULL; CCARRAY_FOREACH(m_pTweenList, object) { ((CCTween *)object)->stop(); } m_pTweenList->removeAllObjects(); CCProcessBase::stop(); } void CCAnimation::setAnimationScale(float animationScale ) { if(animationScale == m_fAnimationScale) { return; } 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); } } } void CCAnimation::play(const char *animationName, int durationTo, int durationTween, int loop, int tweenEasing) { CCAssert(m_pAnimationData, "m_pAnimationData can not be null"); m_pMovementData = m_pAnimationData->getMovement(animationName); CCAssert(m_pMovementData, "m_pMovementData can not be null"); //! Get key frame count m_iRawDuration = m_pMovementData->duration; m_strMovementID = animationName; //! Further processing parameters durationTo = (durationTo == -1) ? m_pMovementData->durationTo : durationTo; durationTween = (durationTween == -1) ? m_pMovementData->durationTween : durationTween; durationTween = (durationTween == 0) ? m_pMovementData->duration : durationTween; tweenEasing = (tweenEasing == TWEEN_EASING_MAX) ? m_pMovementData->tweenEasing : tweenEasing; loop = (loop < 0) ? m_pMovementData->loop : loop; 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; } CCMovementBoneData *movementBoneData = NULL; m_pTweenList->removeAllObjects(); CCDictElement *element = NULL; CCDictionary *dict = m_pArmature->getBoneDic(); CCDICT_FOREACH(dict, element) { CCBone *bone = (CCBone *)element->getObject(); movementBoneData = (CCMovementBoneData *)m_pMovementData->movBoneDataDic.objectForKey(bone->getName()); CCTween *tween = bone->getTween(); if(movementBoneData && movementBoneData->frameList.count() > 0) { m_pTweenList->addObject(tween); tween->play(movementBoneData, durationTo, durationTween, loop, tweenEasing); tween->setAnimationScale(m_fAnimationScale); if (bone->getChildArmature()) { bone->getChildArmature()->getAnimation()->setAnimationScale(m_fAnimationScale); } } else { if(!bone->getIgnoreMovementBoneData()) { //! this bone is not include in this movement, so hide it bone->getDisplayManager()->changeDisplayByIndex(-1, false); tween->stop(); } } } //m_pArmature->update(0); } void CCAnimation::playByIndex(int animationIndex, int durationTo, int durationTween, int loop, int tweenEasing) { std::vector &movName = m_pAnimationData->movementNames; CC_ASSERT((animationIndex > -1) && ((unsigned int)animationIndex < movName.size())); std::string animationName = movName.at(animationIndex); play(animationName.c_str(), durationTo, durationTween, loop, tweenEasing); } int CCAnimation::getMovementCount() { return m_pAnimationData->getMovementCount(); } void CCAnimation::update(float dt) { CCProcessBase::update(dt); CCObject *object = NULL; CCARRAY_FOREACH(m_pTweenList, object) { ((CCTween *)object)->update(dt); } } void CCAnimation::updateHandler() { 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); } } void CCAnimation::updateFrameData(float currentPercent) { m_iPrevFrameIndex = m_iCurFrameIndex; m_iCurFrameIndex = m_iRawDuration * currentPercent; m_iCurFrameIndex = m_iCurFrameIndex % m_iRawDuration; } NS_CC_EXT_END