DALi C# binding - Write pure C# Color & Position classes and use typemaps to do the...
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / examples / dali-test.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 using Dali.CSharp;
22
23 namespace MyCSharpExample
24 {
25   class Example
26   {
27     [UnmanagedFunctionPointer(CallingConvention.StdCall)]
28     delegate void CallbackDelegate(IntPtr appPtr); // void, void delgate
29
30     private Dali.Application _application;
31
32     public Example(Dali.Application application)
33     {
34       Console.WriteLine( "DBG : Example Constructor.... " );
35       _application = application;
36       Console.WriteLine( "InitSignal connection count = " + _application.InitSignal().GetConnectionCount() );
37
38       _application.Initialized += Initialize;
39       Console.WriteLine( "InitSignal connection count = " + _application.InitSignal().GetConnectionCount() );
40     }
41
42     public void Initialize(object source, AUIApplicationInitEventArgs e)
43     {
44
45       Console.WriteLine( "DBG : Initializing.... " );
46       OperatorTests();
47
48       Handle handle = new Handle();
49       Console.WriteLine( "DBG : Initializing.... "+ handle );
50       int myPropertyIndex = handle.RegisterProperty("myProperty", new Property.Value(10.0f), Property.AccessMode.READ_WRITE);
51       float myProperty = 0.0f;
52       handle.GetProperty(myPropertyIndex).Get(ref myProperty);
53       Console.WriteLine( "myProperty value: " + myProperty );
54
55       Size s = new Size(5.0f, 5.0f);
56       Property.Value val = new Property.Value(s);
57       int myPropertyIndex2 = handle.RegisterProperty("myProperty2", val, Property.AccessMode.READ_WRITE);
58       Size myProperty2 = new Size(0.0f, 0.0f);
59       handle.GetProperty(myPropertyIndex2).Get(myProperty2);
60       Console.WriteLine( "myProperty2 value: " + myProperty2.W + ", " + myProperty2.H );
61
62       Actor actor = new Actor();
63       actor.Size = new Position(200.0f, 200.0f, 0.0f);
64       actor.Name = "MyActor";
65       actor.Color = new Color(Colors.Green);
66       Console.WriteLine("Actor id: {0}", actor.GetId());
67       Console.WriteLine( "DBG : Initializing.... "+ actor.Size.X );
68
69       Console.WriteLine("Actor size: " + actor.Size.X + ", " + actor.Size.Y);
70       Console.WriteLine("Actor name: " + actor.Name);
71
72       Stage stage = Stage.GetCurrent();
73       stage.BackgroundColor =  new Dali.CSharp.Color(Dali.CSharp.Colors.Blue) ;
74
75       Size stageSize = stage.Size;
76       Console.WriteLine("Stage size: " + stageSize.W + ", " + stageSize.H);
77       stage.Add(actor);
78
79       TextLabel text = new TextLabel("Hello Mono World");
80       text.ParentOrigin = new Position(NDalic.ParentOriginCenter);
81       text.AnchorPoint = new Position(NDalic.AnchorPointCenter);
82       text.HorizontalAlignment = "CENTER";
83       stage.Add(text);
84
85       Console.WriteLine( "Text label text:  " + text.Text );
86
87       Console.WriteLine( "Text label point size:  " + text.PointSize );
88       text.PointSize = 32.0f;
89       Console.WriteLine( "Text label new point size:  " + text.PointSize );
90
91       using (RectInteger ri = new RectInteger(02,05,20,30))
92       {
93         Console.WriteLine( "    Created " + ri );
94         Console.WriteLine( "    IsEmpty() =  " + ri.IsEmpty() );
95         Console.WriteLine( "    Left =  " + ri.Left() );
96         Console.WriteLine( "    Right =  " + ri.Right() );
97         Console.WriteLine( "    Top  = " + ri.Top() );
98         Console.WriteLine( "    Bottom  = " + ri.Bottom() );
99         Console.WriteLine( "    Area  = " + ri.Area() );
100       }
101       Console.WriteLine( " *************************" );
102       using (RectInteger ri2 = new RectInteger(02,05,20,30))
103       {
104         Console.WriteLine( "    Created " + ri2 );
105         ri2.Set(1,1,40,40);
106         Console.WriteLine( "    IsEmpty() =  " + ri2.IsEmpty() );
107         Console.WriteLine( "    Left =  " + ri2.Left() );
108         Console.WriteLine( "    Right =  " + ri2.Right() );
109         Console.WriteLine( "    Top  = " + ri2.Top() );
110         Console.WriteLine( "    Bottom  = " + ri2.Bottom() );
111         Console.WriteLine( "    Area  = " + ri2.Area() );
112       }
113       Console.WriteLine( " *************************" );
114       using (RectDouble rd = new RectDouble(02,05,20.5,30.5))
115       {
116         Console.WriteLine( "    Created " + rd );
117         Console.WriteLine( "    IsEmpty() =  " + rd.IsEmpty() );
118         Console.WriteLine( "    Left =  " + rd.Left() );
119         Console.WriteLine( "    Right =  " + rd.Right() );
120         Console.WriteLine( "    Top  = " + rd.Top() );
121         Console.WriteLine( "    Bottom  = " + rd.Bottom() );
122         Console.WriteLine( "    Area  = " + rd.Area() );
123       }
124       Console.WriteLine( " *************************" );
125       RectDouble rd2 = new RectDouble();
126       rd2.x = 10;
127       rd2.y = 10;
128       rd2.width = 20;
129       rd2.height = 20;
130       Console.WriteLine( "    Created " + rd2 );
131       Console.WriteLine( "    IsEmpty() =  " + rd2.IsEmpty() );
132       Console.WriteLine( "    Left =  " + rd2.Left() );
133       Console.WriteLine( "    Right =  " + rd2.Right() );
134       Console.WriteLine( "    Top  = " + rd2.Top() );
135       Console.WriteLine( "    Bottom  = " + rd2.Bottom() );
136       Console.WriteLine( "    Area  = " + rd2.Area() );
137
138       Console.WriteLine( " *************************" );
139       Size size = new Size(100, 50);
140       Console.WriteLine( "    Created " + size );
141       Console.WriteLine( "    Size width =  " + size.W + ", height = " + size.H );
142       size += new Size(20, 20);
143       Console.WriteLine( "    Size W =  " + size.W + ", H = " + size.H );
144       size.W += 10;
145       size.H += 10;
146       Console.WriteLine( "    Size width =  " + size.W + ", height = " + size.H );
147       size += new Size(15, 15);
148       Console.WriteLine( "    Size width =  " + size.W + ", height = " + size.H );
149
150       Console.WriteLine( " *************************" );
151       Dali.CSharp.Position position = new Dali.CSharp.Position(20, 100, 50);
152       Console.WriteLine( "    Created " + position );
153       Console.WriteLine( "    Position x =  " + position.X + ", y = " + position.Y + ", z = " + position.Z );
154       position += new Dali.CSharp.Position(20, 20, 20);
155       Console.WriteLine( "    Position x =  " + position.X + ", y = " + position.Y + ", z = " + position.Y );
156       position.X += 10;
157       position.Y += 10;
158       position.Z += 10;
159       Console.WriteLine( "    Position width =  " + position.X + ", height = " + position.Y + ", depth = " + position.Z );
160       Dali.CSharp.Position parentOrigin = new Dali.CSharp.Position(NDalic.ParentOriginBottomRight);
161       Console.WriteLine( "    parentOrigin x =  " + parentOrigin.X + ", y = " + parentOrigin.Y + ", z = " + parentOrigin.Z );
162
163       Console.WriteLine( " *************************" );
164       Dali.CSharp.Color color = new Dali.CSharp.Color(20, 100, 50, 200);
165       Console.WriteLine( "    Created " + color );
166       Console.WriteLine( "    Color x =  " + color.R + ", y = " + color.G + ", z = " + color.B + ", w = " + color.A );
167       color += new Dali.CSharp.Color(20, 20, 20, 20);
168       Console.WriteLine( "    Color x =  " + color.R + ", y = " + color.G + ", z = " + color.B + ", w = " + color.A );
169       color.R += 10;
170       color.G += 10;
171       color.B += 10;
172       color.A += 10;
173       Console.WriteLine( "    Color x =  " + color.R + ", y = " + color.G + ", z = " + color.B + ", w = " + color.A );
174     }
175
176
177   public void OperatorTests()
178   {
179     Actor actor = new Actor();
180     Actor differentActor = new Actor();
181     Actor actorSame = actor;
182     Actor nullActor = null;
183
184       // test the true operator
185     if ( actor )
186     {
187       Console.WriteLine ("BaseHandle Operator true (actor) : test passed ");
188     }
189     else
190     {
191       Console.WriteLine ("BaseHandle Operator true (actor): test failed ");
192     }
193
194     Actor parent = actor.GetParent ();
195
196     if ( parent )
197     {
198       Console.WriteLine ("Handle with Empty body  :failed ");
199     }
200     else
201     {
202       Console.WriteLine ("Valid with Empty body  :passed ");
203     }
204
205     actor.Add( differentActor );
206     // here we test two different C# objects, which on the native side have the same body/ ref-object
207     if ( actor == differentActor.GetParent() )
208     {
209        Console.WriteLine ("actor == differentActor.GetParent() :passed ");
210     }
211     else
212     {
213       Console.WriteLine ("actor == differentActor.GetParent() :failed ");
214     }
215
216     if ( differentActor == differentActor.GetParent() )
217     {
218        Console.WriteLine ("differentActor == differentActor.GetParent() :failed ");
219     }
220     else
221     {
222       Console.WriteLine ("differentActor == differentActor.GetParent() :passed ");
223     }
224
225
226     if ( nullActor )
227     {
228       Console.WriteLine ("BaseHandle Operator true (nullActor) : test failed ");
229     }
230     else
231     {
232       Console.WriteLine ("BaseHandle Operator true (nullActor): test passed ");
233     }
234
235     // ! operator
236     if ( !actor )
237     {
238       Console.WriteLine ("BaseHandle Operator !(actor) : test failed ");
239     }
240     else
241     {
242       Console.WriteLine ("BaseHandle Operator !(actor): test passed ");
243     }
244
245     if ( !nullActor )
246     {
247       Console.WriteLine ("BaseHandle Operator !(nullActor) : test passed ");
248     }
249     else
250     {
251       Console.WriteLine ("BaseHandle Operator !(nullActor): test failed ");
252     }
253
254     // Note: operator false only used inside & operator
255     // test equality operator ==
256     if ( actor == actorSame )
257     {
258       Console.WriteLine ("BaseHandle Operator  (actor == actorSame) : test passed");
259     }
260     else
261     {
262       Console.WriteLine ("BaseHandle Operator  (actor == actorSame) : test failed");
263     }
264
265     if ( actor == differentActor )
266     {
267       Console.WriteLine ("BaseHandle Operator (actor == differentActor) : test failed");
268     }
269     else
270     {
271       Console.WriteLine ("BaseHandle Operator (actor == differentActor) : test passed");
272     }
273
274     if ( actor == nullActor )
275     {
276       Console.WriteLine ("BaseHandle Operator (actor == nullActor) : test failed");
277     }
278     else
279     {
280       Console.WriteLine ("BaseHandle Operator (actor == nullActor) : test passed");
281     }
282
283     if ( nullActor == nullActor )
284     {
285       Console.WriteLine ("BaseHandle Operator (nullActor == nullActor) : test passed");
286     }
287     else
288     {
289       Console.WriteLine ("BaseHandle Operator (nullActor == nullActor) : test failed");
290     }
291
292     // test || operator
293     if ( actor || actorSame )
294     {
295       Console.WriteLine ("BaseHandle Operator (actor || actorSame) : test passed");
296     }
297     else
298     {
299       Console.WriteLine ("BaseHandle Operator (actor || actorSame) : test failed");
300     }
301
302     if ( actor || nullActor )
303     {
304       Console.WriteLine ("BaseHandle Operator (actor || nullActor) : test passed");
305     }
306     else
307     {
308       Console.WriteLine ("BaseHandle Operator (actor || nullActor) : test failed");
309     }
310
311     if ( nullActor || nullActor )
312     {
313       Console.WriteLine ("BaseHandle Operator (nullActor || nullActor) : test failed");
314     }
315     else
316     {
317       Console.WriteLine ("BaseHandle Operator (nullActor || nullActor) : test passed");
318     }
319
320
321     // test && operator
322     if ( actor && actorSame )
323     {
324       Console.WriteLine ("BaseHandle Operator (actor && actorSame) : test passed");
325     }
326     else
327     {
328       Console.WriteLine ("BaseHandle Operator (actor && actorSame) : test failed");
329     }
330
331     if ( actor && nullActor )
332     {
333       Console.WriteLine ("BaseHandle Operator (actor && nullActor) : test failed");
334     }
335     else
336     {
337       Console.WriteLine ("BaseHandle Operator (actor && nullActor) : test passed");
338     }
339
340     if ( nullActor && nullActor )
341     {
342       Console.WriteLine ("BaseHandle Operator (nullActor && nullActor) : test failed");
343     }
344     else
345     {
346       Console.WriteLine ("BaseHandle Operator (nullActor && nullActor) : test passed");
347     }
348
349   }
350
351     public void MainLoop()
352     {
353       _application.MainLoop ();
354     }
355
356     /// <summary>
357     /// The main entry point for the application.
358     /// </summary>
359     [STAThread]
360       static void Main(string[] args)
361       {
362         Console.WriteLine ("Hello Mono World");
363
364         Example example = new Example(Application.NewApplication());
365         example.MainLoop ();
366       }
367   }
368 }