Merge remote-tracking branch 'origin/master' into tizen
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / SliderSample.cs
1 using Tizen.NUI.BaseComponents;
2 using Tizen.NUI.Components;
3
4 namespace Tizen.NUI.Samples
5 {
6     public class SliderSample : IExample
7     {
8         private const float MIN_VALUE = 0;
9         private const float MAX_VALUE = 100;
10
11         private View root;
12         private View top_parent, bottom_parent, ver_slider_parent, hori_slider_parent;
13         private TextLabel[] createText = new TextLabel[2];
14         private TextLabel[] inforText = new TextLabel[2];
15         private Slider[] slider_null_style = new Slider[4];
16         private Slider[] slider_style = new Slider[4];
17
18         public void Activate()
19         {
20             Window window = NUIApplication.GetDefaultWindow();
21
22             root = new View()
23             {
24                 Size = new Size(1920, 1080),
25                 BackgroundColor = new Color(0.7f, 0.9f, 0.8f, 1.0f),
26             };
27             root.Layout = new LinearLayout() { LinearOrientation = LinearLayout.Orientation.Vertical };
28             window.Add(root);
29
30             // Textlabel of Null style construction/Style construction and infoText
31             CreateTopView();
32
33             // Various kinds of Slider
34             CreateBottomView();
35         }
36
37         private void InitSliders()
38         {
39             // Null style construction
40             slider_null_style[0] = CreateByProperty(800, 50, 20, Slider.DirectionType.Horizontal);
41             slider_null_style[1] = CreateByProperty(800, 50, 30, Slider.DirectionType.Horizontal);
42             slider_null_style[1].LowIndicatorTextContent = "SubText";
43             slider_null_style[1].LowIndicatorSize = new Size(100, 40);
44             slider_null_style[2] = CreateByProperty(50, 400, 20, Slider.DirectionType.Vertical);
45             slider_null_style[3] = CreateByProperty(50, 400, 30, Slider.DirectionType.Vertical);
46             slider_null_style[3].LowIndicatorTextContent = "SubText";
47             slider_null_style[3].LowIndicatorSize = new Size(100, 40);
48
49             // Style construction
50             SliderStyle st = new SliderStyle
51             {
52                 TrackThickness = 4,
53                 Track = new ImageViewStyle
54                 {
55                     BackgroundColor = new Selector<Color>
56                     {
57                         All = new Color(0, 0, 0, 0.1f),
58                     }
59                 },
60
61                 Progress = new ImageViewStyle
62                 {
63                     BackgroundColor = new Selector<Color>
64                     {
65                         All = new Color(0.05f, 0.63f, 0.9f, 1),
66                     }
67                 },
68
69                 Thumb = new ImageViewStyle
70                 {
71                     Size = new Size(60, 60),
72                     ResourceUrl = new Selector<string>
73                     {
74                         Normal = CommonResource.GetFHResourcePath() + "9. Controller/controller_btn_slide_handler_normal.png",
75                         Pressed = CommonResource.GetFHResourcePath() + "9. Controller/controller_btn_slide_handler_press.png",
76                     },
77                     BackgroundImage = new Selector<string>
78                     {
79                         Normal = "",
80                         Pressed = CommonResource.GetFHResourcePath() + "9. Controller/controller_btn_slide_handler_effect.png",
81                     }
82                 }
83             };
84             slider_style[0] = CreateByStyle(st, 800, 50, 20, Slider.DirectionType.Horizontal);
85             slider_style[1] = CreateByStyle(st, 800, 50, 30, Slider.DirectionType.Horizontal);
86             slider_style[1].LowIndicatorTextContent = "SubText";
87             slider_style[1].LowIndicatorSize = new Size(100, 40);
88             slider_style[2] = CreateByStyle(st, 50, 400, 20, Slider.DirectionType.Vertical);
89             slider_style[3] = CreateByStyle(st, 50, 400, 30, Slider.DirectionType.Vertical);
90             slider_style[3].LowIndicatorTextContent = "SubText";
91             slider_style[3].LowIndicatorSize = new Size(100, 40);
92         }
93
94         private void CreateTopView()
95         {
96             top_parent = new View() { Size = new Size(1920, 240) };
97             top_parent.Layout = new GridLayout() { Rows = 2, GridOrientation = GridLayout.Orientation.Horizontal };
98             root.Add(top_parent);
99
100             for (int i = 0; i < 2; i++)
101             {
102                 createText[i] = new TextLabel();
103                 createText[i].PointSize = 20;
104                 createText[i].TextColor = Color.Black;
105                 createText[i].Size = new Size(600, 100);
106                 createText[i].Margin = new Extents(200, 100, 40, 0);
107                 createText[i].BackgroundColor = Color.Magenta;
108                 createText[i].HorizontalAlignment = HorizontalAlignment.Center;
109                 createText[i].VerticalAlignment = VerticalAlignment.Center;
110                 top_parent.Add(createText[i]);
111
112                 inforText[i] = new TextLabel();
113                 inforText[i].PointSize = 20;
114                 inforText[i].TextColor = Color.Blue;
115                 inforText[i].Text = "currentValue = ";
116                 inforText[i].BackgroundColor = new Color(0, 0, 0, 0.1f);
117                 inforText[i].HorizontalAlignment = HorizontalAlignment.Center;
118                 inforText[i].VerticalAlignment = VerticalAlignment.Center;
119                 top_parent.Add(inforText[i]);
120             }
121
122             // TextLabel of "Null style construction"
123             createText[0].Text = "Null style constructions";
124
125             // TextLabel of "Style construction"
126             createText[1].Text = "Style construction";
127         }
128
129         private void CreateBottomView()
130         {
131             bottom_parent = new View() { Size = new Size(1920, 840) };
132             bottom_parent.Layout = new LinearLayout() { LinearOrientation = LinearLayout.Orientation.Vertical, CellPadding = new Size2D(0, 0) };
133             root.Add(bottom_parent);
134
135             // Init Sliders
136             InitSliders();
137
138             // Add Horizontal Slider
139             hori_slider_parent = new View() { Size = new Size(1920, 160) };
140             hori_slider_parent.Layout = new GridLayout() { Rows = 2, GridOrientation = GridLayout.Orientation.Horizontal };
141             bottom_parent.Add(hori_slider_parent);
142             slider_null_style[0].Margin = new Extents(100, 0, 30, 0);
143             hori_slider_parent.Add(slider_null_style[0]);
144             hori_slider_parent.Add(slider_null_style[1]);
145             hori_slider_parent.Add(slider_style[0]);
146             hori_slider_parent.Add(slider_style[1]);
147
148             // Add vertical Slider
149             ver_slider_parent = new View() { Size = new Size(1920, 680) };
150             ver_slider_parent.Layout = new LinearLayout() { LinearOrientation = LinearLayout.Orientation.Horizontal, LinearAlignment = LinearLayout.Alignment.CenterVertical, CellPadding = new Size2D(200, 0) };
151             bottom_parent.Add(ver_slider_parent);
152             slider_null_style[2].Margin = new Extents(350, 0, 0, 0);
153             slider_style[2].Margin = new Extents(400, 0, 0, 0);
154             ver_slider_parent.Add(slider_null_style[2]);
155             ver_slider_parent.Add(slider_null_style[3]);
156             ver_slider_parent.Add(slider_style[2]);
157             ver_slider_parent.Add(slider_style[3]);
158         }
159
160         public void Deactivate()
161         {
162             if (root != null)
163             {
164                 NUIApplication.GetDefaultWindow().Remove(root);
165                 for (int i = 0; i < 2; i++)
166                 {
167                     if (createText[i] != null)
168                     {
169                         createText[i].Dispose();
170                         createText[i] = null;
171                     }
172
173                     if (inforText[i] != null)
174                     {
175                         inforText[i].Dispose();
176                         inforText[i] = null;
177                     }
178                 }
179
180                 for (int j = 0; j < 4; j++)
181                 {
182                     if (slider_null_style[j] != null)
183                     {
184                         slider_null_style[j].Dispose();
185                         slider_null_style[j] = null;
186                     }
187
188                     if (slider_style[j] != null)
189                     {
190                         slider_style[j].Dispose();
191                         slider_style[j] = null;
192                     }
193                 }
194
195                 if (top_parent != null)
196                 {
197                     top_parent.Dispose();
198                     top_parent = null;
199                 }
200
201                 if (bottom_parent != null)
202                 {
203                     bottom_parent.Dispose();
204                     bottom_parent = null;
205                 }
206
207                 if (ver_slider_parent != null)
208                 {
209                     ver_slider_parent.Dispose();
210                     ver_slider_parent = null;
211                 }
212
213                 if (hori_slider_parent != null)
214                 {
215                     hori_slider_parent.Dispose();
216                     hori_slider_parent = null;
217                 }
218                 root.Dispose();
219                 root = null;
220             }
221         }
222
223         private Slider CreateByProperty(int w, int h, int curValue, Slider.DirectionType dir)
224         {
225             // Setting the property of selector is not supported now, so add these in style first.
226             SliderStyle st = new SliderStyle
227             {
228                 Thumb = new ImageViewStyle()
229                 {
230                     ResourceUrl = new Selector<string>
231                     {
232                         Normal = CommonResource.GetFHResourcePath() + "9. Controller/controller_btn_slide_handler_normal.png",
233                         Pressed = CommonResource.GetFHResourcePath() + "9. Controller/controller_btn_slide_handler_press.png",
234                     },
235                     BackgroundImage = new Selector<string>
236                     {
237                         Normal = "",
238                         Pressed = CommonResource.GetFHResourcePath() + "9. Controller/controller_btn_slide_handler_effect.png",
239                     }
240                 }
241             };
242             Slider source = new Slider(st);
243             source.TrackThickness = 4;
244             source.ThumbSize = new Size(60, 60);
245             source.BgTrackColor = new Color(0, 0, 0, 0.1f);
246             source.SlidedTrackColor = new Color(0.05f, 0.63f, 0.9f, 1);
247             source.Direction = dir;
248             source.Focusable = true;
249             source.MinValue = MIN_VALUE;
250             source.MaxValue = MAX_VALUE;
251             source.StateChangedEvent += OnStateChanged;
252             source.ValueChanged += OnValueChanged;
253             source.SlidingFinished += OnSlidingFinished;
254             source.Size = new Size(w, h);
255             source.CurrentValue = curValue;
256             return source;
257         }
258
259         private Slider CreateByStyle(SliderStyle st, int w, int h, int curValue, Slider.DirectionType dir)
260         {
261             // input style in construction
262             Slider source = new Slider(st);
263             source.Direction = dir;
264             root.Add(source);
265             source.Focusable = true;
266             source.MinValue = MIN_VALUE;
267             source.MaxValue = MAX_VALUE;
268             source.StateChangedEvent += OnStateChanged;
269             source.ValueChanged += OnValueChanged;
270             source.SlidingFinished += OnSlidingFinished;
271             source.Size = new Size(w, h);
272             source.CurrentValue = curValue;
273             return source;
274         }
275
276         private void OnValueChanged(object sender, SliderValueChangedEventArgs args)
277         {
278             Slider source = sender as Slider;
279             if (source != null)
280             {
281                 if (source == slider_style[0] || source == slider_style[1] || source == slider_style[2] || source == slider_style[3])
282                 {
283                     inforText[1].Text = "currentValue = " + args.CurrentValue;
284                 }
285                 else
286                 {
287                     inforText[0].Text = "currentValue = " + args.CurrentValue;
288                 }
289             }
290         }
291
292         private void OnSlidingFinished(object sender, SliderSlidingFinishedEventArgs args)
293         {
294             Slider source = sender as Slider;
295             if (source != null)
296             {
297                 if (source == slider_style[0] || source == slider_style[1] || source == slider_style[2] || source == slider_style[3])
298                 {
299                     inforText[1].Text = "Finished currentValue = " + args.CurrentValue;
300                 }
301                 else
302                 {
303                     inforText[0].Text = "Finished currentValue = " + args.CurrentValue;
304                 }
305             }
306         }
307
308         private void OnStateChanged(object sender, Slider.StateChangedArgs args)
309         {
310             if (sender is Tizen.NUI.Components.Slider)
311             {
312                 Tizen.NUI.Components.Slider slider = sender as Tizen.NUI.Components.Slider;
313                 if (slider != null)
314                 {
315                     // Do something
316                 }
317             }
318         }
319     }
320 }