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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#include "CCTween.h"
|
2013-06-08 11:16:17 +08:00
|
|
|
#include "CCArmatureAnimation.h"
|
2013-06-06 12:02:54 +08:00
|
|
|
#include "../CCBone.h"
|
|
|
|
#include "../CCArmature.h"
|
|
|
|
#include "../utils/CCUtilMath.h"
|
|
|
|
#include "../utils/CCTweenFunction.h"
|
|
|
|
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
CCTween *CCTween::create(CCBone *bone)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
CCTween *pTween = new CCTween();
|
|
|
|
if (pTween && pTween->init(bone))
|
|
|
|
{
|
|
|
|
pTween->autorelease();
|
|
|
|
return pTween;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pTween);
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CCTween::CCTween()
|
2013-06-07 18:50:35 +08:00
|
|
|
: m_pMovementBoneData(NULL)
|
2013-06-07 10:52:32 +08:00
|
|
|
, m_pTweenData(NULL)
|
2013-06-07 18:50:35 +08:00
|
|
|
, m_pFrom(NULL)
|
|
|
|
, m_pTo(NULL)
|
|
|
|
, m_pBetween(NULL)
|
|
|
|
, m_pCurrentKeyFrame(NULL)
|
2013-06-07 10:52:32 +08:00
|
|
|
, m_pBone(NULL)
|
2013-06-07 18:50:35 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
, m_eFrameTweenEasing(Linear)
|
|
|
|
, m_iFromIndex(0)
|
|
|
|
, m_iToIndex(0)
|
|
|
|
, m_pAnimation(NULL)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CCTween::~CCTween(void)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE( m_pFrom );
|
|
|
|
CC_SAFE_DELETE( m_pBetween );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CCTween::init(CCBone *bone)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
2013-06-06 16:22:58 +08:00
|
|
|
m_pFrom = new CCFrameData();
|
|
|
|
m_pBetween = new CCFrameData();
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
m_pBone = bone;
|
2013-06-07 10:52:32 +08:00
|
|
|
m_pTweenData = m_pBone->getTweenData();
|
|
|
|
|
|
|
|
m_pAnimation = m_pBone->getArmature() != NULL ? m_pBone->getArmature()->getAnimation() : NULL;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
while (0);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-06 16:22:58 +08:00
|
|
|
void CCTween::play(CCMovementBoneData *_movementBoneData, int _durationTo, int _durationTween, int _loop, int _tweenEasing)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
CCProcessBase::play(NULL, _durationTo, _durationTween, _loop, _tweenEasing);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
m_eLoopType = (AnimationType)_loop;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
|
|
|
m_pCurrentKeyFrame = NULL;
|
|
|
|
m_bIsTweenKeyFrame = false;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
m_iTotalDuration = 0;
|
|
|
|
betweenDuration = 0;
|
|
|
|
m_iToIndex = 0;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
setMovementBoneData(_movementBoneData);
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
if (m_pMovementBoneData->frameList.count() == 1)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
m_eLoopType = SINGLE_FRAME;
|
|
|
|
CCFrameData *_nextKeyFrame = m_pMovementBoneData->getFrameData(0);
|
|
|
|
if(_durationTo == 0)
|
|
|
|
{
|
|
|
|
setBetween(_nextKeyFrame, _nextKeyFrame);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_pTweenData->displayIndex = _nextKeyFrame->displayIndex;
|
|
|
|
setBetween(m_pTweenData, _nextKeyFrame);
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
m_bIsTweenKeyFrame = true;
|
|
|
|
m_eFrameTweenEasing = Linear;
|
2013-06-07 10:52:32 +08:00
|
|
|
m_iRawDuration = m_pMovementBoneData->duration;
|
|
|
|
m_iFromIndex = m_iToIndex = 0;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
else if (m_pMovementBoneData->frameList.count() > 1)
|
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
if (_loop)
|
|
|
|
{
|
|
|
|
m_eLoopType = ANIMATION_TO_LOOP_BACK;
|
|
|
|
m_iRawDuration = m_pMovementBoneData->duration;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_eLoopType = ANIMATION_NO_LOOP;
|
|
|
|
m_iRawDuration = m_pMovementBoneData->duration - 1;
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
|
|
|
|
m_iDurationTween = _durationTween * m_pMovementBoneData->scale;
|
|
|
|
|
|
|
|
if (_loop && m_pMovementBoneData->delay != 0)
|
|
|
|
{
|
|
|
|
setBetween(m_pTweenData, tweenNodeTo(updateFrameData(1 - m_pMovementBoneData->delay), m_pBetween));
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
CCFrameData *_nextKeyFrame = m_pMovementBoneData->getFrameData(0);
|
|
|
|
setBetween(m_pTweenData, _nextKeyFrame);
|
2013-06-06 12:02:54 +08:00
|
|
|
m_bIsTweenKeyFrame = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
void CCTween::updateHandler()
|
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
if (m_fCurrentPercent >= 1)
|
|
|
|
{
|
|
|
|
switch(m_eLoopType)
|
|
|
|
{
|
|
|
|
case SINGLE_FRAME:
|
|
|
|
{
|
|
|
|
m_fCurrentPercent = 1;
|
|
|
|
m_bIsComplete = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ANIMATION_NO_LOOP:
|
|
|
|
{
|
|
|
|
m_eLoopType = ANIMATION_MAX;
|
|
|
|
|
|
|
|
|
|
|
|
if (m_iDurationTween <= 0)
|
|
|
|
{
|
|
|
|
m_fCurrentPercent = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_fCurrentPercent = (m_fCurrentPercent - 1) * m_iNextFrameIndex / m_iDurationTween;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_fCurrentPercent >= 1)
|
|
|
|
{
|
|
|
|
m_fCurrentPercent = 1;
|
|
|
|
m_bIsComplete = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_iNextFrameIndex = m_iDurationTween;
|
|
|
|
m_fCurrentFrame = m_fCurrentPercent * m_iNextFrameIndex;
|
|
|
|
m_iTotalDuration = 0;
|
|
|
|
betweenDuration = 0;
|
|
|
|
m_iToIndex = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ANIMATION_TO_LOOP_BACK:
|
|
|
|
{
|
|
|
|
m_eLoopType = ANIMATION_LOOP_BACK;
|
|
|
|
|
|
|
|
m_iNextFrameIndex = m_iDurationTween > 0 ? m_iDurationTween : 1;
|
|
|
|
|
|
|
|
if (m_pMovementBoneData->delay != 0)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
m_fCurrentFrame = (1 - m_pMovementBoneData->delay) * (float)m_iNextFrameIndex;
|
|
|
|
m_fCurrentPercent = m_fCurrentFrame / m_iNextFrameIndex;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_fCurrentPercent = 0;
|
|
|
|
m_fCurrentFrame = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_iTotalDuration = 0;
|
|
|
|
betweenDuration = 0;
|
|
|
|
m_iToIndex = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ANIMATION_MAX:
|
|
|
|
{
|
|
|
|
m_fCurrentPercent = 1;
|
|
|
|
m_bIsComplete = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
m_fCurrentPercent = fmodf(m_fCurrentPercent, 1);
|
|
|
|
m_fCurrentFrame = fmodf(m_fCurrentFrame, m_iNextFrameIndex);
|
|
|
|
|
|
|
|
m_iTotalDuration = 0;
|
|
|
|
betweenDuration = 0;
|
|
|
|
m_iToIndex = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (m_fCurrentPercent < 1 && m_eLoopType <= ANIMATION_TO_LOOP_BACK)
|
|
|
|
{
|
|
|
|
m_fCurrentPercent = sin(m_fCurrentPercent * CC_HALF_PI);
|
|
|
|
}
|
|
|
|
|
|
|
|
float percent = m_fCurrentPercent;
|
|
|
|
|
|
|
|
if (m_eLoopType > ANIMATION_TO_LOOP_BACK)
|
|
|
|
{
|
|
|
|
percent = updateFrameData(percent, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(m_eFrameTweenEasing != TWEEN_EASING_MAX)
|
|
|
|
{
|
|
|
|
tweenNodeTo(percent);
|
|
|
|
}
|
|
|
|
else if(m_pCurrentKeyFrame)
|
|
|
|
{
|
|
|
|
tweenNodeTo(0);
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-06 16:22:58 +08:00
|
|
|
void CCTween::setBetween(CCFrameData *from, CCFrameData *to)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if(to->displayIndex < 0 && from->displayIndex > 0)
|
|
|
|
{
|
|
|
|
m_pFrom->copy(from);
|
|
|
|
m_pBetween->subtract(to, to);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(from->displayIndex < 0 && to->displayIndex > 0)
|
|
|
|
{
|
|
|
|
m_pFrom->copy(to);
|
|
|
|
m_pBetween->subtract(to, to);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pFrom->copy(from);
|
|
|
|
m_pBetween->subtract(from, to);
|
|
|
|
}
|
|
|
|
while (0);
|
|
|
|
|
|
|
|
arriveKeyFrame(from);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-06 16:22:58 +08:00
|
|
|
void CCTween::arriveKeyFrame(CCFrameData *keyFrameData)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
if(keyFrameData)
|
|
|
|
{
|
|
|
|
int displayIndex = keyFrameData->displayIndex;
|
|
|
|
|
|
|
|
if (!m_pBone->getDisplayManager()->getForceChangeDisplay())
|
|
|
|
{
|
|
|
|
m_pBone->getDisplayManager()->changeDisplayByIndex(displayIndex, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m_pBone->setZOrder(keyFrameData->zOrder);
|
|
|
|
|
|
|
|
CCArmature *childAramture = m_pBone->getChildArmature();
|
|
|
|
|
|
|
|
if(childAramture)
|
|
|
|
{
|
2013-06-07 14:01:03 +08:00
|
|
|
if(keyFrameData->m_strMovement.length() != 0)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
|
|
|
childAramture->getAnimation()->play(keyFrameData->m_strMovement.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(keyFrameData->m_strEvent.length() != 0)
|
|
|
|
{
|
|
|
|
m_pAnimation->FrameEventSignal.emit(m_pBone, keyFrameData->m_strEvent.c_str());
|
|
|
|
}
|
|
|
|
// if(keyFrameData->m_strSound.length() != 0)
|
|
|
|
// {
|
|
|
|
// //soundManager.dispatchEventWith(Event.SOUND_FRAME, m_pCurrentKeyFrame->sound);
|
|
|
|
// }
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-06 16:22:58 +08:00
|
|
|
CCFrameData *CCTween::tweenNodeTo(float percent, CCFrameData *node)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
node = node == NULL ? m_pTweenData : node;
|
|
|
|
|
|
|
|
node->x = m_pFrom->x + percent * m_pBetween->x;
|
|
|
|
node->y = m_pFrom->y + percent * m_pBetween->y;
|
|
|
|
node->scaleX = m_pFrom->scaleX + percent * m_pBetween->scaleX;
|
|
|
|
node->scaleY = m_pFrom->scaleY + percent * m_pBetween->scaleY;
|
|
|
|
node->skewX = m_pFrom->skewX + percent * m_pBetween->skewX;
|
|
|
|
node->skewY = m_pFrom->skewY + percent * m_pBetween->skewY;
|
|
|
|
|
|
|
|
m_pBone->setTransformDirty(true);
|
|
|
|
|
|
|
|
if(m_pBetween->isUseColorInfo)
|
|
|
|
{
|
|
|
|
node->a = m_pFrom->a + percent * m_pBetween->a;
|
|
|
|
node->r = m_pFrom->r + percent * m_pBetween->r;
|
|
|
|
node->g = m_pFrom->g + percent * m_pBetween->g;
|
|
|
|
node->b = m_pFrom->b + percent * m_pBetween->b;
|
|
|
|
m_pBone->updateColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
// CCPoint p1 = ccp(m_pFrom->x, m_pFrom->y);
|
|
|
|
// CCPoint p2 = ccp(100, 0);
|
|
|
|
// CCPoint p3 = ccp(200, 400);
|
|
|
|
// CCPoint p4 = ccp(m_pFrom->x + m_pBetween->x, m_pFrom->y + m_pBetween->y);
|
|
|
|
//
|
|
|
|
// CCPoint p = bezierTo(percent, p1, p2, p3, p4);
|
|
|
|
// node->x = p.x;
|
|
|
|
// node->y = p.y;
|
|
|
|
|
|
|
|
return node;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float CCTween::updateFrameData(float currentPrecent, bool activeFrame)
|
|
|
|
{
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
float playedTime = (float)m_iRawDuration * currentPrecent;
|
|
|
|
|
|
|
|
|
2013-06-06 16:22:58 +08:00
|
|
|
CCFrameData *from;
|
|
|
|
CCFrameData *to;
|
2013-06-06 12:02:54 +08:00
|
|
|
bool isListEnd;
|
|
|
|
|
|
|
|
//! If play to current frame's front or back, then find current frame again
|
|
|
|
if (playedTime >= m_iTotalDuration || playedTime < m_iTotalDuration - betweenDuration)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Get frame length, if m_iToIndex >= _length, then set m_iToIndex to 0, start anew.
|
|
|
|
* m_iToIndex is next index will play
|
|
|
|
*/
|
|
|
|
int length = m_pMovementBoneData->frameList.count();
|
|
|
|
do
|
|
|
|
{
|
|
|
|
betweenDuration = m_pMovementBoneData->getFrameData(m_iToIndex)->duration;
|
|
|
|
m_iTotalDuration += betweenDuration;
|
|
|
|
m_iFromIndex = m_iToIndex;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
if (++m_iToIndex >= length)
|
|
|
|
{
|
|
|
|
m_iToIndex = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (playedTime >= m_iTotalDuration);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
isListEnd = m_eLoopType == ANIMATION_MAX && m_iToIndex == 0;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
if(isListEnd)
|
|
|
|
{
|
|
|
|
to = from = m_pMovementBoneData->getFrameData(m_iFromIndex);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
from = m_pMovementBoneData->getFrameData(m_iFromIndex);
|
|
|
|
to = m_pMovementBoneData->getFrameData(m_iToIndex);
|
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
m_eFrameTweenEasing = from->tweenEasing;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
setBetween(from, to);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
currentPrecent = 1 - (m_iTotalDuration - playedTime) / (float)betweenDuration;
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
/*
|
2013-06-06 12:02:54 +08:00
|
|
|
* If frame tween easing equal to TWEEN_EASING_MAX, then it will not do tween.
|
|
|
|
*/
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
CCTweenType tweenType;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
if ( m_eFrameTweenEasing != TWEEN_EASING_MAX)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
tweenType = (m_eTweenEasing == TWEEN_EASING_MAX) ? m_eFrameTweenEasing : m_eTweenEasing;
|
|
|
|
if (tweenType != TWEEN_EASING_MAX)
|
|
|
|
{
|
|
|
|
currentPrecent = CCTweenFunction::tweenTo(0, 1, currentPrecent, 1, tweenType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return currentPrecent;
|
|
|
|
}
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
NS_CC_EXT_END
|