2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://floralicense.org/license/
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.
18 * @file FUiCtrl_AnimationPresenter.cpp
19 * @brief This is the implementation file for the _AnimationPresenter class.
22 #include <FBaseErrorDefine.h>
23 #include <FBaseSysLog.h>
24 #include "FUiCtrl_AnimationPresenter.h"
25 #include "FUiCtrl_Animation.h"
26 #include "FUiCtrl_AnimationModel.h"
28 using namespace Tizen::Graphics;
29 using namespace Tizen::Base::Runtime;
30 using namespace Tizen::Base::Collection;
31 using namespace Tizen::Base;
33 namespace Tizen { namespace Ui { namespace Controls
36 _AnimationPresenter::_AnimationPresenter(void)
38 , __pAnimationModel(null)
40 , __pCurrentFrame(null)
41 , __isTimerRunning(false)
42 , __pVisualElement(null)
46 _AnimationPresenter::~_AnimationPresenter(void)
52 _AnimationPresenter::CreateInstanceN(const _Animation& animation)
56 _AnimationPresenter* pAnimationPresenter = new (std::nothrow) _AnimationPresenter();
57 SysTryReturn(NID_UI_CTRL, pAnimationPresenter != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
59 r = pAnimationPresenter->Initialize(animation);
63 SysLogException(NID_UI_CTRL, r, "Failed to initialize the Animation Presenter.");
65 delete pAnimationPresenter;
66 pAnimationPresenter = null;
71 return pAnimationPresenter;
75 _AnimationPresenter::Initialize(const _Animation& animation)
79 __pAnimation = const_cast <_Animation*>(&animation);
80 SysTryReturnResult(NID_UI_CTRL, __pAnimation != null, E_SYSTEM, "A system error has occurred. Animation control core is null.");
82 __pAnimationModel = new (std::nothrow) _AnimationModel();
83 SysTryCatch(NID_UI_CTRL, __pAnimationModel != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
85 __pTimer = new (std::nothrow) Timer();
86 SysTryCatch(NID_UI_CTRL, __pTimer != null, , E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to create a new timer.");
88 r = __pTimer->Construct(*__pAnimation);
89 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[E_SYSTEM] A system error has occurred. Failed to construct the timer.");
91 __pVisualElement = __pAnimation->GetVisualElement();
92 SysTryCatch(NID_UI_CTRL, __pVisualElement != null, , E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get the root visual element");
102 _AnimationPresenter::IsTimerExpired(Timer& timer)
104 bool isTimerExpired = false;
106 __isTimerRunning = false;
107 SysTryReturn(NID_UI_CTRL, __pAnimationModel->GetAnimationStatus() == ANIMATION_PLAYING, false, E_SYSTEM,
108 "[E_SYSTEM] A system error has occurred. Animation is in ANIMATION_PLAYING state.");
110 AnimationFrame* pNextFrame = GetNextFrame();
111 SysTryReturn(NID_UI_CTRL, pNextFrame != null, false, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get the next animation frame.");
113 if (__pAnimationModel->GetCurrentRepeatedCount() < __pAnimationModel->GetRepeatCount())
115 if (timer.Start(pNextFrame->GetDuration()) == E_SUCCESS)
117 __isTimerRunning = true;
121 __pAnimationModel->SetAnimationStatus(ANIMATION_PAUSED);
123 isTimerExpired = false;
127 __pAnimationModel->SetCurrentRepeatedCount(0);
129 __pAnimationModel->SetAnimationStatus(ANIMATION_STOPPED);
130 isTimerExpired = true;
134 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
136 return isTimerExpired;
140 _AnimationPresenter::SetImageCount(int imageCount)
142 __pAnimationModel->SetImageCount(imageCount);
148 _AnimationPresenter::SetAnimationFrames(const IList* pAnimationFrames)
150 SysTryReturnResult(NID_UI_CTRL, pAnimationFrames != null, E_SYSTEM, "A system error has occurred. AnimationFrame list is null");
152 __pCurrentFrame = pAnimationFrames->GetEnumeratorN();
153 SysTryReturnResult(NID_UI_CTRL, __pCurrentFrame != null, E_SYSTEM, "A system error has occurred. Failed to get the AnimationFrame list.");
159 _AnimationPresenter::SetRepeatCount(int count)
161 __pAnimationModel->SetRepeatCount(count);
167 _AnimationPresenter::GetRepeatCount(void) const
169 return __pAnimationModel->GetRepeatCount();
173 _AnimationPresenter::GetCurrentRepeatedCount(void) const
175 return __pAnimationModel->GetCurrentRepeatedCount();
179 _AnimationPresenter::GetImageCount(void) const
181 return __pAnimationModel->GetImageCount();
185 _AnimationPresenter::Play(void)
187 result r = E_SUCCESS;
189 if (__isTimerRunning == true)
194 if (__pAnimationModel->GetAnimationStatus() != ANIMATION_PAUSED)
196 __pAnimationModel->SetCurrentRepeatedCount(0);
199 AnimationFrame* pFrame = dynamic_cast <AnimationFrame*>(__pCurrentFrame->GetCurrent());
203 pFrame = GetNextFrame();
204 SysTryReturnResult(NID_UI_CTRL, pFrame != null, E_SYSTEM, "A system error has occurred. Failed to get the next AnimationFrame.");
207 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
210 r = __pTimer->Start(pFrame->GetDuration());
211 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "A system error has occurred. Failed to start the timer");
213 __isTimerRunning = true;
214 __pAnimationModel->SetAnimationStatus(ANIMATION_PLAYING);
220 _AnimationPresenter::Stop(void)
222 if (__isTimerRunning == true)
224 if (__pTimer != null)
228 __isTimerRunning = false;
231 __pAnimationModel->SetCurrentRepeatedCount(0);
232 __pCurrentFrame->Reset();
233 __pAnimationModel->SetAnimationStatus(ANIMATION_STOPPED);
236 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
242 _AnimationPresenter::Pause(void)
244 SysTryReturnResult(NID_UI_CTRL, __isTimerRunning == true, E_SYSTEM, "A system error has occurred. Timer is not in running state.");
246 if (__pTimer != null)
250 __isTimerRunning = false;
252 __pAnimationModel->SetAnimationStatus(ANIMATION_PAUSED);
258 _AnimationPresenter::GetStatus(void) const
260 return __pAnimationModel->GetAnimationStatus();
264 _AnimationPresenter::GetCurrentFrame(void) const
266 AnimationFrame* pFrame = dynamic_cast <AnimationFrame*>(__pCurrentFrame->GetCurrent());
273 int imageCount = __pAnimationModel->GetImageCount();
274 for (int index = 0; index < imageCount; index++)
276 if (__pCurrentFrame->MoveNext() == E_OUT_OF_RANGE)
278 __pCurrentFrame->Reset();
281 pFrame = dynamic_cast <AnimationFrame*>(__pCurrentFrame->GetCurrent());
293 _AnimationPresenter::GetNextFrame(void) const
295 AnimationFrame* pNextFrame = null;
297 int imageCount = __pAnimationModel->GetImageCount();
298 for (int index = 0; index < imageCount; index++)
300 if (__pCurrentFrame->MoveNext() == E_SUCCESS)
302 pNextFrame = dynamic_cast <AnimationFrame*>(__pCurrentFrame->GetCurrent());
304 if (pNextFrame != null)
311 __pCurrentFrame->Reset();
312 int currentRepeatedCount = __pAnimationModel->GetCurrentRepeatedCount();
313 __pAnimationModel->SetCurrentRepeatedCount(++currentRepeatedCount);
321 _AnimationPresenter::Dispose(void)
325 delete __pCurrentFrame;
326 __pCurrentFrame = null;
328 if (__pTimer != null && __isTimerRunning == true)
336 delete __pAnimationModel;
337 __pAnimationModel = null;
339 __pVisualElement = null;
345 _AnimationPresenter::Draw(void)
347 result r = E_SUCCESS;
349 Rectangle bounds = __pAnimation->GetBounds();
350 Color bgColor = __pAnimation->GetBackgroundColor();
351 Canvas* pCanvas = null;
352 AnimationFrame* pFrame = null;
353 Bitmap* pBitmap = null;
355 pFrame = GetCurrentFrame();
356 SysTryCatch(NID_UI_CTRL, pFrame != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get the current animation frame.");
358 pBitmap = const_cast <Bitmap*>(pFrame->GetFrame());
362 pCanvas = __pAnimation->GetCanvasN();
364 SysTryReturnResult(NID_UI_CTRL, pCanvas != null, E_SYSTEM, "A system error has occurred. Failed to get the animation canvas.");
366 pCanvas->SetBackgroundColor(Color());
369 if (pBitmap->IsNinePatchedBitmap())
371 r = pCanvas->DrawNinePatchedBitmap(bounds, *pBitmap);
375 r = pCanvas->DrawBitmap(Rectangle(0, 0, bounds.width, bounds.height), *pBitmap);
378 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to draw the bitmap", GetErrorMessage(r));
388 }}} // Tizen::Ui::Controls