Claw  1.7.0
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 
34 /*----------------------------------------------------------------------------*/
39  : m_date(0), m_easing( easing_none::ease_in_out )
40 {
41 
42 } // single_tweener::single_tweener()
43 
44 /*----------------------------------------------------------------------------*/
54 ( double init, double end, double duration, update_function callback,
55  easing_function e )
56  : m_init(init), m_end(end), m_date(0), m_duration(duration),
57  m_callback(callback), m_easing(e)
58 {
59 
60 } // single_tweener::single_tweener()
61 
62 /*----------------------------------------------------------------------------*/
71 ( double& val, double end, double duration, easing_function e )
72  : m_init(val), m_end(end), m_date(0), m_duration(duration), m_easing(e)
73 {
74  m_callback = boost::bind( &std::swap<double>, boost::ref(val), _1 );
75 } // single_tweener::single_tweener()
76 
77 /*----------------------------------------------------------------------------*/
83 {
84  m_init = v;
85 } // single_tweener::set_init()
86 
87 /*----------------------------------------------------------------------------*/
93 {
94  m_end = v;
95 } // single_tweener::set_end()
96 
97 /*----------------------------------------------------------------------------*/
103 {
104  m_duration = v;
105 } // single_tweener::set_duration()
106 
107 /*----------------------------------------------------------------------------*/
113 {
114  m_callback = f;
115 } // single_tweener::set_callback()
116 
117 /*----------------------------------------------------------------------------*/
123 {
124  m_easing = f;
125 } // single_tweener::set_easing()
126 
127 /*----------------------------------------------------------------------------*/
131 claw::tween::single_tweener* claw::tween::single_tweener::do_clone() const
132 {
133  return new single_tweener(*this);
134 } // single_tweener::do_clone()
135 
136 /*----------------------------------------------------------------------------*/
140 bool claw::tween::single_tweener::do_is_finished() const
141 {
142  return m_date >= m_duration;
143 } // single_tweener::do_is_finished()
144 
145 /*----------------------------------------------------------------------------*/
150 double claw::tween::single_tweener::do_update( double dt )
151 {
152  const double t( std::min(m_duration - m_date, dt) );
153  const double result = dt - t;
154  m_date += t;
155 
156  const double coeff = m_easing( m_date / m_duration );
157  const double val = m_init + coeff * (m_end - m_init);
158 
159  m_callback(val);
160 
161  return result;
162 } // single_tweener::do_update()