Revised animations.

This commit is contained in:
ClemensF 2017-11-10 17:26:15 +01:00
parent 1861e9ca5d
commit b3665b92c0
4 changed files with 69 additions and 77 deletions

View file

@ -321,15 +321,15 @@ namespace MapControl
if (rotation != 0d)
{
var heading = (((Heading + rotation) % 360d) + 360d) % 360d;
InternalSetValue(HeadingProperty, heading);
InternalSetValue(TargetHeadingProperty, heading);
SetValueInternal(HeadingProperty, heading);
SetValueInternal(TargetHeadingProperty, heading);
}
if (scale != 1d)
{
var zoomLevel = Math.Min(Math.Max(ZoomLevel + Math.Log(scale, 2d), MinZoomLevel), MaxZoomLevel);
InternalSetValue(ZoomLevelProperty, zoomLevel);
InternalSetValue(TargetZoomLevelProperty, zoomLevel);
SetValueInternal(ZoomLevelProperty, zoomLevel);
SetValueInternal(TargetZoomLevelProperty, zoomLevel);
}
UpdateTransform(true);
@ -355,6 +355,7 @@ namespace MapControl
if (double.IsNaN(MapProjection.LongitudeScale))
{
ZoomLevel = zoomLevel;
ResetTransformCenter();
}
else
{
@ -444,7 +445,7 @@ namespace MapControl
if (center == null)
{
center = new Location();
InternalSetValue(property, center);
SetValueInternal(property, center);
}
else if (center.Longitude < -180d || center.Longitude > 180d ||
center.Latitude < -MapProjection.MaxLatitude || center.Latitude > MapProjection.MaxLatitude)
@ -452,7 +453,7 @@ namespace MapControl
center = new Location(
Math.Min(Math.Max(center.Latitude, -MapProjection.MaxLatitude), MapProjection.MaxLatitude),
Location.NormalizeLongitude(center.Longitude));
InternalSetValue(property, center);
SetValueInternal(property, center);
}
}
@ -465,8 +466,7 @@ namespace MapControl
if (centerAnimation == null)
{
InternalSetValue(TargetCenterProperty, center);
InternalSetValue(CenterPointProperty, MapProjection.LocationToPoint(center));
SetValueInternal(TargetCenterProperty, center);
}
}
}
@ -484,19 +484,16 @@ namespace MapControl
centerAnimation.Completed -= CenterAnimationCompleted;
}
// animate private CenterPoint property by PointAnimation
centerAnimation = new PointAnimation
{
From = MapProjection.LocationToPoint(Center),
To = MapProjection.LocationToPoint(new Location(
targetCenter.Latitude,
Location.NearestLongitude(targetCenter.Longitude, Center.Longitude))),
From = new Point(Center.Longitude, Center.Latitude),
To = new Point(Location.NearestLongitude(targetCenter.Longitude, Center.Longitude), targetCenter.Latitude),
Duration = AnimationDuration,
EasingFunction = AnimationEasingFunction,
FillBehavior = AnimationFillBehavior
EasingFunction = AnimationEasingFunction
};
centerAnimation.Completed += CenterAnimationCompleted;
this.BeginAnimation(CenterPointProperty, centerAnimation);
}
}
@ -509,20 +506,15 @@ namespace MapControl
centerAnimation.Completed -= CenterAnimationCompleted;
centerAnimation = null;
InternalSetValue(CenterProperty, TargetCenter);
InternalSetValue(CenterPointProperty, MapProjection.LocationToPoint(TargetCenter));
UpdateTransform();
this.BeginAnimation(CenterPointProperty, null);
}
}
private void CenterPointPropertyChanged(Point centerPoint)
{
if (!internalPropertyChange)
if (centerAnimation != null)
{
var center = MapProjection.PointToLocation(centerPoint);
center.Longitude = Location.NormalizeLongitude(center.Longitude);
InternalSetValue(CenterProperty, center);
SetValueInternal(CenterProperty, new Location(centerPoint.Y, centerPoint.X));
UpdateTransform();
}
}
@ -532,7 +524,7 @@ namespace MapControl
if (minZoomLevel < 0d || minZoomLevel > MaxZoomLevel)
{
minZoomLevel = Math.Min(Math.Max(minZoomLevel, 0d), MaxZoomLevel);
InternalSetValue(MinZoomLevelProperty, minZoomLevel);
SetValueInternal(MinZoomLevelProperty, minZoomLevel);
}
if (ZoomLevel < minZoomLevel)
@ -546,7 +538,7 @@ namespace MapControl
if (maxZoomLevel < MinZoomLevel || maxZoomLevel > MaximumZoomLevel)
{
maxZoomLevel = Math.Min(Math.Max(maxZoomLevel, MinZoomLevel), MaximumZoomLevel);
InternalSetValue(MaxZoomLevelProperty, maxZoomLevel);
SetValueInternal(MaxZoomLevelProperty, maxZoomLevel);
}
if (ZoomLevel > maxZoomLevel)
@ -560,7 +552,7 @@ namespace MapControl
if (zoomLevel < MinZoomLevel || zoomLevel > MaxZoomLevel)
{
zoomLevel = Math.Min(Math.Max(zoomLevel, MinZoomLevel), MaxZoomLevel);
InternalSetValue(property, zoomLevel);
SetValueInternal(property, zoomLevel);
}
}
@ -573,7 +565,7 @@ namespace MapControl
if (zoomLevelAnimation == null)
{
InternalSetValue(TargetZoomLevelProperty, zoomLevel);
SetValueInternal(TargetZoomLevelProperty, zoomLevel);
}
}
}
@ -595,11 +587,11 @@ namespace MapControl
{
To = targetZoomLevel,
Duration = AnimationDuration,
EasingFunction = AnimationEasingFunction,
FillBehavior = AnimationFillBehavior
EasingFunction = AnimationEasingFunction
};
zoomLevelAnimation.Completed += ZoomLevelAnimationCompleted;
this.BeginAnimation(ZoomLevelProperty, zoomLevelAnimation);
}
}
@ -609,11 +601,13 @@ namespace MapControl
{
if (zoomLevelAnimation != null)
{
SetValueInternal(ZoomLevelProperty, TargetZoomLevel);
UpdateTransform(true);
zoomLevelAnimation.Completed -= ZoomLevelAnimationCompleted;
zoomLevelAnimation = null;
InternalSetValue(ZoomLevelProperty, TargetZoomLevel);
UpdateTransform(true);
this.BeginAnimation(ZoomLevelProperty, null);
}
}
@ -622,7 +616,7 @@ namespace MapControl
if (heading < 0d || heading > 360d)
{
heading = ((heading % 360d) + 360d) % 360d;
InternalSetValue(property, heading);
SetValueInternal(property, heading);
}
}
@ -635,7 +629,7 @@ namespace MapControl
if (headingAnimation == null)
{
InternalSetValue(TargetHeadingProperty, heading);
SetValueInternal(TargetHeadingProperty, heading);
}
}
}
@ -668,11 +662,11 @@ namespace MapControl
{
By = delta,
Duration = AnimationDuration,
EasingFunction = AnimationEasingFunction,
FillBehavior = AnimationFillBehavior
EasingFunction = AnimationEasingFunction
};
headingAnimation.Completed += HeadingAnimationCompleted;
this.BeginAnimation(HeadingProperty, headingAnimation);
}
}
@ -682,15 +676,17 @@ namespace MapControl
{
if (headingAnimation != null)
{
SetValueInternal(HeadingProperty, TargetHeading);
UpdateTransform();
headingAnimation.Completed -= HeadingAnimationCompleted;
headingAnimation = null;
InternalSetValue(HeadingProperty, TargetHeading);
UpdateTransform();
this.BeginAnimation(HeadingProperty, null);
}
}
private void InternalSetValue(DependencyProperty property, object value)
private void SetValueInternal(DependencyProperty property, object value)
{
internalPropertyChange = true;
SetValue(property, value);
@ -715,12 +711,11 @@ namespace MapControl
resetTransformCenter = true;
}
InternalSetValue(CenterProperty, center);
SetValueInternal(CenterProperty, center);
if (centerAnimation == null)
{
InternalSetValue(TargetCenterProperty, center);
InternalSetValue(CenterPointProperty, projection.LocationToPoint(center));
SetValueInternal(TargetCenterProperty, center);
}
if (resetTransformCenter)

