Implemented LocationAnimator

This commit is contained in:
ClemensFischer 2024-05-21 00:00:30 +02:00
parent 3706709cfc
commit abe3bb75f9
3 changed files with 68 additions and 21 deletions

View file

@ -10,7 +10,9 @@ namespace MapControl
{ {
public override Location Interpolate(double progress, Location oldValue, Location newValue) public override Location Interpolate(double progress, Location oldValue, Location newValue)
{ {
throw new System.NotImplementedException(); return new Location(
(1d - progress) * oldValue.Latitude + progress * newValue.Latitude,
(1d - progress) * oldValue.Longitude + progress * newValue.Longitude);
} }
} }
} }

View file

@ -59,11 +59,16 @@ namespace MapControl
static MapBase() static MapBase()
{ {
Animation.RegisterCustomAnimator<Location, LocationAnimator>();
ClipToBoundsProperty.OverrideDefaultValue(typeof(MapBase), true); ClipToBoundsProperty.OverrideDefaultValue(typeof(MapBase), true);
CenterProperty.Changed.AddClassHandler<MapBase, Location>( CenterProperty.Changed.AddClassHandler<MapBase, Location>(
(map, args) => map.CenterPropertyChanged(args.NewValue.Value)); (map, args) => map.CenterPropertyChanged(args.NewValue.Value));
TargetCenterProperty.Changed.AddClassHandler<MapBase, Location>(
async (map, args) => await map.TargetCenterPropertyChanged(args.NewValue.Value));
ZoomLevelProperty.Changed.AddClassHandler<MapBase, double>( ZoomLevelProperty.Changed.AddClassHandler<MapBase, double>(
(map, args) => map.ZoomLevelPropertyChanged(args.NewValue.Value)); (map, args) => map.ZoomLevelPropertyChanged(args.NewValue.Value));
@ -161,6 +166,41 @@ namespace MapControl
if (!internalPropertyChange) if (!internalPropertyChange)
{ {
UpdateTransform(); UpdateTransform();
if (centerAnimation == null)
{
SetValueInternal(TargetCenterProperty, center);
}
}
}
private async Task TargetCenterPropertyChanged(Location targetCenter)
{
if (!internalPropertyChange && !targetCenter.Equals(Center))
{
centerCts?.Cancel();
centerAnimation = new Animation
{
FillMode = FillMode.Forward,
Duration = AnimationDuration,
Children =
{
new KeyFrame
{
KeyTime = AnimationDuration,
Setters = { new Setter(CenterProperty, new Location(targetCenter.Latitude, ConstrainedLongitude(targetCenter.Longitude))) }
}
}
};
centerCts = new CancellationTokenSource();
await centerAnimation.RunAsync(this, centerCts.Token);
centerCts.Dispose();
centerCts = null;
centerAnimation = null;
} }
} }

View file

@ -15,6 +15,7 @@
<PackageId>XAML.MapControl</PackageId> <PackageId>XAML.MapControl</PackageId>
<DefineConstants>AVALONIA</DefineConstants> <DefineConstants>AVALONIA</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="..\Shared\BoundingBox.cs" Link="BoundingBox.cs" /> <Compile Include="..\Shared\BoundingBox.cs" Link="BoundingBox.cs" />
<Compile Include="..\Shared\ImageFileCache.cs" Link="ImageFileCache.cs" /> <Compile Include="..\Shared\ImageFileCache.cs" Link="ImageFileCache.cs" />
@ -38,6 +39,10 @@
<Compile Include="..\WPF\TypeConverters.WPF.cs" Link="TypeConverters.WPF.cs" /> <Compile Include="..\WPF\TypeConverters.WPF.cs" Link="TypeConverters.WPF.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="..\..\MapControl.snk" Link="MapControl.snk" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Avalonia" Version="11.0.10" /> <PackageReference Include="Avalonia" Version="11.0.10" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />