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