axmol/extensions/CCArmature/animation/CCTween.cpp

439 lines
11 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 "CCTween.h"
#include "CCArmatureAnimation.h"
2013-06-06 12:02:54 +08:00
#include "../CCBone.h"
#include "../CCArmature.h"
#include "../utils/CCUtilMath.h"
#include "../utils/CCTweenFunction.h"
namespace cocos2d { namespace extension { namespace armature {
2013-06-06 12:02:54 +08:00
Tween *Tween::create(Bone *bone)
2013-06-06 12:02:54 +08:00
{
Tween *pTween = new Tween();
2013-06-06 12:02:54 +08:00
if (pTween && pTween->init(bone))
{
pTween->autorelease();
return pTween;
}
CC_SAFE_DELETE(pTween);
return NULL;
}
Tween::Tween()
: _movementBoneData(NULL)
, _tweenData(NULL)
, _from(NULL)
, _to(NULL)
, _between(NULL)
, _currentKeyFrame(NULL)
, _bone(NULL)
2013-06-07 18:50:35 +08:00
, _frameTweenEasing(Linear)
, _fromIndex(0)
, _toIndex(0)
, _animation(NULL)
2013-06-06 12:02:54 +08:00
{
}
Tween::~Tween(void)
2013-06-06 12:02:54 +08:00
{
CC_SAFE_DELETE( _from );
CC_SAFE_DELETE( _between );
2013-06-06 12:02:54 +08:00
}
bool Tween::init(Bone *bone)
2013-06-06 12:02:54 +08:00
{
bool bRet = false;
do
{
_from = new FrameData();
_between = new FrameData();
2013-06-07 10:52:32 +08:00
_bone = bone;
_tweenData = _bone->getTweenData();
2013-06-07 10:52:32 +08:00
_animation = _bone->getArmature() != NULL ? _bone->getArmature()->getAnimation() : NULL;
2013-06-06 12:02:54 +08:00
bRet = true;
}
while (0);
return bRet;
}
void Tween::play(MovementBoneData *_movementBoneData, int _durationTo, int _durationTween, int _loop, int _tweenEasing)
2013-06-06 12:02:54 +08:00
{
ProcessBase::play(NULL, _durationTo, _durationTween, _loop, _tweenEasing);
2013-06-07 10:52:32 +08:00
_loopType = (AnimationType)_loop;
2013-06-06 12:02:54 +08:00
_currentKeyFrame = NULL;
_isTweenKeyFrame = false;
2013-06-07 10:52:32 +08:00
_totalDuration = 0;
2013-06-06 12:02:54 +08:00
betweenDuration = 0;
_toIndex = 0;
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
setMovementBoneData(_movementBoneData);
2013-06-07 10:52:32 +08:00
if (_movementBoneData->frameList.count() == 1)
2013-06-06 12:02:54 +08:00
{
_loopType = SINGLE_FRAME;
FrameData *_nextKeyFrame = _movementBoneData->getFrameData(0);
2013-06-07 10:52:32 +08:00
if(_durationTo == 0)
{
setBetween(_nextKeyFrame, _nextKeyFrame);
}
else
{
_tweenData->displayIndex = _nextKeyFrame->displayIndex;
setBetween(_tweenData, _nextKeyFrame);
2013-06-07 10:52:32 +08:00
}
_isTweenKeyFrame = true;
_frameTweenEasing = Linear;
_rawDuration = _movementBoneData->duration;
_fromIndex = _toIndex = 0;
2013-06-06 12:02:54 +08:00
}
else if (_movementBoneData->frameList.count() > 1)
2013-06-06 12:02:54 +08:00
{
2013-06-07 10:52:32 +08:00
if (_loop)
{
_loopType = ANIMATION_TO_LOOP_BACK;
_rawDuration = _movementBoneData->duration;
2013-06-07 10:52:32 +08:00
}
else
{
_loopType = ANIMATION_NO_LOOP;
_rawDuration = _movementBoneData->duration - 1;
2013-06-07 10:52:32 +08:00
}
2013-06-06 12:02:54 +08:00
_durationTween = _durationTween * _movementBoneData->scale;
2013-06-06 12:02:54 +08:00
if (_loop && _movementBoneData->delay != 0)
2013-06-06 12:02:54 +08:00
{
setBetween(_tweenData, tweenNodeTo(updateFrameData(1 - _movementBoneData->delay), _between));
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
}
else
{
FrameData *_nextKeyFrame = _movementBoneData->getFrameData(0);
setBetween(_tweenData, _nextKeyFrame);
_isTweenKeyFrame = true;
2013-06-06 12:02:54 +08:00
}
}
}
2013-06-07 10:52:32 +08:00
void Tween::updateHandler()
2013-06-06 12:02:54 +08:00
{
2013-06-07 10:52:32 +08:00
if (_currentPercent >= 1)
2013-06-07 10:52:32 +08:00
{
switch(_loopType)
2013-06-07 10:52:32 +08:00
{
case SINGLE_FRAME:
{
_currentPercent = 1;
_isComplete = true;
2013-06-07 10:52:32 +08:00
}
break;
case ANIMATION_NO_LOOP:
{
_loopType = ANIMATION_MAX;
2013-06-07 10:52:32 +08:00
if (_durationTween <= 0)
2013-06-07 10:52:32 +08:00
{
_currentPercent = 1;
2013-06-07 10:52:32 +08:00
}
else
{
_currentPercent = (_currentPercent - 1) * _nextFrameIndex / _durationTween;
2013-06-07 10:52:32 +08:00
}
if (_currentPercent >= 1)
2013-06-07 10:52:32 +08:00
{
_currentPercent = 1;
_isComplete = true;
2013-06-07 10:52:32 +08:00
break;
}
else
{
_nextFrameIndex = _durationTween;
_currentFrame = _currentPercent * _nextFrameIndex;
_totalDuration = 0;
2013-06-07 10:52:32 +08:00
betweenDuration = 0;
_toIndex = 0;
2013-06-07 10:52:32 +08:00
break;
}
}
break;
case ANIMATION_TO_LOOP_BACK:
{
_loopType = ANIMATION_LOOP_BACK;
2013-06-07 10:52:32 +08:00
_nextFrameIndex = _durationTween > 0 ? _durationTween : 1;
2013-06-07 10:52:32 +08:00
if (_movementBoneData->delay != 0)
2013-06-07 10:52:32 +08:00
{
//
_currentFrame = (1 - _movementBoneData->delay) * (float)_nextFrameIndex;
_currentPercent = _currentFrame / _nextFrameIndex;
2013-06-07 10:52:32 +08:00
}
else
{
_currentPercent = 0;
_currentFrame = 0;
2013-06-07 10:52:32 +08:00
}
_totalDuration = 0;
2013-06-07 10:52:32 +08:00
betweenDuration = 0;
_toIndex = 0;
2013-06-07 10:52:32 +08:00
}
break;
case ANIMATION_MAX:
{
_currentPercent = 1;
_isComplete = true;
2013-06-07 10:52:32 +08:00
}
break;
default:
{
_currentPercent = fmodf(_currentPercent, 1);
_currentFrame = fmodf(_currentFrame, _nextFrameIndex);
2013-06-07 10:52:32 +08:00
_totalDuration = 0;
2013-06-07 10:52:32 +08:00
betweenDuration = 0;
_toIndex = 0;
2013-06-07 10:52:32 +08:00
}
break;
}
}
if (_currentPercent < 1 && _loopType <= ANIMATION_TO_LOOP_BACK)
2013-06-07 10:52:32 +08:00
{
_currentPercent = sin(_currentPercent * CC_HALF_PI);
2013-06-07 10:52:32 +08:00
}
float percent = _currentPercent;
2013-06-07 10:52:32 +08:00
if (_loopType > ANIMATION_TO_LOOP_BACK)
2013-06-07 10:52:32 +08:00
{
percent = updateFrameData(percent, true);
}
if(_frameTweenEasing != TWEEN_EASING_MAX)
2013-06-07 10:52:32 +08:00
{
tweenNodeTo(percent);
}
else if(_currentKeyFrame)
2013-06-07 10:52:32 +08:00
{
tweenNodeTo(0);
}
2013-06-06 12:02:54 +08:00
}
void Tween::setBetween(FrameData *from, FrameData *to)
2013-06-06 12:02:54 +08:00
{
2013-06-07 10:52:32 +08:00
do
{
if(to->displayIndex < 0 && from->displayIndex > 0)
{
_from->copy(from);
_between->subtract(to, to);
2013-06-07 10:52:32 +08:00
break;
}
else if(from->displayIndex < 0 && to->displayIndex > 0)
{
_from->copy(to);
_between->subtract(to, to);
2013-06-07 10:52:32 +08:00
break;
}
_from->copy(from);
_between->subtract(from, to);
2013-06-07 10:52:32 +08:00
}
while (0);
arriveKeyFrame(from);
2013-06-06 12:02:54 +08:00
}
void Tween::arriveKeyFrame(FrameData *keyFrameData)
2013-06-06 12:02:54 +08:00
{
2013-06-07 10:52:32 +08:00
if(keyFrameData)
{
int displayIndex = keyFrameData->displayIndex;
if (!_bone->getDisplayManager()->getForceChangeDisplay())
2013-06-07 10:52:32 +08:00
{
_bone->getDisplayManager()->changeDisplayByIndex(displayIndex, false);
2013-06-07 10:52:32 +08:00
}
_bone->setZOrder(keyFrameData->zOrder);
2013-06-07 10:52:32 +08:00
Armature *childAramture = _bone->getChildArmature();
2013-06-07 10:52:32 +08:00
if(childAramture)
{
if(keyFrameData->_movement.length() != 0)
2013-06-07 10:52:32 +08:00
{
childAramture->getAnimation()->play(keyFrameData->_movement.c_str());
2013-06-07 10:52:32 +08:00
}
}
if(keyFrameData->_event.length() != 0)
2013-06-07 10:52:32 +08:00
{
_animation->FrameEventSignal.emit(_bone, keyFrameData->_event.c_str());
2013-06-07 10:52:32 +08:00
}
// if(keyFrameData->_sound.length() != 0)
2013-06-07 10:52:32 +08:00
// {
// //soundManager.dispatchEventWith(Event.SOUND_FRAME, _currentKeyFrame->sound);
2013-06-07 10:52:32 +08:00
// }
}
2013-06-06 12:02:54 +08:00
}
FrameData *Tween::tweenNodeTo(float percent, FrameData *node)
2013-06-06 12:02:54 +08:00
{
node = node == NULL ? _tweenData : node;
2013-06-07 10:52:32 +08:00
node->x = _from->x + percent * _between->x;
node->y = _from->y + percent * _between->y;
node->scaleX = _from->scaleX + percent * _between->scaleX;
node->scaleY = _from->scaleY + percent * _between->scaleY;
node->skewX = _from->skewX + percent * _between->skewX;
node->skewY = _from->skewY + percent * _between->skewY;
2013-06-07 10:52:32 +08:00
_bone->setTransformDirty(true);
2013-06-07 10:52:32 +08:00
if(_between->isUseColorInfo)
2013-06-07 10:52:32 +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();
2013-06-07 10:52:32 +08:00
}
2013-07-12 18:04:32 +08:00
// Point p1 = Point(_from->x, _from->y);
// Point p2 = Point(100, 0);
// Point p3 = Point(200, 400);
// Point p4 = Point(_from->x + _between->x, _from->y + _between->y);
2013-06-07 10:52:32 +08:00
//
// Point p = bezierTo(percent, p1, p2, p3, p4);
2013-06-07 10:52:32 +08:00
// node->x = p.x;
// node->y = p.y;
return node;
2013-06-06 12:02:54 +08:00
}
float Tween::updateFrameData(float currentPrecent, bool activeFrame)
2013-06-06 12:02:54 +08:00
{
float playedTime = (float)_rawDuration * currentPrecent;
2013-06-07 10:52:32 +08:00
FrameData *from;
FrameData *to;
2013-06-06 12:02:54 +08:00
bool isListEnd;
//! If play to current frame's front or back, then find current frame again
if (playedTime >= _totalDuration || playedTime < _totalDuration - betweenDuration)
2013-06-06 12:02:54 +08:00
{
/*
* Get frame length, if _toIndex >= _length, then set _toIndex to 0, start anew.
* _toIndex is next index will play
2013-06-06 12:02:54 +08:00
*/
int length = _movementBoneData->frameList.count();
2013-06-06 12:02:54 +08:00
do
{
betweenDuration = _movementBoneData->getFrameData(_toIndex)->duration;
_totalDuration += betweenDuration;
_fromIndex = _toIndex;
2013-06-07 10:52:32 +08:00
if (++_toIndex >= length)
2013-06-06 12:02:54 +08:00
{
_toIndex = 0;
2013-06-06 12:02:54 +08:00
}
}
while (playedTime >= _totalDuration);
2013-06-07 10:52:32 +08:00
isListEnd = _loopType == ANIMATION_MAX && _toIndex == 0;
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
if(isListEnd)
{
to = from = _movementBoneData->getFrameData(_fromIndex);
2013-06-06 12:02:54 +08:00
}
else
{
from = _movementBoneData->getFrameData(_fromIndex);
to = _movementBoneData->getFrameData(_toIndex);
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
_frameTweenEasing = from->tweenEasing;
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
setBetween(from, to);
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
}
currentPrecent = 1 - (_totalDuration - playedTime) / (float)betweenDuration;
2013-06-06 12:02:54 +08:00
2013-06-07 10:52:32 +08:00
/*
2013-06-06 12:02:54 +08:00
* If frame tween easing equal to TWEEN_EASING_MAX, then it will not do tween.
*/
2013-06-07 10:52:32 +08:00
TweenType tweenType;
2013-06-07 10:52:32 +08:00
if ( _frameTweenEasing != TWEEN_EASING_MAX)
2013-06-06 12:02:54 +08:00
{
tweenType = (_tweenEasing == TWEEN_EASING_MAX) ? _frameTweenEasing : _tweenEasing;
2013-06-06 12:02:54 +08:00
if (tweenType != TWEEN_EASING_MAX)
{
currentPrecent = TweenFunction::tweenTo(0, 1, currentPrecent, 1, tweenType);
2013-06-06 12:02:54 +08:00
}
}
return currentPrecent;
}
}}} // namespace cocos2d { namespace extension { namespace armature {