Claw  1.7.3
single_tweener.cpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  contact: julien.jorge@gamned.org
23 */
30 
31 #include <algorithm>
32 #include <boost/bind.hpp>
33 
50 static void local_swap( double& a, double& b )
51 {
52  const double t(a);
53  a = b;
54  b = t;
55 } // local_swap()
56 
57 #ifndef CLAW_TWEENER_DEFINE_BOOST_THROW_EXCEPTION
58 
59 #ifndef WORKAROUND_BOOST_THROW_EXCEPTION
60 #define WORKAROUND_BOOST_THROW_EXCEPTION
61 
62 #include "boost/throw_exception.hpp"
63 
67 namespace boost {
68 
76  void throw_exception( std::exception const& exc ) {
77  // nothing
78  } // throw_exception()
79 
80 } // namespace boost
81 
82 #endif // WORKAROUND_BOOST_THROW_EXCEPTION
83 #endif // ifdef CLAW_TWEENER_DEFINE_BOOST_THROW_EXCEPTION
84 
85 /*----------------------------------------------------------------------------*/
90  : m_date(0), m_easing( easing_none::ease_in_out )
91 {
92 
93 } // single_tweener::single_tweener()
94 
95 /*----------------------------------------------------------------------------*/
105 ( double init, double end, double duration, update_function callback,
106  easing_function e )
107  : m_init(init), m_end(end), m_date(0), m_duration(duration),
108  m_callback(callback), m_easing(e)
109 {
110 
111 } // single_tweener::single_tweener()
112 
113 /*----------------------------------------------------------------------------*/
122 ( double& val, double end, double duration, easing_function e )
123  : m_init(val), m_end(end), m_date(0), m_duration(duration), m_easing(e)
124 {
125  //m_callback = boost::bind( &local_swap, boost::ref(val), _1 );
126 } // single_tweener::single_tweener()
127 
128 /*----------------------------------------------------------------------------*/
133 {
134  return m_init;
135 } // single_tweener::get_init()
136 
137 /*----------------------------------------------------------------------------*/
143 {
144  m_init = v;
145 } // single_tweener::set_init()
146 
147 /*----------------------------------------------------------------------------*/
152 {
153  return m_end;
154 } // single_tweener::get_end()
155 
156 /*----------------------------------------------------------------------------*/
162 {
163  m_end = v;
164 } // single_tweener::set_end()
165 
166 /*----------------------------------------------------------------------------*/
171 {
172  return m_duration;
173 } // single_tweener::get_duration()
174 
175 /*----------------------------------------------------------------------------*/
181 {
182  m_duration = v;
183 } // single_tweener::set_duration()
184 
185 /*----------------------------------------------------------------------------*/
191 {
192  m_callback = f;
193 } // single_tweener::set_callback()
194 
195 /*----------------------------------------------------------------------------*/
201 {
202  m_easing = f;
203 } // single_tweener::set_easing()
204 
205 /*----------------------------------------------------------------------------*/
210 {
211  const double coeff = m_easing( m_date / m_duration );
212  return m_init + coeff * (m_end - m_init);
213 } // single_tweener::get_value()
214 
215 /*----------------------------------------------------------------------------*/
219 claw::tween::single_tweener* claw::tween::single_tweener::do_clone() const
220 {
221  return new single_tweener(*this);
222 } // single_tweener::do_clone()
223 
224 /*----------------------------------------------------------------------------*/
228 bool claw::tween::single_tweener::do_is_finished() const
229 {
230  return m_date >= m_duration;
231 } // single_tweener::do_is_finished()
232 
233 /*----------------------------------------------------------------------------*/
238 double claw::tween::single_tweener::do_update( double dt )
239 {
240  const double t( std::min(m_duration - m_date, dt) );
241  const double result = dt - t;
242  m_date += t;
243 
244  const double val( get_value() );
245 
246  m_callback(val);
247 
248  return result;
249 } // single_tweener::do_update()
void set_init(double v)
Sets the initial value.
boost::function< void(double)> update_function
The type of the function called when the single_tweener is updated.
Redefinition of some boost elements, required on some compilers.
boost::function< double(double)> easing_function
The type of the function used to compute the new value.
double get_value() const
Gets the current value of the tweener.
A single_tweener makes a value to evolve through time from a initial value to an end value according ...
void set_duration(double v)
Sets the total duration.
void set_callback(update_function f)
The function called when the single_tweener is updated.
double get_duration() const
Gets the total duration.
double get_init() const
Gets the initial value.
void set_end(double v)
Sets the final value.
single_tweener()
Default constructor.
A single_tweener makes a value to evolve through time from a initial value to an end value according ...
Easing functions for the tweener. Those functions do nothing.
Definition: easing_none.hpp:40
void set_easing(easing_function f)
The function used to compute the new value.
double get_end() const
Gets the final value.
void throw_exception(std::exception const &exc)
boost::throw_exception definition, Needed on XCode compiler. This implementation does nothing...