Skip to content

Commit 07f51b2

Browse files
committed
Adding ScreenWidth/ScreenHeight and moving public void NavigateToFullEnvelope to shared
1 parent 6e20800 commit 07f51b2

File tree

8 files changed

+54
-28
lines changed

8 files changed

+54
-28
lines changed

Mapsui.UI.Android/MapControl.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void Initialize()
5353
AddView(_canvas);
5454

5555
Map = new Map();
56-
TryInitializeViewport(ToDeviceIndependentUnits(Width), ToDeviceIndependentUnits(Height));
56+
TryInitializeViewport(ScreenWidth, ScreenHeight);
5757
Touch += MapView_Touch;
5858

5959
_gestureDetector = new GestureDetector(Context, new GestureDetector.SimpleOnGestureListener());
@@ -81,8 +81,8 @@ protected override void OnSizeChanged(int width, int height, int oldWidth, int o
8181

8282
if (Map == null) return;
8383

84-
Map.Viewport.Width = ToDeviceIndependentUnits(width);
85-
Map.Viewport.Height = ToDeviceIndependentUnits(height);
84+
Map.Viewport.Width = ScreenWidth;
85+
Map.Viewport.Height = ScreenHeight;
8686
}
8787

8888
private void RunOnUIThread(Action action)
@@ -92,7 +92,7 @@ private void RunOnUIThread(Action action)
9292

9393
private void CanvasOnPaintSurface(object sender, SKPaintSurfaceEventArgs args)
9494
{
95-
TryInitializeViewport(ToDeviceIndependentUnits(Width), ToDeviceIndependentUnits(Height));
95+
TryInitializeViewport(ScreenWidth, ScreenHeight);
9696
if (!_map.Viewport.Initialized) return;
9797

9898
Renderer.Render(args.Surface.Canvas, _map.Viewport, _map.Layers, _map.Widgets, _map.BackColor);
@@ -354,5 +354,8 @@ private static (Point centre, double radius, double angle) GetPinchValues(List<P
354354

355355
return (new Point(centerX, centerY), radius, angle);
356356
}
357+
358+
public float ScreenWidth => ToDeviceIndependentUnits(Width);
359+
public float ScreenHeight => ToDeviceIndependentUnits(Height);
357360
}
358361
}

Mapsui.UI.Shared/MapControl.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Mapsui.Logging;
77
using Mapsui.Rendering;
88
using Mapsui.Rendering.Skia;
9+
using Mapsui.Utilities;
910

1011
#if __ANDROID__
1112
namespace Mapsui.UI.Android
@@ -214,5 +215,10 @@ public void RefreshData()
214215
{
215216
_map?.RefreshData(true);
216217
}
218+
219+
public void NavigateToFullEnvelope(ScaleMethod scaleMethod)
220+
{
221+
_map?.NavigateTo(_map.Envelope, scaleMethod);
222+
}
217223
}
218224
}

Mapsui.UI.Uwp/MapControl.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,14 +242,6 @@ private void ClearBBoxDrawing()
242242
_bboxRect.Height = 0;
243243
}
244244

245-
public void NavigateToFullEnvelope()
246-
{
247-
if (Map.Envelope == null) return;
248-
if (ActualWidth.IsNanOrZero()) return;
249-
Map.Viewport.Resolution = Map.Envelope.Width / ActualWidth;
250-
Map.Viewport.Center = Map.Envelope.Centroid;
251-
}
252-
253245
private static void OnManipulationInertiaStarting(object sender, ManipulationInertiaStartingRoutedEventArgs e)
254246
{
255247
e.TranslationBehavior.DesiredDeceleration = 25 * 96.0 / (1000.0 * 1000.0);
@@ -304,5 +296,8 @@ private void WidgetTouched(IWidget widget, Geometries.Point screenPosition)
304296

305297
widget.HandleWidgetTouched(screenPosition);
306298
}
299+
300+
public float ScreenWidth => (float)ActualWidth;
301+
public float ScreenHeight => (float)ActualHeight;
307302
}
308303
}

Mapsui.UI.Wpf/MapControl.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -529,13 +529,8 @@ private void DrawBbox(Point newPos)
529529
}
530530
}
531531

532-
public void NavigateToFullEnvelope()
533-
{
534-
if (Map.Envelope == null) return;
535-
if (ActualWidth.IsNanOrZero()) return;
536-
Map.Viewport.Resolution = Math.Max(Map.Envelope.Width / ActualWidth, Map.Envelope.Height / ActualHeight);
537-
Map.Viewport.Center = Map.Envelope.Centroid;
538-
}
532+
public float ScreenWidth => (float)ActualWidth;
533+
public float ScreenHeight => (float) ActualHeight;
539534

