Skip to content

Commit ecce035

Browse files
committed
- add gesture rotation events
1 parent 4b71f26 commit ecce035

12 files changed

+226
-8
lines changed

src/libs/Mapbox.Maui/IMapboxView.cs

+13
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ partial interface IMapboxView
5050

5151
event EventHandler<MapTappedEventArgs> MapTapped;
5252
ICommand Command { get; }
53+
54+
event EventHandler<CameraChangedEventArgs> CameraChanged;
55+
ICommand CameraChangedCommand { get; }
56+
57+
event EventHandler<Viewport.ViewportStatusChangedEventArgs> ViewportStatusChanged;
58+
ICommand ViewportStatusChangedCommand { get; }
59+
60+
event EventHandler<Gestures.RotatingBeganEventArgs> RotatingBegan;
61+
ICommand RotatingBeganCommand { get; }
62+
event EventHandler<Gestures.RotatingEndedEventArgs> RotatingEnded;
63+
ICommand RotatingEndedCommand { get; }
64+
event EventHandler<Gestures.RotatingEventArgs> Rotating;
65+
ICommand RotatingCommand { get; }
5366
}
5467

5568
public interface IAnnotationController

src/libs/Mapbox.Maui/Mapbox.Maui.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
<PropertyGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">
7070
<CreatePackage>false</CreatePackage>
7171
</PropertyGroup>
72+
<ItemGroup>
73+
<None Remove="Models\Gestures\" />
74+
</ItemGroup>
7275
<ItemGroup>
7376
<None Include="../../../assets/tv-mapbox.png" Pack="True" PackagePath="tv-mapbox.png" />
7477
<None Include="../../../LICENSE" Pack="True" PackagePath="" />
@@ -93,6 +96,9 @@
9396
<PackageReference Include="MapboxMapsObjC.iOS" Version="11.5.1.1" />
9497
<PackageReference Include="MapboxMaps.iOS" Version="11.5.1" />
9598
</ItemGroup>
99+
<ItemGroup>
100+
<Folder Include="Models\Gestures\" />
101+
</ItemGroup>
96102
<ProjectExtensions>
97103
<MonoDevelop>
98104
<Properties>

src/libs/Mapbox.Maui/MapboxView.Events.cs

+64-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,68 @@
44

