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"
|
2013-09-13 18:07:37 +08:00
|
|
|
#include "../utils/CCTransformHelp.h"
|
2013-06-06 12:02:54 +08:00
|
|
|
|
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
NS_CC_EXT_ARMATURE_BEGIN
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
Tween *Tween::create(Bone *bone)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-15 19:08:45 +08:00
|
|
|
Tween *pTween = new Tween();
|
2013-06-06 12:02:54 +08:00
|
|
|
if (pTween && pTween->init(bone))
|
|
|
|
{
|
|
|
|
pTween->autorelease();
|
|
|
|
return pTween;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pTween);
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
Tween::Tween()
|
2013-09-13 18:07:37 +08:00
|
|
|
: m_pMovementBoneData(NULL)
|
|
|
|
, m_pTweenData(NULL)
|
|
|
|
, m_pFrom(NULL)
|
|
|
|
, m_pTo(NULL)
|
|
|
|
, m_pBetween(NULL)
|
|
|
|
, m_pBone(NULL)
|
|
|
|
|
|
|
|
, m_eFrameTweenEasing(Linear)
|
|
|
|
, m_iFromIndex(0)
|
|
|
|
, m_iToIndex(0)
|
|
|
|
, m_pAnimation(NULL)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
Tween::~Tween(void)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
CC_SAFE_DELETE( m_pFrom );
|
|
|
|
CC_SAFE_DELETE( m_pBetween );
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
bool Tween::init(Bone *bone)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
2013-09-15 19:08:45 +08:00
|
|
|
m_pFrom = new FrameData();
|
|
|
|
m_pBetween = new FrameData();
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
m_pBone = bone;
|
|
|
|
m_pTweenData = m_pBone->getTweenData();
|
|
|
|
m_pTweenData->displayIndex = -1;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
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-09-15 19:08:45 +08:00
|
|
|
void Tween::play(MovementBoneData *movementBoneData, int durationTo, int durationTween, int loop, int tweenEasing)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-15 19:08:45 +08:00
|
|
|
ProcessBase::play(NULL, durationTo, durationTween, loop, tweenEasing);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
m_eLoopType = (AnimationType)loop;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
m_iTotalDuration = 0;
|
|
|
|
m_iBetweenDuration = 0;
|
|
|
|
m_iFromIndex = m_iToIndex = 0;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
bool difMovement = movementBoneData != m_pMovementBoneData;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-07-26 10:19:19 +08:00
|
|
|
setMovementBoneData(movementBoneData);
|
2013-09-13 18:07:37 +08:00
|
|
|
m_iRawDuration = m_pMovementBoneData->duration;
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
FrameData *nextKeyFrame = m_pMovementBoneData->getFrameData(0);
|
2013-09-13 18:07:37 +08:00
|
|
|
m_pTweenData->displayIndex = nextKeyFrame->displayIndex;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
if (m_pBone->getArmature()->getArmatureData()->dataVersion >= VERSION_COMBINED)
|
|
|
|
{
|
2013-09-15 19:08:45 +08:00
|
|
|
TransformHelp::nodeSub(*m_pTweenData, *m_pBone->getBoneData());
|
2013-09-13 18:07:37 +08:00
|
|
|
m_pTweenData->scaleX += 1;
|
|
|
|
m_pTweenData->scaleY += 1;
|
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
if (m_iRawDuration == 0 )
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
m_eLoopType = SINGLE_FRAME;
|
2013-07-26 10:19:19 +08:00
|
|
|
if(durationTo == 0)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
setBetween(nextKeyFrame, nextKeyFrame);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
setBetween(m_pTweenData, nextKeyFrame);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-09-13 18:07:37 +08:00
|
|
|
m_eFrameTweenEasing = Linear;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-09-13 18:07:37 +08:00
|
|
|
else if (m_pMovementBoneData->frameList.count() > 1)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-07-26 10:19:19 +08:00
|
|
|
if (loop)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
m_eLoopType = ANIMATION_TO_LOOP_BACK;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
m_eLoopType = ANIMATION_NO_LOOP;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
m_iDurationTween = durationTween * m_pMovementBoneData->scale;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
if (loop && m_pMovementBoneData->delay != 0)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
setBetween(m_pTweenData, tweenNodeTo(updateFrameData(1 - m_pMovementBoneData->delay), m_pBetween));
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
if (!difMovement || durationTo == 0)
|
|
|
|
{
|
|
|
|
setBetween(nextKeyFrame, nextKeyFrame);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setBetween(m_pTweenData, nextKeyFrame);
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
}
|
2013-09-13 18:07:37 +08:00
|
|
|
|
|
|
|
tweenNodeTo(0);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void Tween::updateHandler()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
if (m_fCurrentPercent >= 1)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
switch(m_eLoopType)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
|
|
|
case SINGLE_FRAME:
|
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
m_fCurrentPercent = 1;
|
|
|
|
m_bIsComplete = true;
|
|
|
|
m_bIsPlaying = false;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ANIMATION_NO_LOOP:
|
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
m_eLoopType = ANIMATION_MAX;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
if (m_iDurationTween <= 0)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
m_fCurrentPercent = 1;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
m_fCurrentPercent = (m_fCurrentPercent - 1) * m_iNextFrameIndex / m_iDurationTween;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
if (m_fCurrentPercent >= 1)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
m_fCurrentPercent = 1;
|
|
|
|
m_bIsComplete = true;
|
|
|
|
m_bIsPlaying = false;
|
2013-06-07 10:52:32 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
m_iNextFrameIndex = m_iDurationTween;
|
|
|
|
m_fCurrentFrame = m_fCurrentPercent * m_iNextFrameIndex;
|
|
|
|
m_iTotalDuration = 0;
|
|
|
|
m_iBetweenDuration = 0;
|
|
|
|
m_iFromIndex = m_iToIndex = 0;
|
2013-06-07 10:52:32 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ANIMATION_TO_LOOP_BACK:
|
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
m_eLoopType = ANIMATION_LOOP_BACK;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
m_iNextFrameIndex = m_iDurationTween > 0 ? m_iDurationTween : 1;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
if (m_pMovementBoneData->delay != 0)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
|
|
|
//
|
2013-09-13 18:07:37 +08:00
|
|
|
m_fCurrentFrame = (1 - m_pMovementBoneData->delay) * (float)m_iNextFrameIndex;
|
|
|
|
m_fCurrentPercent = m_fCurrentFrame / m_iNextFrameIndex;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
m_fCurrentPercent = 0;
|
|
|
|
m_fCurrentFrame = 0;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
m_iTotalDuration = 0;
|
|
|
|
m_iBetweenDuration = 0;
|
|
|
|
m_iFromIndex = m_iToIndex = 0;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ANIMATION_MAX:
|
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
m_fCurrentPercent = 1;
|
|
|
|
m_bIsComplete = true;
|
|
|
|
m_bIsPlaying = false;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
m_fCurrentFrame = fmodf(m_fCurrentFrame, m_iNextFrameIndex);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
m_iTotalDuration = 0;
|
|
|
|
m_iBetweenDuration = 0;
|
|
|
|
m_iFromIndex = m_iToIndex = 0;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
if (m_fCurrentPercent < 1 && m_eLoopType <= ANIMATION_TO_LOOP_BACK)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
m_fCurrentPercent = sin(m_fCurrentPercent * CC_HALF_PI);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
float percent = m_fCurrentPercent;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
if (m_eLoopType > ANIMATION_TO_LOOP_BACK)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
percent = updateFrameData(percent);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
if(m_eFrameTweenEasing != TWEEN_EASING_MAX)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
|
|
|
tweenNodeTo(percent);
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void Tween::setBetween(FrameData *from, FrameData *to)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
do
|
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
if(from->displayIndex < 0 && to->displayIndex >= 0)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
m_pFrom->copy(to);
|
|
|
|
m_pBetween->subtract(to, to);
|
2013-06-07 10:52:32 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-09-13 18:07:37 +08:00
|
|
|
else if(to->displayIndex < 0 && from->displayIndex >= 0)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
m_pFrom->copy(from);
|
|
|
|
m_pBetween->subtract(to, to);
|
2013-06-07 10:52:32 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
m_pFrom->copy(from);
|
|
|
|
m_pBetween->subtract(from, to);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
while (0);
|
|
|
|
|
|
|
|
arriveKeyFrame(from);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void Tween::arriveKeyFrame(FrameData *keyFrameData)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
if(keyFrameData)
|
|
|
|
{
|
2013-09-15 19:08:45 +08:00
|
|
|
DisplayManager *displayManager = m_pBone->getDisplayManager();
|
2013-09-13 18:07:37 +08:00
|
|
|
|
|
|
|
//! Change bone's display
|
2013-06-07 10:52:32 +08:00
|
|
|
int displayIndex = keyFrameData->displayIndex;
|
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
if (!displayManager->getForceChangeDisplay())
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
displayManager->changeDisplayByIndex(displayIndex, false);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
//! Update bone zorder, bone's zorder is determined by frame zorder and bone zorder
|
|
|
|
m_pTweenData->zOrder = keyFrameData->zOrder;
|
|
|
|
m_pBone->updateZOrder();
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
//! Update blend type
|
|
|
|
m_pBone->setBlendType(keyFrameData->blendType);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
//! Update child armature's movement
|
2013-09-15 19:08:45 +08:00
|
|
|
Armature *childAramture = m_pBone->getChildArmature();
|
2013-06-07 10:52:32 +08:00
|
|
|
if(childAramture)
|
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
if(keyFrameData->strMovement.length() != 0)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
childAramture->getAnimation()->play(keyFrameData->strMovement.c_str());
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
FrameData *Tween::tweenNodeTo(float percent, FrameData *node)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
node = node == NULL ? m_pTweenData : node;
|
2013-07-26 10:19:19 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
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;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
m_pBone->setTransformDirty(true);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
if (node && m_pBetween->isUseColorInfo)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
tweenColorTo(percent, node);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void Tween::tweenColorTo(float percent, FrameData *node)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
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();
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
float Tween::updateFrameData(float currentPercent)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
|
|
|
if (currentPercent > 1 && m_pMovementBoneData->delay != 0)
|
|
|
|
{
|
|
|
|
currentPercent = fmodf(currentPercent, 1);
|
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
float playedTime = (float)m_iRawDuration * currentPercent;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
|
|
|
|
//! If play to current frame's front or back, then find current frame again
|
2013-09-13 18:07:37 +08:00
|
|
|
if (playedTime < m_iTotalDuration || playedTime >= m_iTotalDuration + m_iBetweenDuration)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
/*
|
2013-09-13 18:07:37 +08:00
|
|
|
* Get frame length, if m_iToIndex >= _length, then set m_iToIndex to 0, start anew.
|
|
|
|
* m_iToIndex is next index will play
|
2013-06-06 12:02:54 +08:00
|
|
|
*/
|
2013-09-13 18:07:37 +08:00
|
|
|
int length = m_pMovementBoneData->frameList.count();
|
2013-09-15 19:08:45 +08:00
|
|
|
FrameData **frames = (FrameData **)m_pMovementBoneData->frameList.data->arr;
|
2013-09-13 18:07:37 +08:00
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
FrameData *from = NULL;
|
|
|
|
FrameData *to = NULL;
|
2013-09-13 18:07:37 +08:00
|
|
|
|
|
|
|
if (playedTime < frames[0]->frameID)
|
|
|
|
{
|
|
|
|
from = to = frames[0];
|
|
|
|
setBetween(from, to);
|
|
|
|
return currentPercent;
|
|
|
|
}
|
|
|
|
else if(playedTime >= frames[length - 1]->frameID)
|
|
|
|
{
|
|
|
|
from = to = frames[length - 1];
|
|
|
|
setBetween(from, to);
|
|
|
|
return currentPercent;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
do
|
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
from = frames[m_iFromIndex];
|
|
|
|
m_iTotalDuration = from->frameID;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
if (++m_iToIndex >= length)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
m_iToIndex = 0;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
m_iFromIndex = m_iToIndex;
|
|
|
|
to = frames[m_iToIndex];
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
//! Guaranteed to trigger frame event
|
|
|
|
if(from->strEvent.length() != 0)
|
|
|
|
{
|
|
|
|
m_pAnimation->frameEvent(m_pBone, from->strEvent.c_str(), from->frameID, playedTime);
|
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
if (playedTime == from->frameID)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-09-13 18:07:37 +08:00
|
|
|
while (playedTime < from->frameID || playedTime >= to->frameID);
|
|
|
|
|
|
|
|
m_iBetweenDuration = to->frameID - from->frameID;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +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
|
|
|
}
|
2013-09-13 18:07:37 +08:00
|
|
|
currentPercent = m_iBetweenDuration == 0 ? 0 : (playedTime - m_iTotalDuration) / (float)m_iBetweenDuration;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
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-09-13 18:07:37 +08:00
|
|
|
CCTweenType tweenType;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
if ( m_eFrameTweenEasing != TWEEN_EASING_MAX)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
tweenType = (m_eTweenEasing == TWEEN_EASING_MAX) ? m_eFrameTweenEasing : m_eTweenEasing;
|
|
|
|
if (tweenType != TWEEN_EASING_MAX && tweenType != Linear)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-15 19:08:45 +08:00
|
|
|
currentPercent = TweenFunction::tweenTo(0, 1, currentPercent, 1, tweenType);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
return currentPercent;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
NS_CC_EXT_ARMATURE_END
|