View file

@ -2,7 +2,6 @@
// © 2017 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System.Collections.Generic;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
@ -17,37 +16,41 @@ namespace MapControl
return transform.TransformPoint(point);
}
public static void BeginAnimation(this DependencyObject obj, DependencyProperty property, DoubleAnimation animation)
public static void BeginAnimation(this DependencyObject obj, DependencyProperty property, Timeline animation)
{
animation.EnableDependentAnimation = true;
BeginAnimation(obj, property, (Timeline)animation);
}
public static void BeginAnimation(this DependencyObject obj, DependencyProperty property, PointAnimation animation)
{
animation.EnableDependentAnimation = true;
BeginAnimation(obj, property, (Timeline)animation);
}
private static Dictionary<DependencyProperty, string> properties = new Dictionary<DependencyProperty, string>()
{
{ UIElement.OpacityProperty, "Opacity" },
{ MapBase.ZoomLevelProperty, "ZoomLevel" },
{ MapBase.HeadingProperty, "Heading" },
{ MapBase.CenterPointProperty, "CenterPoint" }
};
private static void BeginAnimation(DependencyObject obj, DependencyProperty property, Timeline animation)
{
string propertyName;
if (properties.TryGetValue(property, out propertyName))
if (animation != null)
{
Storyboard.SetTargetProperty(animation, propertyName);
Storyboard.SetTarget(animation, obj);
var storyboard = new Storyboard();
storyboard.Children.Add(animation);
storyboard.Begin();
string propertyName = null;
if (property == MapBase.CenterPointProperty)
{
propertyName = "CenterPoint";
((PointAnimation)animation).EnableDependentAnimation = true;
}
else if (property == MapBase.ZoomLevelProperty)
{
propertyName = "ZoomLevel";
((DoubleAnimation)animation).EnableDependentAnimation = true;
}
else if (property == MapBase.HeadingProperty)
{
propertyName = "Heading";
((DoubleAnimation)animation).EnableDependentAnimation = true;
}
else if (property == UIElement.OpacityProperty)
{
propertyName = "Opacity";
}
if (propertyName != null)
{
Storyboard.SetTargetProperty(animation, propertyName);
Storyboard.SetTarget(animation, obj);
var storyboard = new Storyboard();
storyboard.Children.Add(animation);
storyboard.Begin();
}
}
}
}

View file

@ -6,14 +6,11 @@ using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
namespace MapControl
{
public partial class MapBase
{
private const FillBehavior AnimationFillBehavior = FillBehavior.HoldEnd;
public static readonly DependencyProperty ForegroundProperty = DependencyProperty.Register(
nameof(Foreground), typeof(Brush), typeof(MapBase),
new PropertyMetadata(new SolidColorBrush(Colors.Black)));

View file

@ -5,14 +5,11 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace MapControl
{
public partial class MapBase
{
private const FillBehavior AnimationFillBehavior = FillBehavior.Stop;
public static readonly DependencyProperty ForegroundProperty =
Control.ForegroundProperty.AddOwner(typeof(MapBase));