Skip to content

Commit c456ff4

Browse files
committed
Moving MapControl TryInitializeViewport to shared
1 parent 3de4f6e commit c456ff4

File tree

5 files changed

+31
-70
lines changed

5 files changed

+31
-70
lines changed

Mapsui.UI.Android/MapControl.cs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void Initialize()
4949
AddView(_canvas);
5050

5151
Map = new Map();
52-
TryInitializeViewport();
52+
TryInitializeViewport(ToDeviceIndependentUnits(Width), ToDeviceIndependentUnits(Height));
5353
Touch += MapView_Touch;
5454

5555
_gestureDetector = new GestureDetector(Context, new GestureDetector.SimpleOnGestureListener());
@@ -74,7 +74,11 @@ private void OnSingleTapped(object sender, GestureDetector.SingleTapConfirmedEve
7474
protected override void OnSizeChanged(int width, int height, int oldWidth, int oldHeight)
7575
{
7676
base.OnSizeChanged(width, height, oldWidth, oldHeight);
77-
PushSizeOntoViewport(width, height);
77+
78+
if (Map == null) return;
79+
80+
Map.Viewport.Width = ToDeviceIndependentUnits(width);
81+
Map.Viewport.Height = ToDeviceIndependentUnits(height);
7882
}
7983

8084
private void RunOnUIThread(Action action)
@@ -84,23 +88,12 @@ private void RunOnUIThread(Action action)
8488

8589
private void CanvasOnPaintSurface(object sender, SKPaintSurfaceEventArgs args)
8690
{
87-
TryInitializeViewport();
91+
TryInitializeViewport(ToDeviceIndependentUnits(Width), ToDeviceIndependentUnits(Height));
8892
if (!_map.Viewport.Initialized) return;
8993

9094
Renderer.Render(args.Surface.Canvas, _map.Viewport, _map.Layers, _map.Widgets, _map.BackColor);
9195
}
9296

93-
private void TryInitializeViewport()
94-
{
95-
if (_map.Viewport.Initialized) return;
96-
97-
if (_map.Viewport.TryInitializeViewport(_map.Envelope, ToDeviceIndependentUnits(Width), ToDeviceIndependentUnits(Height)))
98-
{
99-
_map.RefreshData(true);
100-
OnViewportInitialized();
101-
}
102-
}
103-
10497
private void OnViewportInitialized()
10598
{
10699
ViewportInitialized?.Invoke(this, EventArgs.Empty);
@@ -326,15 +319,6 @@ private float ToDeviceIndependentUnits(float pixelCoordinate)
326319
return pixelCoordinate / _density;
327320
}
328321

329-
void PushSizeOntoViewport(float mapControlWidth, float mapControlHeight)
330-
{
331-
if (Map != null)
332-
{
333-
Map.Viewport.Width = ToDeviceIndependentUnits(mapControlWidth);
334-
Map.Viewport.Height = ToDeviceIndependentUnits(mapControlHeight);
335-
}
336-
}
337-
338322
public new void Dispose()
339323
{
340324
Unsubscribe();

Mapsui.UI.Shared/MapControl.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,5 +244,20 @@ public Point ToDeviceIndependentUnits(Point coordinateInPixels)
244244
coordinateInPixels.X / PixelsPerDeviceIndependentUnit,
245245
coordinateInPixels.Y / PixelsPerDeviceIndependentUnit);
246246
}
247+
248+
private void TryInitializeViewport(double screenWidth, double screenHeight)
249+
{
250+
if (_map?.Viewport?.Initialized != false) return;
251+
252+
if (_map.Viewport.TryInitializeViewport(_map.Envelope, screenWidth, screenHeight))
253+
{
254+
// limiter now only properly implemented in WPF.
255+
ViewportLimiter.Limit(_map.Viewport, _map.ZoomMode, _map.ZoomLimits, _map.Resolutions,
256+
_map.PanMode, _map.PanLimits, _map.Envelope);
257+
258+
Map.RefreshData(true);
259+
OnViewportInitialized();
260+
}
261+
}
247262
}
248263
}

Mapsui.UI.Uwp/MapControl.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,13 @@ public void Clear()
190190

191191
private void MapControlLoaded(object sender, RoutedEventArgs e)
192192
{
193-
TryInitializeViewport();
193+
TryInitializeViewport(ActualWidth, ActualHeight);
194194
UpdateSize();
195195
}
196196

