2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.Runtime.InteropServices;
22 namespace MyCSharpExample
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
32 public bool isImplemented;
34 public Item(String name, bool isImplemented)
37 this.isImplemented = isImplemented;
41 private Dali.Application _application;
42 private TableView _contentContainer;
46 private ProgressBar _progressBar;
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)
57 public Example(Dali.Application application)
59 _application = application;
60 _application.Initialized += OnInitialize;
63 public void OnInitialize(object source, NUIApplicationInitEventArgs e)
65 Console.WriteLine("Customized Application Initialize event handler");
66 _stage = Stage.GetCurrent();
67 _stage.BackgroundColor = Color.White;
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;
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);
102 FocusManager.Instance.PreFocusChange += OnPreFocusChange;
105 // Callback for KeyboardFocusManager
106 private Actor OnPreFocusChange(object source, FocusManager.PreFocusChangeEventArgs e)
108 if (!e.Proposed && !e.Current)
110 e.Proposed = _contentContainer.GetChildAt(1);
115 private void CreateContent()
117 for (int i = 0; i < mViewList.Length; i++)
119 CreateItem(mViewList[i], i);
123 private void CreateItem(Item item, int idx)
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));
133 // If item is implemented in public, attach it on stage
134 if (item.isImplemented)
136 if (item.name.CompareTo("PushButton") == 0)
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) =>
146 e.Button.LabelText = "Click Me";
147 e.Button.UnselectedColor = new Vector4(0.0f,0.0f,1.0f,1.0f);
151 _contentContainer.AddChild(pushButton, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
153 if (item.name.CompareTo("DropDown") == 0)
157 if (item.name.CompareTo("Toggle") == 0)
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;
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;
172 toggleButton.WidthResizePolicy = "FILL_TO_PARENT";
173 toggleButton.HeightResizePolicy = "FILL_TO_PARENT";
174 toggleButton.Clicked += (obj, e) =>
176 Console.WriteLine("Toggle button state changed.");
180 _contentContainer.AddChild(toggleButton, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
182 if (item.name.CompareTo("InputField") == 0)
186 if (item.name.CompareTo("AnimateGif") == 0)
190 if (item.name.CompareTo("Loading") == 0)
194 if (item.name.CompareTo("ProgressBar") == 0)
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 );
201 _progressBar.ValueChanged += OnProgressBarValueChanged;
203 _timer = new Timer( 100 );
204 _timer.Tick += ( obj, e ) =>
206 float progress = (float)Math.Round( _progressBar.ProgressValue , 2 );
208 if( progress == 1.0f )
210 _progressBar.ProgressValue = 0.0f;
211 _progressBar.SecondaryProgressValue = 0.01f;
215 _progressBar.ProgressValue = progress + 0.01f;
216 _progressBar.SecondaryProgressValue = progress + 0.21f;
222 _contentContainer.AddChild(_progressBar, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
224 if (item.name.CompareTo("ScrollBar") == 0)
228 if (item.name.CompareTo("CheckBox") == 0)
230 CheckBoxButton checkBoxButton = new CheckBoxButton();
231 checkBoxButton.LabelText = "Yes";
233 _contentContainer.AddChild(checkBoxButton, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
235 if (item.name.CompareTo("RadioButton") == 0)
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);
241 RadioButton rButton = new RadioButton();
242 rButton.LabelText = "Yes";
243 rButton.Selected = true;
244 tableView.AddChild(rButton, new TableView.CellPosition(0, 0));
246 rButton = new RadioButton();
247 rButton.LabelText = "No";
249 tableView.AddChild(rButton, new TableView.CellPosition(1, 0));
251 _contentContainer.AddChild(tableView, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
253 if (item.name.CompareTo("Tooltip") == 0)
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);
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));
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));
274 // Add a simple text only tooltip to the first push button
275 buttonWithSimpleTooltip.TooltipText = "Simple Tooltip";
277 // Create a property map for a tooltip with one icon and one text
278 Property.Array iconTooltipContent = new Property.Array();
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));
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));
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) );
294 // Add the tooltip with icon and text to the second push button
295 buttonWithIconTooltip.Tooltip = iconTooltip;
297 _contentContainer.AddChild(tableView, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
299 if (item.name.CompareTo("Popup") == 0)
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"));
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);
319 button.Clicked += (obj, ee) =>
322 _popup.SetDisplayState(Popup.DisplayStateType.SHOWN);
323 FocusManager.Instance.SetCurrentFocusActor((_popup.FindChildByName( "Footer" )).FindChildByName("OKButton"));
326 _contentContainer.AddChild(button, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
328 if (item.name.CompareTo("Toast") == 0)
330 PushButton button = new PushButton();
331 button.LabelText = "Toast";
332 button.ParentOrigin = NDalic.ParentOriginCenter;
333 button.AnchorPoint = NDalic.AnchorPointCenter;
334 button.Clicked += (obj, ee) =>
336 TypeInfo typeInfo = new TypeInfo(TypeRegistry.Get().GetTypeInfo( "PopupToast" ));
339 BaseHandle baseHandle = typeInfo.CreateInstance();
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);
349 toast.SetDisplayState( Popup.DisplayStateType.SHOWN);
354 _contentContainer.AddChild(button, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
356 if (item.name.CompareTo("ItemView") == 0)
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));
371 Popup confirmationPopup = new Popup();
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;
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));
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));
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));
405 footer.Add(controlLayout);
407 confirmationPopup.SetFooter(footer);
408 return confirmationPopup;
410 Actor CreateTitle(string title)
412 TextLabel titleActor = new TextLabel(title);
413 titleActor.TextColor = Color.White;
414 titleActor.MultiLine = true;
415 titleActor.HorizontalAlignment = "center";
419 PushButton CreateOKButton()
421 PushButton okayButton = new PushButton();
422 okayButton.SetName("OKButton");
423 okayButton.LabelText = "OK";
424 okayButton.SetKeyboardFocusable(true);
425 okayButton.Clicked += (obj, ee) =>
427 _popup.SetDisplayState(Popup.DisplayStateType.HIDDEN);
432 PushButton CreateCancelButton()
434 PushButton cancelButton = new PushButton();
435 cancelButton.LabelText = "Cancel";
436 cancelButton.SetKeyboardFocusable(true);
437 cancelButton.Clicked += (obj, ee) =>
439 _popup.SetDisplayState(Popup.DisplayStateType.HIDDEN);
445 void OnProgressBarValueChanged( object source, ProgressBar.ValueChangedEventArgs e )
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;
454 public void MainLoop()
456 _application.MainLoop();
460 /// The main entry point for the application.
464 static void Main(string[] args)
466 Console.WriteLine("Hello Mono World");
468 Example example = new Example(Application.NewApplication("json/control-dashboard.json"));