-
-
Notifications
You must be signed in to change notification settings - Fork 314
/
MapControl.cs
741 lines (648 loc) · 22.2 KB
/
MapControl.cs
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Mapsui.Extensions;
using Mapsui.Fetcher;
using Mapsui.Layers;
using Mapsui.Logging;
using Mapsui.Rendering;
using Mapsui.Rendering.Skia;
using Mapsui.Utilities;
using Mapsui.Widgets;
#nullable enable
#pragma warning disable IDISP008 // Don't assign member with injected and created disposables
#pragma warning disable IDISP001 // Dispose created.
#pragma warning disable IDISP002 // Dispose member.
#if __MAUI__
using Microsoft.Maui.Controls;
namespace Mapsui.UI.Maui;
#elif __UWP__
namespace Mapsui.UI.Uwp;
#elif __ANDROID__ && !HAS_UNO_WINUI
namespace Mapsui.UI.Android;
#elif __IOS__ && !HAS_UNO_WINUI && !__FORMS__
namespace Mapsui.UI.iOS;
#elif __WINUI__
namespace Mapsui.UI.WinUI;
#elif __FORMS__
namespace Mapsui.UI.Forms;
#elif __AVALONIA__
namespace Mapsui.UI.Avalonia;
#elif __AVALONIA_V0__
namespace Mapsui.UI.Avalonia.V0;
#elif __ETO_FORMS__
namespace Mapsui.UI.Eto;
#elif __BLAZOR__
namespace Mapsui.UI.Blazor;
#else
namespace Mapsui.UI.Wpf;
#endif
public partial class MapControl : INotifyPropertyChanged, IDisposable
{
private double _unSnapRotationDegrees;
// Flag indicating if a drawing process is running
private bool _drawing;
// Flag indicating if the control has to be redrawn
private bool _invalidated;
// Flag indicating if a new drawing process should start
private bool _refresh;
// Action to call for a redraw of the control
private protected Action? _invalidate;
// Timer for loop to invalidating the control
private System.Threading.Timer? _invalidateTimer;
// Interval between two calls of the invalidate function in ms
private int _updateInterval = 16;
// Stopwatch for measuring drawing times
private readonly System.Diagnostics.Stopwatch _stopwatch = new System.Diagnostics.Stopwatch();
// saving list of extended Widgets
private List<IWidgetExtended>? _extendedWidgets;
// old widget Collection to compare if widget Collection was changed.
private ConcurrentQueue<IWidget>? _widgetCollection;
// saving list of touchable Widgets
private List<IWidget>? _touchableWidgets;
// keeps track of the widgets count to see if i need to recalculate the extended widgets.
private int _updateWidget = 0;
// keeps track of the widgets count to see if i need to recalculate the touchable widgets.
private int _updateTouchableWidget;
private protected void CommonInitialize()
{
// Create map
Map = new Map();
// Create timer for invalidating the control
_invalidateTimer?.Dispose();
_invalidateTimer = new System.Threading.Timer(InvalidateTimerCallback, null, System.Threading.Timeout.Infinite, 16);
// Start the invalidation timer
StartUpdates(false);
}
private protected void CommonDrawControl(object canvas)
{
if (_drawing) return;
if (Renderer is null) return;
if (Map is null) return;
if (!Map.Navigator.Viewport.HasSize()) return;
// Start drawing
_drawing = true;
// Start stopwatch before updating animations and drawing control
_stopwatch.Restart();
// All requested updates up to this point will be handled by this redraw
_refresh = false;
Renderer.Render(canvas, Map.Navigator.Viewport, Map.Layers, Map.Widgets, Map.BackColor);
// Stop stopwatch after drawing control
_stopwatch.Stop();
// If we are interested in performance measurements, we save the new drawing time
_performance?.Add(_stopwatch.Elapsed.TotalMilliseconds);
// End drawing
_drawing = false;
_invalidated = false;
}
private void InvalidateTimerCallback(object? state)
{
try
{
// In MAUI if you use binding there is an event where the new value is null even though
// the current value en the value you are binding to are not null. Perhaps this should be
// considered a bug.
if (Map is null) return;
// Check, if we have to redraw the screen
if (Map?.UpdateAnimations() == true)
_refresh = true;
// seems that this could be null sometimes
if (Map?.Navigator?.UpdateAnimations() ?? false)
_refresh = true;
if (!_refresh)
return;
if (_drawing)
{
if (_performance != null)
_performance.Dropped++;
return;
}
if (_invalidated)
{
return;
}
_invalidated = true;
_invalidate?.Invoke();
}
catch (Exception ex)
{
Logger.Log(LogLevel.Error, ex.Message, ex);
}
}
/// <summary>
/// Start updates for control
/// </summary>
/// <remarks>
/// When this function is called, the control is redrawn if needed
/// </remarks>
public void StartUpdates(bool refresh = true)
{
_refresh = refresh;
_invalidateTimer?.Change(0, _updateInterval);
}
/// <summary>
/// Stop updates for control
/// </summary>
/// <remarks>
/// When this function is called, the control stops to redraw itself,
/// even if it is needed
/// </remarks>
public void StopUpdates()
{
_invalidateTimer?.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
}
/// <summary>
/// Force a update of control
/// </summary>
/// <remarks>
/// When this function is called, the control draws itself once
/// </remarks>
public void ForceUpdate()
{
_invalidated = true;
_invalidate?.Invoke();
}
/// <summary>
/// Interval between two redraws of the MapControl in ms
/// </summary>
public int UpdateInterval
{
get => _updateInterval;
set
{
if (value <= 0)
throw new ArgumentOutOfRangeException($"{nameof(UpdateInterval)} must be greater than 0");
if (_updateInterval != value)
{
_updateInterval = value;
StartUpdates();
}
}
}
private Performance? _performance;
/// <summary>
/// Object to save performance information about the drawing of the map
/// </summary>
/// <remarks>
/// If this is null, no performance information is saved.
/// </remarks>
public Performance? Performance
{
get => _performance;
set
{
if (_performance != value)
{
_performance = value;
OnPropertyChanged();
}
}
}
/// <summary>
/// After how many degrees start rotation to take place
/// </summary>
public double UnSnapRotationDegrees
{
get => _unSnapRotationDegrees;
set
{
if (_unSnapRotationDegrees != value)
{
_unSnapRotationDegrees = value;
OnPropertyChanged();
}
}
}
private double _reSnapRotationDegrees;
/// <summary>
/// With how many degrees from 0 should map snap to 0 degrees
/// </summary>
public double ReSnapRotationDegrees
{
get => _reSnapRotationDegrees;
set
{
if (_reSnapRotationDegrees != value)
{
_reSnapRotationDegrees = value;
OnPropertyChanged();
}
}
}
public float PixelDensity => GetPixelDensity();
private IRenderer _renderer = new MapRenderer();
/// <summary>
/// Renderer that is used from this MapControl
/// </summary>
public IRenderer Renderer
{
get => _renderer;
set
{
if (value is null) throw new NullReferenceException(nameof(Renderer));
if (_renderer != value)
{
_renderer = value;
OnPropertyChanged();
}
}
}
/// <summary>
/// Called whenever the map is clicked. The MapInfoEventArgs contain the features that were hit in
/// the layers that have IsMapInfoLayer set to true.
/// </summary>
public event EventHandler<MapInfoEventArgs>? Info;
/// <summary>
/// Called whenever a property is changed
/// </summary>
#if __FORMS__ || __MAUI__ || __AVALONIA__ || __AVALONIA_V0__
public new event PropertyChangedEventHandler? PropertyChanged;
#else
public event PropertyChangedEventHandler? PropertyChanged;
#endif
#if __FORMS__ || __MAUI__
protected override void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#else
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endif
/// <summary>
/// Unsubscribe from map events
/// </summary>
public void Unsubscribe()
{
UnsubscribeFromMapEvents(Map);
}
/// <summary>
/// Subscribe to map events
/// </summary>
/// <param name="map">Map, to which events to subscribe</param>
private void SubscribeToMapEvents(Map map)
{
map.DataChanged += Map_DataChanged;
map.PropertyChanged += Map_PropertyChanged;
map.RefreshGraphicsRequest += Map_RefreshGraphicsRequest;
}
private void Map_RefreshGraphicsRequest(object? sender, EventArgs e)
{
RefreshGraphics();
}
/// <summary>
/// Unsubscribe from map events
/// </summary>
/// <param name="map">Map, to which events to unsubscribe</param>
private void UnsubscribeFromMapEvents(Map map)
{
var localMap = map;
localMap.DataChanged -= Map_DataChanged;
localMap.PropertyChanged -= Map_PropertyChanged;
localMap.RefreshGraphicsRequest -= Map_RefreshGraphicsRequest;
localMap.AbortFetch();
}
public void Refresh(ChangeType changeType = ChangeType.Discrete)
{
Map.Refresh(changeType);
}
public void RefreshGraphics()
{
_refresh = true;
}
private void Map_DataChanged(object? sender, DataChangedEventArgs? e)
{
try
{
if (e == null)
{
Logger.Log(LogLevel.Warning, "Unexpected error: DataChangedEventArgs can not be null");
}
else if (e.Cancelled)
{
Logger.Log(LogLevel.Warning, "Fetching data was cancelled.");
}
else if (e.Error is WebException)
{
Logger.Log(LogLevel.Warning, $"A WebException occurred. Do you have internet? Exception: {e.Error?.Message}", e.Error);
}
else if (e.Error != null)
{
Logger.Log(LogLevel.Warning, $"An error occurred while fetching data. Exception: {e.Error?.Message}", e.Error);
}
else // no problems
{
RefreshGraphics();
}
}
catch (Exception exception)
{
Logger.Log(LogLevel.Warning, $"Unexpected exception in {nameof(Map_DataChanged)}", exception);
}
}
// ReSharper disable RedundantNameQualifier - needed for iOS for disambiguation
private void Map_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(Layers.Layer.Enabled))
{
RefreshGraphics();
}
else if (e.PropertyName == nameof(Layers.Layer.Opacity))
{
RefreshGraphics();
}
else if (e.PropertyName == nameof(Map.BackColor))
{
RefreshGraphics();
}
else if (e.PropertyName == nameof(Layers.Layer.DataSource))
{
Refresh(); // There is a new DataSource so let's fetch the new data.
}
else if (e.PropertyName == nameof(Map.Extent))
{
CallHomeIfNeeded();
Refresh();
}
else if (e.PropertyName == nameof(Map.Layers))
{
CallHomeIfNeeded();
Refresh();
}
}
// ReSharper restore RedundantNameQualifier
public void CallHomeIfNeeded()
{
if (!Map.HomeIsCalledOnce && Map.Navigator.Viewport.HasSize() && Map?.Extent is not null)
{
Map.Home?.Invoke(Map.Navigator);
Map.HomeIsCalledOnce = true;
}
}
#if __MAUI__
public static readonly BindableProperty MapProperty = BindableProperty.Create(nameof(Map),
typeof(Map), typeof(MapControl), default(Map), defaultBindingMode: BindingMode.TwoWay,
propertyChanged: MapPropertyChanged, propertyChanging: MapPropertyChanging);
private static void MapPropertyChanging(BindableObject bindable,
object oldValue, object newValue)
{
var mapControl = (MapControl)bindable;
mapControl.BeforeSetMap();
}
private static void MapPropertyChanged(BindableObject bindable,
object oldValue, object newValue)
{
var mapControl = (MapControl)bindable;
mapControl.AfterSetMap((Map)newValue);
}
public Map Map
{
get => (Map)GetValue(MapProperty);
set => SetValue(MapProperty, value);
}
#else
private Map _map = new Map();
/// <summary>
/// Map holding data for which is shown in this MapControl
/// </summary>
#if __BLAZOR__
[Parameter]
[SuppressMessage("Usage", "BL0007:Component parameters should be auto properties")]
#endif
public Map Map
{
get => _map;
set
{
if (value is null) throw new ArgumentNullException(nameof(value));
BeforeSetMap();
_map = value;
AfterSetMap(_map);
OnPropertyChanged();
}
}
#endif
private void BeforeSetMap()
{
if (Map is null) return; // Although the Map property can not null the map argument can null during initializing and binding.
UnsubscribeFromMapEvents(Map);
}
private void AfterSetMap(Map? map)
{
if (map is null) return; // Although the Map property can not null the map argument can null during initializing and binding.
map.Navigator.SetSize(ViewportWidth, ViewportHeight);
SubscribeToMapEvents(map);
CallHomeIfNeeded();
Refresh();
}
/// <inheritdoc />
public MPoint ToPixels(MPoint coordinateInDeviceIndependentUnits)
{
return new MPoint(
coordinateInDeviceIndependentUnits.X * PixelDensity,
coordinateInDeviceIndependentUnits.Y * PixelDensity);
}
/// <inheritdoc />
public MPoint ToDeviceIndependentUnits(MPoint coordinateInPixels)
{
return new MPoint(coordinateInPixels.X / PixelDensity, coordinateInPixels.Y / PixelDensity);
}
/// <summary>
/// Refresh data of Map, but don't paint it
/// </summary>
public void RefreshData(ChangeType changeType = ChangeType.Discrete)
{
Map.RefreshData(changeType);
}
private protected void OnInfo(MapInfoEventArgs? mapInfoEventArgs)
{
if (mapInfoEventArgs == null) return;
Map?.OnInfo(mapInfoEventArgs); // Also propagate to Map
Info?.Invoke(this, mapInfoEventArgs);
}
/// <inheritdoc />
public MapInfo? GetMapInfo(MPoint? screenPosition, int margin = 0)
{
if (screenPosition == null)
return null;
return Renderer?.GetMapInfo(screenPosition.X, screenPosition.Y, Map.Navigator.Viewport, Map?.Layers ?? new LayerCollection(), margin);
}
/// <inheritdoc />
public byte[] GetSnapshot(IEnumerable<ILayer>? layers = null)
{
using var stream = Renderer.RenderToBitmapStream(Map.Navigator.Viewport, layers ?? Map?.Layers ?? new LayerCollection(), pixelDensity: PixelDensity);
return stream.ToArray();
}
/// <summary>
/// Check if a widget or feature at a given screen position is clicked/tapped
/// </summary>
/// <param name="screenPosition">Screen position to check for widgets and features</param>
/// <param name="startScreenPosition">Screen position of Viewport/MapControl</param>
/// <param name="numTaps">Number of clickes/taps</param>
/// <returns>True, if something done </returns>
private MapInfoEventArgs? CreateMapInfoEventArgs(
MPoint? screenPosition,
MPoint? startScreenPosition,
int numTaps)
{
if (screenPosition == null || startScreenPosition == null)
return null;
// Check which features in the map were tapped.
var mapInfo = Renderer?.GetMapInfo(screenPosition.X, screenPosition.Y, Map.Navigator.Viewport, Map?.Layers ?? new LayerCollection());
if (mapInfo != null)
{
return new MapInfoEventArgs
{
MapInfo = mapInfo,
NumTaps = numTaps,
Handled = false
};
}
return null;
}
private protected void SetViewportSize()
{
var hadSize = Map.Navigator.Viewport.HasSize();
Map.Navigator.SetSize(ViewportWidth, ViewportHeight);
if (!hadSize && Map.Navigator.Viewport.HasSize()) Map.OnViewportSizeInitialized();
CallHomeIfNeeded();
Refresh();
}
private protected void CommonDispose(bool disposing)
{
if (disposing)
{
Unsubscribe();
StopUpdates();
_invalidateTimer?.Dispose();
}
_invalidateTimer = null;
}
private bool HandleWidgetPointerMove(MPoint position, bool leftButton, int clickCount, bool shift)
{
var extendedWidgets = GetExtendedWidgets();
if (extendedWidgets.Count == 0)
return false;
var widgetArgs = new WidgetArgs(clickCount, leftButton, shift);
foreach (var extendedWidget in extendedWidgets)
{
if (extendedWidget.HandleWidgetMoving(Map.Navigator, position, widgetArgs))
return true;
}
return false;
}
private bool HandleTouchingTouched(MPoint position, MPoint? startPosition, bool leftButton, int clickCount, bool shift)
{
bool result = HandleWidgetPointerDown(position, leftButton, clickCount, shift);
if (HandleWidgetPointerUp(position, startPosition, leftButton, clickCount, shift))
{
result = true;
}
return result;
}
private bool HandleWidgetPointerDown(MPoint position, bool leftButton, int clickCount, bool shift)
{
var touchableWidgets = GetTouchableWidgets();
var touchedWidgets = WidgetTouch.GetTouchedWidget(position, position, touchableWidgets);
foreach (var widget in touchedWidgets)
{
if (widget is IWidgetExtended extendedWidget)
{
var widgetArgs = new WidgetArgs(clickCount, leftButton, shift);
if (extendedWidget.HandleWidgetTouching(Map.Navigator, position, widgetArgs))
return true;
}
else
{
// Handle the touched avoid duplicated touched events
return true;
}
}
return false;
}
private bool HandleWidgetPointerUp(MPoint position, MPoint? startPosition, bool leftButton, int clickCount, bool shift)
{
if (startPosition is null)
{
Logger.Log(LogLevel.Error, $"The '{nameof(startPosition)}' is null on release. This is not expected");
return false;
}
var touchableWidgets = GetTouchableWidgets();
var touchedWidgets = WidgetTouch.GetTouchedWidget(position, startPosition, touchableWidgets);
foreach (var widget in touchedWidgets)
{
if (widget is IWidgetExtended extendedWidget)
{
var widgetArgs = new WidgetArgs(clickCount, leftButton, shift);
if (extendedWidget.HandleWidgetTouched(Map.Navigator, position, widgetArgs))
return true;
}
else
{
if (widget.HandleWidgetTouched(Map.Navigator, position))
return true;
else if (widget is Hyperlink hyperlink && !string.IsNullOrWhiteSpace(hyperlink.Url))
{
// The HyperLink is a special case because we need platform specific code to open the
// link in a browser. If the link is not handled within the widget we handle it
// here and return true to indicate this is handled.
OpenBrowser(hyperlink.Url!);
return true;
}
}
}
return false;
}
private List<IWidgetExtended> GetExtendedWidgets()
{
AssureWidgets();
if (_updateWidget != Map.Widgets.Count || _extendedWidgets == null)
{
_updateWidget = Map.Widgets.Count;
_extendedWidgets = new List<IWidgetExtended>();
var widgetsOfMapAndLayers = Map.GetWidgetsOfMapAndLayers().ToList();
foreach (var widget in widgetsOfMapAndLayers)
{
if (widget is IWidgetExtended extendedWidget)
{
_extendedWidgets.Add(extendedWidget);
}
}
}
return _extendedWidgets;
}
private void AssureWidgets()
{
if (_widgetCollection != Map.Widgets)
{
// reset widgets
_extendedWidgets = null;
_touchableWidgets = null;
_widgetCollection = Map.Widgets;
}
}
private List<IWidget> GetTouchableWidgets()
{
AssureWidgets();
if (_updateTouchableWidget != Map.Widgets.Count || _touchableWidgets == null)
{
_updateTouchableWidget = Map.Widgets.Count;
_touchableWidgets = new List<IWidget>();
var touchableWidgets = Map.GetWidgetsOfMapAndLayers().ToList();
foreach (var widget in touchableWidgets)
{
if (widget is IWidgetTouchable { Touchable: false }) continue;
_touchableWidgets.Add(widget);
}
}
return _touchableWidgets;
}
}