axmol/extensions/cocostudio/ActionTimeline/CCActionTimeline.cpp

449 lines
11 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +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 "ActionTimeline/CCActionTimeline.h"
2019-11-23 20:27:39 +08:00
#include "CCComExtensionData.h"
2019-11-23 20:27:39 +08:00
USING_NS_CC;
NS_TIMELINE_BEGIN
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
2019-11-23 20:27:39 +08:00
// ActionTimeline
ActionTimeline* ActionTimeline::create()
{
2021-12-08 00:11:53 +08:00
ActionTimeline* ret = new ActionTimeline();
if (ret->init())
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
ret->autorelease();
return ret;
2019-11-23 20:27:39 +08:00
}
2021-12-08 00:11:53 +08:00
CC_SAFE_DELETE(ret);
2019-11-23 20:27:39 +08:00
return nullptr;
}
ActionTimeline::ActionTimeline()
: _duration(0)
, _time(0)
, _timeSpeed(1)
2021-12-25 10:04:45 +08:00
, _frameInternal(1 / 60.0f)
2019-11-23 20:27:39 +08:00
, _playing(false)
, _currentFrame(0)
, _startFrame(0)
, _endFrame(0)
, _frameEventListener(nullptr)
, _lastFrameListener(nullptr)
2021-12-25 10:04:45 +08:00
{}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
ActionTimeline::~ActionTimeline() {}
2019-11-23 20:27:39 +08:00
bool ActionTimeline::init()
{
return true;
}
void ActionTimeline::play(std::string name, bool loop)
{
if (_animationInfos.find(name) == _animationInfos.end())
{
CCLOG("Can't find animation info for %s", name.c_str());
return;
}
AnimationInfo& index = _animationInfos[name];
gotoFrameAndPlay(index.startIndex, index.endIndex, loop);
}
void ActionTimeline::gotoFrameAndPlay(int startIndex)
{
gotoFrameAndPlay(startIndex, true);
}
void ActionTimeline::gotoFrameAndPlay(int startIndex, bool loop)
{
gotoFrameAndPlay(startIndex, _duration, loop);
}
void ActionTimeline::gotoFrameAndPlay(int startIndex, int endIndex, bool loop)
{
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;
2019-11-23 20:27:39 +08:00
_currentFrame = currentFrameIndex;
2021-12-25 10:04:45 +08:00
_loop = loop;
_time = _currentFrame * _frameInternal;
2019-11-23 20:27:39 +08:00
resume();
gotoFrame(_currentFrame);
}
void ActionTimeline::gotoFrameAndPause(int startIndex)
{
_startFrame = _currentFrame = startIndex;
2021-12-25 10:04:45 +08:00
_time = _currentFrame * _frameInternal;
2019-11-23 20:27:39 +08:00
pause();
gotoFrame(_currentFrame);
}
void ActionTimeline::pause()
{
_playing = false;
}
void ActionTimeline::resume()
{
_playing = true;
}
bool ActionTimeline::isPlaying() const
{
return _playing;
}
void ActionTimeline::setCurrentFrame(int frameIndex)
{
if (frameIndex >= _startFrame && frameIndex <= _endFrame)
{
_currentFrame = frameIndex;
2021-12-25 10:04:45 +08:00
_time = _currentFrame * _frameInternal;
2019-11-23 20:27:39 +08:00
}
else
{
CCLOG("frame index is not between start frame and end frame");
}
}
ActionTimeline* ActionTimeline::clone() const
{
ActionTimeline* newAction = ActionTimeline::create();
2020-10-17 16:32:16 +08:00
newAction->setDuration(_duration);
newAction->setTimeSpeed(_timeSpeed);
2019-11-23 20:27:39 +08:00
2020-10-17 16:32:16 +08:00
for (auto timelines : _timelineMap)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
for (auto timeline : timelines.second)
{
2019-11-23 20:27:39 +08:00
Timeline* newTimeline = timeline->clone();
newAction->addTimeline(newTimeline);
}
}
2021-12-25 10:04:45 +08:00
for (auto info : _animationInfos)
2019-11-23 20:27:39 +08:00
{
newAction->addAnimationInfo(info.second);
}
return newAction;
}
void ActionTimeline::step(float delta)
{
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
2019-11-23 20:27:39 +08:00
_lastFrameListener();
}
else
{
_playing = _loop;
if (!_playing)
{
_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
2019-11-23 20:27:39 +08:00
_lastFrameListener();
}
}
else
gotoFrameAndPlay(_startFrame, _endFrame, _loop);
}
}
typedef std::function<void(Node*)> tCallBack;
void foreachNodeDescendant(Node* parent, tCallBack callback)
{
callback(parent);
auto& children = parent->getChildren();
for (auto child : children)
{
foreachNodeDescendant(child, callback);
}
}
2021-12-25 10:04:45 +08:00
void ActionTimeline::startWithTarget(Node* target)
2019-11-23 20:27:39 +08:00
{
Action::startWithTarget(target);
this->setTag(target->getTag());
2021-12-25 10:04:45 +08:00
foreachNodeDescendant(target, [this, target](Node* child) {
2019-11-23 20:27:39 +08:00
ComExtensionData* data = dynamic_cast<ComExtensionData*>(child->getComponent("ComExtensionData"));
2021-12-25 10:04:45 +08:00
if (data)
2019-11-23 20:27:39 +08:00
{
int actionTag = data->getActionTag();
2021-12-25 10:04:45 +08:00
if (_timelineMap.find(actionTag) != _timelineMap.end())
2019-11-23 20:27:39 +08:00
{
auto timelines = this->_timelineMap[actionTag];
for (auto timeline : timelines)
{
timeline->setNode(child);
}
}
}
});
}
void ActionTimeline::addTimeline(Timeline* timeline)
{
int tag = timeline->getActionTag();
if (_timelineMap.find(tag) == _timelineMap.end())
{
_timelineMap[tag] = Vector<Timeline*>();
}
if (!_timelineMap[tag].contains(timeline))
{
_timelineList.pushBack(timeline);
_timelineMap[tag].pushBack(timeline);
timeline->setActionTimeline(this);
}
}
void ActionTimeline::removeTimeline(Timeline* timeline)
{
int tag = timeline->getActionTag();
if (_timelineMap.find(tag) != _timelineMap.end())
{
2021-12-25 10:04:45 +08:00
if (_timelineMap[tag].contains(timeline))
2019-11-23 20:27:39 +08:00
{
_timelineMap[tag].eraseObject(timeline);
_timelineList.eraseObject(timeline);
timeline->setActionTimeline(nullptr);
}
}
}
void ActionTimeline::addAnimationInfo(const AnimationInfo& animationInfo)
{
if (_animationInfos.find(animationInfo.name) != _animationInfos.end())
{
CCLOG("Animation (%s) already exists.", animationInfo.name.c_str());
return;
}
_animationInfos[animationInfo.name] = animationInfo;
addFrameEndCallFunc(animationInfo.endIndex, animationInfo.name, animationInfo.clipEndCallBack);
}
void ActionTimeline::removeAnimationInfo(std::string animationName)
{
auto clipIter = _animationInfos.find(animationName);
if (clipIter == _animationInfos.end())
{
CCLOG("AnimationInfo (%s) not exists.", animationName.c_str());
return;
}
removeFrameEndCallFunc((*clipIter).second.endIndex, animationName);
_animationInfos.erase(animationName);
}
2021-12-26 23:26:34 +08:00
bool ActionTimeline::IsAnimationInfoExists(std::string_view animationName)
2019-11-23 20:27:39 +08:00
{
return _animationInfos.find(animationName) != _animationInfos.end();
}
2021-12-26 23:26:34 +08:00
const AnimationInfo& ActionTimeline::getAnimationInfo(std::string_view animationName)
2019-11-23 20:27:39 +08:00
{
return _animationInfos.find(animationName)->second;
}
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)
2019-11-23 20:27:39 +08:00
{
_frameEventListener = listener;
}
void ActionTimeline::clearFrameEventCallFunc()
{
_frameEventListener = nullptr;
}
void ActionTimeline::setLastFrameCallFunc(std::function<void()> listener)
{
_lastFrameListener = listener;
}
void ActionTimeline::clearLastFrameCallFunc()
{
_lastFrameListener = nullptr;
}
void ActionTimeline::emitFrameEvent(Frame* frame)
{
2021-12-25 10:04:45 +08:00
if (_frameEventListener)
2019-11-23 20:27:39 +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)
2019-11-23 20:27:39 +08:00
{
if (func != nullptr)
{
_frameEndCallFuncs[frameIndex][funcKey] = func;
}
}
2021-12-26 23:26:34 +08:00
void ActionTimeline::removeFrameEndCallFunc(int frameIndex, std::string_view funcKey)
2019-11-23 20:27:39 +08:00
{
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)
2019-11-23 20:27:39 +08:00
(call).second();
}
}
void ActionTimeline::gotoFrame(int frameIndex)
{
2021-12-25 10:04:45 +08:00
if (_target == nullptr)
2019-11-23 20:27:39 +08:00
return;
ssize_t size = _timelineList.size();
2021-12-25 10:04:45 +08:00
for (ssize_t i = 0; i < size; i++)
{
2019-11-23 20:27:39 +08:00
_timelineList.at(i)->gotoFrame(frameIndex);
}
}
void ActionTimeline::stepToFrame(int frameIndex)
{
ssize_t size = _timelineList.size();
2021-12-25 10:04:45 +08:00
for (ssize_t i = 0; i < size; i++)
{
2019-11-23 20:27:39 +08:00
_timelineList.at(i)->stepToFrame(frameIndex);
}
}
void ActionTimeline::start()
{
gotoFrameAndPlay(0);
}
void ActionTimeline::stop()
{
pause();
}
NS_TIMELINE_END