2014-06-11 11:10:07 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
2022-07-09 22:23:34 +08:00
|
|
|
https://axis-project.github.io/
|
2014-06-11 11:10:07 +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 "ActionTimeline/CCTimeLine.h"
|
|
|
|
#include "ActionTimeline/CCActionTimeline.h"
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2014-06-13 18:51:32 +08:00
|
|
|
|
|
|
|
NS_TIMELINE_BEGIN
|
2014-06-11 11:10:07 +08:00
|
|
|
|
|
|
|
Timeline* Timeline::create()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
Timeline* ret = new Timeline();
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2014-06-11 11:10:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Timeline::Timeline()
|
|
|
|
: _currentKeyFrame(nullptr)
|
|
|
|
, _currentKeyFrameIndex(0)
|
|
|
|
, _fromIndex(0)
|
|
|
|
, _toIndex(0)
|
|
|
|
, _betweenDuration(0)
|
|
|
|
, _actionTag(0)
|
2014-06-13 16:03:03 +08:00
|
|
|
, _ActionTimeline(nullptr)
|
2014-06-11 11:10:07 +08:00
|
|
|
, _node(nullptr)
|
2021-12-25 10:04:45 +08:00
|
|
|
{}
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Timeline::~Timeline() {}
|
2014-06-11 11:10:07 +08:00
|
|
|
|
|
|
|
void Timeline::gotoFrame(int frameIndex)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_frames.size() == 0)
|
2014-06-11 11:10:07 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
binarySearchKeyFrame(frameIndex);
|
|
|
|
apply(frameIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timeline::stepToFrame(int frameIndex)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_frames.size() == 0)
|
2014-06-11 11:10:07 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
updateCurrentKeyFrame(frameIndex);
|
|
|
|
apply(frameIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
Timeline* Timeline::clone()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Timeline* timeline = Timeline::create();
|
2014-06-11 11:10:07 +08:00
|
|
|
timeline->_actionTag = _actionTag;
|
|
|
|
|
|
|
|
for (auto frame : _frames)
|
|
|
|
{
|
|
|
|
Frame* newFrame = frame->clone();
|
2014-06-13 16:03:03 +08:00
|
|
|
timeline->addFrame(newFrame);
|
2014-06-11 11:10:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return timeline;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timeline::addFrame(Frame* frame)
|
|
|
|
{
|
|
|
|
_frames.pushBack(frame);
|
|
|
|
frame->setTimeline(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timeline::insertFrame(Frame* frame, int index)
|
|
|
|
{
|
|
|
|
_frames.insert(index, frame);
|
|
|
|
frame->setTimeline(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timeline::removeFrame(Frame* frame)
|
|
|
|
{
|
|
|
|
_frames.eraseObject(frame);
|
2014-07-10 00:45:27 +08:00
|
|
|
frame->setTimeline(nullptr);
|
2014-06-11 11:10:07 +08:00
|
|
|
}
|
|
|
|
|
2014-06-13 13:36:01 +08:00
|
|
|
void Timeline::setNode(Node* node)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
for (auto frame : _frames)
|
|
|
|
{
|
|
|
|
frame->setNode(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
Node* Timeline::getNode() const
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
return _node;
|
|
|
|
}
|
|
|
|
|
2015-07-07 23:01:24 +08:00
|
|
|
void Timeline::apply(unsigned int frameIndex)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
if (_currentKeyFrame)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
float currentPercent =
|
|
|
|
_betweenDuration == 0 ? 0 : (frameIndex - _currentKeyFrameIndex) / (float)_betweenDuration;
|
2014-06-11 11:10:07 +08:00
|
|
|
_currentKeyFrame->apply(currentPercent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 23:01:24 +08:00
|
|
|
void Timeline::binarySearchKeyFrame(unsigned int frameIndex)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Frame* from = nullptr;
|
|
|
|
Frame* to = nullptr;
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
long length = _frames.size();
|
2014-06-11 11:10:07 +08:00
|
|
|
bool needEnterFrame = false;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
if (frameIndex < _frames.at(0)->getFrameIndex())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_currentKeyFrameIndex >= _frames.at(0)->getFrameIndex())
|
2014-06-11 11:10:07 +08:00
|
|
|
needEnterFrame = true;
|
|
|
|
|
2014-10-19 21:32:23 +08:00
|
|
|
_fromIndex = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
_toIndex = 0;
|
|
|
|
|
|
|
|
from = to = _frames.at(0);
|
2014-06-11 11:10:07 +08:00
|
|
|
_currentKeyFrameIndex = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
_betweenDuration = _frames.at(0)->getFrameIndex();
|
2014-06-11 11:10:07 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (frameIndex >= _frames.at(length - 1)->getFrameIndex())
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
2014-10-19 21:32:23 +08:00
|
|
|
_fromIndex = (int)(length - 1);
|
2021-12-25 10:04:45 +08:00
|
|
|
_toIndex = 0;
|
|
|
|
|
|
|
|
from = to = _frames.at(length - 1);
|
2016-03-11 12:56:58 +08:00
|
|
|
if (from->isEnterWhenPassed())
|
|
|
|
needEnterFrame = true;
|
|
|
|
|
2014-06-11 11:10:07 +08:00
|
|
|
_currentKeyFrameIndex = _frames.at(length - 1)->getFrameIndex();
|
2021-12-25 10:04:45 +08:00
|
|
|
_betweenDuration = 0;
|
2014-06-11 11:10:07 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-07-15 18:07:33 +08:00
|
|
|
long target = -1;
|
2021-12-25 10:04:45 +08:00
|
|
|
long low = 0, high = length - 1, mid = 0;
|
|
|
|
while (low <= high)
|
|
|
|
{
|
|
|
|
mid = (low + high) / 2;
|
|
|
|
if (frameIndex >= _frames.at(mid)->getFrameIndex() && frameIndex < _frames.at(mid + 1)->getFrameIndex())
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
target = mid;
|
|
|
|
break;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_frames.at(mid)->getFrameIndex() > frameIndex)
|
|
|
|
high = mid - 1;
|
2014-06-11 11:10:07 +08:00
|
|
|
else
|
2021-12-25 10:04:45 +08:00
|
|
|
low = mid + 1;
|
2014-06-11 11:10:07 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-10-19 21:32:23 +08:00
|
|
|
_fromIndex = (int)target;
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (length > 1)
|
2014-10-19 21:32:23 +08:00
|
|
|
_toIndex = (int)(target + 1);
|
|
|
|
else
|
|
|
|
_toIndex = (int)target;
|
|
|
|
|
|
|
|
from = _frames.at(_fromIndex);
|
|
|
|
to = _frames.at(_toIndex);
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (target == 0 && _currentKeyFrameIndex < from->getFrameIndex())
|
2014-06-11 11:10:07 +08:00
|
|
|
needEnterFrame = true;
|
|
|
|
|
|
|
|
_currentKeyFrameIndex = from->getFrameIndex();
|
2021-12-25 10:04:45 +08:00
|
|
|
_betweenDuration = to->getFrameIndex() - from->getFrameIndex();
|
2014-06-11 11:10:07 +08:00
|
|
|
} while (0);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (needEnterFrame || _currentKeyFrame != from)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
_currentKeyFrame = from;
|
2014-10-23 17:37:18 +08:00
|
|
|
_currentKeyFrame->onEnter(to, frameIndex);
|
2014-06-11 11:10:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 23:01:24 +08:00
|
|
|
void Timeline::updateCurrentKeyFrame(unsigned int frameIndex)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
//! If play to current frame's front or back, then find current frame again
|
|
|
|
if (frameIndex < _currentKeyFrameIndex || frameIndex >= _currentKeyFrameIndex + _betweenDuration)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Frame* from = nullptr;
|
|
|
|
Frame* to = nullptr;
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
long length = _frames.size();
|
|
|
|
|
|
|
|
if (frameIndex < _frames.at(0)->getFrameIndex())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
from = to = _frames.at(0);
|
2014-06-11 11:10:07 +08:00
|
|
|
_currentKeyFrameIndex = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
_betweenDuration = _frames.at(0)->getFrameIndex();
|
2014-06-11 11:10:07 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (frameIndex >= _frames.at(length - 1)->getFrameIndex())
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
unsigned int lastFrameIndex = _frames.at(length - 1)->getFrameIndex();
|
|
|
|
if (_currentKeyFrameIndex >= lastFrameIndex)
|
2014-10-17 20:43:01 +08:00
|
|
|
return;
|
2014-10-19 21:32:23 +08:00
|
|
|
frameIndex = lastFrameIndex;
|
2014-06-11 11:10:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_fromIndex = _toIndex;
|
|
|
|
from = _frames.at(_fromIndex);
|
|
|
|
_currentKeyFrameIndex = from->getFrameIndex();
|
2014-06-11 11:10:07 +08:00
|
|
|
|
|
|
|
_toIndex = _fromIndex + 1;
|
2015-07-07 23:01:24 +08:00
|
|
|
if ((ssize_t)_toIndex >= length)
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
_toIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
to = _frames.at(_toIndex);
|
2014-10-19 21:32:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (frameIndex == from->getFrameIndex())
|
2014-06-11 11:10:07 +08:00
|
|
|
break;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (frameIndex > from->getFrameIndex() && frameIndex < to->getFrameIndex())
|
2014-10-17 20:43:01 +08:00
|
|
|
break;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (from->isEnterWhenPassed())
|
2014-10-23 17:37:18 +08:00
|
|
|
from->onEnter(to, from->getFrameIndex());
|
2021-12-25 10:04:45 +08:00
|
|
|
} while (true);
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_fromIndex == length - 1)
|
2014-10-17 20:43:01 +08:00
|
|
|
to = from;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-06-11 11:10:07 +08:00
|
|
|
_betweenDuration = to->getFrameIndex() - from->getFrameIndex();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-06-11 11:10:07 +08:00
|
|
|
} while (0);
|
|
|
|
|
|
|
|
_currentKeyFrame = from;
|
2014-10-23 17:37:18 +08:00
|
|
|
_currentKeyFrame->onEnter(to, frameIndex);
|
2014-06-11 11:10:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-13 18:51:32 +08:00
|
|
|
NS_TIMELINE_END
|