Fix BackgroundImage and BackgroudColor properties in C# View
[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 MyView : View
25     {
26         private string _myOwnName;
27         public int _myCurrentValue;
28
29         public MyView()
30         {
31             _myCurrentValue = 0;
32         }
33
34         public string MyOwnName
35         {
36             get
37             {
38                 return _myOwnName;
39             }
40             set
41             {
42               _myOwnName = value;
43             }
44         }
45     }
46
47     class MyButton : PushButton
48     {
49         private string _myOwnName;
50         public int _myCurrentValue;
51
52         public MyButton()
53         {
54             _myCurrentValue = 0;
55         }
56
57         public string MyOwnName
58         {
59             get
60             {
61                 return _myOwnName;
62             }
63             set
64             {
65               _myOwnName = value;
66             }
67         }
68     }
69
70     class MySpin : Spin
71     {
72         private string _myOwnName;
73         public int _myCurrentValue;
74
75         public MySpin()
76         {
77             _myCurrentValue = 0;
78         }
79
80         public string MyOwnName
81         {
82             get
83             {
84                 return _myOwnName;
85             }
86             set
87             {
88               _myOwnName = value;
89             }
90         }
91     }
92
93     class Example
94     {
95         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
96         delegate void CallbackDelegate(IntPtr appPtr); // void, void delgate
97
98         private Dali.Application _application;
99
100         public Example(Dali.Application application)
101         {
102             _application = application;
103             Console.WriteLine( "InitSignal connection count = " + _application.InitSignal().GetConnectionCount() );
104
105             _application.Initialized += Initialize;
106             Console.WriteLine( "InitSignal connection count = " + _application.InitSignal().GetConnectionCount() );
107         }
108
109         public void Initialize(object source, NUIApplicationInitEventArgs e)
110         {
111             OperatorTests();
112
113             CustomViewPropertyTest();
114
115             Handle handle = new Handle();
116             int myPropertyIndex = handle.RegisterProperty("myProperty", new Property.Value(10.0f), Property.AccessMode.READ_WRITE);
117             float myProperty = 0.0f;
118             handle.GetProperty(myPropertyIndex).Get(ref myProperty);
119             Console.WriteLine( "myProperty value: " + myProperty );
120
121             int myPropertyIndex2 = handle.RegisterProperty("myProperty2", new Property.Value(new Size(5.0f, 5.0f)), Property.AccessMode.READ_WRITE);
122             Size myProperty2 = new Size(0.0f, 0.0f);
123             handle.GetProperty(myPropertyIndex2).Get(myProperty2);
124             Console.WriteLine( "myProperty2 value: " + myProperty2.W + ", " + myProperty2.H );
125
126             Actor actor = new Actor();
127             actor.Size = new Position(200.0f, 200.0f, 0.0f);
128             actor.Name = "MyActor";
129             actor.Color = new Color(1.0f, 0.0f, 1.0f, 0.8f);
130             Console.WriteLine("Actor id: {0}", actor.GetId());
131             Console.WriteLine("Actor size: " + actor.Size.X + ", " + actor.Size.Y);
132             Console.WriteLine("Actor name: " + actor.Name);
133
134             Stage stage = Stage.GetCurrent();
135             stage.BackgroundColor = Color.White;
136             Size stageSize = stage.Size;
137             Console.WriteLine("Stage size: " + stageSize.W + ", " + stageSize.H);
138             stage.Add(actor);
139
140             TextLabel text = new TextLabel("Hello Mono World");
141             text.ParentOrigin = NDalic.ParentOriginCenter;
142             text.AnchorPoint = NDalic.AnchorPointCenter;
143             text.HorizontalAlignment = "CENTER";
144             stage.Add(text);
145
146             Console.WriteLine( "Text label text:  " + text.Text );
147
148             Console.WriteLine( "Text label point size:  " + text.PointSize );
149             text.PointSize = 32.0f;
150             Console.WriteLine( "Text label new point size:  " + text.PointSize );
151
152             RectanglePaddingClassTest();
153
154             Console.WriteLine( " *************************" );
155             Size Size = new Size(100, 50);
156             Console.WriteLine( "    Created " + Size );
157             Console.WriteLine( "    Size x =  " + Size.W + ", y = " + Size.H );
158             Size += new Size(20, 20);
159             Console.WriteLine( "    Size x =  " + Size.W + ", y = " + Size.H );
160             Size.W += 10;
161             Size.H += 10;
162             Console.WriteLine( "    Size width =  " + Size.W + ", height = " + Size.H );
163
164             Console.WriteLine( " *************************" );
165             Position Position = new Position(20, 100, 50);
166             Console.WriteLine( "    Created " + Position );
167             Console.WriteLine( "    Position x =  " + Position.X + ", y = " + Position.Y + ", z = " + Position.Z );
168             Position += new Position(20, 20, 20);
169             Console.WriteLine( "    Position x =  " + Position.X + ", y = " + Position.Y + ", z = " + Position.Z );
170             Position.X += 10;
171             Position.Y += 10;
172             Position.Z += 10;
173             Console.WriteLine( "    Position width =  " + Position.X + ", height = " + Position.Y + ", depth = " + Position.Z );
174             Position parentOrigin = new Dali.Position(NDalic.ParentOriginBottomRight);
175             Console.WriteLine( "    parentOrigin x =  " + parentOrigin.X + ", y = " + parentOrigin.Y + ", z = " + parentOrigin.Z );
176
177             Console.WriteLine( " *************************" );
178             Color color = new Color(20, 100, 50, 200);
179             Console.WriteLine( "    Created " + color );
180             Console.WriteLine( "    Color R =  " + color.R + ", G = " + color.G + ", B = " + color.B + ", A = " + color.A );
181             color += new Color(20, 20, 20, 20);
182             Console.WriteLine( "    Color R =  " + color.R + ", G = " + color.G + ", B = " + color.B + ", A = " + color.A );
183             color.R += 10;
184             color.G += 10;
185             color.B += 10;
186             color.A += 10;
187             Console.WriteLine( "    Color r =  " + color.R + ", g = " + color.G + ", b = " + color.B + ", a = " + color.A );
188
189             ViewDownCastTest();
190         }
191
192         public void RectanglePaddingClassTest()
193         {
194             using (Rectangle r1 = new Rectangle(2, 5, 20, 30))
195             {
196                 Console.WriteLine( "    Created " + r1 );
197                 Console.WriteLine( "    IsEmpty() =  " + r1.IsEmpty() );
198                 Console.WriteLine( "    Left =  " + r1.Left() );
199                 Console.WriteLine( "    Right =  " + r1.Right() );
200                 Console.WriteLine( "    Top  = " + r1.Top() );
201                 Console.WriteLine( "    Bottom  = " + r1.Bottom() );
202                 Console.WriteLine( "    Area  = " + r1.Area() );
203             }
204
205             Console.WriteLine( " *************************" );
206
207             using (Rectangle r2 = new Rectangle(2, 5, 20, 30))
208             {
209                 Console.WriteLine( "    Created " + r2 );
210                 r2.Set(1,1,40,40);
211                 Console.WriteLine( "    IsEmpty() =  " + r2.IsEmpty() );
212                 Console.WriteLine( "    Left =  " + r2.Left() );
213                 Console.WriteLine( "    Right =  " + r2.Right() );
214                 Console.WriteLine( "    Top  = " + r2.Top() );
215                 Console.WriteLine( "    Bottom  = " + r2.Bottom() );
216                 Console.WriteLine( "    Area  = " + r2.Area() );
217             }
218
219             Console.WriteLine( " *************************" );
220
221             Rectangle r3 = new Rectangle(10, 10, 20, 20);
222             Rectangle r4 = new Rectangle(10, 10, 20, 20);
223
224             if (r3 == r4)
225             {
226                 Console.WriteLine("r3 == r4");
227             }
228             else
229             {
230                 Console.WriteLine("r3 != r4");
231             }
232
233             r4 = new Rectangle(12, 10, 20, 20);
234
235             if (r3 == r4)
236             {
237                 Console.WriteLine("r3 == r4");
238             }
239             else
240             {
241                 Console.WriteLine("r3 != r4");
242             }
243
244             PaddingType p1 = new PaddingType(10.5f, 10.7f, 20.8f, 20.8f);
245             PaddingType p2 = new PaddingType(10.5f, 10.7f, 20.8f, 20.8f);
246
247             if (p1 == p2)
248             {
249                 Console.WriteLine("p1 == p2");
250             }
251             else
252             {
253                 Console.WriteLine("p1 != p2");
254             }
255
256             p2 = new PaddingType(12.0f, 10.7f, 20.2f, 20.0f);
257
258             if (p1 == p2)
259             {
260                 Console.WriteLine("p1 == p2");
261             }
262             else
263             {
264                 Console.WriteLine("p1 != p2");
265             }
266         }
267
268         public void OperatorTests()
269         {
270             Actor actor = new Actor();
271             Actor differentActor = new Actor();
272             Actor actorSame = actor;
273             Actor nullActor = null;
274
275             // test the true operator
276             if ( actor )
277             {
278                 Console.WriteLine ("BaseHandle Operator true (actor) : test passed ");
279             }
280             else
281             {
282                 Console.WriteLine ("BaseHandle Operator true (actor): test failed ");
283             }
284
285             Actor parent = actor.GetParent ();
286
287             if ( parent )
288             {
289                 Console.WriteLine ("Handle with Empty body  :failed ");
290             }
291             else
292             {
293                 Console.WriteLine ("Valid with Empty body  :passed ");
294             }
295
296             actor.Add( differentActor );
297
298             // here we test two different C# objects, which on the native side have the same body/ ref-object
299             if ( actor == differentActor.GetParent() )
300             {
301                 Console.WriteLine ("actor == differentActor.GetParent() :passed ");
302             }
303             else
304             {
305                 Console.WriteLine ("actor == differentActor.GetParent() :failed ");
306             }
307
308             if ( differentActor == differentActor.GetParent() )
309             {
310                 Console.WriteLine ("differentActor == differentActor.GetParent() :failed ");
311             }
312             else
313             {
314                 Console.WriteLine ("differentActor == differentActor.GetParent() :passed ");
315             }
316
317             if ( nullActor )
318             {
319                 Console.WriteLine ("BaseHandle Operator true (nullActor) : test failed ");
320             }
321             else
322             {
323                 Console.WriteLine ("BaseHandle Operator true (nullActor): test passed ");
324             }
325
326             // ! operator
327             if ( !actor )
328             {
329                 Console.WriteLine ("BaseHandle Operator !(actor) : test failed ");
330             }
331             else
332             {
333                 Console.WriteLine ("BaseHandle Operator !(actor): test passed ");
334             }
335
336             if ( !nullActor )
337             {
338                 Console.WriteLine ("BaseHandle Operator !(nullActor) : test passed ");
339             }
340             else
341             {
342                 Console.WriteLine ("BaseHandle Operator !(nullActor): test failed ");
343             }
344
345             // Note: operator false only used inside & operator
346             // test equality operator ==
347             if ( actor == actorSame )
348             {
349                 Console.WriteLine ("BaseHandle Operator  (actor == actorSame) : test passed");
350             }
351             else
352             {
353                 Console.WriteLine ("BaseHandle Operator  (actor == actorSame) : test failed");
354             }
355
356             if ( actor == differentActor )
357             {
358                 Console.WriteLine ("BaseHandle Operator (actor == differentActor) : test failed");
359             }
360             else
361             {
362                 Console.WriteLine ("BaseHandle Operator (actor == differentActor) : test passed");
363             }
364
365             if ( actor == nullActor )
366             {
367                 Console.WriteLine ("BaseHandle Operator (actor == nullActor) : test failed");
368             }
369             else
370             {
371                 Console.WriteLine ("BaseHandle Operator (actor == nullActor) : test passed");
372             }
373
374             if ( nullActor == nullActor )
375             {
376                 Console.WriteLine ("BaseHandle Operator (nullActor == nullActor) : test passed");
377             }
378             else
379             {
380                 Console.WriteLine ("BaseHandle Operator (nullActor == nullActor) : test failed");
381             }
382
383             // test || operator
384             if ( actor || actorSame )
385             {
386                 Console.WriteLine ("BaseHandle Operator (actor || actorSame) : test passed");
387             }
388             else
389             {
390                 Console.WriteLine ("BaseHandle Operator (actor || actorSame) : test failed");
391             }
392
393             if ( actor || nullActor )
394             {
395                 Console.WriteLine ("BaseHandle Operator (actor || nullActor) : test passed");
396             }
397             else
398             {
399                 Console.WriteLine ("BaseHandle Operator (actor || nullActor) : test failed");
400             }
401
402             if ( nullActor || nullActor )
403             {
404                 Console.WriteLine ("BaseHandle Operator (nullActor || nullActor) : test failed");
405             }
406             else
407             {
408                 Console.WriteLine ("BaseHandle Operator (nullActor || nullActor) : test passed");
409             }
410
411             // test && operator
412             if ( actor && actorSame )
413             {
414                 Console.WriteLine ("BaseHandle Operator (actor && actorSame) : test passed");
415             }
416             else
417             {
418                 Console.WriteLine ("BaseHandle Operator (actor && actorSame) : test failed");
419             }
420
421             if ( actor && nullActor )
422             {
423                 Console.WriteLine ("BaseHandle Operator (actor && nullActor) : test failed");
424             }
425             else
426             {
427                 Console.WriteLine ("BaseHandle Operator (actor && nullActor) : test passed");
428             }
429
430             if ( nullActor && nullActor )
431             {
432                 Console.WriteLine ("BaseHandle Operator (nullActor && nullActor) : test failed");
433             }
434             else
435             {
436                 Console.WriteLine ("BaseHandle Operator (nullActor && nullActor) : test passed");
437             }
438         }
439
440         public void CustomViewPropertyTest()
441         {
442             // Create a Spin control
443             Spin spin = new Spin();
444
445             // Background property
446             Property.Map background = new Property.Map();
447             background.Add( Dali.Constants.Visual.Property.Type, new Property.Value((int)Dali.Constants.Visual.Type.Color) )
448                       .Add( Dali.Constants.ColorVisualProperty.MixColor, new Property.Value(Color.Red) );
449             spin.Background = background;
450
451             background = spin.Background;
452             Vector4 backgroundColor = new Vector4();
453             background.Find(Dali.Constants.ColorVisualProperty.MixColor).Get(backgroundColor);
454             if( backgroundColor == Color.Red )
455             {
456                 Console.WriteLine ("Custom View Background property : test passed");
457             }
458             else
459             {
460                 Console.WriteLine ("Custom View Background property : test failed");
461             }
462
463             // BackgroundColor property
464             spin.BackgroundColor = Color.Yellow;
465             if(spin.BackgroundColor == Color.Yellow)
466             {
467                 Console.WriteLine ("Custom View BackgroundColor property : test passed");
468             }
469             else
470             {
471                 Console.WriteLine ("Custom View BackgroundColor property : test failed");
472             }
473
474             // BackgroundImage property
475             spin.BackgroundImage = "background-image.jpg";
476             if(spin.BackgroundImage == "background-image.jpg")
477             {
478                 Console.WriteLine ("Custom View BackgroundImage property : test passed");
479             }
480             else
481             {
482                 Console.WriteLine ("Custom View BackgroundImage property : test failed");
483             }
484
485             // StyleName property
486             spin.StyleName = "MyCustomStyle";
487             if(spin.StyleName == "MyCustomStyle")
488             {
489                 Console.WriteLine ("Custom View StyleName property : test passed");
490             }
491             else
492             {
493                 Console.WriteLine ("Custom View StyleName property : test failed");
494             }
495         }
496
497         public void ViewDownCastTest()
498         {
499           View container = new View();
500           container.Position = new Position(-800.0f, -800.0f, 0.0f);
501           Stage.GetCurrent().Add(container);
502
503           // Test downcast for native control
504           TextLabel myLabel = new TextLabel();
505           myLabel.Name = "MyLabelName";
506           myLabel.Text = "MyText";
507
508           Console.WriteLine("myLabel.Name = " + myLabel.Name + ", Text = " + myLabel.Text);
509
510           container.Add(myLabel);
511
512           Actor myLabelActor  = container.FindChildByName("MyLabelName");
513           if(myLabelActor)
514           {
515             TextLabel newLabel = View.DownCast<TextLabel>(myLabelActor);
516             if(newLabel)
517             {
518               Console.WriteLine("Downcast to TextLabel successful: newLabel Name = " + newLabel.Name + ", Text = " + newLabel.Text);
519             }
520             else
521             {
522               Console.WriteLine("Downcast to TextLabel failed!");
523             }
524           }
525
526           // Test downcast for class directly inherited from View
527           MyView myView = new MyView();
528           myView.Name = "MyViewName";
529           myView.MyOwnName = "MyOwnViewName";
530           myView._myCurrentValue = 5;
531
532           Console.WriteLine("myView.Name = " + myView.Name + ", MyOwnName = " + myView.MyOwnName + ", myCurrentValue = " + myView._myCurrentValue);
533
534           container.Add(myView);
535
536           Actor myViewActor  = container.FindChildByName("MyViewName");
537           if(myViewActor)
538           {
539             MyView newView = View.DownCast<MyView>(myViewActor);
540             if(newView)
541             {
542               Console.WriteLine("Downcast to MyView successful: newView Name = " + newView.Name + ", MyOwnName = " + newView.MyOwnName + ", myCurrentValue = " + newView._myCurrentValue);
543             }
544             else
545             {
546               Console.WriteLine("Downcast to MyView failed!");
547             }
548           }
549
550           // Test downcast for class directly inherited from native control
551           MyButton myButton = new MyButton();
552           myButton.Name = "MyButtonName";
553           myButton.MyOwnName = "MyOwnViewName";
554           myButton.LabelText = "MyLabelText";
555           myButton._myCurrentValue = 5;
556
557           Console.WriteLine("myButton.Name = " + myButton.Name + ", MyOwnName = " + myButton.MyOwnName + ", LabelText = " + myButton.LabelText + ", myCurrentValue = " + myButton._myCurrentValue);
558
559           container.Add(myButton);
560
561           Actor myButtonActor  = container.FindChildByName("MyButtonName");
562           if(myButtonActor)
563           {
564             MyButton newButton = View.DownCast<MyButton>(myButtonActor);
565             if(newButton)
566             {
567               Console.WriteLine("Downcast to MyButton successful: newButton Name = " + newButton.Name + ", MyOwnName = " + newButton.MyOwnName + ", LabelText = " + myButton.LabelText + ", myCurrentValue = " + newButton._myCurrentValue);
568             }
569             else
570             {
571               Console.WriteLine("Downcast to MyButton failed!");
572             }
573           }
574
575           // Test downcast for a CustomView
576           Spin spin = new Spin();
577           spin.Name = "SpinName";
578           spin.MaxValue = 8888;
579
580           Console.WriteLine("spin.Name = " + spin.Name + ", MaxValue = " + spin.MaxValue);
581
582           container.Add(spin);
583
584           Actor spinActor  = container.FindChildByName("SpinName");
585           if(spinActor)
586           {
587             Spin newSpin = View.DownCast<Spin>(spinActor);
588             if(newSpin)
589             {
590               Console.WriteLine("Downcast to Spin successful: newSpin Name = " + newSpin.Name + ", MaxValue = " + newSpin.MaxValue);
591             }
592             else
593             {
594               Console.WriteLine("Downcast to Spin failed!");
595             }
596           }
597
598           // Test downcast for class inherited from a CustomView
599           MySpin mySpin = new MySpin();
600           mySpin.Name = "MySpinName";
601           mySpin.MyOwnName = "MyOwnSpinName";
602           mySpin.MaxValue = 8888;
603           mySpin._myCurrentValue = 5;
604
605           Console.WriteLine("mySpin.Name = " + mySpin.Name + ", MyOwnName = " + mySpin.MyOwnName + ", MaxValue = " + mySpin.MaxValue + ", currentValue = " + mySpin._myCurrentValue);
606
607           container.Add(mySpin);
608
609           Actor mySpinActor  = container.FindChildByName("MySpinName");
610           if(mySpinActor)
611           {
612             MySpin newSpin = View.DownCast<MySpin>(mySpinActor);
613             if(newSpin)
614             {
615               Console.WriteLine("Downcast to MySpin successful: newSpin Name = " + newSpin.Name + ", MyOwnName = " + newSpin.MyOwnName + ", MaxValue = " + newSpin.MaxValue + ", currentValue = " + newSpin._myCurrentValue);
616             }
617             else
618             {
619               Console.WriteLine("Downcast to MySpin failed!");
620             }
621           }
622         }
623
624         public void MainLoop()
625         {
626             _application.MainLoop ();
627         }
628
629         /// <summary>
630         /// The main entry point for the application.
631         /// </summary>
632         [STAThread]
633         static void Main(string[] args)
634         {
635             Console.WriteLine ("Hello Mono World");
636
637             Example example = new Example(Application.NewApplication());
638             example.MainLoop ();
639         }
640     }
641 }