2021-12-26 23:26:34 +08:00
|
|
|
/****************************************************************************
|
2014-06-11 11:10:07 +08:00
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
2022-02-24 18:51:36 +08:00
|
|
|
https://adxeproject.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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CCTIMELINE_ACTION_H__
|
|
|
|
#define __CCTIMELINE_ACTION_H__
|
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "ActionTimeline/CCTimeLine.h"
|
|
|
|
#include "ActionTimeline/CCActionTimelineData.h"
|
2016-01-12 17:21:01 +08:00
|
|
|
#include "base/CCProtocols.h"
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "CocosStudioExport.h"
|
2014-08-29 15:39:52 +08:00
|
|
|
#include "2d/CCAction.h"
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2014-06-13 18:51:32 +08:00
|
|
|
NS_TIMELINE_BEGIN
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2015-09-10 18:29:09 +08:00
|
|
|
typedef struct AnimationInfo
|
2014-12-22 18:57:48 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
AnimationInfo() : startIndex(0), endIndex(0) {}
|
2015-09-10 18:29:09 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
AnimationInfo(std::string_view otherName, int otherStartIndex, int otherEndIndex)
|
2021-12-25 10:04:45 +08:00
|
|
|
: name(otherName), startIndex(otherStartIndex), endIndex(otherEndIndex)
|
|
|
|
{}
|
2015-09-10 18:29:09 +08:00
|
|
|
|
2014-12-22 18:57:48 +08:00
|
|
|
std::string name;
|
|
|
|
int startIndex;
|
|
|
|
int endIndex;
|
2015-09-10 18:29:09 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// need set call back before clip added to ActionTimeline
|
|
|
|
// or see @ActionTimeline::setAnimationEndCallBack
|
2015-09-10 18:29:09 +08:00
|
|
|
std::function<void()> clipEndCallBack;
|
|
|
|
} AnimationClip;
|
2014-12-22 18:57:48 +08:00
|
|
|
|
2020-10-17 16:32:16 +08:00
|
|
|
#if 0
|
|
|
|
class CCS_DLL ActionTimelineData : public cocos2d::Ref
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static ActionTimelineData* create(int actionTag);
|
|
|
|
|
|
|
|
virtual void setActionTag(int actionTag) { _actionTag = actionTag; }
|
|
|
|
virtual int getActionTag() const { return _actionTag; }
|
2022-03-18 21:46:07 +08:00
|
|
|
|
2020-10-17 16:32:16 +08:00
|
|
|
ActionTimelineData();
|
|
|
|
virtual bool init(int actionTag);
|
|
|
|
protected:
|
|
|
|
int _actionTag;
|
|
|
|
};
|
|
|
|
#endif
|
2014-06-13 13:36:01 +08:00
|
|
|
|
2020-10-17 16:32:16 +08:00
|
|
|
class CCS_DLL ActionTimeline : public cocos2d::Action, public cocos2d::PlayableProtocol
|
2014-06-11 11:10:07 +08:00
|
|
|
{
|
|
|
|
public:
|
2014-06-13 16:03:03 +08:00
|
|
|
friend class Frame;
|
|
|
|
|
|
|
|
static ActionTimeline* create();
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
ActionTimeline();
|
|
|
|
virtual ~ActionTimeline();
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2015-01-12 10:55:15 +08:00
|
|
|
virtual void play(std::string animationName, bool loop);
|
|
|
|
|
2014-06-11 11:10:07 +08:00
|
|
|
virtual bool init();
|
|
|
|
|
|
|
|
/** Goto the specified frame index, and start playing from this index.
|
|
|
|
* @param startIndex The animation will play from this index.
|
|
|
|
*/
|
|
|
|
virtual void gotoFrameAndPlay(int startIndex);
|
|
|
|
|
|
|
|
/** Goto the specified frame index, and start playing from this index.
|
|
|
|
* @param startIndex The animation will play from this index.
|
2021-12-25 10:04:45 +08:00
|
|
|
* @param loop Whether or not the animation need loop.
|
2014-06-11 11:10:07 +08:00
|
|
|
*/
|
|
|
|
virtual void gotoFrameAndPlay(int startIndex, bool loop);
|
|
|
|
|
|
|
|
/** Goto the specified frame index, and start playing from start index, end at end index.
|
|
|
|
* @param startIndex The animation will play from this index.
|
|
|
|
* @param endIndex The animation will end at this index.
|
2021-12-25 10:04:45 +08:00
|
|
|
* @param loop Whether or not the animation need loop.
|
2014-06-11 11:10:07 +08:00
|
|
|
*/
|
|
|
|
virtual void gotoFrameAndPlay(int startIndex, int endIndex, bool loop);
|
|
|
|
|
2014-06-30 15:53:12 +08:00
|
|
|
/** Goto the specified frame index, and start playing from start index, end at end index.
|
|
|
|
* @param startIndex The animation will play from this index.
|
|
|
|
* @param endIndex The animation will end at this index.
|
2021-12-25 10:04:45 +08:00
|
|
|
* @param currentFrameIndex set current frame index.
|
|
|
|
* @param loop Whether or not the animation need loop.
|
2014-06-30 15:53:12 +08:00
|
|
|
*/
|
|
|
|
virtual void gotoFrameAndPlay(int startIndex, int endIndex, int currentFrameIndex, bool loop);
|
|
|
|
|
2014-06-11 11:10:07 +08:00
|
|
|
/** Goto the specified frame index, and pause at this index.
|
|
|
|
* @param startIndex The animation will pause at this index.
|
|
|
|
*/
|
|
|
|
virtual void gotoFrameAndPause(int startIndex);
|
|
|
|
|
|
|
|
/** Pause the animation. */
|
|
|
|
virtual void pause();
|
|
|
|
/** Resume the animation. */
|
|
|
|
virtual void resume();
|
|
|
|
|
|
|
|
/** Whether or not Action is playing. */
|
2014-06-13 16:03:03 +08:00
|
|
|
virtual bool isPlaying() const;
|
2014-06-11 11:10:07 +08:00
|
|
|
|
|
|
|
/** Set the animation speed, this will speed up or slow down the speed. */
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void setTimeSpeed(float speed) { _timeSpeed = speed; }
|
2014-06-11 11:10:07 +08:00
|
|
|
/** Get current animation speed. */
|
2014-06-13 16:03:03 +08:00
|
|
|
virtual float getTimeSpeed() const { return _timeSpeed; }
|
2014-06-11 11:10:07 +08:00
|
|
|
|
|
|
|
/** duration of the whole action*/
|
|
|
|
virtual void setDuration(int duration) { _duration = duration; }
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual int getDuration() const { return _duration; }
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2014-06-17 15:57:30 +08:00
|
|
|
/** Start frame index of this action*/
|
|
|
|
virtual int getStartFrame() const { return _startFrame; }
|
|
|
|
|
2014-06-11 11:10:07 +08:00
|
|
|
/** End frame of this action.
|
2021-12-25 10:04:45 +08:00
|
|
|
* When action play to this frame, if action is not loop, then it will stop,
|
|
|
|
* or it will play from start frame again. */
|
|
|
|
virtual int getEndFrame() const { return _endFrame; }
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2014-06-30 15:53:12 +08:00
|
|
|
/** Set current frame index, this will cause action plays to this frame. */
|
|
|
|
virtual void setCurrentFrame(int frameIndex);
|
2014-06-11 11:10:07 +08:00
|
|
|
/** Get current frame. */
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual int getCurrentFrame() const { return _currentFrame; }
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
/** add Timeline to ActionTimeline */
|
2014-06-11 11:10:07 +08:00
|
|
|
virtual void addTimeline(Timeline* timeline);
|
|
|
|
virtual void removeTimeline(Timeline* timeline);
|
2015-01-12 10:55:15 +08:00
|
|
|
|
2014-06-13 18:51:32 +08:00
|
|
|
virtual const cocos2d::Vector<Timeline*>& getTimelines() const { return _timelineList; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-01-12 11:09:19 +08:00
|
|
|
/** AnimationInfo*/
|
2015-01-12 10:55:15 +08:00
|
|
|
virtual void addAnimationInfo(const AnimationInfo& animationInfo);
|
|
|
|
virtual void removeAnimationInfo(std::string animationName);
|
2021-12-26 23:26:34 +08:00
|
|
|
virtual bool IsAnimationInfoExists(std::string_view animationName);
|
|
|
|
virtual const AnimationInfo& getAnimationInfo(std::string_view animationName);
|
2015-09-20 12:34:34 +08:00
|
|
|
/**add a frame end call back to animation's end frame
|
|
|
|
* @param animationName @addFrameEndCallFunc, make the animationName as funcKey
|
|
|
|
* @param func the callback function
|
|
|
|
*/
|
2015-09-20 16:24:41 +08:00
|
|
|
virtual void setAnimationEndCallFunc(const std::string animationName, std::function<void()> func);
|
2014-06-13 16:03:03 +08:00
|
|
|
|
|
|
|
/** Set ActionTimeline's frame event callback function */
|
2021-12-25 10:04:45 +08:00
|
|
|
void setFrameEventCallFunc(std::function<void(Frame*)> listener);
|
2014-06-11 11:10:07 +08:00
|
|
|
void clearFrameEventCallFunc();
|
|
|
|
|
2014-10-23 17:38:04 +08:00
|
|
|
/** Last frame callback will call when arriving last frame */
|
|
|
|
void setLastFrameCallFunc(std::function<void()> listener);
|
|
|
|
void clearLastFrameCallFunc();
|
|
|
|
|
2015-09-20 12:34:34 +08:00
|
|
|
/** add a callback function after played frameIndex
|
|
|
|
* @param frameIndex the frame index call back after
|
|
|
|
* @param funcKey for identity the callback function
|
|
|
|
* @param func the callback function
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
virtual void addFrameEndCallFunc(int frameIndex, std::string_view funcKey, std::function<void()> func);
|
2015-09-20 12:34:34 +08:00
|
|
|
// remove callback function after frameIndex which identified with funcKey
|
2021-12-26 23:26:34 +08:00
|
|
|
virtual void removeFrameEndCallFunc(int frameIndex, std::string_view funcKey);
|
2015-09-20 12:34:34 +08:00
|
|
|
// clear callback functions after frameIndex
|
|
|
|
virtual void removeFrameEndCallFuncs(int frameIndex);
|
|
|
|
// clear all the callback functions after frameIndexs in this actiontimeline
|
|
|
|
virtual void clearFrameEndCallFuncs();
|
2015-09-10 18:29:09 +08:00
|
|
|
|
2014-06-13 13:36:01 +08:00
|
|
|
/** Inherit from Action. */
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
/** Returns a clone of ActionTimeline */
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual ActionTimeline* clone() const override;
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/** Returns a reverse of ActionTimeline.
|
2014-06-11 11:10:07 +08:00
|
|
|
* Not implement yet.
|
|
|
|
*/
|
2014-06-13 16:03:03 +08:00
|
|
|
virtual ActionTimeline* reverse() const override { return nullptr; }
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void step(float delta) override;
|
|
|
|
virtual void startWithTarget(cocos2d::Node* target) override;
|
2014-06-11 11:10:07 +08:00
|
|
|
virtual bool isDone() const override { return false; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-01-12 17:21:01 +08:00
|
|
|
/// @{
|
|
|
|
/// @name implement Playable Protocol
|
|
|
|
virtual void start() override;
|
|
|
|
virtual void stop() override;
|
2017-01-11 09:31:45 +08:00
|
|
|
/// @} end of PlayableProtocol
|
2016-01-12 17:21:01 +08:00
|
|
|
|
2014-06-11 11:10:07 +08:00
|
|
|
protected:
|
|
|
|
virtual void gotoFrame(int frameIndex);
|
|
|
|
virtual void stepToFrame(int frameIndex);
|
|
|
|
|
2015-09-10 18:29:09 +08:00
|
|
|
// emit call back after frameIndex played
|
|
|
|
virtual void emitFrameEndCallFuncs(int frameIndex);
|
|
|
|
|
2014-06-13 16:03:03 +08:00
|
|
|
/** emit frame event, call it when enter a frame*/
|
|
|
|
virtual void emitFrameEvent(Frame* frame);
|
|
|
|
|
2014-06-13 18:51:32 +08:00
|
|
|
std::map<int, cocos2d::Vector<Timeline*>> _timelineMap;
|
|
|
|
cocos2d::Vector<Timeline*> _timelineList;
|
2014-06-11 11:10:07 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int _duration;
|
|
|
|
double _time;
|
|
|
|
float _timeSpeed;
|
|
|
|
float _frameInternal;
|
|
|
|
bool _playing;
|
|
|
|
int _currentFrame;
|
|
|
|
int _startFrame;
|
|
|
|
int _endFrame;
|
|
|
|
bool _loop;
|
2014-06-11 11:10:07 +08:00
|
|
|
|
|
|
|
std::function<void(Frame*)> _frameEventListener;
|
2014-10-23 17:38:04 +08:00
|
|
|
std::function<void()> _lastFrameListener;
|
2021-12-27 13:52:08 +08:00
|
|
|
std::map<int, hlookup::string_map<std::function<void()>>> _frameEndCallFuncs;
|
|
|
|
hlookup::string_map<AnimationInfo> _animationInfos;
|
2014-06-11 11:10:07 +08:00
|
|
|
};
|
|
|
|
|
2014-06-13 18:51:32 +08:00
|
|
|
NS_TIMELINE_END
|
2014-06-11 11:10:07 +08:00
|
|
|
|
|
|
|
#endif /*__CCTIMELINE_ACTION_H__*/
|