axmol/extensions/cocostudio/CCArmatureAnimation.h

333 lines
10 KiB
C
Raw Normal View History

2021-12-26 23:26:34 +08:00
/****************************************************************************
2020-10-17 16:32:16 +08:00
Copyright (c) 2013-2017 Chukong Technologies Inc.
2019-11-23 20:27:39 +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"
2019-11-23 20:27:39 +08:00
#include <queue>
2021-12-25 10:04:45 +08:00
namespace cocostudio
{
2019-11-23 20:27:39 +08:00
enum MovementEventType
{
START,
COMPLETE,
LOOP_COMPLETE
};
class Armature;
class Bone;
2021-12-27 13:52:08 +08:00
typedef void (cocos2d::Ref::*SEL_MovementEventCallFunc)(Armature*, MovementEventType, std::string_view);
typedef void (cocos2d::Ref::*SEL_FrameEventCallFunc)(Bone*, std::string_view, int, int);
2019-11-23 20:27:39 +08:00
#define movementEvent_selector(_SELECTOR) (cocostudio::SEL_MovementEventCallFunc)(&_SELECTOR)
#define frameEvent_selector(_SELECTOR) (cocostudio::SEL_FrameEventCallFunc)(&_SELECTOR)
struct FrameEvent
{
2021-12-25 10:04:45 +08:00
Bone* bone;
2019-11-23 20:27:39 +08:00
std::string frameEventName;
int originFrameIndex;
int currentFrameIndex;
};
struct MovementEvent
{
2021-12-25 10:04:45 +08:00
Armature* armature;
2019-11-23 20:27:39 +08:00
MovementEventType movementType;
std::string movementID;
};
2021-12-25 10:04:45 +08:00
class CCS_DLL ArmatureAnimation : public ProcessBase
2019-11-23 20:27:39 +08:00
{
public:
/**
* Create with a Armature
* @param armature The Armature ArmatureAnimation will bind to
*/
2021-12-25 10:04:45 +08:00
static ArmatureAnimation* create(Armature* armature);
2019-11-23 20:27:39 +08:00
public:
/**
* @js ctor
*/
ArmatureAnimation();
/**
* @js NA
* @lua NA
*/
2020-10-17 16:32:16 +08:00
virtual ~ArmatureAnimation(void);
2019-11-23 20:27:39 +08:00
/**
* Init with a Armature
* @param armature The Armature ArmatureAnimation will bind to
*/
2021-12-25 10:04:45 +08:00
virtual bool init(Armature* armature);
2019-11-23 20:27:39 +08:00
/**
* Scale animation play speed.
* This method is deprecated, please use setSpeedScale.
* @param animationScale Scale value
*/
CC_DEPRECATED_ATTRIBUTE virtual void setAnimationScale(float animationScale);
CC_DEPRECATED_ATTRIBUTE virtual float getAnimationScale() const;
/**
* 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) {}
using ProcessBase::play;
/**
* Play animation by animation name.
*
* @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
* @param loop Whether the animation is loop
*
* loop < 0 : use the value from MovementData get from flash design panel
* loop = 0 : this animation is not loop
* loop > 0 : this animation is loop
*/
2021-12-26 23:26:34 +08:00
virtual void play(std::string_view animationName, int durationTo = -1, int loop = -1);
2019-11-23 20:27:39 +08:00
/**
* Play animation by index, the other param is the same to play.
* @deprecated, please use playWithIndex
* @param animationIndex the animation index you want to play
*/
2021-12-25 10:04:45 +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);
2019-11-23 20:27:39 +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);
/**
* Go to specified frame and play current movement.
* You need first switch to the movement you want to play, then call this function.
2021-12-25 10:04:45 +08:00
*
2019-11-23 20:27:39 +08:00
* 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);
/**
* Pause the Process
*/
virtual void pause() override;
/**
* Resume the Process
*/
virtual void resume() override;
/**
* Stop the Process
*/
virtual void stop() override;
/**
* Get movement count
*/
ssize_t getMovementCount() const;
virtual void update(float dt) override;
/**
* Get current movementID
* @return The name of current movement
*/
std::string getCurrentMovementID() const;
/**
* Set armature's movement event callback function
* To disconnect this event, just setMovementEventCallFunc(nullptr, nullptr);
*/
2021-12-25 10:04:45 +08:00
CC_DEPRECATED_ATTRIBUTE void setMovementEventCallFunc(cocos2d::Ref* target, SEL_MovementEventCallFunc callFunc);
2019-11-23 20:27:39 +08:00
/**
* Set armature's frame event callback function
* To disconnect this event, just setFrameEventCallFunc(nullptr, nullptr);
*/
2021-12-25 10:04:45 +08:00
CC_DEPRECATED_ATTRIBUTE void setFrameEventCallFunc(cocos2d::Ref* target, SEL_FrameEventCallFunc callFunc);
void setMovementEventCallFunc(
2021-12-28 11:00:34 +08:00
std::function<void(Armature* armature, MovementEventType movementType, std::string_view movementID)> listener);
2021-12-25 10:04:45 +08:00
void setFrameEventCallFunc(
2021-12-26 23:26:34 +08:00
std::function<void(Bone* bone, std::string_view frameEventName, int originFrameIndex, int currentFrameIndex)>
2021-12-25 10:04:45 +08:00
listener);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
virtual void setAnimationData(AnimationData* data)
2019-11-23 20:27:39 +08:00
{
if (_animationData != data)
{
CC_SAFE_RETAIN(data);
CC_SAFE_RELEASE(_animationData);
2021-12-25 10:04:45 +08:00
_animationData = data;
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
virtual AnimationData* getAnimationData() const { return _animationData; }
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
/**
2019-11-23 20:27:39 +08:00
* Returns a user assigned Object
2021-12-25 10:04:45 +08:00
*
2019-11-23 20:27:39 +08:00
* 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; }
2021-12-25 10:04:45 +08:00
/**
* @js NA
* @lua NA
*/
2019-11-23 20:27:39 +08:00
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
*/
2021-12-25 10:04:45 +08:00
virtual void setUserObject(Ref* userObject);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
protected:
2019-11-23 20:27:39 +08:00
/**
* Update(float dt) will call this handler, you can handle your logic here
* @js NA
* @lua NA
*/
void updateHandler() override;
/**
* Update current key frame, and process auto stop, pause
* @js NA
* @lua NA
*/
void updateFrameData(float currentPercent);
/**
* Emit a frame event
* @js NA
* @lua NA
*/
2021-12-26 23:26:34 +08:00
void frameEvent(Bone* bone, std::string_view frameEventName, int originFrameIndex, int currentFrameIndex);
2019-11-23 20:27:39 +08:00
/**
* Emit a movement event
*/
2021-12-26 23:26:34 +08:00
void movementEvent(Armature* armature, MovementEventType movementType, std::string_view movementID);
2019-11-23 20:27:39 +08:00
void updateMovementList();
bool isIgnoreFrameEvent() const { return _ignoreFrameEvent; }
friend class Tween;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
//! AnimationData save all MovementDatas this animation used.
2021-12-25 10:04:45 +08:00
AnimationData* _animationData;
2019-11-23 20:27:39 +08:00
//! Scale the animation speed
float _speedScale;
2021-12-25 10:04:45 +08:00
MovementData* _movementData; //! MovementData save all MovementFrameDatas this animation used.
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
Armature* _armature; //! A weak reference of armature
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
std::string _movementID; //! Current movement's name
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
int _toIndex; //! The frame index in MovementData->m_pMovFrameDataArr, it's different from m_iFrameIndex.
2019-11-23 20:27:39 +08:00
std::vector<Tween*> _tweenList;
bool _ignoreFrameEvent;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
std::queue<FrameEvent*> _frameEventQueue;
std::queue<MovementEvent*> _movementEventQueue;
std::vector<std::string> _movementList;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
bool _onMovementList;
bool _movementListLoop;
unsigned int _movementIndex;
int _movementListDurationTo;
2021-12-25 10:04:45 +08:00
cocos2d::Ref* _userObject;
2019-11-23 20:27:39 +08:00
protected:
/**
* MovementEvent CallFunc.
* @param Armature* a Armature
* @param MovementEventType, Event Type, like START, COMPLETE.
* @param const char*, Movement ID, also called Movement Name
*/
SEL_MovementEventCallFunc _movementEventCallFunc;
/**
* FrameEvent CallFunc.
* @param Bone*, a Bone
* @param const char*, the name of this frame event
* @param int, origin frame index
* @param int, current frame index, animation may be delayed
*/
SEL_FrameEventCallFunc _frameEventCallFunc;
2021-12-25 10:04:45 +08:00
cocos2d::Ref* _movementEventTarget;
cocos2d::Ref* _frameEventTarget;
2019-11-23 20:27:39 +08:00
2021-12-26 23:26:34 +08:00
std::function<void(Armature* armature, MovementEventType movementType, std::string_view movementID)>
2021-12-25 10:04:45 +08:00
_movementEventListener;
2021-12-26 23:26:34 +08:00
std::function<void(Bone* bone, std::string_view frameEventName, int originFrameIndex, int currentFrameIndex)>
2021-12-25 10:04:45 +08:00
_frameEventListener;
2019-11-23 20:27:39 +08:00
};
2021-12-25 10:04:45 +08:00
} // namespace cocostudio
2019-11-23 20:27:39 +08:00
#endif /*__CCANIMATION_H__*/