]> git.mxchange.org Git - simgear.git/blob - simgear/props/PropertyInterpolator.hxx
Fixed a crash: the singleton needs to be instantiated the first time SGCommandMgr...
[simgear.git] / simgear / props / PropertyInterpolator.hxx
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 #ifndef SG_PROPERTY_INTERPOLATOR_HXX_
20 #define SG_PROPERTY_INTERPOLATOR_HXX_
21
22 #include "easing_functions.hxx"
23 #include "propsfwd.hxx"
24
25 #include <memory>
26 #include <cassert>
27 #include <string>
28
29 #include <iostream>
30
31 namespace simgear
32 {
33
34   class PropertyInterpolator;
35   typedef SGSharedPtr<PropertyInterpolator> PropertyInterpolatorRef;
36
37   /**
38    * Base class for interpolating different types of properties over time.
39    */
40   class PropertyInterpolator:
41     public SGReferenced
42   {
43     public:
44       virtual ~PropertyInterpolator();
45
46       /**
47        * Resets animation timer to zero and prepares for interpolation to new
48        * target value.
49        */
50       void reset(const SGPropertyNode& target);
51
52       /**
53        * Set easing function to be used for interpolation.
54        */
55       void setEasingFunction(easing_func_t easing);
56
57       /**
58        * Calculate an animation step.
59        *
60        * @param prop    Property being animated
61        * @param dt      Current frame duration
62        * @return Time not used by the animation (>= 0 if animation has finished,
63        *         else time is negative indicating the remaining time until
64        *         finished)
65        */
66       double update(SGPropertyNode& prop, double dt);
67
68       const std::string& getType() const    { return _type; }
69
70     protected:
71       friend class PropertyInterpolationMgr;
72
73       std::string               _type;
74       easing_func_t             _easing;
75       PropertyInterpolatorRef   _next;
76       double                    _duration,
77                                 _cur_t;
78
79       PropertyInterpolator();
80
81       virtual void setTarget(const SGPropertyNode& target) = 0;
82       virtual void init(const SGPropertyNode& prop) = 0;
83       virtual void write(SGPropertyNode& prop, double t) = 0;
84   };
85
86   class NumericInterpolator:
87     public PropertyInterpolator
88   {
89     protected:
90       double _end,
91              _diff;
92
93       virtual void setTarget(const SGPropertyNode& target);
94       virtual void init(const SGPropertyNode& prop);
95       virtual void write(SGPropertyNode& prop, double t);
96   };
97
98 } // namespace simgear
99
100
101 #endif /* SG_PROPERTY_INTERPOLATOR_HXX_ */