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