axmol/cocos/editor-support/cocostudio/CCArmatureAnimation.cpp

540 lines
14 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 "cocostudio/CCArmatureAnimation.h"
#include "cocostudio/CCArmature.h"
#include "cocostudio/CCBone.h"
#include "cocostudio/CCArmatureDefine.h"
#include "cocostudio/CCUtilMath.h"
#include "cocostudio/CCDatas.h"
2013-06-06 12:02:54 +08:00
using namespace cocos2d;
2013-06-06 12:02:54 +08:00
namespace cocostudio {
2013-06-07 10:52:32 +08:00
ArmatureAnimation *ArmatureAnimation::create(Armature *armature)
2013-06-06 12:02:54 +08:00
{
ArmatureAnimation *pArmatureAnimation = new ArmatureAnimation();
if (pArmatureAnimation && pArmatureAnimation->init(armature))
2013-06-06 12:02:54 +08:00
{
pArmatureAnimation->autorelease();
return pArmatureAnimation;
2013-06-06 12:02:54 +08:00
}
CC_SAFE_DELETE(pArmatureAnimation);
return nullptr;
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
ArmatureAnimation::ArmatureAnimation()
: _animationData(nullptr)
2013-09-15 20:24:25 +08:00
, _speedScale(1)
, _movementData(nullptr)
, _armature(nullptr)
2013-09-15 20:24:25 +08:00
, _movementID("")
, _toIndex(0)
2013-10-30 09:41:40 +08:00
, _ignoreFrameEvent(false)
, _onMovementList(false)
, _movementListLoop(false)
2013-12-18 23:29:54 +08:00
, _movementListDurationTo(-1)
, _userObject(nullptr)
2013-09-15 20:24:25 +08:00
, _movementEventCallFunc(nullptr)
, _frameEventCallFunc(nullptr)
, _movementEventTarget(nullptr)
, _frameEventTarget(nullptr)
2013-12-18 22:07:33 +08:00
, _movementEventListener(nullptr)
, _frameEventListener(nullptr)
2013-06-06 12:02:54 +08:00
{
}
ArmatureAnimation::~ArmatureAnimation(void)
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
CC_SAFE_RELEASE_NULL(_animationData);
CC_SAFE_RELEASE_NULL(_userObject);
2013-06-06 12:02:54 +08:00
}
bool ArmatureAnimation::init(Armature *armature)
2013-06-06 12:02:54 +08:00
{
bool bRet = false;
do
{
2013-09-15 20:24:25 +08:00
_armature = armature;
2013-06-07 10:52:32 +08:00
2013-12-17 19:32:16 +08:00
_tweenList.clear();
2013-06-06 12:02:54 +08:00
bRet = true;
}
while (0);
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
return bRet;
}
2013-12-18 22:07:33 +08:00
void ArmatureAnimation::pause()
2013-06-06 12:02:54 +08:00
{
for (const auto& tween : _tweenList)
2013-06-07 10:52:32 +08:00
{
2013-12-18 22:07:33 +08:00
tween->pause();
}
ProcessBase::pause();
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
void ArmatureAnimation::resume()
2013-06-06 12:02:54 +08:00
{
for (const auto& tween : _tweenList)
2013-06-07 10:52:32 +08:00
{
2013-12-18 22:07:33 +08:00
tween->resume();
}
ProcessBase::resume();
2013-06-06 12:02:54 +08:00
}
void ArmatureAnimation::stop()
2013-06-06 12:02:54 +08:00
{
for (const auto& tween : _tweenList)
2013-09-13 18:07:37 +08:00
{
2013-12-18 22:07:33 +08:00
tween->stop();
}
2013-12-17 19:32:16 +08:00
_tweenList.clear();
ProcessBase::stop();
2013-09-13 18:07:37 +08:00
}
void ArmatureAnimation::setAnimationScale(float animationScale )
2013-09-13 18:07:37 +08:00
{
setSpeedScale(animationScale);
}
float ArmatureAnimation::getAnimationScale() const
2013-09-13 18:07:37 +08:00
{
return getSpeedScale();
}
void ArmatureAnimation::setSpeedScale(float speedScale)
2013-09-13 18:07:37 +08:00
{
2013-09-15 20:24:25 +08:00
if(speedScale == _speedScale)
2013-06-07 10:52:32 +08:00
{
2013-09-13 18:07:37 +08:00
return;
}
2013-09-15 20:24:25 +08:00
_speedScale = speedScale;
2013-09-13 18:07:37 +08:00
2013-09-15 20:24:25 +08:00
_processScale = !_movementData ? _speedScale : _speedScale * _movementData->scale;
2013-09-13 18:07:37 +08:00
const Map<std::string, Bone*>& map = _armature->getBoneDic();
2013-12-18 22:07:33 +08:00
for(auto& element : map)
2013-09-13 18:07:37 +08:00
{
Bone *bone = element.second;
2013-09-13 18:07:37 +08:00
2013-09-15 20:24:25 +08:00
bone->getTween()->setProcessScale(_processScale);
2013-09-13 18:07:37 +08:00
if (bone->getChildArmature())
{
2013-09-15 20:24:25 +08:00
bone->getChildArmature()->getAnimation()->setProcessScale(_processScale);
2013-09-13 18:07:37 +08:00
}
2013-06-07 10:52:32 +08:00
}
2013-06-06 12:02:54 +08:00
}
float ArmatureAnimation::getSpeedScale() const
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
return _speedScale;
2013-09-13 18:07:37 +08:00
}
2013-06-06 12:02:54 +08:00
2013-12-18 22:57:18 +08:00
void ArmatureAnimation::play(const char *animationName, int durationTo, int loop)
2013-06-06 12:02:54 +08:00
{
2013-09-16 12:02:57 +08:00
CCASSERT(_animationData, "_animationData can not be null");
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
_movementData = _animationData->getMovement(animationName);
2013-09-16 12:02:57 +08:00
CCASSERT(_movementData, "_movementData can not be null");
2013-06-07 10:52:32 +08:00
//! Get key frame count
2013-09-15 20:24:25 +08:00
_rawDuration = _movementData->duration;
2013-09-13 18:07:37 +08:00
2013-09-15 20:24:25 +08:00
_movementID = animationName;
2013-06-06 12:02:54 +08:00
2013-09-15 20:24:25 +08:00
_processScale = _speedScale * _movementData->scale;
2013-06-07 10:52:32 +08:00
//! Further processing parameters
2013-09-15 20:24:25 +08:00
durationTo = (durationTo == -1) ? _movementData->durationTo : durationTo;
2013-06-07 10:52:32 +08:00
2013-12-18 22:57:18 +08:00
int durationTween = _movementData->durationTween == 0 ? _rawDuration : _movementData->durationTween;
2013-06-07 10:52:32 +08:00
2013-12-18 22:57:18 +08:00
TweenType tweenEasing = _movementData->tweenEasing;
2013-09-15 20:24:25 +08:00
loop = (loop < 0) ? _movementData->loop : loop;
2013-06-07 10:52:32 +08:00
_onMovementList = false;
2013-06-07 10:52:32 +08:00
ProcessBase::play(durationTo, durationTween, loop, tweenEasing);
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
if (_rawDuration == 0)
2013-06-07 10:52:32 +08:00
{
2013-09-15 20:24:25 +08:00
_loopType = SINGLE_FRAME;
2013-06-07 10:52:32 +08:00
}
else
{
if (loop)
{
2013-09-15 20:24:25 +08:00
_loopType = ANIMATION_TO_LOOP_FRONT;
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-09-15 20:24:25 +08:00
_durationTween = durationTween;
2013-06-07 10:52:32 +08:00
}
MovementBoneData *movementBoneData = nullptr;
2013-12-17 19:32:16 +08:00
_tweenList.clear();
2013-06-06 12:02:54 +08:00
const Map<std::string, Bone*>& map = _armature->getBoneDic();
2013-12-18 22:07:33 +08:00
for(auto& element : map)
2013-06-07 10:52:32 +08:00
{
Bone *bone = element.second;
2013-12-13 19:40:38 +08:00
movementBoneData = static_cast<MovementBoneData *>(_movementData->movBoneDataDic.at(bone->getName()));
2013-06-06 12:02:54 +08:00
Tween *tween = bone->getTween();
2013-12-13 19:40:38 +08:00
if(movementBoneData && movementBoneData->frameList.size() > 0)
2013-06-06 12:02:54 +08:00
{
2013-12-17 19:32:16 +08:00
_tweenList.pushBack(tween);
2013-09-15 20:24:25 +08:00
movementBoneData->duration = _movementData->duration;
2013-06-06 12:02:54 +08:00
tween->play(movementBoneData, durationTo, durationTween, loop, tweenEasing);
2013-09-15 20:24:25 +08:00
tween->setProcessScale(_processScale);
2013-09-13 18:07:37 +08:00
2013-06-07 10:52:32 +08:00
if (bone->getChildArmature())
{
2013-09-15 20:24:25 +08:00
bone->getChildArmature()->getAnimation()->setProcessScale(_processScale);
2013-06-07 10:52:32 +08:00
}
2013-06-06 12:02:54 +08:00
}
else
{
2013-11-05 19:53:38 +08:00
if(!bone->isIgnoreMovementBoneData())
2013-06-07 10:52:32 +08:00
{
//! this bone is not include in this movement, so hide it
bone->getDisplayManager()->changeDisplayByIndex(-1, false);
tween->stop();
}
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
}
2013-10-30 09:41:40 +08:00
_armature->update(0);
2013-06-06 12:02:54 +08:00
}
2013-12-18 22:57:18 +08:00
void ArmatureAnimation::playByIndex(int animationIndex, int durationTo, int loop)
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
std::vector<std::string> &movName = _animationData->movementNames;
2013-06-07 17:10:53 +08:00
CC_ASSERT((animationIndex > -1) && ((unsigned int)animationIndex < movName.size()));
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
std::string animationName = movName.at(animationIndex);
2013-12-18 22:57:18 +08:00
play(animationName.c_str(), durationTo, loop);
2013-06-06 12:02:54 +08:00
}
2013-12-18 23:29:54 +08:00
void ArmatureAnimation::play(const std::vector<std::string>& movementNames, int durationTo, bool loop)
{
_movementList.clear();
_movementListLoop = loop;
2013-12-18 23:29:54 +08:00
_movementListDurationTo = durationTo;
_onMovementList = true;
_movementIndex = 0;
2013-12-18 23:29:54 +08:00
_movementList = movementNames;
updateMovementList();
}
2013-12-18 23:29:54 +08:00
void ArmatureAnimation::playByIndex(const std::vector<int>& movementIndexes, int durationTo, bool loop)
{
_movementList.clear();
_movementListLoop = loop;
2013-12-18 23:29:54 +08:00
_movementListDurationTo = durationTo;
_onMovementList = true;
_movementIndex = 0;
std::vector<std::string> &movName = _animationData->movementNames;
2013-12-18 23:29:54 +08:00
for(auto& index : movementIndexes)
{
2013-12-18 23:29:54 +08:00
std::string name = movName.at(index);
_movementList.push_back(name);
}
updateMovementList();
}
2013-10-30 09:41:40 +08:00
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;
_ignoreFrameEvent = true;
_isPlaying = true;
_isComplete = _isPause = false;
ProcessBase::gotoFrame(frameIndex);
_currentPercent = (float)_curFrameIndex / ((float)_movementData->duration-1);
2013-10-30 09:41:40 +08:00
_currentFrame = _nextFrameIndex * _currentPercent;
2013-12-18 22:07:33 +08:00
for (const auto &tween : _tweenList)
2013-10-30 09:41:40 +08:00
{
2013-12-18 22:07:33 +08:00
tween->gotoAndPlay(frameIndex);
}
2013-10-30 09:41:40 +08:00
_armature->update(0);
_ignoreFrameEvent = ignoreFrameEvent;
}
2013-06-06 12:02:54 +08:00
2013-10-30 09:41:40 +08:00
void ArmatureAnimation::gotoAndPause(int frameIndex)
{
gotoAndPlay(frameIndex);
pause();
}
2013-06-06 12:02:54 +08:00
2013-12-17 19:32:16 +08:00
long ArmatureAnimation::getMovementCount() const
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
return _animationData->getMovementCount();
2013-06-06 12:02:54 +08:00
}
void ArmatureAnimation::update(float dt)
2013-06-06 12:02:54 +08:00
{
ProcessBase::update(dt);
for (const auto &tween : _tweenList)
2013-06-07 10:52:32 +08:00
{
2013-12-18 22:07:33 +08:00
tween->update(dt);
}
2013-10-30 09:41:40 +08:00
while (_frameEventQueue.size() > 0)
{
FrameEvent *event = _frameEventQueue.front();
2013-10-30 09:41:40 +08:00
_frameEventQueue.pop();
_ignoreFrameEvent = true;
2013-12-18 22:07:33 +08:00
if(_frameEventTarget)
{
(_frameEventTarget->*_frameEventCallFunc)(event->bone, event->frameEventName, event->originFrameIndex, event->currentFrameIndex);
}
if (_frameEventListener)
{
_frameEventListener(event->bone, event->frameEventName, event->originFrameIndex, event->currentFrameIndex);
}
2013-10-30 09:41:40 +08:00
_ignoreFrameEvent = false;
CC_SAFE_DELETE(event);
}
while (_movementEventQueue.size() > 0)
{
MovementEvent *event = _movementEventQueue.front();
_movementEventQueue.pop();
2013-12-18 22:07:33 +08:00
if(_movementEventTarget)
{
(_movementEventTarget->*_movementEventCallFunc)(event->armature, event->movementType, event->movementID);
}
if (_movementEventListener)
{
_movementEventListener(event->armature, event->movementType, event->movementID);
}
CC_SAFE_DELETE(event);
2013-10-30 09:41:40 +08:00
}
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
void ArmatureAnimation::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 ANIMATION_NO_LOOP:
{
2013-09-15 20:24:25 +08:00
_loopType = ANIMATION_MAX;
2013-09-16 14:13:55 +08:00
_currentFrame = (_currentPercent - 1) * _nextFrameIndex;
2013-09-15 20:24:25 +08:00
_currentPercent = _currentFrame / _durationTween;
2013-06-07 10:52:32 +08:00
2013-09-15 20:24:25 +08:00
if (_currentPercent >= 1.0f)
2013-06-07 10:52:32 +08:00
{
}
else
{
2013-09-16 14:13:55 +08:00
_nextFrameIndex = _durationTween;
2013-06-07 10:52:32 +08:00
2013-12-18 22:07:33 +08:00
movementEvent(_armature, START, _movementID.c_str());
2013-06-07 10:52:32 +08:00
break;
}
}
break;
case ANIMATION_MAX:
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
2013-12-18 22:07:33 +08:00
movementEvent(_armature, COMPLETE, _movementID.c_str());
updateMovementList();
2013-06-07 10:52:32 +08:00
}
break;
case ANIMATION_TO_LOOP_FRONT:
{
2013-09-15 20:24:25 +08:00
_loopType = ANIMATION_LOOP_FRONT;
_currentPercent = fmodf(_currentPercent, 1);
2013-09-16 14:13:55 +08:00
_currentFrame = _nextFrameIndex == 0 ? 0 : fmodf(_currentFrame, _nextFrameIndex);
_nextFrameIndex = _durationTween > 0 ? _durationTween : 1;
2013-06-07 10:52:32 +08:00
2013-12-18 22:07:33 +08:00
movementEvent(_armature, START, _movementID.c_str());
2013-06-07 10:52:32 +08:00
}
break;
default:
{
2013-09-15 20:24:25 +08:00
//_currentPercent = fmodf(_currentPercent, 1);
2013-09-16 14:13:55 +08:00
_currentFrame = fmodf(_currentFrame, _nextFrameIndex);
2013-09-15 20:24:25 +08:00
_toIndex = 0;
2013-06-07 10:52:32 +08:00
2013-12-18 22:07:33 +08:00
movementEvent(_armature, LOOP_COMPLETE, _movementID.c_str());
2013-06-07 10:52:32 +08:00
}
break;
}
}
2013-09-13 18:07:37 +08:00
}
2013-06-07 10:52:32 +08:00
2013-11-05 19:53:38 +08:00
std::string ArmatureAnimation::getCurrentMovementID() const
2013-09-13 18:07:37 +08:00
{
2013-09-15 20:24:25 +08:00
if (_isComplete)
2013-06-07 10:52:32 +08:00
{
2013-09-13 18:07:37 +08:00
return "";
2013-06-07 10:52:32 +08:00
}
2013-09-15 20:24:25 +08:00
return _movementID;
2013-06-06 12:02:54 +08:00
}
void ArmatureAnimation::setMovementEventCallFunc(Object *target, SEL_MovementEventCallFunc callFunc)
2013-06-06 12:02:54 +08:00
{
_movementEventTarget = target;
2013-09-15 20:24:25 +08:00
_movementEventCallFunc = callFunc;
2013-06-06 12:02:54 +08:00
}
void ArmatureAnimation::setFrameEventCallFunc(Object *target, SEL_FrameEventCallFunc callFunc)
2013-09-13 18:07:37 +08:00
{
_frameEventTarget = target;
2013-09-15 20:24:25 +08:00
_frameEventCallFunc = callFunc;
2013-09-13 18:07:37 +08:00
}
2013-06-07 10:52:32 +08:00
2013-12-18 22:07:33 +08:00
void ArmatureAnimation::setMovementEventCallFunc(std::function<void(Armature *armature, MovementEventType movementType, const char *movementID)> listener)
{
_movementEventListener = listener;
}
void ArmatureAnimation::setFrameEventCallFunc(std::function<void(Bone *bone, const char *frameEventName, int originFrameIndex, int currentFrameIndex)> listener)
{
_frameEventListener = listener;
}
void ArmatureAnimation::setUserObject(Object *pUserObject)
{
CC_SAFE_RETAIN(pUserObject);
CC_SAFE_RELEASE(_userObject);
_userObject = pUserObject;
}
void ArmatureAnimation::frameEvent(Bone *bone, const char *frameEventName, int originFrameIndex, int currentFrameIndex)
2013-09-13 18:07:37 +08:00
{
2013-12-18 22:07:33 +08:00
if ((_frameEventTarget && _frameEventCallFunc) || _frameEventListener)
2013-09-13 18:07:37 +08:00
{
2013-10-30 09:41:40 +08:00
FrameEvent *frameEvent = new FrameEvent();
frameEvent->bone = bone;
frameEvent->frameEventName = frameEventName;
frameEvent->originFrameIndex = originFrameIndex;
frameEvent->currentFrameIndex = currentFrameIndex;
_frameEventQueue.push(frameEvent);
2013-09-13 18:07:37 +08:00
}
}
2013-12-09 13:56:07 +08:00
void ArmatureAnimation::movementEvent(Armature *armature, MovementEventType movementType, const char *movementID)
{
2013-12-18 22:07:33 +08:00
if ((_movementEventTarget && _movementEventCallFunc) || _movementEventListener)
{
MovementEvent *movementEvent = new MovementEvent();
movementEvent->armature = armature;
movementEvent->movementType = movementType;
movementEvent->movementID = movementID;
_movementEventQueue.push(movementEvent);
}
}
2013-12-09 13:56:07 +08:00
void ArmatureAnimation::updateMovementList()
{
if (_onMovementList)
{
if (_movementListLoop)
{
2013-12-18 23:29:54 +08:00
play(_movementList.at(_movementIndex).c_str(), _movementListDurationTo, 0);
_movementIndex++;
if (_movementIndex >= _movementList.size())
{
_movementIndex = 0;
}
}
else
{
if (_movementIndex < _movementList.size())
{
2013-12-18 23:29:54 +08:00
play(_movementList.at(_movementIndex).c_str(), _movementListDurationTo, 0);
_movementIndex++;
}
else
{
_onMovementList = false;
}
}
_onMovementList = true;
}
}
}