Release 8.0.0.15408
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / ButtonSample.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 ButtonSample : IExample
8     {
9         private View root;
10         private View parent1;
11         private View parent2;
12         private View parent3;
13         Button textButton;
14         Button iconButton;
15
16         Button iconTextButton;
17
18         Button utilityBasicButton;
19         Button utilityServiceButton;
20         Button utilityToggleButton;
21         Button utilityOvalButton;
22
23         Button familyBasicButton;
24         Button familyServiceButton;
25         Button familyToggleButton;
26         Button familyOvalButton;
27
28         public void Activate()
29         {
30             Window window = NUIApplication.GetDefaultWindow();
31             root = new View()
32             {
33                 Size = new Size(1920, 1080),
34                 BackgroundColor = new Color(0.7f, 0.9f, 0.8f, 1.0f),
35                 Layout = new LinearLayout()
36                 {
37                     LinearAlignment = LinearLayout.Alignment.Center,
38                     LinearOrientation = LinearLayout.Orientation.Horizontal,
39                     CellPadding = new Size(50, 50),
40                 }
41             };
42             window.Add(root);
43             window.KeyEvent += Window_KeyEvent;
44
45             parent1 = new View()
46             {
47                 Size = new Size(300, 900),
48                 Layout = new LinearLayout()
49                 {
50                     LinearAlignment = LinearLayout.Alignment.Top,
51                     LinearOrientation = LinearLayout.Orientation.Vertical,
52                     CellPadding = new Size(50, 50),
53                 }
54             };
55
56             parent2 = new View()
57             {
58                 Size = new Size(300, 900),
59                 Layout = new LinearLayout()
60                 {
61                     LinearAlignment = LinearLayout.Alignment.Top,
62                     LinearOrientation = LinearLayout.Orientation.Vertical,
63                     CellPadding = new Size(50, 50),
64                 }
65             };
66
67             // Only show a text button.
68             textButton = new Button();
69             textButton.BackgroundImage = CommonResource.GetTVResourcePath() + "component/c_buttonbasic/c_basic_button_white_bg_normal_9patch.png";
70             textButton.BackgroundImageBorder = new Rectangle(4, 4, 5, 5);
71             textButton.Size = new Size(300, 80);
72             textButton.TextLabel.Text = "Button";
73             parent1.Add(textButton);
74
75             //Only show an icon button.
76             iconButton = new Button();
77             iconButton.Text = "";
78             iconButton.Name = "IconButton";
79             iconButton.BackgroundImage = CommonResource.GetTVResourcePath() + "component/c_buttonbasic/c_basic_button_white_bg_normal_9patch.png";
80             iconButton.BackgroundImageBorder = new Rectangle(4, 4, 5, 5);
81             iconButton.Size = new Size(80, 80);
82             iconButton.Icon.ResourceUrl = CommonResource.GetTVResourcePath() + "component/c_radiobutton/c_radiobutton_white_check.png";
83             parent2.Add(iconButton);
84             iconButton.ClickEvent += (ojb, e) => {
85                 var btn = iconButton.Icon.GetParent() as Button;
86                 string name = btn.Name;
87             };
88
89             parent3 = new View()
90             {
91                 Size = new Size(600, 400),
92                 Layout = new LinearLayout()
93                 {
94                     LinearAlignment = LinearLayout.Alignment.Top,
95                     LinearOrientation = LinearLayout.Orientation.Vertical,
96                     CellPadding = new Size(50, 50),
97                 }
98             };
99
100             //Show a button with icon and text.
101             iconTextButton = new Button();
102             iconTextButton.Text = "IconTextButton";
103             iconTextButton.BackgroundImage = CommonResource.GetTVResourcePath() + "component/c_buttonbasic/c_basic_button_white_bg_normal_9patch.png";
104             iconTextButton.BackgroundImageBorder = new Rectangle(4, 4, 5, 5);
105             iconTextButton.IconRelativeOrientation = Button.IconOrientation.Left;
106             iconTextButton.Icon.ResourceUrl = CommonResource.GetTVResourcePath() + "component/c_radiobutton/c_radiobutton_white_check.png";
107             iconTextButton.IconPadding = new Extents(20, 20, 20, 20);
108             iconTextButton.TextPadding = new Extents(20, 50, 20, 20);
109             iconTextButton.Size = new Size(500, 300);
110             parent3.Add(iconTextButton);
111
112             ///////////////////////////////////////////////Create by Property//////////////////////////////////////////////////////////
113             //Create utility basic style of button.
114             var utilityBasicButtonStyle = new ButtonStyle()
115             {
116                 Overlay = new ImageViewStyle()
117                 {
118                     ResourceUrl = new Selector<string>
119                     {
120                         Pressed = CommonResource.GetFHResourcePath() + "3. Button/rectangle_btn_press_overlay.png",
121                         Other = ""
122                     },
123                     Border = new Rectangle(5, 5, 5, 5)
124                 },
125                 Text = new TextLabelStyle()
126                 {
127                     TextColor = new Selector<Color>
128                     {
129                         Normal = new Color(0, 0, 0, 1),
130                         Pressed = new Color(0, 0, 0, 0.7f),
131                         Selected = new Color(0.058f, 0.631f, 0.92f, 1),
132                         Disabled = new Color(0, 0, 0, 0.4f)
133                     },
134                     Text = "UtilityBasicButton",
135                     PointSize = 20,
136                 },
137                 BackgroundImage = CommonResource.GetFHResourcePath() + "3. Button/rectangle_btn_normal.png",
138                 BackgroundImageBorder = new Rectangle(5, 5, 5, 5),
139             };
140             utilityBasicButton = new Button();
141             utilityBasicButton.ApplyStyle(utilityBasicButtonStyle);
142             utilityBasicButton.IsSelectable = true;
143             utilityBasicButton.ImageShadow = new ImageShadow
144             {
145                 Url = CommonResource.GetFHResourcePath() + "3. Button/rectangle_btn_shadow.png",
146                 Border = new Rectangle(5, 5, 5, 5)
147             };
148             utilityBasicButton.Size = new Size(300, 80);
149             utilityBasicButton.IsEnabled = false;
150             parent1.Add(utilityBasicButton);
151
152             //Create utility service style of button.
153             var utilityServiceButtonStyle = new ButtonStyle()
154             {
155                 Overlay = new ImageViewStyle()
156                 {
157                     ResourceUrl = new Selector<string>
158                     {
159                         Pressed = CommonResource.GetFHResourcePath() + "3. Button/rectangle_btn_press_overlay.png",
160                         Other = ""
161                     },
162                     Border = new Rectangle(5, 5, 5, 5)
163                 },
164                 Text = new TextLabelStyle()
165                 {
166                     TextColor = new Selector<Color>
167                     {
168                         Normal = new Color(1, 1, 1, 1),
169                         Pressed = new Color(1, 1, 1, 0.7f),
170                         Disabled = new Color(1, 1, 1, 0.4f)
171                     },
172                     Text = "ServiceBasicButton",
173                     PointSize = 20,
174                 },
175                 BackgroundImage = CommonResource.GetFHResourcePath() + "3. Button/rectangle_point_btn_normal.png",
176                 BackgroundImageBorder = new Rectangle(5, 5, 5, 5)
177             };
178             utilityServiceButton = new Button();
179             utilityServiceButton.ApplyStyle(utilityServiceButtonStyle);
180             utilityServiceButton.ImageShadow = new ImageShadow
181             {
182                 Url = CommonResource.GetFHResourcePath() + "3. Button/rectangle_btn_shadow.png",
183                 Border = new Rectangle(5, 5, 5, 5)
184             };
185
186             utilityServiceButton.Size = new Size(300, 80);
187             parent1.Add(utilityServiceButton);
188
189             //Create utility toggle style of button.
190             var utilityToggleButtonStyle = new ButtonStyle()
191             {
192                 BackgroundImage = new Selector<string>
193                 {
194                     Normal = CommonResource.GetFHResourcePath() + "3. Button/rectangle_toggle_btn_normal.png",
195                     Selected = CommonResource.GetFHResourcePath() + "3. Button/rectangle_point_btn_normal.png",
196                 },
197                 Overlay = new ImageViewStyle()
198                 {
199                     ResourceUrl = new Selector<string>
200                     {
201                         Pressed = CommonResource.GetFHResourcePath() + "3. Button/rectangle_btn_press_overlay.png",
202                         Other = ""
203                     }
204                 },
205                 Text = new TextLabelStyle()
206                 {
207                     TextColor = new Selector<Color>
208                     {
209                         Normal = new Color(0.058f, 0.631f, 0.92f, 1),
210                         Selected = new Color(1, 1, 1, 1),
211                     },
212                     Text = new Selector<string>
213                     {
214                         Normal = "Toggle Off",
215                         Selected = "Toggle On"
216                     },
217                     PointSize = 20
218                 },
219                 BackgroundColor = new Selector<Color>()
220             };
221             utilityToggleButton = new Button();
222             utilityToggleButton.ApplyStyle(utilityToggleButtonStyle);
223             utilityToggleButton.IsSelectable = true;
224             utilityToggleButton.BackgroundImageBorder = new Rectangle(5, 5, 5, 5);
225             utilityToggleButton.ImageShadow = new ImageShadow
226             {
227                 Url = CommonResource.GetFHResourcePath() + "3. Button/rectangle_btn_shadow.png",
228                 Border = new Rectangle(5, 5, 5, 5)
229             };
230             utilityToggleButton.OverlayImage.Border = new Rectangle(5, 5, 5, 5);
231
232             utilityToggleButton.Size = new Size(300, 80);
233             parent1.Add(utilityToggleButton);
234
235             //Create utility oval style of button.
236             var utilityOvalButtonStyle = new ButtonStyle()
237             {
238                 BackgroundImage = new Selector<string>
239                 {
240                     Normal = CommonResource.GetFHResourcePath() + "3. Button/oval_toggle_btn_normal.png",
241                     Selected = CommonResource.GetFHResourcePath() + "3. Button/oval_toggle_btn_select.png",
242                 },
243                 Overlay = new ImageViewStyle()
244                 {
245                     ResourceUrl = new Selector<string>
246                     {
247                         Pressed = CommonResource.GetFHResourcePath() + "3. Button/oval_toggle_btn_press_overlay.png",
248                         Other = ""
249                     }
250                 },
251                 Text = new TextLabelStyle()
252                 {
253                     Text = "",
254                 },
255                 BackgroundColor = new Selector<Color>(),
256             };
257             utilityOvalButton = new Button();
258             utilityOvalButton.ApplyStyle(utilityOvalButtonStyle);
259             utilityOvalButton.IsSelectable = true;
260             utilityOvalButton.BackgroundImageBorder = new Rectangle(5, 5, 5, 5);
261             utilityOvalButton.ImageShadow = new ImageShadow
262             {
263                 Url = CommonResource.GetFHResourcePath() + "3. Button/oval_toggle_btn_shadow.png",
264                 Border = new Rectangle(5, 5, 5, 5)
265             };
266             utilityOvalButton.OverlayImage.Border = new Rectangle(5, 5, 5, 5);
267
268             utilityOvalButton.Size = new Size(104, 104);
269             utilityOvalButton.TextLabel.PointSize = 20;
270             parent1.Add(utilityOvalButton);
271
272             ///////////////////////////////////////////////Create by Attributes//////////////////////////////////////////////////////////
273             //Create family basic style of Button.
274             ButtonStyle familyBasicButtonStyle = new ButtonStyle
275             {
276                 IsSelectable = true,
277                 BackgroundImage = new Selector<string> { All = CommonResource.GetFHResourcePath() + "3. Button/rectangle_btn_normal.png" },
278                 BackgroundImageBorder = new Selector<Rectangle> { All = new Rectangle(5, 5, 5, 5) },
279                 ImageShadow = new ImageShadow
280                 {
281                     Url = CommonResource.GetFHResourcePath() + "3. Button/rectangle_btn_shadow.png",
282                     Border = new Rectangle(5, 5, 5, 5)
283                 },
284
285                 Overlay = new ImageViewStyle
286                 {
287                     ResourceUrl = new Selector<string> { Pressed = CommonResource.GetFHResourcePath() + "3. Button/rectangle_btn_press_overlay.png", Other = "" },
288                     Border = new Selector<Rectangle> { All = new Rectangle(5, 5, 5, 5) },
289                 },
290
291                 Text = new TextLabelStyle
292                 {
293                     PointSize = new Selector<float?> { All = 20 },
294                     HorizontalAlignment = HorizontalAlignment.Center,
295                     VerticalAlignment = VerticalAlignment.Center,
296                     WidthResizePolicy = ResizePolicyType.FillToParent,
297                     HeightResizePolicy = ResizePolicyType.FillToParent,
298
299                     TextColor = new Selector<Color>
300                     {
301                         Normal = new Color(0, 0, 0, 1),
302                         Pressed = new Color(0, 0, 0, 0.7f),
303                         Selected = new Color(0.141f, 0.769f, 0.278f, 1),
304                         Disabled = new Color(0, 0, 0, 0.4f),
305                     },
306                     Text = "FamilyBasicButton",
307                 }
308             };
309             familyBasicButton = new Button(familyBasicButtonStyle);
310             familyBasicButton.Size = new Size(300, 80);
311             parent2.Add(familyBasicButton);
312
313             //Create family service style of button.
314             ButtonStyle familyServiceButtonStyle = new ButtonStyle
315             {
316                 IsSelectable = false,
317                 BackgroundImage = new Selector<string> { All = CommonResource.GetFHResourcePath() + "3. Button/[Button] App Primary Color/rectangle_point_btn_normal_24c447.png" },
318                 BackgroundImageBorder = new Selector<Rectangle> { All = new Rectangle(5, 5, 5, 5) },
319
320                 ImageShadow = new ImageShadow
321                 {
322                     Url = CommonResource.GetFHResourcePath() + "3. Button/rectangle_btn_shadow.png",
323                     Border = new Rectangle(5, 5, 5, 5)
324                 },
325
326                 Overlay = new ImageViewStyle
327                 {
328                     ResourceUrl = new Selector<string> { Pressed = CommonResource.GetFHResourcePath() + "3. Button/rectangle_btn_press_overlay.png", Other = "" },
329                     Border = new Selector<Rectangle> { All = new Rectangle(5, 5, 5, 5) },
330                 },
331
332                 Text = new TextLabelStyle
333                 {
334                     PointSize = new Selector<float?> { All = 20 },
335                     HorizontalAlignment = HorizontalAlignment.Center,
336                     VerticalAlignment = VerticalAlignment.Center,
337                     WidthResizePolicy = ResizePolicyType.FillToParent,
338                     HeightResizePolicy = ResizePolicyType.FillToParent,
339
340                     TextColor = new Selector<Color>
341                     {
342                         Normal = new Color(1, 1, 1, 1),
343                         Pressed = new Color(1, 1, 1, 0.7f),
344                         Disabled = new Color(1, 1, 1, 0.4f),
345                     },
346                     Text = "FamilySeviceButton"
347                 }
348             };
349             familyServiceButton = new Button(familyServiceButtonStyle);
350             familyServiceButton.Size = new Size(300, 80);
351             parent2.Add(familyServiceButton);
352
353             //Create family toggle style of button.
354             ButtonStyle familyToggleButtonStyle = new ButtonStyle
355             {
356                 IsSelectable = true,
357                 BackgroundImage = new Selector<string>
358                 {
359                     Normal = CommonResource.GetFHResourcePath() + "3. Button/[Button] App Primary Color/rectangle_toggle_btn_normal_24c447.png",
360                     Selected = CommonResource.GetFHResourcePath() + "3. Button/[Button] App Primary Color/rectangle_point_btn_normal_24c447.png",
361                 },
362                 BackgroundImageBorder = new Selector<Rectangle> { All = new Rectangle(5, 5, 5, 5) },
363
364                 ImageShadow = new ImageShadow
365                 {
366                     Url = CommonResource.GetFHResourcePath() + "3. Button/rectangle_btn_shadow.png",
367                     Border = new Rectangle(5, 5, 5, 5)
368                 },
369
370                 Overlay = new ImageViewStyle
371                 {
372                     ResourceUrl = new Selector<string> { Pressed = CommonResource.GetFHResourcePath() + "3. Button/rectangle_btn_press_overlay.png", Other = "" },
373                     Border = new Selector<Rectangle> { All = new Rectangle(5, 5, 5, 5) },
374                 },
375
376                 Text = new TextLabelStyle
377                 {
378                     PointSize = new Selector<float?> { All = 20 },
379                     HorizontalAlignment = HorizontalAlignment.Center,
380                     VerticalAlignment = VerticalAlignment.Center,
381                     WidthResizePolicy = ResizePolicyType.FillToParent,
382                     HeightResizePolicy = ResizePolicyType.FillToParent,
383
384                     TextColor = new Selector<Color>
385                     {
386                         Normal = new Color(0.141f, 0.769f, 0.278f, 1),
387                         Selected = new Color(1, 1, 1, 1),
388                     },
389                     Text = new Selector<string>
390                     {
391                         Normal = "Toggle Off",
392                         Selected = "Toggle On"
393                     }
394                 }
395             };
396             familyToggleButton = new Button(familyToggleButtonStyle);
397             familyToggleButton.Size = new Size(300, 80);
398             parent2.Add(familyToggleButton);
399
400             //Create family oval style of button.
401             ButtonStyle familyOvalButtonStyle = new ButtonStyle
402             {
403                 IsSelectable = true,
404                 BackgroundImage = new Selector<string>
405                 {
406                     Normal = CommonResource.GetFHResourcePath() + "3. Button/oval_toggle_btn_normal.png",
407                     Selected = CommonResource.GetFHResourcePath() + "3. Button/[Button] App Primary Color/oval_toggle_btn_select_24c447.png",
408                 },
409                 BackgroundImageBorder = new Selector<Rectangle> { All = new Rectangle(5, 5, 5, 5) },
410
411                 ImageShadow = new ImageShadow
412                 {
413                     Url = CommonResource.GetFHResourcePath() + "3. Button/oval_toggle_btn_shadow.png",
414                     Border = new Rectangle(5, 5, 5, 5)
415                 },
416
417                 Overlay = new ImageViewStyle
418                 {
419                     ResourceUrl = new Selector<string> { Pressed = CommonResource.GetFHResourcePath() + "3. Button/oval_toggle_btn_press_overlay.png", Other = "" },
420                     Border = new Selector<Rectangle> { All = new Rectangle(5, 5, 5, 5) },
421                 },
422             };
423             familyOvalButton = new Button(familyOvalButtonStyle);
424             familyOvalButton.Size = new Size(104, 104);
425             parent2.Add(familyOvalButton);
426
427             // Add three layout into root
428             root.Add(parent1);
429             root.Add(parent2);
430             root.Add(parent3);
431         }
432
433         private void Window_KeyEvent(object sender, Window.KeyEventArgs e)
434         {
435             if(e.Key.State == Key.StateType.Up)
436             {
437                 switch(e.Key.KeyPressedName)
438                 {
439                     case "1":
440                         iconTextButton.IconRelativeOrientation = Button.IconOrientation.Right;
441                         break;
442                     case "2":
443                         iconTextButton.IconRelativeOrientation = Button.IconOrientation.Top;
444                         break;
445                     case "3":
446                         iconTextButton.IconRelativeOrientation = Button.IconOrientation.Bottom;
447                         break;
448                     case "4":
449                         iconTextButton.IconRelativeOrientation = Button.IconOrientation.Left;
450                         break;
451                     case "5":
452                         iconTextButton.Icon.Padding.Start = 50;
453                         break;
454                     case "6":
455                         iconTextButton.Icon.Padding.End = 50;
456                         break;
457                     case "7":
458                         iconTextButton.LayoutDirection = ViewLayoutDirectionType.RTL;
459                         break;
460                     case "8":
461                         iconTextButton.LayoutDirection = ViewLayoutDirectionType.LTR;
462                         break;
463                 }
464             }
465         }
466
467         public void Deactivate()
468         {
469             if (root != null)
470             {
471                 parent1.Remove(textButton);
472                 textButton.Dispose();
473                 textButton = null;
474                 parent1.Remove(utilityBasicButton);
475                 utilityBasicButton.Dispose();
476                 utilityBasicButton = null;
477                 parent1.Remove(utilityServiceButton);
478                 utilityServiceButton.Dispose();
479                 utilityServiceButton = null;
480                 parent1.Remove(utilityToggleButton);
481                 utilityToggleButton.Dispose();
482                 utilityToggleButton = null;
483                 parent1.Remove(utilityOvalButton);
484                 utilityOvalButton.Dispose();
485                 utilityOvalButton = null;
486
487                 parent1.Remove(familyBasicButton);
488                 familyBasicButton.Dispose();
489                 familyBasicButton = null;
490                 parent1.Remove(familyServiceButton);
491                 familyServiceButton.Dispose();
492                 familyServiceButton = null;
493                 parent1.Remove(familyToggleButton);
494                 familyToggleButton.Dispose();
495                 familyToggleButton = null;
496                 parent1.Remove(familyOvalButton);
497                 familyOvalButton.Dispose();
498                 familyOvalButton = null;
499
500                 parent3.Remove(iconTextButton);
501                 iconTextButton.Dispose();
502                 iconTextButton = null;
503
504                 root.Remove(parent1);
505                 parent1.Dispose();
506                 parent1 = null;
507
508                 root.Remove(parent2);
509                 parent2.Dispose();
510                 parent2 = null;
511
512                 root.Remove(parent3);
513                 parent3.Dispose();
514                 parent3 = null;
515
516                 NUIApplication.GetDefaultWindow().Remove(root);
517                 root.Dispose();
518                 root = null;
519             }
520         }
521     }
522 }