Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __SP_ANIMATION_PLAYBACK_H__
00009 #define __SP_ANIMATION_PLAYBACK_H__
00010
00011
00012 #include "Base/spStandard.hpp"
00013 #include "Base/spBaseObject.hpp"
00014
00015 #include <map>
00016 #include <boost/function.hpp>
00017
00018
00019 namespace sp
00020 {
00021 namespace scene
00022 {
00023
00024
00025 static const u32 ANIM_LAST_FRAME = ~0;
00026
00027
00028 class AnimationPlayback;
00029
00036 typedef boost::function<void (AnimationPlayback &Playback, bool isSetManual)> PlaybackFrameCallback;
00037
00038
00040 enum EAnimPlaybackModes
00041 {
00042 PLAYBACK_ONESHOT,
00043 PLAYBACK_ONELOOP,
00044 PLAYBACK_LOOP,
00045 PLAYBACK_PINGPONG,
00046 PLAYBACK_PINGPONG_LOOP,
00047 };
00048
00049
00051 struct SAnimSequence
00052 {
00053 SAnimSequence() :
00054 Mode (PLAYBACK_ONESHOT ),
00055 FirstFrame (0 ),
00056 LastFrame (0 ),
00057 Speed (1.0f )
00058 {
00059 }
00060 ~SAnimSequence()
00061 {
00062 }
00063
00064
00065 EAnimPlaybackModes Mode;
00066 u32 FirstFrame;
00067 u32 LastFrame;
00068 f32 Speed;
00069 };
00070
00077 struct SP_EXPORT SAnimQueue
00078 {
00079 SAnimQueue();
00080 ~SAnimQueue();
00081
00082
00083
00090 void addFrame(u32 Frame, f32 Speed = 1.0f);
00096 void removeEntry(u32 QueueEntryIndex);
00103 void clear();
00104
00106 struct SFrame
00107 {
00108 SFrame() :
00109 Frame(0 ),
00110 Speed(1.0f )
00111 {
00112 }
00113 SFrame(u32 InitFrame, f32 InitSpeed = 1.0f) :
00114 Frame(InitFrame),
00115 Speed(InitSpeed)
00116 {
00117 }
00118 ~SFrame()
00119 {
00120 }
00121
00122
00123 u32 Frame;
00124 f32 Speed;
00125 };
00126
00127
00128 std::vector<SFrame> Queue;
00129 };
00130
00131
00138 class SP_EXPORT AnimationPlayback : public BaseObject
00139 {
00140
00141 public:
00142
00143 AnimationPlayback();
00144 ~AnimationPlayback();
00145
00146
00147
00152 bool update(f32 Speed);
00153
00161 bool play(const EAnimPlaybackModes Mode, u32 FirstFrame, u32 LastFrame);
00162
00168 bool play(u32 SeqId);
00169
00171 void pause(bool IsPaused = true);
00172
00177 void stop(bool IsReset = false);
00178
00180 void setFrame(u32 Index);
00181
00183 void setFirstFrame(u32 Index);
00185 void setLastFrame(u32 Index);
00186
00193 bool addSequence(
00194 u32 SeqId, const EAnimPlaybackModes Mode, u32 FirstFrame, u32 LastFrame, f32 Speed = 1.0f
00195 );
00196
00198 bool removeSequence(u32 SeqId);
00199
00201 void clearSequences();
00202
00208 SAnimSequence getSequence(u32 SeqId) const;
00213 bool playingSeq(u32 SeqId) const;
00214
00215
00216
00224 static bool interpolateRange(u32 &FirstFrame, u32 &LastFrame, f32 &Interpolation);
00225
00226
00227
00229 inline bool playing() const
00230 {
00231 return IsPlaying_;
00232 }
00233
00241 inline bool playing(u32 MinFrame, u32 MaxFrame) const
00242 {
00243 return IsPlaying_ && Frame_ >= MinFrame && Frame_ < MaxFrame;
00244 }
00245
00247 inline u32 getFrame() const
00248 {
00249 return Frame_;
00250 }
00252 inline u32 getNextFrame() const
00253 {
00254 return NextFrame_;
00255 }
00256
00258 inline u32 getFirstFrame() const
00259 {
00260 return FirstFrame_;
00261 }
00263 inline u32 getLastFrame() const
00264 {
00265 return LastFrame_;
00266 }
00267
00269 inline void setInterpolation(f32 Interpolation)
00270 {
00271 Interpolation_ = Interpolation;
00272 }
00274 inline f32 getInterpolation() const
00275 {
00276 return Interpolation_;
00277 }
00278
00280 inline void setMode(const EAnimPlaybackModes Mode)
00281 {
00282 Mode_ = Mode;
00283 }
00285 inline EAnimPlaybackModes getMode() const
00286 {
00287 return Mode_;
00288 }
00289
00291 inline void setSpeed(f32 Speed)
00292 {
00293 Speed_ = Speed;
00294 }
00295 inline f32 getSpeed() const
00296 {
00297 return Speed_;
00298 }
00299
00304 inline void setFrameCallback(const PlaybackFrameCallback &FrameCallback)
00305 {
00306 FrameCallback_ = FrameCallback;
00307 }
00309 inline PlaybackFrameCallback getFrameCallback() const
00310 {
00311 return FrameCallback_;
00312 }
00313
00314 private:
00315
00316
00317
00318 void checkAnimationEnding();
00319
00320
00321
00322 inline void frameCallback(bool isSetManual)
00323 {
00324 if (FrameCallback_)
00325 FrameCallback_(*this, isSetManual);
00326 }
00327
00328 inline void stopAutoAnim()
00329 {
00330 Interpolation_ = 0.0f;
00331 stop();
00332 }
00333
00334
00335
00336 EAnimPlaybackModes Mode_;
00337
00338 bool HasStarted_;
00339 bool IsPlaying_;
00340
00341 u32 Frame_;
00342 s32 NextFrame_;
00343 f32 Interpolation_;
00344
00345 u32 FirstFrame_, LastFrame_;
00346
00347 f32 Speed_;
00348 u32 RepeatCount_;
00349
00350 std::map<u32, SAnimSequence> Sequences_;
00351
00352
00353 PlaybackFrameCallback FrameCallback_;
00354
00355 };
00356
00357
00358 }
00359
00360 }
00361
00362
00363 #endif
00364
00365
00366
00367