Claw 1.7.0
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 
00034 /*----------------------------------------------------------------------------*/
00038 claw::tween::single_tweener::single_tweener()
00039   : m_date(0), m_easing( easing_none::ease_in_out )
00040 {
00041 
00042 } // single_tweener::single_tweener()
00043 
00044 /*----------------------------------------------------------------------------*/
00053 claw::tween::single_tweener::single_tweener
00054 ( double init, double end, double duration, update_function callback,
00055   easing_function e )
00056   : m_init(init), m_end(end), m_date(0), m_duration(duration),
00057     m_callback(callback), m_easing(e)
00058 {
00059 
00060 } // single_tweener::single_tweener()
00061 
00062 /*----------------------------------------------------------------------------*/
00070 claw::tween::single_tweener::single_tweener
00071 ( double& val, double end, double duration, easing_function e )
00072   : m_init(val), m_end(end), m_date(0), m_duration(duration), m_easing(e)
00073 {
00074   m_callback = boost::bind( &std::swap<double>, boost::ref(val), _1 );
00075 } // single_tweener::single_tweener()
00076 
00077 /*----------------------------------------------------------------------------*/
00082 void claw::tween::single_tweener::set_init( double v )
00083 {
00084   m_init = v;
00085 } // single_tweener::set_init()
00086 
00087 /*----------------------------------------------------------------------------*/
00092 void claw::tween::single_tweener::set_end( double v )
00093 {
00094   m_end = v;
00095 } // single_tweener::set_end()
00096 
00097 /*----------------------------------------------------------------------------*/
00102 void claw::tween::single_tweener::set_duration( double v )
00103 {
00104   m_duration = v;
00105 } // single_tweener::set_duration()
00106 
00107 /*----------------------------------------------------------------------------*/
00112 void claw::tween::single_tweener::set_callback( update_function f )
00113 {
00114   m_callback = f;
00115 } // single_tweener::set_callback()
00116 
00117 /*----------------------------------------------------------------------------*/
00122 void claw::tween::single_tweener::set_easing( easing_function f )
00123 {
00124   m_easing = f;
00125 } // single_tweener::set_easing()
00126 
00127 /*----------------------------------------------------------------------------*/
00131 claw::tween::single_tweener* claw::tween::single_tweener::do_clone() const
00132 {
00133   return new single_tweener(*this);
00134 } // single_tweener::do_clone()
00135 
00136 /*----------------------------------------------------------------------------*/
00140 bool claw::tween::single_tweener::do_is_finished() const
00141 {
00142   return m_date >= m_duration;
00143 } // single_tweener::do_is_finished()
00144 
00145 /*----------------------------------------------------------------------------*/
00150 double claw::tween::single_tweener::do_update( double dt )
00151 {
00152   const double t( std::min(m_duration - m_date, dt) );
00153   const double result = dt - t;
00154   m_date += t;
00155 
00156   const double coeff = m_easing( m_date / m_duration );
00157   const double val = m_init + coeff * (m_end - m_init);
00158 
00159   m_callback(val);
00160 
00161   return result;
00162 } // single_tweener::do_update()