add property types include Opacity & CreateFromObject() types added
[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
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.SizeRelativeToParent;
93             _contentView.HeightResizePolicy = ResizePolicyType.SizeRelativeToParent;
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             _icon.MixColor = new Color(0, 1, 0, 0.5f);
107             _icon.Opacity = 0.5f;
108             _contentView.AddVisual("icon_visual1", _icon);
109
110             contentLayout.AddChild(_contentView, new TableView.CellPosition(0, 0));
111
112             _shadowButton = new PushButton();
113             _shadowButton.LabelText = "Toggle Transition";
114             _shadowButton.Name = ("ToggleTransition");
115             _shadowButton.ParentOrigin = ParentOrigin.Center;
116             _shadowButton.AnchorPoint = AnchorPoint.Center;
117             _shadowButton.Clicked += (obj, ev) =>
118             {
119                 _active = !_active;
120                 StartTransition(_active);
121                 return true;
122             };
123             _shadowButton.WidthResizePolicy = ResizePolicyType.FillToParent;
124             _shadowButton.HeightResizePolicy = ResizePolicyType.FillToParent;
125             contentLayout.AddChild(_shadowButton, new TableView.CellPosition(0, 1));
126
127             //////////////////////////////////////////////////////////////////////
128             // make NPatch visual test
129             NPatchVisual npatch1 = new NPatchVisual();
130             npatch1.URL = _resPath + "/images/gallery-2.jpg";
131             npatch1.Size = new Size2D(400, 400);
132             npatch1.SizePolicy = new Vector2(1, 1);
133             npatch1.Position = new Position2D(400, 0);
134             npatch1.PositionPolicy = new Vector2(1, 1);
135             npatch1.Origin = Visual.AlignType.TopBegin;
136             npatch1.AnchorPoint = Visual.AlignType.TopBegin;
137             npatch1.Border = new Rectangle(100, 100, 100, 100);
138             npatch1.DepthIndex = 2;
139             npatch1.MixColor = new Color(1, 0, 0, 1);
140             npatch1.Opacity = 0.5f;
141             _contentView.AddVisual("npatchImageVisual1", npatch1);
142
143
144             //////////////////////////////////////////////////////////////////////
145             // make SVG visual test
146             VisualView VisualView1 = new VisualView();
147             VisualView1.WidthResizePolicy = ResizePolicyType.FillToParent;
148             VisualView1.HeightResizePolicy = ResizePolicyType.FillToParent;
149             VisualView1.BackgroundColor = Color.Black;
150             contentLayout.AddChild(VisualView1, new TableView.CellPosition(1, 0));
151
152             svgVisual = new SVGVisual();
153             svgVisual.URL = _resPath + "/images/Kid1.svg";
154             svgVisual.Size = new Size2D(300, 300);
155             svgVisual.SizePolicy = new Vector2(1, 1);
156             svgVisual.Position = new Position2D(0, 0);
157             svgVisual.PositionPolicy = new Vector2(1, 1);
158             svgVisual.Origin = Visual.AlignType.TopBegin;
159             svgVisual.AnchorPoint = Visual.AlignType.TopBegin;
160             VisualView1.AddVisual("svg_visual1", svgVisual);
161
162             PushButton svgButton = new PushButton();
163             svgButton.LabelText = "SVG Visual Test";
164             svgButton.Name = ("svg_visual_test");
165             svgButton.AnchorPoint = AnchorPoint.Center;
166             svgButton.WidthResizePolicy = ResizePolicyType.FillToParent;
167             svgButton.HeightResizePolicy = ResizePolicyType.FillToParent;
168             svgButton.Clicked += (obj, ev) =>
169             {
170                 cnt1++;
171                 if (cnt1 % 2 == 0)
172                 {
173                     svgVisual.URL = _resPath + "/images/World.svg";
174                 }
175                 else
176                 {
177                     svgVisual.URL = _resPath + "/images/Mail.svg";
178                 }
179                 Tizen.Log.Debug("NUI", "svg button clicked!");
180                 return true;
181             };
182             contentLayout.AddChild(svgButton, new TableView.CellPosition(1, 1));
183
184             //////////////////////////////////////////////////////////////////////
185             // make AnimatedImage visual test
186             VisualView VisualView2 = new VisualView();
187             VisualView2.WidthResizePolicy = ResizePolicyType.FillToParent;
188             VisualView2.HeightResizePolicy = ResizePolicyType.FillToParent;
189             VisualView2.BackgroundColor = Color.Blue;
190             contentLayout.AddChild(VisualView2, new TableView.CellPosition(2, 0));
191
192             gifVisual = new AnimatedImageVisual();
193             gifVisual.URL = _resPath + "/images/dog-anim.gif";
194             gifVisual.Size = new Size2D(200, 200);
195             gifVisual.SizePolicy = new Vector2(1, 1);
196             gifVisual.Position = new Position2D(0, 0);
197             gifVisual.PositionPolicy = new Vector2(1, 1);
198             gifVisual.Origin = Visual.AlignType.TopBegin;
199             gifVisual.AnchorPoint = Visual.AlignType.TopBegin;
200             VisualView2.AddVisual("gif_visual", gifVisual);
201
202             PushButton gifButton = new PushButton();
203             gifButton.LabelText = "AnimatedImage Visual Test";
204             gifButton.Name = ("gif_visual_test");
205             gifButton.AnchorPoint = AnchorPoint.Center;
206             gifButton.WidthResizePolicy = ResizePolicyType.FillToParent;
207             gifButton.HeightResizePolicy = ResizePolicyType.FillToParent;
208             gifButton.Clicked += (obj, ev) =>
209             {
210                 Tizen.Log.Debug("NUI", "gif button clicked!");
211                 cnt2++;
212                 if (cnt2 % 2 == 0)
213                 {
214                     gifVisual.URL = _resPath + "/images/dali-logo-anim.gif";
215                 }
216                 else
217                 {
218                     gifVisual.URL = _resPath + "/images/echo.gif";
219                 }
220                 return true;
221             };
222             contentLayout.AddChild(gifButton, new TableView.CellPosition(2, 1));
223
224
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                 AnimatorVisual grow = new AnimatorVisual();
238                 grow.AlphaFunction = "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.AnimateVisual(grow);
245             }
246             else
247             {
248                 AnimatorVisual shrink = new AnimatorVisual();
249                 shrink.AlphaFunction = "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.AnimateVisual(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 }