dali-1.2.31, nui 0.2.31 upgrade
[platform/core/csapi/nui.git] / NUISamples / NUISamples.TizenTV / examples / image-view.cs
1 /*
2 * Copyright (c) 2016 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.Constants;
22
23 namespace ImageViewTest
24 {
25     class Example : NUIApplication
26     {
27         public static void Log(string str)
28         {
29             Console.WriteLine("[DALI C# SAMPLE] " + str);
30         }
31
32         private Animation _animation;
33         private ImageView _imageView;
34         private bool _isAniFinised = true;
35         private Layer layer, _layer1, _layer2;
36         private PushButton _pushButton1, _pushButton2;
37         private Stage stage;
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             Log("Customized Application Initialize event handler");
60             stage = Stage.Instance;
61             stage.BackgroundColor = Color.Cyan;
62             stage.Touch += OnStageTouched;
63             stage.Wheel += OnStageWheelMoved;
64             stage.Key += OnStageKeyPressed;
65             //stage.EventProcessingFinished += OnStageEventProcessingFinished;
66
67             layer = stage.GetDefaultLayer();
68             _layer1 = new Layer();
69             _layer2 = new Layer();
70             stage.AddLayer(_layer1);
71             stage.AddLayer(_layer2);
72             Log("_layer1.Behavior =" + _layer1.Behavior);
73             if (_layer1.Behavior == Layer.LayerBehavior.LayerUI)
74             {
75                 _layer1.Behavior = Layer.LayerBehavior.Layer2D;
76                 Log("again _layer1.Behavior =" + _layer1.Behavior);
77             }
78             // Add a ImageView to the stage
79             _imageView = new ImageView();
80             _imageView.ResourceUrl = "./images/gallery-3.jpg";
81             _imageView.ParentOrigin = ParentOrigin.Center;
82             _imageView.AnchorPoint = AnchorPoint.Center;
83             _imageView.PixelArea = new Vector4(0.0f, 0.0f, 0.5f, 0.5f);
84             //_imageView.SetResizePolicy(ResizePolicyType.USE_NATURAL_SIZE, DimensionType.ALL_DIMENSIONS);
85             layer.Add(_imageView);
86
87             _pushButton1 = new PushButton();
88             _pushButton1.ParentOrigin = ParentOrigin.BottomLeft;
89             _pushButton1.AnchorPoint = AnchorPoint.BottomLeft;
90             _pushButton1.LabelText = "start animation";
91             _pushButton1.Position = new Vector3(0.0f, stage.Size.Height * 0.1f, 0.0f);
92             _pushButton1.Clicked += OnPushButtonClicked1;
93             _layer1.Add(_pushButton1);
94
95             _pushButton2 = new PushButton();
96             _pushButton2.ParentOrigin = ParentOrigin.BottomLeft;
97             _pushButton2.AnchorPoint = AnchorPoint.BottomLeft;
98             _pushButton2.LabelText = "reload image with same URL";
99             _pushButton2.Position = new Vector3(0.0f, stage.Size.Height * 0.2f, 0.0f);
100             _pushButton2.Clicked += OnPushButtonClicked2;
101             _layer2.Add(_pushButton2);
102
103         }
104
105         public bool OnPushButtonClicked2(object sender, EventArgs e)
106         {
107             if (_imageView)
108             {
109                 Log("OnPushButtonClicked2()!");
110                 layer.Remove(_imageView);
111                 _imageView = new ImageView();
112                 _imageView.ResourceUrl = "./images/gallery-3.jpg";
113                 _imageView.ParentOrigin = ParentOrigin.Center;
114                 _imageView.AnchorPoint = AnchorPoint.Center;
115                 _imageView.PixelArea = new Vector4(0.0f, 0.0f, 0.5f, 0.5f);
116                 //_imageView.SetResizePolicy(ResizePolicyType.USE_NATURAL_SIZE, DimensionType.ALL_DIMENSIONS);
117                 layer.Add(_imageView);
118             }
119
120             return true;
121         }
122
123         public bool OnPushButtonClicked1(object sender, EventArgs e)
124         {
125             if (_isAniFinised == true)
126             {
127                 _isAniFinised = false;
128                 Log("OnPushButtonClicked1()!");
129
130                 // Create a new _animation
131                 if (_animation)
132                 {
133                     //_animation.Stop(Tizen.NUI.Constants.Animation.EndAction.Stop);
134                     _animation.Reset();
135                 }
136
137                 _animation = new Animation();
138                 _animation.AnimateTo(_imageView, "PixelArea", new Vector4(0.5f, 0.0f, 0.5f, 0.5f), 0, 1000);
139                 _animation.AnimateTo(_imageView, "PixelArea", new Vector4(0.5f, 0.5f, 0.5f, 0.5f), 1000, 2000);
140                 _animation.AnimateTo(_imageView, "PixelArea", new Vector4(0.0f, 0.0f, 1.0f, 1.0f), 2000, 3000);
141                 _animation.AnimateTo(_imageView, "PixelArea", new Vector4(0.5f, 0.5f, 0.5f, 0.5f), 3000, 4000);
142
143                 KeyFrames _keyFrames = new KeyFrames();
144                 _keyFrames.Add(0.0f, new Size(0.0f, 0.0f, 0.0f));
145                 _keyFrames.Add(0.3f, new Size(stage.Size.Width * 0.7f, stage.Size.Height * 0.7f, 0.0f));
146                 _keyFrames.Add(1.0f, new Size(stage.Size));
147                 _animation.AnimateBetween(_imageView, "Size", _keyFrames, 4000, 6000, Animation.Interpolation.Linear);
148
149                 _animation.EndAction = Animation.EndActions.Discard;
150
151                 // Connect the signal callback for animaiton finished signal
152                 _animation.Finished += AnimationFinished;
153                 _animation.Finished += AnimationFinished2;
154
155                 // Play the _animation
156                 _animation.Play();
157             }
158
159             return true;
160         }
161
162         // Callback for _animation finished signal handling
163         public void AnimationFinished(object sender, EventArgs e)
164         {
165             Log("AnimationFinished()!");
166         }
167
168         // Callback for second _animation finished signal handling
169         public void AnimationFinished2(object sender, EventArgs e)
170         {
171             Log("AnimationFinished2()!");
172             if (_animation)
173             {
174                 Log("Duration= " + _animation.Duration);
175                 Log("EndAction= " + _animation.EndAction);
176                 _isAniFinised = true;
177             }
178         }
179
180         public void OnStageEventProcessingFinished(object sender, EventArgs e)
181         {
182             Log("OnStageEventProcessingFinished()!");
183             if (e != null)
184             {
185                 Log("e != null !");
186             }
187         }
188
189         public void OnStageKeyPressed(object sender, Stage.KeyEventArgs e)
190         {
191             Log("OnStageKeyEventOccured()!");
192             Log("keyPressedName=" + e.Key.KeyPressedName);
193             Log("state=" + e.Key.State);
194         }
195
196         public void OnStageWheelMoved(object sender, Stage.WheelEventArgs e)
197         {
198             Log("OnStageWheelEventOccured()!");
199             Log("direction=" + e.Wheel.Direction);
200             Log("type=" + e.Wheel.Type);
201         }
202
203         // Callback for stage touched signal handling
204         public void OnStageTouched(object sender, Stage.TouchEventArgs e)
205         {
206             Log("OnStageTouched()! e.TouchData.GetState(0)=" + e.Touch.GetState(0));
207         }
208
209         /// <summary>
210         /// The main entry point for the application.
211         /// </summary>
212         [STAThread]
213         static void _Main(string[] args)
214         {
215             Log("Main() called!");
216
217             Example example = new Example();
218             example.Run(args);
219         }
220     }
221 }