axmol/extensions/cocostudio/CCArmatureAnimation.h

331 lines
10 KiB
C
Raw Normal View History

2015-06-17 13:16:06 +08:00
/****************************************************************************
2020-10-17 16:32:16 +08:00
Copyright (c) 2013-2017 Chukong Technologies Inc.
2013-06-06 12:02:54 +08:00
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.
****************************************************************************/
#ifndef __CCANIMATION_H__
#define __CCANIMATION_H__
#include "CCProcessBase.h"
#include "CCTween.h"
#include "CocosStudioExport.h"
2013-10-30 09:41:40 +08:00
#include <queue>
2013-06-06 12:02:54 +08:00
namespace cocostudio {
2013-06-06 12:02:54 +08:00
2013-06-07 16:13:08 +08:00
enum MovementEventType
{
2013-09-13 18:07:37 +08:00
START,
COMPLETE,
LOOP_COMPLETE
2013-06-07 16:13:08 +08:00
};
class Armature;
class Bone;
2013-06-06 12:02:54 +08:00
typedef void (cocos2d::Ref::*SEL_MovementEventCallFunc)(Armature *, MovementEventType, const std::string&);
typedef void (cocos2d::Ref::*SEL_FrameEventCallFunc)(Bone *, const std::string&, int, int);
2013-09-13 18:07:37 +08:00
#define movementEvent_selector(_SELECTOR) (cocostudio::SEL_MovementEventCallFunc)(&_SELECTOR)
#define frameEvent_selector(_SELECTOR) (cocostudio::SEL_FrameEventCallFunc)(&_SELECTOR)
2013-09-13 18:07:37 +08:00
2013-10-30 09:41:40 +08:00
struct FrameEvent
{
Bone *bone;
std::string frameEventName;
2013-10-30 09:41:40 +08:00
int originFrameIndex;
int currentFrameIndex;
};
2013-09-13 18:07:37 +08:00
struct MovementEvent
{
Armature *armature;
MovementEventType movementType;
std::string movementID;
};
2020-10-17 16:32:16 +08:00
class CCS_DLL ArmatureAnimation : public ProcessBase
2013-06-06 12:02:54 +08:00
{
public:
/**
* Create with a Armature
* @param armature The Armature ArmatureAnimation will bind to
2013-06-06 12:02:54 +08:00
*/
static ArmatureAnimation *create(Armature *armature);
2013-06-06 12:02:54 +08:00
public:
2015-06-17 13:16:06 +08:00
/**
2013-09-16 14:13:55 +08:00
* @js ctor
*/
ArmatureAnimation();
2013-09-16 14:13:55 +08:00
/**
* @js NA
* @lua NA
*/
2020-10-17 16:32:16 +08:00
virtual ~ArmatureAnimation(void);
2013-06-06 12:02:54 +08:00
/**
* Init with a Armature
* @param armature The Armature ArmatureAnimation will bind to
2013-06-06 12:02:54 +08:00
*/
virtual bool init(Armature *armature);
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
/**
2013-06-07 10:52:32 +08:00
* Scale animation play speed.
2013-10-30 09:41:40 +08:00
* This method is deprecated, please use setSpeedScale.
2013-06-06 12:02:54 +08:00
* @param animationScale Scale value
*/
2013-10-30 09:41:40 +08:00
CC_DEPRECATED_ATTRIBUTE virtual void setAnimationScale(float animationScale);
CC_DEPRECATED_ATTRIBUTE virtual float getAnimationScale() const;
2013-09-13 18:07:37 +08:00
/**
* Scale animation play speed.
* @param animationScale Scale value
*/
virtual void setSpeedScale(float speedScale);
virtual float getSpeedScale() const;
//! The animation update speed
CC_DEPRECATED_ATTRIBUTE virtual void setAnimationInternal(float animationInternal) {}
2013-06-07 10:52:32 +08:00
using ProcessBase::play;
2013-06-07 10:52:32 +08:00
/**
* Play animation by animation name.
2013-06-06 12:02:54 +08:00
*
* @param animationName The animation name you want to play
* @param durationTo The frames between two animation changing-over.
* It's meaning is changing to this animation need how many frames
*
* -1 : use the value from MovementData get from flash design panel
2013-06-06 12:02:54 +08:00
* @param loop Whether the animation is loop
*
* loop < 0 : use the value from MovementData get from flash design panel
2013-06-06 12:02:54 +08:00
* loop = 0 : this animation is not loop
* loop > 0 : this animation is loop
*/
virtual void play(const std::string& animationName, int durationTo = -1, int loop = -1);
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
/**
2013-06-07 10:52:32 +08:00
* Play animation by index, the other param is the same to play.
2013-12-26 16:48:19 +08:00
* @deprecated, please use playWithIndex
2013-10-30 09:41:40 +08:00
* @param animationIndex the animation index you want to play
2013-06-06 12:02:54 +08:00
*/
2013-12-25 16:46:31 +08:00
CC_DEPRECATED_ATTRIBUTE virtual void playByIndex(int animationIndex, int durationTo = -1, int loop = -1);
virtual void playWithIndex(int animationIndex, int durationTo = -1, int loop = -1);
2013-12-25 16:46:31 +08:00
virtual void playWithNames(const std::vector<std::string>& movementNames, int durationTo = -1, bool loop = true);
virtual void playWithIndexes(const std::vector<int>& movementIndexes, int durationTo = -1, bool loop = true);
2013-06-07 10:52:32 +08:00
2013-10-30 09:41:40 +08:00
/**
* Go to specified frame and play current movement.
* You need first switch to the movement you want to play, then call this function.
*
* example : playByIndex(0);
* gotoAndPlay(0);
* playByIndex(1);
* gotoAndPlay(0);
* gotoAndPlay(15);
*/
virtual void gotoAndPlay(int frameIndex);
/**
* Go to specified frame and pause current movement.
*/
virtual void gotoAndPause(int frameIndex);
2013-06-06 12:02:54 +08:00
/**
2013-06-07 10:52:32 +08:00
* Pause the Process
2013-06-06 12:02:54 +08:00
*/
virtual void pause() override;
2013-06-06 12:02:54 +08:00
/**
2013-06-07 10:52:32 +08:00
* Resume the Process
2013-06-06 12:02:54 +08:00
*/
virtual void resume() override;
2013-06-06 12:02:54 +08:00
/**
2013-06-07 10:52:32 +08:00
* Stop the Process
2013-06-06 12:02:54 +08:00
*/
virtual void stop() override;
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
/**
2013-06-07 10:52:32 +08:00
* Get movement count
2013-06-06 12:02:54 +08:00
*/
2014-01-07 18:26:31 +08:00
ssize_t getMovementCount() const;
2013-06-07 10:52:32 +08:00
2015-03-16 23:39:43 +08:00
virtual void update(float dt) override;
2013-09-13 18:07:37 +08:00
/**
* Get current movementID
* @return The name of current movement
*/
2013-11-05 19:53:38 +08:00
std::string getCurrentMovementID() const;
2013-09-13 18:07:37 +08:00
/**
* Set armature's movement event callback function
* To disconnect this event, just setMovementEventCallFunc(nullptr, nullptr);
2013-09-13 18:07:37 +08:00
*/
CC_DEPRECATED_ATTRIBUTE void setMovementEventCallFunc(cocos2d::Ref *target, SEL_MovementEventCallFunc callFunc);
2013-09-13 18:07:37 +08:00
/**
* Set armature's frame event callback function
* To disconnect this event, just setFrameEventCallFunc(nullptr, nullptr);
2013-09-13 18:07:37 +08:00
*/
CC_DEPRECATED_ATTRIBUTE void setFrameEventCallFunc(cocos2d::Ref *target, SEL_FrameEventCallFunc callFunc);
2013-12-18 22:07:33 +08:00
void setMovementEventCallFunc(std::function<void(Armature *armature, MovementEventType movementType, const std::string& movementID)> listener);
void setFrameEventCallFunc(std::function<void(Bone *bone, const std::string& frameEventName, int originFrameIndex, int currentFrameIndex)> listener);
2013-09-13 18:07:37 +08:00
2013-11-05 19:53:38 +08:00
virtual void setAnimationData(AnimationData *data)
{
if (_animationData != data)
{
CC_SAFE_RETAIN(data);
CC_SAFE_RELEASE(_animationData);
_animationData = data;
}
}
virtual AnimationData *getAnimationData() const { return _animationData; }
/**
* Returns a user assigned Object
*
* Similar to userData, but instead of holding a void* it holds an object
*
* @return A user assigned Object
* @js NA
* @lua NA
*/
virtual Ref* getUserObject() { return _userObject; }
/**
* @js NA
* @lua NA
*/
virtual const Ref* getUserObject() const { return _userObject; }
/**
* Returns a user assigned Object
*
* Similar to UserData, but instead of holding a void* it holds an object.
* The UserObject will be retained once in this method,
* and the previous UserObject (if existed) will be release.
* The UserObject will be released in Node's destructor.
*
* @param userObject A user assigned Object
*/
virtual void setUserObject(Ref *userObject);
2013-06-06 12:02:54 +08:00
protected:
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
/**
2013-06-07 10:52:32 +08:00
* Update(float dt) will call this handler, you can handle your logic here
* @js NA
* @lua NA
2013-06-06 12:02:54 +08:00
*/
void updateHandler() override;
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
/**
2013-06-07 10:52:32 +08:00
* Update current key frame, and process auto stop, pause
* @js NA
* @lua NA
2013-06-06 12:02:54 +08:00
*/
2013-06-07 10:52:32 +08:00
void updateFrameData(float currentPercent);
2013-09-13 18:07:37 +08:00
/**
* Emit a frame event
* @js NA
* @lua NA
2013-09-13 18:07:37 +08:00
*/
void frameEvent(Bone *bone, const std::string& frameEventName, int originFrameIndex, int currentFrameIndex);
2013-09-13 18:07:37 +08:00
/**
* Emit a movement event
*/
void movementEvent(Armature *armature, MovementEventType movementType, const std::string& movementID);
void updateMovementList();
2013-11-05 19:53:38 +08:00
bool isIgnoreFrameEvent() const { return _ignoreFrameEvent; }
2013-10-30 09:41:40 +08:00
friend class Tween;
2013-06-06 12:02:54 +08:00
protected:
//! AnimationData save all MovementDatas this animation used.
2013-11-05 19:53:38 +08:00
AnimationData *_animationData;
2013-06-07 10:52:32 +08:00
2013-09-13 18:07:37 +08:00
//! Scale the animation speed
2013-09-15 20:24:25 +08:00
float _speedScale;
2013-06-06 12:02:54 +08:00
2015-06-17 13:16:06 +08:00
MovementData *_movementData; //! MovementData save all MovementFrameDatas this animation used.
2013-06-06 12:02:54 +08:00
2015-06-17 13:16:06 +08:00
Armature *_armature; //! A weak reference of armature
2013-06-06 12:02:54 +08:00
std::string _movementID; //! Current movement's name
2013-06-06 12:02:54 +08:00
2015-06-17 13:16:06 +08:00
int _toIndex; //! The frame index in MovementData->m_pMovFrameDataArr, it's different from m_iFrameIndex.
2013-06-06 12:02:54 +08:00
2015-12-17 21:57:19 +08:00
std::vector<Tween*> _tweenList;
2013-09-13 18:07:37 +08:00
2013-10-30 09:41:40 +08:00
bool _ignoreFrameEvent;
std::queue<FrameEvent*> _frameEventQueue;
std::queue<MovementEvent*> _movementEventQueue;
std::vector<std::string> _movementList;
bool _onMovementList;
bool _movementListLoop;
unsigned int _movementIndex;
2013-12-18 23:29:54 +08:00
int _movementListDurationTo;
cocos2d::Ref *_userObject;
2013-09-13 18:07:37 +08:00
protected:
2013-06-07 10:52:32 +08:00
/**
2013-09-13 18:07:37 +08:00
* MovementEvent CallFunc.
* @param Armature* a Armature
2013-09-13 18:07:37 +08:00
* @param MovementEventType, Event Type, like START, COMPLETE.
* @param const char*, Movement ID, also called Movement Name
2013-06-06 12:02:54 +08:00
*/
2013-09-15 20:24:25 +08:00
SEL_MovementEventCallFunc _movementEventCallFunc;
2013-09-13 18:07:37 +08:00
/**
* FrameEvent CallFunc.
* @param Bone*, a Bone
2013-09-13 18:07:37 +08:00
* @param const char*, the name of this frame event
* @param int, origin frame index
* @param int, current frame index, animation may be delayed
*/
2013-09-15 20:24:25 +08:00
SEL_FrameEventCallFunc _frameEventCallFunc;
2013-09-13 18:07:37 +08:00
2013-06-06 12:02:54 +08:00
cocos2d::Ref *_movementEventTarget;
cocos2d::Ref *_frameEventTarget;
2013-12-18 22:07:33 +08:00
std::function<void(Armature *armature, MovementEventType movementType, const std::string& movementID)> _movementEventListener;
std::function<void(Bone *bone, const std::string& frameEventName, int originFrameIndex, int currentFrameIndex)> _frameEventListener;
2013-06-06 12:02:54 +08:00
};
}
2013-06-06 12:02:54 +08:00
2013-06-07 10:52:32 +08:00
#endif /*__CCANIMATION_H__*/