2010-12-27 10:26:56 +08:00
/****************************************************************************
2011-03-19 10:59:01 +08:00
Copyright ( c ) 2008 - 2010 Ricardo Quesada
2014-01-07 11:25:07 +08:00
Copyright ( c ) 2010 - 2012 cocos2d - x . org
2011-07-01 16:11:31 +08:00
Copyright ( c ) 2011 Zynga Inc .
2014-01-07 11:25:07 +08:00
CopyRight ( c ) 2013 - 2014 Chukong Technologies Inc .
2010-12-27 10:26:56 +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 __CC_ANIMATION_H__
# define __CC_ANIMATION_H__
2014-04-27 01:35:57 +08:00
# include "base/CCPlatformConfig.h"
2014-04-27 01:11:22 +08:00
# include "base/CCRef.h"
2014-04-27 01:35:57 +08:00
# include "base/CCValue.h"
2014-04-27 01:11:22 +08:00
# include "base/CCGeometry.h"
# include "2d/CCSpriteFrame.h"
2014-04-27 01:35:57 +08:00
# include "base/CCVector.h"
2013-11-28 16:02:03 +08:00
2011-01-07 09:35:27 +08:00
# include <string>
2010-12-27 10:26:56 +08:00
2012-03-16 13:42:53 +08:00
NS_CC_BEGIN
2013-06-20 14:13:12 +08:00
class Texture2D ;
class SpriteFrame ;
2012-03-16 13:42:53 +08:00
2012-06-20 18:09:11 +08:00
/**
* @ addtogroup sprite_nodes
* @ {
*/
2013-06-20 14:13:12 +08:00
/** AnimationFrame
2012-04-19 14:35:52 +08:00
A frame of the animation . It contains information like :
- sprite frame name
- # of delay units .
- offset
@ since v2 .0
*/
2014-02-20 10:53:49 +08:00
class CC_DLL AnimationFrame : public Ref , public Clonable
2012-04-19 14:35:52 +08:00
{
public :
2013-09-13 16:46:31 +08:00
/**
2013-11-28 16:02:03 +08:00
* Creates the animation frame with a spriteframe , number of delay units and a notification user info
* @ since 3.0
2013-09-13 13:52:42 +08:00
*/
2013-12-04 17:46:57 +08:00
static AnimationFrame * create ( SpriteFrame * spriteFrame , float delayUnits , const ValueMap & userInfo ) ;
2013-06-19 06:06:53 +08:00
2013-07-23 18:26:26 +08:00
SpriteFrame * getSpriteFrame ( ) const { return _spriteFrame ; } ;
void setSpriteFrame ( SpriteFrame * frame )
{
CC_SAFE_RETAIN ( frame ) ;
CC_SAFE_RELEASE ( _spriteFrame ) ;
_spriteFrame = frame ;
}
/** Gets the units of time the frame takes */
float getDelayUnits ( ) const { return _delayUnits ; } ;
/** Sets the units of time the frame takes */
void setDelayUnits ( float delayUnits ) { _delayUnits = delayUnits ; } ;
/** @brief Gets user infomation
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 .
*/
2013-12-04 17:46:57 +08:00
const ValueMap & getUserInfo ( ) const { return _userInfo ; } ;
ValueMap & getUserInfo ( ) { return _userInfo ; } ;
2013-07-23 18:26:26 +08:00
/** Sets user infomation */
2013-12-04 17:46:57 +08:00
void setUserInfo ( const ValueMap & userInfo )
2013-07-23 18:26:26 +08:00
{
_userInfo = userInfo ;
}
2013-07-18 07:56:19 +08:00
// Overrides
virtual AnimationFrame * clone ( ) const override ;
2013-11-28 16:02:03 +08:00
2014-03-21 13:44:29 +08:00
CC_CONSTRUCTOR_ACCESS :
2013-11-28 16:02:03 +08:00
/**
* @ js ctor
*/
AnimationFrame ( ) ;
/**
* @ js NA
* @ lua NA
*/
virtual ~ AnimationFrame ( ) ;
/** initializes the animation frame with a spriteframe, number of delay units and a notification user info */
2013-12-04 17:46:57 +08:00
bool initWithSpriteFrame ( SpriteFrame * spriteFrame , float delayUnits , const ValueMap & userInfo ) ;
2014-03-21 13:44:29 +08:00
protected :
2013-11-28 16:02:03 +08:00
2013-06-20 14:13:12 +08:00
/** SpriteFrameName to be used */
2013-07-23 18:26:26 +08:00
SpriteFrame * _spriteFrame ;
2012-04-19 14:35:52 +08:00
/** how many units of time the frame takes */
2013-07-23 18:26:26 +08:00
float _delayUnits ;
2012-04-19 14:35:52 +08:00
2013-06-20 14:13:12 +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. */
2013-12-04 17:46:57 +08:00
ValueMap _userInfo ;
2013-11-28 16:02:03 +08:00
private :
CC_DISALLOW_COPY_AND_ASSIGN ( AnimationFrame ) ;
2012-04-19 14:35:52 +08:00
} ;
2012-03-16 13:42:53 +08:00
2013-06-20 14:13:12 +08:00
/** A Animation object is used to perform animations on the Sprite objects.
2012-03-16 13:42:53 +08:00
2013-06-20 14:13:12 +08:00
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 :
2012-03-16 13:42:53 +08:00
2013-08-01 16:57:42 +08:00
@ code
2013-08-01 16:39:42 +08:00
sprite - > runAction ( Animate : : create ( animation ) ) ;
2013-08-01 16:57:42 +08:00
@ endcode
2012-03-16 13:42:53 +08:00
*/
2014-02-20 10:53:49 +08:00
class CC_DLL Animation : public Ref , public Clonable
2012-03-16 13:42:53 +08:00
{
public :
2012-04-19 14:35:52 +08:00
/** Creates an animation
2013-07-18 07:56:19 +08:00
@ since v0 .99 .5
*/
2013-06-20 14:13:12 +08:00
static Animation * create ( void ) ;
2012-04-19 14:35:52 +08:00
2013-06-20 14:13:12 +08:00
/* Creates an animation with an array of SpriteFrame and a delay between frames in seconds.
2012-04-19 14:35:52 +08:00
The frames will be added with one " delay unit " .
@ since v0 .99 .5
2013-07-18 07:56:19 +08:00
*/
2014-04-02 18:06:17 +08:00
static Animation * createWithSpriteFrames ( const Vector < SpriteFrame * > & arrayOfSpriteFrameNames , float delay = 0.0f , unsigned int loops = 1 ) ;
2012-04-19 14:35:52 +08:00
2013-06-20 14:13:12 +08:00
/* Creates an animation with an array of AnimationFrame, the delay per units in seconds and and how many times it should be executed.
2012-04-19 14:35:52 +08:00
@ since v2 .0
2013-09-13 11:41:20 +08:00
* @ js NA
2012-04-19 14:35:52 +08:00
*/
2013-11-28 16:02:03 +08:00
static Animation * create ( const Vector < AnimationFrame * > & arrayOfAnimationFrameNames , float delayPerUnit , unsigned int loops = 1 ) ;
2013-07-18 07:56:19 +08:00
2013-06-20 14:13:12 +08:00
/** Adds a SpriteFrame to a Animation.
2012-04-19 14:35:52 +08:00
The frame will be added with one " delay unit " .
2013-07-18 07:56:19 +08:00
*/
2013-12-18 17:47:20 +08:00
void addSpriteFrame ( SpriteFrame * frame ) ;
2012-04-19 14:35:52 +08:00
2013-06-20 14:13:12 +08:00
/** Adds a frame with an image filename. Internally it will create a SpriteFrame and it will add it.
2012-04-19 14:35:52 +08:00
The frame will be added with one " delay unit " .
Added to facilitate the migration from v0 .8 to v0 .9 .
2013-07-18 07:56:19 +08:00
*/
2013-11-15 09:19:16 +08:00
void addSpriteFrameWithFile ( const std : : string & filename ) ;
2013-09-13 11:41:20 +08:00
/**
@ deprecated . Use addSpriteFrameWithFile ( ) instead
*/
2013-11-15 09:19:16 +08:00
CC_DEPRECATED_ATTRIBUTE void addSpriteFrameWithFileName ( const std : : string & filename ) { addSpriteFrameWithFile ( filename ) ; }
2012-04-19 14:35:52 +08:00
2013-06-20 14:13:12 +08:00
/** Adds a frame with a texture and a rect. Internally it will create a SpriteFrame and it will add it.
2012-04-19 14:35:52 +08:00
The frame will be added with one " delay unit " .
Added to facilitate the migration from v0 .8 to v0 .9 .
*/
2013-06-20 14:13:12 +08:00
void addSpriteFrameWithTexture ( Texture2D * pobTexture , const Rect & rect ) ;
2012-04-19 14:35:52 +08:00
2013-07-23 18:26:26 +08:00
/** Gets the total Delay units of the Animation. */
float getTotalDelayUnits ( ) const { return _totalDelayUnits ; } ;
/** Sets the delay in seconds of the "delay unit" */
void setDelayPerUnit ( float delayPerUnit ) { _delayPerUnit = delayPerUnit ; } ;
/** Gets 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 */
float getDuration ( ) const ;
/** Gets the array of AnimationFrames */
2013-11-28 16:02:03 +08:00
const Vector < AnimationFrame * > & getFrames ( ) const { return _frames ; } ;
2013-07-23 18:26:26 +08:00
/** Sets the array of AnimationFrames */
2013-11-28 16:02:03 +08:00
void setFrames ( const Vector < AnimationFrame * > & frames )
2013-07-23 18:26:26 +08:00
{
_frames = frames ;
}
/** Checks whether to restore the original frame when animation finishes. */
bool getRestoreOriginalFrame ( ) const { return _restoreOriginalFrame ; } ;
/** Sets whether to restore the original frame when animation finishes */
void setRestoreOriginalFrame ( bool restoreOriginalFrame ) { _restoreOriginalFrame = restoreOriginalFrame ; } ;
/** Gets the times the animation is going to loop. 0 means animation is not animated. 1, animation is executed one time, ... */
unsigned int getLoops ( ) const { return _loops ; } ;
/** Sets the times the animation is going to loop. 0 means animation is not animated. 1, animation is executed one time, ... */
void setLoops ( unsigned int loops ) { _loops = loops ; } ;
2013-07-18 07:56:19 +08:00
// overrides
virtual Animation * clone ( ) const override ;
2014-03-21 13:44:29 +08:00
CC_CONSTRUCTOR_ACCESS :
2013-11-28 16:02:03 +08:00
Animation ( ) ;
virtual ~ Animation ( void ) ;
/** Initializes a Animation */
bool init ( ) ;
/** Initializes a Animation with frames and a delay between frames
@ since v0 .99 .5
*/
2014-04-02 11:48:49 +08:00
bool initWithSpriteFrames ( const Vector < SpriteFrame * > & arrayOfSpriteFrameNames , float delay = 0.0f , unsigned int loops = 1 ) ;
2013-11-28 16:02:03 +08:00
/** Initializes a Animation with AnimationFrame
@ since v2 .0
*/
bool initWithAnimationFrames ( const Vector < AnimationFrame * > & arrayOfAnimationFrameNames , float delayPerUnit , unsigned int loops ) ;
2014-03-21 13:44:29 +08:00
protected :
2013-06-20 14:13:12 +08:00
/** total Delay units of the Animation. */
2013-07-23 18:26:26 +08:00
float _totalDelayUnits ;
2012-04-19 14:35:52 +08:00
/** Delay in seconds of the "delay unit" */
2013-07-23 18:26:26 +08:00
float _delayPerUnit ;
2012-04-19 14:35:52 +08:00
/** duration in seconds of the whole animation. It is the result of totalDelayUnits * delayPerUnit */
2013-07-23 18:26:26 +08:00
float _duration ;
2012-04-19 14:35:52 +08:00
2013-06-20 14:13:12 +08:00
/** array of AnimationFrames */
2013-11-28 16:02:03 +08:00
Vector < AnimationFrame * > _frames ;
2012-04-19 14:35:52 +08:00
/** whether or not it shall restore the original frame when the animation finishes */
2013-07-23 18:26:26 +08:00
bool _restoreOriginalFrame ;
2012-04-19 14:35:52 +08:00
/** how many times the animation is going to loop. 0 means animation is not animated. 1, animation is executed one time, ... */
2013-07-23 18:26:26 +08:00
unsigned int _loops ;
2013-11-28 16:02:03 +08:00
private :
CC_DISALLOW_COPY_AND_ASSIGN ( Animation ) ;
2012-03-16 13:42:53 +08:00
} ;
2012-06-20 18:09:11 +08:00
// end of sprite_nodes group
/// @}
2012-03-16 13:42:53 +08:00
NS_CC_END
2010-12-27 10:26:56 +08:00
# endif // __CC_ANIMATION_H__