Tizen.Maps Setup
[platform/core/csapi/tizenfx.git] / src / Tizen.Maps / Tizen.Maps / MapView.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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 using System;
18 using ElmSharp;
19 using Layout = ElmSharp.Layout;
20 using EvasObject = ElmSharp.EvasObject;
21 using System.Collections.Generic;
22
23 namespace Tizen.Maps
24 {
25     public class MapView : Layout
26     {
27         internal Interop.ViewHandle handle;
28         private Interop.ServiceHandle _service;
29
30         private HashSet<MapObject> _children = new HashSet<MapObject>();
31
32         private Interop.View.ViewOnEventCallback _gestureEventCallback;
33         private Interop.View.ViewOnEventCallback _objectEventCallback;
34         private Interop.View.ViewOnEventCallback _viewReadyEventCallback;
35
36         private event EventHandler<MapGestureEventArgs> _scrolledEventHandler;
37         private event EventHandler<MapGestureEventArgs> _zoomedEventHandler;
38         private event EventHandler<MapGestureEventArgs> _tappedEventHandler;
39         private event EventHandler<MapGestureEventArgs> _doubleTappedEventHandler;
40         private event EventHandler<MapGestureEventArgs> _twoFingerTappedEventHandler;
41         private event EventHandler<MapGestureEventArgs> _rotatedEventHandler;
42         private event EventHandler<MapGestureEventArgs> _longPressedEventHandler;
43         private event EventHandler _viewReadyEventHandler;
44
45         public MapView(EvasObject parent, MapService service) : base(parent)
46         {
47             IntPtr nativeHandle;
48             var err = Interop.View.Create(service.handle, this, out nativeHandle);
49             err.ThrowIfFailed("Failed to create native map view handle");
50
51             handle = new Interop.ViewHandle(nativeHandle);
52             _service = service.handle;
53
54             Console.WriteLine($"MapView Created: ServiceHandle: {(IntPtr)_service}, ViewHandle: {(IntPtr)handle}");
55
56             // We need to keep Gesture Tap event enabled for object event to work
57             Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.Tap, true);
58             SetObjectEventCallback();
59         }
60
61         public event EventHandler<MapGestureEventArgs> Scrolled
62         {
63             add
64             {
65                 SetGestureEventCallback();
66                 Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.Scroll, true);
67                 _scrolledEventHandler += value;
68             }
69             remove
70             {
71                 _scrolledEventHandler -= value;
72                 if (_scrolledEventHandler == null)
73                 {
74                     Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.Scroll, false);
75                     UnsetGestureEventCallback();
76                 }
77             }
78         }
79
80         public event EventHandler<MapGestureEventArgs> ZoomChanged
81         {
82             add
83             {
84                 SetGestureEventCallback();
85                 Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.Zoom, true);
86                 _zoomedEventHandler += value;
87             }
88             remove
89             {
90                 _zoomedEventHandler -= value;
91                 if (_zoomedEventHandler == null)
92                 {
93                     Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.Zoom, false);
94                     UnsetGestureEventCallback();
95                 }
96             }
97         }
98
99         public event EventHandler<MapGestureEventArgs> Clicked
100         {
101             add
102             {
103                 SetGestureEventCallback();
104                 //Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.Tap, true);
105                 _tappedEventHandler += value;
106             }
107             remove
108             {
109                 _tappedEventHandler -= value;
110                 if (_tappedEventHandler == null)
111                 {
112                     //Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.Tap, false);
113                     UnsetGestureEventCallback();
114                 }
115             }
116         }
117
118         public event EventHandler<MapGestureEventArgs> DoubleClicked
119         {
120             add
121             {
122                 SetGestureEventCallback();
123                 Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.DoubleTap, true);
124                 _doubleTappedEventHandler += value;
125             }
126             remove
127             {
128                 _doubleTappedEventHandler -= value;
129                 if (_doubleTappedEventHandler == null)
130                 {
131                     Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.DoubleTap, false);
132                     UnsetGestureEventCallback();
133                 }
134             }
135         }
136
137         public event EventHandler<MapGestureEventArgs> TwoFingerPressed
138         {
139             add
140             {
141                 SetGestureEventCallback();
142                 Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.TwoFingerTap, true);
143                 _twoFingerTappedEventHandler += value;
144             }
145             remove
146             {
147                 _twoFingerTappedEventHandler -= value;
148                 if (_twoFingerTappedEventHandler == null)
149                 {
150                     Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.TwoFingerTap, false);
151                     UnsetGestureEventCallback();
152                 }
153             }
154         }
155
156         public event EventHandler<MapGestureEventArgs> Rotated
157         {
158             add
159             {
160                 SetGestureEventCallback();
161                 Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.Rotate, true);
162                 _rotatedEventHandler += value;
163             }
164             remove
165             {
166                 _rotatedEventHandler -= value;
167                 if (_rotatedEventHandler == null)
168                 {
169                     Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.Rotate, false);
170                     UnsetGestureEventCallback();
171                 }
172             }
173         }
174
175         public event EventHandler<MapGestureEventArgs> LongPressed
176         {
177             add
178             {
179                 SetGestureEventCallback();
180                 Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.LongPress, true);
181                 _longPressedEventHandler += value;
182             }
183             remove
184             {
185                 _longPressedEventHandler -= value;
186                 if (_longPressedEventHandler == null)
187                 {
188                     Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.LongPress, false);
189                     UnsetGestureEventCallback();
190                 }
191             }
192         }
193
194         public event EventHandler ViewReady
195         {
196             add
197             {
198                 SetViewReadyEventCallback();
199                 _viewReadyEventHandler += value;
200             }
201             remove
202             {
203                 _viewReadyEventHandler -= value;
204                 UnsetGestureEventCallback();
205             }
206         }
207
208         public int ZoomLevel
209         {
210             get
211             {
212                 int value;
213                 Interop.View.GetZoomLevel(handle, out value);
214                 return value;
215             }
216             set
217             {
218                 Interop.View.SetZoomLevel(handle, value);
219             }
220         }
221
222         public int MinimumZoomLevel
223         {
224             get
225             {
226                 int value;
227                 Interop.View.GetMinZoomLevel(handle, out value);
228                 return value;
229             }
230             set
231             {
232                 Interop.View.SetMinZoomLevel(handle, value);
233             }
234         }
235
236         public int MaximumZoomLevel
237         {
238             get
239             {
240                 int value;
241                 Interop.View.GetMaxZoomLevel(handle, out value);
242                 return value;
243             }
244             set
245             {
246                 Interop.View.SetMaxZoomLevel(handle, value);
247             }
248         }
249
250         public double Orientation
251         {
252             get
253             {
254                 double value;
255                 Interop.View.GetOrientation(handle, out value);
256                 return value;
257             }
258             set
259             {
260                 Interop.View.SetOrientation(handle, value);
261             }
262         }
263
264         public MapTypes MapType
265         {
266             get
267             {
268                 Interop.ViewType value;
269                 Interop.View.GetType(handle, out value);
270                 return (MapTypes)value;
271             }
272             set
273             {
274                 Interop.View.SetType(handle, (Interop.ViewType)value);
275             }
276         }
277
278         public bool BuildingsEnabled
279         {
280             get
281             {
282                 bool value;
283                 Interop.View.GetBuildingsEnabled(handle, out value);
284                 return value;
285             }
286             set
287             {
288                 Interop.View.SetBuildingsEnabled(handle, value);
289             }
290         }
291
292         public bool TrafficEnabled
293         {
294             get
295             {
296                 bool value;
297                 Interop.View.GetTrafficEnabled(handle, out value);
298                 return value;
299             }
300             set
301             {
302                 Interop.View.SetTrafficEnabled(handle, value);
303             }
304         }
305
306         public bool PublicTransitEnabled
307         {
308             get
309             {
310                 bool value;
311                 Interop.View.GetPublicTransitEnabled(handle, out value);
312                 return value;
313             }
314             set
315             {
316                 Interop.View.SetPublicTransitEnabled(handle, value);
317             }
318         }
319
320         public bool ScalebarEnabled
321         {
322             get
323             {
324                 bool value;
325                 Interop.View.GetScalebarEnabled(handle, out value);
326                 return value;
327             }
328             set
329             {
330                 Interop.View.SetScalebarEnabled(handle, value);
331             }
332         }
333
334         public string Language
335         {
336             get
337             {
338                 string value;
339                 Interop.View.GetLanguage(handle, out value);
340                 return value;
341             }
342             set
343             {
344                 Interop.View.SetLanguage(handle, value);
345             }
346         }
347
348         public Geocoordinates Center
349         {
350             get
351             {
352                 IntPtr coordinateHandle;
353                 Interop.View.GetCenter(handle, out coordinateHandle);
354                 return new Geocoordinates(coordinateHandle);
355             }
356             set
357             {
358                 Geocoordinates geocoordinate = value;
359                 Interop.View.SetCenter(handle, geocoordinate.handle);
360             }
361         }
362
363         public IEnumerable<MapObject> Children
364         {
365             get
366             {
367                 return _children;
368             }
369         }
370
371         public Point GeolocationToScreen(Geocoordinates coordinates)
372         {
373             Point screenCoordinates = new Point();
374             Geocoordinates geocoordinate = coordinates;
375             Interop.View.GeolocationToScreen(handle, geocoordinate.handle, out screenCoordinates.X, out screenCoordinates.Y);
376             return screenCoordinates;
377         }
378
379         public Geocoordinates ScreenToGeolocation(Point screenCoordinates)
380         {
381             IntPtr coordinateHandle;
382             Interop.View.ScreenToGeolocation(handle, screenCoordinates.X, screenCoordinates.Y, out coordinateHandle);
383             return new Geocoordinates(coordinateHandle);
384         }
385
386         public void Add(MapObject child)
387         {
388             child.handle.ReleaseOwnership();
389             _children.Add(child);
390             child.AddToMapObjectTable();
391             Interop.View.AddObject(handle, child.handle);
392         }
393
394         public void Remove(MapObject child)
395         {
396             _children.Remove(child);
397             child.RemoveFromMapObjectTable();
398             Interop.View.RemoveObject(handle, child.handle);
399         }
400
401         public void RemoveAll()
402         {
403             foreach (var child in _children)
404             {
405                 child.RemoveFromMapObjectTable();
406             }
407             _children.Clear();
408             Interop.View.RemoveAllObjects(handle);
409         }
410
411         public void CaptureSnapshot(SnapshotType type, int quality, string path)
412         {
413             var err = Interop.ViewSnapshot.ViewCaptureSnapshot(handle, (Interop.ViewSnapshotFormatType)type, quality, path);
414             err.ThrowIfFailed("Failed to create snapshot for the view");
415         }
416
417         private void SetGestureEventCallback()
418         {
419             if (_gestureEventCallback == null)
420             {
421                 _gestureEventCallback = (type, eventData, userData) =>
422                 {
423                     if (type != Interop.ViewEventType.Gesture) return;
424                     var eventArg = MapGestureEventArgs.Create(eventData);
425                     switch (eventArg.GestureType)
426                     {
427                         case GestureType.Scroll: _scrolledEventHandler?.Invoke(this, eventArg); break;
428                         case GestureType.Zoom: _zoomedEventHandler?.Invoke(this, eventArg); break;
429                         case GestureType.Tap: _tappedEventHandler?.Invoke(this, eventArg); break;
430                         case GestureType.DoubleTap: _doubleTappedEventHandler?.Invoke(this, eventArg); break;
431                         case GestureType.TwoFingerTap: _twoFingerTappedEventHandler?.Invoke(this, eventArg); break;
432                         case GestureType.Rotate: _rotatedEventHandler?.Invoke(this, eventArg); break;
433                         case GestureType.LongPress: _longPressedEventHandler?.Invoke(this, eventArg); break;
434                     }
435                 };
436                 Interop.View.SetEventCb(handle, Interop.ViewEventType.Gesture, _gestureEventCallback, IntPtr.Zero);
437             }
438         }
439
440         private void UnsetGestureEventCallback()
441         {
442             if (_scrolledEventHandler != null || _zoomedEventHandler != null
443                 || _tappedEventHandler != null || _doubleTappedEventHandler != null
444                 || _twoFingerTappedEventHandler != null || _rotatedEventHandler != null
445                 || _longPressedEventHandler != null)
446             {
447                 return;
448             }
449
450             Interop.View.UnsetEventCb(handle, Interop.ViewEventType.Gesture);
451             _gestureEventCallback = null;
452         }
453
454         private void SetObjectEventCallback()
455         {
456             if (_objectEventCallback == null)
457             {
458                 _objectEventCallback = (type, eventData, userData) =>
459                 {
460                     if (type != Interop.ViewEventType.Object) return;
461                     var eventArg = MapObjectEventArgs.Create(eventData);
462                     switch (eventArg.GestureType)
463                     {
464                         case GestureType.Tap: eventArg.ViewObject.HandleClickedEvent(); break;
465                     }
466                 };
467                 Interop.View.SetEventCb(handle, Interop.ViewEventType.Object, _objectEventCallback, IntPtr.Zero);
468             }
469         }
470
471         private void SetViewReadyEventCallback()
472         {
473             if (_viewReadyEventCallback == null)
474             {
475                 _viewReadyEventCallback = (type, eventData, userData) =>
476                 {
477                     _viewReadyEventHandler?.Invoke(this, EventArgs.Empty);
478                 };
479                 Interop.View.SetEventCb(handle, Interop.ViewEventType.Ready, _viewReadyEventCallback, IntPtr.Zero);
480             }
481         }
482
483         private void UnsetViewReadyEventCallback()
484         {
485             if (_viewReadyEventHandler == null)
486             {
487                 Interop.View.UnsetEventCb(handle, Interop.ViewEventType.Ready);
488                 _viewReadyEventCallback = null;
489             }
490         }
491     }
492 }