forked from Mapsui/Mapsui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapControl.cs
309 lines (260 loc) · 10.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.Platform;
using Avalonia.Rendering.SceneGraph;
using Avalonia.Skia;
using Avalonia.Threading;
using Mapsui.Extensions;
using Mapsui.Layers;
using Mapsui.UI.Avalonia.Extensions;
using Mapsui.Utilities;
namespace Mapsui.UI.Avalonia
{
public partial class MapControl : Grid, IMapControl, IDisposable
{
private MPoint? _mousePosition;
private MapsuiCustomDrawOp? _drawOp;
private MPoint? _currentMousePosition;
private MPoint? _downMousePosition;
private bool _mouseDown;
private MPoint? _previousMousePosition;
private double _toResolution = double.NaN;
public event EventHandler<FeatureInfoEventArgs>? FeatureInfo;
public MouseWheelAnimation MouseWheelAnimation { get; } = new() { Duration = 0 };
public MapControl()
{
ClipToBounds = true;
CommonInitialize();
Initialize();
}
private void Initialize()
{
_invalidate = () => { RunOnUIThread(InvalidateVisual); };
Initialized += MapControlInitialized;
PointerPressed += MapControl_PointerPressed;
PointerReleased += MapControl_PointerReleased;
PointerMoved += MapControlMouseMove;
PointerLeave += MapControlMouseLeave;
PointerWheelChanged += MapControlMouseWheel;
DoubleTapped += OnDoubleTapped;
}
protected override void OnPropertyChanged<T>(AvaloniaPropertyChangedEventArgs<T> change)
{
switch (change.Property.Name)
{
case nameof(Bounds):
// size changed
MapControlSizeChanged();
break;
}
}
private void MapControl_PointerPressed(object? sender, PointerPressedEventArgs e)
{
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
{
MapControlMouseLeftButtonDown(e);
}
}
private void MapControlMouseWheel(object? sender, PointerWheelEventArgs e)
{
if (_map?.ZoomLock ?? true) return;
if (!Viewport.HasSize) return;
_currentMousePosition = e.GetPosition(this).ToMapsui();
//Needed for both MouseMove and MouseWheel event for mousewheel event
if (double.IsNaN(_toResolution))
_toResolution = Viewport.Resolution;
if (e.Delta.Y > Constants.Epsilon)
{
_toResolution = ZoomHelper.ZoomIn(_map.Resolutions, _toResolution);
}
else if (e.Delta.Y < Constants.Epsilon)
{
_toResolution = ZoomHelper.ZoomOut(_map.Resolutions, _toResolution);
}
var resolution = MouseWheelAnimation.GetResolution((int)e.Delta.Y, _viewport, _map);
// Limit target resolution before animation to avoid an animation that is stuck on the max resolution, which would cause a needless delay
resolution = _map.Limiter.LimitResolution(resolution, Viewport.Width, Viewport.Height, _map.Resolutions, _map.Extent);
Navigator?.ZoomTo(resolution, _currentMousePosition, MouseWheelAnimation.Duration, MouseWheelAnimation.Easing);
}
private void MapControlMouseLeftButtonDown(PointerPressedEventArgs e)
{
var touchPosition = e.GetPosition(this).ToMapsui();
_previousMousePosition = touchPosition;
_downMousePosition = touchPosition;
_mouseDown = true;
e.Pointer.Capture(this);
}
private void HandleFeatureInfo(PointerReleasedEventArgs e)
{
if (FeatureInfo == null) return; // don't fetch if you the call back is not set.
if (Map != null && _downMousePosition == e.GetPosition(this).ToMapsui())
foreach (var layer in Map.Layers)
{
// ReSharper disable once SuspiciousTypeConversion.Global
(layer as IFeatureInfo)?.GetFeatureInfo(Viewport, _downMousePosition.X, _downMousePosition.Y,
OnFeatureInfo);
}
}
private void OnFeatureInfo(IDictionary<string, IEnumerable<IFeature>> features)
{
FeatureInfo?.Invoke(this, new FeatureInfoEventArgs { FeatureInfo = features });
}
private void MapControlMouseLeave(object? sender, PointerEventArgs e)
{
_previousMousePosition = null;
}
private void MapControlMouseMove(object? sender, PointerEventArgs e)
{
_currentMousePosition = e.GetPosition(this).ToMapsui(); // Needed for both MouseMove and MouseWheel event
if (_mouseDown)
{
if (_previousMousePosition == null)
{
// Usually MapControlMouseLeftButton down initializes _previousMousePosition but in some
// situations it can be null. So far I could only reproduce this in debug mode when putting
// a breakpoint and continuing.
return;
}
_viewport.Transform(_currentMousePosition, _previousMousePosition);
RefreshGraphics();
_previousMousePosition = _currentMousePosition;
}
}
private void MapControl_PointerReleased(object? sender, PointerReleasedEventArgs e)
{
if (e.GetCurrentPoint(this).Properties.PointerUpdateKind == PointerUpdateKind.LeftButtonReleased)
{
MapControlMouseLeftButtonUp(e);
}
}
private void MapControlMouseLeftButtonUp(PointerReleasedEventArgs e)
{
RefreshData();
_mouseDown = false;
_previousMousePosition = null;
e.Pointer.Capture(null);
if (IsClick(_currentMousePosition, _downMousePosition))
{
HandleFeatureInfo(e);
OnInfo(InvokeInfo(_mousePosition, _mousePosition, 1));
}
}
private static bool IsClick(MPoint? currentPosition, MPoint? previousPosition)
{
if (currentPosition == null || previousPosition == null)
return false;
return
Math.Abs(currentPosition.X - previousPosition.X) < 1 &&
Math.Abs(currentPosition.Y - previousPosition.Y) < 1;
}
protected override void OnPointerMoved(PointerEventArgs e)
{
base.OnPointerMoved(e);
_mousePosition = e.GetPosition(this).ToMapsui();
}
private void OnDoubleTapped(object? sender, RoutedEventArgs e)
{
// We have a new interaction with the screen, so stop all navigator animations
var tapPosition = _mousePosition;
OnInfo(InvokeInfo(tapPosition, tapPosition, 2));
}
public override void Render(DrawingContext context)
{
_drawOp ??= new MapsuiCustomDrawOp(new Rect(0, 0, Bounds.Width, Bounds.Height), this);
_drawOp.Bounds = new Rect(0, 0, Bounds.Width, Bounds.Height);
context.Custom(_drawOp);
}
private void MapControlInitialized(object? sender, EventArgs eventArgs)
{
SetViewportSize();
}
private void MapControlSizeChanged()
{
SetViewportSize();
}
private void RunOnUIThread(Action action)
{
Catch.TaskRun(() => Dispatcher.UIThread.InvokeAsync(action));
}
public void OpenBrowser(string url)
{
using (Process.Start(new ProcessStartInfo
{
FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? url : "open",
Arguments = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? $"-e {url}" : "",
CreateNoWindow = true,
UseShellExecute = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
})) { }
}
private float ViewportWidth => Convert.ToSingle(Bounds.Width);
private float ViewportHeight => Convert.ToSingle(Bounds.Height);
private float GetPixelDensity()
{
if (VisualRoot != null)
{
return Convert.ToSingle(VisualRoot.RenderScaling);
}
return 1f;
}
private sealed class MapsuiCustomDrawOp : ICustomDrawOperation
{
private readonly MapControl _mapControl;
private readonly FormattedText _noSkia = new()
{
Text = "Current rendering API is not Skia"
};
public MapsuiCustomDrawOp(Rect bounds, MapControl mapControl)
{
Bounds = bounds;
_mapControl = mapControl;
}
public void Dispose()
{
// No-op
}
public Rect Bounds { get; set; }
public bool HitTest(Point p)
{
return true;
}
public bool Equals(ICustomDrawOperation? other)
{
return false;
}
public void Render(IDrawingContextImpl context)
{
var canvas = (context as ISkiaDrawingContextImpl)?.SkCanvas;
if (canvas == null)
context.DrawText(Brushes.Black, new Point(), _noSkia.PlatformImpl);
else
{
canvas.Save();
_mapControl.CommonDrawControl(canvas);
canvas.Restore();
}
}
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_drawOp?.Dispose();
_map?.Dispose();
}
CommonDispose(disposing);
}
public virtual void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}