From c49095ef8c27bf0a30576e0c21a2c074fcf82dd5 Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Tue, 21 May 2024 17:39:03 +0200 Subject: [PATCH] Reset transform center before Center animation --- MapControl/Avalonia/MapBase.Avalonia.cs | 16 ++++++- .../Avalonia/MapControl.Avalonia.csproj | 1 + MapControl/Shared/MapBase.cs | 2 +- MapControl/Shared/MapImageLayer.cs | 44 +++++++++++-------- MapControl/Shared/MapPanel.cs | 4 +- MapControl/Shared/MapPath.cs | 2 +- MapControl/WPF/MapBase.WPF.cs | 4 +- MapControl/WinUI/MapBase.WinUI.cs | 4 +- 8 files changed, 51 insertions(+), 26 deletions(-) diff --git a/MapControl/Avalonia/MapBase.Avalonia.cs b/MapControl/Avalonia/MapBase.Avalonia.cs index bfc08ee0..135ddb02 100644 --- a/MapControl/Avalonia/MapBase.Avalonia.cs +++ b/MapControl/Avalonia/MapBase.Avalonia.cs @@ -142,6 +142,8 @@ namespace MapControl { if (!internalPropertyChange && !targetCenter.Equals(Center)) { + ResetTransformCenter(); + centerCts?.Cancel(); centerAnimation = new Animation @@ -154,7 +156,7 @@ namespace MapControl new KeyFrame { KeyTime = AnimationDuration, - Setters = { new Setter(CenterProperty, new Location(targetCenter.Latitude, ConstrainedLongitude(targetCenter.Longitude))) } + Setters = { new Setter(CenterProperty, new Location(targetCenter.Latitude, CoerceLongitude(targetCenter.Longitude))) } } } }; @@ -163,6 +165,11 @@ namespace MapControl await centerAnimation.RunAsync(this, centerCts.Token); + if (!centerCts.IsCancellationRequested) + { + UpdateTransform(); + } + centerCts.Dispose(); centerCts = null; centerAnimation = null; @@ -225,7 +232,7 @@ namespace MapControl if (!zoomLevelCts.IsCancellationRequested) { - UpdateTransform(true); + UpdateTransform(true); // reset transform center } zoomLevelCts.Dispose(); @@ -285,6 +292,11 @@ namespace MapControl await headingAnimation.RunAsync(this, headingCts.Token); + if (!headingCts.IsCancellationRequested) + { + UpdateTransform(); + } + headingCts.Dispose(); headingCts = null; headingAnimation = null; diff --git a/MapControl/Avalonia/MapControl.Avalonia.csproj b/MapControl/Avalonia/MapControl.Avalonia.csproj index c1de38d4..758530db 100644 --- a/MapControl/Avalonia/MapControl.Avalonia.csproj +++ b/MapControl/Avalonia/MapControl.Avalonia.csproj @@ -23,6 +23,7 @@ + diff --git a/MapControl/Shared/MapBase.cs b/MapControl/Shared/MapBase.cs index ad677442..375a8675 100644 --- a/MapControl/Shared/MapBase.cs +++ b/MapControl/Shared/MapBase.cs @@ -372,7 +372,7 @@ namespace MapControl } } - internal double ConstrainedLongitude(double longitude) + internal double CoerceLongitude(double longitude) { var offset = longitude - Center.Longitude; diff --git a/MapControl/Shared/MapImageLayer.cs b/MapControl/Shared/MapImageLayer.cs index 730495a1..5c9a47ac 100644 --- a/MapControl/Shared/MapImageLayer.cs +++ b/MapControl/Shared/MapImageLayer.cs @@ -6,7 +6,13 @@ using System; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; -#if WINUI +#if AVALONIA +using Avalonia.Controls; +using Avalonia.Media; +using Avalonia.Threading; +using DependencyProperty = Avalonia.AvaloniaProperty; +using ImageSource = Avalonia.Media.IImage; +#elif WINUI using Windows.Foundation; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; @@ -35,27 +41,27 @@ namespace MapControl /// public abstract class MapImageLayer : MapPanel, IMapLayer { - public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register( - nameof(Description), typeof(string), typeof(MapImageLayer), new PropertyMetadata(null)); + public static readonly DependencyProperty DescriptionProperty = + DependencyPropertyHelper.Register(nameof(Description)); - public static readonly DependencyProperty RelativeImageSizeProperty = DependencyProperty.Register( - nameof(RelativeImageSize), typeof(double), typeof(MapImageLayer), new PropertyMetadata(1d)); + public static readonly DependencyProperty RelativeImageSizeProperty = + DependencyPropertyHelper.Register(nameof(RelativeImageSize), 1d); - public static readonly DependencyProperty UpdateIntervalProperty = DependencyProperty.Register( - nameof(UpdateInterval), typeof(TimeSpan), typeof(MapImageLayer), - new PropertyMetadata(TimeSpan.FromSeconds(0.2), (o, e) => ((MapImageLayer)o).updateTimer.Interval = (TimeSpan)e.NewValue)); + public static readonly DependencyProperty UpdateIntervalProperty = + DependencyPropertyHelper.Register(nameof(UpdateInterval), TimeSpan.FromSeconds(0.2), + false, (layer, oldValue, newValue) => layer.updateTimer.Interval = newValue); - public static readonly DependencyProperty UpdateWhileViewportChangingProperty = DependencyProperty.Register( - nameof(UpdateWhileViewportChanging), typeof(bool), typeof(MapImageLayer), new PropertyMetadata(false)); + public static readonly DependencyProperty UpdateWhileViewportChangingProperty = + DependencyPropertyHelper.Register(nameof(UpdateWhileViewportChanging)); - public static readonly DependencyProperty MapBackgroundProperty = DependencyProperty.Register( - nameof(MapBackground), typeof(Brush), typeof(MapImageLayer), new PropertyMetadata(null)); + public static readonly DependencyProperty MapBackgroundProperty = + DependencyPropertyHelper.Register(nameof(MapBackground)); - public static readonly DependencyProperty MapForegroundProperty = DependencyProperty.Register( - nameof(MapForeground), typeof(Brush), typeof(MapImageLayer), new PropertyMetadata(null)); + public static readonly DependencyProperty MapForegroundProperty = + DependencyPropertyHelper.Register(nameof(MapForeground)); - public static readonly DependencyProperty LoadingProgressProperty = DependencyProperty.Register( - nameof(LoadingProgress), typeof(double), typeof(MapImageLayer), new PropertyMetadata(1d)); + public static readonly DependencyProperty LoadingProgressProperty = + DependencyPropertyHelper.Register( nameof(LoadingProgress), 1d); private readonly Progress loadingProgress; private readonly DispatcherTimer updateTimer; @@ -63,7 +69,7 @@ namespace MapControl public MapImageLayer() { - loadingProgress = new Progress(p => LoadingProgress = p); + loadingProgress = new Progress(p => SetValue(LoadingProgressProperty, p)); updateTimer = this.CreateTimer(UpdateInterval); updateTimer.Tick += async (s, e) => await UpdateImageAsync(); @@ -131,7 +137,6 @@ namespace MapControl public double LoadingProgress { get => (double)GetValue(LoadingProgressProperty); - private set => SetValue(LoadingProgressProperty, value); } protected override void SetParentMap(MapBase map) @@ -249,6 +254,8 @@ namespace MapControl topImage.Source = image; SetBoundingBox(topImage, boundingBox); +#if AVALONIA +#else topImage.BeginAnimation(OpacityProperty, new DoubleAnimation { To = 1d, @@ -261,6 +268,7 @@ namespace MapControl BeginTime = MapBase.ImageFadeDuration, Duration = TimeSpan.Zero }); +#endif } } } diff --git a/MapControl/Shared/MapPanel.cs b/MapControl/Shared/MapPanel.cs index 97f09c79..b645e71b 100644 --- a/MapControl/Shared/MapPanel.cs +++ b/MapControl/Shared/MapPanel.cs @@ -236,7 +236,7 @@ namespace MapControl IsOutsideViewport(position.Value)) { position = parentMap.LocationToView( - new Location(location.Latitude, parentMap.ConstrainedLongitude(location.Longitude))); + new Location(location.Latitude, parentMap.CoerceLongitude(location.Longitude))); } return position; @@ -267,7 +267,7 @@ namespace MapControl if (location != null) { var pos = parentMap.LocationToView( - new Location(location.Latitude, parentMap.ConstrainedLongitude(location.Longitude))); + new Location(location.Latitude, parentMap.CoerceLongitude(location.Longitude))); if (pos.HasValue) { diff --git a/MapControl/Shared/MapPath.cs b/MapControl/Shared/MapPath.cs index 45e44a58..aba111f7 100644 --- a/MapControl/Shared/MapPath.cs +++ b/MapControl/Shared/MapPath.cs @@ -132,7 +132,7 @@ namespace MapControl (point.Value.X < 0d || point.Value.X > parentMap.RenderSize.Width || point.Value.Y < 0d || point.Value.Y > parentMap.RenderSize.Height)) { - longitudeOffset = parentMap.ConstrainedLongitude(location.Longitude) - location.Longitude; + longitudeOffset = parentMap.CoerceLongitude(location.Longitude) - location.Longitude; } } diff --git a/MapControl/WPF/MapBase.WPF.cs b/MapControl/WPF/MapBase.WPF.cs index ae6b89e3..a306ad09 100644 --- a/MapControl/WPF/MapBase.WPF.cs +++ b/MapControl/WPF/MapBase.WPF.cs @@ -129,6 +129,8 @@ namespace MapControl { if (!internalPropertyChange && !targetCenter.Equals(Center)) { + ResetTransformCenter(); + if (centerAnimation != null) { centerAnimation.Completed -= CenterAnimationCompleted; @@ -136,7 +138,7 @@ namespace MapControl centerAnimation = new LocationAnimation { - To = new Location(targetCenter.Latitude, ConstrainedLongitude(targetCenter.Longitude)), + To = new Location(targetCenter.Latitude, CoerceLongitude(targetCenter.Longitude)), Duration = AnimationDuration, EasingFunction = AnimationEasingFunction }; diff --git a/MapControl/WinUI/MapBase.WinUI.cs b/MapControl/WinUI/MapBase.WinUI.cs index a86da6a0..ff42c963 100644 --- a/MapControl/WinUI/MapBase.WinUI.cs +++ b/MapControl/WinUI/MapBase.WinUI.cs @@ -148,6 +148,8 @@ namespace MapControl { if (!internalPropertyChange) { + ResetTransformCenter(); + var targetCenter = CoerceCenterProperty(value); if (!targetCenter.Equals(value)) @@ -165,7 +167,7 @@ namespace MapControl centerAnimation = new PointAnimation { From = new Windows.Foundation.Point(Center.Longitude, Center.Latitude), - To = new Windows.Foundation.Point(ConstrainedLongitude(targetCenter.Longitude), targetCenter.Latitude), + To = new Windows.Foundation.Point(CoerceLongitude(targetCenter.Longitude), targetCenter.Latitude), Duration = AnimationDuration, EasingFunction = AnimationEasingFunction, EnableDependentAnimation = true