540535
private static void OnManipulationInertiaStarting(object sender, ManipulationInertiaStartingEventArgs e)
541536
{

Mapsui.UI.iOS/MapControl.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void Initialize()
5555
// Unfortunately the SKGLView does not have a IgnorePixelScaling property. We have to adjust for density with SKGLView.Scale.
5656
_density = PixelsPerDeviceIndependentUnit;
5757

58-
TryInitializeViewport(_canvas.Frame.Width, _canvas.Frame.Height);
58+
TryInitializeViewport(ScreenWidth, ScreenHeight);
5959

6060
ClipsToBounds = true;
6161
MultipleTouchEnabled = true;
@@ -99,7 +99,7 @@ private void OnSingleTapped(UITapGestureRecognizer gesture)
9999

100100
void OnPaintSurface(object sender, SKPaintGLSurfaceEventArgs args)
101101
{
102-
TryInitializeViewport(_canvas.Frame.Width, _canvas.Frame.Height);
102+
TryInitializeViewport(ScreenWidth, ScreenHeight);
103103
if (!_map.Viewport.Initialized) return;
104104

105105
args.Surface.Canvas.Scale(_density, _density); // we can only set the scale in the render loop
@@ -217,8 +217,8 @@ public override CGRect Frame
217217

218218
if (_map?.Viewport == null) return;
219219

220-
_map.Viewport.Width = _canvas.Frame.Width;
221-
_map.Viewport.Height = _canvas.Frame.Height;
220+
_map.Viewport.Width = ScreenWidth;
221+
_map.Viewport.Height = ScreenHeight;
222222

223223
Refresh();
224224
}
@@ -232,8 +232,8 @@ public override void LayoutMarginsDidChange()
232232

233233
if (_map?.Viewport == null) return;
234234

235-
_map.Viewport.Width = _canvas.Frame.Width;
236-
_map.Viewport.Height = _canvas.Frame.Height;
235+
_map.Viewport.Width = ScreenWidth;
236+
_map.Viewport.Height = ScreenHeight;
237237

238238
Refresh();
239239
}
@@ -283,5 +283,8 @@ private static (Point centre, double radius, double angle) GetPinchValues(List<P
283283

284284
return (new Point(centerX, centerY), radius, angle);
285285
}
286+
287+
public float ScreenWidth => (float)_canvas.Frame.Width; // todo: check if we need _canvas
288+
public float ScreenHeight => (float)_canvas.Frame.Height; // todo: check if we need _canvas
286289
}
287290
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// ReSharper disable once CheckNamespace
2+
namespace System
3+
{
4+
public static class FloatExtensions
5+
{
6+
public static bool IsNanOrZero(this float target)
7+
{
8+
if (float.IsNaN(target)) return true;
9+
if (float.IsInfinity(target)) return true;
10+
// ReSharper disable once CompareOfFloatsByEqualityOperator
11+
return target == 0;
12+
}
13+
}
14+
}

Mapsui/Map.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public bool InvokeInfo(Point screenPosition, Point startScreenPosition, ISymbolC
260260
{
261261
var layerWidgets = Layers.Select(l => l.Attribution).Where(a => a != null);
262262
var allWidgets = layerWidgets.Concat(Widgets).ToList(); // Concat layer widgets and map widgets.
263-
263+
264264
// First check if a Widget is clicked. In the current design they are always on top of the map.
265265
var widget = WidgetTouch.GetWidget(screenPosition, startScreenPosition, allWidgets);
266266
if (widget != null)
@@ -270,7 +270,7 @@ public bool InvokeInfo(Point screenPosition, Point startScreenPosition, ISymbolC
270270
// Widgets should be iterated through rather than getting a single widget,
271271
// based on Z index and then called until handled = true; Ordered By highest Z
272272
widgetCallback(widget, screenPosition);
273-
return true;
273+
return true;
274274
}
275275

276276
if (Info == null) return false;
@@ -315,7 +315,7 @@ public void InvokeHover(Point screenPosition, ISymbolCache symbolCache)
315315
NumTaps = 0,
316316
Handled = false
317317
};
318-
318+
319319
_previousHoverEventArgs = mapInfoEventArgs;
320320
Hover?.Invoke(this, mapInfoEventArgs);
321321
}

Mapsui/UI/IMapControl.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,15 @@ public interface IMapControl
2626
float PixelsPerDeviceIndependentUnit { get; }
2727

2828
IRenderer Renderer { get; }
29+
30+
/// <summary>
31+
/// The map's screen width in device independent units
32+
/// </summary>
33+
float ScreenWidth { get; }
34+
35+
/// <summary>
36+
/// The map's screen height in device independent units
37+
/// </summary>
38+
float ScreenHeight { get; }
2939
}
3040
}

0 commit comments

Comments
 (0)