axmol/core/2d/CCAnimation.h

326 lines
11 KiB
C
Raw Normal View History

2010-12-27 10:26:56 +08:00
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2010-12-27 10:26:56 +08:00
https://axis-project.github.io/
2010-12-27 10:26:56 +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 __CC_ANIMATION_H__
#define __CC_ANIMATION_H__
#include "platform/CCPlatformConfig.h"
#include "base/CCRef.h"
2014-04-27 01:35:57 +08:00
#include "base/CCValue.h"
#include "base/CCVector.h"
2014-08-28 17:03:29 +08:00
#include "2d/CCSpriteFrame.h"
2011-01-07 09:35:27 +08:00
#include <string>
2010-12-27 10:26:56 +08:00
NS_CC_BEGIN
class Texture2D;
class SpriteFrame;
2012-06-20 18:09:11 +08:00
/**
2015-03-21 17:06:26 +08:00
* @addtogroup _2d
2012-06-20 18:09:11 +08:00
* @{
*/
2015-03-27 11:39:31 +08:00
/** @class AnimationFrame
*
* A frame of the animation. It contains information like:
2015-03-27 11:39:31 +08:00
* - sprite frame name.
* - # of delay units.
* - offset
2021-12-25 10:04:45 +08:00
@since v2.0
*/
class CC_DLL AnimationFrame : public Ref, public Clonable
{
public:
/** @struct DisplayedEventInfo
* When the animation display,Dispatches the event of UserData.
*/
struct DisplayedEventInfo
{
Node* target;
const ValueMap* userInfo;
};
2021-12-25 10:04:45 +08:00
/**
* Creates the animation frame with a spriteframe, number of delay units and a notification user info.
*
* @param spriteFrame The animation frame with a spriteframe.
* @param delayUnits Number of delay units.
* @param userInfo A notification user info.
* @since 3.0
*/
static AnimationFrame* create(SpriteFrame* spriteFrame, float delayUnits, const ValueMap& userInfo);
/** Return a SpriteFrameName to be used.
*
* @return a SpriteFrameName to be used.
*/
SpriteFrame* getSpriteFrame() const { return _spriteFrame; };
/** Set the SpriteFrame.
*
* @param frame A SpriteFrame will be used.
*/
void setSpriteFrame(SpriteFrame* frame)
{
CC_SAFE_RETAIN(frame);
CC_SAFE_RELEASE(_spriteFrame);
_spriteFrame = frame;
}
/** Gets the units of time the frame takes.
*
* @return The units of time the frame takes.
*/
float getDelayUnits() const { return _delayUnits; };
2021-12-25 10:04:45 +08:00
/** Sets the units of time the frame takes.
*
* @param delayUnits The units of time the frame takes.
*/
void setDelayUnits(float delayUnits) { _delayUnits = delayUnits; };
2021-12-25 10:04:45 +08:00
/** @brief Gets user information
2021-12-25 10:04:45 +08:00
* A AnimationFrameDisplayedNotification notification will be broadcast when the frame is displayed with this
* dictionary as UserInfo. If UserInfo is nil, then no notification will be broadcast.
*
* @return A dictionary as UserInfo
*/
const ValueMap& getUserInfo() const { return _userInfo; };
ValueMap& getUserInfo() { return _userInfo; };
2021-12-25 10:04:45 +08:00
/** Sets user information.
* @param userInfo A dictionary as UserInfo.
*/
2021-12-25 10:04:45 +08:00
void setUserInfo(const ValueMap& userInfo) { _userInfo = userInfo; }
// Overrides
2021-12-25 10:04:45 +08:00
virtual AnimationFrame* clone() const override;
/**
* @js ctor
*/
AnimationFrame();
/**
* @js NA
* @lua NA
*/
virtual ~AnimationFrame();
2021-12-25 10:04:45 +08:00
/** initializes the animation frame with a spriteframe, number of delay units and a notification user info */
bool initWithSpriteFrame(SpriteFrame* spriteFrame, float delayUnits, const ValueMap& userInfo);
protected:
/** SpriteFrameName to be used */
SpriteFrame* _spriteFrame;
/** how many units of time the frame takes */
float _delayUnits;
2021-12-25 10:04:45 +08:00
/** A AnimationFrameDisplayedNotification notification will be broadcast when the frame is displayed with this
* dictionary as UserInfo. If UserInfo is nil, then no notification will be broadcast. */
ValueMap _userInfo;
2021-12-25 10:04:45 +08:00
private:
CC_DISALLOW_COPY_AND_ASSIGN(AnimationFrame);
};
/** @class Animation
* A Animation object is used to perform animations on the Sprite objects.
* The Animation object contains AnimationFrame objects, and a possible delay between the frames.
* You can animate a Animation object by using the Animate action. Example:
* @code
* sprite->runAction(Animate::create(animation));
* @endcode
2021-12-25 10:04:45 +08:00
*/
class CC_DLL Animation : public Ref, public Clonable
{
public:
/** Creates an animation.
* @since v0.99.5
*/
static Animation* create();
/* Creates an animation with an array of SpriteFrame and a delay between frames in seconds.
* The frames will be added with one "delay unit".
* @since v0.99.5
* @param arrayOfSpriteFrameNames An array of SpriteFrame.
* @param delay A delay between frames in seconds.
* @param loops The times the animation is going to loop.
*/
2021-12-25 10:04:45 +08:00
static Animation* createWithSpriteFrames(const Vector<SpriteFrame*>& arrayOfSpriteFrameNames,
float delay = 0.0f,
unsigned int loops = 1);
2021-12-25 10:04:45 +08:00
/* Creates an animation with an array of AnimationFrame, the delay per units in seconds and how many times it should
* be executed.
* @since v2.0
* @param arrayOfAnimationFrameNames An animation with an array of AnimationFrame.
* @param delayPerUnit The delay per units in seconds and how many times it should be executed.
* @param loops The times the animation is going to loop.
*/
2021-12-25 10:04:45 +08:00
static Animation* create(const Vector<AnimationFrame*>& arrayOfAnimationFrameNames,
float delayPerUnit,
unsigned int loops = 1);
/** Adds a SpriteFrame to a Animation.
*
* @param frame The frame will be added with one "delay unit".
*/
2021-12-25 10:04:45 +08:00
void addSpriteFrame(SpriteFrame* frame);
/** Adds a frame with an image filename. Internally it will create a SpriteFrame and it will add it.
* The frame will be added with one "delay unit".
* Added to facilitate the migration from v0.8 to v0.9.
* @param filename The path of SpriteFrame.
*/
2021-12-26 23:26:34 +08:00
void addSpriteFrameWithFile(std::string_view filename);
/** Adds a frame with a texture and a rect. Internally it will create a SpriteFrame and it will add it.
* The frame will be added with one "delay unit".
* Added to facilitate the migration from v0.8 to v0.9.
* @param pobTexture A frame with a texture.
* @param rect The Texture of rect.
*/
void addSpriteFrameWithTexture(Texture2D* pobTexture, const Rect& rect);
2021-12-25 10:04:45 +08:00
/** Gets the total Delay units of the Animation.
*
* @return The total Delay units of the Animation.
*/
float getTotalDelayUnits() const { return _totalDelayUnits; };
2021-12-25 10:04:45 +08:00
/** Sets the delay in seconds of the "delay unit".
*
2015-03-27 11:39:31 +08:00
* @param delayPerUnit The delay in seconds of the "delay unit".
*/
void setDelayPerUnit(float delayPerUnit) { _delayPerUnit = delayPerUnit; };
2021-12-25 10:04:45 +08:00
/** Gets the delay in seconds of the "delay unit".
2021-12-25 10:04:45 +08:00
*
* @return The delay in seconds of the "delay unit".
*/
float getDelayPerUnit() const { return _delayPerUnit; };
/** Gets the duration in seconds of the whole animation. It is the result of totalDelayUnits * delayPerUnit.
*
* @return Result of totalDelayUnits * delayPerUnit.
*/
float getDuration() const;
2021-12-25 10:04:45 +08:00
/** Gets the array of AnimationFrames.
2021-12-25 10:04:45 +08:00
*
* @return The array of AnimationFrames.
*/
const Vector<AnimationFrame*>& getFrames() const { return _frames; };
2021-12-25 10:04:45 +08:00
/** Sets the array of AnimationFrames.
*
* @param frames The array of AnimationFrames.
*/
2021-12-25 10:04:45 +08:00
void setFrames(const Vector<AnimationFrame*>& frames) { _frames = frames; }
/** Checks whether to restore the original frame when animation finishes.
*
* @return Restore the original frame when animation finishes.
*/
bool getRestoreOriginalFrame() const { return _restoreOriginalFrame; };
2021-12-25 10:04:45 +08:00
/** Sets whether to restore the original frame when animation finishes.
*
* @param restoreOriginalFrame Whether to restore the original frame when animation finishes.
*/
void setRestoreOriginalFrame(bool restoreOriginalFrame) { _restoreOriginalFrame = restoreOriginalFrame; };
2021-12-25 10:04:45 +08:00
/** Gets the times the animation is going to loop. 0 means animation is not animated. 1, animation is executed one
* time, ...
*
* @return The times the animation is going to loop.
*/
unsigned int getLoops() const { return _loops; };
2021-12-25 10:04:45 +08:00
/** Sets the times the animation is going to loop. 0 means animation is not animated. 1, animation is executed one
* time, ...
*
* @param loops The times the animation is going to loop.
*/
void setLoops(unsigned int loops) { _loops = loops; };
2021-12-25 10:04:45 +08:00
// overrides
2021-12-25 10:04:45 +08:00
virtual Animation* clone() const override;
Animation();
virtual ~Animation();
2021-12-25 10:04:45 +08:00
/** Initializes a Animation. */
bool init();
2021-12-25 10:04:45 +08:00
/** Initializes a Animation with frames and a delay between frames.
* @since v0.99.5
*/
2021-12-25 10:04:45 +08:00
bool initWithSpriteFrames(const Vector<SpriteFrame*>& arrayOfSpriteFrameNames,
float delay = 0.0f,
unsigned int loops = 1);
/** Initializes a Animation with AnimationFrame.
* @since v2.0
*/
2021-12-25 10:04:45 +08:00
bool initWithAnimationFrames(const Vector<AnimationFrame*>& arrayOfAnimationFrameNames,
float delayPerUnit,
unsigned int loops);
protected:
/** total Delay units of the Animation. */
float _totalDelayUnits;
/** Delay in seconds of the "delay unit". */
float _delayPerUnit;
/** duration in seconds of the whole animation. It is the result of totalDelayUnits * delayPerUnit. */
float _duration;
/** array of AnimationFrames. */
Vector<AnimationFrame*> _frames;
/** whether or not it shall restore the original frame when the animation finishes. */
bool _restoreOriginalFrame;
2021-12-25 10:04:45 +08:00
/** how many times the animation is going to loop. 0 means animation is not animated. 1, animation is executed one
* time, ... */
unsigned int _loops;
2021-12-25 10:04:45 +08:00
private:
CC_DISALLOW_COPY_AND_ASSIGN(Animation);
};
2012-06-20 18:09:11 +08:00
// end of sprite_nodes group
/// @}
NS_CC_END
2010-12-27 10:26:56 +08:00
2021-12-25 10:04:45 +08:00
#endif // __CC_ANIMATION_H__