single_tweener.cpp
Go to the documentation of this file.
/*
CLAW - a C++ Library Absolutely Wonderful
CLAW is a free library without any particular aim but being useful to
anyone.
Copyright (C) 2005-2011 Julien Jorge
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
contact: julien.jorge@gamned.org
*/
#include <claw/tween/single_tweener.hpp>
#include <algorithm>
#include <boost/bind.hpp>
static void local_swap( double& a, double& b )
{
const double t(a);
a = b;
b = t;
} // local_swap()
#ifndef CLAW_TWEENER_DEFINE_BOOST_THROW_EXCEPTION
#ifndef WORKAROUND_BOOST_THROW_EXCEPTION
#define WORKAROUND_BOOST_THROW_EXCEPTION
#include "boost/throw_exception.hpp"
namespace boost {
void throw_exception( std::exception const& exc ) {
// nothing
} // throw_exception()
} // namespace boost
#endif // WORKAROUND_BOOST_THROW_EXCEPTION
#endif // ifdef CLAW_TWEENER_DEFINE_BOOST_THROW_EXCEPTION
/*----------------------------------------------------------------------------*/
claw::tween::single_tweener::single_tweener()
: m_date(0), m_easing( easing_none::ease_in_out )
{
} // single_tweener::single_tweener()
/*----------------------------------------------------------------------------*/
claw::tween::single_tweener::single_tweener
( double init, double end, double duration, update_function callback,
easing_function e )
: m_init(init), m_end(end), m_date(0), m_duration(duration),
m_callback(callback), m_easing(e)
{
} // single_tweener::single_tweener()
/*----------------------------------------------------------------------------*/
claw::tween::single_tweener::single_tweener
( double& val, double end, double duration, easing_function e )
: m_init(val), m_end(end), m_date(0), m_duration(duration), m_easing(e)
{
//m_callback = boost::bind( &local_swap, boost::ref(val), _1 );
} // single_tweener::single_tweener()
/*----------------------------------------------------------------------------*/
double claw::tween::single_tweener::get_init() const
{
return m_init;
} // single_tweener::get_init()
/*----------------------------------------------------------------------------*/
void claw::tween::single_tweener::set_init( double v )
{
m_init = v;
} // single_tweener::set_init()
/*----------------------------------------------------------------------------*/
double claw::tween::single_tweener::get_end() const
{
return m_end;
} // single_tweener::get_end()
/*----------------------------------------------------------------------------*/
void claw::tween::single_tweener::set_end( double v )
{
m_end = v;
} // single_tweener::set_end()
/*----------------------------------------------------------------------------*/
double claw::tween::single_tweener::get_duration() const
{
return m_duration;
} // single_tweener::get_duration()
/*----------------------------------------------------------------------------*/
void claw::tween::single_tweener::set_duration( double v )
{
m_duration = v;
} // single_tweener::set_duration()
/*----------------------------------------------------------------------------*/
void claw::tween::single_tweener::set_callback( update_function f )
{
m_callback = f;
} // single_tweener::set_callback()
/*----------------------------------------------------------------------------*/
void claw::tween::single_tweener::set_easing( easing_function f )
{
m_easing = f;
} // single_tweener::set_easing()
/*----------------------------------------------------------------------------*/
double claw::tween::single_tweener::get_value() const
{
const double coeff = m_easing( m_date / m_duration );
return m_init + coeff * (m_end - m_init);
} // single_tweener::get_value()
/*----------------------------------------------------------------------------*/
claw::tween::single_tweener* claw::tween::single_tweener::do_clone() const
{
return new single_tweener(*this);
} // single_tweener::do_clone()
/*----------------------------------------------------------------------------*/
bool claw::tween::single_tweener::do_is_finished() const
{
return m_date >= m_duration;
} // single_tweener::do_is_finished()
/*----------------------------------------------------------------------------*/
double claw::tween::single_tweener::do_update( double dt )
{
const double t( std::min(m_duration - m_date, dt) );
const double result = dt - t;
m_date += t;
const double val( get_value() );
m_callback(val);
return result;
} // single_tweener::do_update()
claw::tween::easing_none
Easing functions for the tweener. Those functions do nothing.
Definition easing_none.hpp:41
claw::tween::single_tweener
A single_tweener makes a value to evolve through time from a initial value to an end value according ...
Definition single_tweener.hpp:50
claw::tween::single_tweener::set_duration
void set_duration(double v)
Sets the total duration.
Definition single_tweener.cpp:180
claw::tween::single_tweener::get_end
double get_end() const
Gets the final value.
Definition single_tweener.cpp:151
claw::tween::single_tweener::set_end
void set_end(double v)
Sets the final value.
Definition single_tweener.cpp:161
claw::tween::single_tweener::single_tweener
single_tweener()
Default constructor.
Definition single_tweener.cpp:89
claw::tween::single_tweener::set_easing
void set_easing(easing_function f)
The function used to compute the new value.
Definition single_tweener.cpp:200
claw::tween::single_tweener::easing_function
boost::function< double(double)> easing_function
The type of the function used to compute the new value.
Definition single_tweener.hpp:57
claw::tween::single_tweener::get_init
double get_init() const
Gets the initial value.
Definition single_tweener.cpp:132
claw::tween::single_tweener::set_init
void set_init(double v)
Sets the initial value.
Definition single_tweener.cpp:142
claw::tween::single_tweener::get_duration
double get_duration() const
Gets the total duration.
Definition single_tweener.cpp:170
claw::tween::single_tweener::set_callback
void set_callback(update_function f)
The function called when the single_tweener is updated.
Definition single_tweener.cpp:190
claw::tween::single_tweener::update_function
boost::function< void(double)> update_function
The type of the function called when the single_tweener is updated.
Definition single_tweener.hpp:54
claw::tween::single_tweener::get_value
double get_value() const
Gets the current value of the tweener.
Definition single_tweener.cpp:209
boost
Redefinition of some boost elements, required on some compilers.
Definition single_tweener.cpp:67
boost::throw_exception
void throw_exception(std::exception const &exc)
boost::throw_exception definition, Needed on XCode compiler. This implementation does nothing.
Definition single_tweener.cpp:76
single_tweener.hpp
A single_tweener makes a value to evolve through time from a initial value to an end value according ...