axmol/extensions/cocostudio/CCTween.cpp

469 lines
12 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
2020-10-17 16:32:16 +08:00
Copyright (c) 2013-2017 Chukong Technologies Inc.
2019-11-23 20:27:39 +08:00
https://axis-project.github.io/
2019-11-23 20:27:39 +08:00
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"
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
namespace cocostudio
{
2019-11-23 20:27:39 +08:00
using axis::tweenfunc::Linear;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
Tween* Tween::create(Bone* bone)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Tween* pTween = new Tween();
2021-12-08 00:11:53 +08:00
if (pTween->init(bone))
2019-11-23 20:27:39 +08:00
{
pTween->autorelease();
return pTween;
}
CC_SAFE_DELETE(pTween);
2019-11-23 20:27:39 +08:00
return nullptr;
}
Tween::Tween()
: _movementBoneData(nullptr)
, _tweenData(nullptr)
, _from(nullptr)
, _to(nullptr)
, _between(nullptr)
, _bone(nullptr)
, _frameTweenEasing(Linear)
, _fromIndex(0)
, _toIndex(0)
, _animation(nullptr)
, _passLastFrame(false)
2021-12-25 10:04:45 +08:00
{}
2019-11-23 20:27:39 +08:00
2020-10-17 16:32:16 +08:00
Tween::~Tween(void)
2019-11-23 20:27:39 +08:00
{
CC_SAFE_DELETE(_from);
CC_SAFE_DELETE(_between);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
bool Tween::init(Bone* bone)
2019-11-23 20:27:39 +08:00
{
bool bRet = false;
do
{
2021-12-25 10:04:45 +08:00
_from = new FrameData();
2021-12-08 00:11:53 +08:00
_between = new FrameData();
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_bone = bone;
_tweenData = _bone->getTweenData();
2019-11-23 20:27:39 +08:00
_tweenData->displayIndex = -1;
_animation = _bone->getArmature() != nullptr ? _bone->getArmature()->getAnimation() : nullptr;
bRet = true;
2021-12-25 10:04:45 +08:00
} while (0);
2019-11-23 20:27:39 +08:00
return bRet;
}
2021-12-25 10:04:45 +08:00
void Tween::play(MovementBoneData* movementBoneData, int durationTo, int durationTween, int loop, int tweenEasing)
2019-11-23 20:27:39 +08:00
{
ProcessBase::play(durationTo, durationTween, loop, tweenEasing);
if (loop)
{
_loopType = ANIMATION_TO_LOOP_FRONT;
}
else
{
_loopType = ANIMATION_NO_LOOP;
}
2021-12-25 10:04:45 +08:00
_totalDuration = 0;
2019-11-23 20:27:39 +08:00
_betweenDuration = 0;
_fromIndex = _toIndex = 0;
bool difMovement = movementBoneData != _movementBoneData;
setMovementBoneData(movementBoneData);
_rawDuration = _movementBoneData->duration;
2021-12-25 10:04:45 +08:00
FrameData* nextKeyFrame = _movementBoneData->getFrameData(0);
2019-11-23 20:27:39 +08:00
_tweenData->displayIndex = nextKeyFrame->displayIndex;
if (_bone->getArmature()->getArmatureData()->dataVersion >= VERSION_COMBINED)
{
TransformHelp::nodeSub(*_tweenData, *_bone->getBoneData());
_tweenData->scaleX += 1;
_tweenData->scaleY += 1;
}
2021-12-25 10:04:45 +08:00
if (_rawDuration == 0)
2019-11-23 20:27:39 +08:00
{
_loopType = SINGLE_FRAME;
2021-12-25 10:04:45 +08:00
if (durationTo == 0)
2019-11-23 20:27:39 +08:00
{
setBetween(nextKeyFrame, nextKeyFrame);
}
else
{
setBetween(_tweenData, nextKeyFrame);
}
_frameTweenEasing = Linear;
}
else if (_movementBoneData->frameList.size() > 1)
{
_durationTween = durationTween * _movementBoneData->scale;
if (loop && _movementBoneData->delay != 0)
{
setBetween(_tweenData, tweenNodeTo(updateFrameData(1 - _movementBoneData->delay), _between));
}
else
{
if (!difMovement || durationTo == 0)
{
setBetween(nextKeyFrame, nextKeyFrame);
}
else
{
setBetween(_tweenData, nextKeyFrame);
}
}
}
tweenNodeTo(0);
}
void Tween::gotoAndPlay(int frameIndex)
{
ProcessBase::gotoFrame(frameIndex);
2021-12-25 10:04:45 +08:00
_totalDuration = 0;
2019-11-23 20:27:39 +08:00
_betweenDuration = 0;
_fromIndex = _toIndex = 0;
2021-12-25 10:04:45 +08:00
_isPlaying = true;
2019-11-23 20:27:39 +08:00
_isComplete = _isPause = false;
2021-12-25 10:04:45 +08:00
_currentPercent = (float)_curFrameIndex / ((float)_rawDuration - 1);
_currentFrame = _nextFrameIndex * _currentPercent;
2019-11-23 20:27:39 +08:00
}
void Tween::gotoAndPause(int frameIndex)
{
gotoAndPlay(frameIndex);
pause();
}
void Tween::updateHandler()
{
if (_currentPercent >= 1)
{
2021-12-25 10:04:45 +08:00
switch (_loopType)
2019-11-23 20:27:39 +08:00
{
case SINGLE_FRAME:
{
_currentPercent = 1;
2021-12-25 10:04:45 +08:00
_isComplete = true;
_isPlaying = false;
2019-11-23 20:27:39 +08:00
}
break;
case ANIMATION_NO_LOOP:
{
_loopType = ANIMATION_MAX;
if (_durationTween <= 0)
{
_currentPercent = 1;
}
else
{
_currentPercent = (_currentPercent - 1) * _nextFrameIndex / _durationTween;
}
if (_currentPercent >= 1)
{
_currentPercent = 1;
2021-12-25 10:04:45 +08:00
_isComplete = true;
_isPlaying = false;
2019-11-23 20:27:39 +08:00
break;
}
else
{
2021-12-25 10:04:45 +08:00
_nextFrameIndex = _durationTween;
_currentFrame = _currentPercent * _nextFrameIndex;
_totalDuration = 0;
2019-11-23 20:27:39 +08:00
_betweenDuration = 0;
_fromIndex = _toIndex = 0;
break;
}
}
break;
case ANIMATION_TO_LOOP_FRONT:
{
_loopType = ANIMATION_LOOP_FRONT;
_nextFrameIndex = _durationTween > 0 ? _durationTween : 1;
if (_movementBoneData->delay != 0)
{
//
2021-12-25 10:04:45 +08:00
_currentFrame = (1 - _movementBoneData->delay) * (float)_nextFrameIndex;
2019-11-23 20:27:39 +08:00
_currentPercent = _currentFrame / _nextFrameIndex;
}
else
{
_currentPercent = 0;
2021-12-25 10:04:45 +08:00
_currentFrame = 0;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
_totalDuration = 0;
2019-11-23 20:27:39 +08:00
_betweenDuration = 0;
_fromIndex = _toIndex = 0;
}
break;
case ANIMATION_MAX:
{
_currentPercent = 1;
2021-12-25 10:04:45 +08:00
_isComplete = true;
_isPlaying = false;
2019-11-23 20:27:39 +08:00
}
break;
default:
{
_currentFrame = fmodf(_currentFrame, _nextFrameIndex);
}
break;
}
}
if (_currentPercent < 1 && _loopType <= ANIMATION_TO_LOOP_BACK)
{
_currentPercent = sin(_currentPercent * CC_HALF_PI);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
float percent = _currentPercent;
2019-11-23 20:27:39 +08:00
if (_loopType > ANIMATION_TO_LOOP_BACK)
{
percent = updateFrameData(percent);
}
if (_frameTweenEasing != ::axis::tweenfunc::TWEEN_EASING_MAX)
2019-11-23 20:27:39 +08:00
{
tweenNodeTo(percent);
}
}
2021-12-25 10:04:45 +08:00
void Tween::setBetween(FrameData* from, FrameData* to, bool limit)
2019-11-23 20:27:39 +08:00
{
do
{
2021-12-25 10:04:45 +08:00
if (from->displayIndex < 0 && to->displayIndex >= 0)
2019-11-23 20:27:39 +08:00
{
_from->copy(to);
_between->subtract(to, to, limit);
break;
}
2021-12-25 10:04:45 +08:00
else if (to->displayIndex < 0 && from->displayIndex >= 0)
2019-11-23 20:27:39 +08:00
{
_from->copy(from);
_between->subtract(to, to, limit);
break;
}
_from->copy(from);
_between->subtract(from, to, limit);
2021-12-25 10:04:45 +08:00
} while (0);
2019-11-23 20:27:39 +08:00
if (!from->isTween)
{
_tweenData->copy(from);
_tweenData->isTween = true;
}
arriveKeyFrame(from);
}
2021-12-25 10:04:45 +08:00
void Tween::arriveKeyFrame(FrameData* keyFrameData)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (keyFrameData)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
DisplayManager* displayManager = _bone->getDisplayManager();
2019-11-23 20:27:39 +08:00
//! Change bone's display
int displayIndex = keyFrameData->displayIndex;
if (!displayManager->isForceChangeDisplay())
{
displayManager->changeDisplayWithIndex(displayIndex, false);
}
//! Update bone zorder, bone's zorder is determined by frame zorder and bone zorder
_tweenData->zOrder = keyFrameData->zOrder;
_bone->updateZOrder();
//! Update blend type
_bone->setBlendFunc(keyFrameData->blendFunc);
//! Update child armature's movement
2021-12-25 10:04:45 +08:00
Armature* childAramture = _bone->getChildArmature();
if (childAramture)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (!keyFrameData->strMovement.empty())
2019-11-23 20:27:39 +08:00
{
childAramture->getAnimation()->play(keyFrameData->strMovement);
}
}
}
}
2021-12-25 10:04:45 +08:00
FrameData* Tween::tweenNodeTo(float percent, FrameData* node)
2019-11-23 20:27:39 +08:00
{
node = node == nullptr ? _tweenData : node;
if (!_from->isTween)
{
percent = 0;
}
2021-12-25 10:04:45 +08:00
node->x = _from->x + percent * _between->x;
node->y = _from->y + percent * _between->y;
2019-11-23 20:27:39 +08:00
node->scaleX = _from->scaleX + percent * _between->scaleX;
node->scaleY = _from->scaleY + percent * _between->scaleY;
2021-12-25 10:04:45 +08:00
node->skewX = _from->skewX + percent * _between->skewX;
node->skewY = _from->skewY + percent * _between->skewY;
2019-11-23 20:27:39 +08:00
_bone->setTransformDirty(true);
if (node && _between->isUseColorInfo)
{
tweenColorTo(percent, node);
}
return node;
}
2021-12-25 10:04:45 +08:00
void Tween::tweenColorTo(float percent, FrameData* node)
2019-11-23 20:27:39 +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();
}
float Tween::updateFrameData(float currentPercent)
{
if (currentPercent > 1 && _movementBoneData->delay != 0)
{
currentPercent = fmodf(currentPercent, 1);
}
2021-12-25 10:04:45 +08:00
float playedTime = ((float)_rawDuration - 1) * currentPercent;
2019-11-23 20:27:39 +08:00
//! If play to current frame's front or back, then find current frame again
if (playedTime < _totalDuration || playedTime >= _totalDuration + _betweenDuration)
{
/*
* Get frame length, if _toIndex >= _length, then set _toIndex to 0, start anew.
* _toIndex is next index will play
*/
2021-12-25 10:04:45 +08:00
long length = _movementBoneData->frameList.size();
axis::Vector<FrameData*>& frames = _movementBoneData->frameList;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
FrameData* from = nullptr;
FrameData* to = nullptr;
2019-11-23 20:27:39 +08:00
if (playedTime < frames.at(0)->frameID)
{
from = to = frames.at(0);
setBetween(from, to);
return _currentPercent;
}
2021-12-25 10:04:45 +08:00
if (playedTime >= frames.at(length - 1)->frameID)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// If _passLastFrame is true and playedTime >= frames[length - 1]->frameID, then do not need to go on.
2019-11-23 20:27:39 +08:00
if (_passLastFrame)
{
from = to = frames.at(length - 1);
setBetween(from, to);
return _currentPercent;
}
_passLastFrame = true;
}
else
{
_passLastFrame = false;
}
do
{
2021-12-25 10:04:45 +08:00
_fromIndex = _toIndex;
from = frames.at(_fromIndex);
_totalDuration = from->frameID;
2019-11-23 20:27:39 +08:00
_toIndex = _fromIndex + 1;
if (_toIndex >= length)
{
_toIndex = 0;
}
to = frames.at(_toIndex);
//! Guaranteed to trigger frame event
2021-12-25 10:04:45 +08:00
if (!from->strEvent.empty() && !_animation->isIgnoreFrameEvent())
2019-11-23 20:27:39 +08:00
{
_animation->frameEvent(_bone, from->strEvent, from->frameID, playedTime);
}
2021-12-25 10:04:45 +08:00
if (playedTime == from->frameID || (_passLastFrame && _fromIndex == length - 1))
2019-11-23 20:27:39 +08:00
{
break;
}
2021-12-25 10:04:45 +08:00
} while (playedTime < from->frameID || playedTime >= to->frameID);
2019-11-23 20:27:39 +08:00
_betweenDuration = to->frameID - from->frameID;
_frameTweenEasing = from->tweenEasing;
setBetween(from, to, false);
}
currentPercent = _betweenDuration == 0 ? 0 : (playedTime - _totalDuration) / (float)_betweenDuration;
/*
* If frame tween easing equal to TWEEN_EASING_MAX, then it will not do tween.
*/
TweenType tweenType = (_frameTweenEasing != Linear) ? _frameTweenEasing : _tweenEasing;
if (tweenType != axis::tweenfunc::TWEEN_EASING_MAX && tweenType != Linear && !_passLastFrame)
2019-11-23 20:27:39 +08:00
{
currentPercent = axis::tweenfunc::tweenTo(currentPercent, tweenType, _from->easingParams);
2019-11-23 20:27:39 +08:00
}
return currentPercent;
}
2021-12-25 10:04:45 +08:00
} // namespace cocostudio