b8a95619fa82ca80c5868127c6eda9df891e742e
[platform/core/csapi/tizenfx.git] / NUISamples / NUISamples / NUISamples.TizenTV / examples / visual-animation-test2.cs
1 /*
2  * Copyright (c) 2017 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 VisaulAnimationExample
26 {
27     class Example2 : NUIApplication
28     {
29         private VisualView _contentView1;
30         private VisualView _contentView2;
31         private VisualView _contentView3;
32         private TextLabel _title;
33         private PushButton _shadowButton1;
34         private PushButton _shadowButton2;
35         private PushButton _shadowButton3;
36         private bool _active1 = false;
37         private bool _active2 = false;
38         private bool _active3 = false;
39         private const string _resPath = "/home/owner/apps_rw/NUISamples.TizenTV/res";
40
41         private Animation _animation;
42         private bool _transitionInProgress = false;
43
44         public Example2() : base()
45         {
46         }
47
48         public Example2(string stylesheet) : base(stylesheet)
49         {
50         }
51
52         public Example2(string stylesheet, WindowMode windowMode) : base(stylesheet, windowMode)
53         {
54         }
55
56         protected override void OnCreate()
57         {
58             base.OnCreate();
59             Initialize();
60         }
61
62         public void Initialize()
63         {
64             Window window = Window.Instance;
65             window.BackgroundColor = Color.White;
66
67             TableView titleLayout = new TableView(2, 1);
68             titleLayout.Name = ("TitleLayout");
69             titleLayout.AnchorPoint = AnchorPoint.TopLeft;
70             titleLayout.Position2D = new Position2D(10, 10);
71             titleLayout.Size2D = new Size2D((int)(window.Size.Width * 0.9f), (int)(window.Size.Height * 0.9f));
72             titleLayout.SetCellPadding(new Size2D(10, 10));
73             titleLayout.BackgroundColor = Color.Cyan;
74             window.GetDefaultLayer().Add(titleLayout);
75
76             _title = new TextLabel("Visual Transition Example");
77             _title.Name = ("Title");
78             _title.SetStyleName("Title");
79             _title.WidthResizePolicy = ResizePolicyType.FillToParent;
80             _title.HeightResizePolicy = ResizePolicyType.UseNaturalSize;
81             _title.HorizontalAlignment = HorizontalAlignment.Center;
82             titleLayout.AddChild(_title, new TableView.CellPosition(0, 0));
83             titleLayout.SetFitHeight(0);
84
85             TableView contentLayout = new TableView(3, 2);
86             contentLayout.Name = ("ContentLayout");
87             contentLayout.WidthResizePolicy = ResizePolicyType.FillToParent;
88             contentLayout.HeightResizePolicy = ResizePolicyType.FillToParent;
89             contentLayout.AnchorPoint = AnchorPoint.TopLeft;
90             contentLayout.SetCellPadding(new Size2D(10, 10));
91             contentLayout.BackgroundColor = Color.Magenta;
92             titleLayout.AddChild(contentLayout, new TableView.CellPosition(1, 0));
93
94             //////////////////////////////////////////////////////////////////////
95             // make visual position animation
96             _contentView1 = new VisualView();
97             _contentView1.WidthResizePolicy = ResizePolicyType.SizeRelativeToParent;
98             _contentView1.HeightResizePolicy = ResizePolicyType.SizeRelativeToParent;
99             //_contentView.Size2D = new Size2D(250, 250);
100             _contentView1.BackgroundImage = _resPath + "/images/background-blocks.jpg";
101
102             ImageVisual _icon = new ImageVisual();
103             _icon.URL = _resPath + "/images/application-icon-0.png";
104             _icon.DepthIndex = 1;
105             _icon.Size = new Size2D(50, 50);
106             _icon.SizePolicy = VisualTransformPolicyType.Absolute;
107             _icon.Position = new Position2D(5, 5);
108             _icon.PositionPolicy = VisualTransformPolicyType.Absolute;
109             _icon.Origin = Visual.AlignType.TopBegin;
110             _icon.AnchorPoint = Visual.AlignType.TopBegin;
111             _icon.MixColor = new Color(0, 1, 0, 0.5f);
112             _icon.Opacity = 0.5f;
113             _contentView1.AddVisual("icon_visual1", _icon);
114
115             contentLayout.AddChild(_contentView1, new TableView.CellPosition(0, 0));
116
117             _shadowButton1 = new PushButton();
118             _shadowButton1.LabelText = "Toggle Transition Position";
119             _shadowButton1.Name = ("ToggleTransition");
120             _shadowButton1.ParentOrigin = ParentOrigin.Center;
121             _shadowButton1.AnchorPoint = AnchorPoint.Center;
122             _shadowButton1.Clicked += (obj, ev) =>
123             {
124                 _active1 = !_active1;
125                 StartTransition(_contentView1, "icon_visual1", "Offset", _active1);
126                 return true;
127             };
128             _shadowButton1.WidthResizePolicy = ResizePolicyType.FillToParent;
129             _shadowButton1.HeightResizePolicy = ResizePolicyType.FillToParent;
130             contentLayout.AddChild(_shadowButton1, new TableView.CellPosition(0, 1));
131
132             //////////////////////////////////////////////////////////////////////
133             // make visual opacity animation
134             _contentView2 = new VisualView();
135             _contentView2.WidthResizePolicy = ResizePolicyType.SizeRelativeToParent;
136             _contentView2.HeightResizePolicy = ResizePolicyType.SizeRelativeToParent;
137             //_contentView.Size2D = new Size2D(250, 250);
138             _contentView2.BackgroundImage = _resPath + "/images/background-blocks.jpg";
139
140             ImageVisual _icon2 = new ImageVisual();
141             _icon2.URL = _resPath + "/images/application-icon-0.png";
142             _icon2.DepthIndex = 1;
143             _icon2.Size = new Size2D(50, 50);
144             _icon2.SizePolicy = VisualTransformPolicyType.Absolute;
145             _icon2.Position = new Position2D(5, 5);
146             _icon2.PositionPolicy = VisualTransformPolicyType.Absolute;
147             _icon2.Origin = Visual.AlignType.TopBegin;
148             _icon2.AnchorPoint = Visual.AlignType.TopBegin;
149             _icon2.MixColor = new Color(0, 1, 0, 0.5f);
150             _icon2.Opacity = 0.5f;
151             _contentView2.AddVisual("icon_visual2", _icon2);
152
153             contentLayout.AddChild(_contentView2, new TableView.CellPosition(1, 0));
154
155             _shadowButton2 = new PushButton();
156             _shadowButton2.LabelText = "Toggle Transition Opacity";
157             _shadowButton2.Name = ("ToggleTransition");
158             _shadowButton2.ParentOrigin = ParentOrigin.Center;
159             _shadowButton2.AnchorPoint = AnchorPoint.Center;
160             _shadowButton2.Clicked += (obj, ev) =>
161             {
162                 _active2 = !_active2;
163                 StartTransition(_contentView2, "icon_visual2", "Opacity", _active2);
164                 return true;
165             };
166             _shadowButton2.WidthResizePolicy = ResizePolicyType.FillToParent;
167             _shadowButton2.HeightResizePolicy = ResizePolicyType.FillToParent;
168             contentLayout.AddChild(_shadowButton2, new TableView.CellPosition(1, 1));
169
170             //////////////////////////////////////////////////////////////////////
171             // make AnimatedImage visual test
172             _contentView3 = new VisualView();
173             _contentView3.WidthResizePolicy = ResizePolicyType.SizeRelativeToParent;
174             _contentView3.HeightResizePolicy = ResizePolicyType.SizeRelativeToParent;
175             //_contentView.Size2D = new Size2D(250, 250);
176             _contentView3.BackgroundImage = _resPath + "/images/background-blocks.jpg";
177
178             ImageVisual _icon3 = new ImageVisual();
179             _icon3.URL = _resPath + "/images/application-icon-0.png";
180             _icon3.DepthIndex = 1;
181             _icon3.Size = new Size2D(50, 50);
182             _icon3.SizePolicy = VisualTransformPolicyType.Absolute;
183             _icon3.Position = new Position2D(5, 5);
184             _icon3.PositionPolicy = VisualTransformPolicyType.Absolute;
185             _icon3.Origin = Visual.AlignType.TopBegin;
186             _icon3.AnchorPoint = Visual.AlignType.TopBegin;
187             _icon3.MixColor = new Color(0, 1, 0, 0.5f);
188             _icon3.Opacity = 0.5f;
189             _contentView3.AddVisual("icon_visual3", _icon3);
190
191             contentLayout.AddChild(_contentView3, new TableView.CellPosition(2, 0));
192
193             _shadowButton3 = new PushButton();
194             _shadowButton3.LabelText = "Toggle Transition MixColor";
195             _shadowButton3.Name = ("ToggleTransition");
196             _shadowButton3.ParentOrigin = ParentOrigin.Center;
197             _shadowButton3.AnchorPoint = AnchorPoint.Center;
198             _shadowButton3.Clicked += (obj, ev) =>
199             {
200                 _active3 = !_active3;
201                 StartTransition(_contentView3, "icon_visual3", "MixColor", _active3);
202                 return true;
203             };
204             _shadowButton3.WidthResizePolicy = ResizePolicyType.FillToParent;
205             _shadowButton3.HeightResizePolicy = ResizePolicyType.FillToParent;
206             contentLayout.AddChild(_shadowButton3, new TableView.CellPosition(2, 1));
207         }
208
209         private void StartTransition(VisualView view, string target, string property, bool activate)
210         {
211             if (_animation)
212             {
213                 _animation.Stop();
214                 _animation.Finished += OnTransitionFinished;
215             }
216
217             if (activate)
218             {
219                 VisualAnimator state1 = new VisualAnimator();
220                 state1.AlphaFunction = AlphaFunction.BuiltinFunctions.Linear;
221                 state1.StartTime = 0;
222                 state1.EndTime = 1000;
223                 state1.Target = target;
224                 state1.PropertyIndex = property;
225                 if (property == "Offset")
226                 {
227                     state1.DestinationValue = new Position2D(20, 20);
228                 }
229                 else if (property == "Opacity")
230                 {
231                     state1.DestinationValue = 0.0f;
232                 }
233                 else if (property == "MixColor")
234                 {
235                     state1.DestinationValue = Color.Green;
236                 }
237                 _animation = view.VisualAnimate(state1);
238             }
239             else
240             {
241                 VisualAnimator state2 = new VisualAnimator();
242                 state2.AlphaFunction = AlphaFunction.BuiltinFunctions.Linear;
243                 state2.StartTime = 0;
244                 state2.EndTime = 1000;
245                 state2.Target = target;
246                 state2.PropertyIndex = property;
247                 if (property == "Offset")
248                 {
249                     state2.DestinationValue = new Position2D(5, 5);
250                 }
251                 else if (property == "Opacity")
252                 {
253                     state2.DestinationValue = 1.0f;
254                 }
255                 else if (property == "MixColor")
256                 {
257                     state2.DestinationValue = Color.Red;
258                 }
259                 _animation = view.VisualAnimate(state2);
260             }
261
262             if (_animation)
263             {
264                 _animation.Finished += OnTransitionFinished;
265                 _transitionInProgress = true;
266                 _animation.Play();
267             }
268         }
269         private void OnTransitionFinished(object sender, EventArgs e)
270         {
271             _transitionInProgress = false;
272             if (_animation)
273             {
274                 _animation.Finished += OnTransitionFinished;
275                 _animation.Reset();
276             }
277         }
278
279
280         /// <summary>
281         /// The main entry point for the application.
282         /// </summary>
283         [STAThread]
284         static void _Main(string[] args)
285         {
286             Example example = new Example();
287             example.Run(args);
288         }
289
290
291     }
292 }