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