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"
NS_CC_EXT_BEGIN
2013-06-07 10:52:32 +08:00
CCTween *CCTween::create(CCBone *bone)
2013-06-06 12:02:54 +08:00
{
CCTween *pTween = new CCTween();
if (pTween && pTween->init(bone))
{
pTween->autorelease();
return pTween;
}
CC_SAFE_DELETE(pTween);
return NULL;
}
CCTween::CCTween()
: _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
{
}
CCTween::~CCTween(void)
{
CC_SAFE_DELETE( _from );
CC_SAFE_DELETE( _between );
2013-06-06 12:02:54 +08:00
}
bool CCTween::init(CCBone *bone)
{
bool bRet = false;
do
{
_from = new CCFrameData();
_between = new CCFrameData();
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;
}
2013-06-06 16:22:58 +08:00
void CCTween::play(CCMovementBoneData *_movementBoneData, int _durationTo, int _durationTween, int _loop, int _tweenEasing)
2013-06-06 12:02:54 +08:00
{
CCProcessBase::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;
CCFrameData *_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
{
CCFrameData *_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
2013-06-06 12:02:54 +08:00
void CCTween::updateHandler()
{
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
}
2013-06-06 16:22:58 +08:00
void CCTween::setBetween(CCFrameData *from, CCFrameData *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
}
2013-06-06 16:22:58 +08:00
void CCTween::arriveKeyFrame(CCFrameData *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
CCArmature *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
}
2013-06-06 16:22:58 +08:00
CCFrameData *CCTween::tweenNodeTo(float percent, CCFrameData *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
}
// CCPoint p1 = ccp(_from->x, _from->y);
2013-06-07 10:52:32 +08:00
// CCPoint p2 = ccp(100, 0);
// CCPoint p3 = ccp(200, 400);
// CCPoint p4 = ccp(_from->x + _between->x, _from->y + _between->y);
2013-06-07 10:52:32 +08:00
//
// CCPoint p = bezierTo(percent, p1, p2, p3, p4);
// node->x = p.x;
// node->y = p.y;
return node;
2013-06-06 12:02:54 +08:00
}
float CCTween::updateFrameData(float currentPrecent, bool activeFrame)
{
float playedTime = (float)_rawDuration * currentPrecent;
2013-06-07 10:52:32 +08:00
2013-06-06 16:22:58 +08:00
CCFrameData *from;
CCFrameData *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
2013-06-06 12:02:54 +08:00
CCTweenType 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 = CCTweenFunction::tweenTo(0, 1, currentPrecent, 1, tweenType);
}
}
return currentPrecent;
}
2013-06-07 10:52:32 +08:00
NS_CC_EXT_END