forked from Mapsui/Mapsui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapControl.cs
More file actions
633 lines (500 loc) · 21 KB
/
Copy pathMapControl.cs
File metadata and controls
633 lines (500 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
using Mapsui.Rendering;
using Mapsui.UI.Utils;
using SkiaSharp;
using SkiaSharp.Views.Forms;
using System;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;
namespace Mapsui.UI.Forms
{
/// <summary>
/// Class, that uses the API of all other Mapsui MapControls
/// </summary>
public partial class MapControl : SKGLView, IMapControl, IDisposable
{
class TouchEvent
{
public long Id { get; }
public Geometries.Point Location { get; }
public long Tick { get; }
public TouchEvent(long id, Geometries.Point screenPosition, long tick)
{
Id = id;
Location = screenPosition;
Tick = tick;
}
}
private const int None = 0;
private const int Dragging = 1;
private const int Zooming = 2;
// See http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.4_r2.1/android/view/ViewConfiguration.java#ViewConfiguration.0PRESSED_STATE_DURATION for values
private const int shortTap = 125;
private const int shortClick = 250;
private const int delayTap = 200;
private const int longTap = 500;
private float _skiaScale;
private double _innerRotation;
private Dictionary<long, TouchEvent> _touches = new Dictionary<long, TouchEvent>();
private Geometries.Point _firstTouch;
private System.Threading.Timer _doubleTapTestTimer;
private int _numOfTaps = 0;
private Dictionary<long, int> _fingers = new Dictionary<long, int>(20);
private VelocityTracker _velocityTracker = new VelocityTracker();
private Geometries.Point _previousCenter;
/// <summary>
/// Saver for angle before last pinch movement
/// </summary>
private double _previousAngle;
/// <summary>
/// Saver for radius before last pinch movement
/// </summary>
private double _previousRadius = 1f;
private TouchMode _mode;
public MapControl()
{
Initialize();
}
public float SkiaScale => _skiaScale;
public float PixelDensity => SkiaScale;
public float ScreenWidth => (float)this.Width;
public float ScreenHeight => (float)this.Height;
private float ViewportWidth => ScreenWidth;
private float ViewportHeight => ScreenHeight;
public ISymbolCache SymbolCache => _renderer.SymbolCache;
public float PixelsPerDeviceIndependentUnit => SkiaScale;
public bool UseDoubleTap = true;
public void Initialize()
{
Map = new Map();
BackgroundColor = Color.White;
EnableTouchEvents = true;
PaintSurface += OnPaintSurface;
Touch += OnTouch;
SizeChanged += OnSizeChanged;
}
private void OnSizeChanged(object sender, EventArgs e)
{
SetViewportSize();
}
private void OnTouch(object sender, SKTouchEventArgs e)
{
// Save time, when the event occures
long ticks = DateTime.Now.Ticks;
var location = GetScreenPosition(e.Location);
// Get finger/handler for this event
if (!_fingers.Keys.Contains(e.Id))
if (_fingers.Count < 10)
{
_fingers.Add(e.Id, _fingers.Count);
}
var id = _fingers[e.Id];
if (e.ActionType == SKTouchAction.Pressed)
{
_firstTouch = location;
_touches[id] = new TouchEvent(id, location, ticks);
_velocityTracker.Clear();
// Do we have a doubleTapTestTimer running?
// If yes, stop it and increment _numOfTaps
if (_doubleTapTestTimer != null)
{
_doubleTapTestTimer.Dispose();
_doubleTapTestTimer = null;
_numOfTaps++;
}
else
_numOfTaps = 1;
e.Handled = OnTouchStart(_touches.Select(t => t.Value.Location).ToList());
}
if (e.ActionType == SKTouchAction.Released)
{
// Delete e.Id from _fingers, because finger is released
_fingers.Remove(e.Id);
double velocityX;
double velocityY;
(velocityX, velocityY) = _velocityTracker.CalcVelocity(id, ticks);
// Is this a fling or swipe?
if (velocityX > 10000 || velocityY > 10000)
{
System.Diagnostics.Debug.WriteLine($"Velocity X = {velocityX}, Velocity Y = {velocityY}");
e.Handled = OnFlinged(velocityX, velocityY);
}
// Do we have a tap event
if (_touches.Count == 0 || _touches[id] == null)
{
e.Handled = false;
return;
}
if (_touches[id].Location.Equals(_firstTouch) && ticks - _touches[id].Tick < (e.DeviceType == SKTouchDeviceType.Mouse ? shortClick : longTap) * 10000)
{
// Start a timer with timeout delayTap ms. If than isn't arrived another tap, than it is a single
_doubleTapTestTimer = new System.Threading.Timer((l) =>
{
if (_numOfTaps > 1)
{
if (!e.Handled)
e.Handled = OnDoubleTapped(location, _numOfTaps);
}
else
if (!e.Handled)
e.Handled = OnSingleTapped((Geometries.Point)l);
_numOfTaps = 1;
if (_doubleTapTestTimer != null)
{
_doubleTapTestTimer.Dispose();
}
_doubleTapTestTimer = null;
}, location, UseDoubleTap ? delayTap : 0, -1);
}
else if (_touches[id].Location.Equals(_firstTouch) && ticks - _touches[id].Tick >= longTap * 10000)
{
if (!e.Handled)
e.Handled = OnLongTapped(location);
}
var releasedTouch = _touches[id];
_touches.Remove(id);
if (!e.Handled)
e.Handled = OnTouchEnd(_touches.Select(t => t.Value.Location).ToList(), releasedTouch.Location);
}
if (e.ActionType == SKTouchAction.Moved)
{
_touches[id] = new TouchEvent(id, location, ticks);
if (e.InContact)
_velocityTracker.AddEvent(id, location, ticks);
if (e.InContact && !e.Handled)
e.Handled = OnTouchMove(_touches.Select(t => t.Value.Location).ToList());
else
e.Handled = OnHovered(_touches.Select(t => t.Value.Location).FirstOrDefault());
}
}
void OnPaintSurface(object sender, SKPaintGLSurfaceEventArgs skPaintSurfaceEventArgs)
{
_skiaScale = (float)(CanvasSize.Width / Width);
skPaintSurfaceEventArgs.Surface.Canvas.Scale(_skiaScale, _skiaScale);
_renderer.Render(skPaintSurfaceEventArgs.Surface.Canvas,
Viewport, _map.Layers, _map.Widgets, _map.BackColor);
}
private Geometries.Point GetScreenPosition(SKPoint point)
{
return new Geometries.Point(point.X / _skiaScale, point.Y / _skiaScale);
}
public void RefreshGraphics()
{
RunOnUIThread(() => InvalidateSurface());
}
/// <summary>
/// Event handlers
/// </summary>
/// <summary>
/// TouchStart is called, when user press a mouse button or touch the display
/// </summary>
public event EventHandler<TouchedEventArgs> TouchStarted;
/// <summary>
/// TouchEnd is called, when user release a mouse button or doesn't touch display anymore
/// </summary>
public event EventHandler<TouchedEventArgs> TouchEnded;
/// <summary>
/// TouchMove is called, when user move mouse over map (independent from mouse button state) or move finger on display
/// </summary>
#if __WPF__
public new event EventHandler<TouchedEventArgs> TouchMove;
#else
public event EventHandler<TouchedEventArgs> TouchMove;
#endif
/// <summary>
/// Hover is called, when user move mouse over map without pressing mouse button
/// </summary>
#if __ANDROID__
public new event EventHandler<HoveredEventArgs> Hovered;
#else
public event EventHandler<HoveredEventArgs> Hovered;
#endif
/// <summary>
/// Swipe is called, when user release mouse button or lift finger while moving with a certain speed
/// </summary>
public event EventHandler<SwipedEventArgs> Swipe;
/// <summary>
/// Fling is called, when user release mouse button or lift finger while moving with a certain speed, higher than speed of swipe
/// </summary>
public event EventHandler<SwipedEventArgs> Fling;
/// <summary>
/// SingleTap is called, when user clicks with a mouse button or tap with a finger on map
/// </summary>
public event EventHandler<TappedEventArgs> SingleTap;
/// <summary>
/// LongTap is called, when user clicks with a mouse button or tap with a finger on map for 500 ms
/// </summary>
public event EventHandler<TappedEventArgs> LongTap;
/// <summary>
/// DoubleTap is called, when user clicks with a mouse button or tap with a finger two or more times on map
/// </summary>
public event EventHandler<TappedEventArgs> DoubleTap;
/// <summary>
/// Zoom is called, when map should be zoomed
/// </summary>
public event EventHandler<ZoomedEventArgs> Zoomed;
/// <summary>
/// Called, when map should zoom out
/// </summary>
/// <param name="screenPosition">Center of zoom out event</param>
private bool OnZoomOut(Geometries.Point screenPosition)
{
if (Map.ZoomLock)
{
return true;
}
var args = new ZoomedEventArgs(screenPosition, ZoomDirection.ZoomOut);
Zoomed?.Invoke(this, args);
if (args.Handled)
return true;
// TODO
// Perform standard behavior
return true;
}
/// <summary>
/// Called, when map should zoom in
/// </summary>
/// <param name="screenPosition">Center of zoom in event</param>
private bool OnZoomIn(Geometries.Point screenPosition)
{
if (Map.ZoomLock)
{
return true;
}
var args = new ZoomedEventArgs(screenPosition, ZoomDirection.ZoomIn);
Zoomed?.Invoke(this, args);
if (args.Handled)
return true;
// TODO
// Perform standard behavior
return true;
}
/// <summary>
/// Called, when mouse/finger/pen hovers around
/// </summary>
/// <param name="screenPosition">Actual position of mouse/finger/pen</param>
private bool OnHovered(Geometries.Point screenPosition)
{
var args = new HoveredEventArgs(screenPosition);
Hovered?.Invoke(this, args);
return args.Handled;
}
/// <summary>
/// Called, when mouse/finger/pen swiped over map
/// </summary>
/// <param name="velocityX">Velocity in x direction in pixel/second</param>
/// <param name="velocityY">Velocity in y direction in pixel/second</param>
private bool OnSwiped(double velocityX, double velocityY)
{
var args = new SwipedEventArgs(velocityX, velocityY);
Swipe?.Invoke(this, args);
// TODO
// Perform standard behavior
return args.Handled;
}
/// <summary>
/// Called, when mouse/finger/pen flinged over map
/// </summary>
/// <param name="velocityX">Velocity in x direction in pixel/second</param>
/// <param name="velocityY">Velocity in y direction in pixel/second</param>
private bool OnFlinged(double velocityX, double velocityY)
{
var args = new SwipedEventArgs(velocityX, velocityY);
Fling?.Invoke(this, args);
// TODO
// Perform standard behavior
return args.Handled;
}
/// <summary>
/// Called, when mouse/finger/pen click/touch map
/// </summary>
/// <param name="touchPoints">List of all touched points</param>
private bool OnTouchStart(List<Geometries.Point> touchPoints)
{
var args = new TouchedEventArgs(touchPoints);
TouchStarted?.Invoke(this, args);
if (args.Handled)
return true;
if (touchPoints.Count >= 2)
{
(_previousCenter, _previousRadius, _previousAngle) = GetPinchValues(touchPoints);
_mode = TouchMode.Zooming;
_innerRotation = Viewport.Rotation;
}
else
{
_mode = TouchMode.Dragging;
_previousCenter = touchPoints.First();
}
return true;
}
/// <summary>
/// Called, when mouse/finger/pen anymore click/touch map
/// </summary>
/// <param name="touchPoints">List of all touched points</param>
/// <param name="releasedPoint">Released point, which was touched before</param>
private bool OnTouchEnd(List<Geometries.Point> touchPoints, Geometries.Point releasedPoint)
{
var args = new TouchedEventArgs(touchPoints);
TouchEnded?.Invoke(this, args);
// Last touch released
if (touchPoints.Count == 0)
{
RefreshGraphics();
_mode = TouchMode.None;
_map.RefreshData(_viewport.Extent, _viewport.Resolution, true);
}
return args.Handled;
}
/// <summary>
/// Called, when mouse/finger/pen moves over map
/// </summary>
/// <param name="touchPoints">List of all touched points</param>
private bool OnTouchMove(List<Geometries.Point> touchPoints)
{
var args = new TouchedEventArgs(touchPoints);
TouchMove?.Invoke(this, args);
if (args.Handled)
return true;
switch (_mode)
{
case TouchMode.Dragging:
{
if (touchPoints.Count != 1)
return false;
var touchPosition = touchPoints.First();
if (!Map.PanLock && _previousCenter != null && !_previousCenter.IsEmpty())
{
_viewport.Transform(touchPosition, _previousCenter);
RefreshGraphics();
}
_previousCenter = touchPosition;
}
break;
case TouchMode.Zooming:
{
if (touchPoints.Count < 2)
return false;
var (prevCenter, prevRadius, prevAngle) = (_previousCenter, _previousRadius, _previousAngle);
var (center, radius, angle) = GetPinchValues(touchPoints);
double rotationDelta = 0;
if (!Map.RotationLock)
{
_innerRotation += angle - prevAngle;
_innerRotation %= 360;
if (_innerRotation > 180)
_innerRotation -= 360;
else if (_innerRotation < -180)
_innerRotation += 360;
if (Viewport.Rotation == 0 && Math.Abs(_innerRotation) >= Math.Abs(UnSnapRotationDegrees))
rotationDelta = _innerRotation;
else if (Viewport.Rotation != 0)
{
if (Math.Abs(_innerRotation) <= Math.Abs(ReSnapRotationDegrees))
rotationDelta = -Viewport.Rotation;
else
rotationDelta = _innerRotation - Viewport.Rotation;
}
}
_viewport.Transform(center, prevCenter, Map.ZoomLock ? 1 : radius / prevRadius, rotationDelta);
(_previousCenter, _previousRadius, _previousAngle) = (center, radius, angle);
RefreshGraphics();
}
break;
}
return true;
}
/// <summary>
/// Called, when mouse/finger/pen tapped on map 2 or more times
/// </summary>
/// <param name="screenPosition">First clicked/touched position on screen</param>
/// <param name="numOfTaps">Number of taps on map (2 is a double click/tap)</param>
/// <returns>True, if the event is handled</returns>
private bool OnDoubleTapped(Geometries.Point screenPosition, int numOfTaps)
{
var args = new TappedEventArgs(screenPosition, numOfTaps);
DoubleTap?.Invoke(this, args);
if (args.Handled)
return true;
var eventReturn = InvokeInfo(Map.Layers, Map.GetWidgetsOfMapAndLayers(), Viewport, screenPosition,
screenPosition, _renderer.SymbolCache, WidgetTouched, numOfTaps);
if (eventReturn != null)
{
if (!eventReturn.Handled)
{
// Double tap as zoom
return OnZoomIn(screenPosition);
}
}
return false;
}
/// <summary>
/// Called, when mouse/finger/pen tapped on map one time
/// </summary>
/// <param name="screenPosition">Clicked/touched position on screen</param>
/// <returns>True, if the event is handled</returns>
private bool OnSingleTapped(Geometries.Point screenPosition)
{
var args = new TappedEventArgs(screenPosition, 1);
SingleTap?.Invoke(this, args);
if (args.Handled)
return true;
var eventReturn = InvokeInfo(Map.Layers, Map.GetWidgetsOfMapAndLayers(), Viewport, screenPosition,
screenPosition, _renderer.SymbolCache, WidgetTouched, 1);
if (eventReturn != null)
return eventReturn.Handled;
return false;
}
/// <summary>
/// Called, when mouse/finger/pen tapped long on map
/// </summary>
/// <param name="screenPosition">Clicked/touched position on screen</param>
/// <returns>True, if the event is handled</returns>
private bool OnLongTapped(Geometries.Point screenPosition)
{
var args = new TappedEventArgs(screenPosition, 1);
LongTap?.Invoke(this, args);
return args.Handled;
}
private static (Geometries.Point centre, double radius, double angle) GetPinchValues(List<Geometries.Point> locations)
{
if (locations.Count < 2)
throw new ArgumentException();
double centerX = 0;
double centerY = 0;
foreach (var location in locations)
{
centerX += location.X;
centerY += location.Y;
}
centerX = centerX / locations.Count;
centerY = centerY / locations.Count;
var radius = Geometries.Utilities.Algorithms.Distance(centerX, centerY, locations[0].X, locations[0].Y);
var angle = Math.Atan2(locations[1].Y - locations[0].Y, locations[1].X - locations[0].X) * 180.0 / Math.PI;
return (new Geometries.Point(centerX, centerY), radius, angle);
}
/// <summary>
/// Public functions
/// </summary>
public float GetDeviceIndependentUnits()
{
return SkiaScale;
}
public void OpenBrowser(string url)
{
Device.OpenUri(new Uri(url));
}
private void RunOnUIThread(Action action)
{
Device.BeginInvokeOnMainThread(action);
}
public void Dispose()
{
Unsubscribe();
}
protected void Dispose(bool disposing)
{
Unsubscribe();
}
}
}