Merge "Added api function to specify speed factor of an animation" into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / progress-value.h
1 #ifndef __DALI_INTERNAL_PROGRESS_VALUE_H__
2 #define __DALI_INTERNAL_PROGRESS_VALUE_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/math/quaternion.h>
23 #include <dali/public-api/math/vector3.h>
24 #include <dali/public-api/math/radian.h>
25 #include <dali/public-api/math/degree.h>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 /**
34  * Progress / value pair for animating channels (properties) with keyframes
35  */
36 template <typename T>
37 class ProgressValue
38 {
39 public:
40   ProgressValue (float progress, T value)
41   : mProgress(progress),
42     mValue (value)
43   {
44   }
45
46   ~ProgressValue ()
47   {
48   }
49
50   float GetProgress () const
51   {
52     return mProgress;
53   }
54
55   const T& GetValue () const
56   {
57     return mValue;
58   }
59
60 public:
61   float mProgress;   ///< Progress this value applies to animation channel
62   T mValue;          ///< value this animation channel should take
63 };
64
65 typedef ProgressValue<Quaternion>                       ProgressQuaternion;
66 typedef std::vector<ProgressQuaternion>                 ProgressQuaternionContainer;
67
68 typedef ProgressValue<AngleAxis>                        ProgressAngleAxis;
69 typedef std::vector<AngleAxis>                          ProgressAngleAxisContainer;
70
71 typedef ProgressValue<bool>                             ProgressBoolean;
72 typedef std::vector<ProgressBoolean>                    ProgressBooleanContainer;
73
74 typedef ProgressValue<float>                            ProgressNumber;
75 typedef std::vector<ProgressNumber>                     ProgressNumberContainer;
76
77 typedef ProgressValue<int>                              ProgressInteger;
78 typedef std::vector<ProgressInteger>                    ProgressIntegerContainer;
79
80 typedef ProgressValue<Vector2>                          ProgressVector2;
81 typedef std::vector<ProgressVector2>                    ProgressVector2Container;
82
83 typedef ProgressValue<Vector3>                          ProgressVector3;
84 typedef std::vector<ProgressVector3>                    ProgressVector3Container;
85
86 typedef ProgressValue<Vector4>                          ProgressVector4;
87 typedef std::vector<ProgressVector4>                    ProgressVector4Container;
88
89 inline Quaternion Interpolate (ProgressQuaternion& a, ProgressQuaternion& b, float progress)
90 {
91   return Quaternion::Slerp(a.GetValue(), b.GetValue(), progress);
92 }
93
94 inline AngleAxis Interpolate (ProgressAngleAxis& a, ProgressAngleAxis& b, float progress)
95 {
96   AngleAxis av(a.GetValue());
97   AngleAxis bv(b.GetValue());
98   Quaternion q1(Radian(av.angle), av.axis);
99   Quaternion q2(Radian(bv.angle), bv.axis);
100
101   Quaternion iq = Quaternion::Slerp(q1, q2, progress);
102   AngleAxis result;
103   iq.ToAxisAngle(result.axis, result.angle);
104   return result;
105 }
106
107
108 inline bool Interpolate (ProgressBoolean& a, ProgressBoolean& b, float progress)
109 {
110   return progress < 0.5f ? a.GetValue() : b.GetValue();
111 }
112
113 inline float Interpolate (ProgressNumber& a, ProgressNumber& b, float progress)
114 {
115   return (a.GetValue() + (b.GetValue() - a.GetValue()) * progress);
116 }
117
118 inline int Interpolate (ProgressInteger& a, ProgressInteger& b, float progress)
119 {
120   return static_cast<int>(a.GetValue() + (b.GetValue() - a.GetValue()) * progress + 0.5f);
121 }
122
123 inline Vector2 Interpolate (ProgressVector2& a, ProgressVector2& b, float progress)
124 {
125   return (a.GetValue() + (b.GetValue() - a.GetValue()) * progress);
126 }
127
128 inline Vector3 Interpolate (ProgressVector3& a, ProgressVector3& b, float progress)
129 {
130   return (a.GetValue() + (b.GetValue() - a.GetValue()) * progress);
131 }
132
133 inline Vector4 Interpolate (ProgressVector4& a, ProgressVector4& b, float progress)
134 {
135   return (a.GetValue() + (b.GetValue() - a.GetValue()) * progress);
136 }
137
138 } // namespace Internal
139
140 } // namespace Dali
141
142 #endif //__DALI_PROGRESS_VALUE_H__