Tizen 2.1 base
[sdk/ide/native-sample.git] / samples / native / cpp / Sample / Tizen C++ / UiControlAnimator / UiControlAnimator / project / src / SampleCustomImplicitAnimation.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 "SampleCustomImplicitAnimation.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 SampleCustomImplicitAnimation::SampleCustomImplicitAnimation(AnimationForm& animationForm)
27         : AnimationManager(animationForm)
28         , __pButton1(null)
29         , __pButton2(null)
30         , __pButton3(null)
31         , __pButton4(null)
32 {
33 }
34
35 SampleCustomImplicitAnimation::~SampleCustomImplicitAnimation(void)
36 {
37 }
38
39 void
40 SampleCustomImplicitAnimation::Initialize(void)
41 {
42         int offset = 50;
43         int buttonWidth = __buttonSize + offset;
44         int buttonHeight = __buttonSize/2;
45
46         __pButton1 = new (std::nothrow) Button();
47         __pButton1->Construct(Rectangle(0+offset,0+offset,buttonWidth,buttonHeight),L"Position");
48         __pAnimationForm->AddControl(*__pButton1);
49
50         __pButton2 = new (std::nothrow) Button();
51         __pButton2->Construct(Rectangle(0+offset,0+3*offset,buttonWidth,buttonHeight),L"Size");
52         __pAnimationForm->AddControl(*__pButton2);
53
54         __pButton3 = new (std::nothrow) Button();
55         __pButton3->Construct(Rectangle(0+offset,0+5*offset,buttonWidth,buttonHeight),L"Bounds");
56         __pAnimationForm->AddControl(*__pButton3);
57
58         __pButton4 = new (std::nothrow) Button();
59         __pButton4->Construct(Rectangle(0+offset,0+7*offset,buttonWidth,buttonHeight),L"ShowState");
60         __pAnimationForm->AddControl(*__pButton4);
61 }
62
63 void
64 SampleCustomImplicitAnimation::PlayAnimation(void)
65 {
66         result r = E_SUCCESS;
67
68         int offset = 50;
69         int buttonWidth = __buttonSize + offset;
70         int buttonHeight = __buttonSize/2;
71
72         Point controlPosition1 = __pButton1->GetPosition();
73         Dimension controlSize2 = __pButton2->GetSize();
74         Rectangle controlBounds3 = __pButton3->GetBounds();
75         bool controlShowState4 = __pButton4->GetShowState();
76
77         Point finalPosition1 = Point(__clientCenter.x,0+offset);
78         Dimension finalSize2 = Dimension(buttonWidth+offset,buttonWidth-offset-20);
79         Rectangle finalBounds3 = Rectangle(__clientCenter.x,0+5*offset,buttonWidth+offset,buttonWidth-offset-20);
80
81         PointAnimation pointAnimation1(__clientCenter,__clientCenter,1000,ANIMATION_INTERPOLATOR_LINEAR);
82         AnimationManager::CheckAnimationSuccessStatus(pointAnimation1);
83         DimensionAnimation dimensionAnimation2(Dimension(__buttonSize,__buttonSize),Dimension(__buttonSize,__buttonSize),1000,ANIMATION_INTERPOLATOR_LINEAR);
84         AnimationManager::CheckAnimationSuccessStatus(dimensionAnimation2);
85         RectangleAnimation rectangleAnimation3(Rectangle(0,0,buttonWidth,buttonHeight),Rectangle(__clientCenter.x,0+offset,buttonWidth+offset,buttonHeight+offset-20),1000,ANIMATION_INTERPOLATOR_LINEAR);
86         AnimationManager::CheckAnimationSuccessStatus(rectangleAnimation3);
87         FloatAnimation floatAnimation4(0,0,1000,ANIMATION_INTERPOLATOR_LINEAR);
88         AnimationManager::CheckAnimationSuccessStatus(floatAnimation4);
89         RotateAnimation rotateAnimation(0,0,1000,ANIMATION_INTERPOLATOR_LINEAR);
90         AnimationManager::CheckAnimationSuccessStatus(rotateAnimation);
91
92         //Initialize Animator
93         ControlAnimator* pAnimator1 = __pButton1->GetControlAnimator();
94         AnimationManager::CheckAnimatorSuccessStatus(*pAnimator1);
95         ControlAnimator* pAnimator2 = __pButton2->GetControlAnimator();
96         AnimationManager::CheckAnimatorSuccessStatus(*pAnimator2);
97         ControlAnimator* pAnimator3 = __pButton3->GetControlAnimator();
98         AnimationManager::CheckAnimatorSuccessStatus(*pAnimator3);
99         ControlAnimator* pAnimator4 = __pButton4->GetControlAnimator();
100         AnimationManager::CheckAnimatorSuccessStatus(*pAnimator4);
101
102         // Initialize Animation Group
103         ParallelAnimationGroup animationGroup1;
104         AnimationManager::CheckAnimationSuccessStatus(animationGroup1);
105         ParallelAnimationGroup animationGroup2;
106         AnimationManager::CheckAnimationSuccessStatus(animationGroup2);
107         ParallelAnimationGroup animationGroup3a;
108         AnimationManager::CheckAnimationSuccessStatus(animationGroup3a);
109         ParallelAnimationGroup animationGroup3b;
110         AnimationManager::CheckAnimationSuccessStatus(animationGroup3b);
111         ParallelAnimationGroup animationGroup4;
112         AnimationManager::CheckAnimationSuccessStatus(animationGroup4);
113
114         //Initialize Animations
115         if (controlPosition1 == finalPosition1)
116         {
117                 pointAnimation1 = PointAnimation(Point(__clientCenter.x,0+offset),Point(0+offset,0+offset),1000,ANIMATION_INTERPOLATOR_LINEAR);
118         }
119         else
120         {
121                 pointAnimation1 = PointAnimation(Point(0+offset,0+offset),Point(__clientCenter.x,0+offset),1000,ANIMATION_INTERPOLATOR_LINEAR);
122         }
123         AnimationManager::CheckAnimationSuccessStatus(r,L"SetPosition");
124
125         if (controlSize2 == finalSize2)
126         {
127                 dimensionAnimation2 = DimensionAnimation(Dimension(buttonWidth+offset,buttonWidth-offset-20),Dimension(buttonWidth,buttonHeight),1000,ANIMATION_INTERPOLATOR_LINEAR);
128         }
129         else
130         {
131                 dimensionAnimation2 = DimensionAnimation(Dimension(buttonWidth,buttonHeight),Dimension(buttonWidth+offset,buttonWidth-offset-20),1000,ANIMATION_INTERPOLATOR_LINEAR);
132         }
133         AnimationManager::CheckAnimationSuccessStatus(r,L"SetSize");
134
135         if (controlBounds3 == finalBounds3)
136         {
137                 rectangleAnimation3 = RectangleAnimation(Rectangle(__clientCenter.x,0+5*offset,buttonWidth+offset,buttonWidth-offset-20),Rectangle(0+offset,0+5*offset,buttonWidth,buttonHeight),1000,ANIMATION_INTERPOLATOR_LINEAR);
138         }
139         else
140         {
141                 rectangleAnimation3 = RectangleAnimation(Rectangle(0+offset,0+5*offset,buttonWidth,buttonHeight),Rectangle(__clientCenter.x,0+5*offset,buttonWidth+offset,buttonWidth-offset-20),1000,ANIMATION_INTERPOLATOR_LINEAR);
142         }
143         AnimationManager::CheckAnimationSuccessStatus(r,L"SetBounds");
144
145         if (controlShowState4 == true)
146         {
147                 floatAnimation4 = FloatAnimation((1.0),(0.0),1000,ANIMATION_INTERPOLATOR_LINEAR);
148         }
149         else
150         {
151                 floatAnimation4 = FloatAnimation((0.0),(1.0),1000,ANIMATION_INTERPOLATOR_LINEAR);
152         }
153         AnimationManager::CheckAnimationSuccessStatus(r,L"SetShowState");
154
155         if (controlPosition1 == finalPosition1 && controlSize2 == finalSize2)
156         {
157                 rotateAnimation = RotateAnimation(0,360,1000,ANIMATION_INTERPOLATOR_LINEAR);
158         }
159         else
160         {
161                 rotateAnimation = RotateAnimation(360,0,1000,ANIMATION_INTERPOLATOR_LINEAR);
162         }
163         AnimationManager::CheckAnimationSuccessStatus(rotateAnimation);
164         r = rotateAnimation.SetAnchor(0.5,0.5);
165         AnimationManager::CheckAnimationSuccessStatus(r,L"SetAnchor");
166
167         //Add Animations to Animation Group
168         r = animationGroup1.AddAnimation(ANIMATION_TARGET_ROTATION,rotateAnimation);
169         AnimationManager::CheckAnimationSuccessStatus(r,L"AddAnimation");
170         r = animationGroup1.AddAnimation(ANIMATION_TARGET_POSITION,pointAnimation1);
171         AnimationManager::CheckAnimationSuccessStatus(r,L"AddAnimation");
172
173         r = animationGroup2.AddAnimation(ANIMATION_TARGET_ROTATION,rotateAnimation);
174         AnimationManager::CheckAnimationSuccessStatus(r,L"AddAnimation");
175         r = animationGroup2.AddAnimation(ANIMATION_TARGET_SIZE,dimensionAnimation2);
176         AnimationManager::CheckAnimationSuccessStatus(r,L"AddAnimation");
177
178         r = animationGroup3a.AddAnimation(ANIMATION_TARGET_ROTATION,rotateAnimation);
179         AnimationManager::CheckAnimationSuccessStatus(r,L"AddAnimation");
180         r = animationGroup3a.AddAnimation(ANIMATION_TARGET_POSITION,rectangleAnimation3);
181         AnimationManager::CheckAnimationSuccessStatus(r,L"AddAnimation");
182
183         r = animationGroup3b.AddAnimation(ANIMATION_TARGET_SIZE,rectangleAnimation3);
184         AnimationManager::CheckAnimationSuccessStatus(r,L"AddAnimation");
185
186         r = animationGroup4.AddAnimation(ANIMATION_TARGET_ROTATION,rotateAnimation);
187         AnimationManager::CheckAnimationSuccessStatus(r,L"AddAnimation");
188         r = animationGroup4.AddAnimation(ANIMATION_TARGET_ALPHA,floatAnimation4);
189         AnimationManager::CheckAnimationSuccessStatus(r,L"AddAnimation");
190
191         //Set Animation
192         r = pAnimator1->SetAnimation(ANIMATION_TRIGGER_POSITION_CHANGE,&animationGroup1);
193         AnimationManager::CheckAnimationSuccessStatus(r,L"SetAnimation");
194
195         r = pAnimator2->SetAnimation(ANIMATION_TRIGGER_SIZE_CHANGE,&animationGroup2);
196         AnimationManager::CheckAnimationSuccessStatus(r,L"SetAnimation");
197
198         r = pAnimator3->SetAnimation(ANIMATION_TRIGGER_POSITION_CHANGE,&animationGroup3a);
199         AnimationManager::CheckAnimationSuccessStatus(r,L"SetAnimation");
200         r = pAnimator3->SetAnimation(ANIMATION_TRIGGER_SIZE_CHANGE,&animationGroup3b);
201         AnimationManager::CheckAnimationSuccessStatus(r,L"SetAnimation");
202
203         r = pAnimator4->SetAnimation(ANIMATION_TRIGGER_SHOW_STATE_CHANGE,&animationGroup4);
204         AnimationManager::CheckAnimationSuccessStatus(r,L"SetAnimation");
205
206         //Start Custom Implicit animation
207         if (controlPosition1 == finalPosition1)
208         {
209                 r = pAnimator1->SetPosition(Point(0+offset,0+offset));
210         }
211         else
212         {
213                 r = pAnimator1->SetPosition(Point(__clientCenter.x,0+offset));
214         }
215         AnimationManager::CheckAnimationSuccessStatus(r,L"SetPosition");
216
217         if (controlSize2 == finalSize2)
218         {
219                 r = pAnimator2->SetSize(Dimension(buttonWidth,buttonHeight));
220         }
221         else
222         {
223                 r = pAnimator2->SetSize(Dimension(buttonWidth+offset,buttonWidth-offset-20));
224         }
225         AnimationManager::CheckAnimationSuccessStatus(r,L"SetSize");
226
227         if (controlBounds3 == finalBounds3)
228         {
229                 r = pAnimator3->SetBounds(Rectangle(0+offset,0+5*offset,buttonWidth,buttonHeight));
230         }
231         else
232         {
233                 r = pAnimator3->SetBounds(Rectangle(__clientCenter.x,0+5*offset,buttonWidth+offset,buttonWidth-offset-20));
234         }
235         AnimationManager::CheckAnimationSuccessStatus(r,L"SetBounds");
236
237         if (controlShowState4 == true)
238         {
239                 r = pAnimator4->SetShowState(false);
240         }
241         else
242         {
243                 r = pAnimator4->SetShowState(true);
244         }
245         AnimationManager::CheckAnimationSuccessStatus(r,L"SetShowState");
246 }
247
248 void
249 SampleCustomImplicitAnimation::StopAnimation(void)
250 {
251         result r = E_SUCCESS;
252
253         //Initialize Animator
254         ControlAnimator* pAnimator1 = __pButton1->GetControlAnimator();
255         AnimationManager::CheckAnimatorSuccessStatus(*pAnimator1);
256         ControlAnimator* pAnimator2 = __pButton2->GetControlAnimator();
257         AnimationManager::CheckAnimatorSuccessStatus(*pAnimator2);
258         ControlAnimator* pAnimator3 = __pButton3->GetControlAnimator();
259         AnimationManager::CheckAnimatorSuccessStatus(*pAnimator3);
260         ControlAnimator* pAnimator4 = __pButton4->GetControlAnimator();
261         AnimationManager::CheckAnimatorSuccessStatus(*pAnimator4);
262
263         if (pAnimator1->GetStatus() == ANIMATOR_STATUS_PLAYING)
264         {
265                 r = pAnimator1->StopAnimation(ANIMATION_TRIGGER_POSITION_CHANGE);
266                 CheckAnimationSuccessStatus(r,L"StopAnimation");
267         }
268         if (pAnimator2->GetStatus() == ANIMATOR_STATUS_PLAYING)
269         {
270                 r = pAnimator2->StopAnimation(ANIMATION_TRIGGER_SIZE_CHANGE);
271                 CheckAnimationSuccessStatus(r,L"StopAnimation");
272         }
273         if (pAnimator3->GetStatus() == ANIMATOR_STATUS_PLAYING)
274         {
275                 r = pAnimator3->StopAnimation(ANIMATION_TRIGGER_SIZE_CHANGE);
276                 CheckAnimationSuccessStatus(r,L"StopAnimation");
277
278                 r = pAnimator3->StopAnimation(ANIMATION_TRIGGER_POSITION_CHANGE);
279                 CheckAnimationSuccessStatus(r,L"StopAnimation");
280         }
281         if (pAnimator4->GetStatus() == ANIMATOR_STATUS_PLAYING)
282         {
283                 r = pAnimator4->StopAnimation(ANIMATION_TRIGGER_SHOW_STATE_CHANGE);
284                 CheckAnimationSuccessStatus(r,L"StopAnimation");
285         }
286 }