DALi C# binding
[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
44             CallbackDelegate initializeCallback = new CallbackDelegate( Initialize );
45             _application.InitSignal().Connect( initializeCallback );
46         }
47
48         private void Initialize(IntPtr appPtr)
49         {
50             Stage stage = Stage.GetCurrent();
51             stage.SetBackgroundColor( NDalic.WHITE );
52
53             // Connect the signal callback for stage touched signal
54             TouchCallbackDelegate stageTouchedCallback = new TouchCallbackDelegate( OnStageTouched );
55             stage.TouchSignal().Connect( stageTouchedCallback );
56
57             // Add a _text label to the stage
58             _text = new TextLabel("Hello Mono World");
59             _text.ParentOrigin = NDalic.ParentOriginCenter;
60             _text.AnchorPoint = NDalic.AnchorPointCenter;
61             _text.HorizontalAlignment = "CENTER";
62             _text.PointSize = 32.0f;
63
64             stage.Add(_text);
65         }
66
67         // Callback for _animation finished signal handling
68         private void AnimationFinished(IntPtr data)
69         {
70             Animation _animation = Animation.GetAnimationFromPtr( data );
71             Console.WriteLine("Animation finished: duration = " + _animation.GetDuration());
72         }
73
74         // Callback for stage touched signal handling
75         private void OnStageTouched(IntPtr data)
76         {
77             TouchData touchData = TouchData.GetTouchDataFromPtr( data );
78
79             // Only animate the _text label when touch down happens
80             if( touchData.GetState(0) == PointStateType.DOWN )
81             {
82                 // Create a new _animation
83                 if( _animation )
84                 {
85                     _animation.Reset();
86                 }
87
88                 _animation = new Animation(1.0f); // 1 second of duration
89
90                 _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));
91                 _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));
92
93                 // Connect the signal callback for animaiton finished signal
94                 AnimationCallbackDelegate animFinishedDelegate = new AnimationCallbackDelegate( AnimationFinished );
95                 _animation.FinishedSignal().Connect( animFinishedDelegate );
96
97                 // Play the _animation
98                 _animation.Play();
99             }
100         }
101
102         public void MainLoop()
103         {
104             _application.MainLoop ();
105         }
106
107         /// <summary>
108         /// The main entry point for the application.
109         /// </summary>
110
111         [STAThread]
112         static void Main(string[] args)
113         {
114             Console.WriteLine ("Hello Mono World");
115
116             Example example = new Example(Application.NewApplication());
117             example.MainLoop ();
118         }
119     }
120 }