2021-12-31 12:12:40 +08:00
|
|
|
#include "Animation.h"
|
2020-10-18 00:27:23 +08:00
|
|
|
#include "../model/DisplayData.h"
|
|
|
|
#include "../model/AnimationConfig.h"
|
|
|
|
#include "../model/AnimationData.h"
|
|
|
|
#include "../armature/Armature.h"
|
|
|
|
#include "../armature/Bone.h"
|
|
|
|
#include "../armature/Slot.h"
|
|
|
|
#include "AnimationState.h"
|
|
|
|
|
|
|
|
DRAGONBONES_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
void Animation::_onClear()
|
|
|
|
{
|
|
|
|
for (const auto animationState : _animationStates)
|
|
|
|
{
|
|
|
|
animationState->returnToPool();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_animationConfig != nullptr)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
_animationConfig->returnToPool();
|
|
|
|
}
|
|
|
|
|
|
|
|
timeScale = 1.0f;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_animationDirty = false;
|
2020-10-18 00:27:23 +08:00
|
|
|
_inheritTimeScale = 1.0f;
|
|
|
|
_animations.clear();
|
|
|
|
_animationNames.clear();
|
|
|
|
_animationStates.clear();
|
2021-12-25 10:04:45 +08:00
|
|
|
_armature = nullptr;
|
|
|
|
_animationConfig = nullptr;
|
2020-10-18 00:27:23 +08:00
|
|
|
_lastAnimationState = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Animation::_fadeOut(AnimationConfig* animationConfig)
|
|
|
|
{
|
|
|
|
switch (animationConfig->fadeOutMode)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case AnimationFadeOutMode::SameLayer:
|
|
|
|
for (const auto animationState : _animationStates)
|
|
|
|
{
|
|
|
|
if (animationState->layer == (unsigned)animationConfig->layer)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
animationState->fadeOut(animationConfig->fadeOutTime, animationConfig->pauseFadeOut);
|
2020-10-18 00:27:23 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
break;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case AnimationFadeOutMode::SameGroup:
|
|
|
|
for (const auto animationState : _animationStates)
|
|
|
|
{
|
|
|
|
if (animationState->group == animationConfig->group)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
animationState->fadeOut(animationConfig->fadeOutTime, animationConfig->pauseFadeOut);
|
2020-10-18 00:27:23 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
break;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case AnimationFadeOutMode::SameLayerAndGroup:
|
|
|
|
for (const auto animationState : _animationStates)
|
|
|
|
{
|
|
|
|
if (animationState->layer == (unsigned)animationConfig->layer &&
|
|
|
|
animationState->group == animationConfig->group)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
animationState->fadeOut(animationConfig->fadeOutTime, animationConfig->pauseFadeOut);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AnimationFadeOutMode::All:
|
|
|
|
for (const auto animationState : _animationStates)
|
|
|
|
{
|
|
|
|
animationState->fadeOut(animationConfig->fadeOutTime, animationConfig->pauseFadeOut);
|
|
|
|
}
|
|
|
|
break;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case AnimationFadeOutMode::None:
|
|
|
|
case AnimationFadeOutMode::Single:
|
|
|
|
default:
|
|
|
|
break;
|
2020-10-18 00:27:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Animation::init(Armature* armature)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_armature != nullptr)
|
|
|
|
{
|
2020-10-18 00:27:23 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_armature = armature;
|
2020-10-18 00:27:23 +08:00
|
|
|
_animationConfig = BaseObject::borrowObject<AnimationConfig>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Animation::advanceTime(float passedTime)
|
|
|
|
{
|
|
|
|
if (passedTime < 0.0f)
|
|
|
|
{
|
|
|
|
passedTime = -passedTime;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_armature->inheritAnimation && _armature->_parent != nullptr) // Inherit parent animation timeScale.
|
|
|
|
{
|
2020-10-18 00:27:23 +08:00
|
|
|
_inheritTimeScale = _armature->_parent->_armature->getAnimation()->_inheritTimeScale * timeScale;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
_inheritTimeScale = timeScale;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_inheritTimeScale != 1.0f)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
passedTime *= _inheritTimeScale;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto animationStateCount = _animationStates.size();
|
|
|
|
if (animationStateCount == 1)
|
|
|
|
{
|
|
|
|
const auto animationState = _animationStates[0];
|
|
|
|
if (animationState->_fadeState > 0 && animationState->_subFadeState > 0)
|
|
|
|
{
|
|
|
|
_armature->_dragonBones->bufferObject(animationState);
|
|
|
|
_animationStates.clear();
|
|
|
|
_lastAnimationState = nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto animationData = animationState->_animationData;
|
2020-10-18 00:27:23 +08:00
|
|
|
const auto cacheFrameRate = animationData->cacheFrameRate;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_animationDirty && cacheFrameRate > 0.0f) // Update cachedFrameIndices.
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
_animationDirty = false;
|
|
|
|
|
|
|
|
for (const auto bone : _armature->getBones())
|
|
|
|
{
|
|
|
|
bone->_cachedFrameIndices = animationData->getBoneCachedFrameIndices(bone->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto slot : _armature->getSlots())
|
|
|
|
{
|
|
|
|
const auto rawDisplayDatas = slot->getRawDisplayDatas();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (rawDisplayDatas != nullptr && !(*rawDisplayDatas).empty())
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
const auto rawDsplayData = (*rawDisplayDatas)[0];
|
2021-12-25 10:04:45 +08:00
|
|
|
if (rawDsplayData != nullptr)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
if (rawDsplayData->parent == _armature->getArmatureData()->defaultSkin)
|
|
|
|
{
|
|
|
|
slot->_cachedFrameIndices = animationData->getSlotCachedFrameIndices(slot->getName());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
slot->_cachedFrameIndices = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
animationState->advanceTime(passedTime, cacheFrameRate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (animationStateCount > 1)
|
|
|
|
{
|
|
|
|
for (std::size_t i = 0, r = 0; i < animationStateCount; ++i)
|
|
|
|
{
|
|
|
|
const auto animationState = _animationStates[i];
|
|
|
|
if (animationState->_fadeState > 0 && animationState->_subFadeState > 0)
|
|
|
|
{
|
|
|
|
r++;
|
|
|
|
_armature->_dragonBones->bufferObject(animationState);
|
|
|
|
_animationDirty = true;
|
|
|
|
if (_lastAnimationState == animationState)
|
|
|
|
{
|
|
|
|
_lastAnimationState = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (r > 0)
|
|
|
|
{
|
|
|
|
_animationStates[i - r] = animationState;
|
|
|
|
}
|
|
|
|
|
|
|
|
animationState->advanceTime(passedTime, 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == animationStateCount - 1 && r > 0)
|
|
|
|
{
|
|
|
|
_animationStates.resize(animationStateCount - r);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_lastAnimationState == nullptr && !_animationStates.empty())
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
_lastAnimationState = _animationStates[_animationStates.size() - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_armature->_cacheFrameIndex = -1;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
_armature->_cacheFrameIndex = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Animation::reset()
|
|
|
|
{
|
|
|
|
for (const auto animationState : _animationStates)
|
|
|
|
{
|
|
|
|
animationState->returnToPool();
|
|
|
|
}
|
|
|
|
|
|
|
|
_animationDirty = false;
|
|
|
|
_animationConfig->clear();
|
|
|
|
_animationStates.clear();
|
|
|
|
_lastAnimationState = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void Animation::stop(std::string_view animationName)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
if (!animationName.empty())
|
|
|
|
{
|
|
|
|
const auto animationState = getState(animationName);
|
|
|
|
if (animationState != nullptr)
|
|
|
|
{
|
|
|
|
animationState->stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto animationState : _animationStates)
|
|
|
|
{
|
2020-10-18 00:27:23 +08:00
|
|
|
animationState->stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AnimationState* Animation::playConfig(AnimationConfig* animationConfig)
|
|
|
|
{
|
|
|
|
const auto& animationName = animationConfig->animation;
|
|
|
|
if (_animations.find(animationName) == _animations.end())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
DRAGONBONES_ASSERT(false, "Non-existent animation.\n" +
|
|
|
|
" DragonBones name: " + this->_armature->getArmatureData().parent->name +
|
|
|
|
" Armature name: " + this->_armature->name + " Animation name: " + animationName);
|
2020-10-18 00:27:23 +08:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto animationData = _animations[animationName];
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (animationConfig->fadeOutMode == AnimationFadeOutMode::Single)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto animationState : _animationStates)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (animationState->_animationData == animationData)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
return animationState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (animationConfig->fadeInTime < 0.0f)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_animationStates.empty())
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
animationConfig->fadeInTime = 0.0f;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
animationConfig->fadeInTime = animationData->fadeInTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (animationConfig->fadeOutTime < 0.0f)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
animationConfig->fadeOutTime = animationConfig->fadeInTime;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (animationConfig->timeScale <= -100.0f)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
animationConfig->timeScale = 1.0f / animationData->scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (animationData->frameCount > 1)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (animationConfig->position < 0.0f)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
animationConfig->position = fmod(animationConfig->position, animationData->duration);
|
|
|
|
animationConfig->position = animationData->duration - animationConfig->position;
|
|
|
|
}
|
|
|
|
else if (animationConfig->position == animationData->duration)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
animationConfig->position -= 0.000001f; // Play a little time before end.
|
2020-10-18 00:27:23 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (animationConfig->position > animationData->duration)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
animationConfig->position = fmod(animationConfig->position, animationData->duration);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (animationConfig->duration > 0.0f &&
|
|
|
|
animationConfig->position + animationConfig->duration > animationData->duration)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
animationConfig->duration = animationData->duration - animationConfig->position;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (animationConfig->playTimes < 0)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
animationConfig->playTimes = animationData->playTimes;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
animationConfig->playTimes = 1;
|
2021-12-25 10:04:45 +08:00
|
|
|
animationConfig->position = 0.0f;
|
|
|
|
if (animationConfig->duration > 0.0f)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
animationConfig->duration = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (animationConfig->duration == 0.0f)
|
|
|
|
{
|
|
|
|
animationConfig->duration = -1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
_fadeOut(animationConfig);
|
|
|
|
|
|
|
|
const auto animationState = BaseObject::borrowObject<AnimationState>();
|
|
|
|
animationState->init(_armature, animationData, animationConfig);
|
2021-12-25 10:04:45 +08:00
|
|
|
_animationDirty = true;
|
2020-10-18 00:27:23 +08:00
|
|
|
_armature->_cacheFrameIndex = -1;
|
|
|
|
|
|
|
|
if (!_animationStates.empty())
|
|
|
|
{
|
|
|
|
auto added = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (std::size_t i = 0, l = _animationStates.size(); i < l; ++i)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (animationState->layer > _animationStates[i]->layer)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
added = true;
|
2020-10-18 00:27:23 +08:00
|
|
|
auto parentInerator = std::find(_animationStates.begin(), _animationStates.end(), _animationStates[i]);
|
|
|
|
_animationStates.insert(parentInerator, animationState);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (i != l - 1 && animationState->layer > _animationStates[i + 1]->layer)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
added = true;
|
2020-10-18 00:27:23 +08:00
|
|
|
auto parentInerator = std::find(_animationStates.begin(), _animationStates.end(), _animationStates[i]);
|
|
|
|
_animationStates.insert(parentInerator + 1, animationState);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!added)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
_animationStates.push_back(animationState);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
_animationStates.push_back(animationState);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Child armature play same name animation.
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto slot : _armature->getSlots())
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
const auto childArmature = slot->getChildArmature();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (childArmature != nullptr && childArmature->inheritAnimation &&
|
2020-10-18 00:27:23 +08:00
|
|
|
childArmature->getAnimation()->hasAnimation(animationName) &&
|
2021-12-25 10:04:45 +08:00
|
|
|
childArmature->getAnimation()->getState(animationName) == nullptr)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
childArmature->getAnimation()->fadeIn(animationName); //
|
2020-10-18 00:27:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (animationConfig->fadeInTime <= 0.0f) // Blend animation state, update armature.
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_armature->advanceTime(0.0f);
|
2020-10-18 00:27:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_lastAnimationState = animationState;
|
|
|
|
|
|
|
|
return animationState;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
AnimationState* Animation::play(std::string_view animationName, int playTimes)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
_animationConfig->clear();
|
|
|
|
_animationConfig->resetToPose = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
_animationConfig->playTimes = playTimes;
|
|
|
|
_animationConfig->fadeInTime = 0.0f;
|
|
|
|
_animationConfig->animation = animationName;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
|
|
|
if (!animationName.empty())
|
|
|
|
{
|
|
|
|
playConfig(_animationConfig);
|
|
|
|
}
|
|
|
|
else if (_lastAnimationState == nullptr)
|
|
|
|
{
|
|
|
|
const auto defaultAnimation = _armature->_armatureData->defaultAnimation;
|
|
|
|
if (defaultAnimation != nullptr)
|
|
|
|
{
|
|
|
|
_animationConfig->animation = defaultAnimation->name;
|
|
|
|
playConfig(_animationConfig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!_lastAnimationState->isPlaying() && !_lastAnimationState->isCompleted())
|
|
|
|
{
|
|
|
|
_lastAnimationState->play();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_animationConfig->animation = _lastAnimationState->name;
|
|
|
|
playConfig(_animationConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _lastAnimationState;
|
|
|
|
}
|
|
|
|
#ifdef EGRET_WASM
|
2021-12-31 12:12:40 +08:00
|
|
|
AnimationState* Animation::fadeIn(std::string_view animationName,
|
2021-12-25 10:04:45 +08:00
|
|
|
float fadeInTime,
|
|
|
|
int playTimes,
|
|
|
|
int layer,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view group,
|
2021-12-25 10:04:45 +08:00
|
|
|
int fadeOutMode /*AnimationFadeOutMode*/
|
2020-10-18 00:27:23 +08:00
|
|
|
#else
|
2021-12-31 12:12:40 +08:00
|
|
|
AnimationState* Animation::fadeIn(std::string_view animationName,
|
2021-12-25 10:04:45 +08:00
|
|
|
float fadeInTime,
|
|
|
|
int playTimes,
|
|
|
|
int layer,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view group,
|
2021-12-25 10:04:45 +08:00
|
|
|
AnimationFadeOutMode fadeOutMode
|
|
|
|
#endif // EGRET_WASM
|
2020-10-18 00:27:23 +08:00
|
|
|
)
|
|
|
|
{
|
|
|
|
_animationConfig->clear();
|
|
|
|
_animationConfig->fadeOutMode = (AnimationFadeOutMode)fadeOutMode;
|
2021-12-25 10:04:45 +08:00
|
|
|
_animationConfig->playTimes = playTimes;
|
|
|
|
_animationConfig->layer = layer;
|
|
|
|
_animationConfig->fadeInTime = fadeInTime;
|
|
|
|
_animationConfig->animation = animationName;
|
|
|
|
_animationConfig->group = group;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
|
|
|
return playConfig(_animationConfig);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
AnimationState* Animation::gotoAndPlayByTime(std::string_view animationName, float time, int playTimes)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
_animationConfig->clear();
|
|
|
|
_animationConfig->resetToPose = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
_animationConfig->playTimes = playTimes;
|
|
|
|
_animationConfig->position = time;
|
|
|
|
_animationConfig->fadeInTime = 0.0f;
|
|
|
|
_animationConfig->animation = animationName;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
|
|
|
return playConfig(_animationConfig);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
AnimationState* Animation::gotoAndPlayByFrame(std::string_view animationName, unsigned frame, int playTimes)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
_animationConfig->clear();
|
|
|
|
_animationConfig->resetToPose = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
_animationConfig->playTimes = playTimes;
|
|
|
|
_animationConfig->fadeInTime = 0.0f;
|
|
|
|
_animationConfig->animation = animationName;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
|
|
|
const auto animationData = mapFind(_animations, animationName);
|
|
|
|
if (animationData != nullptr)
|
|
|
|
{
|
|
|
|
_animationConfig->position = animationData->duration * frame / animationData->frameCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return playConfig(_animationConfig);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
AnimationState* Animation::gotoAndPlayByProgress(std::string_view animationName, float progress, int playTimes)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
_animationConfig->clear();
|
|
|
|
_animationConfig->resetToPose = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
_animationConfig->playTimes = playTimes;
|
|
|
|
_animationConfig->fadeInTime = 0.0f;
|
|
|
|
_animationConfig->animation = animationName;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
|
|
|
const auto animationData = mapFind(_animations, animationName);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (animationData != nullptr)
|
|
|
|
{
|
2020-10-18 00:27:23 +08:00
|
|
|
_animationConfig->position = animationData->duration * (progress > 0.0f ? progress : 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return playConfig(_animationConfig);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
AnimationState* Animation::gotoAndStopByTime(std::string_view animationName, float time)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
const auto animationState = gotoAndPlayByTime(animationName, time, 1);
|
|
|
|
if (animationState != nullptr)
|
|
|
|
{
|
|
|
|
animationState->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
return animationState;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
AnimationState* Animation::gotoAndStopByFrame(std::string_view animationName, unsigned frame)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
const auto animationState = gotoAndPlayByFrame(animationName, frame, 1);
|
|
|
|
if (animationState != nullptr)
|
|
|
|
{
|
|
|
|
animationState->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
return animationState;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
AnimationState* Animation::gotoAndStopByProgress(std::string_view animationName, float progress)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
const auto animationState = gotoAndPlayByProgress(animationName, progress, 1);
|
|
|
|
if (animationState != nullptr)
|
|
|
|
{
|
|
|
|
animationState->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
return animationState;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
AnimationState* Animation::getState(std::string_view animationName) const
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
int i = _animationStates.size();
|
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
const auto animationState = _animationStates[i];
|
|
|
|
if (animationState->name == animationName)
|
|
|
|
{
|
|
|
|
return animationState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool Animation::hasAnimation(std::string_view animationName) const
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
return _animations.find(animationName) != _animations.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Animation::isPlaying() const
|
|
|
|
{
|
|
|
|
for (const auto animationState : _animationStates)
|
|
|
|
{
|
|
|
|
if (animationState->isPlaying())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Animation::isCompleted() const
|
|
|
|
{
|
|
|
|
for (const auto animationState : _animationStates)
|
|
|
|
{
|
|
|
|
if (!animationState->isCompleted())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return !_animationStates.empty();
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view Animation::getLastAnimationName() const
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
if (_lastAnimationState != nullptr)
|
|
|
|
{
|
|
|
|
return _lastAnimationState->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const std::string DEFAULT_NAME = "";
|
|
|
|
return DEFAULT_NAME;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void Animation::setAnimations(const hlookup::string_map<AnimationData*>& value)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
if (_animations == value)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_animationNames.clear();
|
|
|
|
_animations.clear();
|
|
|
|
|
|
|
|
for (const auto& pair : value)
|
|
|
|
{
|
|
|
|
_animationNames.push_back(pair.first);
|
|
|
|
_animations[pair.first] = pair.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AnimationConfig* Animation::getAnimationConfig() const
|
|
|
|
{
|
|
|
|
_animationConfig->clear();
|
|
|
|
return _animationConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
DRAGONBONES_NAMESPACE_END
|