axmol/extensions/cocostudio/ActionTimeline/CCActionTimeline.cpp

449 lines
11 KiB
C++
Raw Normal View History

2014-06-11 11:10:07 +08:00
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
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.
****************************************************************************/
#include "ActionTimeline/CCActionTimeline.h"
2014-06-11 11:10:07 +08:00
#include "CCComExtensionData.h"
2015-03-12 15:47:15 +08:00
USING_NS_CC;
2014-06-11 11:10:07 +08:00
NS_TIMELINE_BEGIN
2014-06-11 11:10:07 +08:00
2020-10-17 16:32:16 +08:00
#if 0
// ActionTimelineData
ActionTimelineData* ActionTimelineData::create(int actionTag)
{
2021-12-08 00:11:53 +08:00
ActionTimelineData * ret = new ActionTimelineData();
if (ret->init(actionTag))
2020-10-17 16:32:16 +08:00
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
ActionTimelineData::ActionTimelineData()
: _actionTag(0)
{
}
bool ActionTimelineData::init(int actionTag)
{
_actionTag = actionTag;
return true;
}
#endif
// ActionTimeline
ActionTimeline* ActionTimeline::create()
2014-06-11 11:10:07 +08:00
{
2021-12-08 00:11:53 +08:00
ActionTimeline* ret = new ActionTimeline();
if (ret->init())
2014-06-11 11:10:07 +08:00
{
2021-12-08 00:11:53 +08:00
ret->autorelease();
return ret;
2014-06-11 11:10:07 +08:00
}
2021-12-08 00:11:53 +08:00
CC_SAFE_DELETE(ret);
2014-06-11 11:10:07 +08:00
return nullptr;
}
ActionTimeline::ActionTimeline()
2014-06-11 11:10:07 +08:00
: _duration(0)
, _time(0)
, _timeSpeed(1)
2021-12-25 10:04:45 +08:00
, _frameInternal(1 / 60.0f)
2014-06-11 11:10:07 +08:00
, _playing(false)
, _currentFrame(0)
2014-06-17 11:45:57 +08:00
, _startFrame(0)
2014-06-11 11:10:07 +08:00
, _endFrame(0)
, _frameEventListener(nullptr)
2014-10-23 17:38:04 +08:00
, _lastFrameListener(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
ActionTimeline::~ActionTimeline() {}
2014-06-11 11:10:07 +08:00
bool ActionTimeline::init()
2014-06-11 11:10:07 +08:00
{
return true;
}
2014-12-22 18:57:48 +08:00
void ActionTimeline::play(std::string name, bool loop)
{
if (_animationInfos.find(name) == _animationInfos.end())
2014-12-22 18:57:48 +08:00
{
CCLOG("Can't find animation info for %s", name.c_str());
return;
2014-12-22 18:57:48 +08:00
}
AnimationInfo& index = _animationInfos[name];
gotoFrameAndPlay(index.startIndex, index.endIndex, loop);
2014-12-22 18:57:48 +08:00
}
void ActionTimeline::gotoFrameAndPlay(int startIndex)
2014-06-11 11:10:07 +08:00
{
gotoFrameAndPlay(startIndex, true);
}
void ActionTimeline::gotoFrameAndPlay(int startIndex, bool loop)
2014-06-11 11:10:07 +08:00
{
gotoFrameAndPlay(startIndex, _duration, loop);
}
void ActionTimeline::gotoFrameAndPlay(int startIndex, int endIndex, bool loop)
2014-06-11 11:10:07 +08:00
{
gotoFrameAndPlay(startIndex, endIndex, startIndex, loop);
}
void ActionTimeline::gotoFrameAndPlay(int startIndex, int endIndex, int currentFrameIndex, bool loop)
{
2021-12-25 10:04:45 +08:00
_startFrame = startIndex;
_endFrame = endIndex;
_currentFrame = currentFrameIndex;
2021-12-25 10:04:45 +08:00
_loop = loop;
_time = _currentFrame * _frameInternal;
2014-06-11 11:10:07 +08:00
resume();
gotoFrame(_currentFrame);
}
void ActionTimeline::gotoFrameAndPause(int startIndex)
2014-06-11 11:10:07 +08:00
{
2014-06-17 11:45:57 +08:00
_startFrame = _currentFrame = startIndex;
2021-12-25 10:04:45 +08:00
_time = _currentFrame * _frameInternal;
2014-06-17 11:45:57 +08:00
2014-06-11 11:10:07 +08:00
pause();
gotoFrame(_currentFrame);
}
void ActionTimeline::pause()
2014-06-11 11:10:07 +08:00
{
_playing = false;
}
void ActionTimeline::resume()
2014-06-11 11:10:07 +08:00
{
_playing = true;
}
bool ActionTimeline::isPlaying() const
2014-06-11 11:10:07 +08:00
{
return _playing;
}
void ActionTimeline::setCurrentFrame(int frameIndex)
{
2014-11-24 10:49:22 +08:00
if (frameIndex >= _startFrame && frameIndex <= _endFrame)
{
_currentFrame = frameIndex;
2021-12-25 10:04:45 +08:00
_time = _currentFrame * _frameInternal;
}
else
{
CCLOG("frame index is not between start frame and end frame");
}
}
ActionTimeline* ActionTimeline::clone() const
2014-06-11 11:10:07 +08:00
{
ActionTimeline* newAction = ActionTimeline::create();
2020-10-17 16:32:16 +08:00
newAction->setDuration(_duration);
newAction->setTimeSpeed(_timeSpeed);
2014-06-11 11:10:07 +08:00
2020-10-17 16:32:16 +08:00
for (auto timelines : _timelineMap)
2014-06-11 11:10:07 +08:00
{
2021-12-25 10:04:45 +08:00
for (auto timeline : timelines.second)
{
2014-06-11 11:10:07 +08:00
Timeline* newTimeline = timeline->clone();
newAction->addTimeline(newTimeline);
}
}
2021-12-25 10:04:45 +08:00
for (auto info : _animationInfos)
{
newAction->addAnimationInfo(info.second);
}
2014-06-11 11:10:07 +08:00
return newAction;
}
void ActionTimeline::step(float delta)
2014-06-11 11:10:07 +08:00
{
if (!_playing || _timelineMap.size() == 0 || _duration == 0)
{
return;
}
_time += delta * _timeSpeed;
float deltaCurrFrameTime = std::abs(_time - _currentFrame * _frameInternal);
if (deltaCurrFrameTime < _frameInternal)
return;
const float endtoffset = _time - _endFrame * _frameInternal;
if (endtoffset < _frameInternal)
{
_currentFrame = (int)(_time / _frameInternal);
stepToFrame(_currentFrame);
emitFrameEndCallFuncs(_currentFrame);
2021-12-25 10:04:45 +08:00
if (endtoffset >= 0 && _lastFrameListener != nullptr) // last frame
_lastFrameListener();
}
else
2014-06-11 11:10:07 +08:00
{
_playing = _loop;
if (!_playing)
{
2014-06-17 11:45:57 +08:00
_time = _endFrame * _frameInternal;
if (_currentFrame != _endFrame)
{
_currentFrame = _endFrame;
stepToFrame(_currentFrame);
emitFrameEndCallFuncs(_currentFrame);
2021-12-25 10:04:45 +08:00
if (_lastFrameListener != nullptr) // last frame
_lastFrameListener();
}
}
else
gotoFrameAndPlay(_startFrame, _endFrame, _loop);
2014-06-11 11:10:07 +08:00
}
}
typedef std::function<void(Node*)> tCallBack;
void foreachNodeDescendant(Node* parent, tCallBack callback)
{
callback(parent);
2014-12-21 01:03:30 +08:00
auto& children = parent->getChildren();
2014-06-11 11:10:07 +08:00
for (auto child : children)
{
foreachNodeDescendant(child, callback);
}
}
2021-12-25 10:04:45 +08:00
void ActionTimeline::startWithTarget(Node* target)
2014-06-11 11:10:07 +08:00
{
Action::startWithTarget(target);
2014-10-16 18:14:58 +08:00
this->setTag(target->getTag());
2014-06-11 11:10:07 +08:00
2021-12-25 10:04:45 +08:00
foreachNodeDescendant(target, [this, target](Node* child) {
ComExtensionData* data = dynamic_cast<ComExtensionData*>(child->getComponent("ComExtensionData"));
2021-12-25 10:04:45 +08:00
if (data)
2014-06-11 11:10:07 +08:00
{
int actionTag = data->getActionTag();
2021-12-25 10:04:45 +08:00
if (_timelineMap.find(actionTag) != _timelineMap.end())
2014-06-11 11:10:07 +08:00
{
auto timelines = this->_timelineMap[actionTag];
for (auto timeline : timelines)
{
timeline->setNode(child);
}
2014-06-11 11:10:07 +08:00
}
}
});
}
void ActionTimeline::addTimeline(Timeline* timeline)
2014-06-11 11:10:07 +08:00
{
int tag = timeline->getActionTag();
if (_timelineMap.find(tag) == _timelineMap.end())
{
_timelineMap[tag] = Vector<Timeline*>();
2014-06-11 11:10:07 +08:00
}
if (!_timelineMap[tag].contains(timeline))
{
_timelineList.pushBack(timeline);
_timelineMap[tag].pushBack(timeline);
timeline->setActionTimeline(this);
2014-06-11 11:10:07 +08:00
}
}
void ActionTimeline::removeTimeline(Timeline* timeline)
2014-06-11 11:10:07 +08:00
{
int tag = timeline->getActionTag();
if (_timelineMap.find(tag) != _timelineMap.end())
{
2021-12-25 10:04:45 +08:00
if (_timelineMap[tag].contains(timeline))
2014-06-11 11:10:07 +08:00
{
_timelineMap[tag].eraseObject(timeline);
_timelineList.eraseObject(timeline);
timeline->setActionTimeline(nullptr);
2014-06-11 11:10:07 +08:00
}
}
}
void ActionTimeline::addAnimationInfo(const AnimationInfo& animationInfo)
2014-12-22 18:57:48 +08:00
{
if (_animationInfos.find(animationInfo.name) != _animationInfos.end())
2014-12-22 18:57:48 +08:00
{
CCLOG("Animation (%s) already exists.", animationInfo.name.c_str());
2014-12-22 18:57:48 +08:00
return;
}
_animationInfos[animationInfo.name] = animationInfo;
addFrameEndCallFunc(animationInfo.endIndex, animationInfo.name, animationInfo.clipEndCallBack);
2014-12-22 18:57:48 +08:00
}
void ActionTimeline::removeAnimationInfo(std::string animationName)
2014-12-22 18:57:48 +08:00
{
auto clipIter = _animationInfos.find(animationName);
if (clipIter == _animationInfos.end())
2014-12-22 18:57:48 +08:00
{
CCLOG("AnimationInfo (%s) not exists.", animationName.c_str());
2014-12-22 18:57:48 +08:00
return;
}
removeFrameEndCallFunc((*clipIter).second.endIndex, animationName);
_animationInfos.erase(animationName);
}
2021-12-26 23:26:34 +08:00
bool ActionTimeline::IsAnimationInfoExists(std::string_view animationName)
{
return _animationInfos.find(animationName) != _animationInfos.end();
}
2021-12-26 23:26:34 +08:00
const AnimationInfo& ActionTimeline::getAnimationInfo(std::string_view animationName)
{
return _animationInfos.find(animationName)->second;
2014-12-22 18:57:48 +08:00
}
2015-09-20 16:24:41 +08:00
void ActionTimeline::setAnimationEndCallFunc(const std::string animationName, std::function<void()> func)
{
auto clipIter = _animationInfos.find(animationName);
if (clipIter == _animationInfos.end())
{
CCLOG("AnimationInfo (%s) not exists.", animationName.c_str());
return;
}
clipIter->second.clipEndCallBack = func;
addFrameEndCallFunc(clipIter->second.endIndex, animationName, func);
}
2021-12-25 10:04:45 +08:00
void ActionTimeline::setFrameEventCallFunc(std::function<void(Frame*)> listener)
2014-06-11 11:10:07 +08:00
{
_frameEventListener = listener;
}
void ActionTimeline::clearFrameEventCallFunc()
2014-06-11 11:10:07 +08:00
{
_frameEventListener = nullptr;
}
2014-10-23 17:38:04 +08:00
void ActionTimeline::setLastFrameCallFunc(std::function<void()> listener)
{
_lastFrameListener = listener;
}
void ActionTimeline::clearLastFrameCallFunc()
{
_lastFrameListener = nullptr;
}
2014-06-11 11:10:07 +08:00
void ActionTimeline::emitFrameEvent(Frame* frame)
2014-06-11 11:10:07 +08:00
{
2021-12-25 10:04:45 +08:00
if (_frameEventListener)
2014-06-11 11:10:07 +08:00
{
_frameEventListener(frame);
}
}
2021-12-26 23:26:34 +08:00
void ActionTimeline::addFrameEndCallFunc(int frameIndex, std::string_view funcKey, std::function<void()> func)
{
if (func != nullptr)
{
_frameEndCallFuncs[frameIndex][funcKey] = func;
}
}
2021-12-26 23:26:34 +08:00
void ActionTimeline::removeFrameEndCallFunc(int frameIndex, std::string_view funcKey)
{
auto endClipCallsIter = _frameEndCallFuncs.find(frameIndex);
if (endClipCallsIter != _frameEndCallFuncs.end())
{
auto funcIter = (*endClipCallsIter).second.find(funcKey);
if (funcIter != (*endClipCallsIter).second.end())
(*endClipCallsIter).second.erase(funcKey);
if ((*endClipCallsIter).second.empty())
_frameEndCallFuncs.erase(endClipCallsIter);
}
}
void ActionTimeline::removeFrameEndCallFuncs(int frameIndex)
{
auto endClipCallsIter = _frameEndCallFuncs.find(frameIndex);
if (endClipCallsIter != _frameEndCallFuncs.end())
{
_frameEndCallFuncs.erase(endClipCallsIter);
}
}
void ActionTimeline::clearFrameEndCallFuncs()
{
_frameEndCallFuncs.clear();
}
void ActionTimeline::emitFrameEndCallFuncs(int frameIndex)
{
auto clipEndCallsIter = _frameEndCallFuncs.find(frameIndex);
if (clipEndCallsIter != _frameEndCallFuncs.end())
{
auto clipEndCalls = (*clipEndCallsIter).second;
2020-10-17 16:32:16 +08:00
for (auto call : clipEndCalls)
(call).second();
}
}
void ActionTimeline::gotoFrame(int frameIndex)
2014-06-11 11:10:07 +08:00
{
2021-12-25 10:04:45 +08:00
if (_target == nullptr)
2014-10-23 17:38:04 +08:00
return;
2014-07-09 00:09:35 +08:00
ssize_t size = _timelineList.size();
2021-12-25 10:04:45 +08:00
for (ssize_t i = 0; i < size; i++)
{
2014-06-11 11:10:07 +08:00
_timelineList.at(i)->gotoFrame(frameIndex);
}
}
void ActionTimeline::stepToFrame(int frameIndex)
2014-06-11 11:10:07 +08:00
{
2014-07-09 00:09:35 +08:00
ssize_t size = _timelineList.size();
2021-12-25 10:04:45 +08:00
for (ssize_t i = 0; i < size; i++)
{
2014-06-11 11:10:07 +08:00
_timelineList.at(i)->stepToFrame(frameIndex);
}
}
2016-01-12 17:21:01 +08:00
void ActionTimeline::start()
{
gotoFrameAndPlay(0);
}
void ActionTimeline::stop()
{
pause();
}
NS_TIMELINE_END