axmol/cocos/editor-support/cocostudio/ActionTimeline/CCActionTimeline.cpp

298 lines
6.9 KiB
C++
Raw Normal View History

2014-06-11 11:10:07 +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 "CCActionTimeline.h"
2014-06-11 11:10:07 +08:00
USING_NS_CC;
2014-06-11 11:10:07 +08:00
NS_TIMELINE_BEGIN
2014-06-11 11:10:07 +08:00
// ActionTimelineData
ActionTimelineData* ActionTimelineData::create(int actionTag)
{
ActionTimelineData * ret = new ActionTimelineData();
if (ret && ret->init(actionTag))
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
ActionTimelineData::ActionTimelineData()
: _actionTag(0)
{
}
bool ActionTimelineData::init(int actionTag)
{
_actionTag = actionTag;
return true;
}
// ActionTimeline
ActionTimeline* ActionTimeline::create()
2014-06-11 11:10:07 +08:00
{
ActionTimeline* object = new ActionTimeline();
2014-06-11 11:10:07 +08:00
if (object && object->init())
{
object->autorelease();
return object;
}
CC_SAFE_DELETE(object);
return nullptr;
}
ActionTimeline::ActionTimeline()
2014-06-11 11:10:07 +08:00
: _duration(0)
, _time(0)
, _timeSpeed(1)
, _frameInternal(1/60.0f)
, _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)
{
}
ActionTimeline::~ActionTimeline()
2014-06-11 11:10:07 +08:00
{
}
bool ActionTimeline::init()
2014-06-11 11:10:07 +08:00
{
return true;
}
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)
{
_startFrame = startIndex;
_endFrame = endIndex;
_currentFrame = currentFrameIndex;
_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;
_time = _currentFrame * _frameInternal;
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)
{
if (frameIndex >= _startFrame && frameIndex >= _endFrame)
{
_currentFrame = frameIndex;
_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();
2014-06-11 11:10:07 +08:00
newAction->setDuration(_duration);
newAction->setTimeSpeed(_timeSpeed);
for (auto timelines : _timelineMap)
{
for(auto timeline : timelines.second)
{
Timeline* newTimeline = timeline->clone();
newAction->addTimeline(newTimeline);
}
}
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;
2014-06-18 11:22:38 +08:00
_currentFrame = (int)(_time / _frameInternal);
stepToFrame(_currentFrame);
2014-06-11 11:10:07 +08:00
2014-06-17 11:45:57 +08:00
if(_time > _endFrame * _frameInternal)
2014-06-11 11:10:07 +08:00
{
_playing = _loop;
if(!_playing)
2014-06-17 11:45:57 +08:00
_time = _endFrame * _frameInternal;
2014-06-11 11:10:07 +08:00
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);
auto children = parent->getChildren();
for (auto child : children)
{
foreachNodeDescendant(child, callback);
}
}
void ActionTimeline::startWithTarget(Node *target)
2014-06-11 11:10:07 +08:00
{
Action::startWithTarget(target);
foreachNodeDescendant(target,
[this, target](Node* child)
{
ActionTimelineData* data = dynamic_cast<ActionTimelineData*>(child->getUserObject());
2014-06-12 18:50:37 +08:00
int actionTag = data->getActionTag();
2014-06-11 11:10:07 +08:00
if(_timelineMap.find(actionTag) != _timelineMap.end())
{
auto timelines = this->_timelineMap[actionTag];
for (auto timeline : timelines)
{
timeline->setNode(child);
}
}
});
}
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())
{
if(_timelineMap[tag].contains(timeline))
{
_timelineMap[tag].eraseObject(timeline);
_timelineList.eraseObject(timeline);
timeline->setActionTimeline(nullptr);
2014-06-11 11:10:07 +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;
}
void ActionTimeline::emitFrameEvent(Frame* frame)
2014-06-11 11:10:07 +08:00
{
if(_frameEventListener)
{
_frameEventListener(frame);
}
}
void ActionTimeline::gotoFrame(int frameIndex)
2014-06-11 11:10:07 +08:00
{
2014-07-09 00:09:35 +08:00
ssize_t size = _timelineList.size();
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();
for(ssize_t i = 0; i < size; i++)
2014-06-11 11:10:07 +08:00
{
_timelineList.at(i)->stepToFrame(frameIndex);
}
}
NS_TIMELINE_END