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
341 lines (282 loc) · 11 KB
/
MapControl.cs
File metadata and controls
341 lines (282 loc) · 11 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
// Copyright (c) The Mapsui authors.
// The Mapsui authors licensed this file under the MIT license.
// See the LICENSE file in the project root for full license information.
using Mapsui.Extensions;
using Mapsui.Logging;
using Mapsui.Manipulations;
using Mapsui.UI.WinUI.Extensions;
using Microsoft.UI;
using Microsoft.UI.Input;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Shapes;
using Windows.Devices.Sensors;
using Windows.Foundation;
using Windows.System;
using LogLevel = Mapsui.Logging.LogLevel;
#if __WINUI__
// for fixing the Linux build this pragma disable is needed some tooling issue.
#pragma warning disable IDE0005 // Using directive is unnecessary.
using System;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml;
#endif
#pragma warning disable Uno0001 // PointerWheelChanged is not implemented in Uno. Justification: This is not implemented in all platforms. Also see: https://github.com/unoplatform/uno/issues/15629
namespace Mapsui.UI.WinUI;
public partial class MapControl : Grid, IMapControl, IDisposable
{
// GPU does not work currently on Windows
public static bool UseGPU = OperatingSystem.IsBrowser() || OperatingSystem.IsAndroid(); // Works not on iPhone Mini;
#pragma warning disable IDISP002 // These should not be disposed here in WINUI they are not disposable and in UNO They shouldn't be disposed
private readonly SKSwapChainPanel? _canvasGpu;
private readonly Rectangle _selectRectangle = CreateSelectRectangle();
private readonly SKXamlCanvas? _canvas;
#pragma warning restore IDISP002
bool _shiftPressed;
public MapControl()
{
// The commented out code crashes the app when MouseWheelAnimation.Duration > 0. Could be a bug in SKXamlCanvas
//if (Dispatcher.HasThreadAccess) _canvas?.Invalidate();
//else RunOnUIThread(() => _canvas?.Invalidate());
Background = new SolidColorBrush(Colors.White); // DON'T REMOVE! Touch events do not work without a background
if (UseGPU)
{
_canvasGpu = CreateGpuRenderTarget();
Children.Add(_canvasGpu);
_canvasGpu.PaintSurface += CanvasGpu_PaintSurface;
}
else
{
_canvas = CreateRenderTarget();
Children.Add(_canvas);
_canvas.PaintSurface += Canvas_PaintSurface;
}
// The Canvas needs to be first set before calling the Shared Constructor or else it crashes in the InvalidateCanvas
SharedConstructor();
Children.Add(_selectRectangle);
Loaded += MapControlLoaded;
SizeChanged += MapControlSizeChanged;
ManipulationMode = ManipulationModes.Scale | ManipulationModes.TranslateX | ManipulationModes.TranslateY | ManipulationModes.Rotate;
ManipulationInertiaStarting += OnManipulationInertiaStarting;
ManipulationDelta += OnManipulationDelta;
ManipulationCompleted += OnManipulationCompleted;
PointerPressed += MapControl_PointerPressed;
PointerMoved += MapControl_PointerMoved;
PointerReleased += MapControl_PointerReleased;
PointerWheelChanged += MapControl_PointerWheelChanged;
KeyDown += MapControl_KeyDown;
KeyUp += MapControl_KeyUp;
var orientationSensor = SimpleOrientationSensor.GetDefault();
if (orientationSensor != null)
orientationSensor.OrientationChanged += (s, e) => RunOnUIThread(() => Refresh());
}
public void InvalidateCanvas()
{
if (_canvasGpu is not null)
RunOnUIThread(_canvasGpu.Invalidate);
else if (_canvas is not null)
RunOnUIThread(_canvas.Invalidate);
else
throw new Exception("No canvas was assigned. This is unexpected.");
}
private void MapControl_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Shift)
{
_shiftPressed = true;
}
}
private void MapControl_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Shift)
{
_shiftPressed = false;
}
}
private void OnManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
RefreshData();
}
private void MapControl_PointerPressed(object sender, PointerRoutedEventArgs e)
{
var screenPosition = e.GetCurrentPoint(this).Position.ToScreenPosition();
if (OnPointerPressed([screenPosition]))
return;
}
private void MapControl_PointerMoved(object sender, PointerRoutedEventArgs e)
{
// This is a bit weird. The OnManipulationDelta event fires on both touch and mouse events
// and deals with both properly, except for mouse hover events. This handler only deals with
// hover events.
if (!IsHovering(e))
return;
var position = e.GetCurrentPoint(this).Position.ToScreenPosition();
if (OnPointerMoved([position], true)) // Only for hover events
return;
RefreshGraphics(); // Todo: Figure out if we really need to refresh the graphics here. It might be better to only do this when the map is actually changed. In that case it should perhaps be done in the users handler to OnMapPointerMoved
}
private void MapControl_PointerReleased(object sender, PointerRoutedEventArgs e)
{
var screenPosition = e.GetCurrentPoint(this).Position.ToScreenPosition();
OnPointerReleased([screenPosition]);
}
private bool IsHovering(PointerRoutedEventArgs e)
{
if (e.Pointer.PointerDeviceType == PointerDeviceType.Touch)
return false;
return !e.GetCurrentPoint(this).Properties.IsLeftButtonPressed;
}
private static Rectangle CreateSelectRectangle()
{
return new Rectangle
{
Fill = new SolidColorBrush(Colors.Red),
Stroke = new SolidColorBrush(Colors.Black),
StrokeThickness = 3,
RadiusX = 0.5,
RadiusY = 0.5,
StrokeDashArray = [3.0],
Opacity = 0.3,
VerticalAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Left,
Visibility = Visibility.Collapsed
};
}
private static SKXamlCanvas CreateRenderTarget()
{
return new SKXamlCanvas
{
VerticalAlignment = VerticalAlignment.Stretch,
HorizontalAlignment = HorizontalAlignment.Stretch,
Background = new SolidColorBrush(Colors.Transparent)
};
}
private static SKSwapChainPanel CreateGpuRenderTarget()
{
return new SKSwapChainPanel
{
VerticalAlignment = VerticalAlignment.Stretch,
HorizontalAlignment = HorizontalAlignment.Stretch,
Background = new SolidColorBrush(Colors.Transparent)
};
}
private void MapControl_PointerWheelChanged(object sender, PointerRoutedEventArgs e)
{
var mousePointerPoint = e.GetCurrentPoint(this);
var mouseWheelDelta = mousePointerPoint.Properties.MouseWheelDelta;
Map.Navigator.MouseWheelZoom(mouseWheelDelta, mousePointerPoint.ToScreenPosition());
e.Handled = true;
}
private void MapControlLoaded(object sender, RoutedEventArgs e)
{
SharedOnSizeChanged(ActualWidth, ActualHeight);
}
private void MapControlSizeChanged(object sender, SizeChangedEventArgs e)
{
// Accessing ActualWidth and ActualHeight before SizeChange results in a com exception.
Clip = new RectangleGeometry { Rect = new Rect(0, 0, ActualWidth, ActualHeight) };
SharedOnSizeChanged(ActualWidth, ActualHeight);
}
private void RunOnUIThread(Action action)
{
Catch.TaskRun(() => DispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal, () =>
{
try
{
action();
}
catch (Exception ex)
{
Logger.Log(LogLevel.Error, ex.Message, ex);
}
}));
}
private void Canvas_PaintSurface(object? sender, SKPaintSurfaceEventArgs e)
{
if (GetPixelDensity() is not float pixelDensity)
return;
var canvas = e.Surface.Canvas;
canvas.Scale(pixelDensity, pixelDensity);
_renderController?.Render(canvas);
}
private void CanvasGpu_PaintSurface(object? sender, SKPaintGLSurfaceEventArgs e)
{
if (GetPixelDensity() is not float pixelDensity)
return;
var canvas = e.Surface.Canvas;
canvas.Scale(pixelDensity, pixelDensity);
_renderController?.Render(canvas);
}
private static void OnManipulationInertiaStarting(object sender, ManipulationInertiaStartingRoutedEventArgs e)
{
e.TranslationBehavior.DesiredDeceleration = 25 * 96.0 / (1000.0 * 1000.0);
}
private void OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
var manipulation = ToManipulation(e);
if (OnPointerMoved([manipulation.Center], false))
return;
Map.Navigator.Manipulate(ToManipulation(e));
RefreshGraphics();
}
private Manipulation ToManipulation(ManipulationDeltaRoutedEventArgs e)
{
var previousCenter = TransformToVisual(this).Inverse.TransformPoint(e.Position).ToScreenPosition();
var center = previousCenter.Offset(e.Delta.Translation.X, e.Delta.Translation.Y);
return new Manipulation(center, previousCenter, e.Delta.Scale, e.Delta.Rotation, e.Cumulative.Rotation);
}
public void OpenInBrowser(string url)
{
Catch.TaskRun(async () => await Launcher.LaunchUriAsync(new Uri(url)));
}
public float? GetPixelDensity()
{
var canvasWidth = UseGPU ? _canvasGpu!.CanvasSize.Width : _canvas!.CanvasSize.Width;
var canvasActualWidth = UseGPU ? _canvasGpu!.ActualWidth : _canvas!.ActualWidth;
if (canvasWidth <= 0 || canvasActualWidth <= 0)
return null;
return (float)(canvasWidth / canvasActualWidth);
}
private bool GetShiftPressed() => _shiftPressed;
#if !HAS_UNO
protected virtual void Dispose(bool disposing)
{
SharedDispose(disposing);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#elif HAS_UNO && __IOS__ // on ios don't dispose _canvas, _canvasGPU, _selectRectangle, base class
protected new virtual void Dispose(bool disposing)
{
SharedDispose(disposing);
}
public new void Dispose()
{
GC.SuppressFinalize(this);
}
#else
protected virtual void Dispose(bool disposing)
{
CommonUnoDispose(disposing);
SharedDispose(disposing);
base.Dispose();
}
public new void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void CommonUnoDispose(bool disposing)
{
if (disposing)
{
_canvas?.Dispose();
_canvasGpu?.Dispose();
_selectRectangle?.Dispose();
}
}
#endif
}