Merge "Dali C#: Common Interface Define related changes" into devel/master
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / examples / user-alphafunction.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.Constants;
22
23 namespace MyCSharpExample
24 {
25   class Example
26   {
27     private Dali.Application _application;
28     private Animation _animation;
29     private TextLabel _text;
30     private View _view1, _view2, _view3;
31     private UserAlphaFunctionDelegate _user_alpha_func;
32     private int myCount;
33
34     public static void Log(string str)
35     {
36       Console.WriteLine("[DALI C# SAMPLE] " + str);
37     }
38
39     public Example(Dali.Application application)
40     {
41       _application = application;
42       _application.Initialized += Initialize;
43     }
44
45     // Declare user alpha function delegate
46     [UnmanagedFunctionPointer(CallingConvention.StdCall)]
47     delegate float UserAlphaFunctionDelegate(float progress);
48
49     public void Initialize(object source, NUIApplicationInitEventArgs e)
50     {
51       Log("Initialize() is called!");
52       Stage stage = Stage.GetCurrent();
53       stage.BackgroundColor = Color.White;
54       stage.TouchEvent += OnStageTouched;
55       stage.TouchEvent += OnStageTouched2;
56       //stage.EventProcessingFinished += OnEventProcessingFinished;
57       stage.WheelEvent += OnStageWheelEvent;
58
59       // Add a _text label to the stage
60       _text = new TextLabel("Hello Mono World");
61       _text.ParentOrigin = ParentOrigin.BottomCenter;
62       _text.AnchorPoint = AnchorPoint.BottomCenter;
63       _text.HorizontalAlignment = "CENTER";
64       _text.PointSize = 32.0f;
65       stage.Add(_text);
66
67       _view1 = new View();
68       _view1.Size = new Vector3(200.0f, 200.0f, 0.0f);
69       _view1.BackgroundColor = Color.Green;
70       _view1.ParentOrigin = ParentOrigin.Center;
71       _view1.AnchorPoint = AnchorPoint.Center;
72       _view1.SetResizePolicy(ResizePolicyType.FIXED, DimensionType.ALL_DIMENSIONS);
73       _view1.OnStageEvent += OnStage;
74       stage.Add(_view1);
75
76       _view2 = new View();
77       _view2.BackgroundColor = Color.Red;
78       _view2.Size = new Vector3(50.0f, 50.0f, 0.0f);
79       _view2.ParentOrigin = ParentOrigin.TopLeft;
80       _view2.AnchorPoint = AnchorPoint.TopLeft;
81       _view2.SetResizePolicy(ResizePolicyType.FIXED, DimensionType.ALL_DIMENSIONS);
82       _view1.Add(_view2);
83
84       _view3 = new View();
85       _view3.BackgroundColor = Color.Blue;
86       _view3.Size = new Vector3(50.0f, 50.0f, 0.0f);
87       _view3.ParentOrigin = ParentOrigin.TopLeft;
88       _view3.AnchorPoint = AnchorPoint.TopLeft;
89       _view3.SetResizePolicy(ResizePolicyType.FIXED, DimensionType.ALL_DIMENSIONS);
90       _view1.Add(_view3);
91
92       _user_alpha_func = new UserAlphaFunctionDelegate(body);
93
94       MyAnimating();
95     }
96
97     // User defines alpha function as custom alpha function
98     // Important Notification : when this custom alpha-function is implemented,
99     // the other function call nor other data excess is prevented.
100     // this method must be implemented to calculate the values of input and output purely.
101     // unless, this will cause application crash.
102     float body(float progress)
103     {
104       if (progress > 0.2f && progress< 0.7f)
105       {
106         return progress + 0.8f;
107       }
108       return progress;
109     }
110
111     // Callback for _animation finished signal handling
112     public void AnimationFinished(object sender, EventArgs e)
113     {
114         Log("AnimationFinished() is called!");
115         myCount = 0;
116     }
117
118     public void MyAnimating()
119     {
120       // Create a new _animation
121       if( _animation )
122       {
123         _animation.Clear();
124         _animation.Reset();
125       }
126
127       _animation = new Animation(10000); // 10000 milli-second of duration
128       _animation.StartTime = 5000;
129       _animation.EndTime = 10000;
130       _animation.TargetProperty = "Position";
131       _animation.AlphaFunction = new AlphaFunction(_user_alpha_func);
132       _animation.Destination = new Vector3(150.0f, 150.0f, 0.0f);
133       _animation.AnimateTo(_view2);
134       // Connect the signal callback for animaiton finished signal
135       _animation.Finished += AnimationFinished;
136       _animation.EndAction = Animation.EndActions.Discard;
137       // Play the _animation
138       _animation.Play();
139     }
140
141     // Callback for stage touched signal handling
142     public void OnStageTouched(object source, Stage.TouchEventArgs e)
143     {
144       // Only animate the _text label when touch down happens
145       if( e.TouchData.GetState(0) == PointStateType.DOWN )
146       {
147         Log("OnStageTouched() is called! PointStateType.DOWN came!");
148         myCount++;
149         if( myCount > 1 )
150         {
151           _animation.Stop();
152           Log("_animation.Stop() is called!");
153         }
154       }
155     }
156
157     // Callback for stage touched signal handling
158     public void OnStageTouched2(object source, Stage.TouchEventArgs e)
159     {
160       Log("OnStageTouched2() is called!state="+ e.TouchData.GetState(0) );
161     }
162
163     public void OnEventProcessingFinished(object source)
164         {
165       Log("OnEventProcessingFinished() is called!");
166     }
167
168     public void OnStageWheelEvent(object source, Stage.WheelEventArgs e)
169     {
170       Log("OnStageWheelEvent() is called!");
171       //Log("OnStageWheelEvent() is called!direction="+ e.WheelEvent.direction + " timeStamp=" + e.WheelEvent.timeStamp );
172     }
173
174
175     public void OnStage(object source , View.OnStageEventArgs e)
176         {
177       Log("OnStage() is called!");
178         }
179
180     public void MainLoop()
181     {
182       _application.MainLoop ();
183     }
184
185     [STAThread]
186     static void Main(string[] args)
187     {
188       Log("Main() is called!");
189
190       Example example = new Example(Application.NewApplication());
191       example.MainLoop ();
192
193       Log("After MainLoop()");
194     }
195   }
196 }
197