Claw  1.7.3
code/tween/single_tweener.cpp
Go to the documentation of this file.
00001 /*
00002   CLAW - a C++ Library Absolutely Wonderful
00003 
00004   CLAW is a free library without any particular aim but being useful to 
00005   anyone.
00006 
00007   Copyright (C) 2005-2011 Julien Jorge
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022   contact: julien.jorge@gamned.org
00023 */
00029 #include <claw/tween/single_tweener.hpp>
00030 
00031 #include <algorithm>
00032 #include <boost/bind.hpp>
00033 
00050 static void local_swap( double& a, double& b )
00051 {
00052   const double t(a);
00053   a = b;
00054   b = t;
00055 } // local_swap()
00056 
00057 #ifndef CLAW_TWEENER_DEFINE_BOOST_THROW_EXCEPTION
00058 
00059 #ifndef WORKAROUND_BOOST_THROW_EXCEPTION
00060 #define WORKAROUND_BOOST_THROW_EXCEPTION
00061 
00062 #include "boost/throw_exception.hpp"
00063 
00067 namespace boost {
00068 
00076   void throw_exception( std::exception const& exc ) {
00077     // nothing
00078   } // throw_exception()
00079 
00080 } // namespace boost
00081 
00082 #endif // WORKAROUND_BOOST_THROW_EXCEPTION
00083 #endif // ifdef CLAW_TWEENER_DEFINE_BOOST_THROW_EXCEPTION
00084 
00085 /*----------------------------------------------------------------------------*/
00089 claw::tween::single_tweener::single_tweener()
00090   : m_date(0), m_easing( easing_none::ease_in_out )
00091 {
00092 
00093 } // single_tweener::single_tweener()
00094 
00095 /*----------------------------------------------------------------------------*/
00104 claw::tween::single_tweener::single_tweener
00105 ( double init, double end, double duration, update_function callback,
00106   easing_function e )
00107   : m_init(init), m_end(end), m_date(0), m_duration(duration),
00108     m_callback(callback), m_easing(e)
00109 {
00110 
00111 } // single_tweener::single_tweener()
00112 
00113 /*----------------------------------------------------------------------------*/
00121 claw::tween::single_tweener::single_tweener
00122 ( double& val, double end, double duration, easing_function e )
00123   : m_init(val), m_end(end), m_date(0), m_duration(duration), m_easing(e)
00124 {
00125   m_callback = boost::bind( &local_swap, boost::ref(val), _1 );
00126 } // single_tweener::single_tweener()
00127 
00128 /*----------------------------------------------------------------------------*/
00132 double claw::tween::single_tweener::get_init() const
00133 {
00134   return m_init;
00135 } // single_tweener::get_init()
00136 
00137 /*----------------------------------------------------------------------------*/
00142 void claw::tween::single_tweener::set_init( double v )
00143 {
00144   m_init = v;
00145 } // single_tweener::set_init()
00146 
00147 /*----------------------------------------------------------------------------*/
00151 double claw::tween::single_tweener::get_end() const
00152 {
00153   return m_end;
00154 } // single_tweener::get_end()
00155 
00156 /*----------------------------------------------------------------------------*/
00161 void claw::tween::single_tweener::set_end( double v )
00162 {
00163   m_end = v;
00164 } // single_tweener::set_end()
00165 
00166 /*----------------------------------------------------------------------------*/
00170 double claw::tween::single_tweener::get_duration() const
00171 {
00172   return m_duration;
00173 } // single_tweener::get_duration()
00174 
00175 /*----------------------------------------------------------------------------*/
00180 void claw::tween::single_tweener::set_duration( double v )
00181 {
00182   m_duration = v;
00183 } // single_tweener::set_duration()
00184 
00185 /*----------------------------------------------------------------------------*/
00190 void claw::tween::single_tweener::set_callback( update_function f )
00191 {
00192   m_callback = f;
00193 } // single_tweener::set_callback()
00194 
00195 /*----------------------------------------------------------------------------*/
00200 void claw::tween::single_tweener::set_easing( easing_function f )
00201 {
00202   m_easing = f;
00203 } // single_tweener::set_easing()
00204 
00205 /*----------------------------------------------------------------------------*/
00209 double claw::tween::single_tweener::get_value() const
00210 {
00211   const double coeff = m_easing( m_date / m_duration );
00212   return m_init + coeff * (m_end - m_init);
00213 } // single_tweener::get_value()
00214 
00215 /*----------------------------------------------------------------------------*/
00219 claw::tween::single_tweener* claw::tween::single_tweener::do_clone() const
00220 {
00221   return new single_tweener(*this);
00222 } // single_tweener::do_clone()
00223 
00224 /*----------------------------------------------------------------------------*/
00228 bool claw::tween::single_tweener::do_is_finished() const
00229 {
00230   return m_date >= m_duration;
00231 } // single_tweener::do_is_finished()
00232 
00233 /*----------------------------------------------------------------------------*/
00238 double claw::tween::single_tweener::do_update( double dt )
00239 {
00240   const double t( std::min(m_duration - m_date, dt) );
00241   const double result = dt - t;
00242   m_date += t;
00243 
00244   const double val( get_value() );
00245 
00246   m_callback(val);
00247 
00248   return result;
00249 } // single_tweener::do_update()