Size, Position, Color high level class added
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / examples / control-dashboard.cs
1 /** Copyright (c) 2017 Samsung Electronics Co., Ltd.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16
17 using System;
18 using System.Runtime.InteropServices;
19 using Dali;
20
21 namespace MyCSharpExample
22 {
23     class Example
24     {
25         // This is simple structure to contain Control name and implement state at once
26         // name : control name
27         // isImplemented : the state which the control is implemented in public or not
28         private struct Item
29         {
30             public String name;
31             public bool isImplemented;
32
33             public Item(String name, bool isImplemented)
34             {
35                 this.name = name;
36                 this.isImplemented = isImplemented;
37             }
38         }
39
40         private Dali.Application _application;
41         private TableView _contentContainer;
42         private Timer _timer;
43         private Stage _stage;
44         private Popup _popup;
45         private ProgressBar _progressBar;
46
47         // List of items
48         private Item[] mViewList = {
49       new Item("PushButton", true),  new Item("DropDown", false),    new Item("Toggle", true),
50       new Item("InputField", false),  new Item("AnimateGif", false),  new Item("Loading", false),
51       new Item("ProgressBar", true), new Item("CheckBox", false),    new Item("RadioButton", true),
52       new Item("Tooltip", true),     new Item("Popup", true),       new Item("Toast", true),
53       new Item("ItemView", false),    new Item("CheckBox", true)
54     };
55
56         public Example(Dali.Application application)
57         {
58             _application = application;
59             _application.Initialized += OnInitialize;
60         }
61
62         public void OnInitialize(object source, NUIApplicationInitEventArgs e)
63         {
64             Console.WriteLine("Customized Application Initialize event handler");
65             _stage = Stage.GetCurrent();
66             _stage.BackgroundColor = Color.White;
67
68             // Top label
69             TextLabel topLabel = new TextLabel();
70             topLabel.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.WIDTH);
71             topLabel.SetResizePolicy(ResizePolicyType.SIZE_RELATIVE_TO_PARENT, DimensionType.HEIGHT);
72             topLabel.AnchorPoint = NDalic.AnchorPointTopCenter;
73             topLabel.Position = new Position(0, 0, 0);
74             topLabel.SetSizeModeFactor(new Vector3(0.0f, 0.1f, 0.0f));
75             topLabel.BackgroundColor = new Color(43.0f / 255.0f, 145.0f / 255.0f, 175.0f / 255.0f, 1.0f);
76             topLabel.TextColor = NDalic.WHITE;
77             topLabel.Text = " DALi Views";
78             topLabel.HorizontalAlignment = "BEGIN";
79             topLabel.VerticalAlignment = "CENTER";
80             topLabel.PointSize = 42.0f;
81             _stage.Add(topLabel);
82
83             // Grid container to contain items. Use tableView because FlexContainer support focus navigation just two direction ( up/down or left/right )
84             _contentContainer = new TableView(6, 5);
85             _contentContainer.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.WIDTH);
86             _contentContainer.SetResizePolicy(ResizePolicyType.SIZE_RELATIVE_TO_PARENT, DimensionType.HEIGHT);
87             _contentContainer.SetSizeModeFactor(new Vector3(0.0f, 0.9f, 0.0f));
88             _contentContainer.AnchorPoint = NDalic.AnchorPointBottomCenter;
89             _contentContainer.Position = new Position(0, _stage.Size.Height * 0.1f, 0);
90             _contentContainer.SetRelativeHeight(0, 0.07f);
91             _contentContainer.SetRelativeHeight(1, 0.26f);
92             _contentContainer.SetRelativeHeight(2, 0.07f);
93             _contentContainer.SetRelativeHeight(3, 0.26f);
94             _contentContainer.SetRelativeHeight(4, 0.07f);
95             _contentContainer.SetRelativeHeight(5, 0.26f);
96             _contentContainer.SetKeyboardFocusable(true);
97             _stage.Add(_contentContainer);
98
99             CreateContent();
100
101             FocusManager.Instance.PreFocusChange += OnPreFocusChange;
102         }
103
104         // Callback for KeyboardFocusManager
105         private Actor OnPreFocusChange(object source, FocusManager.PreFocusChangeEventArgs e)
106         {
107             if (!e.Proposed && !e.Current)
108             {
109                 e.Proposed = _contentContainer.GetChildAt(1);
110             }
111             return e.Proposed;
112         }
113
114         private void CreateContent()
115         {
116             for (int i = 0; i < mViewList.Length; i++)
117             {
118                 CreateItem(mViewList[i], i);
119             }
120         }
121
122         private void CreateItem(Item item, int idx)
123         {
124             // Make label for item
125             TextLabel itemLabel = new TextLabel("    " + item.name);
126             itemLabel.Size = new Vector3(_stage.GetSize().Width * 0.2f, _stage.GetSize().Height * 0.05f, 0.0f);
127             itemLabel.HorizontalAlignment = "BEGIN";
128             itemLabel.VerticalAlignment = "BOTTOM";
129             itemLabel.PointSize = 18.0f;
130             _contentContainer.AddChild(itemLabel, new TableView.CellPosition(((uint)idx / 5) * 2, (uint)idx % 5));
131
132             // If item is implemented in public, attach it on stage
133             if (item.isImplemented)
134             {
135                 if (item.name.CompareTo("PushButton") == 0)
136                 {
137                     PushButton pushButton = new PushButton();
138                     pushButton.LabelText = "Push Button";
139                     pushButton.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.WIDTH);
140                     pushButton.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.HEIGHT);
141                     pushButton.UnselectedColor = new Vector4(1.0f, 0.0f, 0.0f, 1.0f);
142                     pushButton.SelectedColor = new Vector4(0.0f, 1.0f, 0.0f, 1.0f);
143                     pushButton.Clicked += (obj, e) =>
144                     {
145                         e.Button.LabelText = "Click Me";
146                         e.Button.UnselectedColor = new Vector4(0.0f, 0.0f, 1.0f, 1.0f);
147                         return true;
148                     };
149
150                     _contentContainer.AddChild(pushButton, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
151                 }
152                 if (item.name.CompareTo("DropDown") == 0)
153                 {
154
155                 }
156                 if (item.name.CompareTo("Toggle") == 0)
157                 {
158                     ToggleButton toggleButton = new ToggleButton();
159                     Dali.Property.Array array = new Dali.Property.Array();
160                     array.Add(new Dali.Property.Value("./images/star-highlight.png"));
161                     array.Add(new Dali.Property.Value("./images/star-mod.png"));
162                     array.Add(new Dali.Property.Value("./images/star-dim.png"));
163                     toggleButton.StateVisuals = array;
164
165                     Dali.Property.Array tooltips = new Dali.Property.Array();
166                     tooltips.Add(new Dali.Property.Value("State A"));
167                     tooltips.Add(new Dali.Property.Value("State B"));
168                     tooltips.Add(new Dali.Property.Value("State C"));
169                     toggleButton.Tooltips = tooltips;
170
171                     toggleButton.WidthResizePolicy = "FILL_TO_PARENT";
172                     toggleButton.HeightResizePolicy = "FILL_TO_PARENT";
173                     toggleButton.Clicked += (obj, e) =>
174                     {
175                         Console.WriteLine("Toggle button state changed.");
176                         return true;
177                     };
178
179                     _contentContainer.AddChild(toggleButton, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
180                 }
181                 if (item.name.CompareTo("InputField") == 0)
182                 {
183
184                 }
185                 if (item.name.CompareTo("AnimateGif") == 0)
186                 {
187
188                 }
189                 if (item.name.CompareTo("Loading") == 0)
190                 {
191
192                 }
193                 if (item.name.CompareTo("ProgressBar") == 0)
194                 {
195                     _progressBar = new ProgressBar();
196                     _progressBar.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.WIDTH);
197                     _progressBar.SetResizePolicy(ResizePolicyType.FIXED, DimensionType.HEIGHT);
198                     _progressBar.SetSize(0, 50);
199
200                     _progressBar.ValueChanged += OnProgressBarValueChanged;
201
202                     _timer = new Timer(100);
203                     _timer.Tick += (obj, e) =>
204                     {
205                         float progress = (float)Math.Round(_progressBar.ProgressValue, 2);
206
207                         if (progress == 1.0f)
208                         {
209                             _progressBar.ProgressValue = 0.0f;
210                             _progressBar.SecondaryProgressValue = 0.01f;
211                         }
212                         else
213                         {
214                             _progressBar.ProgressValue = progress + 0.01f;
215                             _progressBar.SecondaryProgressValue = progress + 0.21f;
216                         }
217                         return true;
218                     };
219                     _timer.Start();
220
221                     _contentContainer.AddChild(_progressBar, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
222                 }
223                 if (item.name.CompareTo("ScrollBar") == 0)
224                 {
225
226                 }
227                 if (item.name.CompareTo("CheckBox") == 0)
228                 {
229                     CheckBoxButton checkBoxButton = new CheckBoxButton();
230                     checkBoxButton.LabelText = "Yes";
231
232                     _contentContainer.AddChild(checkBoxButton, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
233                 }
234                 if (item.name.CompareTo("RadioButton") == 0)
235                 {
236                     TableView tableView = new TableView(2, 1);
237                     tableView.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.WIDTH);
238                     tableView.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.HEIGHT);
239
240                     RadioButton rButton = new RadioButton();
241                     rButton.LabelText = "Yes";
242                     rButton.Selected = true;
243
244                     tableView.AddChild(rButton, new TableView.CellPosition(0, 0));
245
246                     rButton = new RadioButton();
247                     rButton.LabelText = "No";
248
249                     tableView.AddChild(rButton, new TableView.CellPosition(1, 0));
250
251                     _contentContainer.AddChild(tableView, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
252                 }
253                 if (item.name.CompareTo("Tooltip") == 0)
254                 {
255                     TableView tableView = new TableView(2, 1);
256                     tableView.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.WIDTH);
257                     tableView.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.HEIGHT);
258
259                     // Create two push buttons and add them to a table view
260                     PushButton buttonWithSimpleTooltip = new PushButton();
261                     buttonWithSimpleTooltip.LabelText = "Tooltip with text only";
262                     buttonWithSimpleTooltip.UnselectedColor = new Vector4(0.5f, 0.5f, 0.7f, 1.0f);
263                     buttonWithSimpleTooltip.SelectedColor = new Vector4(0.7f, 0.5f, 0.7f, 1.0f);
264                     buttonWithSimpleTooltip.WidthResizePolicy = "FILL_TO_PARENT";
265                     tableView.AddChild(buttonWithSimpleTooltip, new TableView.CellPosition(0, 0));
266
267                     PushButton buttonWithIconTooltip = new PushButton();
268                     buttonWithIconTooltip.LabelText = "Tooltip with Text and Icon";
269                     buttonWithIconTooltip.WidthResizePolicy = "FILL_TO_PARENT";
270                     buttonWithIconTooltip.UnselectedColor = new Vector4(0.5f, 0.5f, 0.7f, 1.0f);
271                     buttonWithIconTooltip.SelectedColor = new Vector4(0.7f, 0.5f, 0.7f, 1.0f);
272
273                     tableView.AddChild(buttonWithIconTooltip, new TableView.CellPosition(1, 0));
274
275                     // Add a simple text only tooltip to the first push button
276                     buttonWithSimpleTooltip.TooltipText = "Simple Tooltip";
277
278                     // Create a property map for a tooltip with one icon and one text
279                     Property.Array iconTooltipContent = new Property.Array();
280
281                     Property.Map iconVisual = new Property.Map();
282                     iconVisual.Add(Dali.Constants.Visual.Property.Type, new Property.Value((int)Dali.Constants.Visual.Type.Image))
283                       .Add(Dali.Constants.ImageVisualProperty.URL, new Property.Value("./images/star-highlight.png"));
284                     iconTooltipContent.Add(new Property.Value(iconVisual));
285
286                     Property.Map textVisual = new Property.Map();
287                     textVisual.Add(Dali.Constants.Visual.Property.Type, new Property.Value((int)Dali.Constants.Visual.Type.Text))
288                       .Add(Dali.Constants.TextVisualProperty.Text, new Property.Value("Tooltip with Icon"));
289                     iconTooltipContent.Add(new Property.Value(textVisual));
290
291                     Property.Map iconTooltip = new Property.Map();
292                     iconTooltip.Add(Dali.Constants.Tooltip.Property.Content, new Property.Value(iconTooltipContent))
293                       .Add(Dali.Constants.Tooltip.Property.Tail, new Property.Value(true));
294
295                     // Add the tooltip with icon and text to the second push button
296                     buttonWithIconTooltip.Tooltip = iconTooltip;
297
298                     _contentContainer.AddChild(tableView, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
299                 }
300                 if (item.name.CompareTo("Popup") == 0)
301                 {
302                     PushButton button = new PushButton();
303                     button.LabelText = "Popup";
304                     button.ParentOrigin = NDalic.ParentOriginCenter;
305                     button.AnchorPoint = NDalic.AnchorPointCenter;
306                     button.MaximumSize = new Vector2(90.0f, 50.0f);
307                     _popup = CreatePopup();
308                     _popup.SetTitle(CreateTitle("Popup"));
309
310                     TextLabel text = new TextLabel("This will erase the file permanently. Are you sure?");
311                     text.BackgroundColor = Color.White;
312                     text.MultiLine = true;
313                     text.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.WIDTH);
314                     text.SetResizePolicy(ResizePolicyType.DIMENSION_DEPENDENCY, DimensionType.HEIGHT);
315                     text.SetPadding(new PaddingType(10.0f, 10.0f, 20.0f, 0.0f));
316                     _popup.SetContent(text);
317                     _popup.SetKeyboardFocusable(true);
318                     _popup.SetDisplayState(Popup.DisplayStateType.HIDDEN);
319
320                     button.Clicked += (obj, ee) =>
321                     {
322                         _stage.Add(_popup);
323                         _popup.SetDisplayState(Popup.DisplayStateType.SHOWN);
324                         FocusManager.Instance.SetCurrentFocusActor((_popup.FindChildByName("Footer")).FindChildByName("OKButton"));
325                         return true;
326                     };
327                     _contentContainer.AddChild(button, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
328                 }
329                 if (item.name.CompareTo("Toast") == 0)
330                 {
331                     PushButton button = new PushButton();
332                     button.LabelText = "Toast";
333                     button.ParentOrigin = NDalic.ParentOriginCenter;
334                     button.AnchorPoint = NDalic.AnchorPointCenter;
335                     button.Clicked += (obj, ee) =>
336                     {
337                         TypeInfo typeInfo = new TypeInfo(TypeRegistry.Get().GetTypeInfo("PopupToast"));
338                         if (typeInfo)
339                         {
340                             BaseHandle baseHandle = typeInfo.CreateInstance();
341                             if (baseHandle)
342                             {
343                                 Popup toast = Popup.DownCast(baseHandle);
344                                 TextLabel text = new TextLabel("This is a Toast.\nIt will auto-hide itself");
345                                 text.TextColor = Color.White;
346                                 text.MultiLine = true;
347                                 text.HorizontalAlignment = "center";
348                                 toast.SetTitle(text);
349                                 _stage.Add(toast);
350                                 toast.SetDisplayState(Popup.DisplayStateType.SHOWN);
351                             }
352                         }
353                         return true;
354                     };
355                     _contentContainer.AddChild(button, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
356                 }
357                 if (item.name.CompareTo("ItemView") == 0)
358                 {
359
360                 }
361             }
362             else
363             {
364                 ImageView notSupportView = new ImageView("images/not_yet_sign.png");
365                 notSupportView.Size = new Vector3(_stage.GetSize().Width * 0.2f, _stage.GetSize().Height * 0.25f, 0.0f);
366                 notSupportView.SetKeyboardFocusable(true);
367
368                 _contentContainer.AddChild(notSupportView, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
369             }
370         }
371         Popup CreatePopup()
372         {
373             Popup confirmationPopup = new Popup();
374
375             Actor footer = new Actor();
376             footer.SetName("Footer");
377             footer.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.WIDTH);
378             footer.SetResizePolicy(ResizePolicyType.FIXED, DimensionType.HEIGHT);
379             footer.SetSize(0.0f, 80.0f);
380             footer.ParentOrigin = NDalic.ParentOriginCenter;
381             footer.AnchorPoint = NDalic.AnchorPointCenter;
382
383             PushButton okButton = CreateOKButton();
384             okButton.ParentOrigin = NDalic.ParentOriginCenter;
385             okButton.AnchorPoint = NDalic.AnchorPointCenter;
386             okButton.SetResizePolicy(ResizePolicyType.SIZE_FIXED_OFFSET_FROM_PARENT, DimensionType.ALL_DIMENSIONS);
387             okButton.SetSizeModeFactor(new Vector3(-20.0f, -20.0f, 0.0f));
388
389
390             PushButton cancelButton = CreateCancelButton();
391             cancelButton.ParentOrigin = NDalic.ParentOriginCenter;
392             cancelButton.AnchorPoint = NDalic.AnchorPointCenter;
393             cancelButton.SetResizePolicy(ResizePolicyType.SIZE_FIXED_OFFSET_FROM_PARENT, DimensionType.ALL_DIMENSIONS);
394             cancelButton.SetSizeModeFactor(new Vector3(-20.0f, -20.0f, 0.0f));
395
396
397             TableView controlLayout = new TableView(1, 2);
398             controlLayout.ParentOrigin = NDalic.ParentOriginCenter;
399             controlLayout.AnchorPoint = NDalic.AnchorPointCenter;
400             controlLayout.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.ALL_DIMENSIONS);
401             controlLayout.SetCellPadding(new Size2D(10, 10));
402             controlLayout.SetRelativeWidth(0, 0.5f);
403             controlLayout.SetRelativeWidth(1, 0.5f);
404             controlLayout.SetCellAlignment(new TableView.CellPosition(0, 0), HorizontalAlignmentType.CENTER, VerticalAlignmentType.CENTER);
405             controlLayout.SetCellAlignment(new TableView.CellPosition(0, 1), HorizontalAlignmentType.CENTER, VerticalAlignmentType.CENTER);
406             controlLayout.AddChild(okButton, new TableView.CellPosition(0, 0));
407             controlLayout.AddChild(cancelButton, new TableView.CellPosition(0, 1));
408
409
410             footer.Add(controlLayout);
411
412             confirmationPopup.SetFooter(footer);
413             return confirmationPopup;
414         }
415         Actor CreateTitle(string title)
416         {
417             TextLabel titleActor = new TextLabel(title);
418             titleActor.TextColor = Color.White;
419             titleActor.MultiLine = true;
420             titleActor.HorizontalAlignment = "center";
421
422             return titleActor;
423         }
424
425         PushButton CreateOKButton()
426         {
427             PushButton okayButton = new PushButton();
428             okayButton.SetName("OKButton");
429             okayButton.LabelText = "OK";
430             okayButton.SetKeyboardFocusable(true);
431             okayButton.Clicked += (obj, ee) =>
432             {
433                 _popup.SetDisplayState(Popup.DisplayStateType.HIDDEN);
434                 return true;
435             };
436             return okayButton;
437         }
438         PushButton CreateCancelButton()
439         {
440             PushButton cancelButton = new PushButton();
441             cancelButton.LabelText = "Cancel";
442             cancelButton.SetKeyboardFocusable(true);
443             cancelButton.Clicked += (obj, ee) =>
444             {
445                 _popup.SetDisplayState(Popup.DisplayStateType.HIDDEN);
446                 return true;
447             };
448             return cancelButton;
449         }
450
451         void OnProgressBarValueChanged(object source, ProgressBar.ValueChangedEventArgs e)
452         {
453             Property.Map labelVisual = new Property.Map();
454             labelVisual.Add(Dali.Constants.Visual.Property.Type, new Property.Value((int)Dali.Constants.Visual.Type.Text))
455               .Add(Dali.Constants.TextVisualProperty.Text, new Property.Value(Math.Round(e.ProgressBar.ProgressValue, 2) + " / " + Math.Round(e.ProgressBar.SecondaryProgressValue, 2)));
456             e.ProgressBar.LabelVisual = labelVisual;
457             return;
458         }
459
460         public void MainLoop()
461         {
462             _application.MainLoop();
463         }
464
465         /// <summary>
466         /// The main entry point for the application.
467         /// </summary>
468
469         [STAThread]
470         static void Main(string[] args)
471         {
472             Console.WriteLine("Hello Mono World");
473
474             Example example = new Example(Application.NewApplication("json/control-dashboard.json"));
475             example.MainLoop();
476         }
477     }
478 }
479