6574f2bbf50e9f3fb97f9edb924a6df617f2e7a9
[platform/core/csapi/tizenfx.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.Constants;
23
24 namespace VisaulAnimationExample
25 {
26     class Example : NUIApplication
27     {
28         private VisualView _contentView;
29         private TextLabel _title;
30         private PushButton _shadowButton;
31         private bool _active = false;
32         private const string _resPath = "/home/owner/apps_rw/NUISamples.TizenTV/res";
33
34         private Animation _animation;
35         private bool _transitionInProgress = false;
36         private int cnt1, cnt2;
37
38         private SVGVisual svgVisual;
39         private AnimatedImageVisual gifVisual;
40
41         public Example() : base()
42         {
43         }
44
45         public Example(string stylesheet) : base(stylesheet)
46         {
47         }
48
49         public Example(string stylesheet, WindowMode windowMode) : base(stylesheet, windowMode)
50         {
51         }
52
53         protected override void OnCreate()
54         {
55             base.OnCreate();
56             Initialize();
57         }
58
59         public void Initialize()
60         {
61             Stage stage = Stage.Instance;
62             stage.BackgroundColor = Color.White;
63
64             TableView titleLayout = new TableView(2, 1);
65             titleLayout.Name = ("TitleLayout");
66             titleLayout.AnchorPoint = AnchorPoint.TopLeft;
67             titleLayout.Position2D = new Position2D(10, 10);
68             titleLayout.Size2D = new Size2D((int)(stage.Size.Width * 0.9f), (int)(stage.Size.Height * 0.9f));
69             titleLayout.SetCellPadding(new Size2D(10, 10));
70             titleLayout.BackgroundColor = Color.Cyan;
71             stage.GetDefaultLayer().Add(titleLayout);
72
73             _title = new TextLabel("Visual Transition / SVG / AGIF Example");
74             _title.Name = ("Title");
75             _title.SetStyleName("Title");
76             _title.WidthResizePolicy = ResizePolicyType.FillToParent;
77             _title.HeightResizePolicy = ResizePolicyType.UseNaturalSize;
78             _title.HorizontalAlignment = HorizontalAlignment.HorizontalAlignCenter;
79             titleLayout.AddChild(_title, new TableView.CellPosition(0, 0));
80             titleLayout.SetFitHeight(0);
81
82             TableView contentLayout = new TableView(3, 2);
83             contentLayout.Name = ("ContentLayout");
84             contentLayout.WidthResizePolicy = ResizePolicyType.FillToParent;
85             contentLayout.HeightResizePolicy = ResizePolicyType.FillToParent;
86             contentLayout.AnchorPoint = AnchorPoint.TopLeft;
87             contentLayout.SetCellPadding(new Size2D(10, 10));
88             contentLayout.BackgroundColor = Color.Magenta;
89             titleLayout.AddChild(contentLayout, new TableView.CellPosition(1, 0));
90
91             //////////////////////////////////////////////////////////////////////
92             // Create a conttent view
93             _contentView = new VisualView();
94             _contentView.WidthResizePolicy = ResizePolicyType.SizeRelativeToParent;
95             _contentView.HeightResizePolicy = ResizePolicyType.SizeRelativeToParent;
96             //_contentView.Size2D = new Size2D(250, 250);
97             _contentView.BackgroundImage = _resPath + "/images/background-blocks.jpg";
98
99             ImageVisual _icon = new ImageVisual();
100             _icon.URL = _resPath + "/images/application-icon-0.png";
101             _icon.DepthIndex = 1;
102             _icon.Size = new Size2D(50, 50);
103             _icon.SizePolicy = new Vector2(1, 1);
104             _icon.Position = new Position2D(5, 5);
105             _icon.PositionPolicy = new Vector2(1, 1);
106             _icon.Origin = Visual.AlignType.TopBegin;
107             _icon.AnchorPoint = Visual.AlignType.TopBegin;
108             _icon.MixColor = new Color(0, 1, 0, 0.5f);
109             _icon.Opacity = 0.5f;
110             _contentView.AddVisual("icon_visual1", _icon);
111
112             contentLayout.AddChild(_contentView, new TableView.CellPosition(0, 0));
113
114             _shadowButton = new PushButton();
115             _shadowButton.LabelText = "Toggle Transition";
116             _shadowButton.Name = ("ToggleTransition");
117             _shadowButton.ParentOrigin = ParentOrigin.Center;
118             _shadowButton.AnchorPoint = AnchorPoint.Center;
119             _shadowButton.Clicked += (obj, ev) =>
120             {
121                 _active = !_active;
122                 StartTransition(_active);
123                 return true;
124             };
125             _shadowButton.WidthResizePolicy = ResizePolicyType.FillToParent;
126             _shadowButton.HeightResizePolicy = ResizePolicyType.FillToParent;
127             contentLayout.AddChild(_shadowButton, new TableView.CellPosition(0, 1));
128
129             //////////////////////////////////////////////////////////////////////
130             // make NPatch visual test
131             NPatchVisual npatch1 = new NPatchVisual();
132             npatch1.URL = _resPath + "/images/gallery-2.jpg";
133             npatch1.Size = new Size2D(400, 400);
134             npatch1.SizePolicy = new Vector2(1, 1);
135             npatch1.Position = new Position2D(400, 0);
136             npatch1.PositionPolicy = new Vector2(1, 1);
137             npatch1.Origin = Visual.AlignType.TopBegin;
138             npatch1.AnchorPoint = Visual.AlignType.TopBegin;
139             npatch1.Border = new Rectangle(100, 100, 100, 100);
140             npatch1.DepthIndex = 2;
141             npatch1.MixColor = new Color(1, 0, 0, 1);
142             npatch1.Opacity = 0.5f;
143             _contentView.AddVisual("npatchImageVisual1", npatch1);
144
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 = new Vector2(1, 1);
158             svgVisual.Position = new Position2D(0, 0);
159             svgVisual.PositionPolicy = new Vector2(1, 1);
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 = new Vector2(1, 1);
198             gifVisual.Position = new Position2D(0, 0);
199             gifVisual.PositionPolicy = new Vector2(1, 1);
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         }
228
229         private void StartTransition(bool activate)
230         {
231             if (_animation)
232             {
233                 _animation.Stop();
234                 _animation.Finished += OnTransitionFinished;
235             }
236
237             if (activate)
238             {
239                 AnimatorVisual grow = new AnimatorVisual();
240                 grow.AlphaFunction = "LINEAR";
241                 grow.StartTime = 0;
242                 grow.EndTime = 1000;
243                 grow.Target = "icon_visual1";
244                 grow.PropertyIndex = "Size";
245                 grow.DestinationValue = new Size2D(200, 200);
246                 _animation = _contentView.AnimateVisual(grow);
247             }
248             else
249             {
250                 AnimatorVisual shrink = new AnimatorVisual();
251                 shrink.AlphaFunction = "LINEAR";
252                 shrink.StartTime = 0;
253                 shrink.EndTime = 1000;
254                 shrink.Target = "icon_visual1";
255                 shrink.PropertyIndex = "Size";
256                 shrink.DestinationValue = new Size2D(50, 50);
257                 _animation = _contentView.AnimateVisual(shrink);
258             }
259
260             if (_animation)
261             {
262                 _animation.Finished += OnTransitionFinished;
263                 _transitionInProgress = true;
264                 _animation.Play();
265             }
266         }
267         private void OnTransitionFinished(object sender, EventArgs e)
268         {
269             _transitionInProgress = false;
270             if (_animation)
271             {
272                 _animation.Finished += OnTransitionFinished;
273                 _animation.Reset();
274             }
275         }
276
277
278         /// <summary>
279         /// The main entry point for the application.
280         /// </summary>
281         [STAThread]
282         static void _Main(string[] args)
283         {
284             Example example = new Example();
285             example.Run(args);
286         }
287
288
289     }
290 }