Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: GestureSettings #27

Merged
merged 5 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libs/Mapbox.Maui/IMapboxView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public partial interface IMapboxView : IView
{
CameraOptions CameraOptions { get; set; }
GestureSettings GestureSettings { get; set; }
MapboxStyle MapboxStyle { get; set; }
IPosition MapCenter { get; set; }
float? MapZoom { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions src/libs/Mapbox.Maui/Mapbox.Maui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<RepositoryUrl>https://github.com/tuyen-vuduc/mapbox-maui</RepositoryUrl>
<PackageProjectUrl>https://mapbox.tuyen-vuduc.tech</PackageProjectUrl>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageVersion>11.4.0-alpha01</PackageVersion>
<PackageVersion>11.4.0-alpha02</PackageVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageIcon>tv-mapbox.png</PackageIcon>
Expand All @@ -80,8 +80,8 @@
<PackageReference Include="Xamarin.Kotlin.StdLib.Common" Version="1.9.23.3" PrivateAssets="none" />
</ItemGroup>
<ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">
<PackageReference Include="MapboxMapsObjC.iOS" Version="11.4.0" />
<PackageReference Include="MapboxMaps.iOS" Version="11.4.0" />
<PackageReference Include="MapboxMapsObjC.iOS" Version="11.4.0.2" />
<PackageReference Include="MapboxMaps.iOS" Version="11.4.0.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="Models\Styles\" />
Expand Down
11 changes: 11 additions & 0 deletions src/libs/Mapbox.Maui/MapboxView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@

public partial class MapboxView : View, IMapboxView
{
public static readonly BindableProperty GestureSettingsProperty = BindableProperty.Create(
nameof(GestureSettings),
typeof(GestureSettings),
typeof(MapboxView)
);
public GestureSettings GestureSettings
{
get => (GestureSettings)GetValue(GestureSettingsProperty);
set => SetValue(GestureSettingsProperty, value);
}

public static readonly BindableProperty ViewAnnotationsProperty = BindableProperty.Create(
nameof(ViewAnnotations),
typeof(IEnumerable<ViewAnnotationOptions>),
Expand Down
1 change: 1 addition & 0 deletions src/libs/Mapbox.Maui/MapboxViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static IPropertyMapper<IMapboxView, MapboxViewHandler> PropertyMapper
= new PropertyMapper<IMapboxView, MapboxViewHandler>(ViewHandler.ViewMapper)
{
[nameof(MapboxView.CameraOptions)] = HandleCameraOptionsChanged,
[nameof(MapboxView.GestureSettings)] = HandleGestureSettingsChanged,
[nameof(MapboxView.MapboxStyle)] = HandleMapboxStyleChanged,
[nameof(MapboxView.ScaleBarVisibility)] = HandleScaleBarVisibilityChanged,
[nameof(MapboxView.DebugOptions)] = HandleDebugOptionsChanged,
Expand Down
106 changes: 106 additions & 0 deletions src/libs/Mapbox.Maui/Models/GestureSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
namespace MapboxMaui;

public record struct GestureSettings
{
public GestureSettings()
{
}

/// <summary>
/// Whether the rotate gesture is enabled.
/// </summary>
public bool RotateEnabled { get; set; } = true;
/// <summary>
/// Whether the pinch to zoom gesture is enabled.
///
/// iOS: PinchZoomEnabled
/// </summary>
public bool PinchToZoomEnabled { get; set; } = true;
/// <summary>
/// Whether the single-touch scroll gesture is enabled.
/// </summary>
public bool ScrollEnabled { get; set; } = true;
/// <summary>
/// Whether rotation is enabled for the pinch to zoom gesture.
///
/// iOS: SimultaneousRotateAndPinchZoomEnabled
/// </summary>
public bool SimultaneousRotateAndPinchToZoomEnabled { get; set; } = true;
/// <summary>
/// Whether the pitch gesture is enabled.
/// </summary>
public bool PitchEnabled { get; set; } = true;
/// <summary>
/// Configures the directions in which the map is allowed to move during a scroll gesture.
///
/// iOS: PanMode
/// </summary>
public PanMode ScrollMode { get; set; } = PanMode.Both;
/// <summary>
/// Whether double tapping the map with one touch results in a zoom-in animation.
/// </summary>
public bool DoubleTapToZoomInEnabled { get; set; } = true;
/// <summary>
/// Whether single tapping the map with two touches results in a zoom-out animation.
/// </summary>
public bool DoubleTouchToZoomOutEnabled { get; set; } = true;
/// <summary>
/// Whether the quick zoom gesture is enabled.
/// </summary>
public bool QuickZoomEnabled { get; set; } = true;
/// <summary>
/// By default, gestures rotate and zoom around the center of the gesture. Set this property to
/// rotate and zoom around a fixed point instead.
/// </summary>
public ScreenPosition? FocalPoint { get; set; }
/// <summary>
/// Android only.
/// Whether a deceleration animation following a pinch-to-zoom gesture is enabled. True by default.
/// </summary>
public bool PinchToZoomDecelerationEnabled { get; set; } = true;
/// <summary>
/// Android only.
/// Whether a deceleration animation following a rotate gesture is enabled. True by default.
/// </summary>
public bool RotateDecelerationEnabled { get; set; } = true;
/// <summary>
/// Android only.
/// Whether a deceleration animation following a scroll gesture is enabled. True by default.
/// </summary>
public bool ScrollDecelerationEnabled { get; set; } = true;
/// <summary>
/// Android only.
///
/// Whether rotate threshold increases when pinching to zoom. true by default.
/// </summary>
public bool IncreaseRotateThresholdWhenPinchingToZoom { get; set; } = true;
/// <summary>
/// Android only.
/// Whether pinch to zoom threshold increases when rotating. true by default.
/// </summary>
public bool IncreasePinchToZoomThresholdWhenRotating { get; set; } = true;
/// <summary>
/// Android only.
/// The amount by which the zoom level increases or decreases during a double-tap-to-zoom-in or
/// double-touch-to-zoom-out gesture. 1.0 by default. Must be positive.
/// </summary>
public float ZoomAnimationAmount { get; set; } = 1.0f;
/// <summary>
/// iOS only.
/// A constant factor that determines how quickly pan deceleration animations happen.
/// Multiplied with the velocity vector once per millisecond during deceleration animations.
/// </summary>
public float PanDecelerationFactor { get; set; } = 1.0f;
/// <summary>
/// Whether pan is enabled for the pinch gesture.
///
/// iOS: PanEnabled
/// </summary>
public bool PinchScrollEnabled { get; set; } = true;
}
public enum PanMode
{
Horizontal = 0x0001,
Vertical = 0x0010,
Both = 0x0011,
}
10 changes: 10 additions & 0 deletions src/libs/Mapbox.Maui/Platforms/Android/AdditionalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@
using AndroidX.Fragment.App;
using Com.Mapbox.Maps.Plugins.Animation;
using Com.Mapbox.Functions;
using Com.Mapbox.Maps.Plugins;

static class AdditionalExtensions
{
internal static DisplayMetrics Metrics;

public static ScrollMode ToNative(this PanMode scrollDirection)
{
return scrollDirection switch {
PanMode.Horizontal => ScrollMode.Horizontal,
PanMode.Vertical => ScrollMode.Vertical,
_ => ScrollMode.HorizontalAndVertical,
};
}

public static MapAnimationOptions ToNative(this AnimationOptions animationOptions)
{
Expand Down
32 changes: 30 additions & 2 deletions src/libs/Mapbox.Maui/Platforms/Android/MapboxViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,40 @@
using Android.Content;
using Android.Graphics;
using GeoJSON.Text;

using Com.Mapbox.Maps.Plugins.Gestures;
using Com.Mapbox.Maps.Plugins.Gestures.Generated;

namespace MapboxMaui;
public partial class MapboxViewHandler
{
MapboxFragment mapboxFragment;

private static void HandleGestureSettingsChanged(MapboxViewHandler handler, IMapboxView view)
{
var mapView = handler.GetMapView();
if (mapView == null) return;

var gestures = mapView.GetGestures();

gestures.DoubleTapToZoomInEnabled = view.GestureSettings.DoubleTapToZoomInEnabled;
gestures.DoubleTouchToZoomOutEnabled = view.GestureSettings.DoubleTouchToZoomOutEnabled;
gestures.FocalPoint = view.GestureSettings.FocalPoint?.ToScreenCoordinate();
gestures.IncreasePinchToZoomThresholdWhenRotating = view.GestureSettings.IncreasePinchToZoomThresholdWhenRotating;
gestures.IncreaseRotateThresholdWhenPinchingToZoom = view.GestureSettings.IncreaseRotateThresholdWhenPinchingToZoom;
gestures.PinchScrollEnabled = view.GestureSettings.PinchScrollEnabled;
gestures.PinchToZoomDecelerationEnabled = view.GestureSettings.PinchToZoomDecelerationEnabled;
gestures.PinchToZoomEnabled = view.GestureSettings.PinchToZoomEnabled;
gestures.PitchEnabled = view.GestureSettings.PitchEnabled;
gestures.QuickZoomEnabled = view.GestureSettings.QuickZoomEnabled;
gestures.RotateDecelerationEnabled = view.GestureSettings.RotateDecelerationEnabled;
gestures.RotateEnabled = view.GestureSettings.RotateEnabled;
gestures.ScrollDecelerationEnabled = view.GestureSettings.ScrollDecelerationEnabled;
gestures.ScrollEnabled = view.GestureSettings.ScrollEnabled;
gestures.ScrollMode = view.GestureSettings.ScrollMode.ToNative();
gestures.SimultaneousRotateAndPinchToZoomEnabled = view.GestureSettings.SimultaneousRotateAndPinchToZoomEnabled;
gestures.ZoomAnimationAmount = view.GestureSettings.ZoomAnimationAmount;
}

private static void HandleLightChanged(MapboxViewHandler handler, IMapboxView view)
{
var mapView = handler.GetMapView();
Expand Down Expand Up @@ -197,7 +225,7 @@ private static void HandleMapboxStyleChanged(MapboxViewHandler handler, IMapboxV
styleUri = MapboxMapsStyle.MapboxStreets;
}

handler.GetMapView()?.MapboxMap.LoadStyleUri(styleUri);
handler.GetMapView()?.MapboxMap.LoadStyle(styleUri);
}

protected override PlatformView CreatePlatformView()
Expand Down
30 changes: 23 additions & 7 deletions src/libs/Mapbox.Maui/Platforms/iOS/AdditionalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,20 @@ namespace MapboxMaui;
using MapboxMapsObjC;
using Microsoft.Maui.Controls.Compatibility.Platform.iOS;
using Microsoft.Maui.Platform;
using System.Runtime.InteropServices;
using CoreGraphics;

public static partial class AdditionalExtensions
{
internal static TMBPanMode ToNative(this PanMode panMode)
{
return panMode switch
{
PanMode.Horizontal => TMBPanMode.Horizontal,
PanMode.Vertical => TMBPanMode.Vertical,
_ => TMBPanMode.HorizontalAndVertical,
};
}

internal static NSDictionary<NSString, TMBExpression> ToNative(this IDictionary<string, DslExpression> dictionary)
{
var list = new NSMutableDictionary<NSString, TMBExpression>();
Expand Down Expand Up @@ -278,12 +288,8 @@ public static TMBCameraOptions ToNative(this CameraOptions cameraOptions)
(float)cameraOptions.Padding.Value.Bottom,
(float)cameraOptions.Padding.Value.Right)
: UIKit.UIEdgeInsets.Zero;
var anchor = cameraOptions.Anchor is not null
? new CoreGraphics.CGPoint(
cameraOptions.Anchor.Value.X,
cameraOptions.Anchor.Value.Y
)
: CoreGraphics.CGPoint.Empty;
var anchor = cameraOptions.Anchor?.ToNative()
?? CGPoint.Empty;
var zoom = cameraOptions.Zoom.HasValue
? cameraOptions.Zoom.Value
: 14f;
Expand All @@ -303,5 +309,15 @@ public static TMBCameraOptions ToNative(this CameraOptions cameraOptions)
pitch
);
}

internal static CGPoint ToNative(this ScreenPosition screenPosition)
{
return new CGPoint(screenPosition.X, screenPosition.Y);
}

internal static NSValue ToNSValue(this ScreenPosition screenPosition)
{
return NSValue.FromCGPoint(screenPosition.ToNative());
}
}

8 changes: 0 additions & 8 deletions src/libs/Mapbox.Maui/Platforms/iOS/GeometryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,6 @@ internal static CGPoint ToCGPoint(this IPosition xobj)
);
}

internal static CGPoint ToCGPoint(this ScreenPosition xobj)
{
return new CGPoint(
xobj.X,
xobj.Y
);
}

public static IPosition ToMapPosition(this CLLocationCoordinate2D point)
{
return new MapPosition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public IPosition GetMapPosition(ScreenPosition position)
if (mapView == null) return null;

var coords = mapView.MapboxMap().CoordinateFor(
position.ToCGPoint()
position.ToNative()
);

return coords.ToMapPosition();
Expand Down
23 changes: 23 additions & 0 deletions src/libs/Mapbox.Maui/Platforms/iOS/MapboxViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@ public partial class MapboxViewHandler
UITapGestureRecognizer mapTapGestureRecognizer;
UILongPressGestureRecognizer mapLongPressGestureRecognizer;

private static void HandleGestureSettingsChanged(MapboxViewHandler handler, IMapboxView view)
{
var mapView = handler.PlatformView.MapView;
if (mapView == null) return;

var gestureOptions = new TMBGestureOptions(
panEnabled: view.GestureSettings.PinchScrollEnabled,
pinchEnabled: false,
rotateEnabled: view.GestureSettings.RotateEnabled,
simultaneousRotateAndPinchZoomEnabled: view.GestureSettings.SimultaneousRotateAndPinchToZoomEnabled,
pinchZoomEnabled: view.GestureSettings.PinchToZoomEnabled,
pinchPanEnabled: false,
pitchEnabled: view.GestureSettings.PitchEnabled,
doubleTapToZoomInEnabled: view.GestureSettings.DoubleTapToZoomInEnabled,
doubleTouchToZoomOutEnabled: view.GestureSettings.DoubleTouchToZoomOutEnabled,
quickZoomEnabled: view.GestureSettings.QuickZoomEnabled,
panDecelerationFactor: UIScrollView.DecelerationRateNormal,
panMode: view.GestureSettings.ScrollMode.ToNative(),
focalPoint: view.GestureSettings.FocalPoint?.ToNSValue()
);
mapView.Gestures().GestureOptions = gestureOptions;
}

private static void HandleLightChanged(MapboxViewHandler handler, IMapboxView view)
{
var mapView = handler.PlatformView.MapView;
Expand Down
2 changes: 2 additions & 0 deletions src/qs/MapboxMauiQs/AppShell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ public partial class AppShell : Shell
public AppShell()
{
InitializeComponent();

Routing.RegisterRoute(nameof(GestureSettingsExample.GestureSettingsExampleSettingsPage), typeof(GestureSettingsExample.GestureSettingsExampleSettingsPage));
}
}

Loading
Loading