Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/lerp/lerp.cxx

Go to the documentation of this file.
00001 // Filename: lerp.cxx
00002 // Created by:  frang (30May00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 
00020 #include "lerp.h"
00021 
00022 #include <clockObject.h>
00023 #include <throw_event.h>
00024 
00025 TypeHandle Lerp::_type_handle;
00026 TypeHandle AutonomousLerp::_type_handle;
00027 
00028 static INLINE float scale_t(float t, float start, float end) {
00029   float ret = t;
00030   if (ret < start)
00031     ret = start;
00032   if (ret > end)
00033     ret = end;
00034   // Avoid a possible divide-by-zero
00035   if (end == 0.0f)
00036       return 0.0f;
00037   return ret / end;
00038 }
00039 
00040 Lerp::Lerp(LerpFunctor* func, float endt, LerpBlendType* blend)
00041   : _blend(blend), _func(func), _startt(0.), _endt(endt),
00042     _delta(1.), _t(0.) {}
00043 
00044 Lerp::Lerp(LerpFunctor* func, float startt, float endt,
00045            LerpBlendType* blend) : _blend(blend), _func(func),
00046                                    _startt(startt),
00047                                    _endt(endt),
00048                                    _delta(1.),
00049                                    _t(startt) {}
00050 
00051 Lerp::Lerp(const Lerp& c) : _blend(c._blend), _func(c._func), _event(c._event),
00052                             _startt(c._startt), _endt(c._endt),
00053                             _delta(c._delta), _t(c._t) {}
00054 
00055 Lerp::~Lerp(void) {}
00056 
00057 Lerp& Lerp::operator=(const Lerp& c) {
00058   _blend = c._blend;
00059   _func = c._func;
00060   _event = c._event;
00061   _startt = c._startt;
00062   _endt = c._endt;
00063   _delta = c._delta;
00064   _t = c._t;
00065   return *this;
00066 }
00067 
00068 void Lerp::step(void) {
00069   _t += _delta;
00070   if (is_done()) {
00071     (*_func)(1.0);
00072     if (!_event.empty()) {
00073       throw_event(_event);
00074     }
00075   } else {
00076     float t = scale_t(_t, _startt, _endt);
00077     t = (_blend==(LerpBlendType*)0L)?t:(*_blend)(t);
00078     (*_func)(t);
00079   }
00080 }
00081 
00082 void Lerp::set_step_size(float delta) {
00083   _delta = delta;
00084 }
00085 
00086 float Lerp::get_step_size(void) const {
00087   return _delta;
00088 }
00089 
00090 void Lerp::set_t(float t) {
00091   _t = t;
00092   float x = scale_t(_t, _startt, _endt);
00093   x = (_blend==(LerpBlendType*)0L)?x:(*_blend)(x);
00094   (*_func)(x);
00095 }
00096 
00097 float Lerp::get_t(void) const {
00098   return _t;
00099 }
00100 
00101 bool Lerp::is_done(void) const {
00102   return (_t >= _endt);
00103 }
00104 
00105 LerpFunctor* Lerp::get_functor(void) const {
00106   return _func;
00107 }
00108 
00109 void Lerp::set_end_event(const std::string& event) {
00110   _event = event;
00111 }
00112 
00113 std::string Lerp::get_end_event(void) const {
00114   return _event;
00115 }
00116 
00117 AutonomousLerp::AutonomousLerp(LerpFunctor* func, float endt,
00118                                LerpBlendType* blend, EventHandler* handler)
00119   : _blend(blend), _func(func), _handler(handler),
00120     _startt(0.), _endt(endt), _t(0.) {}
00121 
00122 AutonomousLerp::AutonomousLerp(LerpFunctor* func, float startt, float endt,
00123                                LerpBlendType* blend, EventHandler* handler)
00124   : _blend(blend), _func(func), _handler(handler),
00125     _startt(startt), _endt(endt), _t(startt) {}
00126 
00127 AutonomousLerp::AutonomousLerp(const AutonomousLerp& c) : _blend(c._blend),
00128                                                           _func(c._func),
00129                                                           _handler(c._handler),
00130                                                           _event(c._event),
00131                                                           _startt(c._startt),
00132                                                           _endt(c._endt),
00133                                                           _t(c._t) {}
00134 
00135 AutonomousLerp::~AutonomousLerp(void) {}
00136 
00137 AutonomousLerp& AutonomousLerp::operator=(const AutonomousLerp& c) {
00138   _blend = c._blend;
00139   _func = c._func;
00140   _handler = c._handler;
00141   _event = c._event;
00142   _startt = c._startt;
00143   _endt = c._endt;
00144   _t = c._t;
00145   return *this;
00146 }
00147 
00148 void AutonomousLerp::start(void) {
00149   _t = _startt;
00150   _handler->add_hook("NewFrame", handle_event, this);
00151 }
00152 
00153 void AutonomousLerp::stop(void) {
00154   _handler->remove_hook("NewFrame", handle_event, this);
00155 }
00156 
00157 void AutonomousLerp::resume(void) {
00158   _handler->add_hook("NewFrame", handle_event, this);
00159 }
00160 
00161 bool AutonomousLerp::is_done(void) const {
00162   return (_t >= _endt);
00163 }
00164 
00165 LerpFunctor* AutonomousLerp::get_functor(void) const {
00166   return _func;
00167 }
00168 
00169 void AutonomousLerp::set_t(float t) {
00170   _t = t;
00171   float x = scale_t(_t, _startt, _endt);
00172   x = (_blend==(LerpBlendType*)0L)?x:(*_blend)(x);
00173   (*_func)(x);
00174 }
00175 
00176 float AutonomousLerp::get_t(void) const {
00177   return _t;
00178 }
00179 
00180 void AutonomousLerp::set_end_event(const std::string& event) {
00181   _event = event;
00182 }
00183 
00184 std::string AutonomousLerp::get_end_event(void) const {
00185   return _event;
00186 }
00187 
00188 void AutonomousLerp::step(void) {
00189   // Probably broken because it does not set the final value when t
00190   // exceeds end_t. see fixed Lerp::step() above
00191   if (is_done()) {
00192     stop();
00193     return;
00194   }
00195   float delta = ClockObject::get_global_clock()->get_dt();
00196   _t += delta;
00197   float t = scale_t(_t, _startt, _endt);
00198   t = (_blend==(LerpBlendType*)0L)?t:(*_blend)(t);
00199   (*_func)(t);
00200   if (is_done() && !_event.empty())
00201     throw_event(_event);
00202 }
00203 
00204 void AutonomousLerp::handle_event(CPT(Event), void* data) {
00205   AutonomousLerp* l = (AutonomousLerp*)data;
00206   l->step();
00207 }

Generated on Fri May 2 00:39:51 2003 for Panda by doxygen1.3