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