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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-06-08 11:16:17 +08:00
|
|
|
#include "CCArmatureAnimation.h"
|
2013-06-06 12:02:54 +08:00
|
|
|
#include "../CCArmature.h"
|
|
|
|
#include "../CCBone.h"
|
|
|
|
#include "../utils/CCArmatureDefine.h"
|
|
|
|
#include "../utils/CCUtilMath.h"
|
|
|
|
#include "../utils/CCConstValue.h"
|
|
|
|
#include "../datas/CCDatas.h"
|
|
|
|
|
|
|
|
|
2013-06-17 21:32:15 +08:00
|
|
|
namespace cocos2d { namespace extension { namespace armature {
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ArmatureAnimation *ArmatureAnimation::create(Armature *armature)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ArmatureAnimation *pArmatureAnimation = new ArmatureAnimation();
|
2013-06-08 11:16:17 +08:00
|
|
|
if (pArmatureAnimation && pArmatureAnimation->init(armature))
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-08 11:16:17 +08:00
|
|
|
pArmatureAnimation->autorelease();
|
|
|
|
return pArmatureAnimation;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-08 11:16:17 +08:00
|
|
|
CC_SAFE_DELETE(pArmatureAnimation);
|
2013-06-06 12:02:54 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ArmatureAnimation::ArmatureAnimation()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _animationData(NULL)
|
|
|
|
, _armature(NULL)
|
|
|
|
, _movementID("")
|
|
|
|
, _toIndex(0)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ArmatureAnimation::~ArmatureAnimation(void)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_tweenList);
|
|
|
|
CC_SAFE_RELEASE_NULL(_animationData);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool ArmatureAnimation::init(Armature *armature)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_armature = armature;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
_tweenList = new Array();
|
2013-06-15 14:03:30 +08:00
|
|
|
_tweenList->init();
|
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-06-20 14:15:53 +08:00
|
|
|
void ArmatureAnimation:: pause()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Object *object = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_tweenList, object)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
static_cast<Tween *>(object)->pause();
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-20 14:15:53 +08:00
|
|
|
ProcessBase::pause();
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ArmatureAnimation::resume()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Object *object = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_tweenList, object)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
static_cast<Tween *>(object)->resume();
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-20 14:15:53 +08:00
|
|
|
ProcessBase::resume();
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ArmatureAnimation::stop()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Object *object = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_tweenList, object)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
static_cast<Tween *>(object)->stop();
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_tweenList->removeAllObjects();
|
2013-06-20 14:15:53 +08:00
|
|
|
ProcessBase::stop();
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ArmatureAnimation::setAnimationScale(float animationScale )
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if(animationScale == _animationScale)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_animationScale = animationScale;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
DictElement *element = NULL;
|
|
|
|
Dictionary *dict = _armature->getBoneDic();
|
2013-06-07 10:52:32 +08:00
|
|
|
CCDICT_FOREACH(dict, element)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Bone *bone = (Bone *)element->getObject();
|
2013-06-15 14:03:30 +08:00
|
|
|
bone->getTween()->setAnimationScale(_animationScale);
|
2013-06-07 10:52:32 +08:00
|
|
|
if (bone->getChildArmature())
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
bone->getChildArmature()->getAnimation()->setAnimationScale(_animationScale);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ArmatureAnimation::play(const char *animationName, int durationTo, int durationTween, int loop, int tweenEasing)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(_animationData, "_animationData can not be null");
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_movementData = _animationData->getMovement(animationName);
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(_movementData, "_movementData can not be null");
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
//! Get key frame count
|
2013-06-15 14:03:30 +08:00
|
|
|
_rawDuration = _movementData->duration;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_movementID = animationName;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
//! Further processing parameters
|
2013-06-15 14:03:30 +08:00
|
|
|
durationTo = (durationTo == -1) ? _movementData->durationTo : durationTo;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
durationTween = (durationTween == -1) ? _movementData->durationTween : durationTween;
|
|
|
|
durationTween = (durationTween == 0) ? _movementData->duration : durationTween;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
tweenEasing = (tweenEasing == TWEEN_EASING_MAX) ? _movementData->tweenEasing : tweenEasing;
|
|
|
|
loop = (loop < 0) ? _movementData->loop : loop;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ProcessBase::play((void *)animationName, durationTo, durationTween, loop, tweenEasing);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_rawDuration == 1)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_loopType = SINGLE_FRAME;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (loop)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_loopType = ANIMATION_TO_LOOP_FRONT;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_loopType = ANIMATION_NO_LOOP;
|
|
|
|
_rawDuration --;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_durationTween = durationTween;
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
MovementBoneData *movementBoneData = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
_tweenList->removeAllObjects();
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
DictElement *element = NULL;
|
|
|
|
Dictionary *dict = _armature->getBoneDic();
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
CCDICT_FOREACH(dict, element)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Bone *bone = (Bone *)element->getObject();
|
|
|
|
movementBoneData = (MovementBoneData *)_movementData->movBoneDataDic.objectForKey(bone->getName());
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Tween *tween = bone->getTween();
|
2013-06-07 10:52:32 +08:00
|
|
|
if(movementBoneData && movementBoneData->frameList.count() > 0)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_tweenList->addObject(tween);
|
2013-06-06 12:02:54 +08:00
|
|
|
tween->play(movementBoneData, durationTo, durationTween, loop, tweenEasing);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
tween->setAnimationScale(_animationScale);
|
2013-06-07 10:52:32 +08:00
|
|
|
if (bone->getChildArmature())
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
bone->getChildArmature()->getAnimation()->setAnimationScale(_animationScale);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
if(!bone->getIgnoreMovementBoneData())
|
|
|
|
{
|
|
|
|
//! 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-06-06 12:02:54 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
//_armature->update(0);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ArmatureAnimation::playByIndex(int animationIndex, int durationTo, int durationTween, int loop, int tweenEasing)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +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);
|
|
|
|
play(animationName.c_str(), durationTo, durationTween, loop, tweenEasing);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
int ArmatureAnimation::getMovementCount()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _animationData->getMovementCount();
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ArmatureAnimation::update(float dt)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ProcessBase::update(dt);
|
|
|
|
Object *object = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_tweenList, object)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
static_cast<Tween *>(object)->update(dt);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ArmatureAnimation::updateHandler()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_currentPercent >= 1)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
switch(_loopType)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
|
|
|
case ANIMATION_NO_LOOP:
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_loopType = ANIMATION_MAX;
|
|
|
|
_currentFrame = (_currentPercent - 1) * _nextFrameIndex;
|
|
|
|
_currentPercent = _currentFrame / _durationTween;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_currentPercent >= 1.0f)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_nextFrameIndex = _durationTween;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
MovementEventSignal.emit(_armature, START, _movementID.c_str());
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ANIMATION_MAX:
|
|
|
|
case SINGLE_FRAME:
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_currentPercent = 1;
|
|
|
|
_isComplete = true;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
MovementEventSignal.emit(_armature, COMPLETE, _movementID.c_str());
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ANIMATION_TO_LOOP_FRONT:
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_loopType = ANIMATION_LOOP_FRONT;
|
|
|
|
_currentPercent = fmodf(_currentPercent, 1);
|
|
|
|
_currentFrame = fmodf(_currentFrame, _nextFrameIndex);
|
|
|
|
_nextFrameIndex = _durationTween > 0 ? _durationTween : 1;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
MovementEventSignal.emit(_armature, START, _movementID.c_str());
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_currentPercent = fmodf(_currentPercent, 1);
|
|
|
|
_currentFrame = fmodf(_currentFrame, _nextFrameIndex);
|
|
|
|
_toIndex = 0;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
MovementEventSignal.emit(_armature, LOOP_COMPLETE, _movementID.c_str());
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_loopType == ANIMATION_LOOP_FRONT || _loopType == ANIMATION_LOOP_BACK)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
updateFrameData(_currentPercent);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void ArmatureAnimation::updateFrameData(float currentPercent)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_prevFrameIndex = _curFrameIndex;
|
|
|
|
_curFrameIndex = _rawDuration * currentPercent;
|
|
|
|
_curFrameIndex = _curFrameIndex % _rawDuration;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-17 21:32:15 +08:00
|
|
|
}}} // namespace cocos2d { namespace extension { namespace armature {
|