DALi C# binding - EventHandler Support
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / examples / hello-world.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         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
27         delegate void CallbackDelegate(IntPtr data);
28
29         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
30         delegate void TouchCallbackDelegate(IntPtr data);
31
32         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
33         delegate void AnimationCallbackDelegate(IntPtr data);
34
35         private Dali.Application _application;
36
37         private Animation _animation;
38         private TextLabel _text;
39
40         public Example(Dali.Application application)
41         {
42             _application = application;
43             _application.Initialized += new Dali.AUIApplicationInitEventHandler(Initialize);
44         }
45
46         public void Initialize(object source, AUIApplicationInitEventArgs e)
47         {
48             Console.WriteLine("Customized Application Initialize event handler");
49             Stage stage = Stage.GetCurrent();
50             stage.SetBackgroundColor( NDalic.WHITE );
51
52             stage.Touched += new Dali.Stage.TouchEventHandler(OnStageTouched);
53
54             // Add a _text label to the stage
55             _text = new TextLabel("Hello Mono World");
56             _text.ParentOrigin = NDalic.ParentOriginCenter;
57             _text.AnchorPoint = NDalic.AnchorPointCenter;
58             _text.HorizontalAlignment = "CENTER";
59             _text.PointSize = 32.0f;
60
61             stage.Add(_text);
62         }
63
64         // Callback for _animation finished signal handling
65         public void AnimationFinished(object source, Animation.FinishedEventArgs e)
66         {
67             Console.WriteLine("Customized Animation Finished Event handler");
68             Console.WriteLine("Animation finished: duration = " + e.Animation.GetDuration());
69         }
70
71         // Callback for stage touched signal handling
72         public void OnStageTouched(object source, Stage.TouchEventArgs e)
73         {
74             //TouchData touchData = TouchData.GetTouchDataFromPtr( data );
75
76             // Only animate the _text label when touch down happens
77             if( e.TouchData.GetState(0) == PointStateType.DOWN )
78             {
79                 Console.WriteLine("Customized Stage Touch event handler");
80                 // Create a new _animation
81                 if( _animation )
82                 {
83                     _animation.Reset();
84                 }
85
86                 _animation = new Animation(1.0f); // 1 second of duration
87
88                 _animation.AnimateTo(new Property(_text, Actor.Property.ORIENTATION), new Property.Value(new Quaternion( new Radian( new Degree( 180.0f ) ), Vector3.XAXIS )), new AlphaFunction(AlphaFunction.BuiltinFunction.LINEAR), new TimePeriod(0.0f, 0.5f));
89                 _animation.AnimateTo(new Property(_text, Actor.Property.ORIENTATION), new Property.Value(new Quaternion( new Radian( new Degree( 0.0f ) ), Vector3.XAXIS )), new AlphaFunction(AlphaFunction.BuiltinFunction.LINEAR), new TimePeriod(0.5f, 0.5f));
90
91                 // Connect the signal callback for animaiton finished signal
92       _animation.Finished += new Dali.Animation.FinishedEventHandler(AnimationFinished);
93
94                 // Play the _animation
95                 _animation.Play();
96             }
97         }
98
99         public void MainLoop()
100         {
101             _application.MainLoop ();
102         }
103
104         /// <summary>
105         /// The main entry point for the application.
106         /// </summary>
107
108         [STAThread]
109         static void Main(string[] args)
110         {
111             Console.WriteLine ("Hello Mono World");
112
113             Example example = new Example(Application.NewApplication());
114             example.MainLoop ();
115         }
116     }
117 }