]> git.mxchange.org Git - simgear.git/blob - simgear/props/PropertyInterpolator.hxx
New interpolation/animation system.
[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       /**
71        * Create new animation for given property.
72        *
73        * @param prop    Property to be animated
74        * @param target  Property containing target value
75        */
76       template<class Derived>
77       static PropertyInterpolator* create(const SGPropertyNode* target)
78       {
79         assert(target);
80
81         PropertyInterpolator* interp = new Derived;
82         interp->reset(target);
83
84         return interp;
85       }
86
87     protected:
88       friend class PropertyInterpolationMgr;
89
90       std::string               _type;
91       easing_func_t             _easing;
92       PropertyInterpolatorRef   _next;
93       double                    _duration,
94                                 _cur_t;
95
96       PropertyInterpolator();
97
98       virtual void setTarget(const SGPropertyNode* target) = 0;
99       virtual void init(const SGPropertyNode* prop) = 0;
100       virtual void write(SGPropertyNode* prop, double t) = 0;
101   };
102
103   class NumericInterpolator:
104     public PropertyInterpolator
105   {
106     protected:
107       double _end,
108              _diff;
109
110       virtual void setTarget(const SGPropertyNode* target);
111       virtual void init(const SGPropertyNode* prop);
112       virtual void write(SGPropertyNode* prop, double t);
113   };
114
115 } // namespace simgear
116
117
118 #endif /* SG_PROPERTY_INTERPOLATOR_HXX_ */