1 // Adapter for interpolating different types of properties.
3 // Copyright (C) 2013 Thomas Geymayer <tomgey@gmail.com>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "PropertyInterpolator.hxx"
28 //----------------------------------------------------------------------------
29 PropertyInterpolator::~PropertyInterpolator()
34 //----------------------------------------------------------------------------
35 void PropertyInterpolator::reset(const SGPropertyNode* target)
41 //----------------------------------------------------------------------------
42 void PropertyInterpolator::setEasingFunction(easing_func_t easing)
44 _easing = easing ? easing : easing_functions[0].func;
47 //----------------------------------------------------------------------------
48 double PropertyInterpolator::update(SGPropertyNode* prop, double dt)
53 _cur_t += dt / _duration;
55 double unused = _cur_t - 1;
59 write(prop, _easing(_cur_t) );
62 // Reset timer to allow animation to be run again.
68 //----------------------------------------------------------------------------
69 PropertyInterpolator::PropertyInterpolator():
76 //----------------------------------------------------------------------------
77 void NumericInterpolator::setTarget(const SGPropertyNode* target)
79 _end = target->getDoubleValue();
82 //----------------------------------------------------------------------------
83 void NumericInterpolator::init(const SGPropertyNode* prop)
85 // If unable to get start value, immediately change to target value
86 double value_start = prop->getType() == props::NONE
88 : prop->getDoubleValue();
90 _diff = _end - value_start;
93 //----------------------------------------------------------------------------
94 void NumericInterpolator::write(SGPropertyNode* prop, double t)
96 double cur = _end - (1 - t) * _diff;
98 if( prop->getType() == props::INT || prop->getType() == props::LONG )
99 prop->setLongValue( static_cast<long>(std::floor(cur + 0.5)) );
101 prop->setDoubleValue(cur);
104 } // namespace simgear