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