[NUI] Add Transition Group
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / PageTransitionSample.cs
1 using System;
2 using Tizen.NUI.BaseComponents;
3 using Tizen.NUI.Components;
4
5 namespace Tizen.NUI.Samples
6 {
7     public class PageTransitionSample : IExample
8     {
9         private readonly string[,] Keywords = new string[3, 2]
10         {
11             {"red", "redGrey"},
12             {"green", "greenGrey"},
13             {"blue", "blueGrey"}
14         };
15         private readonly string totalGreyTag = "totalGrey";
16
17         private Navigator navigator;
18         private ContentPage mainPage;
19         private ContentPage redPage, greenPage, bluePage, totalPage;
20
21         private readonly Vector4 ColorGrey = new Vector4(0.82f, 0.80f, 0.78f, 1.0f);
22         private readonly Vector4 ColorBackground = new Vector4(0.99f, 0.94f, 0.83f, 1.0f);
23
24         private readonly Vector4[] TileColor = { new Color("#F5625D"), new Color("#7DFF83"), new Color("#7E72DF") };
25
26         private readonly Vector2 baseSize = new Vector2(480.0f, 800.0f);
27         private Vector2 contentSize;
28
29         private float magnification;
30
31         private float convertSize(float size)
32         {
33             return size * magnification;
34         }
35
36
37         public void Activate()
38         {
39             Window window = NUIApplication.GetDefaultWindow();
40             Vector2 windowSize = new Vector2((float)(window.Size.Width), (float)(window.Size.Height));
41             magnification = Math.Min(windowSize.X / baseSize.X, windowSize.Y / baseSize.Y);
42             contentSize = baseSize * magnification;
43
44             navigator = new Navigator()
45             {
46                 WidthResizePolicy = ResizePolicyType.FillToParent,
47                 HeightResizePolicy = ResizePolicyType.FillToParent,
48                 Transition = new Transition()
49                 {
50                     TimePeriod = new TimePeriod(400),
51                     AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.EaseInOutSine),
52                 },
53             };
54             window.Add(navigator);
55
56             View mainRoot = new View()
57             {
58                 WidthResizePolicy = ResizePolicyType.FillToParent,
59                 HeightResizePolicy = ResizePolicyType.FillToParent
60             };
61
62             View layoutView = new View()
63             {
64                 PositionUsesPivotPoint = true,
65                 PivotPoint = PivotPoint.BottomCenter,
66                 ParentOrigin = ParentOrigin.BottomCenter,
67                 Layout = new LinearLayout()
68                 {
69                     LinearAlignment = LinearLayout.Alignment.Center,
70                     LinearOrientation = LinearLayout.Orientation.Horizontal,
71                     CellPadding = new Size(convertSize(60), convertSize(60)),
72                 },
73                 Position = new Position(0, -convertSize(30))
74             };
75             mainRoot.Add(layoutView);
76
77             View redButton = CreateButton(TileColor[0], Keywords[0, 0], Keywords[0, 1], redPage);
78             View greenButton = CreateButton(TileColor[1], Keywords[1, 0], Keywords[1, 1], greenPage);
79             View blueButton = CreateButton(TileColor[2], Keywords[2, 0], Keywords[2, 1], bluePage);
80
81             layoutView.Add(redButton);
82             layoutView.Add(greenButton);
83             layoutView.Add(blueButton);
84
85
86             TransitionGroup transitionGroup = new TransitionGroup()
87             {
88                 UseGroupTimePeriod = true,
89                 UseGroupAlphaFunction = true,
90                 StepTransition = true,
91                 TimePeriod = new TimePeriod(500, 3000),
92                 AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.EaseInOut),
93             };
94             SlideTransition slide = new SlideTransition()
95             {
96                 TimePeriod = new TimePeriod(1000),
97                 AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Default),
98                 Direction = SlideTransitionDirection.Top
99             };
100             transitionGroup.AddTransition(slide);
101             FadeTransition fade = new FadeTransition()
102             {
103                 Opacity = 0.3f,
104                 TimePeriod = new TimePeriod(1000),
105                 AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Default)
106             };
107             transitionGroup.AddTransition(fade);
108             ScaleTransition scale = new ScaleTransition()
109             {
110                 ScaleFactor = new Vector2(0.3f, 0.3f),
111                 TimePeriod = new TimePeriod(1000),
112                 AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Default)
113             };
114             transitionGroup.AddTransition(scale);
115
116             mainPage = new ContentPage()
117             {
118                 BackgroundColor = Color.Transparent,
119                 Content = mainRoot,
120                 DisappearingTransition = new ScaleTransition()
121                 {
122                     TimePeriod = new TimePeriod(500),
123                     AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Default),
124                     ScaleFactor = new Vector2(0.5f, 1.5f)
125                 },
126                 AppearingTransition = transitionGroup,
127             };
128             navigator.Push(mainPage);
129
130             View totalGreyView = new View()
131             {
132                 Size = new Size(convertSize(50), convertSize(50)),
133                 CornerRadius = convertSize(25),
134                 BackgroundColor = ColorGrey,
135                 TransitionOptions = new TransitionOptions()
136                 {
137                     TransitionTag = totalGreyTag,
138                 }
139             };
140
141             totalGreyView.TouchEvent += (object sender, View.TouchEventArgs e) =>
142             {
143                 if (e.Touch.GetState(0) == PointStateType.Down)
144                 {
145                     navigator.PushWithTransition(totalPage);
146                 }
147                 return true;
148             };
149             layoutView.Add(totalGreyView);
150
151
152             // ------------------------------------------------------
153
154
155             View totalPageRoot = new View()
156             {
157                 WidthResizePolicy = ResizePolicyType.FillToParent,
158                 SizeHeight = contentSize.Height,
159             };
160
161             View totalLayoutView = new View()
162             {
163                 Layout = new GridLayout()
164                 {
165                     Rows = 2,
166                     GridOrientation = GridLayout.Orientation.Vertical,
167                 },
168                 PositionUsesPivotPoint = true,
169                 PivotPoint = PivotPoint.Center,
170                 ParentOrigin = ParentOrigin.Center,
171             };
172             totalPageRoot.Add(totalLayoutView);
173
174             for (int i = 0; i < 3; ++i)
175             {
176                 View sizeView = new View()
177                 {
178                     Size = new Size(contentSize.Width / 2.0f, contentSize.Height / 2.0f),
179                 };
180                 View smallView = CreatePageScene(TileColor[i], Keywords[i, 0], Keywords[i, 1]);
181                 smallView.Scale = new Vector3(0.45f, 0.45f, 1.0f);
182                 smallView.PositionUsesPivotPoint = true;
183                 smallView.PivotPoint = PivotPoint.Center;
184                 smallView.ParentOrigin = ParentOrigin.Center;
185                 sizeView.Add(smallView);
186                 totalLayoutView.Add(sizeView);
187             }
188
189             View sizeGreyView = new View()
190             {
191                 Size = new Size(contentSize.Width / 2.0f, contentSize.Height / 2.0f),
192             };
193
194             View totalGreyReturnView = new View()
195             {
196                 PositionUsesPivotPoint = true,
197                 PivotPoint = PivotPoint.Center,
198                 ParentOrigin = ParentOrigin.Center,
199                 Size = new Size(convertSize(70), convertSize(70)),
200                 CornerRadius = convertSize(20),
201                 BackgroundColor = ColorGrey,
202                 TransitionOptions = new TransitionOptions()
203                 {
204                     TransitionTag = totalGreyTag,
205                 }
206             };
207             sizeGreyView.Add(totalGreyReturnView);
208             totalLayoutView.Add(sizeGreyView);
209
210             totalGreyReturnView.TouchEvent += (object sender, View.TouchEventArgs e) =>
211             {
212                 if (e.Touch.GetState(0) == PointStateType.Down)
213                 {
214                     navigator.PopWithTransition();
215                 }
216                 return true;
217             };
218
219             totalPage = new ContentPage()
220             {
221                 BackgroundColor = Color.Transparent,
222                 Content = totalPageRoot,
223                 AppearingTransition = new FadeTransition()
224                 {
225                     TimePeriod = new TimePeriod(500),
226                     AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Default),
227                 },
228                 DisappearingTransition = new FadeTransition()
229                 {
230                     TimePeriod = new TimePeriod(500),
231                     AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Default),
232                 },
233             };
234         }
235
236         private View CreateButton(Color color, string colorTag, string greyTag, Page secondPage)
237         {
238             View colorView = new View()
239             {
240                 Size = new Size(convertSize(50), convertSize(50)),
241                 CornerRadius = 0.45f,
242                 CornerRadiusPolicy = VisualTransformPolicyType.Relative,
243                 BackgroundColor = color,
244                 Orientation = new Rotation(new Radian((float)Math.PI / 2.0f), Vector3.ZAxis),
245                 TransitionOptions = new TransitionOptions()
246                 {
247                     TransitionTag = colorTag,
248                 },
249             };
250
251             View greyView = new View()
252             {
253                 PositionUsesPivotPoint = true,
254                 PivotPoint = PivotPoint.Center,
255                 ParentOrigin = ParentOrigin.Center,
256                 Size = new Size(convertSize(40), convertSize(40)),
257                 CornerRadius = 0.45f,
258                 CornerRadiusPolicy = VisualTransformPolicyType.Relative,
259                 BackgroundColor = ColorGrey,
260                 Orientation = new Rotation(new Radian(-(float)Math.PI / 2.0f), Vector3.ZAxis),
261                 TransitionOptions = new TransitionOptions()
262                 {
263                     TransitionTag = greyTag,
264                 }
265             };
266
267             secondPage = CreatePage(color, colorTag, greyTag);
268
269             greyView.TouchEvent += (object sender, View.TouchEventArgs e) =>
270             {
271                 if (e.Touch.GetState(0) == PointStateType.Down)
272                 {
273                     navigator.PushWithTransition(secondPage);
274                 }
275                 return true;
276             };
277             colorView.Add(greyView);
278             return colorView;
279         }
280
281         private View CreatePageScene(Color color, string colorTag, string greyTag)
282         {
283             View pageBackground = new View()
284             {
285                 SizeWidth = contentSize.Width,
286                 SizeHeight = contentSize.Height,
287             };
288
289             View colorView = new View()
290             {
291                 WidthResizePolicy = ResizePolicyType.FillToParent,
292                 HeightResizePolicy = ResizePolicyType.FillToParent,
293                 CornerRadius = 0.05f,
294                 CornerRadiusPolicy = VisualTransformPolicyType.Relative,
295                 BackgroundColor = color,
296                 TransitionOptions = new TransitionOptions()
297                 {
298                     TransitionTag = colorTag
299                 }
300             };
301
302             View greyView = new View()
303             {
304                 PositionUsesPivotPoint = true,
305                 PivotPoint = PivotPoint.TopCenter,
306                 ParentOrigin = ParentOrigin.TopCenter,
307                 Position = new Position(0, convertSize(80)),
308                 SizeWidth = contentSize.Width * 0.7f,
309                 SizeHeight = contentSize.Height * 0.06f,
310                 CornerRadius = 0.1f,
311                 CornerRadiusPolicy = VisualTransformPolicyType.Relative,
312                 BackgroundColor = ColorGrey,
313                 TransitionOptions = new TransitionOptions()
314                 {
315                     TransitionTag = greyTag
316                 }
317             };
318
319             View whiteView = new View()
320             {
321                 PositionUsesPivotPoint = true,
322                 PivotPoint = PivotPoint.BottomCenter,
323                 ParentOrigin = ParentOrigin.BottomCenter,
324                 Position = new Position(0, -convertSize(60)),
325                 SizeWidth = contentSize.Width * 0.75f,
326                 SizeHeight = contentSize.Height * 0.7f,
327                 CornerRadius = 0.1f,
328                 CornerRadiusPolicy = VisualTransformPolicyType.Relative,
329                 BackgroundColor = Color.AntiqueWhite,
330             };
331             pageBackground.Add(colorView);
332             pageBackground.Add(whiteView);
333             pageBackground.Add(greyView);
334
335             return pageBackground;
336         }
337
338         private Page CreatePage(Color color, string colorTag, string greyTag)
339         {
340             View pageRoot = new View()
341             {
342                 WidthResizePolicy = ResizePolicyType.FillToParent,
343                 HeightResizePolicy = ResizePolicyType.FillToParent
344             };
345
346             View pageBackground = CreatePageScene(color, colorTag, greyTag);
347             pageBackground.TouchEvent += (object sender, View.TouchEventArgs e) =>
348             {
349                 if (e.Touch.GetState(0) == PointStateType.Down)
350                 {
351                     navigator.PopWithTransition();
352                 }
353                 return true;
354             };
355             pageRoot.Add(pageBackground);
356
357             TransitionGroup transitionGroup = new TransitionGroup();
358             FadeTransition slide = new FadeTransition()
359             {
360                 TimePeriod = new TimePeriod(500),
361                 AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Default),
362             };
363             transitionGroup.AddTransition(slide);
364
365             Page page = new ContentPage()
366             {
367                 BackgroundColor = Color.Transparent,
368                 Content = pageRoot,
369
370                 AppearingTransition = transitionGroup,
371                 DisappearingTransition = new SlideTransition()
372                 {
373                     TimePeriod = new TimePeriod(500),
374                     AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Default),
375                     Direction = SlideTransitionDirection.Left
376                 },
377             };
378             return page;
379         }
380
381         public void Deactivate()
382         {
383         }
384     }
385 }