[NUI] Merge NUI.Samples to TizenFX (#1222)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / InputFieldSample.cs
1 using System;
2 using System.Collections.Generic;
3 using Tizen.NUI.BaseComponents;
4 using Tizen.NUI.Components;
5
6 namespace Tizen.NUI.Samples
7 {
8     public class InputFieldSample : IExample
9     {
10         private TextLabel guideText;
11         private View rootView;
12         InputField inputField;
13         InputFieldStyle inputFieldAttrs;
14
15         public void Activate()
16         {
17             Window window = Window.Instance;
18
19             rootView = new View()
20             {
21                 WidthResizePolicy = ResizePolicyType.FillToParent,
22                 HeightResizePolicy = ResizePolicyType.FillToParent,
23                 BackgroundColor = new Color(0.8f, 0.8f, 0.8f, 0.8f),
24                 Focusable = true
25             };
26             window.Add(rootView);
27             rootView.TouchEvent += OnRootViewTouchEvent;
28             CreateInputField();
29             CreateGuideText();
30         }
31
32         private void CreateGuideText()
33         {
34             guideText = new TextLabel();
35             guideText.Size2D = new Size2D(1600, 150);
36             guideText.Position2D = new Position2D(30, 30);
37             guideText.TextColor = Color.Blue;
38             guideText.BackgroundColor = Color.White;
39             guideText.PointSize = 15;
40             guideText.MultiLine = true;
41             guideText.Focusable = true;
42             rootView.Add(guideText);
43             guideText.Text =
44                 "Tips: \n" +
45                 "This InputField is created with attibutes; \n" +
46                 "User can input text after press on it; \n" +
47                 "User can exit the sample by press \"Esc\" key after touch on any area except the InputField.";
48         }
49
50         private void CreateInputField()
51         {
52             inputFieldAttrs = new InputFieldStyle();
53             inputFieldAttrs.Space = 24;
54             inputFieldAttrs.BackgroundImageAttributes = new ImageViewStyle
55             {
56                 ResourceUrl = new Selector<string> { All = CommonResource.GetFHResourcePath() + "1. Action bar/search_bg.png" },
57                 Border = new Selector<Rectangle> { All = new Rectangle(45, 45, 0, 0) }
58             };
59
60             inputFieldAttrs.InputBoxAttributes = new TextFieldStyle
61             {
62                 TextColor = new Selector<Color>
63                 {
64                     Normal = new Color(0, 0, 0, 1),
65                     Pressed = new Color(0, 0, 0, 1),
66                     Disabled = new Color(0, 0, 0, 0.4f)
67                 },
68                 PlaceholderTextColor = new Selector<Color>
69                 {
70                     All = new Color(0, 0, 0, 0.4f)
71                 },
72                 HorizontalAlignment =  HorizontalAlignment.Begin,
73                 VerticalAlignment =  VerticalAlignment.Center,
74                 FontFamily = "SamsungOne 500",
75                 PointSize = new Selector<float?>
76                 {
77                     All = 38
78                 },
79                 CursorWidth = 2,
80             };
81
82             inputField = new InputField(inputFieldAttrs);
83             inputField.Size2D = new Size2D(1600, 95);
84             inputField.Position2D = new Position2D(100, 300);
85             //inputField.Focusable = true;
86             rootView.Add(inputField);
87             inputField.FocusGained += onFocusGained;
88             inputField.FocusLost += onFocusLost;
89             //inputField.TouchEvent += onTouchEvent;
90             inputField.HintText = "Please input key word...";
91         }
92
93         private bool OnRootViewTouchEvent(object sender, View.TouchEventArgs e)
94         {
95             FocusManager.Instance.SetCurrentFocusView(rootView);
96             return false;
97         }
98
99         private void onFocusLost(object sender, EventArgs e)
100         {
101             
102         }
103
104         private void onFocusGained(object sender, EventArgs e)
105         {
106             
107         }
108
109         //private bool onTouchEvent(object sender, View.TouchEventArgs e)
110         //{
111         //    return false;
112         //}
113
114         public void Deactivate()
115         {
116             if (inputField != null)
117             {
118                 inputField.FocusGained -= onFocusGained;
119                 inputField.FocusLost -= onFocusLost;
120                 //inputField.TouchEvent -= onTouchEvent;
121                 rootView.Remove(inputField);
122                 inputField.Dispose();
123                 inputField = null;
124             }
125             if (rootView != null)
126             {
127                 rootView.TouchEvent -= OnRootViewTouchEvent;
128                 Window.Instance.Remove(rootView);
129                 rootView.Dispose();
130             }
131         }
132     }
133 }