Merge "Prepare for Tizen 4.0 Build" into devel/master
[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 Stage _stage;
44     private Popup _popup;
45
46     // List of items
47     private Item[] mViewList = {
48       new Item("PushButton", true),  new Item("DropDown", false),    new Item("Toggle", true),
49       new Item("InputField", false),  new Item("AnimateGif", false),  new Item("Loading", false),
50       new Item("ProgressBar", false), new Item("CheckBox", false),    new Item("RadioButton", true),
51       new Item("Tooltip", true),     new Item("Popup", true),       new Item("Toast", true),
52       new Item("ItemView", false),    new Item("CheckBox", true)
53     };
54
55     public Example(Dali.Application application)
56     {
57       _application = application;
58       _application.Initialized += OnInitialize;
59     }
60
61     public void OnInitialize(object source, NUIApplicationInitEventArgs e)
62     {
63       Console.WriteLine("Customized Application Initialize event handler");
64       _stage = Stage.GetCurrent();
65       _stage.BackgroundColor = Color.White;
66
67       // Top label
68       TextLabel topLabel = new TextLabel();
69       topLabel.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.WIDTH);
70       topLabel.SetResizePolicy(ResizePolicyType.SIZE_RELATIVE_TO_PARENT, DimensionType.HEIGHT);
71       topLabel.AnchorPoint = NDalic.AnchorPointTopCenter;
72       topLabel.ParentOrigin = NDalic.ParentOriginTopCenter;
73       topLabel.SetSizeModeFactor(new Vector3( 0.0f, 0.1f, 0.0f ) );
74       topLabel.BackgroundColor = new Color(43.0f / 255.0f, 145.0f / 255.0f, 175.0f / 255.0f, 1.0f);
75       topLabel.TextColor = NDalic.WHITE;
76       topLabel.Text = " DALi Views";
77       topLabel.HorizontalAlignment = "BEGIN";
78       topLabel.VerticalAlignment = "CENTER";
79       topLabel.PointSize = 42.0f;
80       _stage.Add(topLabel);
81
82       // Grid container to contain items. Use tableView because FlexContainer support focus navigation just two direction ( up/down or left/right )
83       _contentContainer = new TableView(6, 5);
84       _contentContainer.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.WIDTH);
85       _contentContainer.SetResizePolicy(ResizePolicyType.SIZE_RELATIVE_TO_PARENT, DimensionType.HEIGHT);
86       _contentContainer.SetSizeModeFactor(new Vector3( 0.0f, 0.9f, 0.0f ) );
87       _contentContainer.AnchorPoint = NDalic.AnchorPointBottomCenter;
88       _contentContainer.ParentOrigin = NDalic.ParentOriginBottomCenter;
89       _contentContainer.SetRelativeHeight(0, 0.07f);
90       _contentContainer.SetRelativeHeight(1, 0.26f);
91       _contentContainer.SetRelativeHeight(2, 0.07f);
92       _contentContainer.SetRelativeHeight(3, 0.26f);
93       _contentContainer.SetRelativeHeight(4, 0.07f);
94       _contentContainer.SetRelativeHeight(5, 0.26f);
95       _contentContainer.SetKeyboardFocusable(true);
96       _stage.Add(_contentContainer);
97
98       CreateContent();
99
100       FocusManager.Instance.PreFocusChange += OnPreFocusChange;
101     }
102
103     // Callback for KeyboardFocusManager
104     private Actor OnPreFocusChange(object source, FocusManager.PreFocusChangeEventArgs e)
105     {
106       if (!e.Proposed && !e.Current)
107       {
108         e.Proposed = _contentContainer.GetChildAt(1);
109       }
110       return e.Proposed;
111     }
112
113     private void CreateContent()
114     {
115       for (int i = 0; i < mViewList.Length; i++)
116       {
117         CreateItem(mViewList[i], i);
118       }
119     }
120
121     private void CreateItem(Item item, int idx)
122     {
123       // Make label for item
124       TextLabel itemLabel = new TextLabel("    " + item.name);
125       itemLabel.Size = new Vector3(_stage.GetSize().Width * 0.2f, _stage.GetSize().Height * 0.05f, 0.0f);
126       itemLabel.HorizontalAlignment = "BEGIN";
127       itemLabel.VerticalAlignment = "BOTTOM";
128       itemLabel.PointSize = 18.0f;
129       _contentContainer.AddChild(itemLabel, new TableView.CellPosition(((uint)idx / 5) * 2, (uint)idx % 5));
130
131       // If item is implemented in public, attach it on stage
132       if (item.isImplemented)
133       {
134         if (item.name.CompareTo("PushButton") == 0)
135         {
136           PushButton pushButton = new PushButton();
137           pushButton.LabelText = "Push Button";
138           pushButton.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.WIDTH);
139           pushButton.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.HEIGHT);
140           pushButton.UnselectedColor = new Vector4(1.0f,0.0f,0.0f,1.0f);
141           pushButton.SelectedColor = new Vector4(0.0f,1.0f,0.0f,1.0f);
142           pushButton.Clicked += (obj, e) =>
143           {
144             e.Button.LabelText = "Click Me";
145             e.Button.UnselectedColor = new Vector4(0.0f,0.0f,1.0f,1.0f);
146             return true;
147           };
148
149           _contentContainer.AddChild(pushButton, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
150         }
151         if (item.name.CompareTo("DropDown") == 0)
152         {
153
154         }
155         if (item.name.CompareTo("Toggle") == 0)
156         {
157           ToggleButton toggleButton = new ToggleButton();
158           Dali.Property.Array array = new Dali.Property.Array();
159           array.Add( new Dali.Property.Value("./images/star-highlight.png") );
160           array.Add( new Dali.Property.Value("./images/star-mod.png") );
161           array.Add( new Dali.Property.Value("./images/star-dim.png") );
162           toggleButton.StateVisuals = array;
163
164           Dali.Property.Array tooltips = new Dali.Property.Array();
165           tooltips.Add( new Dali.Property.Value("State A") );
166           tooltips.Add( new Dali.Property.Value("State B") );
167           tooltips.Add( new Dali.Property.Value("State C") );
168           toggleButton.Tooltips = tooltips;
169
170           toggleButton.WidthResizePolicy  = "FILL_TO_PARENT";
171           toggleButton.HeightResizePolicy = "FILL_TO_PARENT";
172           toggleButton.Clicked += (obj, e) =>
173           {
174             Console.WriteLine("Toggle button state changed.");
175             return true;
176           };
177
178           _contentContainer.AddChild(toggleButton, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
179         }
180         if (item.name.CompareTo("InputField") == 0)
181         {
182
183         }
184         if (item.name.CompareTo("AnimateGif") == 0)
185         {
186
187         }
188         if (item.name.CompareTo("Loading") == 0)
189         {
190
191         }
192         if (item.name.CompareTo("ProgressBar") == 0)
193         {
194
195         }
196         if (item.name.CompareTo("ScrollBar") == 0)
197         {
198
199         }
200         if (item.name.CompareTo("CheckBox") == 0)
201         {
202           CheckBoxButton checkBoxButton = new CheckBoxButton();
203           checkBoxButton.LabelText = "Yes";
204
205           _contentContainer.AddChild(checkBoxButton, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
206         }
207         if (item.name.CompareTo("RadioButton") == 0)
208         {
209           TableView tableView = new TableView(2, 1);
210           tableView.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.WIDTH);
211           tableView.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.HEIGHT);
212
213           RadioButton rButton = new RadioButton();
214           rButton.LabelText = "Yes";
215           rButton.Selected = true;
216           tableView.AddChild(rButton, new TableView.CellPosition(0, 0));
217
218           rButton = new RadioButton();
219           rButton.LabelText = "No";
220
221           tableView.AddChild(rButton, new TableView.CellPosition(1, 0));
222
223           _contentContainer.AddChild(tableView, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
224         }
225         if (item.name.CompareTo("Tooltip") == 0)
226         {
227           TableView tableView = new TableView(2, 1);
228           tableView.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.WIDTH);
229           tableView.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.HEIGHT);
230
231           // Create two push buttons and add them to a table view
232           PushButton buttonWithSimpleTooltip = new PushButton();
233           buttonWithSimpleTooltip.LabelText = "Tooltip with text only";
234           buttonWithSimpleTooltip.UnselectedColor = new Vector4(0.5f,0.5f,0.7f,1.0f);
235           buttonWithSimpleTooltip.SelectedColor = new Vector4(0.7f,0.5f,0.7f,1.0f);
236           buttonWithSimpleTooltip.WidthResizePolicy = "FILL_TO_PARENT";
237           tableView.AddChild(buttonWithSimpleTooltip, new TableView.CellPosition(0, 0));
238
239           PushButton buttonWithIconTooltip = new PushButton();
240           buttonWithIconTooltip.LabelText = "Tooltip with Text and Icon";
241           buttonWithIconTooltip.WidthResizePolicy = "FILL_TO_PARENT";
242           buttonWithIconTooltip.UnselectedColor = new Vector4(0.5f,0.5f,0.7f,1.0f);
243           buttonWithIconTooltip.SelectedColor = new Vector4(0.7f,0.5f,0.7f,1.0f);
244           tableView.AddChild(buttonWithIconTooltip, new TableView.CellPosition(1, 0));
245
246           // Add a simple text only tooltip to the first push button
247           buttonWithSimpleTooltip.TooltipText = "Simple Tooltip";
248
249           // Create a property map for a tooltip with one icon and one text
250           Property.Array iconTooltipContent = new Property.Array();
251
252           Property.Map iconVisual = new Property.Map();
253           iconVisual.Add( Dali.Constants.Visual.Property.Type, new Property.Value((int)Dali.Constants.Visual.Type.Image) )
254             .Add( Dali.Constants.ImageVisualProperty.URL, new Property.Value("./images/star-highlight.png") );
255           iconTooltipContent.Add(new Property.Value(iconVisual));
256
257           Property.Map textVisual = new Property.Map();
258           textVisual.Add( Dali.Constants.Visual.Property.Type, new Property.Value((int)Dali.Constants.Visual.Type.Text) )
259             .Add( Dali.Constants.TextVisualProperty.Text, new Property.Value("Tooltip with Icon") );
260           iconTooltipContent.Add(new Property.Value(textVisual));
261
262           Property.Map iconTooltip = new Property.Map();
263           iconTooltip.Add( Dali.Constants.Tooltip.Property.Content, new Property.Value(iconTooltipContent) )
264             .Add( Dali.Constants.Tooltip.Property.Tail, new Property.Value(true) );
265
266           // Add the tooltip with icon and text to the second push button
267           buttonWithIconTooltip.Tooltip = iconTooltip;
268
269           _contentContainer.AddChild(tableView, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
270         }
271         if (item.name.CompareTo("Popup") == 0)
272         {
273           PushButton button = new PushButton();
274           button.LabelText = "Popup";
275           button.ParentOrigin = NDalic.ParentOriginCenter;
276           button.AnchorPoint = NDalic.AnchorPointCenter;
277           button.MaximumSize = new Vector2(90.0f,50.0f);
278           _popup = CreatePopup();
279           _popup.SetTitle(CreateTitle("Popup"));
280
281           TextLabel text = new TextLabel("This will erase the file permanently. Are you sure?");
282           text.BackgroundColor = Color.White;
283           text.MultiLine = true;
284           text.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.WIDTH);
285           text.SetResizePolicy(ResizePolicyType.DIMENSION_DEPENDENCY, DimensionType.HEIGHT);
286           text.SetPadding(new PaddingType(10.0f, 10.0f, 20.0f, 0.0f));
287           _popup.SetContent(text);
288           _popup.SetKeyboardFocusable(true);
289           _popup.SetDisplayState(Popup.DisplayStateType.HIDDEN);
290
291           button.Clicked += (obj, ee) =>
292           {
293             _stage.Add(_popup);
294             _popup.SetDisplayState(Popup.DisplayStateType.SHOWN);
295             FocusManager.Instance.SetCurrentFocusActor((_popup.FindChildByName( "Footer" )).FindChildByName("OKButton"));
296             return true;
297           };
298           _contentContainer.AddChild(button, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
299         }
300         if (item.name.CompareTo("Toast") == 0)
301         {
302           PushButton button = new PushButton();
303           button.LabelText = "Toast";
304           button.ParentOrigin = NDalic.ParentOriginCenter;
305           button.AnchorPoint = NDalic.AnchorPointCenter;
306           button.Clicked += (obj, ee) =>
307           {
308             TypeInfo typeInfo = new TypeInfo(TypeRegistry.Get().GetTypeInfo( "PopupToast" ));
309             if( typeInfo )
310             {
311               BaseHandle baseHandle = typeInfo.CreateInstance();
312               if( baseHandle )
313               {
314                 Popup toast = Popup.DownCast( baseHandle );
315                 TextLabel text = new TextLabel("This is a Toast.\nIt will auto-hide itself");
316                 text.TextColor = Color.White;
317                 text.MultiLine = true;
318                 text.HorizontalAlignment = "center";
319                 toast.SetTitle( text);
320                 _stage.Add(toast);
321                 toast.SetDisplayState( Popup.DisplayStateType.SHOWN);
322               }
323             }
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("ItemView") == 0)
329         {
330
331         }
332       }
333       else
334       {
335         ImageView notSupportView = new ImageView("images/not_yet_sign.png");
336         notSupportView.Size = new Vector3(_stage.GetSize().Width * 0.2f, _stage.GetSize().Height * 0.25f, 0.0f);
337         notSupportView.SetKeyboardFocusable(true);
338         _contentContainer.AddChild(notSupportView, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
339       }
340     }
341     Popup CreatePopup()
342     {
343       Popup confirmationPopup = new Popup();
344
345       Actor footer = new Actor();
346       footer.SetName("Footer");
347       footer.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.WIDTH);
348       footer.SetResizePolicy(ResizePolicyType.FIXED, DimensionType.HEIGHT);
349       footer.SetSize(0.0f, 80.0f);
350       footer.ParentOrigin = NDalic.ParentOriginCenter;
351       footer.AnchorPoint = NDalic.AnchorPointCenter;
352
353       PushButton okButton = CreateOKButton();
354       okButton.ParentOrigin = NDalic.ParentOriginCenter;
355       okButton.AnchorPoint = NDalic.AnchorPointCenter;
356       okButton.SetResizePolicy(ResizePolicyType.SIZE_FIXED_OFFSET_FROM_PARENT, DimensionType.ALL_DIMENSIONS);
357       okButton.SetSizeModeFactor(new Vector3(-20.0f, -20.0f, 0.0f));
358
359       PushButton cancelButton = CreateCancelButton();
360       cancelButton.ParentOrigin = NDalic.ParentOriginCenter;
361       cancelButton.AnchorPoint = NDalic.AnchorPointCenter;
362       cancelButton.SetResizePolicy(ResizePolicyType.SIZE_FIXED_OFFSET_FROM_PARENT, DimensionType.ALL_DIMENSIONS);
363       cancelButton.SetSizeModeFactor(new Vector3(-20.0f, -20.0f, 0.0f));
364
365       TableView controlLayout = new TableView(1, 2);
366       controlLayout.ParentOrigin = NDalic.ParentOriginCenter;
367       controlLayout.AnchorPoint = NDalic.AnchorPointCenter;
368       controlLayout.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.ALL_DIMENSIONS);
369       controlLayout.SetCellPadding(new Size(10.0f, 10.0f));
370       controlLayout.SetRelativeWidth(0, 0.5f);
371       controlLayout.SetRelativeWidth(1, 0.5f);
372       controlLayout.SetCellAlignment(new TableView.CellPosition(0, 0), HorizontalAlignmentType.CENTER, VerticalAlignmentType.CENTER);
373       controlLayout.SetCellAlignment(new TableView.CellPosition(0, 1), HorizontalAlignmentType.CENTER, VerticalAlignmentType.CENTER);
374       controlLayout.AddChild(okButton, new TableView.CellPosition(0, 0));
375       controlLayout.AddChild(cancelButton, new TableView.CellPosition(0, 1));
376
377       footer.Add(controlLayout);
378
379       confirmationPopup.SetFooter(footer);
380       return confirmationPopup;
381     }
382     Actor CreateTitle(string title)
383     {
384       TextLabel titleActor = new TextLabel(title);
385       titleActor.TextColor = Color.White;
386       titleActor.MultiLine = true;
387       titleActor.HorizontalAlignment = "center";
388       return titleActor;
389     }
390
391     PushButton CreateOKButton()
392     {
393       PushButton okayButton = new PushButton();
394       okayButton.SetName("OKButton");
395       okayButton.LabelText = "OK";
396       okayButton.SetKeyboardFocusable(true);
397       okayButton.Clicked += (obj, ee) =>
398       {
399         _popup.SetDisplayState(Popup.DisplayStateType.HIDDEN);
400         return true;
401       };
402       return okayButton;
403     }
404     PushButton CreateCancelButton()
405     {
406       PushButton cancelButton = new PushButton();
407       cancelButton.LabelText = "Cancel";
408       cancelButton.SetKeyboardFocusable(true);
409       cancelButton.Clicked += (obj, ee) =>
410       {
411         _popup.SetDisplayState(Popup.DisplayStateType.HIDDEN);
412         return true;
413       };
414       return cancelButton;
415     }
416
417     public void MainLoop()
418     {
419       _application.MainLoop();
420     }
421
422     /// <summary>
423     /// The main entry point for the application.
424     /// </summary>
425
426     [STAThread]
427       static void Main(string[] args)
428       {
429         Console.WriteLine("Hello Mono World");
430
431         Example example = new Example(Application.NewApplication("json/control-dashboard.json"));
432         example.MainLoop();
433       }
434   }
435 }