[dali_1.0.17] Merge branch 'tizen'
[platform/core/uifw/dali-core.git] / dali / public-api / animation / alpha-functions.h
1 #ifndef __DALI_ALPHA_FUNCTIONS_H__
2 #define __DALI_ALPHA_FUNCTIONS_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/common/dali-common.h>
23 #include <dali/public-api/common/constants.h>
24
25 namespace Dali
26 {
27
28 typedef float (*AlphaFunction)(float progress); ///< Definition of an alpha function
29
30 /**
31  * @brief Namespace containing a set of alpha functions.
32  */
33 namespace AlphaFunctions
34 {
35
36 inline float Linear( float progress )
37 {
38   return progress;
39 }
40
41 inline float Default( float progress )
42 {
43   return Linear( progress );
44 }
45
46 inline float Reverse( float progress )    ///< Reverse linear
47 {
48   return 1.0f - progress;
49 }
50
51 inline float Square( float progress )     ///< Square (x^2)
52 {
53   return progress * progress;
54 }
55
56 inline float EaseIn( float progress )     ///< Speeds up and comes to a sudden stop
57 {
58   return progress * progress * progress;
59 }
60
61 inline float EaseOut( float progress )    ///< Sudden start and slows to a gradual stop
62 {
63   progress -= 1.0f;
64
65   return progress * progress * progress + 1.0f;
66 }
67
68 inline float EaseInOut( float progress )  ///< Speeds up and slows to a gradual stop
69 {
70   if (progress > 0.5f)
71   {
72     return EaseOut((progress - 0.5f)*2.0f) * 0.5f + 0.5f;
73   }
74   else
75   {
76     return EaseIn(progress * 2.0f) * 0.5f;
77   }
78 }
79
80 inline float EaseInSine( float progress )    ///< Speeds up and comes to a sudden stop
81 {
82   return -1.0f * cosf(progress * Math::PI_2) + 1.0f;
83 }
84
85 inline float EaseOutSine( float progress )   ///< Sudden start and slows to a gradual stop
86 {
87   return sinf(progress * Math::PI_2);
88 }
89
90 inline float EaseInOutSine( float progress ) ///< Speeds up and slows to a gradual stop
91 {
92   return -0.5f * (cosf(Math::PI * progress) - 1.0f);
93 }
94
95 inline float EaseInSine33( float progress )  ///< Speeds up and comes to a sudden stop
96 {
97   float tmp = cosf(Math::PI_2 * 33.0f / 90.0f);
98   return -1.0f * (cosf(progress * Math::PI_2 * 33.0f / 90.0f) - tmp) / (1.0f - tmp) + 1.0f;
99 }
100
101 inline float EaseOutSine33( float progress ) ///< Sudden start and slows to a gradual stop
102 {
103   float tmp = cosf(Math::PI_2 * 33.0f / 90.0f);
104   return (cosf((1.0f - progress) * Math::PI_2 * 33.0f / 90.0f) - tmp) / (1.0f - tmp);
105 }
106
107 inline float EaseInOutSine33( float progress ) ///< Speeds up and slows to a gradual stop
108 {
109   float tmp = sinf(Math::PI_2 * 33.0f / 90.0f);
110   return (sinf((progress * Math::PI - Math::PI_2) * 33.0f / 90.0f) + tmp) / (2.0f * tmp);
111 }
112
113 inline float EaseInOutSine50( float progress ) ///< Speeds up and slows to a gradual stop
114 {
115   float tmp = sinf(Math::PI_2 * 50.0f / 90.0f);
116   return (sinf((progress * Math::PI - Math::PI_2) * 50.0f / 90.0f) + tmp) / (2.0f * tmp);
117 }
118
119 inline float EaseInOutSine60( float progress ) ///< Speeds up and slows to a gradual stop
120 {
121   float tmp = sinf(Math::PI_2 * 60.0f / 90.0f);
122   return (sinf((progress * Math::PI - Math::PI_2) * 60.0f / 90.0f) + tmp) / (2.0f * tmp);
123 }
124
125 inline float EaseInOutSine70( float progress ) ///< Speeds up and slows to a gradual stop
126 {
127   float tmp = sinf(Math::PI_2 * 70.0f / 90.0f);
128   return (sinf((progress * Math::PI - Math::PI_2) * 70.0f / 90.0f) + tmp) / (2.0f * tmp);
129 }
130
131 inline float EaseInOutSine80( float progress ) ///< Speeds up and slows to a gradual stop
132 {
133   float tmp = sinf(Math::PI_2 * 80.0f / 90.0f);
134   return (sinf((progress * Math::PI - Math::PI_2) * 80.0f / 90.0f) + tmp) / (2.0f * tmp);
135 }
136
137 inline float EaseInOutSine90( float progress ) ///< Speeds up and slows to a gradual stop
138 {
139   return EaseInOutSine(progress);
140 }
141
142 inline float DoubleEaseInOutSine60( float progress ) ///< Speeds up and slows to a gradual stop, then speeds up again and slows to a gradual stop
143 {
144   if (progress < 0.5f)
145   {
146     return EaseInOutSine60(progress * 2.0f) / 2.0f;
147   }
148   else
149   {
150     return EaseInOutSine60((progress - 0.5f) * 2.0f) / 2.0f + 0.5f;
151   }
152 }
153
154 inline float EaseOutQuint50( float progress ) ///< Sudden start and slows to a gradual stop
155 {
156   return 1.0f - powf(1.0f - progress, 1.7f);
157 }
158
159 inline float EaseOutQuint80( float progress ) ///< Sudden start and slows to a gradual stop
160 {
161   return 1.0f - powf(1.0f - progress, 2.3f);
162 }
163
164 inline float Bounce( float progress ) ///< Sudden start, loses momentum and returns to start position
165 {
166   return sinf(progress * Math::PI);
167 }
168
169 inline float BounceBack( float progress ) ///< Sudden start, loses momentum and returns to exceed start position and gradual stop at start position
170 {
171   if( progress > 0.0f )
172   {
173     return (sinf(progress * 2.0f * Math::PI) * sinf(progress * Math::PI)) / (progress * Math::PI);
174   }
175   else
176   {
177     return 0;
178   }
179 }
180
181 inline float EaseInBack( float progress ) ///< Slow start, exceed start position and quickly reach destination
182 {
183   const float sqrt2 = 1.70158f;
184
185   return  progress * progress * ( ( sqrt2 + 1.0f ) * progress - sqrt2 );
186 }
187
188 inline float EaseOutBack( float progress ) ///< Sudden start, exceed end position and return to a gradual stop
189 {
190   const float sqrt2 = 1.70158f;
191   progress -= 1.0f;
192
193   return 1.0f + progress * progress * ( ( sqrt2 + 1.0f ) * progress + sqrt2 );
194 }
195
196 inline float EaseInOutBack( float progress ) ///< Slow start, exceed start position, fast middle, exceed end position and return to a gradual stop
197 {
198   if (progress > 0.5f)
199   {
200     return EaseOutBack((progress - 0.5f)*2.0f) * 0.5f + 0.5f;
201   }
202   else
203   {
204     return EaseInBack(progress * 2.0f) * 0.5f;
205   }
206 }
207
208 inline float Sin( float progress ) ///< Single revolution
209 {
210   return 0.5f - cosf(progress * 2.0f * Math::PI) * 0.5f;
211 }
212
213 inline float Sin2x( float progress ) ///< Two revolutions
214 {
215   return 0.5f - cosf(progress * 4.0f * Math::PI) * 0.5f;
216 }
217
218 } // namespace AlphaFunctions
219
220 } // namespace Dali
221
222 #endif // __DALI_ALPHA_FUNCTIONS_H__