]> git.mxchange.org Git - simgear.git/blob - simgear/props/PropertyInterpolationMgr.hxx
Update doxgen config and some comments.
[simgear.git] / simgear / props / PropertyInterpolationMgr.hxx
1 // Subsystem that manages interpolation 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_INTERPOLATION_MGR_HXX_
20 #define SG_PROPERTY_INTERPOLATION_MGR_HXX_
21
22 #include "PropertyInterpolator.hxx"
23
24 #include <simgear/math/sg_types.hxx>
25 #include <simgear/misc/make_new.hxx>
26 #include <simgear/props/props.hxx>
27 #include <simgear/structure/subsystem_mgr.hxx>
28
29 #include <list>
30
31 namespace simgear
32 {
33
34   /**
35    * Subsystem that manages interpolation of properties.
36    *
37    * By default the numeric values of the properties are interpolated. For
38    * example, for strings this is probably not the wanted behavior. For this
39    * adapter classes can be registered to allow providing specific
40    * interpolations for certain types of properties. Using the type "color",
41    * provided by ColorInterpolator, strings containing %CSS colors can also be
42    * interpolated.
43    *
44    * Additionally different functions can be used for easing of the animation.
45    * By default "linear" (constant animation speed) and "swing" (smooth
46    * acceleration and deceleration) are available.
47    */
48   class PropertyInterpolationMgr:
49     public SGSubsystem
50   {
51     public:
52       typedef PropertyInterpolator* (*InterpolatorFactory)();
53
54       PropertyInterpolationMgr();
55
56       /**
57        * Update all active interpolators.
58        */
59       void update(double dt);
60
61       /**
62        * Create a new property interpolator.
63        *
64        * @note To actually use it the interpolator needs to be attached to a
65        *       property using PropertyInterpolationMgr::interpolate.
66        *
67        * @param type        Type of animation ("numeric", "color", etc.)
68        * @param target      Property containing target value
69        * @param duration    Duration if the animation (in seconds)
70        * @param easing      Type of easing ("linear", "swing", etc.)
71        */
72       PropertyInterpolator*
73       createInterpolator( const std::string& type,
74                           const SGPropertyNode& target,
75                           double duration,
76                           const std::string& easing );
77
78       /**
79        * Add animation of the given property from its current value to the
80        * target value of the interpolator. If no interpolator is given any
81        * existing animation of the given property is aborted.
82        *
83        * @param prop    Property to be interpolated
84        * @param interp  Interpolator used for interpolation
85        */
86       bool interpolate( SGPropertyNode* prop,
87                         PropertyInterpolatorRef interp  = 0 );
88
89       bool interpolate( SGPropertyNode* prop,
90                         const std::string& type,
91                         const SGPropertyNode& target,
92                         double duration,
93                         const std::string& easing );
94
95       bool interpolate( SGPropertyNode* prop,
96                         const std::string& type,
97                         const PropertyList& values,
98                         const double_list& deltas,
99                         const std::string& easing );
100
101       /**
102        * Register factory for interpolation type.
103        */
104       void addInterpolatorFactory( const std::string& type,
105                                    InterpolatorFactory factory );
106       template<class T>
107       void addInterpolatorFactory(const std::string& type)
108       {
109         addInterpolatorFactory
110         (
111           type,
112           &simgear::make_new_derived<PropertyInterpolator, T>
113         );
114       }
115
116       /**
117        * Register easing function.
118        */
119       void addEasingFunction(const std::string& type, easing_func_t func);
120
121       /**
122        * Set property containing real time delta (not sim time)
123        *
124        * TODO better pass both deltas to all update methods...
125        */
126       void setRealtimeProperty(SGPropertyNode* node);
127
128     protected:
129
130       typedef std::map<std::string, InterpolatorFactory> InterpolatorFactoryMap;
131       typedef std::map<std::string, easing_func_t> EasingFunctionMap;
132       typedef std::pair< SGPropertyNode*,
133                          PropertyInterpolatorRef > PropertyInterpolatorPair;
134       typedef std::list<PropertyInterpolatorPair> InterpolatorList;
135
136       struct PredicateIsSameProp;
137
138       InterpolatorFactoryMap    _interpolator_factories;
139       EasingFunctionMap         _easing_functions;
140       InterpolatorList          _interpolators;
141
142       SGPropertyNode_ptr        _rt_prop;
143   };
144
145 } // namespace simgear
146
147 #endif /* SG_PROPERTY_INTERPOLATION_MGR_HXX_ */