Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / animations / FUiAnim_AnimationBaseImpl.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_AnimationBaseImpl.cpp
20  * @brief       This file contains implementation of _AnimationBaseImpl class
21  *
22  * This file contains implementation _AnimationBaseImpl class.
23  */
24
25 #include <FBaseSysLog.h>
26 #include <FBaseColIMapEnumerator.h>
27 #include <FBaseColIListT.h>
28 #include <FBaseColArrayList.h>
29 #include <FBaseColHashMapT.h>
30 #include <FBaseFloatComparer.h>
31 #include <FBaseComparerT.h>
32
33 #include <FUiAnimVisualElementAnimation.h>
34 #include <FUiAnimBezierTimingFunction.h>
35
36 #include "FUiAnim_AnimationBaseImpl.h"
37
38
39 using namespace Tizen::Ui::Animations;
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Collection;
42
43
44 #define INTERPOLATOR_LINEAR     L"Linear"
45 #define INTERPOLATOR_EASEIN     L"EaseIn"
46 #define INTERPOLATOR_EASEOUT    L"EaseOut"
47 #define INTERPOLATOR_EASEINOUT  L"EaseInOut"
48 #define INTERPOLATOR_DISCRETE   L"Discrete"
49 #define INTERPOLATOR_BEZIER     L"Bezier"
50
51
52 namespace Tizen { namespace Ui { namespace Animations
53 {
54
55 _AnimationBaseImpl::_AnimationBaseImpl(AnimationBase* pAnimationBase)
56         : controlPointTime1(0.0)
57         , controlPointValue1(0.0)
58         , controlPointTime2(0.0)
59         , controlPointValue2(0.0)
60         , interpolator(ANIMATION_INTERPOLATOR_LINEAR)
61         , animName(String())
62         , duration(250)
63         , offset(0)
64         , delay(0)
65         , repeatCount(1)
66         , autoReverse(false)
67         , scaleRatio(1.0f)
68         , holdEnd(true)
69         , __pAnimationBase(pAnimationBase)
70 {
71
72 }
73
74 _AnimationBaseImpl::~_AnimationBaseImpl(void)
75 {
76         result r = RemoveAllKeyFrames();
77
78         SysTryReturnVoidResult(NID_UI_ANIM, (r == E_SUCCESS), r, "[%s] Propagating.", GetErrorMessage(r));
79 }
80
81 result
82 _AnimationBaseImpl::Construct(void)
83 {
84         return keyFrameList.Construct();
85 }
86
87
88 result
89 _AnimationBaseImpl::RemoveKeyFrame(long time)
90 {
91         SysTryReturnResult(NID_UI_ANIM, (time > 0 && time <= duration), E_INVALID_ARG, "Invalid argument(s) is used. time =%ld", time);
92
93         result r = E_SUCCESS;
94
95         Object* pObj = null;
96
97         r = keyFrameList.GetValue(time, pObj);
98         SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_OBJ_NOT_FOUND, "Key frame is not found. time = %ld", time);
99
100         delete pObj;
101
102         keyFrameList.Remove(time);
103
104         return E_SUCCESS;
105 }
106
107 result
108 _AnimationBaseImpl::RemoveKeyFrameAt(int index)
109 {
110         SysTryReturnResult(NID_UI_ANIM, (index >= 0 && index < keyFrameList.GetCount()), E_OUT_OF_RANGE, "Index (%d) is out of range.", index);
111
112         result r = E_SUCCESS;
113
114         ComparerT< long > comparer;
115
116         Object* pObj = null;
117         long time = 0;
118
119         ArrayListT< long >* pKeyList = new (std::nothrow) ArrayListT< long >;
120         SysTryReturnResult(NID_UI_ANIM, (pKeyList), E_OUT_OF_MEMORY, "Memory allocation failed.");
121
122         ICollectionT< long >* pKeyN = keyFrameList.GetKeysN();
123         SysTryCatch(NID_UI_ANIM, (pKeyN), , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
124
125         r = pKeyList->Construct(*pKeyN);
126         SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to construct key list.");
127         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.");
128
129         r = pKeyList->Sort(comparer);
130         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.");
131
132         r = pKeyList->GetAt(index, time);
133         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.");
134
135         keyFrameList.GetValue(time, pObj);
136         delete pObj;
137
138         keyFrameList.Remove(time);
139
140         //fall through
141 CATCH:
142         delete pKeyN;
143         delete pKeyList;
144         return r;
145
146         return E_SUCCESS;
147 }
148
149 result
150 _AnimationBaseImpl::RemoveAllKeyFrames(void)
151 {
152         if (keyFrameList.GetCount() > 0)
153         {
154                 IMapEnumeratorT< long, Object* >* pMapEnum = keyFrameList.GetMapEnumeratorN();
155                 result r = GetLastResult();
156                 SysTryReturnResult(NID_UI_ANIM, (pMapEnum != null), r, "Propagating.");
157
158                 while (pMapEnum->MoveNext() == E_SUCCESS)
159                 {
160                         Object* pValue = null;
161                         pMapEnum->GetValue(pValue);
162                         delete pValue;
163                 }
164
165                 delete pMapEnum;
166
167                 keyFrameList.RemoveAll();
168         }
169
170         return E_SUCCESS;
171 }
172
173 bool
174 _AnimationBaseImpl::CompareTimingValues(const AnimationBase& animBase)
175 {
176         return ((interpolator == animBase._pAnimationBaseImpl->interpolator)
177                         && (animName == animBase._pAnimationBaseImpl->animName)
178                         && (duration == animBase._pAnimationBaseImpl->duration)
179                         && (offset == animBase._pAnimationBaseImpl->offset)
180                         && (delay == animBase._pAnimationBaseImpl->delay)
181                         && (repeatCount == animBase._pAnimationBaseImpl->repeatCount)
182                         && (autoReverse == animBase._pAnimationBaseImpl->autoReverse)
183                         && (!(Float::Compare(scaleRatio, animBase._pAnimationBaseImpl->scaleRatio)))
184                         && (holdEnd == animBase._pAnimationBaseImpl->holdEnd)
185                         && (!(Float::Compare(controlPointTime1, animBase._pAnimationBaseImpl->controlPointTime1)))
186                         && (!(Float::Compare(controlPointValue1, animBase._pAnimationBaseImpl->controlPointValue1)))
187                         && (!(Float::Compare(controlPointTime2, animBase._pAnimationBaseImpl->controlPointTime2)))
188                         && (!(Float::Compare(controlPointValue2, animBase._pAnimationBaseImpl->controlPointValue2))));
189 }
190
191 float
192 _AnimationBaseImpl::GetActualProgressValue(long currentTime)
193 {
194         const IVisualElementAnimationTimingFunction* pTimingFunction = null;
195
196         switch (interpolator)
197         {
198         case ANIMATION_INTERPOLATOR_LINEAR:
199         {
200                 pTimingFunction = VisualElementAnimation::GetTimingFunctionByName(INTERPOLATOR_LINEAR);
201         }
202         break;
203
204         case ANIMATION_INTERPOLATOR_EASE_IN:
205         {
206                 pTimingFunction = VisualElementAnimation::GetTimingFunctionByName(INTERPOLATOR_EASEIN);
207         }
208         break;
209
210         case ANIMATION_INTERPOLATOR_EASE_OUT:
211         {
212                 pTimingFunction = VisualElementAnimation::GetTimingFunctionByName(INTERPOLATOR_EASEOUT);
213         }
214         break;
215
216         case ANIMATION_INTERPOLATOR_EASE_IN_OUT:
217         {
218                 pTimingFunction = VisualElementAnimation::GetTimingFunctionByName(INTERPOLATOR_EASEINOUT);
219         }
220         break;
221
222         case ANIMATION_INTERPOLATOR_DISCRETE:
223         {
224                 pTimingFunction = VisualElementAnimation::GetTimingFunctionByName(INTERPOLATOR_DISCRETE);
225         }
226         break;
227
228         case ANIMATION_INTERPOLATOR_BEZIER:
229         {
230                 pTimingFunction = new (std::nothrow) BezierTimingFunction();
231                 SysTryReturn(NID_UI_ANIM, (pTimingFunction), -1.0f, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
232
233                 ((BezierTimingFunction*) pTimingFunction)->SetControlPoints(controlPointTime1, controlPointValue1, controlPointTime2, controlPointValue2);
234         }
235         break;
236
237         }
238
239         SysTryReturn(NID_UI_ANIM, pTimingFunction, -1.0f, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Timing function is null.");
240
241         long duration = __pAnimationBase->GetDuration();
242         float dur = (1.0f / (float) duration) * ((float) currentTime);
243         float actualProgress = pTimingFunction->CalculateProgress( static_cast< float >(dur));
244
245         return actualProgress;
246 }
247
248
249 }}}             // Tizen::Ui::Animations
250