Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / animations / FUiAnim_FrameAnimatorImpl.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_FrameAnimatorImpl.cpp
20  * @brief       This file contains implementation of _FrameAnimatorImpl class
21  *
22  * This file contains implementation of _FrameAnimatorImpl class.
23  */
24
25 #include <FBaseSysLog.h>
26 #include <FBaseTypes.h>
27
28 #include <FUiControl.h>
29 #include <FUiCtrlFrame.h>
30 #include <FUiCtrlForm.h>
31 #include <FUiAnimDimensionAnimation.h>
32 #include <FUiAnimPointAnimation.h>
33 #include <FUiAnimRectangleAnimation.h>
34 #include <FUiAnimIntegerAnimation.h>
35 #include <FUiAnimFloatAnimation.h>
36 #include <FUiAnimRotateAnimation.h>
37 #include <FUiAnimAnimationTransaction.h>
38 #include <FUiAnimVisualElement.h>
39 #include <FUiAnimBezierTimingFunction.h>
40
41 #include "FUi_ControlImpl.h"
42 #include "FUi_Matrix3Df.h"
43 #include "FUiAnim_FrameAnimatorImpl.h"
44 #include "FUiAnim_VisualElementAnimationImpl.h"
45
46 #include "FUiAnim_VisualElementImpl.h"
47 #include "FUiAnim_ControlVisualElement.h"
48 #include "FUiAnim_MatrixUtil.h"
49
50
51 using namespace Tizen::Base;
52 using namespace Tizen::Base::Collection;
53 using namespace Tizen::Graphics;
54 using namespace Tizen::Ui;
55 using namespace Tizen::Ui::Controls;
56
57
58 namespace Tizen { namespace Ui { namespace Animations
59 {
60
61
62 _FrameAnimatorImpl::_FrameAnimatorImpl(FrameAnimator* pFrameAnimator)
63         : __pFrame(null)
64         , __pFrameAnimator(pFrameAnimator)
65         , __transactionId(-1)
66         , __frameAnimatorStatus(ANIMATOR_STATUS_STOPPED)
67         , __pCurrentForm(null)
68         , __pNextForm(null)
69         , __pCurrentFormVisualElement(null)
70         , __pNextFormVisualElement(null)
71         , __animationEffectType(FRAME_ANIMATOR_FORM_TRANSITION_ANIMATION_FADE_IN_OUT)
72         , __animInterpolatorType(ANIMATION_INTERPOLATOR_LINEAR)
73         , __duration(400)
74         , __firstBezierControlPointsTime(0.0f)
75         , __firstBezierControlPointsValue(0.0f)
76         , __secondBezierControlPointsTime(0.0f)
77         , __secondBezierControlPointsValue(0.0f)
78         , __veDefaultMatrix()
79         , __veDefaultBounds(0, 0, 0, 0)
80         , __veDefaultOpacity(0)
81         , __pBgElement(null)
82         , __pCurrentCaptureVisualElement(null)
83         , __pNextCaptureVisualElement(null)
84         , __pDummySurface(null)
85 {
86 }
87
88 result
89 _FrameAnimatorImpl::Construct(const Frame& source)
90 {
91         SysAssertf((__pFrame == null), "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class.");
92
93         result r = E_SUCCESS;
94
95         __pFrame = const_cast< Frame* >(&source);
96
97         r = __frameActiveAnimationList.Construct();
98         SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), , r,"[%s] Failed to construct FrameActiveAnimationList.", GetErrorMessage(r));
99
100         __pBgElement = new (std::nothrow) _ControlVisualElement();
101         SysTryCatch(NID_UI_ANIM, (__pBgElement != null), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
102
103         r = __pBgElement->ConstructControlVisualElement();
104         SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), , r, "[%s] Failed to construct ControlVisualElement.", GetErrorMessage(r));
105
106         __pBgElement->SetName("FrameAnimatorBG");
107         __pBgElement->SetShowState(true);
108         __pBgElement->SetImplicitAnimationEnabled(false);
109
110         __pCurrentCaptureVisualElement = new (std::nothrow) _VisualElement();
111         SysTryCatch(NID_UI_ANIM, (__pCurrentCaptureVisualElement != null), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
112
113         r = __pCurrentCaptureVisualElement->Construct();
114         SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), , r, "[%s] Failed to construct VisualElement.", GetErrorMessage(r));
115
116         __pCurrentCaptureVisualElement->SetName("FrameAnimatorCurrentCapture");
117         __pCurrentCaptureVisualElement->SetShowState(false);
118         __pCurrentCaptureVisualElement->SetImplicitAnimationEnabled(false);
119
120         __pNextCaptureVisualElement = new (std::nothrow) _VisualElement();
121         SysTryCatch(NID_UI_ANIM, (__pNextCaptureVisualElement != null), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
122
123         r = __pNextCaptureVisualElement->Construct();
124         SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), , r, "[%s] Failed to construct VisualElement.", GetErrorMessage(r));
125
126         __pNextCaptureVisualElement->SetName("FrameAnimatorNextCapture");
127         __pNextCaptureVisualElement->SetShowState(false);
128         __pNextCaptureVisualElement->SetImplicitAnimationEnabled(false);
129
130         __pBgElement->AttachChild(*__pCurrentCaptureVisualElement);
131         __pBgElement->AttachChild(*__pNextCaptureVisualElement);
132
133         __pDummySurface = new (std::nothrow) VisualElementSurface();
134         SysTryCatch(NID_UI_ANIM, (__pDummySurface != null), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
135
136         r = __pDummySurface->Construct(*__pFrame->GetDisplayContext(), Dimension(1,1));
137         SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), , r, "[%s] Failed to construct VisualElementSurface.", GetErrorMessage(r));
138
139         return r;
140
141 CATCH:
142         Dispose();
143
144         return r;
145 }
146
147 _FrameAnimatorImpl::~_FrameAnimatorImpl(void)
148 {
149         Dispose();
150 }
151
152 result
153 _FrameAnimatorImpl::Dispose(void)
154 {
155         if (__transactionId)
156         {
157                 AnimationTransaction::Stop(__transactionId);
158                 __transactionId = -1;
159         }
160
161         if (__pDummySurface)
162         {
163                 delete __pDummySurface;
164                 __pDummySurface = null;
165         }
166
167         // delete with children
168         if (__pBgElement)
169         {
170                 __pBgElement->Destroy();
171                 __pBgElement = null;
172         }
173
174         __frameActiveAnimationList.RemoveAll();
175         __frameAnimStatusEventListener.RemoveAll();
176
177         return E_SUCCESS;
178 }
179
180 bool
181 _FrameAnimatorImpl::FrameActiveAnimation::operator ==(const FrameActiveAnimation& frameActiveAnim) const
182 {
183         if ((formTransitionAnimationType == frameActiveAnim.formTransitionAnimationType) && (pPropertyAnimation == frameActiveAnim.pPropertyAnimation))
184         {
185                 return true;
186         }
187
188         return false;
189 }
190
191 bool
192 _FrameAnimatorImpl::FrameActiveAnimation::operator !=(const FrameActiveAnimation& frameActiveAnim) const
193 {
194         if ((formTransitionAnimationType != frameActiveAnim.formTransitionAnimationType) || (pPropertyAnimation != frameActiveAnim.pPropertyAnimation))
195         {
196                 return true;
197         }
198
199         return false;
200 }
201
202 result
203 _FrameAnimatorImpl::AddFrameAnimatorEventListener(IFrameAnimatorEventListener& listener)
204 {
205         IFrameAnimatorEventListener* pStatusListener = &listener;
206
207         if (__frameAnimStatusEventListener.Contains(pStatusListener) == true)
208         {
209                 return E_OBJ_ALREADY_EXIST;
210         }
211
212         result r = __frameAnimStatusEventListener.Add(pStatusListener);
213         SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "A system error has been occurred. Failed to add frame animator event listener.");
214
215         return r;
216 }
217
218 result
219 _FrameAnimatorImpl::RemoveFrameAnimatorEventListener(IFrameAnimatorEventListener& listener)
220 {
221         IFrameAnimatorEventListener* pStatusListener = &listener;
222
223         result r = __frameAnimStatusEventListener.Remove(pStatusListener);
224         SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_OBJ_NOT_FOUND, "Propagating.");
225
226         return r;
227 }
228
229 result
230 _FrameAnimatorImpl::StopAllAnimations(void)
231 {
232         if (__transactionId != -1)
233         {
234                 result r = AnimationTransaction::Stop(__transactionId);
235                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), r, "Propagating.");
236         }
237
238         return E_SUCCESS;
239 }
240
241 result
242 _FrameAnimatorImpl::AddControl(const Control& control)
243 {
244         Form* pCurrentForm = __pFrame->GetCurrentForm();
245
246         Form* pNextForm = dynamic_cast< Form* >(const_cast< Control* >(&control));
247         SysTryReturnResult(NID_UI_ANIM, (pNextForm), E_INVALID_ARG, "Invalid argument(s) is used. Control is not Form.");
248         SysTryReturnResult(NID_UI_ANIM, (pCurrentForm != pNextForm), E_SUCCESS, "Current form is same as the new form.");
249
250         result r = __pFrame->AddControl(control);
251         SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), r, "Propagating.");
252
253         r = ChangeCurrentForm(pCurrentForm, *pNextForm);
254         SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), r, "Propagating.");
255
256         return E_SUCCESS;
257 }
258
259 result
260 _FrameAnimatorImpl::SetCurrentForm(const Form& form)
261 {
262         Form* pCurrentForm = __pFrame->GetCurrentForm();
263
264         Form* pNextForm = const_cast< Form* >(&form);
265         SysTryReturnResult(NID_UI_ANIM, (pCurrentForm != pNextForm), E_SUCCESS, "Current form is same as the new form.");
266
267         result r = ChangeCurrentForm(pCurrentForm, *pNextForm);
268         SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), r, "Propagating.");
269
270         return E_SUCCESS;
271 }
272
273 result
274 _FrameAnimatorImpl::ChangeCurrentForm(Form* pCurrentForm, Form& nextForm)
275 {
276     SysTryReturnResult(NID_UI_ANIM, __transactionId == -1, E_INVALID_OPERATION, "Last frame animation was not completed.");
277
278         result r = E_SUCCESS;
279
280         if (IsAnimationSupported() == false || pCurrentForm == null)
281         {
282                 r = __pFrame->SetCurrentForm(nextForm);
283                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), r, "Propagating.");
284
285                 __pNextForm->Draw();
286
287                 return E_SUCCESS;
288         }
289
290         __pCurrentForm = pCurrentForm;
291         __pNextForm = &nextForm;
292
293         _ControlImpl* pControlImpl = null;
294
295         pControlImpl = _ControlImpl::GetInstance(*__pCurrentForm);
296         __pCurrentFormVisualElement = pControlImpl->GetCore().GetVisualElement();
297
298         r = __pFrame->SetCurrentForm(*__pNextForm);
299         SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), r, "Propagating.");
300
301         pControlImpl = _ControlImpl::GetInstance(*__pNextForm);
302         __pNextFormVisualElement = pControlImpl->GetCore().GetVisualElement();
303
304         // for Layout
305         pControlImpl->SetBoundsAndUpdateLayout(pControlImpl->GetBounds());
306
307         __veDefaultMatrix = __pCurrentFormVisualElement->GetTransformMatrix();
308         __veDefaultBounds = _VisualElementImpl::GetInstance(*__pCurrentFormVisualElement)->GetBoundingBox();
309         __veDefaultOpacity = 1.0f;      //TODO: check__pCurrentFormVisualElement->GetOpacity();
310         __pCurrentFormVisualElement->SetShowState(true);
311
312         // prepare background
313         FloatRectangle bgBounds = __pCurrentFormVisualElement->GetBounds();
314
315         if (__animationEffectType == FRAME_ANIMATOR_FORM_TRANSITION_ANIMATION_TRANSLATE_LEFT
316                 || __animationEffectType == FRAME_ANIMATOR_FORM_TRANSITION_ANIMATION_TRANSLATE_RIGHT)
317         {
318                 if (bgBounds.width <= bgBounds.height)
319                 {
320                         bgBounds.width = bgBounds.height;
321                 }
322                 else
323                 {
324                         bgBounds.height = bgBounds.width;
325                 }
326         }
327
328         __pBgElement->SetBounds(bgBounds);
329
330         Color bgColor = pCurrentForm->GetBackgroundColor();
331         __pBgElement->SetBackgroundColor(_Colorf((float)bgColor.GetRed() / 255, (float)bgColor.GetGreen() / 255, (float)bgColor.GetBlue() / 255, (float)bgColor.GetAlpha() / 255));
332
333         VisualElement* pParentVisualElement = __pCurrentFormVisualElement->GetParent();
334         SysTryReturnResult(NID_UI_ANIM, (pParentVisualElement), E_SYSTEM, "A system error has been occurred. Failed to get visual element's parent.");
335
336         pParentVisualElement->InsertChild(*__pBgElement, __pCurrentFormVisualElement, false);
337
338         AnimationTransaction::Begin(__transactionId);
339         AnimationTransaction::SetCurrentTransactionEventListener(this);
340
341         r = SetAnimations();
342
343         if (r != E_SUCCESS)
344         {
345                 AnimationTransaction::Discard();
346                 RemoveAllAnimations(true);
347
348                 return r;
349         }
350
351         r = AnimationTransaction::Commit();
352         SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to commit transaction.");
353
354         __frameAnimatorStatus = ANIMATOR_STATUS_PLAYING;
355         return E_SUCCESS;
356 }
357
358 result
359 _FrameAnimatorImpl::SetAnimations(void)
360 {
361         result r = E_SUCCESS;
362
363         String transId = "";
364         transId.Append(__transactionId);
365
366         switch (__animationEffectType)
367         {
368         case FRAME_ANIMATOR_FORM_TRANSITION_ANIMATION_TRANSLATE_LEFT:
369         {
370                 Dimension currentSize = __pCurrentForm->GetSize();
371                 Dimension nextSize = __pNextForm->GetSize();
372
373                 Point currFormStartPoint = Point(0, 0);
374                 Point currFormEndPoint = Point((-1 * (currentSize.width)), 0);
375
376                 Point nextFormStartPoint = Point((currentSize.width), 0);
377                 Point nextFormEndPoint = Point(0, 0);
378
379                 if (currentSize.width > nextSize.width)
380                 {
381                         currFormStartPoint = Point((-1 * (currentSize.width - nextSize.width)), 0);
382                         nextFormStartPoint = Point((nextSize.width), 0);
383                 }
384
385                 r = StartLayerAnimation(__pCurrentFormVisualElement, ANIMATION_TARGET_POSITION, Variant(currFormStartPoint), Variant(currFormEndPoint), transId + (L"CurrentFormLeftTranslatePosition"));
386                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list.");
387
388                 r = StartLayerAnimation(__pNextFormVisualElement, ANIMATION_TARGET_POSITION, Variant(nextFormStartPoint), Variant(nextFormEndPoint), transId + (L"NextFormLeftTranslatePosition"));
389                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list.");
390         }
391         break;
392
393         case FRAME_ANIMATOR_FORM_TRANSITION_ANIMATION_TRANSLATE_RIGHT:
394         {
395                 Dimension currentSize = __pCurrentForm->GetSize();
396                 Dimension nextSize = __pNextForm->GetSize();
397
398                 Point currFormStartPoint = Point(0, 0);
399                 Point currFormEndPoint = Point((nextSize.width), 0);
400
401                 Point nextFormStartPoint = Point((-1 * (nextSize.width)), 0);
402                 Point nextFormEndPoint = Point(0, 0);
403
404                 if (currentSize.width < nextSize.width)
405                 {
406                         currFormStartPoint = Point((nextSize.width - currentSize.width), 0);
407                         nextFormStartPoint = Point((-1 * (currentSize.width)), 0);
408                 }
409
410                 r = StartLayerAnimation(__pCurrentFormVisualElement, ANIMATION_TARGET_POSITION, Variant(currFormStartPoint), Variant(currFormEndPoint), transId + (L"CurrentFormRightTranslatePosition"));
411                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list");
412
413                 r = StartLayerAnimation(__pNextFormVisualElement, ANIMATION_TARGET_POSITION, Variant(nextFormStartPoint), Variant(nextFormEndPoint), transId + (L"NextFormRightTranslatePosition"));
414                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list");
415         }
416         break;
417
418         case FRAME_ANIMATOR_FORM_TRANSITION_ANIMATION_FADE_IN_OUT:
419         {
420                 r = PrepareCapture();
421                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to capture.");
422
423 //              r = StartLayerAnimation(__pCurrentCaptureVisualElement, ANIMATION_TARGET_ALPHA, Variant(1.0f), Variant(0.0f), transId + (L"CurrentFormFadeOutAlpha"));
424 //              SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list.");
425
426                 r = StartLayerAnimation(__pNextCaptureVisualElement, ANIMATION_TARGET_ALPHA, Variant(0.0f), Variant(1.0f), transId + (L"NextFormFadeInAlpha"));
427                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list.");
428         }
429         break;
430
431         case FRAME_ANIMATOR_FORM_TRANSITION_ANIMATION_ZOOM_IN:
432         {
433                 Dimension currentSize = __pCurrentForm->GetSize();
434
435                 Dimension startSize = Dimension((currentSize.width * 80) / 100, (currentSize.height * 80) / 100);
436                 Dimension endSize = currentSize;
437
438                 r = StartLayerAnimation(__pCurrentFormVisualElement, ANIMATION_TARGET_ALPHA, Variant(1.0f), Variant(0.0f), transId + (L"CurrentFormZoomInAlpha"));
439                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list.");
440
441                 r = StartLayerAnimation(__pNextFormVisualElement, ANIMATION_TARGET_ALPHA, Variant(0.0f), Variant(1.0f), transId + (L"NextFormZoomInAlpha"));
442                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list.");
443
444                 r = StartLayerAnimation(__pNextFormVisualElement, ANIMATION_TARGET_SIZE, Variant(startSize), Variant(endSize), transId + (L"NextFormZoomInSize"));
445                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list.");
446         }
447         break;
448
449         case FRAME_ANIMATOR_FORM_TRANSITION_ANIMATION_ZOOM_OUT:
450         {
451                 r = PrepareCapture();
452                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to capture.");
453
454                 Dimension currentSize = __pCurrentForm->GetSize();
455
456                 Dimension startSize = currentSize;
457                 Dimension endSize = Dimension((currentSize.width * 80) / 100, (currentSize.height * 80) / 100);
458
459                 r = StartLayerAnimation(__pCurrentCaptureVisualElement, ANIMATION_TARGET_SIZE, Variant(startSize), Variant(endSize), transId + (L"CurrentFormZoomOutSize"));
460                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list.");
461
462                 r = StartLayerAnimation(__pCurrentCaptureVisualElement, ANIMATION_TARGET_ALPHA, Variant(1.0f), Variant(0.0f), transId + (L"CurrentFormZoomOutAlpha"));
463                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list.");
464
465                 r = StartLayerAnimation(__pNextCaptureVisualElement, ANIMATION_TARGET_ALPHA, Variant(0.0f), Variant(1.0f), transId + (L"NextFormZoomOutAlpha"));
466                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list.");
467         }
468         break;
469
470
471         case FRAME_ANIMATOR_FORM_TRANSITION_ANIMATION_DEPTH_IN:
472         {
473                 r = PrepareCapture();
474                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to capture.");
475
476                 Dimension currentSize = __pCurrentForm->GetSize();
477
478                 Dimension currFormStartSize = currentSize;
479                 Dimension currFormEndSize = Dimension((currentSize.width * 80) / 100, (currentSize.height * 80) / 100);
480                 Dimension nextFormStartSize = Dimension((currentSize.width * 120) / 100, (currentSize.height * 120) / 100);
481                 Dimension nextFormEndSize = currentSize;
482
483                 r = StartLayerAnimation(__pCurrentCaptureVisualElement, ANIMATION_TARGET_ALPHA, Variant(1.0f), Variant(0.0f), transId + (L"CurrentFormDepthInAplha"));
484                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list.");
485
486                 r = StartLayerAnimation(__pCurrentCaptureVisualElement, ANIMATION_TARGET_SIZE, Variant(currFormStartSize), Variant(currFormEndSize), transId + (L"CurrentFormDepthInSize"));
487                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list.");
488
489                 r = StartLayerAnimation(__pNextCaptureVisualElement, ANIMATION_TARGET_ALPHA, Variant(0.0f), Variant(1.0f), transId + (L"NextFormDepthInAplha"));
490                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list.");
491
492                 r = StartLayerAnimation(__pNextCaptureVisualElement, ANIMATION_TARGET_SIZE, Variant(nextFormStartSize), Variant(nextFormEndSize), transId + (L"NextFormDepthInSize"));
493                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list.");
494         }
495         break;
496
497         case FRAME_ANIMATOR_FORM_TRANSITION_ANIMATION_DEPTH_OUT:
498         {
499                 Dimension currentSize = __pCurrentForm->GetSize();
500
501                 Dimension currFormStartSize = currentSize;
502                 Dimension currFormEndSize = Dimension((currentSize.width * 120) / 100, (currentSize.height * 120) / 100);
503                 Dimension nextFormStartSize = Dimension((currentSize.width * 80) / 100, (currentSize.height * 80) / 100);
504                 Dimension nextFormEndSize = currentSize;
505
506                 r = StartLayerAnimation(__pCurrentFormVisualElement, ANIMATION_TARGET_ALPHA, Variant(1.0f), Variant(0.0f), transId + (L"CurrentFormDepthOutAlpha"));
507                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list.");
508
509                 r = StartLayerAnimation(__pNextFormVisualElement, ANIMATION_TARGET_ALPHA, Variant(0.0f), Variant(1.0f), transId + (L"NextFormDepthOutAlpha"));
510                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list.");
511
512                 r = StartLayerAnimation(__pCurrentFormVisualElement, ANIMATION_TARGET_SIZE, Variant(currFormStartSize), Variant(currFormEndSize), transId + (L"CurrentFormDepthOutSize"));
513                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list.");
514
515                 r = StartLayerAnimation(__pNextFormVisualElement, ANIMATION_TARGET_SIZE, Variant(nextFormStartSize), Variant(nextFormEndSize), transId + (L"NextFormDepthOutSize"));
516                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to add animation to play list.");
517         }
518         break;
519
520         default:
521                 SysLogException(NID_UI_ANIM, E_SYSTEM, "A system error has been occurred. FrameAnimatorFormTransitionAnimation is invalid.");
522                 return E_SYSTEM;
523         }
524
525         return E_SUCCESS;
526 }
527
528 result
529 _FrameAnimatorImpl::PrepareCapture(void)
530 {
531         VisualElement* pParentVisualElement = __pNextFormVisualElement->GetParent();
532         SysTryReturnResult(NID_UI_ANIM, (pParentVisualElement), E_SYSTEM, "A system error has been occurred. Failed to get visual element's parent.");
533
534         pParentVisualElement->InsertChild(*__pBgElement, __pNextFormVisualElement, true);
535
536         // remove dummy surface
537         __pCurrentCaptureVisualElement->SetSurface(null);
538         __pNextCaptureVisualElement->SetSurface(null);
539
540         // set default value
541         __pCurrentCaptureVisualElement->SetOpacity(1.0f);
542         __pNextCaptureVisualElement->SetOpacity(1.0f);
543
544         __pCurrentCaptureVisualElement->SetTransformMatrix(__pCurrentFormVisualElement->GetTransformMatrix());
545         __pNextCaptureVisualElement->SetTransformMatrix(__pNextFormVisualElement->GetTransformMatrix());
546
547         __pCurrentCaptureVisualElement->SetShowState(true);
548         __pNextCaptureVisualElement->SetShowState(true);
549
550         FloatRectangle currentBounds = __pCurrentFormVisualElement->GetBounds();
551         __pCurrentCaptureVisualElement->SetBounds(currentBounds);
552
553         FloatRectangle nextBounds = __pNextFormVisualElement->GetBounds();
554         __pNextCaptureVisualElement->SetBounds(nextBounds);
555
556         // drawing
557         __pCurrentFormVisualElement->Draw();
558         __pNextFormVisualElement->Draw();
559
560         Canvas* pCanvas = null;
561
562         pCanvas = __pCurrentCaptureVisualElement->GetCanvasN();
563         SysTryReturnResult(NID_UI_ANIM, (pCanvas), E_SYSTEM, "A system error has been occurred. Failed to get canvas.");
564
565         currentBounds.SetPosition(0, 0);
566         _VisualElementImpl::GetInstance(*__pCurrentFormVisualElement)->Capture(*pCanvas, currentBounds, true);
567         delete pCanvas;
568
569         pCanvas = __pNextCaptureVisualElement->GetCanvasN();
570         SysTryReturnResult(NID_UI_ANIM, (pCanvas), E_SYSTEM, "A system error has been occurred. Failed to get canvas.");
571
572         nextBounds.SetPosition(0, 0);
573         _VisualElementImpl::GetInstance(*__pNextFormVisualElement)->Capture(*pCanvas, nextBounds, true);
574         delete pCanvas;
575
576         return E_SUCCESS;
577 }
578
579
580 result
581 _FrameAnimatorImpl::StartLayerAnimation(VisualElement* pLayer, AnimationTargetType animTarget, Variant startValue, Variant endValue, String animName)
582 {
583         result r = E_SUCCESS;
584
585         Dimension startSize, endSize;
586         Point startPoint, endPoint;
587
588         VisualElementPropertyAnimation* propAnimation = null;
589         propAnimation = new (std::nothrow) VisualElementPropertyAnimation();
590         SysTryReturnResult(NID_UI_ANIM, (propAnimation), E_OUT_OF_MEMORY, "Memory allocation failed.");
591
592         propAnimation->SetDuration(__duration);
593
594         const IVisualElementAnimationTimingFunction* pTimingFunction = null;
595         const wchar_t* pVePropType = null;
596         Rectangle rect = __pCurrentForm->GetBounds();
597
598         switch (__animInterpolatorType)
599         {
600         case ANIMATION_INTERPOLATOR_LINEAR:
601                 pTimingFunction = VisualElementAnimation::GetTimingFunctionByName(L"Linear"); //INTERPOLATION_LINEAR
602                 break;
603
604         case ANIMATION_INTERPOLATOR_EASE_IN:
605                 pTimingFunction = VisualElementAnimation::GetTimingFunctionByName(L"EaseIn"); //INTERPOLATION_EASE_IN
606                 break;
607
608         case ANIMATION_INTERPOLATOR_EASE_OUT:
609                 pTimingFunction = VisualElementAnimation::GetTimingFunctionByName(L"EaseOut"); //INTERPOLATION_EASE_OUT
610                 break;
611
612         case ANIMATION_INTERPOLATOR_EASE_IN_OUT:
613                 pTimingFunction = VisualElementAnimation::GetTimingFunctionByName(L"EaseInOut"); //INTERPOLATION_EASE_INOUT
614                 break;
615
616         case ANIMATION_INTERPOLATOR_DISCRETE:
617                 pTimingFunction = VisualElementAnimation::GetTimingFunctionByName(L"Discrete"); //INTERPOLATION_DISCRETE
618                 break;
619
620         case ANIMATION_INTERPOLATOR_BEZIER:
621         {
622                 pTimingFunction = new (std::nothrow) BezierTimingFunction();
623                 SysTryCatch(NID_UI_ANIM, (pTimingFunction), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to get TimingFunction.");
624
625                 ((BezierTimingFunction*) pTimingFunction)->SetControlPoints(__firstBezierControlPointsTime, __firstBezierControlPointsValue, __secondBezierControlPointsTime, __secondBezierControlPointsValue);
626         }
627         break;
628
629         default:
630                 SysTryCatch(NID_UI_ANIM, false, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Animation interpolator is invalid. ");
631                 break;
632         }
633
634         SysTryCatch(NID_UI_ANIM, (pTimingFunction), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to get TimingFunction.");
635         propAnimation->SetTimingFunction(pTimingFunction);
636
637         switch(startValue.GetType())
638         {
639         case VARIANT_TYPE_POINT:
640                 startPoint = startValue.ToPoint();
641                 endPoint = endValue.ToPoint();
642                 break;
643         case VARIANT_TYPE_DIMENSION:
644                 startSize = startValue.ToDimension();
645                 endSize = endValue.ToDimension();
646                 break;
647         default: break;
648         }
649
650         switch (animTarget)
651         {
652                 case ANIMATION_TARGET_SIZE:
653                 {
654                         float anchorX = 0.0f;
655                         float anchorY = 0.0f;
656                         float anchorZ = 0.0f;
657
658                         float scaleX = 1.0f;
659                         float scaleY = 1.0f;
660                         float scaleZ = 1.0f;
661
662                         pVePropType = VePropTransform;
663
664                         anchorX = (float) rect.width * anchorX;
665                         anchorY = (float) rect.height * anchorY;
666
667                         scaleX = (float) startSize.width / (float) rect.width;
668                         scaleY = (float) startSize.height / (float) rect.height;
669
670                         Tizen::Graphics::FloatMatrix4 startMatrix = Tizen::Graphics::FloatMatrix4();
671                         //startMatrix.SetAsIdentity(); // default value is identity matrix
672                         _MatrixUtilScale(startMatrix, scaleX, scaleY, scaleZ);
673                         _MatrixUtilAtAnchor(startMatrix, anchorX, anchorY, anchorZ);
674
675                         scaleX = (float) endSize.width / (float) rect.width;
676                         scaleY = (float) endSize.height / (float) rect.height;
677
678                         Tizen::Graphics::FloatMatrix4 endMatrix = Tizen::Graphics::FloatMatrix4();
679                         //endMatrix.SetAsIdentity(); // default value is identity matrix
680                         _MatrixUtilScale(endMatrix, scaleX, scaleY, scaleZ);
681                         _MatrixUtilAtAnchor(endMatrix, anchorX, anchorY, anchorZ);
682
683                         propAnimation->SetStartValue(Variant(startMatrix));
684                         propAnimation->SetEndValue(Variant(endMatrix));
685                 }
686                 break;
687
688                 case ANIMATION_TARGET_POSITION:
689                 {
690                         pVePropType = VePropBounds;
691
692                         FloatRectangle rect;
693
694                         rect = pLayer->GetBounds();
695                         rect.x = startPoint.x;
696                         rect.y = startPoint.y;
697                         propAnimation->SetStartValue(Variant(rect));
698
699                         rect.x = endPoint.x;
700                         rect.y = endPoint.y;
701                         propAnimation->SetEndValue(Variant(rect));
702                 }
703                 break;
704
705                 case ANIMATION_TARGET_ALPHA:
706                 {
707                         pVePropType = VePropOpacity;
708                         propAnimation->SetStartValue(startValue);
709                         propAnimation->SetEndValue(endValue);
710                 }
711                 break;
712
713                 case ANIMATION_TARGET_ROTATION:
714                 {
715                         float anchorX = 0.0f;
716                         float anchorY = 0.0f;
717
718                         pVePropType = VeSubPropTransformRotationZ;
719                         _VisualElementImpl::GetInstance(*pLayer)->SetAnchor(FloatPoint(anchorX, anchorY));
720
721                         propAnimation->SetStartValue(startValue);
722                         propAnimation->SetEndValue(endValue);
723                 }
724                 break;
725
726                 default:
727                 {
728                         SysTryCatch(NID_UI_ANIM, false, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] animTarget is invalid.");
729                 }
730                 break;
731         }
732
733         SysTryCatch(NID_UI_ANIM, (pVePropType), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Animation property type is invalid.");
734         propAnimation->SetPropertyName(pVePropType);
735
736         r = pLayer->AddAnimation(animName, *propAnimation);
737         SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), r = E_SYSTEM, r, "[E_SYSTEM] A system error has been occurred. Failed to add animation to visual element.");
738
739         r = AddFrameActiveAnimation(pLayer, __animationEffectType, propAnimation, const_cast < IVisualElementAnimationTimingFunction* >(pTimingFunction), animName);
740         SysTryCatch(NID_UI_ANIM, (r == E_SUCCESS), r = E_SYSTEM, r, "[E_SYSTEM] A system error has been occurred. Failed to add frame active animation to the playing list.");
741
742         return E_SUCCESS;
743
744 CATCH:
745         delete propAnimation;
746
747         SysLogException(NID_UI_ANIM, E_SYSTEM, "A system error has been occurred. Failed to start frame animation.");
748         return E_SYSTEM;
749 }
750
751 result
752 _FrameAnimatorImpl::AddFrameActiveAnimation(VisualElement* _pVisualElement, FrameAnimatorFormTransitionAnimation _formAnimation,
753                                                                                         VisualElementPropertyAnimation* _pPropertyAnimation, IVisualElementAnimationTimingFunction* _pTimingFunction,
754                                                                                         String _animName)
755 {
756         SysTryReturnResult(NID_UI_ANIM
757                                                 , (_formAnimation >= FRAME_ANIMATOR_FORM_TRANSITION_ANIMATION_TRANSLATE_LEFT
758                                                 && _formAnimation < FRAME_ANIMATOR_FORM_TRANSITION_ANIMATION_MAX)
759                                                 , E_INVALID_ARG, "Invalid argument(s) is used. FormTransitionType argument is invalid.");
760
761         result r = E_SUCCESS;
762         FrameActiveAnimation frameActiveAnim;
763
764         frameActiveAnim.pVisualElement = _pVisualElement;
765         frameActiveAnim.formTransitionAnimationType = _formAnimation;
766         frameActiveAnim.pPropertyAnimation = _pPropertyAnimation;
767         frameActiveAnim.pTimingFunction = _pTimingFunction;
768         frameActiveAnim.animName = _animName;
769
770         r = __frameActiveAnimationList.Add(frameActiveAnim);
771         SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), r, "Propagating.");
772
773         return r;
774 }
775
776 Tizen::Ui::Animations::VisualElementPropertyAnimation*
777 _FrameAnimatorImpl::GetFrameActiveAnimationAt(int index)
778 {
779         SysTryReturn(NID_UI_ANIM, (index < __frameActiveAnimationList.GetCount() && (index >= 0)),
780                                  null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument(s) is used. Index (%d) is out of range.");
781
782         FrameActiveAnimation frameActiveAnim;
783         result r = __frameActiveAnimationList.GetAt(index, frameActiveAnim);
784
785         SysTryReturn(NID_UI_ANIM, (r == E_SUCCESS), null, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed  to retrieve FrameActiveAnimation.");
786         return frameActiveAnim.pPropertyAnimation;
787 }
788
789 result
790 _FrameAnimatorImpl::RemoveAnimationAt(int index)
791 {
792         result r = E_SUCCESS;
793         SysTryReturnResult(NID_UI_ANIM, (index >= 0 && index < __frameActiveAnimationList.GetCount()), E_OUT_OF_RANGE, "Index (%d) is out of range.", index);
794
795         FrameActiveAnimation frameActiveAnim;
796
797         r = __frameActiveAnimationList.GetAt(index, frameActiveAnim);
798         SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "A system error has been occurred. Failed to retrieve FrameActiveAnimation.");
799
800         r = __frameActiveAnimationList.RemoveAt(index);
801         SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), E_SYSTEM, "A system error has been occurred. Failed to remove animation from play list.");
802
803         if (__animInterpolatorType == ANIMATION_INTERPOLATOR_BEZIER)
804         {
805                 frameActiveAnim.pPropertyAnimation->SetTimingFunction(null);
806                 delete frameActiveAnim.pTimingFunction;
807                 frameActiveAnim.pTimingFunction = null;
808         }
809
810         delete frameActiveAnim.pPropertyAnimation;
811         frameActiveAnim.pPropertyAnimation = null;
812
813         return r;
814 }
815
816 result
817 _FrameAnimatorImpl::RemoveAllAnimations(bool deleteAnimation)
818 {
819         VisualElement* pParentVisualElement = __pBgElement->GetParent();
820         if (pParentVisualElement)
821         {
822                 pParentVisualElement->DetachChild(*__pBgElement);
823
824                 __pCurrentCaptureVisualElement->SetShowState(false);
825                 __pNextCaptureVisualElement->SetShowState(false);
826
827                 __pCurrentCaptureVisualElement->SetSurface(__pDummySurface);
828                 __pNextCaptureVisualElement->SetSurface(__pDummySurface);
829         }
830
831         if (deleteAnimation)
832         {
833                 __pNextFormVisualElement->RemoveAllAnimations();
834                 __pCurrentFormVisualElement->RemoveAllAnimations();
835         }
836
837         __transactionId = -1;
838
839         result r = E_SUCCESS;
840
841         for (int index = __frameActiveAnimationList.GetCount() - 1; index >= 0; index--)
842         {
843                 r = RemoveAnimationAt(index);
844                 SysTryReturnResult(NID_UI_ANIM, (r == E_SUCCESS), r, "Propagating.");
845         }
846
847         return r;
848 }
849
850 bool
851 _FrameAnimatorImpl::IsAnimationSupported(void) const
852 {
853         return _VisualElementAnimationImpl::IsAnimationSupported();
854 }
855
856 bool
857 _FrameAnimatorImpl::IsFormPresent(Tizen::Ui::Controls::Form& pPrevForm) const
858 {
859         LinkedList* pLinkedList = null;
860
861         pLinkedList = dynamic_cast< LinkedList* >(__pFrame->GetControls());
862                 SysTryReturn(NID_UI_ANIM, (pLinkedList), false, E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Failed to get control list.");
863
864         bool isPresent = pLinkedList->Contains(pPrevForm);
865
866         if (isPresent)
867         {
868                 return true;
869         }
870         else
871         {
872                 return false;
873         }
874 }
875
876 void
877 _FrameAnimatorImpl::OnAnimationTransactionStarted(int transactionId)
878 {
879         OnFrameAnimationStarted();
880 }
881
882
883 void
884 _FrameAnimatorImpl::OnAnimationTransactionStopped(int transactionId)
885 {
886         OnFrameAnimationStopped(false);
887 }
888
889 void
890 _FrameAnimatorImpl::OnAnimationTransactionFinished(int transactionId)
891 {
892         OnFrameAnimationStopped(true);
893 }
894
895 void
896 _FrameAnimatorImpl::OnFrameAnimationStarted(void)
897 {
898         result r = E_SUCCESS;
899
900         for (int index = __frameAnimStatusEventListener.GetCount() - 1; index >= 0; index--)
901         {
902                 IFrameAnimatorEventListener* pStatusListener = null;
903
904                 r = __frameAnimStatusEventListener.GetAt(index, pStatusListener);
905                 SysTryReturnVoidResult(NID_UI_ANIM, (r == E_SUCCESS), E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] Index (%d) is out of range.", index);
906
907                 pStatusListener->OnFormTransitionAnimationStarted(*__pFrameAnimator, *__pFrame, *__pCurrentForm, *__pNextForm);
908         }
909 }
910
911 void
912 _FrameAnimatorImpl::OnFrameAnimationStopped(bool completed)
913 {
914         result r = E_SUCCESS;
915
916         if (IsFormPresent(*__pCurrentForm))
917         {
918                 __pCurrentFormVisualElement->SetTransformMatrix(__veDefaultMatrix);
919                 __pCurrentFormVisualElement->SetBounds(__veDefaultBounds);
920                 __pCurrentFormVisualElement->SetOpacity(__veDefaultOpacity);
921                 __pCurrentFormVisualElement->SetShowState(false);
922         }
923
924         RemoveAllAnimations(false);
925
926         for (int index = __frameAnimStatusEventListener.GetCount() - 1; index >= 0; index--)
927         {
928                 IFrameAnimatorEventListener* pStatusListener = null;
929
930                 r = __frameAnimStatusEventListener.GetAt(index, pStatusListener);
931                 SysTryReturnVoidResult(NID_UI_ANIM, (r == E_SUCCESS), E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] Index (%d) is out of range.", index);
932
933                 if (!completed)
934                 {
935                         pStatusListener->OnFormTransitionAnimationStopped(*__pFrameAnimator, *__pFrame, *__pCurrentForm, *__pNextForm);
936                 }
937                 else
938                 {
939                         pStatusListener->OnFormTransitionAnimationFinished(*__pFrameAnimator, *__pFrame, *__pCurrentForm, *__pNextForm);
940                 }
941         }
942
943         __frameAnimatorStatus = ANIMATOR_STATUS_STOPPED;
944 }
945
946 _FrameAnimatorImpl*
947 _FrameAnimatorImpl::GetInstance(FrameAnimator& frameAnimator)
948 {
949         return frameAnimator._pFrameAnimatorImpl;
950 }
951
952 const _FrameAnimatorImpl*
953 _FrameAnimatorImpl::GetInstance(const FrameAnimator& frameAnimator)
954 {
955         return frameAnimator._pFrameAnimatorImpl;
956 }
957
958 }}}   // Tizen::Ui::Animations
959