axmol/extensions/cocostudio/CCTween.cpp

487 lines
12 KiB
C++
Raw Normal View History

2013-06-06 12:02:54 +08:00
/****************************************************************************
2020-10-17 16:32:16 +08:00
Copyright (c) 2013-2017 Chukong Technologies Inc.
2013-06-06 12:02:54 +08:00
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"
#include "CCBone.h"
#include "CCArmature.h"
#include "CCTransformHelp.h"
2013-06-06 12:02:54 +08:00
namespace cocostudio {
2013-06-06 12:02:54 +08:00
using cocos2d::tweenfunc::Linear;
Tween *Tween::create(Bone *bone)
2013-06-06 12:02:54 +08:00
{
Tween *pTween = new (std::nothrow) Tween();
2013-06-06 12:02:54 +08:00
if (pTween && pTween->init(bone))
{
pTween->autorelease();
return pTween;
}
CC_SAFE_DELETE(pTween);
return nullptr;
2013-06-06 12:02:54 +08:00
}
Tween::Tween()
: _movementBoneData(nullptr)
, _tweenData(nullptr)
, _from(nullptr)
, _to(nullptr)
, _between(nullptr)
, _bone(nullptr)
2013-09-15 20:24:25 +08:00
, _frameTweenEasing(Linear)
, _fromIndex(0)
, _toIndex(0)
, _animation(nullptr)
, _passLastFrame(false)
2013-06-06 12:02:54 +08:00
{
}
2020-10-17 16:32:16 +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
{
_from = new (std::nothrow) FrameData();
_between = new (std::nothrow) 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
_animation = _bone->getArmature() != nullptr ? _bone->getArmature()->getAnimation() : nullptr;
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(durationTo, durationTween, loop, tweenEasing);
2013-06-07 10:52:32 +08:00
2013-10-30 09:41:40 +08:00
if (loop)
{
_loopType = ANIMATION_TO_LOOP_FRONT;
}
else
{
_loopType = ANIMATION_NO_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-12-13 19:40:38 +08:00
else if (_movementBoneData->frameList.size() > 1)
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
2013-10-30 09:41:40 +08:00
void Tween::gotoAndPlay(int frameIndex)
{
ProcessBase::gotoFrame(frameIndex);
_totalDuration = 0;
_betweenDuration = 0;
_fromIndex = _toIndex = 0;
_isPlaying = true;
_isComplete = _isPause = false;
_currentPercent = (float)_curFrameIndex / ((float)_rawDuration-1);
2013-10-30 09:41:40 +08:00
_currentFrame = _nextFrameIndex * _currentPercent;
}
void Tween::gotoAndPause(int frameIndex)
{
gotoAndPlay(frameIndex);
pause();
}
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;
2013-10-30 09:41:40 +08:00
case ANIMATION_TO_LOOP_FRONT:
2013-06-07 10:52:32 +08:00
{
2013-10-30 09:41:40 +08:00
_loopType = ANIMATION_LOOP_FRONT;
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
}
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
}
if(_frameTweenEasing != ::cocos2d::tweenfunc::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-11-05 19:53:38 +08:00
if (!displayManager->isForceChangeDisplay())
2013-06-07 10:52:32 +08:00
{
2014-01-02 11:07:31 +08:00
displayManager->changeDisplayWithIndex(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
_bone->setBlendFunc(keyFrameData->blendFunc);
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)
{
if(!keyFrameData->strMovement.empty())
2013-06-07 10:52:32 +08:00
{
childAramture->getAnimation()->play(keyFrameData->strMovement);
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
{
node = node == nullptr ? _tweenData : node;
2013-07-26 10:19:19 +08:00
2013-09-17 20:14:08 +08:00
if (!_from->isTween)
{
2013-10-30 09:41:40 +08:00
percent = 0;
2013-09-17 20:14:08 +08:00
}
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
float playedTime = ((float)_rawDuration-1) * 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-12-17 19:32:16 +08:00
long length = _movementBoneData->frameList.size();
2013-12-13 19:40:38 +08:00
cocos2d::Vector<FrameData *> &frames = _movementBoneData->frameList;
2013-09-13 18:07:37 +08:00
FrameData *from = nullptr;
FrameData *to = nullptr;
2013-09-13 18:07:37 +08:00
2013-12-13 19:40:38 +08:00
if (playedTime < frames.at(0)->frameID)
2013-09-13 18:07:37 +08:00
{
2013-12-13 19:40:38 +08:00
from = to = frames.at(0);
2013-09-13 18:07:37 +08:00
setBetween(from, to);
return _currentPercent;
2013-09-13 18:07:37 +08:00
}
2013-12-13 19:40:38 +08:00
if(playedTime >= frames.at(length - 1)->frameID)
2013-09-13 18:07:37 +08:00
{
// If _passLastFrame is true and playedTime >= frames[length - 1]->frameID, then do not need to go on.
if (_passLastFrame)
{
2013-12-13 19:40:38 +08:00
from = to = frames.at(length - 1);
setBetween(from, to);
return _currentPercent;
}
_passLastFrame = true;
}
else
{
_passLastFrame = false;
2013-09-13 18:07:37 +08:00
}
2013-06-06 12:02:54 +08:00
do
{
2013-10-30 09:41:40 +08:00
_fromIndex = _toIndex;
2013-12-13 19:40:38 +08:00
from = frames.at(_fromIndex);
2013-09-15 20:24:25 +08:00
_totalDuration = from->frameID;
2013-06-07 10:52:32 +08:00
2013-10-30 09:41:40 +08:00
_toIndex = _fromIndex + 1;
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-12-13 19:40:38 +08:00
to = frames.at(_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.empty() && !_animation->isIgnoreFrameEvent())
2013-09-13 18:07:37 +08:00
{
_animation->frameEvent(_bone, from->strEvent, from->frameID, playedTime);
2013-09-13 18:07:37 +08:00
}
2013-06-07 10:52:32 +08:00
if (playedTime == from->frameID || (_passLastFrame && _fromIndex == length-1))
2013-09-13 18:07:37 +08:00
{
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.
*/
TweenType tweenType = (_frameTweenEasing != Linear) ? _frameTweenEasing : _tweenEasing;
if (tweenType != cocos2d::tweenfunc::TWEEN_EASING_MAX && tweenType != Linear && !_passLastFrame)
2013-06-06 12:02:54 +08:00
{
currentPercent = cocos2d::tweenfunc::tweenTo(currentPercent, tweenType, _from->easingParams);
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
}
}