Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / animations / FUiAnim_VisualElementAnimationVariantInterpolator.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FUiAnim_VisualElementAnimationVariantInterpolator.cpp
20  * @brief       This file contains implementation of _VisualElementAnimationVariantInterpolator class
21  *
22  * This file contains implementation _VisualElementAnimationVariantInterpolator class.
23  */
24
25 #include <FBaseSysLog.h>
26 #include <FBaseErrors.h>
27
28 #include <FGrpFloatMatrix4.h>
29 #include <FGrpRectangle.h>
30 #include <FGrpDimension.h>
31 #include <FGrpPoint.h>
32 #include <FGrpColor.h>
33 #include <FGrpFloatPoint.h>
34 #include <FGrpFloatRectangle.h>
35 #include <FGrpFloatDimension.h>
36
37 #include "FUiAnim_VariantEx.h"
38 #include "FUiAnim_VisualElementAnimationVariantInterpolator.h"
39
40 using namespace Tizen;
41 using namespace Tizen::Graphics;
42
43 namespace Tizen { namespace Ui { namespace Animations
44 {
45
46 _VisualElementAnimationVariantInterpolator::_VisualElementAnimationVariantInterpolator(void)
47 {
48 }
49
50 _VisualElementAnimationVariantInterpolator::~_VisualElementAnimationVariantInterpolator(void)
51 {
52 }
53
54
55 result
56 _VisualElementAnimationVariantInterpolator::Interpolate(float progress, const Tizen::Ui::Variant& startValue, const Tizen::Ui::Variant& endValue, Tizen::Ui::Variant& value) const
57 {
58         SysTryReturnResult(NID_UI_ANIM, startValue.GetType() == endValue.GetType(), E_INVALID_ARG, "Invalid argument(s) is used. Variant types are not matched.");
59
60         const _VariantEx* pStartValueEx = dynamic_cast< const _VariantEx* >(&startValue);
61         const _VariantEx* pEndValueEx = dynamic_cast< const _VariantEx* >(&endValue);
62
63         if (pStartValueEx && pEndValueEx)
64         {
65                 if (pStartValueEx->IsValidTransformMatrix() && pEndValueEx->IsValidTransformMatrix())
66                 {
67                         value = pEndValueEx->GetTransformMatrix().Interpolate(
68                                 pStartValueEx->GetTransformMatrix(),
69                                 progress
70                         );
71                         return E_SUCCESS;
72                 }
73         }
74
75         switch (startValue.GetType())
76         {
77         case VARIANT_TYPE_INT:
78         {
79                 int start = startValue.ToInt();
80                 int end = endValue.ToInt();
81
82                 value = static_cast< int >(static_cast< float >(start) * (1.0f - progress) + static_cast< float >(end) * progress);
83         }
84         break;
85
86         case VARIANT_TYPE_LONG:
87         {
88                 long start = startValue.ToLong();
89                 long end = endValue.ToLong();
90
91
92                 value = static_cast< long >(static_cast< float >(start) * (1.0f - progress) + static_cast< float >(end) * progress);
93         }
94         break;
95
96         case VARIANT_TYPE_LONGLONG:
97         {
98                 long long start = startValue.ToLongLong();
99                 long long end = endValue.ToLongLong();
100
101                 value = static_cast< long long >(static_cast< float >(start) * (1.0f - progress) + static_cast< float >(end) * progress);
102         }
103         break;
104
105         case VARIANT_TYPE_FLOAT:
106         {
107                 float start = startValue.ToFloat();
108                 float end = endValue.ToFloat();
109
110                 value = start * (1.0f - progress) + end * progress;
111         }
112         break;
113
114         case VARIANT_TYPE_DOUBLE:
115         {
116                 double start = startValue.ToDouble();
117                 double end = endValue.ToDouble();
118
119                 value = static_cast< double >(static_cast< double >(start) * (1.0 - static_cast< double >(progress)) + static_cast< double >(end) * static_cast< double >(progress));
120         }
121         break;
122
123         case VARIANT_TYPE_RECTANGLE:
124         {
125                 const Tizen::Graphics::Rectangle& start = startValue.ToRectangle();
126                 const Tizen::Graphics::Rectangle& end = endValue.ToRectangle();
127                 Tizen::Graphics::Rectangle rect;
128
129                 rect.x = static_cast< int >(static_cast< float >(start.x) * (1.0f - progress) + static_cast< float >(end.x) * progress);
130                 rect.y = static_cast< int >(static_cast< float >(start.y) * (1.0f - progress) + static_cast< float >(end.y) * progress);
131                 rect.width = static_cast< int >(static_cast< float >(start.width) * (1.0f - progress) + static_cast< float >(end.width) * progress);
132                 rect.height = static_cast< int >(static_cast< float >(start.height) * (1.0f - progress) + static_cast< float >(end.height) * progress);
133
134                 value = rect;
135         }
136         break;
137
138         case VARIANT_TYPE_FLOAT_RECTANGLE:
139         {
140                 const Tizen::Graphics::FloatRectangle& start = startValue.ToFloatRectangle();
141                 const Tizen::Graphics::FloatRectangle& end = endValue.ToFloatRectangle();
142
143                 float left = start.x * (1.0f - progress) + end.x * progress;
144                 float top = start.y * (1.0f - progress) + end.y * progress;
145                 float width = start.width * (1.0f - progress) + end.width * progress;
146                 float height = start.height * (1.0f - progress) + end.height * progress;
147
148                 value = Variant(Tizen::Graphics::FloatRectangle(left, top, width, height));
149         }
150         break;
151
152         case VARIANT_TYPE_POINT:
153         {
154                 const Tizen::Graphics::Point& start = startValue.ToPoint();
155                 const Tizen::Graphics::Point& end = endValue.ToPoint();
156                 Tizen::Graphics::Point point;
157
158                 point.x = static_cast< int >(static_cast< float >(start.x) * (1.0f - progress) + static_cast< float >(end.x) * progress);
159                 point.y = static_cast< int >(static_cast< float >(start.y) * (1.0f - progress) + static_cast< float >(end.y) * progress);
160
161                 value = point;
162         }
163         break;
164
165         case VARIANT_TYPE_FLOAT_POINT:
166         {
167                 const Tizen::Graphics::FloatPoint& start = startValue.ToFloatPoint();
168                 const Tizen::Graphics::FloatPoint& end = endValue.ToFloatPoint();
169
170                 float x = start.x * (1.0f - progress) + end.x * progress;
171                 float y = start.y * (1.0f - progress) + end.y * progress;
172
173                 value = Variant(Tizen::Graphics::FloatPoint(x, y));
174         }
175         break;
176
177         case VARIANT_TYPE_DIMENSION:
178         {
179                 const Tizen::Graphics::Dimension& start = startValue.ToDimension();
180                 const Tizen::Graphics::Dimension& end = endValue.ToDimension();
181                 Tizen::Graphics::Dimension size;
182
183                 size.width = static_cast< int >(static_cast< float >(start.width) * (1.0f - progress) + static_cast< float >(end.width) * progress);
184                 size.height = static_cast< int >(static_cast< float >(start.height) * (1.0f - progress) + static_cast< float >(end.height) * progress);
185
186                 value = size;
187         }
188         break;
189
190         case VARIANT_TYPE_FLOAT_DIMENSION:
191         {
192                 const Tizen::Graphics::FloatDimension& start = startValue.ToFloatDimension();
193                 const Tizen::Graphics::FloatDimension& end = endValue.ToFloatDimension();
194                 Tizen::Graphics::FloatDimension size;
195
196                 size.width = start.width * (1.0f - progress) + end.width * progress;
197                 size.height = start.height * (1.0f - progress) + end.height * progress;
198
199                 value = size;
200         }
201         break;
202
203         case VARIANT_TYPE_COLOR:
204         {
205                 const Tizen::Graphics::Color& start = startValue.ToColor();
206                 const Tizen::Graphics::Color& end = endValue.ToColor();
207
208                 byte r = static_cast< byte >(static_cast< float >(start.GetRed()) * (1.0f - progress) + static_cast< float >(end.GetRed()) * progress);
209                 byte g = static_cast< byte >(static_cast< float >(start.GetGreen()) * (1.0f - progress) + static_cast< float >(end.GetGreen()) * progress);
210                 byte b = static_cast< byte >(static_cast< float >(start.GetBlue()) * (1.0f - progress) + static_cast< float >(end.GetBlue()) * progress);
211                 byte a = static_cast< byte >(static_cast< float >(start.GetAlpha()) * (1.0f - progress) + static_cast< float >(end.GetAlpha()) * progress);
212
213                 value = Variant(Tizen::Graphics::Color(r, g, b, a));
214         }
215         break;
216
217 #if 0
218         case VARIANT_TYPE_COLORF:
219         {
220                 const _Colorf& startValue = start.ToColorf();
221                 const _Colorf& endValue = end.ToColorf();
222
223                 float r = startValue.Red() * (1.0f - progress) + endValue.Red() * progress;
224                 float g = startValue.Green() * (1.0f - progress) + endValue.Green() * progress;
225                 float b = startValue.Blue() * (1.0f - progress) + endValue.Blue() * progress;
226                 float a = startValue.Alpha() * (1.0f - progress) + endValue.Alpha() * progress;
227
228                 value = Variant(_Colorf(r, g, b, a));
229         }
230         break;
231 #endif
232
233         case VARIANT_TYPE_FLOAT_MATRIX4:
234         {
235                 const Tizen::Graphics::FloatMatrix4& start = startValue.ToFloatMatrix4();
236                 const Tizen::Graphics::FloatMatrix4& end = endValue.ToFloatMatrix4();
237                 float m[4][4] = {{0.0f}, };
238
239                 for (int i = 0; i < 4; i++)
240                 {
241                         for (int j = 0; j < 4; j++)
242                         {
243 //                              m[j][i] = start(i, j) * (1.0f - progress) + end(i, j) * progress;
244                                 m[j][i] = start.matrix[j][i] * (1.0f - progress) + end.matrix[j][i] * progress;
245                         }
246                 }
247
248                 value = Variant(Tizen::Graphics::FloatMatrix4(m));
249         }
250         break;
251
252         case VARIANT_TYPE_BOOL:
253         {
254                 bool start = startValue.ToBool();
255                 bool end = endValue.ToBool();
256
257                 if (progress > 0.0f)
258                 {
259                         value = end;
260                 }
261                 else
262                 {
263                         value = start;
264                 }
265         }
266         break;
267
268         case VARIANT_TYPE_NONE:
269         case VARIANT_TYPE_STRING:
270         default:
271                 SysLogException(NID_UI_ANIM, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument(s) is used. Variant type is invalid.");
272                 return E_INVALID_ARG;
273         }
274
275         return E_SUCCESS;
276 }
277
278 }}} //Tizen::Ui::Animations
279