2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
2020-10-17 16:32:16 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
https://axys1.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "CCProcessBase.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace cocostudio
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-10-17 16:32:16 +08:00
|
|
|
ProcessBase::ProcessBase(void)
|
2019-11-23 20:27:39 +08:00
|
|
|
: _processScale(1)
|
|
|
|
, _isPause(true)
|
|
|
|
, _isComplete(true)
|
|
|
|
, _isPlaying(false)
|
|
|
|
, _currentPercent(0.0f)
|
|
|
|
, _rawDuration(0)
|
|
|
|
, _loopType(ANIMATION_LOOP_BACK)
|
2022-08-08 18:02:17 +08:00
|
|
|
, _tweenEasing(ax::tweenfunc::Linear)
|
2021-12-25 10:04:45 +08:00
|
|
|
, _animationInternal(1 / 60.0f)
|
2019-11-23 20:27:39 +08:00
|
|
|
, _durationTween(0)
|
|
|
|
, _currentFrame(0)
|
|
|
|
, _curFrameIndex(0)
|
|
|
|
, _isLoopBack(false)
|
2021-12-25 10:04:45 +08:00
|
|
|
{}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ProcessBase::~ProcessBase(void) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
void ProcessBase::pause()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_isPause = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
_isPlaying = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProcessBase::resume()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_isPause = false;
|
2019-11-23 20:27:39 +08:00
|
|
|
_isPlaying = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProcessBase::stop()
|
|
|
|
{
|
|
|
|
_isComplete = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
_isPlaying = false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ProcessBase::play(int durationTo, int /*durationTween*/, int /*loop*/, int tweenEasing)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_isComplete = false;
|
|
|
|
_isPause = false;
|
|
|
|
_isPlaying = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
_currentFrame = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set m_iTotalFrames to durationTo, it is used for change tween between two animation.
|
|
|
|
* When changing end, m_iTotalFrames will be set to _durationTween
|
|
|
|
*/
|
|
|
|
_nextFrameIndex = durationTo;
|
2022-08-08 18:02:17 +08:00
|
|
|
_tweenEasing = (ax::tweenfunc::TweenType)tweenEasing;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProcessBase::update(float dt)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (_isComplete || _isPause)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Filter the m_iDuration <=0 and dt >1
|
|
|
|
* If dt>1, generally speaking the reason is the device is stuck.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_rawDuration <= 0 || dt > 1)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_nextFrameIndex <= 0)
|
|
|
|
{
|
|
|
|
_currentPercent = 1;
|
2021-12-25 10:04:45 +08:00
|
|
|
_currentFrame = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
2021-12-25 10:04:45 +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
|
|
|
|
* animation speed slower or quicker.
|
|
|
|
*/
|
2019-11-23 20:27:39 +08:00
|
|
|
_currentFrame += _processScale * (dt / _animationInternal);
|
|
|
|
|
|
|
|
_currentPercent = _currentFrame / _nextFrameIndex;
|
|
|
|
|
|
|
|
/*
|
2021-12-25 10:04:45 +08:00
|
|
|
* if _currentFrame is bigger or equal than m_iTotalFrames, then reduce it until _currentFrame is
|
|
|
|
* smaller than m_iTotalFrames
|
|
|
|
*/
|
2019-11-23 20:27:39 +08:00
|
|
|
_currentFrame = fmodf(_currentFrame, _nextFrameIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateHandler();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProcessBase::gotoFrame(int frameIndex)
|
|
|
|
{
|
|
|
|
if (_loopType == ANIMATION_NO_LOOP)
|
|
|
|
{
|
|
|
|
_loopType = ANIMATION_MAX;
|
|
|
|
}
|
|
|
|
else if (_loopType == ANIMATION_TO_LOOP_FRONT)
|
|
|
|
{
|
|
|
|
_loopType = ANIMATION_LOOP_FRONT;
|
|
|
|
}
|
|
|
|
|
|
|
|
_curFrameIndex = frameIndex;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_nextFrameIndex = _durationTween;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ProcessBase::getCurrentFrameIndex()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_curFrameIndex = (_rawDuration - 1) * _currentPercent;
|
2019-11-23 20:27:39 +08:00
|
|
|
return _curFrameIndex;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace cocostudio
|