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