bdd3d5d1b530d6fd5568e1c02f92f2bec42389ef
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / examples / control-dashboard.cs
1 /*
2  * Copyright (c) 2016 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", false),
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", false),     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, AUIApplicationInitEventArgs 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
158                 }
159                 if (item.name.CompareTo("InputField") == 0)
160                 {
161
162                 }
163                 if (item.name.CompareTo("AnimateGif") == 0)
164                 {
165
166                 }
167                 if (item.name.CompareTo("Loading") == 0)
168                 {
169
170                 }
171                 if (item.name.CompareTo("ProgressBar") == 0)
172                 {
173
174                 }
175                 if (item.name.CompareTo("ScrollBar") == 0)
176                 {
177
178                 }
179                 if (item.name.CompareTo("CheckBox") == 0)
180                 {
181                     CheckBoxButton checkBoxButton = new CheckBoxButton();
182                     checkBoxButton.LabelText = "Yes";
183
184                     _contentContainer.AddChild(checkBoxButton, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
185                 }
186                 if (item.name.CompareTo("RadioButton") == 0)
187                 {
188                     TableView tableView = new TableView(2, 1);
189                     tableView.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.WIDTH);
190                     tableView.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.HEIGHT);
191
192                     RadioButton rButton = new RadioButton();
193                     rButton.LabelText = "Yes";
194                     rButton.Selected = true;
195                     tableView.AddChild(rButton, new TableView.CellPosition(0, 0));
196
197                     rButton = new RadioButton();
198                     rButton.LabelText = "No";
199
200                     tableView.AddChild(rButton, new TableView.CellPosition(1, 0));
201
202                     _contentContainer.AddChild(tableView, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
203                 }
204                 if (item.name.CompareTo("Tooltip") == 0)
205                 {
206
207                 }
208                 if (item.name.CompareTo("Popup") == 0)
209                 {
210                     PushButton button = new PushButton();
211                     button.LabelText = "Popup";
212                     button.ParentOrigin = NDalic.ParentOriginCenter;
213                     button.AnchorPoint = NDalic.AnchorPointCenter;
214                     button.MaximumSize = new Vector2(90.0f,50.0f);
215                     _popup = CreatePopup();
216                     _popup.SetTitle(CreateTitle("Popup"));
217
218                     TextLabel text = new TextLabel("This will erase the file permanently. Are you sure?");
219                     text.BackgroundColor = Color.White;
220                     text.MultiLine = true;
221                     text.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.WIDTH);
222                     text.SetResizePolicy(ResizePolicyType.DIMENSION_DEPENDENCY, DimensionType.HEIGHT);
223                     text.SetPadding(new RectFloat(10.0f, 10.0f, 20.0f, 0.0f));
224                     _popup.SetContent(text);
225                     _popup.SetKeyboardFocusable(true);
226                     _popup.SetDisplayState(Popup.DisplayStateType.HIDDEN);
227
228                     button.Clicked += (obj, ee) =>
229                     {
230                         _stage.Add(_popup);
231                         _popup.SetDisplayState(Popup.DisplayStateType.SHOWN);
232                         FocusManager.Instance.SetCurrentFocusActor((_popup.FindChildByName( "Footer" )).FindChildByName("OKButton"));
233                         return true;
234                     };
235                     _contentContainer.AddChild(button, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
236                 }
237                 if (item.name.CompareTo("Toast") == 0)
238                 {
239                     PushButton button = new PushButton();
240                     button.LabelText = "Toast";
241                     button.ParentOrigin = NDalic.ParentOriginCenter;
242                     button.AnchorPoint = NDalic.AnchorPointCenter;
243                     button.Clicked += (obj, ee) =>
244                     {
245                         TypeInfo typeInfo = new TypeInfo(TypeRegistry.Get().GetTypeInfo( "PopupToast" ));
246                         if( typeInfo )
247                         {
248                             BaseHandle baseHandle = typeInfo.CreateInstance();
249                             if( baseHandle )
250                             {
251                                 Popup toast = Popup.DownCast( baseHandle );
252                                 TextLabel text = new TextLabel("This is a Toast.\nIt will auto-hide itself");
253                                 text.TextColor = Color.White;
254                                 text.MultiLine = true;
255                                 text.HorizontalAlignment = "center";
256                                 toast.SetTitle( text);
257                                 _stage.Add(toast);
258                                 toast.SetDisplayState( Popup.DisplayStateType.SHOWN);
259                             }
260                         }
261                         return true;
262                     };
263                     _contentContainer.AddChild(button, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
264                 }
265                 if (item.name.CompareTo("ItemView") == 0)
266                 {
267
268                 }
269             }
270             else
271             {
272                 ImageView notSupportView = new ImageView("images/not_yet_sign.png");
273                 notSupportView.Size = new Vector3(_stage.GetSize().width * 0.2f, _stage.GetSize().height * 0.25f, 0.0f);
274                 notSupportView.SetKeyboardFocusable(true);
275                 _contentContainer.AddChild(notSupportView, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
276             }
277         }
278         Popup CreatePopup()
279         {
280             Popup confirmationPopup = new Popup();
281
282             Actor footer = new Actor();
283             footer.SetName("Footer");
284             footer.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.WIDTH);
285             footer.SetResizePolicy(ResizePolicyType.FIXED, DimensionType.HEIGHT);
286             footer.SetSize(0.0f, 80.0f);
287             footer.ParentOrigin = NDalic.ParentOriginCenter;
288             footer.AnchorPoint = NDalic.AnchorPointCenter;
289
290             PushButton okButton = CreateOKButton();
291             okButton.ParentOrigin = NDalic.ParentOriginCenter;
292             okButton.AnchorPoint = NDalic.AnchorPointCenter;
293             okButton.SetResizePolicy(ResizePolicyType.SIZE_FIXED_OFFSET_FROM_PARENT, DimensionType.ALL_DIMENSIONS);
294             okButton.SetSizeModeFactor(new Vector3(-20.0f, -20.0f, 0.0f));
295
296             PushButton cancelButton = CreateCancelButton();
297             cancelButton.ParentOrigin = NDalic.ParentOriginCenter;
298             cancelButton.AnchorPoint = NDalic.AnchorPointCenter;
299             cancelButton.SetResizePolicy(ResizePolicyType.SIZE_FIXED_OFFSET_FROM_PARENT, DimensionType.ALL_DIMENSIONS);
300             cancelButton.SetSizeModeFactor(new Vector3(-20.0f, -20.0f, 0.0f));
301
302             TableView controlLayout = new TableView(1, 2);
303             controlLayout.ParentOrigin = NDalic.ParentOriginCenter;
304             controlLayout.AnchorPoint = NDalic.AnchorPointCenter;
305             controlLayout.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.ALL_DIMENSIONS);
306             controlLayout.SetCellPadding(new Size(10.0f, 10.0f));
307             controlLayout.SetRelativeWidth(0, 0.5f);
308             controlLayout.SetRelativeWidth(1, 0.5f);
309             controlLayout.SetCellAlignment(new TableView.CellPosition(0, 0), HorizontalAlignmentType.CENTER, VerticalAlignmentType.CENTER);
310             controlLayout.SetCellAlignment(new TableView.CellPosition(0, 1), HorizontalAlignmentType.CENTER, VerticalAlignmentType.CENTER);
311             controlLayout.AddChild(okButton, new TableView.CellPosition(0, 0));
312             controlLayout.AddChild(cancelButton, new TableView.CellPosition(0, 1));
313
314             footer.Add(controlLayout);
315
316             confirmationPopup.SetFooter(footer);
317             return confirmationPopup;
318         }
319         Actor CreateTitle(string title)
320         {
321             TextLabel titleActor = new TextLabel(title);
322             titleActor.TextColor = Color.White;
323             titleActor.MultiLine = true;
324             titleActor.HorizontalAlignment = "center";
325             return titleActor;
326         }
327
328         PushButton CreateOKButton()
329         {
330             PushButton okayButton = new PushButton();
331             okayButton.SetName("OKButton");
332             okayButton.LabelText = "OK";
333             okayButton.SetKeyboardFocusable(true);
334             okayButton.Clicked += (obj, ee) =>
335             {
336                 _popup.SetDisplayState(Popup.DisplayStateType.HIDDEN);
337                 return true;
338             };
339             return okayButton;
340         }
341         PushButton CreateCancelButton()
342         {
343             PushButton cancelButton = new PushButton();
344             cancelButton.LabelText = "Cancel";
345             cancelButton.SetKeyboardFocusable(true);
346             cancelButton.Clicked += (obj, ee) =>
347             {
348                 _popup.SetDisplayState(Popup.DisplayStateType.HIDDEN);
349                 return true;
350             };
351             return cancelButton;
352         }
353
354         public void MainLoop()
355         {
356             _application.MainLoop();
357         }
358
359         /// <summary>
360         /// The main entry point for the application.
361         /// </summary>
362
363         [STAThread]
364         static void Main(string[] args)
365         {
366             Console.WriteLine("Hello Mono World");
367
368             Example example = new Example(Application.NewApplication());
369             example.MainLoop();
370         }
371     }
372 }