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