83667912e2f3403149e0864ef1a9e5997c9755d7
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / PopupSample.cs
1 using System.Collections.Generic;
2 using Tizen.NUI.BaseComponents;
3 using Tizen.NUI.Components;
4
5 namespace Tizen.NUI.Samples
6 {
7     public class PopupSample : IExample
8     {
9         private View root;
10
11         private TextLabel[] createText = new TextLabel[2];
12
13         private Popup popup = null;
14         private Popup popup2 = null;
15         private TextLabel contentText = null;
16         private TextLabel contentText2 = null;
17         private int index = 0;
18         private int index2 = 0;
19         private Button button = null;
20
21         private static string[] buttonStyles = new string[]
22         {
23             "UtilityPopupButton",
24             "FamilyPopupButton",
25             "FoodPopupButton",
26             "KitchenPopupButton",
27         };
28         private static Color[] color = new Color[]
29         {
30             new Color(0.05f, 0.63f, 0.9f, 1),//#ff0ea1e6
31             new Color(0.14f, 0.77f, 0.28f, 1),//#ff24c447
32             new Color(0.75f, 0.46f, 0.06f, 1),//#ffec7510
33             new Color(0.59f, 0.38f, 0.85f, 1),//#ff9762d9
34         };
35         public void Activate()
36         {
37             Window window = NUIApplication.GetDefaultWindow();
38
39             root = new View()
40             {
41                 Size2D = new Size2D(1920, 1080),
42                 BackgroundColor = Color.White,
43             };
44             window.Add(root);
45
46             ///////////////////////////////////////////////Create by Property//////////////////////////////////////////////////////////
47             createText[0] = new TextLabel();
48             createText[0].Text = "Create Popup just by properties";
49             createText[0].Size2D = new Size2D(500, 100);
50             createText[0].Position2D = new Position2D(500, 50);
51             root.Add(createText[0]);
52
53             popup = new Popup();
54             popup.MinimumSize = new Size2D(1032, 184);
55             popup.Size = new Size(1032, 400);
56             popup.Position = new Position(200, 100);
57
58             // Title
59             popup.Style.Title.PointSize = 25;
60             popup.Style.Title.SizeHeight = 68;
61             popup.Style.Title.HorizontalAlignment = HorizontalAlignment.Begin;
62             popup.Style.Title.Position = new Position(64, 52);
63             popup.Style.Title.Text = "Popup Title";
64
65             // Shadow
66             popup.Style.ImageShadow = new ImageShadow
67             {
68                 Url = CommonResource.GetFHResourcePath() + "11. Popup/popup_background_shadow.png",
69                 Border = new Rectangle(24, 24, 24, 24),
70                 Offset = new Vector2(-24, -24),
71                 Extents = new Vector2(48, 48),
72             };
73
74             // Background
75             popup.BackgroundImage = CommonResource.GetFHResourcePath() + "11. Popup/popup_background.png";
76             popup.BackgroundImageBorder = new Rectangle(0, 0, 81, 81);
77
78             // Buttons
79             popup.AddButton("Yes");
80             popup.AddButton("Exit");
81             popup.ButtonBackground = CommonResource.GetFHResourcePath() + "3. Button/rectangle_btn_normal.png";
82             popup.ButtonBackgroundBorder = new Rectangle(5, 5, 5, 5);
83             popup.ButtonOverLayBackgroundColorSelector = new Selector<Color>
84             {
85                 Normal = new Color(1.0f, 1.0f, 1.0f, 1.0f),
86                 Pressed = new Color(0.0f, 0.0f, 0.0f, 0.1f),
87                 Selected = new Color(1.0f, 1.0f, 1.0f, 1.0f),
88             };
89             popup.ButtonImageShadow = new ImageShadow
90             {
91                 Url = CommonResource.GetFHResourcePath() + "3. Button/rectangle_btn_shadow.png",
92                 Border = new Rectangle(5, 5, 5, 5)
93             };
94             popup.ButtonTextColor = color[0];
95             popup.ButtonHeight = 132;
96             popup.PopupButtonClickEvent += PopupButtonClickedEvent;
97             popup.LayoutDirectionChanged += PopupLayoutDirectionChanged;
98             popup.Post(window);
99
100             contentText = new TextLabel();
101             contentText.Size2D = new Size2D(904, 100);
102             contentText.PointSize = 20;
103             contentText.HorizontalAlignment = HorizontalAlignment.Begin;
104             contentText.VerticalAlignment = VerticalAlignment.Center;
105             contentText.Text = "Popup ButtonStyle is " + buttonStyles[index];
106             contentText.TextColor = new Color(0,0,222,1);
107             popup.AddContentText(contentText);
108
109             ///////////////////////////////////////////////Create by Attributes//////////////////////////////////////////////////////////
110             createText[1] = new TextLabel();
111             createText[1].Text = "Create Popup just by Attributes";
112             createText[1].Size2D = new Size2D(500, 100);
113             createText[1].Position2D = new Position2D(500, 550);
114             root.Add(createText[1]);
115
116             PopupStyle attrs = new PopupStyle
117             {
118                 MinimumSize = new Size2D(1032, 184),
119                 BackgroundImage = new Selector<string> { All = CommonResource.GetFHResourcePath() + "11. Popup/popup_background.png" },
120                 BackgroundImageBorder = new Selector<Rectangle> { All = new Rectangle(0, 0, 81, 81) },
121                 ImageShadow = new ImageShadow
122                 {
123                     Url = CommonResource.GetFHResourcePath() + "11. Popup/popup_background_shadow.png",
124                     Border = new Rectangle(24, 24, 24, 24),
125                     Offset = new Vector2(-24, -24),
126                     Extents = new Vector2(48, 48),
127                 },
128                 Title = new TextLabelStyle
129                 {
130                     PointSize = new Selector<float?> { All = 25 },
131                     TextColor = new Selector<Color> { All = Color.Black },
132                     Size = new Size(0, 68),
133                     PositionUsesPivotPoint = true,
134                     ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
135                     PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
136                     HorizontalAlignment = HorizontalAlignment.Begin,
137                     VerticalAlignment = VerticalAlignment.Bottom,
138                     Position = new Position(64, 52),
139                     Text = new Selector<string> { All = "Popup Title" },
140                 },
141                 Buttons = new ButtonStyle
142                 {
143                     PositionUsesPivotPoint = true,
144                     ParentOrigin = Tizen.NUI.ParentOrigin.BottomLeft,
145                     PivotPoint = Tizen.NUI.PivotPoint.BottomLeft,
146                     BackgroundImage = new Selector<string> { All = CommonResource.GetFHResourcePath() + "3. Button/rectangle_btn_normal.png" },
147                     BackgroundImageBorder = new Selector<Rectangle> { All = new Rectangle(5, 5, 5, 5) },
148                     ImageShadow = new ImageShadow
149                     {
150                         Url = CommonResource.GetFHResourcePath() + "3. Button/rectangle_btn_shadow.png",
151                         Border = new Rectangle(5, 5, 5, 5),
152                     },
153                     Overlay = new ImageViewStyle
154                     {
155                         PositionUsesPivotPoint = true,
156                         ParentOrigin = Tizen.NUI.ParentOrigin.Center,
157                         PivotPoint = Tizen.NUI.PivotPoint.Center,
158                         WidthResizePolicy = ResizePolicyType.FillToParent,
159                         HeightResizePolicy = ResizePolicyType.FillToParent,
160                         BackgroundColor = new Selector<Color>
161                         {
162                             Normal = new Color(1.0f, 1.0f, 1.0f, 1.0f),
163                             Pressed = new Color(0.0f, 0.0f, 0.0f, 0.1f),
164                             Selected = new Color(1.0f, 1.0f, 1.0f, 1.0f),
165                         }
166                     },
167                     Text = new TextLabelStyle
168                     {
169                         PositionUsesPivotPoint = true,
170                         ParentOrigin = Tizen.NUI.ParentOrigin.Center,
171                         PivotPoint = Tizen.NUI.PivotPoint.Center,
172                         HorizontalAlignment = HorizontalAlignment.Center,
173                         VerticalAlignment = VerticalAlignment.Center,
174                         TextColor = new Selector<Color> { All = color[index2] },
175                     },
176                 },
177             };
178
179             popup2 = new Popup(attrs);
180             popup2.Size = new Size(1032, 400);
181             popup2.Position = new Position(200, 600);     
182             popup2.AddButton("Yes");
183             popup2.AddButton("Exit");
184             popup2.ButtonHeight = 132;
185             popup2.PopupButtonClickEvent += PopupButtonClickedEvent;
186             popup2.LayoutDirectionChanged += Popup2LayoutDirectionChanged;
187             popup2.Post(window);
188
189             contentText2 = new TextLabel();
190             contentText2.Size2D = new Size2D(904, 100);
191             contentText2.PointSize = 20;
192             contentText2.HorizontalAlignment = HorizontalAlignment.Begin;
193             contentText2.VerticalAlignment = VerticalAlignment.Center;
194             contentText2.Text = "Popup2 ButtonStyle is " + buttonStyles[index];
195             popup2.ContentView.Add(contentText2);
196
197             button = new Button();
198             button.Style.BackgroundImage = CommonResource.GetTVResourcePath() + "component/c_buttonbasic/c_basic_button_white_bg_normal_9patch.png";
199             button.Style.BackgroundImageBorder = new Rectangle(4, 4, 5, 5);
200             button.Size2D = new Size2D(580, 80);
201             button.Position2D = new Position2D(1300, 500);
202             button.Style.Text.Text = "LayoutDirection is left to right";
203             button.ClickEvent += ButtonClickEvent;
204             root.Add(button);
205         }
206
207         private void Popup2LayoutDirectionChanged(object sender, View.LayoutDirectionChangedEventArgs e)
208         {
209             if (contentText2.HorizontalAlignment == HorizontalAlignment.Begin)
210             {
211                 contentText2.HorizontalAlignment = HorizontalAlignment.End;
212             }
213             else if (contentText2.HorizontalAlignment == HorizontalAlignment.End)
214             {
215                 contentText2.HorizontalAlignment = HorizontalAlignment.Begin;
216             }
217         }
218
219         private void PopupLayoutDirectionChanged(object sender, View.LayoutDirectionChangedEventArgs e)
220         {
221             if (contentText.HorizontalAlignment == HorizontalAlignment.Begin)
222             {
223                 contentText.HorizontalAlignment = HorizontalAlignment.End;
224             }
225             else if (contentText.HorizontalAlignment == HorizontalAlignment.End)
226             {
227                 contentText.HorizontalAlignment = HorizontalAlignment.Begin;
228             }
229         }
230
231         private void PopupButtonClickedEvent(object sender, Popup.ButtonClickEventArgs e)
232         {
233             Popup obj = sender as Popup;
234             if (obj != null && e.ButtonIndex == 0)
235             {
236                 if(obj == popup)
237                 {
238                     index++;
239                     index = index % buttonStyles.Length;
240                     obj.ButtonTextColor = color[index];
241                     contentText.Text = "Popup ButtonStyle is " + buttonStyles[index];
242                 }
243                 else
244                 {
245                     index2++;
246                     index2 = index2 % buttonStyles.Length;
247                     obj.ButtonTextColor = color[index2];
248                     contentText2.Text = "Popup2 ButtonStyle is " + buttonStyles[index2];
249                 }               
250             }
251         }
252
253         private void ButtonClickEvent(object sender, Button.ClickEventArgs e)
254         {
255             if (popup.LayoutDirection == ViewLayoutDirectionType.LTR)
256             {
257                 popup.LayoutDirection = ViewLayoutDirectionType.RTL;
258                 popup2.LayoutDirection = ViewLayoutDirectionType.RTL;
259                 button.Style.Text.Text = "LayoutDirection is right to left";
260             }
261             else
262             {
263                 popup.LayoutDirection = ViewLayoutDirectionType.LTR;
264                 popup2.LayoutDirection = ViewLayoutDirectionType.LTR;
265                 button.Style.Text.Text = "LayoutDirection is left to right";
266             }           
267         }
268
269         public void Deactivate()
270         {
271             if (root != null)
272             {
273                 if (popup != null)
274                 {
275                     if (contentText != null)
276                     {
277                         popup.ContentView.Remove(contentText);
278                         contentText.Dispose();
279                         contentText = null;
280                     }
281
282                     root.Remove(popup);
283                     popup.Dispose();
284                     popup = null;
285                 }
286
287                 if (popup2 != null)
288                 {
289                     if (contentText2 != null)
290                     {
291                         popup2.ContentView.Remove(contentText2);
292                         contentText2.Dispose();
293                         contentText2 = null;
294                     }
295
296                     root.Remove(popup2);
297                     popup2.Dispose();
298                     popup2 = null;
299                 }
300
301                 if (createText[0] != null)
302                 {
303                     root.Remove(createText[0]);
304                     createText[0].Dispose();
305                     createText[0] = null;
306                 }
307
308                 if (createText[1] != null)
309                 {
310                     root.Remove(createText[1]);
311                     createText[1].Dispose();
312                     createText[1] = null;
313                 }
314
315                 if (button != null)
316                 {
317                     root.Remove(button);
318                     button.Dispose();
319                     button = null;
320                 }
321
322                 NUIApplication.GetDefaultWindow().Remove(root);
323                 root.Dispose();
324             }
325         }
326     }
327 }