55
partial class MapboxView
66
{
7+
public event EventHandler<Gestures.RotatingEventArgs> Rotating;
8+
public static readonly BindableProperty RotatingCommandProperty = BindableProperty.Create(
9+
nameof(RotatingCommand),
10+
typeof(ICommand),
11+
typeof(MapboxView)
12+
);
13+
public ICommand RotatingCommand
14+
{
15+
get => (ICommand)GetValue(RotatingCommandProperty);
16+
set => SetValue(RotatingCommandProperty, value);
17+
}
18+
internal void InvokeRotating(Gestures.RotatingEventArgs args)
19+
{
20+
Rotating?.Invoke(this, args);
21+
22+
if (RotatingCommand?.CanExecute(args) == true)
23+
{
24+
RotatingCommand.Execute(args);
25+
}
26+
}
27+
28+
public event EventHandler<Gestures.RotatingBeganEventArgs> RotatingBegan;
29+
public static readonly BindableProperty RotatingBeganCommandProperty = BindableProperty.Create(
30+
nameof(RotatingBeganCommand),
31+
typeof(ICommand),
32+
typeof(MapboxView)
33+
);
34+
public ICommand RotatingBeganCommand
35+
{
36+
get => (ICommand)GetValue(RotatingBeganCommandProperty);
37+
set => SetValue(RotatingBeganCommandProperty, value);
38+
}
39+
internal void InvokeRotatingBegan(Gestures.RotatingBeganEventArgs args)
40+
{
41+
RotatingBegan?.Invoke(this, args);
42+
43+
if (RotatingBeganCommand?.CanExecute(args) == true)
44+
{
45+
RotatingBeganCommand.Execute(args);
46+
}
47+
}
48+
49+
public event EventHandler<Gestures.RotatingEndedEventArgs> RotatingEnded;
50+
public static readonly BindableProperty RotatingEndedCommandProperty = BindableProperty.Create(
51+
nameof(RotatingEndedCommand),
52+
typeof(ICommand),
53+
typeof(MapboxView)
54+
);
55+
public ICommand RotatingEndedCommand
56+
{
57+
get => (ICommand)GetValue(RotatingEndedCommandProperty);
58+
set => SetValue(RotatingEndedCommandProperty, value);
59+
}
60+
internal void InvokeRotatingEnded(Gestures.RotatingEndedEventArgs args)
61+
{
62+
RotatingEnded?.Invoke(this, args);
63+
64+
if (RotatingEndedCommand?.CanExecute(args) == true)
65+
{
66+
RotatingEndedCommand.Execute(args);
67+
}
68+
}
769
public event EventHandler<Viewport.ViewportStatusChangedEventArgs> ViewportStatusChanged;
870
public static readonly BindableProperty ViewportStatusChangedCommandProperty = BindableProperty.Create(
971
nameof(ViewportStatusChangedCommand),
@@ -19,9 +81,9 @@ internal void InvokeViewportStatusChanged(Viewport.ViewportStatusChangedEventArg
1981
{
2082
ViewportStatusChanged?.Invoke(this, args);
2183

22-
if (CameraChangedCommand?.CanExecute(args) == true)
84+
if (ViewportStatusChangedCommand?.CanExecute(args) == true)
2385
{
24-
CameraChangedCommand.Execute(args);
86+
ViewportStatusChangedCommand.Execute(args);
2587
}
2688
}
2789
public event EventHandler<CameraChangedEventArgs> CameraChanged;

src/libs/Mapbox.Maui/Models/Camera/ICameraPlugin.cs

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
public interface ICameraPlugin
44
{
5+
CameraOptions CameraState { get; }
6+
57
void FlyTo(CameraOptions cameraOptions, AnimationOptions animationOptions = default, Action<AnimationState> completion = default);
68
void EaseTo(CameraOptions cameraOptions, AnimationOptions animationOptions = default, Action<AnimationState> completion = default);
79
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
namespace MapboxMaui.Gestures;
3+
4+
public class RotatingEventArgs
5+
{
6+
}
7+
public class RotatingBeganEventArgs
8+
{
9+
}
10+
public class RotatingEndedEventArgs
11+
{
12+
}

src/libs/Mapbox.Maui/Platforms/Android/AdditionalExtensions.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,15 @@ public static CameraOptions ToX(this MapboxMapsCameraOptions cameraOptions)
352352
Pitch = cameraOptions.Pitch?.FloatValue(),
353353
Zoom = cameraOptions.Zoom?.FloatValue(),
354354
};
355-
355+
public static CameraOptions ToX(this CameraState cameraOptions)
356+
=> new()
357+
{
358+
Bearing = (float?)cameraOptions.Bearing,
359+
Center = cameraOptions.Center?.ToMapPosition(),
360+
Padding = cameraOptions.Padding?.ToX(),
361+
Pitch = (float?)cameraOptions.Pitch,
362+
Zoom = (float?)cameraOptions.Zoom,
363+
};
356364
public static ScreenPosition ToX(this ScreenCoordinate screenCoordinate)
357365
=> new(screenCoordinate.GetX(), screenCoordinate.GetY());
358366

src/libs/Mapbox.Maui/Platforms/Android/MapboxFragment.cs

+25
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
using Com.Mapbox.Maps.Plugins.Locationcomponent;
1313
using MapboxMaui.Viewport;
1414
using Com.Mapbox.Maps.Plugins.Viewport;
15+
using Com.Mapbox.Android.Gestures;
16+
using MapboxMaui.Gestures;
1517

1618
public partial class MapboxFragment : Fragment
1719
{
@@ -25,6 +27,9 @@ public partial class MapboxFragment : Fragment
2527
public event Action<MapTappedPosition> MapLongClicked;
2628
public event Action<MapView> StyleLoaded;
2729
public event Action<ViewportStatusChangedEventArgs> ViewportStatusChanged;
30+
public event Action<RotatingEventArgs> Rotating;
31+
public event Action<RotatingBeganEventArgs> RotatingBegan;
32+
public event Action<RotatingEndedEventArgs> RotatingEnded;
2833

2934
public MapView MapView { get; private set; }
3035

@@ -79,6 +84,7 @@ public override void OnViewCreated(View view, Bundle savedInstanceState)
7984

8085
GesturesUtils.AddOnMapClickListener(MapView.MapboxMap, this);
8186
GesturesUtils.AddOnMapLongClickListener(MapView.MapboxMap, this);
87+
GesturesUtils.AddOnRotateListener(MapView.MapboxMap, this);
8288

8389
ViewportUtils.GetViewport(MapView).AddStatusObserver(new XViewportStatusObserver(ViewportStatusChanged));
8490
}
@@ -121,6 +127,7 @@ protected override void Dispose(bool disposing)
121127

122128
GesturesUtils.RemoveOnMapClickListener(MapView.MapboxMap, this);
123129
GesturesUtils.RemoveOnMapLongClickListener(MapView.MapboxMap, this);
130+
GesturesUtils.RemoveOnRotateListener(MapView.MapboxMap, this);
124131

125132
var locationUtils = LocationComponentUtils.GetLocationComponent(MapView);
126133
locationUtils.RemoveOnIndicatorAccuracyRadiusChangedListener(this);
@@ -134,6 +141,24 @@ protected override void Dispose(bool disposing)
134141
private IList<Com.Mapbox.Common.ICancelable> cancelables = new List<Com.Mapbox.Common.ICancelable>();
135142
}
136143

144+
partial class MapboxFragment : IOnRotateListener
145+
{
146+
public void OnRotate(RotateGestureDetector detector)
147+
{
148+
Rotating?.Invoke(new RotatingEventArgs());
149+
}
150+
151+
public void OnRotateBegin(RotateGestureDetector detector)
152+
{
153+
RotatingBegan?.Invoke(new RotatingBeganEventArgs());
154+
}
155+
156+
public void OnRotateEnd(RotateGestureDetector detector)
157+
{
158+
RotatingEnded?.Invoke(new RotatingEndedEventArgs());
159+
}
160+
}
161+
137162
partial class MapboxFragment
138163
: ICameraChangedCallback
139164
{

src/libs/Mapbox.Maui/Platforms/Android/MapboxViewHandler.Camera.cs

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ namespace MapboxMaui;
66

77
partial class MapboxViewHandler : ICameraPlugin
88
{
9+
public CameraOptions CameraState
10+
{
11+
get
12+
{
13+
var mapView = mapboxFragment?.MapView;
14+
15+
if (mapView == null) return default;
16+
17+
return mapView.MapboxMap.CameraState.ToX();
18+
}
19+
}
20+
921
public void EaseTo(CameraOptions cameraOptions, AnimationOptions animationOptions = null, Action<AnimationState> completion = null)
1022
{
1123
var mapView = mapboxFragment?.MapView;

src/libs/Mapbox.Maui/Platforms/Android/MapboxViewHandler.Events.cs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Com.Mapbox.Maps;
2+
using MapboxMaui.Gestures;
23

34
namespace MapboxMaui;
45

@@ -27,6 +28,9 @@ private void RegisterEvents(MapboxFragment mapboxFragment)
2728
mapboxFragment.MapLongClicked += HandleMapLongClicked;
2829
mapboxFragment.StyleLoaded += HandleStyleLoaded;
2930
mapboxFragment.ViewportStatusChanged += HandleViewportStatusChanged;
31+
mapboxFragment.Rotating -= HandleRotating;
32+
mapboxFragment.RotatingBegan -= HandleRotatingBegan;
33+
mapboxFragment.RotatingEnded -= HandleRotatingEnded;
3034
}
3135

3236
private void UnregisterEvents(MapboxFragment mapboxFragment)
@@ -50,6 +54,9 @@ private void UnregisterEvents(MapboxFragment mapboxFragment)
5054
mapboxFragment.MapLongClicked -= HandleMapLongClicked;
5155
mapboxFragment.StyleLoaded -= HandleStyleLoaded;
5256
mapboxFragment.ViewportStatusChanged -= HandleViewportStatusChanged;
57+
mapboxFragment.Rotating -= HandleRotating;
58+
mapboxFragment.RotatingBegan -= HandleRotatingBegan;
59+
mapboxFragment.RotatingEnded -= HandleRotatingEnded;
5360
}
5461

5562
private void HandleCameraChanged(CameraOptions options)
@@ -72,6 +79,10 @@ private void HandleStyleLoaded(MapView view)
7279
=> (VirtualView as MapboxView)?.InvokeStyleLoaded();
7380
private void HandleViewportStatusChanged(Viewport.ViewportStatusChangedEventArgs args)
7481
=> (VirtualView as MapboxView)?.InvokeViewportStatusChanged(args);
75-
76-
82+
private void HandleRotatingBegan(RotatingBeganEventArgs args)
83+
=> (VirtualView as MapboxView)?.InvokeRotatingBegan(args);
84+
private void HandleRotating(RotatingEventArgs args)
85+
=> (VirtualView as MapboxView)?.InvokeRotating(args);
86+
private void HandleRotatingEnded(RotatingEndedEventArgs args)
87+
=> (VirtualView as MapboxView)?.InvokeRotatingEnded(args);
7788
}

src/libs/Mapbox.Maui/Platforms/iOS/MapboxViewHandler.Camera.cs

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ namespace MapboxMaui;
66

77
partial class MapboxViewHandler : ICameraPlugin
88
{
9+
public CameraOptions CameraState
10+
{
11+
get
12+
{
13+
var mapView = PlatformView.MapView;
14+
15+
if (mapView == null) return default;
16+
17+
return mapView.MapboxMap().CameraState.ToX();
18+
}
19+
}
20+
921
public void EaseTo(CameraOptions cameraOptions, AnimationOptions animationOptions = default, Action<AnimationState> completion = default)
1022
{
1123
var mapView = PlatformView.MapView;

src/libs/Mapbox.Maui/Platforms/iOS/MapboxViewHandler.Events.cs

+46-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using MapboxMapsObjC;
1+
using Foundation;
2+
using MapboxMapsObjC;
23
using UIKit;
34

45
namespace MapboxMaui;
@@ -10,7 +11,6 @@ partial class MapboxViewHandler
1011
private UILongPressGestureRecognizer mapLongPressGestureRecognizer;
1112
private XViewportStatusObserver viewportStatusObserver;
1213

13-
1414
void RegisterEvents(PlatformView platformView)
1515
{
1616
var mapboxView = VirtualView as MapboxView;
@@ -57,6 +57,8 @@ void RegisterEvents(PlatformView platformView)
5757
mapView.Viewport().AddStatusObserver(
5858
viewportStatusObserver
5959
);
60+
61+
mapView.Gestures().WeakDelegate = new XTMBGestureManagerDelegate(mapboxView);
6062
}
6163

6264
void UnRegisterEvents(PlatformView platformView)
@@ -93,6 +95,9 @@ void UnRegisterEvents(PlatformView platformView)
9395
viewportStatusObserver.Dispose();
9496
viewportStatusObserver = null;
9597
}
98+
99+
mapView.Gestures().WeakDelegate?.Dispose();
100+
mapView.Gestures().WeakDelegate = null;
96101
}
97102

98103
private void HandleMapLongPress(UILongPressGestureRecognizer longPressGestureRecognizer)
@@ -130,3 +135,42 @@ private MapTappedPosition GetPositionForGesture(UIGestureRecognizer gesture)
130135
return coords.ToMapTappedPosition(screenPosition);
131136
}
132137
}
138+
139+
140+
sealed class XTMBGestureManagerDelegate
141+
: NSObject
142+
, ITMBGestureManagerDelegate
143+
{
144+
private readonly MapboxView mapboxView;
145+
146+
public XTMBGestureManagerDelegate(
147+
MapboxView mapboxView
148+
)
149+
{
150+
this.mapboxView = mapboxView;
151+
}
152+
153+
public void GestureManagerWithDidBegin(TMBGestureType gestureType)
154+
{
155+
switch(gestureType)
156+
{
157+
case TMBGestureType.Rotation:
158+
mapboxView?.InvokeRotatingBegan(new Gestures.RotatingBeganEventArgs());
159+
break;
160+
}
161+
}
162+
163+
public void GestureManagerWithDidEnd(TMBGestureType gestureType, bool willAnimate)
164+
{
165+
switch (gestureType)
166+
{
167+
case TMBGestureType.Rotation:
168+
mapboxView?.InvokeRotatingEnded(new Gestures.RotatingEndedEventArgs());
169+
break;
170+
}
171+
}
172+
173+
public void GestureManagerWithDidEndAnimatingFor(TMBGestureType gestureType)
174+
{
175+
}
176+
}

0 commit comments

Comments
 (0)