1 /** Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Runtime.InteropServices;
21 namespace MyCSharpExample
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
31 public bool isImplemented;
33 public Item(String name, bool isImplemented)
36 this.isImplemented = isImplemented;
40 private Dali.Application _application;
41 private TableView _contentContainer;
43 private Window _window;
45 private ProgressBar _progressBar;
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)
56 public Example(Dali.Application application)
58 _application = application;
59 _application.Initialized += OnInitialize;
62 public void OnInitialize(object source, NUIApplicationInitEventArgs e)
64 Console.WriteLine("Customized Application Initialize event handler");
65 _window = Window.Instance;
66 _window.BackgroundColor = Color.White;
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 _window.Add(topLabel);
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, _window.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 _window.Add(_contentContainer);
101 FocusManager.Instance.PreFocusChange += OnPreFocusChange;
104 // Callback for KeyboardFocusManager
105 private View OnPreFocusChange(object source, FocusManager.PreFocusChangeEventArgs e)
107 if (!e.Proposed && !e.Current)
109 e.Proposed = _contentContainer.GetChildAt(1);
114 private void CreateContent()
116 for (int i = 0; i < mViewList.Length; i++)
118 CreateItem(mViewList[i], i);
122 private void CreateItem(Item item, int idx)
124 // Make label for item
125 TextLabel itemLabel = new TextLabel(" " + item.name);
126 itemLabel.Size = new Vector3(_window.GetSize().Width * 0.2f, _window.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));
132 // If item is implemented in public, attach it on window
133 if (item.isImplemented)
135 if (item.name.CompareTo("PushButton") == 0)
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) =>
145 e.Button.LabelText = "Click Me";
146 e.Button.UnselectedColor = new Vector4(0.0f, 0.0f, 1.0f, 1.0f);
150 _contentContainer.AddChild(pushButton, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
152 if (item.name.CompareTo("DropDown") == 0)
156 if (item.name.CompareTo("Toggle") == 0)
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;
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;
171 toggleButton.WidthResizePolicy = "FILL_TO_PARENT";
172 toggleButton.HeightResizePolicy = "FILL_TO_PARENT";
173 toggleButton.Clicked += (obj, e) =>
175 Console.WriteLine("Toggle button state changed.");
179 _contentContainer.AddChild(toggleButton, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
181 if (item.name.CompareTo("InputField") == 0)
185 if (item.name.CompareTo("AnimateGif") == 0)
189 if (item.name.CompareTo("Loading") == 0)
193 if (item.name.CompareTo("ProgressBar") == 0)
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);
200 _progressBar.ValueChanged += OnProgressBarValueChanged;
202 _timer = new Timer(100);
203 _timer.Tick += (obj, e) =>
205 float progress = (float)Math.Round(_progressBar.ProgressValue, 2);
207 if (progress == 1.0f)
209 _progressBar.ProgressValue = 0.0f;
210 _progressBar.SecondaryProgressValue = 0.01f;
214 _progressBar.ProgressValue = progress + 0.01f;
215 _progressBar.SecondaryProgressValue = progress + 0.21f;
221 _contentContainer.AddChild(_progressBar, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
223 if (item.name.CompareTo("ScrollBar") == 0)
227 if (item.name.CompareTo("CheckBox") == 0)
229 CheckBoxButton checkBoxButton = new CheckBoxButton();
230 checkBoxButton.LabelText = "Yes";
232 _contentContainer.AddChild(checkBoxButton, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
234 if (item.name.CompareTo("RadioButton") == 0)
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);
240 RadioButton rButton = new RadioButton();
241 rButton.LabelText = "Yes";
242 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);
273 tableView.AddChild(buttonWithIconTooltip, new TableView.CellPosition(1, 0));
275 // Add a simple text only tooltip to the first push button
276 buttonWithSimpleTooltip.TooltipText = "Simple Tooltip";
278 // Create a property map for a tooltip with one icon and one text
279 Property.Array iconTooltipContent = new Property.Array();
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));
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));
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));
295 // Add the tooltip with icon and text to the second push button
296 buttonWithIconTooltip.Tooltip = iconTooltip;
298 _contentContainer.AddChild(tableView, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
300 if (item.name.CompareTo("Popup") == 0)
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"));
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);
320 button.Clicked += (obj, ee) =>
323 _popup.SetDisplayState(Popup.DisplayStateType.SHOWN);
324 FocusManager.Instance.SetCurrentFocusView((_popup.FindChildByName("Footer")).FindChildByName("OKButton"));
327 _contentContainer.AddChild(button, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
329 if (item.name.CompareTo("Toast") == 0)
331 PushButton button = new PushButton();
332 button.LabelText = "Toast";
333 button.ParentOrigin = NDalic.ParentOriginCenter;
334 button.AnchorPoint = NDalic.AnchorPointCenter;
335 button.Clicked += (obj, ee) =>
337 TypeInfo typeInfo = new TypeInfo(TypeRegistry.Get().GetTypeInfo("PopupToast"));
340 BaseHandle baseHandle = typeInfo.CreateInstance();
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);
350 toast.SetDisplayState(Popup.DisplayStateType.SHOWN);
355 _contentContainer.AddChild(button, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
357 if (item.name.CompareTo("ItemView") == 0)
364 ImageView notSupportView = new ImageView("images/not_yet_sign.png");
365 notSupportView.Size = new Vector3(_window.GetSize().Width * 0.2f, _window.GetSize().Height * 0.25f, 0.0f);
366 notSupportView.SetKeyboardFocusable(true);
368 _contentContainer.AddChild(notSupportView, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
373 Popup confirmationPopup = new Popup();
375 View footer = new View();
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;
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));
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));
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));
410 footer.Add(controlLayout);
412 confirmationPopup.SetFooter(footer);
413 return confirmationPopup;
415 View CreateTitle(string title)
417 TextLabel titleActor = new TextLabel(title);
418 titleActor.TextColor = Color.White;
419 titleActor.MultiLine = true;
420 titleActor.HorizontalAlignment = "center";
425 PushButton CreateOKButton()
427 PushButton okayButton = new PushButton();
428 okayButton.SetName("OKButton");
429 okayButton.LabelText = "OK";
430 okayButton.SetKeyboardFocusable(true);
431 okayButton.Clicked += (obj, ee) =>
433 _popup.SetDisplayState(Popup.DisplayStateType.HIDDEN);
438 PushButton CreateCancelButton()
440 PushButton cancelButton = new PushButton();
441 cancelButton.LabelText = "Cancel";
442 cancelButton.SetKeyboardFocusable(true);
443 cancelButton.Clicked += (obj, ee) =>
445 _popup.SetDisplayState(Popup.DisplayStateType.HIDDEN);
451 void OnProgressBarValueChanged(object source, ProgressBar.ValueChangedEventArgs e)
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;
460 public void MainLoop()
462 _application.MainLoop();
466 /// The main entry point for the application.
470 static void Main(string[] args)
472 Console.WriteLine("Hello Mono World");
474 Example example = new Example(Application.NewApplication("json/control-dashboard.json"));