[Tizen] Update NUISamples to support .netcore 2.0
[platform/core/csapi/nui.git] / NUISamples / examples / image-view.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 ImageViewTest
26 {
27     class Example : NUIApplication
28     {
29
30         private const string resources = "/home/owner/apps_rw/NUISamples.TizenTV/res";
31         public static void Log(string str)
32         {
33             Tizen.Log.Debug("NUI", "[DALI C# SAMPLE] " + str);
34         }
35
36         private Animation _animation;
37         private ImageView _imageView;
38         private bool _isAniFinised = true;
39         private Layer layer, _layer1, _layer2;
40         private PushButton _pushButton1, _pushButton2;
41         private Window window;
42
43         public Example() : base()
44         {
45         }
46
47         public Example(string stylesheet) : base(stylesheet)
48         {
49         }
50
51         public Example(string stylesheet, WindowMode windowMode) : base(stylesheet, windowMode)
52         {
53         }
54
55         protected override void OnCreate()
56         {
57             base.OnCreate();
58             Initialize();
59         }
60
61         public void Initialize()
62         {
63             Log("Customized Application Initialize event handler");
64             window = Window.Instance;
65             window.BackgroundColor = Color.Cyan;
66             window.TouchEvent += OnWindowTouched;
67             window.WheelEvent += OnWindowWheelMoved;
68             window.KeyEvent += OnWindowKeyPressed;
69             //window.EventProcessingFinished += OnWindowEventProcessingFinished;
70
71             layer = window.GetDefaultLayer();
72             _layer1 = new Layer();
73             _layer2 = new Layer();
74             window.AddLayer(_layer1);
75             window.AddLayer(_layer2);
76             Log("_layer1.Behavior =" + _layer1.Behavior);
77             if (_layer1.Behavior == Layer.LayerBehavior.LayerUI)
78             {
79                 _layer1.Behavior = Layer.LayerBehavior.Layer2D;
80                 Log("again _layer1.Behavior =" + _layer1.Behavior);
81             }
82             // Add a ImageView to the window
83             // PropertyMap map = new PropertyMap();
84             // map.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.NPatch));
85             // map.Add(NpatchImageVisualProperty.URL, new PropertyValue(resources+"/images/00_popup_bg.9.png"));
86             // map.Add(NpatchImageVisualProperty.Border, new PropertyValue(new Rectangle(100, 100, 100, 100)));
87             _imageView = new ImageView();
88             //_imageView.ImageMap = map;
89             _imageView.ResourceUrl = resources+"/images/00_popup_bg.9.png";
90             //_imageView.Border = new Rectangle(100, 100, 100, 100);
91             _imageView.ParentOrigin = ParentOrigin.TopLeft;
92             _imageView.PivotPoint = PivotPoint.TopLeft;
93             _imageView.Position = new Position(5.0f, 5.0f, 0.0f);
94             _imageView.PixelArea = new Vector4(0.0f, 0.0f, 0.5f, 0.5f);
95             _imageView.Size2D = new Size2D(200, 80);
96             //_imageView.SetResizePolicy(ResizePolicyType.USE_NATURAL_SIZE, DimensionType.ALL_DIMENSIONS);
97             layer.Add(_imageView);
98
99             _pushButton1 = new PushButton();
100             _pushButton1.ParentOrigin = ParentOrigin.BottomLeft;
101             _pushButton1.PivotPoint = PivotPoint.BottomLeft;
102             _pushButton1.LabelText = "start animation";
103             _pushButton1.Position = new Vector3(0.0f, window.Size.Height * 0.1f, 0.0f);
104             _pushButton1.Clicked += OnPushButtonClicked1;
105             _layer1.Add(_pushButton1);
106
107             _pushButton2 = new PushButton();
108             _pushButton2.ParentOrigin = ParentOrigin.BottomLeft;
109             _pushButton2.PivotPoint = PivotPoint.BottomLeft;
110             _pushButton2.LabelText = "reload image with same URL";
111             _pushButton2.Position = new Vector3(0.0f, window.Size.Height * 0.2f, 0.0f);
112             _pushButton2.Clicked += OnPushButtonClicked2;
113             _layer2.Add(_pushButton2);
114
115             ImageView syncImage = new ImageView();
116             syncImage.ParentOrigin = ParentOrigin.CenterLeft;
117             syncImage.PivotPoint = PivotPoint.CenterLeft;
118             syncImage.PositionUsesPivotPoint = true;
119             syncImage.Size2D = new Size2D(150, 150);
120             syncImage.ResourceUrl = resources+"/images/gallery-3.jpg";
121             syncImage.SynchronosLoading = true;
122             layer.Add(syncImage);
123
124             PropertyMap _map = new PropertyMap();
125             _map.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.NPatch));
126             _map.Add(NpatchImageVisualProperty.URL, new PropertyValue(resources+"/images/00_popup_bg.9.png"));
127             _map.Add(NpatchImageVisualProperty.SynchronousLoading, new PropertyValue(true));
128             ImageView nPatchImage = new ImageView();
129             nPatchImage.ParentOrigin = ParentOrigin.BottomLeft;
130             nPatchImage.PivotPoint = PivotPoint.BottomLeft;
131             nPatchImage.PositionUsesPivotPoint = true;
132             nPatchImage.Size2D = new Size2D(300, 100);
133             nPatchImage.ImageMap = _map;
134             layer.Add(nPatchImage);
135
136             ImageView syncNineImage = new ImageView();
137             syncNineImage.ParentOrigin = ParentOrigin.CenterLeft;
138             syncNineImage.PivotPoint = PivotPoint.CenterLeft;
139             syncNineImage.Position2D = new Position2D(0, 200);
140             syncNineImage.PositionUsesPivotPoint = true;
141             syncNineImage.Size2D = new Size2D(150, 150);
142             syncNineImage.ResourceUrl = resources+"/images/00_popup_bg.9.png";
143             syncNineImage.SynchronosLoading = true;
144             syncNineImage.Border = new Rectangle(0, 0, 0, 0);
145             layer.Add(syncNineImage);
146         }
147
148         public bool OnPushButtonClicked2(object sender, EventArgs e)
149         {
150             if (_imageView)
151             {
152                 Log("OnPushButtonClicked2()!");
153                 layer.Remove(_imageView);
154                 _imageView = new ImageView();
155                 _imageView.ResourceUrl = resources+"/images/gallery-3.jpg";
156                 _imageView.ParentOrigin = ParentOrigin.Center;
157                 _imageView.PivotPoint = PivotPoint.Center;
158                 _imageView.PixelArea = new Vector4(0.0f, 0.0f, 0.5f, 0.5f);
159                 //_imageView.SetResizePolicy(ResizePolicyType.USE_NATURAL_SIZE, DimensionType.ALL_DIMENSIONS);
160                 layer.Add(_imageView);
161             }
162
163             return true;
164         }
165
166         public bool OnPushButtonClicked1(object sender, EventArgs e)
167         {
168             if (_isAniFinised == true)
169             {
170                 _isAniFinised = false;
171                 Log("OnPushButtonClicked1()!");
172
173                 // Create a new _animation
174                 if (_animation)
175                 {
176                     //_animation.Stop(Tizen.NUI.Constants.Animation.EndAction.Stop);
177                     _animation.Reset();
178                 }
179
180                 _animation = new Animation();
181                 _animation.AnimateTo(_imageView, "PixelArea", new Vector4(0.5f, 0.0f, 0.5f, 0.5f), 0, 1000);
182                 _animation.AnimateTo(_imageView, "PixelArea", new Vector4(0.5f, 0.5f, 0.5f, 0.5f), 1000, 2000);
183                 _animation.AnimateTo(_imageView, "PixelArea", new Vector4(0.0f, 0.0f, 1.0f, 1.0f), 2000, 3000);
184                 _animation.AnimateTo(_imageView, "PixelArea", new Vector4(0.5f, 0.5f, 0.5f, 0.5f), 3000, 4000);
185
186                 KeyFrames _keyFrames = new KeyFrames();
187                 _keyFrames.Add(0.0f, new Size(0.0f, 0.0f, 0.0f));
188                 _keyFrames.Add(0.3f, new Size(window.Size.Width * 0.7f, window.Size.Height * 0.7f, 0.0f));
189                 _keyFrames.Add(1.0f, new Size(window.Size));
190                 _animation.AnimateBetween(_imageView, "Size", _keyFrames, 4000, 6000, Animation.Interpolation.Linear);
191
192                 _animation.EndAction = Animation.EndActions.Discard;
193
194                 // Connect the signal callback for animaiton finished signal
195                 _animation.Finished += AnimationFinished;
196                 _animation.Finished += AnimationFinished2;
197
198                 // Play the _animation
199                 _animation.Play();
200             }
201
202             return true;
203         }
204
205         // Callback for _animation finished signal handling
206         public void AnimationFinished(object sender, EventArgs e)
207         {
208             Log("AnimationFinished()!");
209         }
210
211         // Callback for second _animation finished signal handling
212         public void AnimationFinished2(object sender, EventArgs e)
213         {
214             Log("AnimationFinished2()!");
215             if (_animation)
216             {
217                 Log("Duration= " + _animation.Duration);
218                 Log("EndAction= " + _animation.EndAction);
219                 _isAniFinised = true;
220             }
221         }
222
223         public void OnWindowEventProcessingFinished(object sender, EventArgs e)
224         {
225             Log("OnWindowEventProcessingFinished()!");
226             if (e != null)
227             {
228                 Log("e != null !");
229             }
230         }
231
232         public void OnWindowKeyPressed(object sender, Window.KeyEventArgs e)
233         {
234             Log("OnWindowKeyEventOccured()!");
235             Log("keyPressedName=" + e.Key.KeyPressedName);
236             Log("state=" + e.Key.State);
237         }
238
239         public void OnWindowWheelMoved(object sender, Window.WheelEventArgs e)
240         {
241             Log("OnWindowWheelEventOccured()!");
242             Log("direction=" + e.Wheel.Direction);
243             Log("type=" + e.Wheel.Type);
244         }
245
246         // Callback for window touched signal handling
247         public void OnWindowTouched(object sender, Window.TouchEventArgs e)
248         {
249             Log("OnWindowTouched()! e.TouchData.GetState(0)=" + e.Touch.GetState(0));
250         }
251
252         /// <summary>
253         /// The main entry point for the application.
254         /// </summary>
255         [STAThread]
256         static void _Main(string[] args)
257         {
258             Log("Main() called!");
259
260             Example example = new Example();
261             example.Run(args);
262         }
263     }
264 }