Merge "Add Visual descriptions to generate doxygen page" into devel/master
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / examples / dali-test.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         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
27         delegate void CallbackDelegate(IntPtr appPtr); // void, void delgate
28
29         private Dali.Application _application;
30
31         public Example(Dali.Application application)
32         {
33             _application = application;
34             Console.WriteLine( "InitSignal connection count = " + _application.InitSignal().GetConnectionCount() );
35
36             _application.Initialized += Initialize;
37             Console.WriteLine( "InitSignal connection count = " + _application.InitSignal().GetConnectionCount() );
38         }
39
40         public void Initialize(object source, NUIApplicationInitEventArgs e)
41         {
42             OperatorTests();
43
44             CustomViewPropertyTest();
45
46             Handle handle = new Handle();
47             int myPropertyIndex = handle.RegisterProperty("myProperty", new Property.Value(10.0f), Property.AccessMode.READ_WRITE);
48             float myProperty = 0.0f;
49             handle.GetProperty(myPropertyIndex).Get(ref myProperty);
50             Console.WriteLine( "myProperty value: " + myProperty );
51
52             int myPropertyIndex2 = handle.RegisterProperty("myProperty2", new Property.Value(new Size(5.0f, 5.0f)), Property.AccessMode.READ_WRITE);
53             Size myProperty2 = new Size(0.0f, 0.0f);
54             handle.GetProperty(myPropertyIndex2).Get(myProperty2);
55             Console.WriteLine( "myProperty2 value: " + myProperty2.W + ", " + myProperty2.H );
56
57             Actor actor = new Actor();
58             actor.Size = new Position(200.0f, 200.0f, 0.0f);
59             actor.Name = "MyActor";
60             actor.Color = new Color(1.0f, 0.0f, 1.0f, 0.8f);
61             Console.WriteLine("Actor id: {0}", actor.GetId());
62             Console.WriteLine("Actor size: " + actor.Size.X + ", " + actor.Size.Y);
63             Console.WriteLine("Actor name: " + actor.Name);
64
65             Stage stage = Stage.GetCurrent();
66             stage.BackgroundColor = Color.White;
67             Size stageSize = stage.Size;
68             Console.WriteLine("Stage size: " + stageSize.W + ", " + stageSize.H);
69             stage.Add(actor);
70
71             TextLabel text = new TextLabel("Hello Mono World");
72             text.ParentOrigin = NDalic.ParentOriginCenter;
73             text.AnchorPoint = NDalic.AnchorPointCenter;
74             text.HorizontalAlignment = "CENTER";
75             stage.Add(text);
76
77             Console.WriteLine( "Text label text:  " + text.Text );
78
79             Console.WriteLine( "Text label point size:  " + text.PointSize );
80             text.PointSize = 32.0f;
81             Console.WriteLine( "Text label new point size:  " + text.PointSize );
82
83             RectanglePaddingClassTest();
84
85             Console.WriteLine( " *************************" );
86             Size Size = new Size(100, 50);
87             Console.WriteLine( "    Created " + Size );
88             Console.WriteLine( "    Size x =  " + Size.W + ", y = " + Size.H );
89             Size += new Size(20, 20);
90             Console.WriteLine( "    Size x =  " + Size.W + ", y = " + Size.H );
91             Size.W += 10;
92             Size.H += 10;
93             Console.WriteLine( "    Size width =  " + Size.W + ", height = " + Size.H );
94
95             Console.WriteLine( " *************************" );
96             Position Position = new Position(20, 100, 50);
97             Console.WriteLine( "    Created " + Position );
98             Console.WriteLine( "    Position x =  " + Position.X + ", y = " + Position.Y + ", z = " + Position.Z );
99             Position += new Position(20, 20, 20);
100             Console.WriteLine( "    Position x =  " + Position.X + ", y = " + Position.Y + ", z = " + Position.Z );
101             Position.X += 10;
102             Position.Y += 10;
103             Position.Z += 10;
104             Console.WriteLine( "    Position width =  " + Position.X + ", height = " + Position.Y + ", depth = " + Position.Z );
105             Position parentOrigin = new Dali.Position(NDalic.ParentOriginBottomRight);
106             Console.WriteLine( "    parentOrigin x =  " + parentOrigin.X + ", y = " + parentOrigin.Y + ", z = " + parentOrigin.Z );
107
108             Console.WriteLine( " *************************" );
109             Color color = new Color(20, 100, 50, 200);
110             Console.WriteLine( "    Created " + color );
111             Console.WriteLine( "    Color R =  " + color.R + ", G = " + color.G + ", B = " + color.B + ", A = " + color.A );
112             color += new Color(20, 20, 20, 20);
113             Console.WriteLine( "    Color R =  " + color.R + ", G = " + color.G + ", B = " + color.B + ", A = " + color.A );
114             color.R += 10;
115             color.G += 10;
116             color.B += 10;
117             color.A += 10;
118             Console.WriteLine( "    Color r =  " + color.R + ", g = " + color.G + ", b = " + color.B + ", a = " + color.A );
119         }
120
121         public void RectanglePaddingClassTest()
122         {
123             using (Rectangle r1 = new Rectangle(2, 5, 20, 30))
124             {
125                 Console.WriteLine( "    Created " + r1 );
126                 Console.WriteLine( "    IsEmpty() =  " + r1.IsEmpty() );
127                 Console.WriteLine( "    Left =  " + r1.Left() );
128                 Console.WriteLine( "    Right =  " + r1.Right() );
129                 Console.WriteLine( "    Top  = " + r1.Top() );
130                 Console.WriteLine( "    Bottom  = " + r1.Bottom() );
131                 Console.WriteLine( "    Area  = " + r1.Area() );
132             }
133
134             Console.WriteLine( " *************************" );
135
136             using (Rectangle r2 = new Rectangle(2, 5, 20, 30))
137             {
138                 Console.WriteLine( "    Created " + r2 );
139                 r2.Set(1,1,40,40);
140                 Console.WriteLine( "    IsEmpty() =  " + r2.IsEmpty() );
141                 Console.WriteLine( "    Left =  " + r2.Left() );
142                 Console.WriteLine( "    Right =  " + r2.Right() );
143                 Console.WriteLine( "    Top  = " + r2.Top() );
144                 Console.WriteLine( "    Bottom  = " + r2.Bottom() );
145                 Console.WriteLine( "    Area  = " + r2.Area() );
146             }
147
148             Console.WriteLine( " *************************" );
149
150             Rectangle r3 = new Rectangle(10, 10, 20, 20);
151             Rectangle r4 = new Rectangle(10, 10, 20, 20);
152
153             if (r3 == r4)
154             {
155                 Console.WriteLine("r3 == r4");
156             }
157             else
158             {
159                 Console.WriteLine("r3 != r4");
160             }
161
162             r4 = new Rectangle(12, 10, 20, 20);
163
164             if (r3 == r4)
165             {
166                 Console.WriteLine("r3 == r4");
167             }
168             else
169             {
170                 Console.WriteLine("r3 != r4");
171             }
172
173             PaddingType p1 = new PaddingType(10.5f, 10.7f, 20.8f, 20.8f);
174             PaddingType p2 = new PaddingType(10.5f, 10.7f, 20.8f, 20.8f);
175
176             if (p1 == p2)
177             {
178                 Console.WriteLine("p1 == p2");
179             }
180             else
181             {
182                 Console.WriteLine("p1 != p2");
183             }
184
185             p2 = new PaddingType(12.0f, 10.7f, 20.2f, 20.0f);
186
187             if (p1 == p2)
188             {
189                 Console.WriteLine("p1 == p2");
190             }
191             else
192             {
193                 Console.WriteLine("p1 != p2");
194             }
195         }
196
197         public void OperatorTests()
198         {
199             Actor actor = new Actor();
200             Actor differentActor = new Actor();
201             Actor actorSame = actor;
202             Actor nullActor = null;
203
204             // test the true operator
205             if ( actor )
206             {
207                 Console.WriteLine ("BaseHandle Operator true (actor) : test passed ");
208             }
209             else
210             {
211                 Console.WriteLine ("BaseHandle Operator true (actor): test failed ");
212             }
213
214             Actor parent = actor.GetParent ();
215
216             if ( parent )
217             {
218                 Console.WriteLine ("Handle with Empty body  :failed ");
219             }
220             else
221             {
222                 Console.WriteLine ("Valid with Empty body  :passed ");
223             }
224
225             actor.Add( differentActor );
226
227             // here we test two different C# objects, which on the native side have the same body/ ref-object
228             if ( actor == differentActor.GetParent() )
229             {
230                 Console.WriteLine ("actor == differentActor.GetParent() :passed ");
231             }
232             else
233             {
234                 Console.WriteLine ("actor == differentActor.GetParent() :failed ");
235             }
236
237             if ( differentActor == differentActor.GetParent() )
238             {
239                 Console.WriteLine ("differentActor == differentActor.GetParent() :failed ");
240             }
241             else
242             {
243                 Console.WriteLine ("differentActor == differentActor.GetParent() :passed ");
244             }
245
246             if ( nullActor )
247             {
248                 Console.WriteLine ("BaseHandle Operator true (nullActor) : test failed ");
249             }
250             else
251             {
252                 Console.WriteLine ("BaseHandle Operator true (nullActor): test passed ");
253             }
254
255             // ! operator
256             if ( !actor )
257             {
258                 Console.WriteLine ("BaseHandle Operator !(actor) : test failed ");
259             }
260             else
261             {
262                 Console.WriteLine ("BaseHandle Operator !(actor): test passed ");
263             }
264
265             if ( !nullActor )
266             {
267                 Console.WriteLine ("BaseHandle Operator !(nullActor) : test passed ");
268             }
269             else
270             {
271                 Console.WriteLine ("BaseHandle Operator !(nullActor): test failed ");
272             }
273
274             // Note: operator false only used inside & operator
275             // test equality operator ==
276             if ( actor == actorSame )
277             {
278                 Console.WriteLine ("BaseHandle Operator  (actor == actorSame) : test passed");
279             }
280             else
281             {
282                 Console.WriteLine ("BaseHandle Operator  (actor == actorSame) : test failed");
283             }
284
285             if ( actor == differentActor )
286             {
287                 Console.WriteLine ("BaseHandle Operator (actor == differentActor) : test failed");
288             }
289             else
290             {
291                 Console.WriteLine ("BaseHandle Operator (actor == differentActor) : test passed");
292             }
293
294             if ( actor == nullActor )
295             {
296                 Console.WriteLine ("BaseHandle Operator (actor == nullActor) : test failed");
297             }
298             else
299             {
300                 Console.WriteLine ("BaseHandle Operator (actor == nullActor) : test passed");
301             }
302
303             if ( nullActor == nullActor )
304             {
305                 Console.WriteLine ("BaseHandle Operator (nullActor == nullActor) : test passed");
306             }
307             else
308             {
309                 Console.WriteLine ("BaseHandle Operator (nullActor == nullActor) : test failed");
310             }
311
312             // test || operator
313             if ( actor || actorSame )
314             {
315                 Console.WriteLine ("BaseHandle Operator (actor || actorSame) : test passed");
316             }
317             else
318             {
319                 Console.WriteLine ("BaseHandle Operator (actor || actorSame) : test failed");
320             }
321
322             if ( actor || nullActor )
323             {
324                 Console.WriteLine ("BaseHandle Operator (actor || nullActor) : test passed");
325             }
326             else
327             {
328                 Console.WriteLine ("BaseHandle Operator (actor || nullActor) : test failed");
329             }
330
331             if ( nullActor || nullActor )
332             {
333                 Console.WriteLine ("BaseHandle Operator (nullActor || nullActor) : test failed");
334             }
335             else
336             {
337                 Console.WriteLine ("BaseHandle Operator (nullActor || nullActor) : test passed");
338             }
339
340             // test && operator
341             if ( actor && actorSame )
342             {
343                 Console.WriteLine ("BaseHandle Operator (actor && actorSame) : test passed");
344             }
345             else
346             {
347                 Console.WriteLine ("BaseHandle Operator (actor && actorSame) : test failed");
348             }
349
350             if ( actor && nullActor )
351             {
352                 Console.WriteLine ("BaseHandle Operator (actor && nullActor) : test failed");
353             }
354             else
355             {
356                 Console.WriteLine ("BaseHandle Operator (actor && nullActor) : test passed");
357             }
358
359             if ( nullActor && nullActor )
360             {
361                 Console.WriteLine ("BaseHandle Operator (nullActor && nullActor) : test failed");
362             }
363             else
364             {
365                 Console.WriteLine ("BaseHandle Operator (nullActor && nullActor) : test passed");
366             }
367         }
368
369         public void CustomViewPropertyTest()
370         {
371             // Create a Spin control
372             Spin spin = new Spin();
373
374             // Background property
375             Property.Map background = new Property.Map();
376             background.Add( Dali.Constants.Visual.Property.Type, new Property.Value((int)Dali.Constants.Visual.Type.Color) )
377                 .Add( Dali.Constants.ColorVisualProperty.MixColor, new Property.Value(Color.Red) );
378             spin.Background = background;
379
380             background = spin.Background;
381             Vector4 backgroundColor = new Vector4();
382             background.Find(Dali.Constants.ColorVisualProperty.MixColor).Get(backgroundColor);
383             if( backgroundColor == Color.Red )
384             {
385                 Console.WriteLine ("Custom View Background property : test passed");
386             }
387             else
388             {
389                 Console.WriteLine ("Custom View Background property : test failed");
390             }
391
392             // BackgroundColor property
393             spin.BackgroundColor = Color.Yellow;
394             if(spin.BackgroundColor == Color.Yellow)
395             {
396                 Console.WriteLine ("Custom View BackgroundColor property : test passed");
397             }
398             else
399             {
400                 Console.WriteLine ("Custom View BackgroundColor property : test failed");
401             }
402
403             // StyleName property
404             spin.StyleName = "MyCustomStyle";
405             if(spin.StyleName == "MyCustomStyle")
406             {
407                 Console.WriteLine ("Custom View StyleName property : test passed");
408             }
409             else
410             {
411                 Console.WriteLine ("Custom View StyleName property : test failed");
412             }
413         }
414
415         public void MainLoop()
416         {
417             _application.MainLoop ();
418         }
419
420         /// <summary>
421         /// The main entry point for the application.
422         /// </summary>
423         [STAThread]
424         static void Main(string[] args)
425         {
426             Console.WriteLine ("Hello Mono World");
427
428             Example example = new Example(Application.NewApplication());
429             example.MainLoop ();
430         }
431     }
432 }