Tizen 2.1 base
[sdk/ide/native-sample.git] / samples / native / partner / cpp / Sample / Tizen C++ / UiControlAnimator / UiControlAnimator / project / src / SampleKeyFrameAnimation.cpp
1 //
2 // Tizen C++ SDK
3 // Copyright (c) 2012 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://www.tizenopensource.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 #include "SampleKeyFrameAnimation.h"
19 #include "AnimationForm.h"
20
21 using namespace Osp::Graphics;
22 using namespace Osp::Ui;
23 using namespace Osp::Ui::Controls;
24 using namespace Osp::Ui::Animations;
25
26 SampleKeyFrameAnimation::SampleKeyFrameAnimation(AnimationForm& animationForm)
27         : AnimationManager(animationForm)
28         , __pButton1(null)
29         , __pButton2(null)
30 {
31 }
32
33 SampleKeyFrameAnimation::~SampleKeyFrameAnimation(void)
34 {
35 }
36
37 void
38 SampleKeyFrameAnimation::Initialize(void)
39 {
40         int offset = 50;
41
42         __pButton1 = new (std::nothrow) Button();
43         __pButton1->Construct(Rectangle(0+offset,0+offset,__buttonSize,__buttonSize),L"I");
44         __pAnimationForm->AddControl(*__pButton1);
45
46         __pButton2 = new (std::nothrow) Button();
47         __pButton2->Construct(Rectangle(__clientSize.width-__buttonSize-offset,0+offset,__buttonSize,__buttonSize),L"II");
48         __pAnimationForm->AddControl(*__pButton2);
49 }
50
51 void
52 SampleKeyFrameAnimation::PlayAnimation(void)
53 {
54         result r = E_SUCCESS;
55
56         int offset = 50;
57
58         Point controlPosition1 = __pButton1->GetPosition();
59         Point controlPosition2 = __pButton2->GetPosition();
60
61         Point finalPosition1(0+offset,__clientSize.height-__buttonSize-offset);
62         Point finalPosition2(__clientSize.width-__buttonSize-offset,__clientSize.height-__buttonSize-offset);
63
64         PointAnimation pointAnimation1(__clientCenter,__clientCenter,1000,ANIMATION_INTERPOLATOR_LINEAR);
65         AnimationManager::CheckAnimationSuccessStatus(pointAnimation1);
66         PointAnimation pointAnimation2(__clientCenter,__clientCenter,1000,ANIMATION_INTERPOLATOR_LINEAR);
67         AnimationManager::CheckAnimationSuccessStatus(pointAnimation2);
68
69         //Initialize Animation
70         if (controlPosition1 == finalPosition1)
71         {
72                 pointAnimation1 = PointAnimation(Point(0+offset,__clientSize.height-__buttonSize-offset),Point(0+offset,0+offset),1000,ANIMATION_INTERPOLATOR_LINEAR);
73         }
74         else
75         {
76                 pointAnimation1 = PointAnimation(Point(0+offset,0+offset),Point(0+offset,__clientSize.height-__buttonSize-offset),1000,ANIMATION_INTERPOLATOR_LINEAR);
77         }
78         AnimationManager::CheckAnimationSuccessStatus(pointAnimation1);
79         AnimationManager::InitializeAnimationSettings(pointAnimation1);
80
81         if (controlPosition2 == finalPosition2)
82         {
83                 pointAnimation2 = PointAnimation(Point(__clientSize.width-__buttonSize-offset,__clientSize.height-__buttonSize-offset),Point(__clientSize.width-__buttonSize-offset,0+offset),1000,ANIMATION_INTERPOLATOR_LINEAR);
84         }
85         else
86         {
87                 pointAnimation2 = PointAnimation(Point(__clientSize.width-__buttonSize-offset,0+offset),Point(__clientSize.width-__buttonSize-offset,__clientSize.height-__buttonSize-offset),1000,ANIMATION_INTERPOLATOR_LINEAR);
88         }
89         AnimationManager::CheckAnimationSuccessStatus(pointAnimation2);
90         AnimationManager::InitializeAnimationSettings(pointAnimation2);
91
92         //Add KeyFrame to Animation
93         pointAnimation1.AddKeyFrame(__duration/2,__clientCenter);
94         pointAnimation2.AddKeyFrame(__duration/2,__clientCenter);
95
96         //Initialize Animator
97         ControlAnimator* pAnimator1 = __pButton1->GetControlAnimator();
98         AnimationManager::CheckAnimatorSuccessStatus(*pAnimator1);
99         ControlAnimator* pAnimator2 = __pButton2->GetControlAnimator();
100         AnimationManager::CheckAnimatorSuccessStatus(*pAnimator2);
101
102         //Start Animation
103         r = pAnimator1->StartUserAnimation(ANIMATION_TARGET_POSITION,pointAnimation1);
104         AnimationManager::CheckAnimationSuccessStatus(r,L"StartAnimation");
105
106         r = pAnimator2->StartUserAnimation(ANIMATION_TARGET_POSITION,pointAnimation2);
107         AnimationManager::CheckAnimationSuccessStatus(r,L"StartAnimation");
108 }
109
110 void
111 SampleKeyFrameAnimation::StopAnimation(void)
112 {
113         result r = E_SUCCESS;
114
115         for (int i=0; i<__pAnimationForm->GetControlCount(); ++i)
116         {
117                 Control* pControl = __pAnimationForm->GetControl(i);
118                 ControlAnimator* pAnimator = pControl->GetControlAnimator();
119                 AnimationManager::CheckAnimatorSuccessStatus(*pAnimator);
120
121                 if (pAnimator->GetStatus() == ANIMATOR_STATUS_PLAYING)
122                 {
123                         r = pAnimator->StopAllAnimations();
124                         CheckAnimationSuccessStatus(r,L"StopAnimation");
125                 }
126         }
127 }