Panda3D
 All Classes Functions Variables Enumerations
animInterface.I
00001 // Filename: animInterface.I
00002 // Created by:  drose (20Sep05)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 
00016 ////////////////////////////////////////////////////////////////////
00017 //     Function: AnimInterface::play
00018 //       Access: Published
00019 //  Description: Runs the entire animation from beginning to end and
00020 //               stops.
00021 ////////////////////////////////////////////////////////////////////
00022 INLINE void AnimInterface::
00023 play() {
00024   play(0, get_num_frames() - 1);
00025 }
00026 
00027 ////////////////////////////////////////////////////////////////////
00028 //     Function: AnimInterface::play
00029 //       Access: Published
00030 //  Description: Runs the animation from the frame "from" to and
00031 //               including the frame "to", at which point the
00032 //               animation is stopped.  Both "from" and "to" frame
00033 //               numbers may be outside the range (0,
00034 //               get_num_frames()) and the animation will follow the
00035 //               range correctly, reporting numbers modulo
00036 //               get_num_frames().  For instance, play(0,
00037 //               get_num_frames() * 2) will play the animation twice
00038 //               and then stop.
00039 ////////////////////////////////////////////////////////////////////
00040 INLINE void AnimInterface::
00041 play(double from, double to) {
00042   {
00043     CDWriter cdata(_cycler);
00044     cdata->play(from, to);
00045   }
00046   animation_activated();
00047 }
00048 
00049 ////////////////////////////////////////////////////////////////////
00050 //     Function: AnimInterface::loop
00051 //       Access: Published
00052 //  Description: Starts the entire animation looping.  If restart is
00053 //               true, the animation is restarted from the beginning;
00054 //               otherwise, it continues from the current frame.
00055 ////////////////////////////////////////////////////////////////////
00056 INLINE void AnimInterface::
00057 loop(bool restart) {
00058   loop(restart, 0, get_num_frames() - 1);
00059 }
00060 
00061 ////////////////////////////////////////////////////////////////////
00062 //     Function: AnimInterface::loop
00063 //       Access: Published
00064 //  Description: Loops the animation from the frame "from" to and
00065 //               including the frame "to", indefinitely.  If restart
00066 //               is true, the animation is restarted from the
00067 //               beginning; otherwise, it continues from the current
00068 //               frame.
00069 ////////////////////////////////////////////////////////////////////
00070 INLINE void AnimInterface::
00071 loop(bool restart, double from, double to) {
00072   {
00073     CDWriter cdata(_cycler);
00074     cdata->loop(restart, from, to);
00075   }
00076   animation_activated();
00077 }
00078 
00079 ////////////////////////////////////////////////////////////////////
00080 //     Function: AnimInterface::pingpong
00081 //       Access: Published
00082 //  Description: Starts the entire animation bouncing back and forth
00083 //               between its first frame and last frame.  If restart
00084 //               is true, the animation is restarted from the
00085 //               beginning; otherwise, it continues from the current
00086 //               frame.
00087 ////////////////////////////////////////////////////////////////////
00088 INLINE void AnimInterface::
00089 pingpong(bool restart) {
00090   pingpong(restart, 0, get_num_frames() - 1);
00091 }
00092 
00093 ////////////////////////////////////////////////////////////////////
00094 //     Function: AnimInterface::pingpong
00095 //       Access: Published
00096 //  Description: Loops the animation from the frame "from" to and
00097 //               including the frame "to", and then back in the
00098 //               opposite direction, indefinitely.
00099 ////////////////////////////////////////////////////////////////////
00100 INLINE void AnimInterface::
00101 pingpong(bool restart, double from, double to) {
00102   {
00103     CDWriter cdata(_cycler);
00104     cdata->pingpong(restart, from, to);
00105   }
00106   animation_activated();
00107 }
00108 
00109 ////////////////////////////////////////////////////////////////////
00110 //     Function: AnimInterface::stop
00111 //       Access: Published
00112 //  Description: Stops a currently playing or looping animation right
00113 //               where it is.  The animation remains posed at the
00114 //               current frame.
00115 ////////////////////////////////////////////////////////////////////
00116 INLINE void AnimInterface::
00117 stop() {
00118   CDWriter cdata(_cycler);
00119   cdata->pose(cdata->get_full_fframe());
00120 
00121   // Don't call animation_activated() here; stopping an animation
00122   // should not activate it.
00123 }
00124 
00125 ////////////////////////////////////////////////////////////////////
00126 //     Function: AnimInterface::pose
00127 //       Access: Published
00128 //  Description: Sets the animation to the indicated frame and holds
00129 //               it there.
00130 ////////////////////////////////////////////////////////////////////
00131 INLINE void AnimInterface::
00132 pose(double frame) {
00133   {
00134     CDWriter cdata(_cycler);
00135     cdata->pose(frame);
00136   }
00137   animation_activated();
00138 }
00139 
00140 ////////////////////////////////////////////////////////////////////
00141 //     Function: AnimInterface::set_play_rate
00142 //       Access: Published
00143 //  Description: Changes the rate at which the animation plays.  1.0
00144 //               is the normal speed, 2.0 is twice normal speed, and
00145 //               0.5 is half normal speed.  0.0 is legal to pause the
00146 //               animation, and a negative value will play the
00147 //               animation backwards.
00148 ////////////////////////////////////////////////////////////////////
00149 INLINE void AnimInterface::
00150 set_play_rate(double play_rate) {
00151   CDWriter cdata(_cycler);
00152   cdata->internal_set_rate(cdata->_frame_rate, play_rate);
00153 }
00154 
00155 ////////////////////////////////////////////////////////////////////
00156 //     Function: AnimInterface::get_play_rate
00157 //       Access: Published
00158 //  Description: Returns the rate at which the animation plays.  See
00159 //               set_play_rate().
00160 ////////////////////////////////////////////////////////////////////
00161 INLINE double AnimInterface::
00162 get_play_rate() const {
00163   CDReader cdata(_cycler);
00164   return cdata->_play_rate;
00165 }
00166 
00167 ////////////////////////////////////////////////////////////////////
00168 //     Function: AnimInterface::get_frame_rate
00169 //       Access: Published
00170 //  Description: Returns the native frame rate of the animation.  This
00171 //               is the number of frames per second that will elapse
00172 //               when the play_rate is set to 1.0.  It is a fixed
00173 //               property of the animation and may not be adjusted by
00174 //               the user.
00175 ////////////////////////////////////////////////////////////////////
00176 INLINE double AnimInterface::
00177 get_frame_rate() const {
00178   CDReader cdata(_cycler);
00179   return cdata->_frame_rate;
00180 }
00181 
00182 ////////////////////////////////////////////////////////////////////
00183 //     Function: AnimInterface::get_frame
00184 //       Access: Published
00185 //  Description: Returns the current integer frame number.
00186 //               This number will be in the range 0 <= f <
00187 //               get_num_frames().
00188 ////////////////////////////////////////////////////////////////////
00189 INLINE int AnimInterface::
00190 get_frame() const {
00191   int num_frames = get_num_frames();
00192   if (num_frames <= 0) {
00193     return 0;
00194   }
00195   CDReader cdata(_cycler);
00196   return cmod(cdata->get_full_frame(0), num_frames);
00197 }
00198 
00199 ////////////////////////////////////////////////////////////////////
00200 //     Function: AnimInterface::get_next_frame
00201 //       Access: Published
00202 //  Description: Returns the current integer frame number + 1,
00203 //               constrained to the range 0 <= f < get_num_frames().
00204 //
00205 //               If the play mode is PM_play, this will clamp to the
00206 //               same value as get_frame() at the end of the
00207 //               animation.  If the play mode is any other value, this
00208 //               will wrap around to frame 0 at the end of the
00209 //               animation.
00210 ////////////////////////////////////////////////////////////////////
00211 INLINE int AnimInterface::
00212 get_next_frame() const {
00213   int num_frames = get_num_frames();
00214   if (num_frames <= 0) {
00215     return 0;
00216   }
00217   CDReader cdata(_cycler);
00218   return cmod(cdata->get_full_frame(1), num_frames);
00219 }
00220 
00221 ////////////////////////////////////////////////////////////////////
00222 //     Function: AnimInterface::get_frac
00223 //       Access: Published
00224 //  Description: Returns the fractional part of the current frame.
00225 //               Normally, this is in the range 0.0 <= f < 1.0, but in
00226 //               the one special case of an animation playing to its
00227 //               end frame and stopping, it might exactly equal 1.0.
00228 //
00229 //               It will always be true that get_full_frame() +
00230 //               get_frac() == get_full_fframe().
00231 ////////////////////////////////////////////////////////////////////
00232 INLINE double AnimInterface::
00233 get_frac() const {
00234   CDReader cdata(_cycler);
00235   return cdata->get_frac();
00236 }
00237 
00238 ////////////////////////////////////////////////////////////////////
00239 //     Function: AnimInterface::get_full_frame
00240 //       Access: Published
00241 //  Description: Returns the current integer frame number.
00242 //
00243 //               Unlike the value returned by get_frame(), this frame
00244 //               number may extend beyond the range of
00245 //               get_num_frames() if the frame range passed to play(),
00246 //               loop(), etc. did.
00247 //
00248 //               Unlike the value returned by get_full_fframe(), this
00249 //               return value will never exceed the value passed to
00250 //               to_frame in the play() method.
00251 ////////////////////////////////////////////////////////////////////
00252 INLINE int AnimInterface::
00253 get_full_frame() const {
00254   CDReader cdata(_cycler);
00255   return cdata->get_full_frame(0);
00256 }
00257 
00258 ////////////////////////////////////////////////////////////////////
00259 //     Function: AnimInterface::get_full_fframe
00260 //       Access: Published
00261 //  Description: Returns the current floating-point frame number.
00262 //
00263 //               Unlike the value returned by get_frame(), this frame
00264 //               number may extend beyond the range of
00265 //               get_num_frames() if the frame range passed to play(),
00266 //               loop(), etc. did.
00267 //
00268 //               Unlike the value returned by get_full_frame(), this
00269 //               return value may equal (to_frame + 1.0), when the
00270 //               animation has played to its natural end.  However, in
00271 //               this case the return value of get_full_frame() will
00272 //               be to_frame, not (to_frame + 1).
00273 ////////////////////////////////////////////////////////////////////
00274 INLINE double AnimInterface::
00275 get_full_fframe() const {
00276   CDReader cdata(_cycler);
00277   return cdata->get_full_fframe();
00278 }
00279 
00280 ////////////////////////////////////////////////////////////////////
00281 //     Function: AnimInterface::is_playing
00282 //       Access: Published
00283 //  Description: Returns true if the animation is currently playing,
00284 //               false if it is stopped (e.g. because stop() or pose()
00285 //               was called, or because it reached the end of the
00286 //               animation after play() was called).
00287 ////////////////////////////////////////////////////////////////////
00288 INLINE bool AnimInterface::
00289 is_playing() const {
00290   CDReader cdata(_cycler);
00291   return cdata->is_playing();
00292 }
00293 
00294 ////////////////////////////////////////////////////////////////////
00295 //     Function: AnimInterface::set_frame_rate
00296 //       Access: Protected
00297 //  Description: Should be called by a derived class to specify the
00298 //               native frame rate of the animation.  It is legal to
00299 //               call this after the animation has already started.
00300 ////////////////////////////////////////////////////////////////////
00301 INLINE void AnimInterface::
00302 set_frame_rate(double frame_rate) {
00303   CDWriter cdata(_cycler);
00304   cdata->internal_set_rate(frame_rate, cdata->_play_rate);
00305 }
00306 
00307 ////////////////////////////////////////////////////////////////////
00308 //     Function: AnimInterface::set_num_frames
00309 //       Access: Protected
00310 //  Description: Should be called by a derived class to specify the
00311 //               number of frames of the animation.  It is legal to
00312 //               call this after the animation has already started,
00313 //               but doing so may suddenly change the apparent current
00314 //               frame number.
00315 ////////////////////////////////////////////////////////////////////
00316 INLINE void AnimInterface::
00317 set_num_frames(int num_frames) {
00318   _num_frames = num_frames;
00319 }
00320 
00321 ////////////////////////////////////////////////////////////////////
00322 //     Function: AnimInterface::CData::get_frac
00323 //       Access: Published
00324 //  Description: Returns the fractional part of the current frame.
00325 //               Normally, this is in the range 0.0 <= f < 1.0, but in
00326 //               the one special case of an animation playing to its
00327 //               end frame and stopping, it might exactly equal 1.0.
00328 //
00329 //               It will always be true that get_full_frame() +
00330 //               get_frac() == get_full_fframe().
00331 ////////////////////////////////////////////////////////////////////
00332 INLINE double AnimInterface::CData::
00333 get_frac() const {
00334   return get_full_fframe() - (double)get_full_frame(0);
00335 }
00336 
00337 INLINE ostream &
00338 operator << (ostream &out, const AnimInterface &ai) {
00339   ai.output(out);
00340   return out;
00341 }
 All Classes Functions Variables Enumerations