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