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

159 lines
4.0 KiB
C++
Raw Normal View History

2013-06-06 12:02:54 +08:00
/****************************************************************************
2016-08-05 09:42:15 +08:00
Copyright (c) 2013-2016 Chukong Technologies Inc.
2013-06-06 12:02:54 +08:00
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 "editor-support/cocostudio/CCProcessBase.h"
#include "editor-support/cocostudio/CCUtilMath.h"
2013-06-06 12:02:54 +08:00
using namespace cocos2d;
namespace cocostudio {
2013-09-13 18:07:37 +08:00
ProcessBase::ProcessBase(void)
2013-09-15 20:24:25 +08:00
: _processScale(1)
, _isPause(true)
, _isComplete(true)
, _isPlaying(false)
, _currentPercent(0.0f)
, _rawDuration(0)
, _loopType(ANIMATION_LOOP_BACK)
, _tweenEasing(cocos2d::tweenfunc::Linear)
, _animationInternal(1/60.0f)
2013-09-15 20:24:25 +08:00
, _durationTween(0)
, _currentFrame(0)
, _curFrameIndex(0)
, _isLoopBack(false)
2013-06-06 12:02:54 +08:00
{
}
ProcessBase::~ProcessBase(void)
2013-06-06 12:02:54 +08:00
{
}
void ProcessBase::pause()
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
_isPause = true;
_isPlaying = false;
2013-06-06 12:02:54 +08:00
}
void ProcessBase::resume()
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
_isPause = false;
_isPlaying = true;
2013-06-06 12:02:54 +08:00
}
void ProcessBase::stop()
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
_isComplete = true;
_isPlaying = false;
2013-06-06 12:02:54 +08:00
}
void ProcessBase::play(int durationTo, int /*durationTween*/, int /*loop*/, int tweenEasing)
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
_isComplete = false;
_isPause = false;
_isPlaying = true;
_currentFrame = 0;
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
/*
2013-09-13 18:07:37 +08:00
* Set m_iTotalFrames to durationTo, it is used for change tween between two animation.
* When changing end, m_iTotalFrames will be set to _durationTween
2013-06-06 12:02:54 +08:00
*/
2013-09-16 14:13:55 +08:00
_nextFrameIndex = durationTo;
_tweenEasing = (cocos2d::tweenfunc::TweenType)tweenEasing;
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
void ProcessBase::update(float dt)
2013-06-07 10:52:32 +08:00
{
2013-09-15 20:24:25 +08:00
if (_isComplete || _isPause)
2013-06-06 12:02:54 +08:00
{
return;
}
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
/*
* Filter the m_iDuration <=0 and dt >1
2013-06-06 12:02:54 +08:00
* If dt>1, generally speaking the reason is the device is stuck.
*/
2013-09-15 20:24:25 +08:00
if(_rawDuration <= 0 || dt > 1)
2013-06-06 12:02:54 +08:00
{
return;
}
2013-06-07 10:52:32 +08:00
2013-09-16 14:13:55 +08:00
if (_nextFrameIndex <= 0)
2013-06-07 10:52:32 +08:00
{
2013-09-15 20:24:25 +08:00
_currentPercent = 1;
_currentFrame = 0;
2013-06-07 10:52:32 +08:00
}
2013-09-13 18:07:37 +08:00
else
{
/*
2013-09-15 20:24:25 +08:00
* update _currentFrame, every update add the frame passed.
* dt/_animationInternal determine it is not a frame animation. If frame speed changed, it will not make our
2013-09-13 18:07:37 +08:00
* animation speed slower or quicker.
*/
2013-09-15 20:24:25 +08:00
_currentFrame += _processScale * (dt / _animationInternal);
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
2013-09-16 14:13:55 +08:00
_currentPercent = _currentFrame / _nextFrameIndex;
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
/*
* if _currentFrame is bigger or equal than m_iTotalFrames, then reduce it until _currentFrame is
2013-09-13 18:07:37 +08:00
* smaller than m_iTotalFrames
*/
2013-09-16 14:13:55 +08:00
_currentFrame = fmodf(_currentFrame, _nextFrameIndex);
2013-09-13 18:07:37 +08:00
}
2013-06-07 10:52:32 +08:00
updateHandler();
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
void ProcessBase::gotoFrame(int frameIndex)
2013-06-06 12:02:54 +08:00
{
2013-10-30 09:41:40 +08:00
if (_loopType == ANIMATION_NO_LOOP)
{
_loopType = ANIMATION_MAX;
}
else if (_loopType == ANIMATION_TO_LOOP_FRONT)
{
_loopType = ANIMATION_LOOP_FRONT;
}
2013-09-15 20:24:25 +08:00
_curFrameIndex = frameIndex;
2013-10-30 09:41:40 +08:00
_nextFrameIndex = _durationTween;
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
int ProcessBase::getCurrentFrameIndex()
2013-06-06 12:02:54 +08:00
{
_curFrameIndex = (_rawDuration-1) * _currentPercent;
2013-09-15 20:24:25 +08:00
return _curFrameIndex;
2013-06-06 12:02:54 +08:00
}
}