[NUI.Sample] BlendPoint demo
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / BlendPointSample.cs
1 using Tizen.NUI;
2 using Tizen.NUI.BaseComponents;
3 using Tizen.NUI.Components;
4 using System;
5
6 namespace Tizen.NUI.Samples
7 {
8     public class BlendPointSample : IExample
9     {
10         // Layout base
11         private Window window;
12         private View root;
13         private View header;
14         private View contents;
15         private View footer;
16
17         private View layoutOriginRoot;
18         private View layoutBlendRoot;
19
20         private bool contentsLayoutVertical = true;
21
22         // Animate Views
23         private int viewSize = 50;
24         private View viewOrigin;
25         private View viewBlend;
26         private Animation animationOrigin;
27         private Animation animationBlend;
28
29         // Animaiton properties controller
30         private Slider sliderBlendPoint;
31         private Slider sliderDuration;
32         private TextLabel labelBlendPoint;
33         private TextLabel labelDuration;
34         private float blendPoint = 0.25f;
35         private float duration = 5.0f;
36         private bool requiredNewAnimationCreate = true;
37
38         private Button buttonPlayStop;
39
40         public void Activate()
41         {
42             window = NUIApplication.GetDefaultWindow();
43
44             window.AddAvailableOrientation(Window.WindowOrientation.Portrait);
45             window.AddAvailableOrientation(Window.WindowOrientation.Landscape);
46             window.AddAvailableOrientation(Window.WindowOrientation.PortraitInverse);
47             window.AddAvailableOrientation(Window.WindowOrientation.LandscapeInverse);
48
49             window.Resized += (o, e) =>
50             {
51                 DetermineContentsDirection();
52             };
53
54             CreateLayoutViews();
55             CreateSlider();
56             CreateButton();
57             CreateAnimatableViews();
58
59             contents.Relayout += (o, e) =>
60             {
61                 requiredNewAnimationCreate = true;
62
63                 animationOrigin?.Stop();
64                 animationBlend?.Stop();
65             };
66
67             DetermineContentsDirection();
68
69             window.Add(root);
70         }
71
72         public void Deactivate()
73         {
74             if (root != null)
75             {
76                 window.Remove(root);
77                 root.DisposeRecursively();
78             }
79
80             animationOrigin?.Stop();
81             animationOrigin?.Clear();
82             animationOrigin?.Dispose();
83
84             animationBlend?.Stop();
85             animationBlend?.Clear();
86             animationBlend?.Dispose();
87         }
88
89         // Layout relative codes
90         private void DetermineContentsDirection()
91         {
92             Size2D windowSize = window.WindowSize;
93             if(windowSize.Width < windowSize.Height)
94             {
95                 if(!contentsLayoutVertical)
96                 {
97                     ushort viewRootPadding = (ushort)(10 + viewSize / 2);
98                     contentsLayoutVertical = true;
99                     contents.Layout = new LinearLayout()
100                     {
101                         LinearOrientation = LinearLayout.Orientation.Vertical,
102                         Padding = new Extents((ushort)viewRootPadding),
103                         CellPadding = new Size2D(viewRootPadding, viewRootPadding),
104                     };
105                 }
106             }
107             else
108             {
109                 if(contentsLayoutVertical)
110                 {
111                     ushort viewRootPadding = (ushort)(10 + viewSize / 2);
112                     contentsLayoutVertical = false;
113                     contents.Layout = new LinearLayout()
114                     {
115                         LinearOrientation = LinearLayout.Orientation.Horizontal,
116                         Padding = new Extents((ushort)viewRootPadding),
117                         CellPadding = new Size2D(viewRootPadding, viewRootPadding),
118                     };
119                 }
120             }
121         }
122
123         private void CreateLayoutViews()
124         {
125             root = new View()
126             {
127                 WidthSpecification = LayoutParamPolicies.MatchParent,
128                 HeightSpecification = LayoutParamPolicies.MatchParent,
129
130                 Layout = new LinearLayout()
131                 {
132                     LinearOrientation = LinearLayout.Orientation.Vertical,
133                     Padding = new Extents(10, 10, 10, 10),
134                 },
135             };
136
137             header = new View()
138             {
139                 WidthSpecification = LayoutParamPolicies.MatchParent,
140                 HeightSpecification = LayoutParamPolicies.WrapContent,
141
142                 Layout = new LinearLayout()
143                 {
144                     LinearOrientation = LinearLayout.Orientation.Vertical,
145                     Padding = new Extents(10, 10, 10, 10),
146                 },
147             };
148
149
150             int viewRootPadding = 10 + viewSize / 2;
151             contentsLayoutVertical = true;
152             contents = new View()
153             {
154                 WidthSpecification = LayoutParamPolicies.MatchParent,
155                 HeightSpecification = LayoutParamPolicies.MatchParent,
156
157                 Layout = new LinearLayout()
158                 {
159                     LinearOrientation = LinearLayout.Orientation.Vertical,
160                     Padding = new Extents((ushort)viewRootPadding),
161                     CellPadding = new Size2D(viewRootPadding, viewRootPadding),
162                 },
163             };
164
165             footer = new View()
166             {
167                 WidthSpecification = LayoutParamPolicies.MatchParent,
168                 HeightSpecification = LayoutParamPolicies.WrapContent,
169
170                 Layout = new LinearLayout()
171                 {
172                     LinearOrientation = LinearLayout.Orientation.Vertical,
173                     Padding = new Extents(10, 10, 10, 10),
174                 },
175             };
176
177             root.Add(header);
178             root.Add(contents);
179             root.Add(footer);
180
181             layoutOriginRoot = new View()
182             {
183                 WidthSpecification = LayoutParamPolicies.MatchParent,
184                 HeightSpecification = LayoutParamPolicies.MatchParent,
185                 BorderlineWidth = 4.0f,
186             };
187             layoutBlendRoot = new View()
188             {
189                 WidthSpecification = LayoutParamPolicies.MatchParent,
190                 HeightSpecification = LayoutParamPolicies.MatchParent,
191                 BorderlineWidth = 4.0f,
192             };
193
194             contents.Add(layoutOriginRoot);
195             contents.Add(layoutBlendRoot);
196         }
197
198         private void CreateSlider()
199         {
200             labelBlendPoint = new TextLabel()
201             {
202                 WidthSpecification = LayoutParamPolicies.WrapContent,
203                 HeightSpecification = LayoutParamPolicies.WrapContent,
204                 Text = "Blend Point : " + blendPoint.ToString(),
205             };
206             sliderBlendPoint = new Slider()
207             {
208                 WidthSpecification = LayoutParamPolicies.MatchParent,
209                 HeightSpecification = LayoutParamPolicies.WrapContent,
210                 MinValue = 0.0f,
211                 MaxValue = 1.0f,
212                 CurrentValue = blendPoint,
213             };
214             sliderBlendPoint.ValueChanged += (o, e) =>
215             {
216                 requiredNewAnimationCreate = true;
217                 animationOrigin?.Stop();
218                 animationBlend?.Stop();
219
220                 blendPoint = e.CurrentValue;
221                 labelBlendPoint.Text = "Blend Point : " + blendPoint.ToString();
222             };
223
224             header.Add(labelBlendPoint);
225             header.Add(sliderBlendPoint);
226
227             labelDuration = new TextLabel()
228             {
229                 WidthSpecification = LayoutParamPolicies.WrapContent,
230                 HeightSpecification = LayoutParamPolicies.WrapContent,
231                 Text = "Duration : " + duration.ToString() + " seconds",
232             };
233             sliderDuration = new Slider()
234             {
235                 WidthSpecification = LayoutParamPolicies.MatchParent,
236                 HeightSpecification = LayoutParamPolicies.WrapContent,
237                 MinValue = 0.0f,
238                 MaxValue = 10.0f,
239                 CurrentValue = duration,
240             };
241             sliderDuration.ValueChanged += (o, e) =>
242             {
243                 requiredNewAnimationCreate = true;
244                 animationOrigin?.Stop();
245                 animationBlend?.Stop();
246
247                 duration = e.CurrentValue;
248                 labelDuration.Text = "Duration : " + duration.ToString() + " seconds";
249             };
250
251             header.Add(labelDuration);
252             header.Add(sliderDuration);
253         }
254
255         private void CreateButton()
256         {
257             buttonPlayStop = new Button()
258             {
259                 WidthSpecification = LayoutParamPolicies.MatchParent,
260                 Padding = new Extents(10, 10, 10, 10),
261
262                 Text = "Play",
263             };
264             buttonPlayStop.Clicked += (o, e)=>
265             {
266                 if(requiredNewAnimationCreate)
267                 {
268                     RegenerateAnimation();
269                     animationOrigin.Play();
270                     animationBlend.Play();
271                     buttonPlayStop.Text = "Stop";
272                 }
273                 else if(animationOrigin != null && animationBlend != null)
274                 {
275                     if(animationOrigin.State == Animation.States.Playing)
276                     {
277                         animationOrigin.Stop();
278                         animationBlend.Stop();
279                         buttonPlayStop.Text = "Play";
280                     }
281                     else
282                     {
283                         animationOrigin.Play();
284                         animationBlend.Play();
285                         buttonPlayStop.Text = "Stop";
286                     }
287                 }
288                 else
289                 {
290                     buttonPlayStop.Text = "??? Bug!";
291                 }
292             };
293
294             header.Add(buttonPlayStop);
295         }
296
297         private void CreateAnimatableViews()
298         {
299             viewOrigin = CreateNewAnimatableView("O");
300             viewBlend = CreateNewAnimatableView("B");
301
302             layoutOriginRoot.Add(viewOrigin);
303             layoutBlendRoot.Add(viewBlend);
304         }
305
306         private void RegenerateAnimation()
307         {
308             requiredNewAnimationCreate = false;
309         
310             animationOrigin?.Dispose();
311             animationOrigin = null;
312             animationBlend?.Dispose();
313             animationBlend = null;
314
315             Size originSize = layoutOriginRoot.Size;
316             Size blendSize = layoutBlendRoot.Size;
317
318             animationOrigin = CreateNewAnimation(viewOrigin, originSize, duration, 0.0f);
319             animationBlend = CreateNewAnimation(viewBlend, blendSize, duration, blendPoint);
320         }
321
322
323         // Creation functions. It will be used when we are doing duplicated jobs
324         private View CreateNewAnimatableView(string name)
325         {
326             View view = new TextLabel()
327             {
328                 Text = name,
329                 VerticalAlignment = VerticalAlignment.Center,
330                 HorizontalAlignment = HorizontalAlignment.Center,
331
332                 Size2D = new Size2D(viewSize, viewSize),
333
334                 PositionUsesPivotPoint = true,
335                 PivotPoint = Position.PivotPointCenter,
336                 ParentOrigin = Position.ParentOriginCenter,
337
338                 BackgroundColor = Color.BlancheDalmond,
339                 BorderlineColor = Color.Blue,
340                 BorderlineWidth = 10.0f,
341                 BorderlineOffset = -1.0f,
342
343                 CornerRadiusPolicy = VisualTransformPolicyType.Relative,
344                 CornerRadius = new Vector4(0.5f, 0.5f, 0.0f, 0.5f),
345             };
346             return view;
347         }
348         private Animation CreateNewAnimation(View target, Size parentSize, float durationSeconds, float blendPoint)
349         {
350             Animation animation = new Animation((int)(durationSeconds * 1000.0f))
351             {
352                 BlendPoint = blendPoint,
353             };
354             animation.Finished += (o, e) =>
355             {
356                 // Reset to zero
357                 target.Position = new Position(0.0f, 0.0f);
358                 target.Orientation = new Rotation(new Radian(0.0f), Vector3.ZAxis);
359                 if(buttonPlayStop)
360                 {
361                     buttonPlayStop.Text = "Play";
362                 }
363             };
364
365             KeyFrames positionKeyFrames = new KeyFrames();
366             positionKeyFrames.Add(0.0f, new Vector3(-parentSize.Width * 0.5f, parentSize.Height * 0.5f, 0.0f));
367             positionKeyFrames.Add(0.2f, new Vector3(parentSize.Width * 0.5f, parentSize.Height * 0.5f, 0.0f));
368             positionKeyFrames.Add(0.25f, new Vector3(parentSize.Width * 0.5f, parentSize.Height * 0.5f, 0.0f));
369             positionKeyFrames.Add(0.45f, new Vector3(parentSize.Width * 0.5f, -parentSize.Height * 0.5f, 0.0f));
370             positionKeyFrames.Add(0.5f, new Vector3(parentSize.Width * 0.5f, -parentSize.Height * 0.5f, 0.0f));
371             positionKeyFrames.Add(0.7f, new Vector3(-parentSize.Width * 0.5f, -parentSize.Height * 0.5f, 0.0f));
372             positionKeyFrames.Add(0.75f, new Vector3(-parentSize.Width * 0.5f, -parentSize.Height * 0.5f, 0.0f));
373             positionKeyFrames.Add(0.95f, new Vector3(-parentSize.Width * 0.5f, parentSize.Height * 0.5f, 0.0f));
374             positionKeyFrames.Add(1.0f, new Vector3(-parentSize.Width * 0.5f, parentSize.Height * 0.5f, 0.0f));
375
376             KeyFrames orientationKeyFrames = new KeyFrames();
377             orientationKeyFrames.Add(0.0f, new Rotation(new Radian(new Degree(-45.0f)), Vector3.ZAxis));
378             orientationKeyFrames.Add(0.2f, new Rotation(new Radian(new Degree(-45.0f)), Vector3.ZAxis));
379             orientationKeyFrames.Add(0.25f, new Rotation(new Radian(new Degree(-135.0f)), Vector3.ZAxis));
380             orientationKeyFrames.Add(0.45f, new Rotation(new Radian(new Degree(-135.0f)), Vector3.ZAxis));
381             orientationKeyFrames.Add(0.5f, new Rotation(new Radian(new Degree(135.0f)), Vector3.ZAxis));
382             orientationKeyFrames.Add(0.7f, new Rotation(new Radian(new Degree(135.0f)), Vector3.ZAxis));
383             orientationKeyFrames.Add(0.75f, new Rotation(new Radian(new Degree(45.0f)), Vector3.ZAxis));
384             orientationKeyFrames.Add(0.95f, new Rotation(new Radian(new Degree(45.0f)), Vector3.ZAxis));
385             orientationKeyFrames.Add(1.0f, new Rotation(new Radian(new Degree(-45.0f)), Vector3.ZAxis));
386
387             animation.AnimateBetween(target, "Position", positionKeyFrames);
388             animation.AnimateBetween(target, "Orientation", orientationKeyFrames);
389
390             return animation;
391         }
392     }
393 }