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