[NUI] All Layers have an Absolute layout (#313)
[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, cnt2;
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                 var tmpLayout = flexContainer.Layout as FlexLayout;
84                 cnt2++;
85
86                 switch (e.Key.KeyPressedName)
87                 {
88                     case "Right":
89                         if (cnt1 < 4 && cnt1 > 0)
90                         {
91                             flexContainer.Add(imageViews[cnt1 + 3]);
92                             cnt1++;
93                         }
94                         break;
95
96                     case "Left":
97                         if (cnt1 - 1 < 4 && cnt1 - 1 > 0)
98                         {
99                             View tmp = flexContainer.FindChildByName("t_image" + (cnt1 - 1));
100                             if (tmp != null)
101                             {
102                                 flexContainer.Remove(tmp);
103                                 cnt1--;
104                             }
105                         }
106                         break;
107
108                     case "Up":
109                         var vertical = new FlexLayout();
110                         vertical.LayoutAnimate = true;
111                         vertical.Direction = FlexLayout.FlexDirection.Column;
112                         flexContainer.Layout = vertical;
113                         break;
114
115                     case "Down":
116                         var horizon = new FlexLayout();
117                         horizon.LayoutAnimate = true;
118                         horizon.Direction = FlexLayout.FlexDirection.Row;
119                         flexContainer.Layout = horizon;
120                         break;
121
122                     case "Return":
123                         if (flexContainer.LayoutDirection == ViewLayoutDirectionType.LTR) { flexContainer.LayoutDirection = ViewLayoutDirectionType.RTL; }
124                         else { flexContainer.LayoutDirection = ViewLayoutDirectionType.LTR; }
125                         break;
126
127                     case "1":
128                         var dir = cnt2 % 4;
129                         tmpLayout.Direction = (FlexLayout.FlexDirection)dir;
130                         break;
131
132                     case "2":
133                         var justi = cnt2 % 5;
134                         tmpLayout.Justification = (FlexLayout.FlexJustification)justi;
135                         break;
136
137                     case "3":
138                         var wrap = cnt2 % 2;
139                         tmpLayout.WrapType = (FlexLayout.FlexWrapType)wrap;
140                         break;
141
142                     case "4":
143                         var align = cnt2 % 5;
144                         tmpLayout.ItemsAlignment = (FlexLayout.AlignmentType)align;
145                         break;
146
147                 }
148             }
149         }
150
151         [STAThread]
152         static void _Main(string[] args)
153         {
154             Example layoutSample = new Example();
155             layoutSample.Run(args);
156         }
157
158     }
159 }