Skip to content

Commit 3cfe5f6

Browse files
committed
Moved all MapInfo handling to MapControl
1 parent 4bccd72 commit 3cfe5f6

File tree

8 files changed

+105
-69
lines changed

8 files changed

+105
-69
lines changed

Mapsui.UI.Android/MapControl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ public void Initialize()
5959
private void OnDoubleTapped(object sender, GestureDetector.DoubleTapEventArgs e)
6060
{
6161
var position = GetScreenPosition(e.Event, this);
62-
OnInfo(Map.InvokeInfo(Map.Layers.Where(l => l.IsMapInfoLayer), Map.Viewport,
62+
OnInfo(InvokeInfo(Map.Layers.Where(l => l.IsMapInfoLayer), Map.Widgets, Map.Viewport,
6363
position, position, Renderer.SymbolCache, WidgetTouched, 2));
6464
}
6565

6666
private void OnSingleTapped(object sender, GestureDetector.SingleTapConfirmedEventArgs e)
6767
{
6868
var position = GetScreenPosition(e.Event, this);
69-
OnInfo(Map.InvokeInfo(Map.Layers.Where(l => l.IsMapInfoLayer), Map.Viewport,
69+
OnInfo(InvokeInfo(Map.Layers.Where(l => l.IsMapInfoLayer), Map.Widgets, Map.Viewport,
7070
position, position, Renderer.SymbolCache, WidgetTouched, 1));
7171
}
7272

Mapsui.UI.Shared/MapControl.cs

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using Mapsui.Geometries;
22
using System;
3+
using System.Collections.Generic;
34
using System.ComponentModel;
5+
using System.Linq;
46
using System.Net;
57
using Mapsui.Fetcher;
8+
using Mapsui.Layers;
69
using Mapsui.Logging;
710
using Mapsui.Rendering;
811
using Mapsui.Rendering.Skia;
@@ -140,11 +143,11 @@ private void MapDataChanged(object sender, DataChangedEventArgs e)
140143

141144
private void MapPropertyChanged(object sender, PropertyChangedEventArgs e)
142145
{
143-
if (e.PropertyName == nameof(Layers.Layer.Enabled))
146+
if (e.PropertyName == nameof(Layer.Enabled))
144147
{
145148
RefreshGraphics();
146149
}
147-
else if (e.PropertyName == nameof(Layers.Layer.Opacity))
150+
else if (e.PropertyName == nameof(Layer.Opacity))
148151
{
149152
RefreshGraphics();
150153
}
@@ -235,5 +238,65 @@ private void WidgetTouched(IWidget widget, Point screenPosition)
235238

236239
widget.HandleWidgetTouched(screenPosition);
237240
}
241+
242+
/// <inheritdoc />
243+
public MapInfo GetMapInfo(Point screenPosition, int margin = 0)
244+
{
245+
return InfoHelper.GetMapInfo(Map.Layers.Where(l => l.IsMapInfoLayer), Map.Viewport,
246+
screenPosition, Renderer.SymbolCache, margin);
247+
}
248+
249+
/// <inheritdoc />
250+
public MapInfo GetMapInfo(IEnumerable<ILayer> layers, Point screenPosition, int margin = 0)
251+
{
252+
return InfoHelper.GetMapInfo(layers, Map.Viewport,
253+
screenPosition, Renderer.SymbolCache, margin);
254+
}
255+
256+
/// <summary>
257+
/// Check, if a widget or feature at a given screen position is clicked/tapped
258+
/// </summary>
259+
/// <param name="layers">The layers to query for MapInfo</param>
260+
/// <param name="widgets">The Map widgets</param>
261+
/// <param name="viewport">The current Viewport</param>
262+
/// <param name="screenPosition">Screen position to check for widgets and features</param>
263+
/// <param name="startScreenPosition">Screen position of Viewport/MapControl</param>
264+
/// <param name="symbolCache">Cache for symbols to determin size</param>
265+
/// <param name="widgetCallback">Callback, which is called when Widget is hiten</param>
266+
/// <param name="numTaps">Number of clickes/taps</param>
267+
/// <returns>True, if something done </returns>
268+
private static MapInfoEventArgs InvokeInfo(IEnumerable<ILayer> layers, IEnumerable<IWidget> widgets,
269+
Viewport viewport, Point screenPosition, Point startScreenPosition, ISymbolCache symbolCache,
270+
Action<IWidget, Point> widgetCallback, int numTaps)
271+
{
272+
var layerWidgets = layers.Select(l => l.Attribution).Where(a => a != null);
273+
var allWidgets = layerWidgets.Concat(widgets).ToList(); // Concat layer widgets and map widgets.
274+
275+
// First check if a Widget is clicked. In the current design they are always on top of the map.
276+
var widget = WidgetTouch.GetTouchedWidget(screenPosition, startScreenPosition, allWidgets);
277+
if (widget != null)
278+
{
279+
// todo:
280+
// How should widgetCallback have a handled type thing?
281+
// Widgets should be iterated through rather than getting a single widget,
282+
// based on Z index and then called until handled = true; Ordered By highest Z
283+
widgetCallback(widget, screenPosition);
284+
return null;
285+
}
286+
287+
var mapInfo = InfoHelper.GetMapInfo(layers, viewport, screenPosition, symbolCache);
288+
289+
if (mapInfo != null)
290+
{
291+
return new MapInfoEventArgs
292+
{
293+
MapInfo = mapInfo,
294+
NumTaps = numTaps,
295+
Handled = false
296+
};
297+
}
298+
299+
return null;
300+
}
238301
}
239302
}

Mapsui.UI.Uwp/MapControl.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ public MapControl()
7575
private void OnDoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
7676
{
7777
var tabPosition = e.GetPosition(this).ToMapsui();
78-
OnInfo(Map.InvokeInfo(Map.Layers.Where(l => l.IsMapInfoLayer), Map.Viewport,
78+
OnInfo(InvokeInfo(Map.Layers.Where(l => l.IsMapInfoLayer), Map.Widgets, Map.Viewport,
7979
tabPosition, tabPosition, Renderer.SymbolCache, WidgetTouched, 2));
8080
}
8181

8282
private void OnSingleTapped(object sender, TappedRoutedEventArgs e)
8383
{
8484
var tabPosition = e.GetPosition(this).ToMapsui();
85-
OnInfo(Map.InvokeInfo(Map.Layers.Where(l => l.IsMapInfoLayer), Map.Viewport,
85+
OnInfo(InvokeInfo(Map.Layers.Where(l => l.IsMapInfoLayer), Map.Widgets, Map.Viewport,
8686
tabPosition, tabPosition, Renderer.SymbolCache, WidgetTouched, 1));
8787
}
8888

@@ -114,7 +114,6 @@ private static SKXamlCanvas CreateRenderTarget()
114114
}
115115

116116
[Obsolete("Use Viewport.ViewportChanged", true)]
117-
// ReSharper disable once UnusedMember.Global
118117
#pragma warning disable 67
119118
public event EventHandler<ViewChangedEventArgs> ViewChanged;
120119
#pragma warning restore 67

Mapsui.UI.Wpf/MapControl.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ private void MapControlMouseLeftButtonDown(object sender, MouseButtonEventArgs e
358358
if (IsClick(_currentMousePosition, _downMousePosition))
359359
{
360360
HandleFeatureInfo(e);
361-
OnInfo(Map.InvokeInfo(Map.Layers.Where(l => l.IsMapInfoLayer), Map.Viewport,
361+
OnInfo(InvokeInfo(Map.Layers.Where(l => l.IsMapInfoLayer), Map.Widgets, Map.Viewport,
362362
touchPosition, _downMousePosition, Renderer.SymbolCache, WidgetTouched, e.ClickCount));
363363
}
364364
}
@@ -399,7 +399,7 @@ private void MapControlTouchUp(object sender, TouchEventArgs e)
399399
// todo: Pass the touchDown position. It needs to be set at touch down.
400400

401401
// todo: Figure out how to do a number of taps for WPF
402-
OnInfo(Map.InvokeInfo(Map.Layers.Where(l => l.IsMapInfoLayer), Map.Viewport,
402+
OnInfo(InvokeInfo(Map.Layers.Where(l => l.IsMapInfoLayer), Map.Widgets, Map.Viewport,
403403
touchPosition, touchPosition, Renderer.SymbolCache,
404404
WidgetTouched, 1));
405405
}
@@ -590,12 +590,6 @@ private void OnManipulationCompleted(object sender, ManipulationCompletedEventAr
590590
Refresh();
591591
}
592592

593-
public MapInfo GetMapInfo(Geometries.Point screenPosition, int margin = 0)
594-
{
595-
return InfoHelper.GetMapInfo(Map.Layers.Where(l => l.IsMapInfoLayer), Map.Viewport,
596-
screenPosition, Renderer.SymbolCache, margin);
597-
}
598-
599593
private void SKElementOnPaintSurface(object sender, SKPaintSurfaceEventArgs args)
600594
{
601595
if (Renderer == null) return;

Mapsui.UI.iOS/MapControl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ public void Initialize()
7272
private void OnDoubleTapped(UITapGestureRecognizer gesture)
7373
{
7474
var position = GetScreenPosition(gesture.LocationInView(this));
75-
OnInfo(Map.InvokeInfo(Map.Layers.Where(l => l.IsMapInfoLayer), Map.Viewport,
75+
OnInfo(InvokeInfo(Map.Layers.Where(l => l.IsMapInfoLayer), Map.Widgets, Map.Viewport,
7676
position, position, Renderer.SymbolCache, WidgetTouched, 2));
7777
}
7878

7979
private void OnSingleTapped(UITapGestureRecognizer gesture)
8080
{
8181
var position = GetScreenPosition(gesture.LocationInView(this));
82-
OnInfo(Map.InvokeInfo(Map.Layers.Where(l => l.IsMapInfoLayer), Map.Viewport,
82+
OnInfo(InvokeInfo(Map.Layers.Where(l => l.IsMapInfoLayer), Map.Widgets, Map.Viewport,
8383
position, position, Renderer.SymbolCache, WidgetTouched, 1));
8484
}
8585

Mapsui/Map.cs

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private set
113113
[Obsolete("Use ILayer.IsMapInfoLayer instead", true)]
114114
public IList<ILayer> InfoLayers { get; } = new List<ILayer>();
115115

116-
[Obsolete("Use your own hover event and call Map.GetMapInfo", true)]
116+
[Obsolete("Use your own hover event and call MapControl.GetMapInfo", true)]
117117
public IList<ILayer> HoverLayers { get; } = new List<ILayer>();
118118

119119
/// <summary>
@@ -234,57 +234,14 @@ public BoundingBox Envelope
234234
public event EventHandler RefreshGraphics;
235235

236236
[Obsolete("Use MapControl.Info instead", true)]
237+
#pragma warning disable 67
237238
public event EventHandler<MapInfoEventArgs> Info;
239+
#pragma warning restore 67
238240

239-
[Obsolete("Use your own hover event instead and call Map.GetMapInfo", true)]
241+
[Obsolete("Use your own hover event instead and call MapControl.GetMapInfo", true)]
242+
#pragma warning disable 67
240243
public event EventHandler<MapInfoEventArgs> Hover;
241-
242-
/// <summary>
243-
/// Check, if a widget or feature at a given screen position is clicked/tapped
244-
/// </summary>
245-
/// <param name="screenPosition">Screen position to check for widgets and features</param>
246-
/// <param name="startScreenPosition">Screen position of Viewport/MapControl</param>
247-
/// <param name="symbolCache">Cache for symbols to determin size</param>
248-
/// <param name="widgetCallback">Callback, which is called when Widget is hiten</param>
249-
/// <param name="numTaps">Number of clickes/taps</param>
250-
/// <returns>True, if something done </returns>
251-
public MapInfoEventArgs InvokeInfo(IEnumerable<ILayer> layers, Viewport viewport, Point screenPosition,
252-
Point startScreenPosition, ISymbolCache symbolCache,
253-
Action<IWidget, Point> widgetCallback, int numTaps)
254-
{
255-
var layerWidgets = layers.Select(l => l.Attribution).Where(a => a != null);
256-
var allWidgets = layerWidgets.Concat(Widgets).ToList(); // Concat layer widgets and map widgets.
257-
258-
// First check if a Widget is clicked. In the current design they are always on top of the map.
259-
var widget = WidgetTouch.GetWidget(screenPosition, startScreenPosition, allWidgets);
260-
if (widget != null)
261-
{
262-
// todo:
263-
// How should widgetCallback have a handled type thing?
264-
// Widgets should be iterated through rather than getting a single widget,
265-
// based on Z index and then called until handled = true; Ordered By highest Z
266-
widgetCallback(widget, screenPosition);
267-
return null;
268-
}
269-
270-
var mapInfo = InfoHelper.GetMapInfo(layers, viewport, screenPosition, symbolCache);
271-
272-
if (mapInfo != null)
273-
{
274-
// todo:
275-
// Info items should be iterated through rather than getting a single item,
276-
// based on Z index and then called until handled = true; Ordered By highest Z
277-
var mapInfoEventArgs = new MapInfoEventArgs
278-
{
279-
MapInfo = mapInfo,
280-
NumTaps = numTaps,
281-
Handled = false
282-
};
283-
return mapInfoEventArgs;
284-
}
285-
286-
return null;
287-
}
244+
#pragma warning restore 67
288245

289246
/// <summary>
290247
/// Abort fetching of all layers
@@ -412,5 +369,11 @@ private void OnDataChanged(object sender, DataChangedEventArgs e)
412369
// let users set the viewport. Adding the navigate methods to the Viewport
413370
// would make sense for that scenario.
414371
public Func<BoundingBox> DefaultExtent { get; set; }
372+
373+
public MapInfo GetMapInfo(IEnumerable<ILayer> layers, IViewport viewport, Point screenPosition,
374+
ISymbolCache symbolCache, int margin = 0)
375+
{
376+
return InfoHelper.GetMapInfo(layers, viewport, screenPosition, symbolCache);
377+
}
415378
}
416379
}

Mapsui/UI/IMapControl.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
23
using Mapsui.Geometries;
4+
using Mapsui.Layers;
35
using Mapsui.Rendering;
46

57
namespace Mapsui.UI
@@ -41,7 +43,7 @@ public interface IMapControl
4143
/// </summary>
4244
float ViewportHeight { get; }
4345

44-
void OpenBrowser(string url); //todo: remove when implemented an all platforms.
46+
void OpenBrowser(string url); //todo: Perhaps remove
4547

4648
/// <summary>
4749
/// Converts coordinates in pixels to device independent units (or DIP or DP).
@@ -56,5 +58,20 @@ public interface IMapControl
5658
/// <param name="coordinateInDeviceIndependentUnits">Coordinate in device independent units (or DIP or DP)</param>
5759
/// <returns>Coordinate in pixels</returns>
5860
Point ToPixels(Point coordinateInDeviceIndependentUnits);
61+
62+
/// <summary>
63+
/// Check, if a feature at a given screen position is hit
64+
/// </summary>
65+
/// <param name="screenPosition">Screen position to check for widgets and features</param>
66+
/// <param name="margin">An optional extra margin around the feature to enlarge the hit area.</param>
67+
MapInfo GetMapInfo(Point screenPosition, int margin = 0);
68+
69+
/// <summary>
70+
/// Check, if a feature at a given screen position is hit
71+
/// </summary>
72+
/// <param name="layers">The layers to query for MapInfo</param>
73+
/// <param name="screenPosition">Screen position to check for widgets and features</param>
74+
/// <param name="margin">An optional extra margin around the feature to enlarge the hit area.</param>
75+
MapInfo GetMapInfo(IEnumerable<ILayer> layers, Point screenPosition, int margin = 0);
5976
}
6077
}

Mapsui/Widgets/WidgetTouch.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Mapsui.Widgets
66
{
7-
static class WidgetTouch
7+
public static class WidgetTouch
88
{
99
/// <summary>
1010
/// Gets the widget selected by touch positions
@@ -16,7 +16,7 @@ static class WidgetTouch
1616
/// Returns the first Widget in the list that contains the screenPosition
1717
/// within it's Envelope. Returns null if there are none.
1818
/// </returns>
19-
public static IWidget GetWidget(Point screenPosition, Point startScreenPosition,
19+
public static IWidget GetTouchedWidget(Point screenPosition, Point startScreenPosition,
2020
IEnumerable<IWidget> widgets)
2121
{
2222
foreach (var widget in widgets.Reverse())

0 commit comments

Comments
 (0)