197197
private void MapControlSizeChanged(object sender, SizeChangedEventArgs e)
198198
{
199-
TryInitializeViewport();
199+
TryInitializeViewport(ActualWidth, ActualHeight);
200200
Clip = new RectangleGeometry { Rect = new Rect(0, 0, ActualWidth, ActualHeight) };
201201
UpdateSize();
202202
_map.RefreshData(true);
@@ -221,7 +221,7 @@ private void Canvas_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
221221
if (Renderer == null) return;
222222
if (_map == null) return;
223223

224-
TryInitializeViewport();
224+
TryInitializeViewport(ActualWidth, ActualHeight);
225225
if (!_map.Viewport.Initialized) return;
226226

227227
Renderer.Render(e.Surface.Canvas, Map.Viewport, _map.Layers, _map.Widgets, _map.BackColor);
@@ -315,17 +315,6 @@ private void OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs
315315
e.Handled = true;
316316
}
317317

318-
private void TryInitializeViewport()
319-
{
320-
if (_map.Viewport.Initialized) return;
321-
322-
if (_map.Viewport.TryInitializeViewport(_map.Envelope, ActualWidth, ActualHeight))
323-
{
324-
Map.RefreshData(true);
325-
OnViewportInitialized();
326-
}
327-
}
328-
329318
private void OnViewportInitialized()
330319
{
331320
ViewportInitialized?.Invoke(this, EventArgs.Empty);

Mapsui.UI.Wpf/MapControl.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ private void ZoomMiddle()
243243

244244
private void MapControlLoaded(object sender, RoutedEventArgs e)
245245
{
246-
TryInitializeViewport();
246+
TryInitializeViewport(ActualWidth, ActualHeight);
247247
UpdateSize();
248248
InitAnimation();
249249
Focusable = true;
@@ -327,7 +327,7 @@ private void ZoomAnimationCompleted(object sender, EventArgs e)
327327

328328
private void MapControlSizeChanged(object sender, SizeChangedEventArgs e)
329329
{
330-
TryInitializeViewport();
330+
TryInitializeViewport(ActualWidth, ActualHeight);
331331
Clip = new RectangleGeometry { Rect = new Rect(0, 0, ActualWidth, ActualHeight) };
332332
UpdateSize();
333333
_map.RefreshData(true);
@@ -490,21 +490,6 @@ private void MapControlMouseMove(object sender, MouseEventArgs e)
490490
}
491491
}
492492

493-
private void TryInitializeViewport()
494-
{
495-
if (_map?.Viewport == null) return;
496-
if (_map.Viewport.Initialized) return;
497-
498-
if (_map.Viewport.TryInitializeViewport(_map.Envelope, ActualWidth, ActualHeight))
499-
{
500-
ViewportLimiter.Limit(_map.Viewport, _map.ZoomMode, _map.ZoomLimits, _map.Resolutions,
501-
_map.PanMode, _map.PanLimits, _map.Envelope);
502-
503-
Map.RefreshData(true);
504-
OnViewportInitialized();
505-
}
506-
}
507-
508493
private void OnViewportInitialized()
509494
{
510495
ViewportInitialized?.Invoke(this, EventArgs.Empty);
@@ -655,8 +640,7 @@ private void SKElementOnPaintSurface(object sender, SKPaintSurfaceEventArgs args
655640
if (Renderer == null) return;
656641
if (_map == null) return;
657642

658-
Debug.WriteLine(DateTime.Now.Ticks);
659-
TryInitializeViewport();
643+
TryInitializeViewport(ActualWidth, ActualHeight);
660644
if (!_map.Viewport.Initialized) return;
661645

662646
Renderer.Render(args.Surface.Canvas, Map.Viewport, Map.Layers, Map.Widgets, Map.BackColor);
@@ -667,7 +651,7 @@ private void PaintWpf()
667651
if (Renderer == null) return;
668652
if (_map == null) return;
669653

670-
TryInitializeViewport();
654+
TryInitializeViewport(ActualWidth, ActualHeight);
671655
if (!_map.Viewport.Initialized) return;
672656

673657
Renderer.Render(WpfCanvas, Map.Viewport, _map.Layers, Map.Widgets, _map.BackColor);

Mapsui.UI.iOS/MapControl.cs

Lines changed: 2 additions & 13 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();
58+
TryInitializeViewport(_canvas.Frame.Width, _canvas.Frame.Height);
5959

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

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

105105
args.Surface.Canvas.Scale(_density, _density); // we can only set the scale in the render loop
106106

107107
Renderer.Render(args.Surface.Canvas, _map.Viewport, _map.Layers, _map.Widgets, _map.BackColor);
108108
}
109109

110-
private void TryInitializeViewport()
111-
{
112-
if (_map.Viewport.Initialized) return;
113-
114-
if (_map.Viewport.TryInitializeViewport(_map.Envelope, (float)_canvas.Frame.Width, (float)_canvas.Frame.Height))
115-
{
116-
Map.RefreshData(true);
117-
OnViewportInitialized();
118-
}
119-
}
120-
121110
private void OnViewportInitialized()
122111
{
123112
ViewportInitialized?.Invoke(this, EventArgs.Empty);

0 commit comments

Comments
 (0)