Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / animations / FUiAnimRectangleAnimation.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        FUiAnimRectangleAnimation.cpp
20  * @brief       This file contains implementation of RectangleAnimation class
21  *
22  * This file contains implementation of RectangleAnimation class.
23  */
24
25 #include <FBaseSysLog.h>
26 #include <FBaseColHashMapT.h>
27
28 #include <FUiAnimRectangleAnimation.h>
29
30 #include "FUiAnim_RectangleAnimationImpl.h"
31 #include "FUiAnim_AnimationBaseImpl.h"
32
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Ui::Animations;
36 using namespace Tizen::Base::Collection;
37 using namespace Tizen::Graphics;
38
39
40 namespace Tizen { namespace Ui { namespace Animations
41 {
42
43 RectangleAnimation::RectangleAnimation(void)
44         : _pRectangleAnimationImpl(null)
45 {
46 }
47
48 RectangleAnimation::RectangleAnimation(const Rectangle& startValue, const Rectangle& endValue, long duration, AnimationInterpolatorType interpolator)
49         : AnimationBase(duration, interpolator)
50         , _pRectangleAnimationImpl(null)
51 {
52         result r = GetLastResult();
53         SysTryReturnVoidResult(NID_UI_ANIM, (_pAnimationBaseImpl), r, "[%s] Propagating.", GetErrorMessage(r));
54
55         SysTryReturnVoidResult(NID_UI_ANIM, (startValue.height >= 0 && startValue.width >= 0 && endValue.height >= 0 && endValue.width >= 0),
56                                                         E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument(s) is used. startValue = (%d, %d, %d, %d), endValue = (%d, %d, %d, %d)",
57                                                         startValue.x, startValue.y, startValue.width, startValue.height, endValue.x, endValue.y, endValue.width, endValue.height);
58
59         _pRectangleAnimationImpl = new (std::nothrow) _RectangleAnimationImpl(this);
60         if (_pRectangleAnimationImpl == null)
61         {
62                 SysLogException(NID_UI_ANIM, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
63
64                 delete _pAnimationBaseImpl;
65                 _pAnimationBaseImpl = null;
66
67                 return;
68         }
69
70         _pRectangleAnimationImpl->startValue = startValue;
71         _pRectangleAnimationImpl->endValue = endValue;
72 }
73
74 RectangleAnimation::~RectangleAnimation(void)
75 {
76         delete _pRectangleAnimationImpl;
77         _pRectangleAnimationImpl = null;
78 }
79
80 RectangleAnimation::RectangleAnimation(const RectangleAnimation& rectangleAnimation)
81         : AnimationBase(rectangleAnimation)
82         , _pRectangleAnimationImpl(null)
83 {
84         result r = GetLastResult();
85         SysTryReturnVoidResult(NID_UI_ANIM, (_pAnimationBaseImpl), r, "[%s] Propagating.", GetErrorMessage(r));
86
87         _pRectangleAnimationImpl = new (std::nothrow) _RectangleAnimationImpl(this);
88         SysTryCatch(NID_UI_ANIM, (_pRectangleAnimationImpl), , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
89
90         rectangleAnimation.GetAnchor(_pRectangleAnimationImpl->anchorX, _pRectangleAnimationImpl->anchorY);
91
92         r = _pRectangleAnimationImpl->CopyRectangleAnimationValue(rectangleAnimation);
93         SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), , E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to copy rectangle animation value.");
94
95         return;
96
97 CATCH:
98         delete _pAnimationBaseImpl;
99         _pAnimationBaseImpl = null;
100         delete _pRectangleAnimationImpl;
101         _pRectangleAnimationImpl = null;
102 }
103
104 RectangleAnimation&
105 RectangleAnimation::operator =(const RectangleAnimation& rhs)
106 {
107         ClearLastResult();
108
109         if ((&rhs) != this)
110         {
111                 result r = E_SUCCESS;
112
113                 AnimationBase::operator =(rhs);
114
115                 rhs.GetAnchor(_pRectangleAnimationImpl->anchorX, _pRectangleAnimationImpl->anchorY);
116
117                 r = _pRectangleAnimationImpl->CopyRectangleAnimationValue(rhs);
118                 SysTryReturn(NID_UI_ANIM, (r == E_SUCCESS), *this, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to copy rectangle animation value.");
119         }
120
121         return *this;
122 }
123
124 bool
125 RectangleAnimation::operator ==(const RectangleAnimation& rhs) const
126 {
127         result r = E_SUCCESS;
128
129         if (_pAnimationBaseImpl->keyFrameList.GetCount() != rhs._pAnimationBaseImpl->keyFrameList.GetCount())
130         {
131                 return false;
132         }
133
134         ArrayListT< long >* pKeyList1 = null;
135         ArrayListT< long >* pKeyList2 = null;
136         ICollectionT< long >* pKeyN = _pAnimationBaseImpl->keyFrameList.GetKeysN();
137
138         if (_pAnimationBaseImpl->keyFrameList.GetCount() != 0)
139         {
140                 pKeyList1 = new (std::nothrow) ArrayListT< long >;
141                 SysTryCatch(NID_UI_ANIM, (pKeyList1 != null && pKeyN != null), , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
142
143                 r = pKeyList1->Construct(*pKeyN);
144                 SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), , E_SYSTEM, "[E_SYSTEM] Failed to construct key list.");
145
146                 pKeyList2 = new (std::nothrow) ArrayListT< long >;
147                 SysTryCatch(NID_UI_ANIM, (pKeyList2 != null), , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
148
149                 ComparerT< long > comparer;
150
151                 r = pKeyList1->Sort(comparer);
152                 SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), , E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to sort key list.");
153
154                 r = pKeyList2->Sort(comparer);
155                 SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), , E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to sort key list.");
156
157                 long time1 = 0;
158                 long time2 = 0;
159                 Object* pObjValue1 = null;
160                 Object* pObjValue2 = null;
161
162                 for (int index = 0; index < _pAnimationBaseImpl->keyFrameList.GetCount(); index++)
163                 {
164                         pKeyList1->GetAt(index, time1);
165                         pKeyList2->GetAt(index, time2);
166                         if (time1 != time2)
167                         {
168                                 goto CATCH;
169                         }
170
171                         _pAnimationBaseImpl->keyFrameList.GetValue(time1, pObjValue1);
172                         rhs._pAnimationBaseImpl->keyFrameList.GetValue(time2, pObjValue2);
173
174                         if ((!pObjValue1) && (!pObjValue2))
175                         {
176                                 continue;
177                         }
178                         else if ((!pObjValue1) || (!pObjValue2))
179                         {
180                                 goto CATCH;
181                         }
182
183                         Rectangle* pRectValue1 = dynamic_cast< Rectangle* >(pObjValue1);
184                         if (pRectValue1 == null)
185                         {
186                                 goto CATCH;
187                         }
188                         Rectangle* pRectValue2 = dynamic_cast< Rectangle* >(pObjValue2);
189                         if (pRectValue2 == null)
190                         {
191                                 goto CATCH;
192                         }
193                         if (!(pRectValue1->Equals(*pRectValue2)))
194                         {
195                                 goto CATCH;
196                         }
197
198                 }
199
200                 delete pKeyList1;
201                 delete pKeyList2;
202         }
203         delete pKeyN;
204
205         if (!((_pRectangleAnimationImpl->startValue).Equals(rhs._pRectangleAnimationImpl->startValue)) ||
206                 !((_pRectangleAnimationImpl->endValue).Equals(rhs._pRectangleAnimationImpl->endValue)))
207         {
208                 return false;
209         }
210
211         if (((Float::Compare(_pRectangleAnimationImpl->anchorX, rhs._pRectangleAnimationImpl->anchorX)) != 0) &&
212                 ((Float::Compare(_pRectangleAnimationImpl->anchorY, rhs._pRectangleAnimationImpl->anchorY)) != 0))
213         {
214                 return false;
215         }
216
217         return _pAnimationBaseImpl->CompareTimingValues(rhs);
218
219
220 CATCH:
221         delete pKeyN;
222         delete pKeyList1;
223         delete pKeyList2;
224         return false;
225 }
226
227 bool
228 RectangleAnimation::operator !=(const RectangleAnimation& rhs) const
229 {
230         return !(*this == rhs);
231 }
232
233 bool
234 RectangleAnimation::Equals(const Object& obj) const
235 {
236         const RectangleAnimation* pRectangleAnim = dynamic_cast< const RectangleAnimation* >(&obj);
237         if (pRectangleAnim == null)
238         {
239                 return false;
240         }
241
242         return (*this == *pRectangleAnim);
243 }
244
245 int
246 RectangleAnimation::GetHashCode(void) const
247 {
248         return (((int) GetRepeatCount()) + ((int) GetOffset()) + ((int) GetDuration()) + ((int) GetDelay()));
249 }
250
251 result
252 RectangleAnimation::SetAnchor(float anchorX, float anchorY)
253 {
254         SysTryReturnResult(NID_UI_ANIM, (anchorX >= 0.0 && anchorX <= 1.0 && anchorY >= 0.0 && anchorY <= 1.0), E_INVALID_ARG, "Invalid argument(s) is used. anchorX = %f, anchorY = %f.", anchorX, anchorY);
255
256         _pRectangleAnimationImpl->anchorX = anchorX;
257         _pRectangleAnimationImpl->anchorY = anchorY;
258
259         return E_SUCCESS;
260 }
261
262 void
263 RectangleAnimation::GetAnchor(float& anchorX, float& anchorY) const
264 {
265         anchorX = _pRectangleAnimationImpl->anchorX;
266         anchorY = _pRectangleAnimationImpl->anchorY;
267 }
268
269 result
270 RectangleAnimation::GetAnimatedValue(long currentTime, Rectangle& animatedValue) const
271 {
272         SysTryReturnResult(NID_UI_ANIM, (currentTime >= 0 && currentTime <= GetDuration()), E_INVALID_ARG, "Invalid argument(s) is used. currentTime = %ld", currentTime);
273
274         float actualProgress = _pAnimationBaseImpl->GetActualProgressValue(currentTime);
275         SysTryReturnResult(NID_UI_ANIM, (actualProgress >= 0.0f && actualProgress <= 1.0f), E_SYSTEM, "System error in calculating progress.");
276
277         return _pRectangleAnimationImpl->GetAnimatedValue(actualProgress, animatedValue);
278 }
279
280 result
281 RectangleAnimation::AddKeyFrame(long time, const Rectangle& value)
282 {
283         SysTryReturnResult(NID_UI_ANIM, (time > 0 && time != _pAnimationBaseImpl->duration), E_INVALID_ARG, "Invalid argument(s) is used. time = %ld", time);
284
285         result r = E_SUCCESS;
286         Rectangle* pRectObj = null;
287
288         if (time > _pAnimationBaseImpl->duration)
289         {
290                 pRectObj = new (std::nothrow) Rectangle(_pRectangleAnimationImpl->endValue);
291                 SysTryReturnResult(NID_UI_ANIM, (pRectObj != null), E_OUT_OF_MEMORY, "Memory allocation failed");
292
293                 r = _pAnimationBaseImpl->keyFrameList.Add(_pAnimationBaseImpl->duration, dynamic_cast< Object* >(pRectObj));
294                 SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add to key frame list.");
295
296                 _pAnimationBaseImpl->duration = time;
297                 _pRectangleAnimationImpl->endValue = value;
298         }
299         else
300         {
301                 pRectObj = new (std::nothrow) Rectangle(value);
302                 SysTryReturnResult(NID_UI_ANIM, (pRectObj != null), E_OUT_OF_MEMORY, "Memory allocation failed.");
303
304                 Object* pKeyValue = null;
305                 _pAnimationBaseImpl->keyFrameList.GetValue(time, pKeyValue);
306                 if (pKeyValue != null)
307                 {
308                         _pAnimationBaseImpl->keyFrameList.Remove(time); // Remove the already existing entry
309                         delete pKeyValue;
310                 }
311                 r = _pAnimationBaseImpl->keyFrameList.Add(time, dynamic_cast< Object* >(pRectObj));
312                 SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add to key frame list.");
313         }
314
315         return r;
316
317 CATCH:
318         delete pRectObj;
319         return r;
320 }
321
322 result
323 RectangleAnimation::GetKeyFrameAt(int index, long& time, Rectangle& value) const
324 {
325         SysTryReturnResult(NID_UI_ANIM, (index >= 0 && index < _pAnimationBaseImpl->keyFrameList.GetCount()),
326                                                 E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] Index (%d) is out of range.", index);
327
328         result r = E_SUCCESS;
329         Object* pObjValue = null;
330         Rectangle* pDimValue = null;
331         ComparerT< long > comparer;
332
333         ArrayListT< long >* pKeyList = new (std::nothrow) ArrayListT< long >;
334         ICollectionT< long >* pKeyN = _pAnimationBaseImpl->keyFrameList.GetKeysN();
335
336         SysTryCatch(NID_UI_ANIM, (pKeyList != null && pKeyN != null), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
337
338         r = pKeyList->Construct(*pKeyN);
339         SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to construct key list.");
340         SysTryCatch(NID_UI_ANIM, !(pKeyList->GetCount() <= 0), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Key list count is invalid.");
341
342         r = pKeyList->Sort(comparer);
343         SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to sort key list.");
344
345         r = pKeyList->GetAt(index, time);
346         SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to get the time from list.");
347
348         r = _pAnimationBaseImpl->keyFrameList.GetValue(time, pObjValue);
349         SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to get key value.");
350
351         pDimValue = dynamic_cast< Rectangle* >(pObjValue);
352         if (pDimValue)
353         {
354                 value = *pDimValue;
355         }
356
357         //fall through
358 CATCH:
359         delete pKeyN;
360         delete pKeyList;
361         return r;
362 }
363
364 result
365 RectangleAnimation::RemoveKeyFrame(long time)
366 {
367         return _pAnimationBaseImpl->RemoveKeyFrame(time);
368 }
369
370 result
371 RectangleAnimation::RemoveKeyFrameAt(int index)
372 {
373         return _pAnimationBaseImpl->RemoveKeyFrameAt(index);
374 }
375
376 result
377 RectangleAnimation::RemoveAllKeyFrames(void)
378 {
379         result r = E_SUCCESS;
380
381         IMapEnumeratorT< long, Object* >* pMapEnum = _pAnimationBaseImpl->keyFrameList.GetMapEnumeratorN();
382         SysTryReturnResult(NID_UI_ANIM, (pMapEnum), E_OUT_OF_MEMORY, "Memory allocation failed.");
383
384         MapEntryT< long, Object* > value;
385
386         while (pMapEnum->MoveNext() == E_SUCCESS)
387         {
388                 r = pMapEnum->GetCurrent(value);
389                 SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to get the current value.");
390
391                 Object* pObj = value.GetValue();
392
393                 delete pObj;
394         }
395
396         _pAnimationBaseImpl->keyFrameList.RemoveAll();
397
398         //fall through
399 CATCH:
400         delete pMapEnum;
401
402         return r;
403 }
404
405 result
406 RectangleAnimation::SetStartValue(const Rectangle& startValue)
407 {
408         SysTryReturnResult(NID_UI_ANIM, (startValue.width >= 0 && startValue.height >= 0), E_INVALID_ARG, "Invalid argument(s) is used. size = (%d, %d)", startValue.width, startValue.height);
409
410         _pRectangleAnimationImpl->startValue = startValue;
411
412         return E_SUCCESS;
413
414 }
415
416 result
417 RectangleAnimation::SetEndValue(const Rectangle& endValue)
418 {
419         SysTryReturnResult(NID_UI_ANIM, (endValue.width >= 0 && endValue.height >= 0), E_INVALID_ARG, "Invalid argument(s) is used. size = (%d, %d)", endValue.width, endValue.height);
420
421         _pRectangleAnimationImpl->endValue = endValue;
422
423         return E_SUCCESS;
424 }
425
426 Rectangle
427 RectangleAnimation::GetStartValue(void) const
428 {
429         return _pRectangleAnimationImpl->startValue;
430 }
431
432 Rectangle
433 RectangleAnimation::GetEndValue(void) const
434 {
435         return _pRectangleAnimationImpl->endValue;
436 }
437
438 AnimationType
439 RectangleAnimation::GetType(void) const
440 {
441         return ANIMATION_TYPE_RECTANGLE_ANIMATION;
442 }
443
444
445 }}}             // Tizen::Ui::Animations
446