[dali_1.2.40] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / examples / image-view.cs
1 /** Copyright (c) 2017 Samsung Electronics Co., Ltd.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16
17 using System;
18 using System.Runtime.InteropServices;
19 using Dali;
20 using Dali.Constants;
21
22 namespace ImageViewExample
23 {
24
25     class Example
26     {
27         public static void Log(string str)
28         {
29             Console.WriteLine("[DALI C# SAMPLE] " + str);
30         }
31
32         private Dali.Application _application;
33         private Animation _animation;
34         private ImageView _imageView;
35         private bool _isAniFinised = true;
36         private Layer layer, _layer1, _layer2;
37         private PushButton _pushButton1, _pushButton2;
38         private Stage stage;
39
40         public Example(Dali.Application application)
41         {
42             _application = application;
43             _application.Initialized += Initialize;
44         }
45
46         public void Initialize(object source, NUIApplicationInitEventArgs e)
47         {
48             Log("Customized Application Initialize event handler");
49             stage = Stage.Instance;
50             stage.BackgroundColor = Color.Cyan;
51             stage.Touch += OnStageTouched;
52             stage.Wheel += OnStageWheelMoved;
53             stage.Key += OnStageKeyPressed;
54             //stage.EventProcessingFinished += OnStageEventProcessingFinished;
55
56             layer = stage.GetDefaultLayer();
57             _layer1 = new Layer();
58             _layer2 = new Layer();
59             stage.AddLayer(_layer1);
60             stage.AddLayer(_layer2);
61             Log("_layer1.Behavior =" + _layer1.Behavior);
62             if (_layer1.Behavior == Layer.LayerBehavior.LAYER_UI)
63             {
64                 _layer1.Behavior = Layer.LayerBehavior.LAYER_2D;
65                 Log("again _layer1.Behavior =" + _layer1.Behavior);
66             }
67             // Add a ImageView to the stage
68             _imageView = new ImageView();
69             _imageView.ResourceUrl = "./images/gallery-3.jpg";
70             _imageView.ParentOrigin = ParentOrigin.Center;
71             _imageView.AnchorPoint = AnchorPoint.Center;
72             _imageView.PixelArea = new Vector4(0.0f, 0.0f, 0.5f, 0.5f);
73             //_imageView.SetResizePolicy(ResizePolicyType.USE_NATURAL_SIZE, DimensionType.ALL_DIMENSIONS);
74             layer.Add(_imageView);
75
76             _pushButton1 = new PushButton();
77             _pushButton1.ParentOrigin = ParentOrigin.BottomLeft;
78             _pushButton1.AnchorPoint = AnchorPoint.BottomLeft;
79             _pushButton1.LabelText = "start animation";
80             _pushButton1.Position = new Vector3(0.0f, stage.Size.Height * 0.1f, 0.0f);
81             _pushButton1.Clicked += OnPushButtonClicked1;
82             _layer1.Add(_pushButton1);
83
84             _pushButton2 = new PushButton();
85             _pushButton2.ParentOrigin = ParentOrigin.BottomLeft;
86             _pushButton2.AnchorPoint = AnchorPoint.BottomLeft;
87             _pushButton2.LabelText = "reload image with same URL";
88             _pushButton2.Position = new Vector3(0.0f, stage.Size.Height * 0.2f, 0.0f);
89             _pushButton2.Clicked += OnPushButtonClicked2;
90             _layer2.Add(_pushButton2);
91
92         }
93
94         public bool OnPushButtonClicked2(object sender, Button.ClickedEventArgs e)
95         {
96             if (_imageView)
97             {
98                 Log("OnPushButtonClicked2()!");
99                 layer.Remove(_imageView);
100                 _imageView = new ImageView();
101                 _imageView.ResourceUrl = "./images/gallery-3.jpg";
102                 _imageView.ParentOrigin = ParentOrigin.Center;
103                 _imageView.AnchorPoint = AnchorPoint.Center;
104                 _imageView.PixelArea = new Vector4(0.0f, 0.0f, 0.5f, 0.5f);
105                 //_imageView.SetResizePolicy(ResizePolicyType.USE_NATURAL_SIZE, DimensionType.ALL_DIMENSIONS);
106                 layer.Add(_imageView);
107             }
108
109             return true;
110         }
111
112
113         public bool OnPushButtonClicked1(object sender, Button.ClickedEventArgs e)
114         {
115             if (_isAniFinised == true)
116             {
117                 _isAniFinised = false;
118                 Log("OnPushButtonClicked1()!");
119
120                 // Create a new _animation
121                 if (_animation)
122                 {
123                     //_animation.Stop(Dali.Constants.Animation.EndAction.Stop);
124                     _animation.Reset();
125                 }
126
127                 _animation = new Animation();
128                 _animation.StartTime = 0;
129                 _animation.EndTime = 1000;
130                 _animation.TargetProperty = "PixelArea";
131                 _animation.Destination = new Vector4(0.5f, 0.0f, 0.5f, 0.5f);
132                 _animation.AnimateTo(_imageView);
133
134                 _animation.StartTime = 1000;
135                 _animation.EndTime = 2000;
136                 _animation.TargetProperty = "PixelArea";
137                 _animation.Destination = new Vector4(0.5f, 0.5f, 0.5f, 0.5f);
138                 _animation.AnimateTo(_imageView);
139
140                 _animation.StartTime = 2000;
141                 _animation.EndTime = 3000;
142                 _animation.TargetProperty = "PixelArea";
143                 _animation.Destination = new Vector4(0.0f, 0.0f, 1.0f, 1.0f);
144                 _animation.AnimateTo(_imageView);
145
146                 _animation.StartTime = 3000;
147                 _animation.EndTime = 4000;
148                 _animation.TargetProperty = "PixelArea";
149                 _animation.Destination = new Vector4(0.5f, 0.5f, 0.5f, 0.5f);
150                 _animation.AnimateTo(_imageView);
151
152                 _animation.StartTime = 4000;
153                 _animation.EndTime = 6000;
154                 _animation.TargetProperty = "Size";
155                 KeyFrames _keyFrames = new KeyFrames();
156                 _keyFrames.Add(0.0f, new Size(0.0f, 0.0f, 0.0f));
157                 _keyFrames.Add(0.3f, new Size((stage.Size * 0.7f)));
158                 _keyFrames.Add(1.0f, new Size(stage.Size));
159                 _animation.AnimateBetween(_imageView, _keyFrames, Animation.Interpolation.Linear);
160
161                 _animation.EndAction = Animation.EndActions.Discard;
162
163                 // Connect the signal callback for animaiton finished signal
164                 _animation.Finished += AnimationFinished;
165                 _animation.Finished += AnimationFinished2;
166
167                 // Play the _animation
168                 _animation.Play();
169             }
170
171             return true;
172         }
173
174         // Callback for _animation finished signal handling
175         public void AnimationFinished(object sender, EventArgs e)
176         {
177             Log("AnimationFinished()!");
178         }
179
180         // Callback for second _animation finished signal handling
181         public void AnimationFinished2(object sender, EventArgs e)
182         {
183             Log("AnimationFinished2()!");
184             if (_animation)
185             {
186                 Log("Duration= " + _animation.Duration);
187                 Log("EndAction= " + _animation.EndAction);
188                 _isAniFinised = true;
189             }
190         }
191
192         public void OnStageEventProcessingFinished(object sender, EventArgs e)
193         {
194             Log("OnStageEventProcessingFinished()!");
195             if (e != null)
196             {
197                 Log("e != null !");
198             }
199         }
200
201         public void OnStageKeyPressed(object sender, Stage.KeyEventArgs e)
202         {
203             Log("OnStageKeyEventOccured()!");
204             Log("keyPressedName=" + e.Key.KeyPressedName);
205             Log("state=" + e.Key.State);
206         }
207
208         public void OnStageWheelMoved(object sender, Stage.WheelEventArgs e)
209         {
210             Log("OnStageWheelEventOccured()!");
211             Log("direction=" + e.Wheel.Direction);
212             Log("type=" + e.Wheel.Type);
213         }
214
215         // Callback for stage touched signal handling
216         public void OnStageTouched(object sender, Stage.TouchEventArgs e)
217         {
218             Log("OnStageTouched()! e.TouchData.GetState(0)=" + e.Touch.GetState(0));
219         }
220
221         public void MainLoop()
222         {
223             _application.MainLoop();
224         }
225
226         /// <summary>
227         /// The main entry point for the application.
228         /// </summary>
229         [STAThread]
230         static void Main(string[] args)
231         {
232             Log("Main() called!");
233
234             Example example = new Example(Application.NewApplication());
235             example.MainLoop();
236         }
237     }
238 }
239