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