[NUI] Add FlexLayout sample (#309)
[platform/core/csapi/tizenfx.git] / test / NUITestSample / NUITestSample / examples / layouting / flex-layout-test.cs
1 using System;
2 using System.Threading;
3 using Tizen.NUI;
4 using Tizen.NUI.BaseComponents;
5
6 namespace NUIFlexLayoutSample
7 {
8     static class Images
9     {
10         public static string resources = Tizen.Applications.Application.Current.DirectoryInfo.Resource;
11         public static readonly string[] s_images = new string[]
12         {
13             resources + "images/application-icon-101.png",
14             resources + "images/application-icon-102.png",
15             resources + "images/application-icon-103.png",
16             resources + "images/application-icon-104.png",
17             resources + "images/image-1.jpg",
18             resources + "images/image-2.jpg",
19             resources + "images/image-3.jpg",
20         };
21     }
22
23     class Example : NUIApplication
24     {
25         public Example() : base()
26         {
27             Console.WriteLine("Example()!");
28         }
29
30         protected override void OnCreate()
31         {
32             base.OnCreate();
33             Initialize();
34         }
35
36         View flexContainer;
37         const int MAX_CHILDREN = 7;
38         ImageView[] imageViews = new ImageView[MAX_CHILDREN];
39         private void Initialize()
40         {
41             Console.WriteLine("Initialize()!");
42             Window window = Window.Instance;
43             window.BackgroundColor = Color.Green;
44
45             flexContainer = new View();
46             flexContainer.PositionUsesPivotPoint = true;
47             flexContainer.PivotPoint = PivotPoint.Center;
48             flexContainer.ParentOrigin = ParentOrigin.Center;
49             flexContainer.BackgroundColor = Color.Yellow;
50
51             for (int index = 0; index < MAX_CHILDREN - 3; index++)
52             {
53                 imageViews[index] = new ImageView(Images.s_images[index]);
54                 imageViews[index].WidthSpecificationFixed = 100;
55                 imageViews[index].HeightSpecificationFixed = 100;
56                 flexContainer.Add(imageViews[index]);
57             }
58             for (int index = MAX_CHILDREN - 3; index < MAX_CHILDREN; index++)
59             {
60                 imageViews[index] = new ImageView(Images.s_images[index]);
61                 imageViews[index].WidthSpecificationFixed = 200;
62                 imageViews[index].HeightSpecificationFixed = 200;
63                 imageViews[index].Name = "t_image" + (index - 3);
64             }
65
66             var layout = new FlexLayout();
67             layout.LayoutAnimate = true;
68             layout.Direction = FlexLayout.FlexDirection.ColumnReverse;
69             flexContainer.WidthSpecificationFixed = 500;
70             flexContainer.HeightSpecificationFixed = 500;
71             flexContainer.Layout = layout;
72
73             window.Add(flexContainer);
74             window.KeyEvent += OnKeyEvent;
75         }
76
77         int cnt1 = 1;
78         private void OnKeyEvent(object source, Window.KeyEventArgs e)
79         {
80             if (e.Key.State == Key.StateType.Down)
81             {
82                 Console.WriteLine($"key pressed name={e.Key.KeyPressedName}");
83                 switch (e.Key.KeyPressedName)
84                 {
85                     case "Right":
86                         if (cnt1 < 4 && cnt1 > 0)
87                         {
88                             flexContainer.Add(imageViews[cnt1 + 3]);
89                             cnt1++;
90                         }
91                         break;
92
93                     case "Left":
94                         if (cnt1 - 1 < 4 && cnt1 - 1 > 0)
95                         {
96                             View tmp = flexContainer.FindChildByName("t_image" + (cnt1 - 1));
97                             if (tmp != null)
98                             {
99                                 flexContainer.Remove(tmp);
100                                 cnt1--;
101                             }
102                         }
103                         break;
104
105                     case "Up":
106                         var vertical = new FlexLayout();
107                         vertical.LayoutAnimate = true;
108                         vertical.Direction = FlexLayout.FlexDirection.Column;
109                         flexContainer.Layout = vertical;
110                         break;
111
112                     case "Down":
113                         var horizon = new FlexLayout();
114                         horizon.LayoutAnimate = true;
115                         horizon.Direction = FlexLayout.FlexDirection.Row;
116                         flexContainer.Layout = horizon;
117                         break;
118
119                     case "Return":
120                         if (flexContainer.LayoutDirection == ViewLayoutDirectionType.LTR) { flexContainer.LayoutDirection = ViewLayoutDirectionType.RTL; }
121                         else { flexContainer.LayoutDirection = ViewLayoutDirectionType.LTR; }
122                         break;
123
124                     case "1":
125                         var tmpLayout = flexContainer.Layout as FlexLayout;
126                         if (tmpLayout.WrapType == FlexLayout.FlexWrapType.NoWrap) { tmpLayout.WrapType = FlexLayout.FlexWrapType.Wrap; }
127                         else { tmpLayout.WrapType = FlexLayout.FlexWrapType.NoWrap; }
128                         break;
129                 }
130             }
131         }
132
133         [STAThread]
134         static void Main(string[] args)
135         {
136             Example layoutSample = new Example();
137             layoutSample.Run(args);
138         }
139
140     }
141 }