]> git.mxchange.org Git - simgear.git/blob - simgear/props/PropertyInterpolator.cxx
cppbind: automatic conversion of SGReferenced derived pointers.
[simgear.git] / simgear / props / PropertyInterpolator.cxx
1 // Adapter for interpolating different types of properties.
2 //
3 // Copyright (C) 2013  Thomas Geymayer <tomgey@gmail.com>
4 //
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.
9 //
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.
14 //
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
18
19 #include "PropertyInterpolator.hxx"
20 #include "props.hxx"
21
22 #include <cassert>
23 #include <cmath>
24
25 namespace simgear
26 {
27
28   //----------------------------------------------------------------------------
29   PropertyInterpolator::~PropertyInterpolator()
30   {
31
32   }
33
34   //----------------------------------------------------------------------------
35   void PropertyInterpolator::reset(const SGPropertyNode& target)
36   {
37     _cur_t = 0;
38     setTarget(target);
39   }
40
41   //----------------------------------------------------------------------------
42   void PropertyInterpolator::setEasingFunction(easing_func_t easing)
43   {
44     _easing = easing ? easing : easing_functions[0].func;
45   }
46
47   //----------------------------------------------------------------------------
48   double PropertyInterpolator::update(SGPropertyNode& prop, double dt)
49   {
50     if( _cur_t == 0 )
51       init(prop);
52
53     _cur_t += dt / _duration;
54
55     double unused = _cur_t - 1;
56     if( unused > 0 )
57       _cur_t = 1;
58
59     write(prop, _easing(_cur_t) );
60
61     if( _cur_t == 1 )
62       // Reset timer to allow animation to be run again.
63       _cur_t = 0;
64
65     return unused;
66   }
67
68   //----------------------------------------------------------------------------
69   PropertyInterpolator::PropertyInterpolator():
70     _duration(1),
71     _cur_t(0)
72   {
73     setEasingFunction(0);
74   }
75
76   //----------------------------------------------------------------------------
77   void NumericInterpolator::setTarget(const SGPropertyNode& target)
78   {
79     _end = target.getDoubleValue();
80   }
81
82   //----------------------------------------------------------------------------
83   void NumericInterpolator::init(const SGPropertyNode& prop)
84   {
85     // If unable to get start value, immediately change to target value
86     double value_start = prop.getType() == props::NONE
87                        ? _end
88                        : prop.getDoubleValue();
89
90     _diff = _end - value_start;
91   }
92
93   //----------------------------------------------------------------------------
94   void NumericInterpolator::write(SGPropertyNode& prop, double t)
95   {
96     double cur = _end - (1 - t) * _diff;
97
98     if( prop.getType() == props::INT || prop.getType() == props::LONG )
99       prop.setLongValue( static_cast<long>(std::floor(cur + 0.5)) );
100     else
101       prop.setDoubleValue(cur);
102   }
103
104 } // namespace simgear