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