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