axmol/extensions/CocoStudio/Armature/animation/CCTween.cpp

466 lines
12 KiB
C++
Raw Normal View History

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"
#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
Tween *Tween::create(Bone *bone)
2013-06-06 12:02:54 +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;
}
Tween::Tween()
2013-09-15 20:24:25 +08:00
: _movementBoneData(NULL)
, _tweenData(NULL)
, _from(NULL)
, _to(NULL)
, _between(NULL)
, _bone(NULL)
, _frameTweenEasing(Linear)
, _fromIndex(0)
, _toIndex(0)
, _animation(NULL)
2013-06-06 12:02:54 +08:00
{
}
Tween::~Tween(void)
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
CC_SAFE_DELETE( _from );
CC_SAFE_DELETE( _between );
2013-06-06 12:02:54 +08:00
}
bool Tween::init(Bone *bone)
2013-06-06 12:02:54 +08:00
{
bool bRet = false;
do
{
2013-09-15 20:24:25 +08:00
_from = new FrameData();
_between = new FrameData();
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
_bone = bone;
_tweenData = _bone->getTweenData();
_tweenData->displayIndex = -1;
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
_animation = _bone->getArmature() != NULL ? _bone->getArmature()->getAnimation() : NULL;
2013-06-06 12:02:54 +08:00
bRet = true;
}
while (0);
return bRet;
}
void Tween::play(MovementBoneData *movementBoneData, int durationTo, int durationTween, int loop, int tweenEasing)
2013-06-06 12:02:54 +08:00
{
ProcessBase::play(NULL, durationTo, durationTween, loop, tweenEasing);
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
_loopType = (AnimationType)loop;
2013-06-06 12:02:54 +08:00
2013-09-15 20:24:25 +08:00
_totalDuration = 0;
_betweenDuration = 0;
_fromIndex = _toIndex = 0;
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
bool difMovement = movementBoneData != _movementBoneData;
2013-06-07 10:52:32 +08:00
2013-07-26 10:19:19 +08:00
setMovementBoneData(movementBoneData);
2013-09-15 20:24:25 +08:00
_rawDuration = _movementBoneData->duration;
2013-09-13 18:07:37 +08:00
2013-09-15 20:24:25 +08:00
FrameData *nextKeyFrame = _movementBoneData->getFrameData(0);
_tweenData->displayIndex = nextKeyFrame->displayIndex;
2013-06-06 12:02:54 +08:00
2013-09-15 20:24:25 +08:00
if (_bone->getArmature()->getArmatureData()->dataVersion >= VERSION_COMBINED)
2013-09-13 18:07:37 +08:00
{
2013-09-15 20:24:25 +08:00
TransformHelp::nodeSub(*_tweenData, *_bone->getBoneData());
_tweenData->scaleX += 1;
_tweenData->scaleY += 1;
2013-09-13 18:07:37 +08:00
}
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
if (_rawDuration == 0 )
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
_loopType = 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-15 20:24:25 +08:00
setBetween(_tweenData, nextKeyFrame);
2013-06-07 10:52:32 +08:00
}
2013-09-15 20:24:25 +08:00
_frameTweenEasing = Linear;
2013-06-06 12:02:54 +08:00
}
2013-09-15 20:24:25 +08:00
else if (_movementBoneData->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-15 20:24:25 +08:00
_loopType = ANIMATION_TO_LOOP_BACK;
2013-06-07 10:52:32 +08:00
}
else
{
2013-09-15 20:24:25 +08:00
_loopType = ANIMATION_NO_LOOP;
2013-06-07 10:52:32 +08:00
}
2013-06-06 12:02:54 +08:00
2013-09-15 20:24:25 +08:00
_durationTween = durationTween * _movementBoneData->scale;
2013-06-06 12:02:54 +08:00
2013-09-15 20:24:25 +08:00
if (loop && _movementBoneData->delay != 0)
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
setBetween(_tweenData, tweenNodeTo(updateFrameData(1 - _movementBoneData->delay), _between));
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
{
2013-09-15 20:24:25 +08:00
setBetween(_tweenData, nextKeyFrame);
2013-09-13 18:07:37 +08:00
}
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
void Tween::updateHandler()
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
if (_currentPercent >= 1)
2013-06-07 10:52:32 +08:00
{
2013-09-15 20:24:25 +08:00
switch(_loopType)
2013-06-07 10:52:32 +08:00
{
case SINGLE_FRAME:
{
2013-09-15 20:24:25 +08:00
_currentPercent = 1;
_isComplete = true;
_isPlaying = false;
2013-06-07 10:52:32 +08:00
}
break;
case ANIMATION_NO_LOOP:
{
2013-09-15 20:24:25 +08:00
_loopType = ANIMATION_MAX;
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
if (_durationTween <= 0)
2013-06-07 10:52:32 +08:00
{
2013-09-15 20:24:25 +08:00
_currentPercent = 1;
2013-06-07 10:52:32 +08:00
}
else
{
2013-09-16 14:13:55 +08:00
_currentPercent = (_currentPercent - 1) * _nextFrameIndex / _durationTween;
2013-06-07 10:52:32 +08:00
}
2013-09-15 20:24:25 +08:00
if (_currentPercent >= 1)
2013-06-07 10:52:32 +08:00
{
2013-09-15 20:24:25 +08:00
_currentPercent = 1;
_isComplete = true;
_isPlaying = false;
2013-06-07 10:52:32 +08:00
break;
}
else
{
2013-09-16 14:13:55 +08:00
_nextFrameIndex = _durationTween;
_currentFrame = _currentPercent * _nextFrameIndex;
2013-09-15 20:24:25 +08:00
_totalDuration = 0;
_betweenDuration = 0;
_fromIndex = _toIndex = 0;
2013-06-07 10:52:32 +08:00
break;
}
}
break;
case ANIMATION_TO_LOOP_BACK:
{
2013-09-15 20:24:25 +08:00
_loopType = ANIMATION_LOOP_BACK;
2013-06-07 10:52:32 +08:00
2013-09-16 14:13:55 +08:00
_nextFrameIndex = _durationTween > 0 ? _durationTween : 1;
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
if (_movementBoneData->delay != 0)
2013-06-07 10:52:32 +08:00
{
//
2013-09-16 14:13:55 +08:00
_currentFrame = (1 - _movementBoneData->delay) * (float)_nextFrameIndex;
_currentPercent = _currentFrame / _nextFrameIndex;
2013-06-07 10:52:32 +08:00
}
else
{
2013-09-15 20:24:25 +08:00
_currentPercent = 0;
_currentFrame = 0;
2013-06-07 10:52:32 +08:00
}
2013-09-15 20:24:25 +08:00
_totalDuration = 0;
_betweenDuration = 0;
_fromIndex = _toIndex = 0;
2013-06-07 10:52:32 +08:00
}
break;
case ANIMATION_MAX:
{
2013-09-15 20:24:25 +08:00
_currentPercent = 1;
_isComplete = true;
_isPlaying = false;
2013-06-07 10:52:32 +08:00
}
break;
default:
{
2013-09-16 14:13:55 +08:00
_currentFrame = fmodf(_currentFrame, _nextFrameIndex);
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
_totalDuration = 0;
_betweenDuration = 0;
_fromIndex = _toIndex = 0;
2013-06-07 10:52:32 +08:00
}
break;
}
}
2013-09-15 20:24:25 +08:00
if (_currentPercent < 1 && _loopType <= ANIMATION_TO_LOOP_BACK)
2013-06-07 10:52:32 +08:00
{
2013-09-15 20:24:25 +08:00
_currentPercent = sin(_currentPercent * CC_HALF_PI);
2013-06-07 10:52:32 +08:00
}
2013-09-15 20:24:25 +08:00
float percent = _currentPercent;
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
if (_loopType > 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-15 20:24:25 +08:00
if(_frameTweenEasing != TWEEN_EASING_MAX)
2013-06-07 10:52:32 +08:00
{
tweenNodeTo(percent);
}
2013-06-06 12:02:54 +08:00
}
void Tween::setBetween(FrameData *from, FrameData *to, bool limit)
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-15 20:24:25 +08:00
_from->copy(to);
_between->subtract(to, to, limit);
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-15 20:24:25 +08:00
_from->copy(from);
_between->subtract(to, to, limit);
2013-06-07 10:52:32 +08:00
break;
}
2013-09-15 20:24:25 +08:00
_from->copy(from);
_between->subtract(from, to, limit);
2013-06-07 10:52:32 +08:00
}
while (0);
2013-09-17 20:14:08 +08:00
if (!from->isTween)
{
_tweenData->copy(from);
_tweenData->isTween = true;
}
2013-06-07 10:52:32 +08:00
arriveKeyFrame(from);
2013-06-06 12:02:54 +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 20:24:25 +08:00
DisplayManager *displayManager = _bone->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
2013-09-15 20:24:25 +08:00
_tweenData->zOrder = keyFrameData->zOrder;
_bone->updateZOrder();
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
//! Update blend type
2013-09-15 20:24:25 +08:00
_bone->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 20:24:25 +08:00
Armature *childAramture = _bone->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
}
FrameData *Tween::tweenNodeTo(float percent, FrameData *node)
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
node = node == NULL ? _tweenData : node;
2013-07-26 10:19:19 +08:00
2013-09-17 20:14:08 +08:00
if (!_from->isTween)
{
return _from;
}
2013-09-15 20:24:25 +08:00
node->x = _from->x + percent * _between->x;
node->y = _from->y + percent * _between->y;
node->scaleX = _from->scaleX + percent * _between->scaleX;
node->scaleY = _from->scaleY + percent * _between->scaleY;
node->skewX = _from->skewX + percent * _between->skewX;
node->skewY = _from->skewY + percent * _between->skewY;
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
_bone->setTransformDirty(true);
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
if (node && _between->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
}
void Tween::tweenColorTo(float percent, FrameData *node)
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
node->a = _from->a + percent * _between->a;
node->r = _from->r + percent * _between->r;
node->g = _from->g + percent * _between->g;
node->b = _from->b + percent * _between->b;
_bone->updateColor();
2013-09-13 18:07:37 +08:00
}
2013-06-06 12:02:54 +08:00
float Tween::updateFrameData(float currentPercent)
2013-09-13 18:07:37 +08:00
{
2013-09-15 20:24:25 +08:00
if (currentPercent > 1 && _movementBoneData->delay != 0)
2013-09-13 18:07:37 +08:00
{
currentPercent = fmodf(currentPercent, 1);
}
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
float playedTime = (float)_rawDuration * 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-15 20:24:25 +08:00
if (playedTime < _totalDuration || playedTime >= _totalDuration + _betweenDuration)
2013-06-06 12:02:54 +08:00
{
/*
2013-09-15 20:24:25 +08:00
* Get frame length, if _toIndex >= _length, then set _toIndex to 0, start anew.
* _toIndex is next index will play
2013-06-06 12:02:54 +08:00
*/
2013-09-15 20:24:25 +08:00
int length = _movementBoneData->frameList.count();
FrameData **frames = (FrameData **)_movementBoneData->frameList.data->arr;
2013-09-13 18:07:37 +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-15 20:24:25 +08:00
from = frames[_fromIndex];
_totalDuration = from->frameID;
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
if (++_toIndex >= length)
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
_toIndex = 0;
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
_fromIndex = _toIndex;
to = frames[_toIndex];
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)
{
2013-09-15 20:24:25 +08:00
_animation->frameEvent(_bone, from->strEvent.c_str(), from->frameID, playedTime);
2013-09-13 18:07:37 +08:00
}
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);
2013-09-15 20:24:25 +08:00
_betweenDuration = to->frameID - from->frameID;
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
_frameTweenEasing = from->tweenEasing;
2013-06-07 10:52:32 +08:00
setBetween(from, to, false);
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
}
2013-09-15 20:24:25 +08:00
currentPercent = _betweenDuration == 0 ? 0 : (playedTime - _totalDuration) / (float)_betweenDuration;
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-15 20:24:25 +08:00
if ( _frameTweenEasing != TWEEN_EASING_MAX)
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
tweenType = (_tweenEasing == TWEEN_EASING_MAX) ? _frameTweenEasing : _tweenEasing;
2013-09-13 18:07:37 +08:00
if (tweenType != TWEEN_EASING_MAX && tweenType != Linear)
2013-06-06 12:02:54 +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