Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_Animation.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  * @file                FUiCtrl_Animation.cpp
19  * @brief               This is the implementation file for the _Animation class.
20  */
21
22 #include <FBaseErrorDefine.h>
23 #include <FBaseSysLog.h>
24 #include "FUi_AccessibilityContainer.h"
25 #include "FUi_AccessibilityElement.h"
26 #include "FUiAnim_VisualElement.h"
27 #include "FUiCtrl_Animation.h"
28 #include "FUiCtrl_AnimationPresenter.h"
29
30 using namespace Tizen::Graphics;
31 using namespace Tizen::Base::Runtime;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::Ui::Animations;
34
35 namespace Tizen { namespace Ui { namespace Controls
36 {
37
38 IMPLEMENT_PROPERTY(_Animation);
39
40 _Animation::_Animation(void)
41         : __pAnimationPresenter(null)
42         , __pControlAnimation(null)
43         , __pAnimationEvent(null)
44         , __pAnimationFrames(null)
45         , __pAccessibilityElement(null)
46 {
47         _VisualElement* pBase = GetVisualElement();
48
49         if (pBase != null)
50         {
51                 pBase->SetSurfaceOpaque(false);
52         }
53
54         _AccessibilityContainer* pContainer = GetAccessibilityContainer();
55         if (pContainer)
56         {
57                 pContainer->Activate(true);
58         }
59 }
60
61 _Animation::~_Animation(void)
62 {
63         if (__pAnimationEvent != null)
64         {
65                 delete __pAnimationEvent;
66                 __pAnimationEvent = null;
67         }
68
69         if (__pAnimationFrames != null)
70         {
71                 __pAnimationFrames->RemoveAll(false);
72                 delete __pAnimationFrames;
73                 __pAnimationFrames = null;
74         }
75
76         delete __pAnimationPresenter;
77         __pAnimationPresenter = null;
78
79         if (__pAccessibilityElement)
80         {
81                 __pAccessibilityElement->Activate(false);
82                 __pAccessibilityElement = null;
83         }
84 }
85
86 _Animation*
87 _Animation::CreateAnimationN(void)
88 {
89         result r = E_SUCCESS;
90
91         _Animation* pAnimation = new (std::nothrow) _Animation();
92         SysTryReturn(NID_UI_CTRL, pAnimation != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
93
94         pAnimation->__pAnimationPresenter = _AnimationPresenter::CreateInstanceN(*pAnimation);
95         r = GetLastResult();
96         SysTryCatch(NID_UI_CTRL, pAnimation->__pAnimationPresenter, , r, "[%s] Propagating.", GetErrorMessage(r));
97
98         pAnimation->__pAnimationEvent = _AnimationEvent::CreateInstanceN(*pAnimation);
99         r = GetLastResult();
100         SysTryCatch(NID_UI_CTRL, pAnimation->__pAnimationEvent, , r, "[%s] Propagating.", GetErrorMessage(r));
101
102         pAnimation->AcquireHandle();
103         pAnimation->SetFocusable(false);
104
105         SetLastResult(E_SUCCESS);
106
107         return pAnimation;
108
109 CATCH:
110         delete pAnimation;
111         pAnimation = null;
112
113         return null;
114 }
115
116 void
117 _Animation::OnTimerExpired(Timer& timer)
118 {
119         if (__pAnimationPresenter->IsTimerExpired(timer) == true)
120         {
121                 IEventArg* pEventArg = _AnimationEvent::CreateAnimationEventArgN();
122                 if (pEventArg != null)
123                 {
124                         __pAnimationEvent->Fire(*pEventArg);
125                 }
126         }
127
128         return;
129 }
130
131 result
132 _Animation::OnAttachedToMainTree(void)
133 {
134         if (__pAccessibilityElement)
135         {
136                 return E_SUCCESS;
137         }
138
139         if (likely((_AccessibilityManager::IsActivated())))
140         {
141                 _AccessibilityContainer* pContainer = GetAccessibilityContainer();
142
143                 if (pContainer)
144                 {
145                         __pAccessibilityElement = new (std::nothrow) _AccessibilityElement(true);
146                         if (__pAccessibilityElement)
147                         {
148                                 __pAccessibilityElement->SetBounds(Rectangle(0, 0, GetBounds().width, GetBounds().height));
149                                 __pAccessibilityElement->SetLabel(L"Animation");
150                                 __pAccessibilityElement->SetTrait(ACCESSIBILITY_TRAITS_NONE);
151                                 pContainer->AddElement(*__pAccessibilityElement);
152                         }
153                 }
154         }
155
156         return E_SUCCESS;
157 }
158
159 void
160 _Animation::OnBoundsChanged(void)
161 {
162         if (likely(!(_AccessibilityManager::IsActivated())))
163         {
164                 return;
165         }
166
167         if (__pAccessibilityElement)
168         {
169                 __pAccessibilityElement->SetBounds(Rectangle(0, 0, GetBounds().width, GetBounds().height));
170         }
171
172         return;
173 }
174
175 void
176 _Animation::SetImageCount(int imageCount)
177 {
178         __pAnimationPresenter->SetImageCount(imageCount);
179
180         return;
181 }
182
183 result
184 _Animation::SetAnimationFrames(const IList& animationFrames)
185 {
186         int frameCount = 0;
187
188         ArrayList* pTempList = new (std::nothrow) ArrayList();
189         SysTryReturnResult(NID_UI_CTRL, pTempList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
190
191         result r = pTempList->Construct();
192         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
193
194         frameCount = animationFrames.GetCount();
195
196         for (int index = 0; index < frameCount; index++)
197         {
198                 AnimationFrame* pInputAnimationFrame = dynamic_cast <AnimationFrame*>(const_cast <Object*>(animationFrames.GetAt(index)));
199                 SysTryCatch(NID_UI_CTRL, pInputAnimationFrame != null, pTempList->RemoveAll(true), E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get the AnimationFrame at index %d", index);
200
201                 r = pTempList->Add(*pInputAnimationFrame);
202                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, pTempList->RemoveAll(true), r, "[%s] Propagating.", GetErrorMessage(r));
203         }
204
205         if (__pAnimationFrames != null)
206         {
207                 __pAnimationFrames->RemoveAll(true);
208                 delete __pAnimationFrames;
209         }
210         __pAnimationFrames = pTempList;
211
212         r =  __pAnimationPresenter->SetAnimationFrames(__pAnimationFrames);
213         if (r != E_SUCCESS)
214         {
215                 __pAnimationFrames->RemoveAll(true);
216                 __pAnimationFrames = null;
217                 goto CATCH;
218         }
219
220         return r;
221
222 CATCH:
223         delete pTempList;
224         pTempList = null;
225
226         return r;
227 }
228
229 IList*
230 _Animation::GetAnimationFrames(void) const
231 {
232         return __pAnimationFrames;
233 }
234
235 result
236 _Animation::AddAnimationEventListener(const _IAnimationEventListener& listener)
237 {
238         return __pAnimationEvent->AddListener(listener);
239 }
240
241 result
242 _Animation::RemoveAnimationEventListener(const _IAnimationEventListener& listener)
243 {
244         return __pAnimationEvent->RemoveListener(listener);
245 }
246
247 result
248 _Animation::SetPropertyRepeatCount(const Variant& repeatCount)
249 {
250         SysTryReturnResult(NID_UI_CTRL, __pAnimationPresenter->GetStatus() == ANIMATION_STOPPED, E_SYSTEM,
251                            "A system error has occurred. Repeat count cannot be set during animation play.");
252
253         SysTryReturnResult(NID_UI_CTRL, repeatCount.ToInt() >= 0, E_INVALID_ARG, "Invalid argument(s) is used. Repeat count must be a positive integer.");
254
255         __pAnimationPresenter->SetRepeatCount(repeatCount.ToInt());
256
257         return E_SUCCESS;
258 }
259
260 result
261 _Animation::SetRepeatCount(int count)
262 {
263         return SetProperty("repeatCount", Variant(count));
264 }
265
266 Variant
267 _Animation::GetPropertyRepeatCount(void) const
268 {
269         return Variant(__pAnimationPresenter->GetRepeatCount());
270 }
271
272 int
273 _Animation::GetRepeatCount(void) const
274 {
275         Variant repeatCount = GetProperty("repeatCount");
276
277         return repeatCount.ToInt();
278 }
279
280 int
281 _Animation::GetCurrentRepeatedCount(void) const
282 {
283         return __pAnimationPresenter->GetCurrentRepeatedCount();
284 }
285
286 int
287 _Animation::GetImageCount(void) const
288 {
289         return __pAnimationPresenter->GetImageCount();
290 }
291
292 result
293 _Animation::Play(void)
294 {
295         return __pAnimationPresenter->Play();
296 }
297
298 result
299 _Animation::Stop(void)
300 {
301         return __pAnimationPresenter->Stop();
302 }
303
304 result
305 _Animation::Pause(void)
306 {
307         return __pAnimationPresenter->Pause();
308 }
309
310 AnimationStatus
311 _Animation::GetStatus(void) const
312 {
313         return __pAnimationPresenter->GetStatus();
314 }
315
316 void
317 _Animation::OnDraw(void)
318 {
319         result r = __pAnimationPresenter->Draw();
320
321         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
322
323         SetLastResult(E_SUCCESS);
324         return;
325 }
326
327 }}} // Tizen::Ui::Controls