Calculate a new height when text was changed.
[platform/core/uifw/dali-toolkit.git] / plugins / dali-sharp / examples / user-alphafunction.cs
1 /** Copyright (c) 2017 Samsung Electronics Co., Ltd.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16
17 using System;
18 using System.Runtime.InteropServices;
19 using Dali;
20 using Dali.Constants;
21
22 namespace MyCSharpExample
23 {
24     class Example
25     {
26         private Dali.Application _application;
27         private Animation _animation;
28         private TextLabel _text;
29         private View _view1, _view2, _view3;
30         private UserAlphaFunctionDelegate _user_alpha_func;
31         private int myCount;
32
33         public static void Log(string str)
34         {
35             Console.WriteLine("[DALI C# SAMPLE] " + str);
36         }
37
38         public Example(Dali.Application application)
39         {
40             _application = application;
41             _application.Initialized += Initialize;
42         }
43
44         // Declare user alpha function delegate
45         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
46         delegate float UserAlphaFunctionDelegate(float progress);
47
48         public void Initialize(object source, NUIApplicationInitEventArgs e)
49         {
50             Log("Initialize() is called!");
51             Window window = Window.Instance;
52             window.BackgroundColor = Color.White;
53             window.Touched += OnWindowTouched;
54             window.Touched += OnWindowTouched2;
55             window.WheelRolled += OnWindowWheelEvent;
56
57             // Add a _text label to the window
58             _text = new TextLabel("Hello Mono World");
59             _text.ParentOrigin = ParentOrigin.BottomCenter;
60             _text.PivotPoint = AnchorPoint.BottomCenter;
61             _text.HorizontalAlignment = "CENTER";
62             _text.PointSize = 32.0f;
63             window.Add(_text);
64
65             _view1 = new View();
66             _view1.Size = new Vector3(200.0f, 200.0f, 0.0f);
67             _view1.BackgroundColor = Color.Green;
68             _view1.ParentOrigin = ParentOrigin.Center;
69             _view1.PivotPoint = AnchorPoint.Center;
70             _view1.SetResizePolicy(ResizePolicyType.FIXED, DimensionType.AllDimensions);
71             _view1.AddedToWindow += OnWindow;
72             window.Add(_view1);
73
74             _view2 = new View();
75             _view2.BackgroundColor = Color.Red;
76             _view2.Size = new Vector3(50.0f, 50.0f, 0.0f);
77             _view2.ParentOrigin = ParentOrigin.TopLeft;
78             _view2.PivotPoint = AnchorPoint.TopLeft;
79             _view2.SetResizePolicy(ResizePolicyType.FIXED, DimensionType.AllDimensions);
80             _view1.Add(_view2);
81
82             _view3 = new View();
83             _view3.BackgroundColor = Color.Blue;
84             _view3.Size = new Vector3(50.0f, 50.0f, 0.0f);
85             _view3.ParentOrigin = ParentOrigin.TopLeft;
86             _view3.PivotPoint = AnchorPoint.TopLeft;
87             _view3.SetResizePolicy(ResizePolicyType.FIXED, DimensionType.AllDimensions);
88             _view1.Add(_view3);
89
90             _user_alpha_func = new UserAlphaFunctionDelegate(body);
91
92             MyAnimating();
93         }
94
95         // User defines alpha function as custom alpha function
96         // Important Notification : when this custom alpha-function is implemented,
97         // the other function call nor other data excess is prevented.
98         // this method must be implemented to calculate the values of input and output purely.
99         // unless, this will cause application crash.
100         float body(float progress)
101         {
102             if (progress > 0.2f && progress < 0.7f)
103             {
104                 return progress + 0.8f;
105             }
106             return progress;
107         }
108
109         // Callback for _animation finished signal handling
110         public void AnimationFinished(object sender, EventArgs e)
111         {
112             Log("AnimationFinished() is called!");
113             myCount = 0;
114         }
115
116         public void MyAnimating()
117         {
118             // Create a new _animation
119             if (_animation)
120             {
121                 _animation.Clear();
122                 _animation.Reset();
123             }
124
125             _animation = new Animation(10000); // 10000 milli-second of duration
126             _animation.StartTime = 5000;
127             _animation.EndTime = 10000;
128             _animation.TargetProperty = "Position";
129             _animation.AlphaFunction = new AlphaFunction(_user_alpha_func);
130             _animation.Destination = new Vector3(150.0f, 150.0f, 0.0f);
131             _animation.AnimateTo(_view2);
132             // Connect the signal callback for animaiton finished signal
133             _animation.Finished += AnimationFinished;
134             _animation.EndAction = Animation.EndActions.Discard;
135             // Play the _animation
136             _animation.Play();
137         }
138
139         // Callback for window touched signal handling
140         public void OnWindowTouched(object source, Window.TouchEventArgs e)
141         {
142             // Only animate the _text label when touch down happens
143             if (e.Touch.GetState(0) == PointStateType.DOWN)
144             {
145                 Log("OnWindowTouched() is called! PointStateType.DOWN came!");
146                 myCount++;
147                 if (myCount > 1)
148                 {
149                     _animation.Stop();
150                     Log("_animation.Stop() is called!");
151                 }
152             }
153         }
154
155         // Callback for window touched signal handling
156         public void OnWindowTouched2(object source, Window.TouchEventArgs e)
157         {
158             Log("OnWindowTouched2() is called!state=" + e.Touch.GetState(0));
159         }
160
161         public void OnEventProcessingFinished(object source)
162         {
163             Log("OnEventProcessingFinished() is called!");
164         }
165
166         public void OnWindowWheelEvent(object source, Window.WheelEventArgs e)
167         {
168             Log("OnWindowWheelEvent() is called!");
169         }
170
171
172         public void OnWindow(object source, View.OnWindowEventArgs e)
173         {
174             Log("OnWindow() is called!");
175         }
176
177         public void MainLoop()
178         {
179             _application.MainLoop();
180         }
181
182         [STAThread]
183         static void Main(string[] args)
184         {
185             Log("Main() is called!");
186
187             Example example = new Example(Application.NewApplication());
188             example.MainLoop();
189
190             Log("After MainLoop()");
191         }
192     }
193 }
194
195