axmol/extensions/cocostudio/CCArmatureAnimation.cpp

551 lines
15 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
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 "CCArmatureAnimation.h"
#include "CCArmature.h"
#include "CCBone.h"
#include "CCArmatureDefine.h"
#include "CCDatas.h"
2019-11-23 20:27:39 +08:00
using namespace cocos2d;
2021-12-25 10:04:45 +08:00
namespace cocostudio
{
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
ArmatureAnimation* ArmatureAnimation::create(Armature* armature)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ArmatureAnimation* pArmatureAnimation = new ArmatureAnimation();
2021-12-08 00:11:53 +08:00
if (pArmatureAnimation->init(armature))
2019-11-23 20:27:39 +08:00
{
pArmatureAnimation->autorelease();
return pArmatureAnimation;
}
CC_SAFE_DELETE(pArmatureAnimation);
return nullptr;
}
ArmatureAnimation::ArmatureAnimation()
: _animationData(nullptr)
, _speedScale(1)
, _movementData(nullptr)
, _armature(nullptr)
, _movementID("")
, _toIndex(0)
, _ignoreFrameEvent(false)
, _onMovementList(false)
, _movementListLoop(false)
, _movementListDurationTo(-1)
, _userObject(nullptr)
, _movementEventCallFunc(nullptr)
, _frameEventCallFunc(nullptr)
, _movementEventTarget(nullptr)
, _frameEventTarget(nullptr)
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
, _movementEventListener(nullptr)
, _frameEventListener(nullptr)
2021-12-25 10:04:45 +08:00
{}
2019-11-23 20:27:39 +08:00
2020-10-17 16:32:16 +08:00
ArmatureAnimation::~ArmatureAnimation(void)
2019-11-23 20:27:39 +08:00
{
CC_SAFE_RELEASE_NULL(_animationData);
CC_SAFE_RELEASE_NULL(_userObject);
}
2021-12-25 10:04:45 +08:00
bool ArmatureAnimation::init(Armature* armature)
2019-11-23 20:27:39 +08:00
{
bool bRet = false;
do
{
_armature = armature;
_tweenList.clear();
bRet = true;
2021-12-25 10:04:45 +08:00
} while (0);
2019-11-23 20:27:39 +08:00
return bRet;
}
void ArmatureAnimation::pause()
{
for (const auto& tween : _tweenList)
{
tween->pause();
}
ProcessBase::pause();
}
void ArmatureAnimation::resume()
{
for (const auto& tween : _tweenList)
{
tween->resume();
}
ProcessBase::resume();
}
void ArmatureAnimation::stop()
{
for (const auto& tween : _tweenList)
{
tween->stop();
}
_tweenList.clear();
ProcessBase::stop();
}
2021-12-25 10:04:45 +08:00
void ArmatureAnimation::setAnimationScale(float animationScale)
2019-11-23 20:27:39 +08:00
{
setSpeedScale(animationScale);
}
float ArmatureAnimation::getAnimationScale() const
{
return getSpeedScale();
}
void ArmatureAnimation::setSpeedScale(float speedScale)
{
2021-12-25 10:04:45 +08:00
if (speedScale == _speedScale)
2019-11-23 20:27:39 +08:00
{
return;
}
_speedScale = speedScale;
_processScale = !_movementData ? _speedScale : _speedScale * _movementData->scale;
auto& map = _armature->getBoneDic();
2021-12-25 10:04:45 +08:00
for (auto& element : map)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Bone* bone = element.second;
2019-11-23 20:27:39 +08:00
bone->getTween()->setProcessScale(_processScale);
if (bone->getChildArmature())
{
bone->getChildArmature()->getAnimation()->setSpeedScale(_processScale);
}
}
}
float ArmatureAnimation::getSpeedScale() const
{
return _speedScale;
}
void ArmatureAnimation::play(std::string_view animationName, int durationTo, int loop)
2019-11-23 20:27:39 +08:00
{
if (animationName.empty())
{
CCLOG("_animationData can not be null");
return;
}
2021-12-25 10:04:45 +08:00
// CCASSERT(_animationData, "_animationData can not be null");
2019-11-23 20:27:39 +08:00
_movementData = _animationData->getMovement(animationName);
if (nullptr == _movementData)
{
CCLOG("_movementData can not be null");
return;
}
2021-12-25 10:04:45 +08:00
// CCASSERT(_movementData, "_movementData can not be null");
2019-11-23 20:27:39 +08:00
//! Get key frame count
_rawDuration = _movementData->duration;
_movementID = animationName;
_processScale = _speedScale * _movementData->scale;
//! Further processing parameters
durationTo = (durationTo == -1) ? _movementData->durationTo : durationTo;
int durationTween = _movementData->durationTween == 0 ? _rawDuration : _movementData->durationTween;
cocos2d::tweenfunc::TweenType tweenEasing = _movementData->tweenEasing;
2021-12-25 10:04:45 +08:00
loop = (loop < 0) ? _movementData->loop : loop;
2019-11-23 20:27:39 +08:00
_onMovementList = false;
ProcessBase::play(durationTo, durationTween, loop, tweenEasing);
if (_rawDuration == 0)
{
_loopType = SINGLE_FRAME;
}
else
{
if (loop)
{
_loopType = ANIMATION_TO_LOOP_FRONT;
}
else
{
_loopType = ANIMATION_NO_LOOP;
}
_durationTween = durationTween;
}
2021-12-25 10:04:45 +08:00
MovementBoneData* movementBoneData = nullptr;
2019-11-23 20:27:39 +08:00
_tweenList.clear();
auto& map = _armature->getBoneDic();
2021-12-25 10:04:45 +08:00
for (auto& element : map)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Bone* bone = element.second;
movementBoneData = static_cast<MovementBoneData*>(_movementData->movBoneDataDic.at(bone->getName()));
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
Tween* tween = bone->getTween();
if (movementBoneData && movementBoneData->frameList.size() > 0)
2019-11-23 20:27:39 +08:00
{
_tweenList.push_back(tween);
movementBoneData->duration = _movementData->duration;
tween->play(movementBoneData, durationTo, durationTween, loop, tweenEasing);
tween->setProcessScale(_processScale);
if (bone->getChildArmature())
{
bone->getChildArmature()->getAnimation()->setSpeedScale(_processScale);
}
}
else
{
2021-12-25 10:04:45 +08:00
if (!bone->isIgnoreMovementBoneData())
2019-11-23 20:27:39 +08:00
{
//! this bone is not include in this movement, so hide it
bone->getDisplayManager()->changeDisplayWithIndex(-1, false);
tween->stop();
}
}
}
_armature->update(0);
}
void ArmatureAnimation::playByIndex(int animationIndex, int durationTo, int loop)
{
playWithIndex(animationIndex, durationTo, loop);
}
void ArmatureAnimation::playWithIndex(int animationIndex, int durationTo, int loop)
{
2021-12-25 10:04:45 +08:00
std::vector<std::string>& movName = _animationData->movementNames;
2019-11-23 20:27:39 +08:00
CC_ASSERT((animationIndex > -1) && ((unsigned int)animationIndex < movName.size()));
std::string animationName = movName.at(animationIndex);
play(animationName, durationTo, loop);
}
void ArmatureAnimation::playWithNames(const std::vector<std::string>& movementNames, int durationTo, bool loop)
{
_movementList.clear();
2021-12-25 10:04:45 +08:00
_movementListLoop = loop;
2019-11-23 20:27:39 +08:00
_movementListDurationTo = durationTo;
2021-12-25 10:04:45 +08:00
_onMovementList = true;
_movementIndex = 0;
2019-11-23 20:27:39 +08:00
_movementList = movementNames;
updateMovementList();
}
void ArmatureAnimation::playWithIndexes(const std::vector<int>& movementIndexes, int durationTo, bool loop)
{
_movementList.clear();
2021-12-25 10:04:45 +08:00
_movementListLoop = loop;
2019-11-23 20:27:39 +08:00
_movementListDurationTo = durationTo;
2021-12-25 10:04:45 +08:00
_onMovementList = true;
_movementIndex = 0;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
std::vector<std::string>& movName = _animationData->movementNames;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
for (auto& index : movementIndexes)
2019-11-23 20:27:39 +08:00
{
std::string name = movName.at(index);
_movementList.push_back(name);
}
updateMovementList();
}
void ArmatureAnimation::gotoAndPlay(int frameIndex)
{
if (!_movementData || frameIndex < 0 || frameIndex >= _movementData->duration)
{
CCLOG("Please ensure you have played a movement, and the frameIndex is in the range.");
return;
}
bool ignoreFrameEvent = _ignoreFrameEvent;
2021-12-25 10:04:45 +08:00
_ignoreFrameEvent = true;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_isPlaying = true;
2019-11-23 20:27:39 +08:00
_isComplete = _isPause = false;
ProcessBase::gotoFrame(frameIndex);
2021-12-25 10:04:45 +08:00
_currentPercent = (float)_curFrameIndex / ((float)_movementData->duration - 1);
_currentFrame = _nextFrameIndex * _currentPercent;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
for (const auto& tween : _tweenList)
2019-11-23 20:27:39 +08:00
{
tween->gotoAndPlay(frameIndex);
}
_armature->update(0);
_ignoreFrameEvent = ignoreFrameEvent;
}
void ArmatureAnimation::gotoAndPause(int frameIndex)
{
gotoAndPlay(frameIndex);
pause();
}
ssize_t ArmatureAnimation::getMovementCount() const
{
return _animationData->getMovementCount();
}
void ArmatureAnimation::update(float dt)
{
ProcessBase::update(dt);
2021-12-25 10:04:45 +08:00
for (const auto& tween : _tweenList)
2019-11-23 20:27:39 +08:00
{
tween->update(dt);
}
2021-12-25 10:04:45 +08:00
if (_frameEventQueue.size() > 0 || _movementEventQueue.size() > 0)
2019-11-23 20:27:39 +08:00
{
_armature->retain();
_armature->autorelease();
}
while (_frameEventQueue.size() > 0)
{
2021-12-25 10:04:45 +08:00
FrameEvent* event = _frameEventQueue.front();
2019-11-23 20:27:39 +08:00
_frameEventQueue.pop();
_ignoreFrameEvent = true;
2021-12-25 10:04:45 +08:00
if (_frameEventTarget)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
(_frameEventTarget->*_frameEventCallFunc)(event->bone, event->frameEventName, event->originFrameIndex,
event->currentFrameIndex);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (_frameEventListener)
{
_frameEventListener(event->bone, event->frameEventName, event->originFrameIndex, event->currentFrameIndex);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_ignoreFrameEvent = false;
CC_SAFE_DELETE(event);
}
while (_movementEventQueue.size() > 0)
{
2021-12-25 10:04:45 +08:00
MovementEvent* event = _movementEventQueue.front();
2019-11-23 20:27:39 +08:00
_movementEventQueue.pop();
2021-12-25 10:04:45 +08:00
if (_movementEventTarget)
2019-11-23 20:27:39 +08:00
{
(_movementEventTarget->*_movementEventCallFunc)(event->armature, event->movementType, event->movementID);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (_movementEventListener)
{
_movementEventListener(event->armature, event->movementType, event->movementID);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
CC_SAFE_DELETE(event);
}
}
void ArmatureAnimation::updateHandler()
{
if (_currentPercent >= 1)
{
2021-12-25 10:04:45 +08:00
switch (_loopType)
2019-11-23 20:27:39 +08:00
{
case ANIMATION_NO_LOOP:
{
2021-12-25 10:04:45 +08:00
_loopType = ANIMATION_MAX;
_currentFrame = (_currentPercent - 1) * _nextFrameIndex;
2019-11-23 20:27:39 +08:00
_currentPercent = _currentFrame / _durationTween;
if (_currentPercent >= 1.0f)
2021-12-25 10:04:45 +08:00
{}
2019-11-23 20:27:39 +08:00
else
{
_nextFrameIndex = _durationTween;
movementEvent(_armature, START, _movementID);
break;
}
}
break;
case ANIMATION_MAX:
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
movementEvent(_armature, COMPLETE, _movementID);
updateMovementList();
}
break;
case ANIMATION_TO_LOOP_FRONT:
{
2021-12-25 10:04:45 +08:00
_loopType = ANIMATION_LOOP_FRONT;
2019-11-23 20:27:39 +08:00
_currentPercent = fmodf(_currentPercent, 1);
2021-12-25 10:04:45 +08:00
_currentFrame = _nextFrameIndex == 0 ? 0 : fmodf(_currentFrame, _nextFrameIndex);
2019-11-23 20:27:39 +08:00
_nextFrameIndex = _durationTween > 0 ? _durationTween : 1;
movementEvent(_armature, START, _movementID);
}
break;
default:
{
//_currentPercent = fmodf(_currentPercent, 1);
_currentFrame = fmodf(_currentFrame, _nextFrameIndex);
2021-12-25 10:04:45 +08:00
_toIndex = 0;
2019-11-23 20:27:39 +08:00
movementEvent(_armature, LOOP_COMPLETE, _movementID);
}
break;
}
}
}
std::string ArmatureAnimation::getCurrentMovementID() const
{
if (_isComplete)
{
return "";
}
return _movementID;
}
2021-12-25 10:04:45 +08:00
void ArmatureAnimation::setMovementEventCallFunc(Ref* target, SEL_MovementEventCallFunc callFunc)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_movementEventTarget = target;
2019-11-23 20:27:39 +08:00
_movementEventCallFunc = callFunc;
}
2021-12-25 10:04:45 +08:00
void ArmatureAnimation::setFrameEventCallFunc(Ref* target, SEL_FrameEventCallFunc callFunc)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_frameEventTarget = target;
2019-11-23 20:27:39 +08:00
_frameEventCallFunc = callFunc;
}
2021-12-25 10:04:45 +08:00
void ArmatureAnimation::setMovementEventCallFunc(
std::function<void(Armature* armature, MovementEventType movementType, std::string_view movementID)> listener)
2019-11-23 20:27:39 +08:00
{
_movementEventListener = listener;
}
2021-12-25 10:04:45 +08:00
void ArmatureAnimation::setFrameEventCallFunc(
std::function<void(Bone* bone, std::string_view frameEventName, int originFrameIndex, int currentFrameIndex)>
2021-12-25 10:04:45 +08:00
listener)
2019-11-23 20:27:39 +08:00
{
_frameEventListener = listener;
}
2021-12-25 10:04:45 +08:00
void ArmatureAnimation::setUserObject(Ref* pUserObject)
2019-11-23 20:27:39 +08:00
{
CC_SAFE_RETAIN(pUserObject);
CC_SAFE_RELEASE(_userObject);
_userObject = pUserObject;
}
2021-12-25 10:04:45 +08:00
void ArmatureAnimation::frameEvent(Bone* bone,
std::string_view frameEventName,
2021-12-25 10:04:45 +08:00
int originFrameIndex,
int currentFrameIndex)
2019-11-23 20:27:39 +08:00
{
if ((_frameEventTarget && _frameEventCallFunc) || _frameEventListener)
{
2021-12-25 10:04:45 +08:00
FrameEvent* frameEvent = new FrameEvent();
frameEvent->bone = bone;
frameEvent->frameEventName = frameEventName;
frameEvent->originFrameIndex = originFrameIndex;
2019-11-23 20:27:39 +08:00
frameEvent->currentFrameIndex = currentFrameIndex;
_frameEventQueue.push(frameEvent);
}
}
void ArmatureAnimation::movementEvent(Armature* armature, MovementEventType movementType, std::string_view movementID)
2019-11-23 20:27:39 +08:00
{
if ((_movementEventTarget && _movementEventCallFunc) || _movementEventListener)
{
2021-12-25 10:04:45 +08:00
MovementEvent* movementEvent = new MovementEvent();
movementEvent->armature = armature;
movementEvent->movementType = movementType;
movementEvent->movementID = movementID;
2019-11-23 20:27:39 +08:00
_movementEventQueue.push(movementEvent);
}
}
void ArmatureAnimation::updateMovementList()
{
if (_onMovementList)
{
if (_movementListLoop)
{
play(_movementList.at(_movementIndex), _movementListDurationTo, 0);
_movementIndex++;
if (_movementIndex >= _movementList.size())
2021-12-25 10:04:45 +08:00
{
_movementIndex = 0;
2019-11-23 20:27:39 +08:00
}
}
else
{
if (_movementIndex < _movementList.size())
{
play(_movementList.at(_movementIndex), _movementListDurationTo, 0);
_movementIndex++;
}
else
{
_onMovementList = false;
}
}
_onMovementList = true;
}
}
2021-12-25 10:04:45 +08:00
} // namespace cocostudio