Code refactoring, switched to previous EasingType

This commit is contained in:
Sergio Pedri 2020-12-17 22:18:38 +01:00
Родитель 9d874a8913
Коммит 1cd580269b
13 изменённых файлов: 67 добавлений и 1291 удалений

Просмотреть файл

@ -1,67 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Microsoft.Toolkit.Uwp.UI.Animations
{
/// <summary>
/// EasingType is used to describe how the animation interpolates between keyframes.
/// </summary>
public enum EasingType
{
/// <summary>
/// Creates an animation that accelerates with the default EasingType which is specified in AnimationExtensions.DefaultEasingType which is by default Cubic.
/// </summary>
Default,
/// <summary>
/// Creates an animation that accelerates or decelerates linearly.
/// </summary>
Linear,
/// <summary>
/// Creates an animation that accelerates or decelerates using the formula f(t) = t3.
/// </summary>
Cubic,
/// <summary>
/// Retracts the motion of an animation slightly before it begins to animate in the path indicated.
/// </summary>
Back,
/// <summary>
/// Creates a bouncing effect.
/// </summary>
Bounce,
/// <summary>
/// Creates an animation that resembles a spring oscillating back and forth until it comes to rest.
/// </summary>
Elastic,
/// <summary>
/// Creates an animation that accelerates or decelerates using a circular function.
/// </summary>
Circle,
/// <summary>
/// Creates an animation that accelerates or decelerates using the formula f(t) = t2.
/// </summary>
Quadratic,
/// <summary>
/// Creates an animation that accelerates or decelerates using the formula f(t) = t4.
/// </summary>
Quartic,
/// <summary>
/// Create an animation that accelerates or decelerates using the formula f(t) = t5.
/// </summary>
Quintic,
/// <summary>
/// Creates an animation that accelerates or decelerates using a sine formula.
/// </summary>
Sine
}
}

Просмотреть файл

@ -1,62 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Microsoft.Toolkit.Uwp.UI.Animations
{
/// <summary>
/// Indicates an easing function for an animation.
/// </summary>
public enum Easing
{
/// <summary>
/// The linear easing, with [0, 0] and [1, 1] as control points.
/// </summary>
Linear,
/// <summary>
/// The sine in easing, with [0.4, 0] and [1, 1] as control points.
/// </summary>
SineEaseIn,
/// <summary>
/// The sine out easing, with [0, 0] and [0.6, 1] as control points.
/// </summary>
SineEaseOut,
/// <summary>
/// The sine in out easing, with [0.4, 0] and [0.6, 1] as control points.
/// </summary>
SineEaseInOut,
/// <summary>
/// The quadratic in easing, with [0.8, 0] and [1, 1] as control points.
/// </summary>
QuadraticEaseIn,
/// <summary>
/// The quadratic out easing, with [0, 0] and [0.2, 1] as control points.
/// </summary>
QuadraticEaseOut,
/// <summary>
/// The quadratic in out easing, with [0.8, 0] and [0.2, 1] as control points.
/// </summary>
QuadraticEaseInOut,
/// <summary>
/// The circle in easing, with [1, 0] and [1, 0.8] as control points.
/// </summary>
CircleEaseIn,
/// <summary>
/// The circle out easing, with [0, 0.3] and [0, 1] as control points.
/// </summary>
CircleEaseOut,
/// <summary>
/// The circle in out easing, with [0.9, 0] and [0.1, 1] as control points.
/// </summary>
CircleEaseInOut
}
}

Просмотреть файл

@ -0,0 +1,67 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Microsoft.Toolkit.Uwp.UI.Animations
{
/// <summary>
/// Indicates how the animation interpolates between keyframes.
/// </summary>
public enum EasingType
{
/// <summary>
/// The default easing type, which is specified in AnimationExtensions.DefaultEasingType which is by default Cubic.
/// </summary>
Default,
/// <summary>
/// A linear acceleration and deceleration.
/// </summary>
Linear,
/// <summary>
/// An acceleration or deceleration using the formula f(t) = t3.
/// </summary>
Cubic,
/// <summary>
/// An animation that rectracts its motion slightly before it begins to animate in the path indicated.
/// </summary>
Back,
/// <summary>
/// A bouncing animation.
/// </summary>
Bounce,
/// <summary>
/// An animation that resembles a spring oscillating back and forth until it comes to rest.
/// </summary>
Elastic,
/// <summary>
/// An animation that accelerates or decelerates using a circular function.
/// </summary>
Circle,
/// <summary>
/// An animation that accelerates or decelerates using the formula f(t) = t^2.
/// </summary>
Quadratic,
/// <summary>
/// An animation that accelerates or decelerates using the formula f(t) = t^4.
/// </summary>
Quartic,
/// <summary>
/// An animation that accelerates or decelerates using the formula f(t) = t^5.
/// </summary>
Quintic,
/// <summary>
/// An animation that accelerates or decelerates using a sine formula.
/// </summary>
Sine
}
}

Просмотреть файл

@ -1,76 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.Toolkit.Uwp.UI.Animations.Effects;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media.Animation;
namespace Microsoft.Toolkit.Uwp.UI.Animations
{
/// <summary>
/// These extension methods perform animation on UIElements
/// </summary>
public static partial class AnimationExtensions
{
/// <summary>
/// Gets the blur effect.
/// </summary>
/// <value>
/// The blur effect.
/// </value>
public static Blur BlurEffect { get; } = new Blur();
/// <summary>
/// Animates the Gaussian blur of the UIElement.
/// </summary>
/// <param name="associatedObject">The associated object.</param>
/// <param name="value">The blur amount.</param>
/// <param name="duration">The duration in milliseconds.</param>
/// <param name="delay">The delay. (ignored if duration == 0)</param>
/// <param name="easingType">The easing function</param>
/// <param name="easingMode">The easing mode</param>
/// <returns>
/// An Animation Set.
/// </returns>
public static AnimationSet Blur(
this FrameworkElement associatedObject,
double value = 0d,
double duration = 500d,
double delay = 0d,
EasingType easingType = EasingType.Default,
EasingMode easingMode = EasingMode.EaseOut)
{
if (associatedObject == null)
{
return null;
}
var animationSet = new AnimationSet(associatedObject);
return animationSet.Blur(value, duration, delay, easingType, easingMode);
}
/// <summary>
/// Animates the Gaussian blur of the UIElement.
/// </summary>
/// <param name="animationSet">The animation set.</param>
/// <param name="value">The blur amount.</param>
/// <param name="duration">The duration in milliseconds.</param>
/// <param name="delay">The delay. (ignored if duration == 0)</param>
/// <param name="easingType">The easing function</param>
/// <param name="easingMode">The easing mode</param>
/// <returns>
/// An Animation Set.
/// </returns>
public static AnimationSet Blur(
this AnimationSet animationSet,
double value = 0d,
double duration = 500d,
double delay = 0d,
EasingType easingType = EasingType.Default,
EasingMode easingMode = EasingMode.EaseOut)
{
return BlurEffect.EffectAnimation(animationSet, value, duration, delay, easingType, easingMode);
}
}
}

Просмотреть файл

@ -1,110 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media.Animation;
namespace Microsoft.Toolkit.Uwp.UI.Animations
{
/// <summary>
/// These extension methods perform animation on UIElements
/// </summary>
public static partial class AnimationExtensions
{
/// <summary>
/// Animates the opacity of the UIElement.
/// </summary>
/// <param name="associatedObject">The UI Element to change the opacity of.</param>
/// <param name="value">The fade value, between 0 and 1.</param>
/// <param name="duration">The duration in milliseconds.</param>
/// <param name="delay">The delay. (ignored if duration == 0)</param>
/// <param name="easingType">Used to describe how the animation interpolates between keyframes.</param>
/// <param name="easingMode">The easing mode to use to interpolate between keyframes.</param>
/// <returns>
/// An AnimationSet.
/// </returns>
public static AnimationSet Fade(
this UIElement associatedObject,
float value = 0f,
double duration = 500d,
double delay = 0d,
EasingType easingType = EasingType.Default,
EasingMode easingMode = EasingMode.EaseOut)
{
if (associatedObject == null)
{
return null;
}
var animationSet = new AnimationSet(associatedObject);
return animationSet.Fade(value, duration, delay, easingType, easingMode);
}
/// <summary>
/// Animates the opacity of the UIElement.
/// </summary>
/// <param name="animationSet">The animation set.</param>
/// <param name="value">The fade value, between 0 and 1.</param>
/// <param name="duration">The duration in milliseconds.</param>
/// <param name="delay">The delay. (ignored if duration == 0)</param>
/// <param name="easingType">Used to describe how the animation interpolates between keyframes.</param>
/// <param name="easingMode">The EasingMode to use to interpolate between keyframes.</param>
/// <returns>
/// An AnimationSet.
/// </returns>
public static AnimationSet Fade(
this AnimationSet animationSet,
float value = 0f,
double duration = 500d,
double delay = 0d,
EasingType easingType = EasingType.Default,
EasingMode easingMode = EasingMode.EaseOut)
{
if (animationSet == null)
{
return null;
}
if (!AnimationSet.UseComposition)
{
var animation = new DoubleAnimation
{
To = value,
Duration = TimeSpan.FromMilliseconds(duration),
BeginTime = TimeSpan.FromMilliseconds(delay),
EasingFunction = GetEasingFunction(easingType, easingMode)
};
animationSet.AddStoryboardAnimation("Opacity", animation);
}
else
{
if (duration <= 0)
{
animationSet.AddCompositionDirectPropertyChange("Opacity", value);
return animationSet;
}
var visual = animationSet.Visual;
var compositor = visual?.Compositor;
if (compositor == null)
{
return null;
}
var animation = compositor.CreateScalarKeyFrameAnimation();
animation.Duration = TimeSpan.FromMilliseconds(duration);
animation.DelayTime = TimeSpan.FromMilliseconds(delay);
animation.InsertKeyFrame(1f, value, GetCompositionEasingFunction(easingType, compositor, easingMode));
animationSet.AddCompositionAnimation("Opacity", animation);
}
return animationSet;
}
}
}

Просмотреть файл

@ -1,193 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Effects;
using Microsoft.Toolkit.Uwp.Extensions;
using Windows.UI;
using Windows.UI.Composition;
using Windows.UI.Composition.Effects;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Hosting;
using Windows.UI.Xaml.Media.Animation;
namespace Microsoft.Toolkit.Uwp.UI.Animations
{
/// <summary>
/// Provides an extension which allows lighting.
/// </summary>
public static partial class AnimationExtensions
{
/// <summary>
/// Stores all the point lights along with the visuals that they are applied to.
/// This is to stop multiplication of point lights on a single visual.
/// </summary>
private static Dictionary<Visual, PointLight> pointLights = new Dictionary<Visual, PointLight>();
/// <summary>
/// Animates a point light and it's distance.
/// </summary>
/// <param name="associatedObject">The associated object.</param>
/// <param name="distance">The value.</param>
/// <param name="duration">The duration.</param>
/// <param name="delay">The delay.</param>
/// <param name="color">The color of the spotlight.</param>
/// <param name="easingType">The easing function</param>
/// <param name="easingMode">The easing mode</param>
/// <returns>An animation set.</returns>
[Obsolete("The Light effect will be removed in a future major release. Please use XamlLight instead")]
public static AnimationSet Light(
this FrameworkElement associatedObject,
double distance = 0d,
double duration = 500d,
double delay = 0d,
Color? color = null,
EasingType easingType = EasingType.Default,
EasingMode easingMode = EasingMode.EaseOut)
{
if (associatedObject == null)
{
return null;
}
var animationSet = new AnimationSet(associatedObject);
return animationSet.Light(distance, duration, delay, color, easingType, easingMode);
}
/// <summary>
/// Animates a point light and it's distance.
/// </summary>
/// <param name="animationSet">The animation set.</param>
/// <param name="distance">The distance of the light.</param>
/// <param name="duration">The duration in milliseconds.</param>
/// <param name="delay">The delay. (ignored if duration == 0)</param>
/// <param name="color">The color of the spotlight.</param>
/// <param name="easingType">The easing function</param>
/// <param name="easingMode">The easing mode</param>
/// <returns>
/// An Animation Set.
/// </returns>
[Obsolete("The Light effect will be removed in a future major release. Please use XamlLight instead")]
public static AnimationSet Light(
this AnimationSet animationSet,
double distance = 0d,
double duration = 500d,
double delay = 0d,
Color? color = null,
EasingType easingType = EasingType.Default,
EasingMode easingMode = EasingMode.EaseOut)
{
if (animationSet == null)
{
return null;
}
var visual = animationSet.Visual;
var associatedObject = animationSet.Element as FrameworkElement;
if (associatedObject == null)
{
return animationSet;
}
var compositor = visual?.Compositor;
if (compositor == null)
{
return null;
}
var task = new AnimationTask();
task.AnimationSet = animationSet;
task.Task = visual.DispatcherQueue.EnqueueAsync(
() =>
{
const string sceneName = "PointLightScene";
PointLight pointLight;
CompositionDrawingSurface normalMap = null;
if (!pointLights.ContainsKey(visual))
{
SurfaceLoader.Initialize(compositor);
normalMap = SurfaceLoader.LoadText(string.Empty, new Windows.Foundation.Size(512, 512), new Graphics.Canvas.Text.CanvasTextFormat(), Colors.Transparent, Colors.Transparent);
}
if (pointLights.ContainsKey(visual))
{
pointLight = pointLights[visual];
}
else
{
pointLight = compositor.CreatePointLight();
var normalBrush = compositor.CreateSurfaceBrush(normalMap);
normalBrush.Stretch = CompositionStretch.Fill;
// check to see if the visual already has a point light applied.
var spriteVisual = ElementCompositionPreview.GetElementChildVisual(associatedObject) as SpriteVisual;
var normalsBrush = spriteVisual?.Brush as CompositionEffectBrush;
if (normalsBrush == null || normalsBrush.Comment != sceneName)
{
var lightEffect = new CompositeEffect()
{
Mode = CanvasComposite.Add,
Sources =
{
new CompositionEffectSourceParameter("ImageSource"),
new SceneLightingEffect()
{
Name = sceneName,
AmbientAmount = 0,
DiffuseAmount = 0.5f,
SpecularAmount = 0,
NormalMapSource = new CompositionEffectSourceParameter("NormalMap"),
}
}
};
var effectFactory = compositor.CreateEffectFactory(lightEffect);
var brush = effectFactory.CreateBrush();
brush.SetSourceParameter("NormalMap", normalBrush);
var sprite = compositor.CreateSpriteVisual();
sprite.Size = visual.Size;
sprite.Brush = brush;
sprite.Comment = sceneName;
ElementCompositionPreview.SetElementChildVisual(task.AnimationSet.Element, sprite);
pointLight.CoordinateSpace = visual;
pointLight.Targets.Add(visual);
}
}
pointLight.Color = color ?? Colors.White;
var delayTime = task.Delay != null ? task.Delay.Value : TimeSpan.FromMilliseconds(delay);
var durationTime = task.Duration != null ? task.Duration.Value : TimeSpan.FromMilliseconds(duration);
if (durationTime.TotalMilliseconds <= 0)
{
task.AnimationSet.AddEffectDirectPropertyChange(pointLight, (float)distance, nameof(pointLight.Offset));
}
else
{
var diffuseAnimation = compositor.CreateVector3KeyFrameAnimation();
diffuseAnimation.InsertKeyFrame(1f, new System.Numerics.Vector3(visual.Size.X / 2, visual.Size.Y / 2, (float)distance), GetCompositionEasingFunction(easingType, compositor, easingMode));
diffuseAnimation.Duration = durationTime;
diffuseAnimation.DelayTime = delayTime;
task.AnimationSet.AddCompositionEffectAnimation(pointLight, diffuseAnimation, nameof(pointLight.Offset));
}
pointLights[visual] = pointLight;
});
animationSet.AddAnimationThroughTask(task);
return animationSet;
}
}
}

Просмотреть файл

@ -1,122 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Numerics;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media.Animation;
namespace Microsoft.Toolkit.Uwp.UI.Animations
{
/// <summary>
/// These extension methods perform animation on UIElements
/// </summary>
public static partial class AnimationExtensions
{
/// <summary>
/// Animates the offset of the UIElement.
/// </summary>
/// <param name="associatedObject">The specified UI Element.</param>
/// <param name="offsetX">The offset on the x axis.</param>
/// <param name="offsetY">The offset on the y axis.</param>
/// <param name="duration">The duration in milliseconds.</param>
/// <param name="delay">The delay in milliseconds. (ignored if duration == 0)</param>
/// <param name="easingType">Used to describe how the animation interpolates between keyframes.</param>
/// <param name="easingMode">The EasingMode to use to interpolate between keyframes.</param>
/// <returns>
/// An AnimationSet.
/// </returns>
public static AnimationSet Offset(
this UIElement associatedObject,
float offsetX = 0f,
float offsetY = 0f,
double duration = 500d,
double delay = 0d,
EasingType easingType = EasingType.Default,
EasingMode easingMode = EasingMode.EaseOut)
{
if (associatedObject == null)
{
return null;
}
var animationSet = new AnimationSet(associatedObject);
return animationSet.Offset(offsetX, offsetY, duration, delay, easingType, easingMode);
}
/// <summary>
/// Animates the offset of the UIElement.
/// </summary>
/// <param name="animationSet">The animation set.</param>
/// <param name="offsetX">The offset on the x axis.</param>
/// <param name="offsetY">The offset on the y axis.</param>
/// <param name="duration">The duration in milliseconds.</param>
/// <param name="delay">The delay in milliseconds. (ignored if duration == 0)</param>
/// <param name="easingType">Used to describe how the animation interpolates between keyframes.</param>
/// <param name="easingMode">The EasingMode to use to interpolate between keyframes.</param>
/// <returns>
/// An AnimationSet.
/// </returns>
public static AnimationSet Offset(
this AnimationSet animationSet,
float offsetX = 0f,
float offsetY = 0f,
double duration = 500d,
double delay = 0d,
EasingType easingType = EasingType.Default,
EasingMode easingMode = EasingMode.EaseOut)
{
if (animationSet == null)
{
return null;
}
if (!AnimationSet.UseComposition)
{
var element = animationSet.Element;
var transform = GetAttachedCompositeTransform(element);
var animationX = new DoubleAnimation();
var animationY = new DoubleAnimation();
animationX.To = offsetX;
animationY.To = offsetY;
animationX.Duration = animationY.Duration = TimeSpan.FromMilliseconds(duration);
animationX.BeginTime = animationY.BeginTime = TimeSpan.FromMilliseconds(delay);
animationX.EasingFunction = animationY.EasingFunction = GetEasingFunction(easingType, easingMode);
animationSet.AddStoryboardAnimation(GetAnimationPath(transform, element, "TranslateX"), animationX);
animationSet.AddStoryboardAnimation(GetAnimationPath(transform, element, "TranslateY"), animationY);
}
else
{
var visual = animationSet.Visual;
var offsetVector = new Vector3(offsetX, offsetY, 0);
if (duration <= 0)
{
animationSet.AddCompositionDirectPropertyChange("Offset", offsetVector);
return animationSet;
}
var compositor = visual?.Compositor;
if (compositor == null)
{
return null;
}
var animation = compositor.CreateVector3KeyFrameAnimation();
animation.Duration = TimeSpan.FromMilliseconds(duration);
animation.DelayTime = TimeSpan.FromMilliseconds(delay);
animation.InsertKeyFrame(1f, offsetVector, GetCompositionEasingFunction(easingType, compositor, easingMode));
animationSet.AddCompositionAnimation("Offset", animation);
}
return animationSet;
}
}
}

Просмотреть файл

@ -1,126 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Numerics;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media.Animation;
namespace Microsoft.Toolkit.Uwp.UI.Animations
{
/// <summary>
/// These extension methods perform animation on UIElements
/// </summary>
public static partial class AnimationExtensions
{
/// <summary>
/// Animates the rotation in degrees of the UIElement.
/// </summary>
/// <param name="associatedObject">The UI Element to rotate.</param>
/// <param name="value">The value in degrees to rotate.</param>
/// <param name="centerX">The center x in pixels.</param>
/// <param name="centerY">The center y in pixels.</param>
/// <param name="duration">The duration in milliseconds.</param>
/// <param name="delay">The delay in milliseconds. (ignored if duration == 0)</param>
/// <param name="easingType">Used to describe how the animation interpolates between keyframes.</param>
/// <param name="easingMode">EasingMode used to interpolate between keyframes.</param>
/// <returns>
/// An AnimationSet.
/// </returns>
public static AnimationSet Rotate(
this UIElement associatedObject,
float value = 0f,
float centerX = 0f,
float centerY = 0f,
double duration = 500d,
double delay = 0d,
EasingType easingType = EasingType.Default,
EasingMode easingMode = EasingMode.EaseOut)
{
if (associatedObject == null)
{
return null;
}
var animationSet = new AnimationSet(associatedObject);
return animationSet.Rotate(value, centerX, centerY, duration, delay, easingType);
}
/// <summary>
/// Animates the rotation in degrees of the UIElement.
/// </summary>
/// <param name="animationSet">The animation set.</param>
/// <param name="value">The value in degrees to rotate.</param>
/// <param name="centerX">The center x in pixels.</param>
/// <param name="centerY">The center y in pixels.</param>
/// <param name="duration">The duration in milliseconds.</param>
/// <param name="delay">The delay in milliseconds. (ignored if duration == 0)</param>
/// <param name="easingType">Used to describe how the animation interpolates between keyframes.</param>
/// <param name="easingMode">The EasingMode to use to interpolate between keyframes.</param>
/// <returns>
/// An AnimationSet.
/// </returns>
public static AnimationSet Rotate(
this AnimationSet animationSet,
float value = 0f,
float centerX = 0f,
float centerY = 0f,
double duration = 500d,
double delay = 0d,
EasingType easingType = EasingType.Default,
EasingMode easingMode = EasingMode.EaseOut)
{
if (animationSet == null)
{
return null;
}
if (!AnimationSet.UseComposition)
{
var element = animationSet.Element;
var transform = GetAttachedCompositeTransform(element);
transform.CenterX = centerX;
transform.CenterY = centerY;
var animation = new DoubleAnimation
{
To = value,
Duration = TimeSpan.FromMilliseconds(duration),
BeginTime = TimeSpan.FromMilliseconds(delay),
EasingFunction = GetEasingFunction(easingType, easingMode)
};
animationSet.AddStoryboardAnimation(GetAnimationPath(transform, element, "Rotation"), animation);
}
else
{
var visual = animationSet.Visual;
visual.CenterPoint = new Vector3(centerX, centerY, 0);
if (duration <= 0)
{
animationSet.AddCompositionDirectPropertyChange("RotationAngleInDegrees", value);
return animationSet;
}
var compositor = visual.Compositor;
if (compositor == null)
{
return null;
}
var animation = compositor.CreateScalarKeyFrameAnimation();
animation.Duration = TimeSpan.FromMilliseconds(duration);
animation.DelayTime = TimeSpan.FromMilliseconds(delay);
animation.InsertKeyFrame(1f, value, GetCompositionEasingFunction(easingType, compositor, easingMode));
animationSet.AddCompositionAnimation("RotationAngleInDegrees", animation);
}
return animationSet;
}
}
}

Просмотреть файл

@ -1,72 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.Toolkit.Uwp.UI.Animations.Effects;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media.Animation;
namespace Microsoft.Toolkit.Uwp.UI.Animations
{
/// <summary>
/// A partial for the AnimationExtension which includes saturation.
/// </summary>
public static partial class AnimationExtensions
{
/// <summary>
/// Gets the saturation effect.
/// </summary>
/// <value>
/// The saturation effect.
/// </value>
public static Saturation SaturationEffect { get; } = new Saturation();
/// <summary>
/// Saturates the FrameworkElement.
/// </summary>
/// <param name="associatedObject">The associated object.</param>
/// <param name="value">The value, between 0 and 1. 0 is desaturated, 1 is saturated.</param>
/// <param name="duration">The duration in milliseconds.</param>
/// <param name="delay">The delay in milliseconds.</param>
/// <param name="easingType">The <see cref="EasingType"/></param>
/// <param name="easingMode">The <see cref="EasingMode"/></param>
/// <returns>An animation set with saturation effects incorporated.</returns>
public static AnimationSet Saturation(
this FrameworkElement associatedObject,
double value = 0d,
double duration = 500d,
double delay = 0d,
EasingType easingType = EasingType.Default,
EasingMode easingMode = EasingMode.EaseOut)
{
if (associatedObject == null)
{
return null;
}
var animationSet = new AnimationSet(associatedObject);
return animationSet.Saturation(value, duration, delay, easingType, easingMode);
}
/// <summary>
/// Saturates the visual within the animation set.
/// </summary>
/// <param name="animationSet">The animation set.</param>
/// <param name="value">The value. 0 is desaturated, 1 is saturated.</param>
/// <param name="duration">The duration in milliseconds.</param>
/// <param name="delay">The delay in milliseconds.</param>
/// <param name="easingType">The <see cref="EasingType"/></param>
/// <param name="easingMode">The <see cref="EasingMode"/></param>
/// <returns>An animation set with saturation effects incorporated.</returns>
public static AnimationSet Saturation(
this AnimationSet animationSet,
double value = 0d,
double duration = 500d,
double delay = 0d,
EasingType easingType = EasingType.Default,
EasingMode easingMode = EasingMode.EaseOut)
{
return SaturationEffect.EffectAnimation(animationSet, value, duration, delay, easingType, easingMode);
}
}
}

Просмотреть файл

@ -1,134 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Numerics;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media.Animation;
namespace Microsoft.Toolkit.Uwp.UI.Animations
{
/// <summary>
/// These extension methods perform animation on UIElements
/// </summary>
public static partial class AnimationExtensions
{
/// <summary>
/// Animates the scale of the specified UIElement.
/// </summary>
/// <param name="associatedObject">The associated UIElement.</param>
/// <param name="scaleX">The scale on the x axis.</param>
/// <param name="scaleY">The scale on the y axis.</param>
/// <param name="centerX">The center x in pixels.</param>
/// <param name="centerY">The center y in pixels.</param>
/// <param name="duration">The duration in millisecond.</param>
/// <param name="delay">The delay in milliseconds. (ignored if duration == 0)</param>
/// <param name="easingType">Used to describe how the animation interpolates between keyframes.</param>
/// <param name="easingMode">The EasingMode to use to interpolate between keyframes.</param>
/// <returns>
/// An AnimationSet.
/// </returns>
public static AnimationSet Scale(
this UIElement associatedObject,
float scaleX = 1f,
float scaleY = 1f,
float centerX = 0f,
float centerY = 0f,
double duration = 500d,
double delay = 0d,
EasingType easingType = EasingType.Default,
EasingMode easingMode = EasingMode.EaseOut)
{
if (associatedObject == null)
{
return null;
}
var animationSet = new AnimationSet(associatedObject);
return animationSet.Scale(scaleX, scaleY, centerX, centerY, duration, delay, easingType, easingMode);
}
/// <summary>
/// Animates the scale of the specified UIElement.
/// </summary>
/// <param name="animationSet">The animationSet object.</param>
/// <param name="scaleX">The scale on the x axis.</param>
/// <param name="scaleY">The scale on the y axis.</param>
/// <param name="centerX">The center x in pixels.</param>
/// <param name="centerY">The center y in pixels.</param>
/// <param name="duration">The duration in milliseconds.</param>
/// <param name="delay">The delay in milliseconds. (ignored if duration == 0)</param>
/// <param name="easingType">Used to describe how the animation interpolates between keyframes.</param>
/// <param name="easingMode">The EasingMode to use to interpolate between keyframes.</param>
/// <returns>
/// An AnimationSet.
/// </returns>
public static AnimationSet Scale(
this AnimationSet animationSet,
float scaleX = 1f,
float scaleY = 1f,
float centerX = 0f,
float centerY = 0f,
double duration = 500d,
double delay = 0d,
EasingType easingType = EasingType.Default,
EasingMode easingMode = EasingMode.EaseOut)
{
if (animationSet == null)
{
return null;
}
if (!AnimationSet.UseComposition)
{
var element = animationSet.Element;
var transform = GetAttachedCompositeTransform(element);
transform.CenterX = centerX;
transform.CenterY = centerY;
var animationX = new DoubleAnimation();
var animationY = new DoubleAnimation();
animationX.To = scaleX;
animationY.To = scaleY;
animationX.Duration = animationY.Duration = TimeSpan.FromMilliseconds(duration);
animationX.BeginTime = animationY.BeginTime = TimeSpan.FromMilliseconds(delay);
animationX.EasingFunction = animationY.EasingFunction = GetEasingFunction(easingType, easingMode);
animationSet.AddStoryboardAnimation(GetAnimationPath(transform, element, "ScaleX"), animationX);
animationSet.AddStoryboardAnimation(GetAnimationPath(transform, element, "ScaleY"), animationY);
}
else
{
var visual = animationSet.Visual;
visual.CenterPoint = new Vector3(centerX, centerY, 0);
var scaleVector = new Vector3(scaleX, scaleY, 1.0f);
if (duration <= 0)
{
animationSet.AddCompositionDirectPropertyChange("Scale", scaleVector);
return animationSet;
}
var compositor = visual.Compositor;
if (compositor == null)
{
return null;
}
var animation = compositor.CreateVector3KeyFrameAnimation();
animation.Duration = TimeSpan.FromMilliseconds(duration);
animation.DelayTime = TimeSpan.FromMilliseconds(delay);
animation.InsertKeyFrame(1f, scaleVector, GetCompositionEasingFunction(easingType, compositor, easingMode));
animationSet.AddCompositionAnimation("Scale", animation);
}
return animationSet;
}
}
}

Просмотреть файл

@ -1,282 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Threading.Tasks;
using Windows.UI.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
namespace Microsoft.Toolkit.Uwp.UI.Animations
{
/// <summary>
/// These extension methods perform animation on UIElements
/// </summary>
public static partial class AnimationExtensions
{
#pragma warning disable SA1008 // Opening parenthesis must be spaced correctly
#pragma warning disable SA1009 // Closing parenthesis must be spaced correctly
/// <summary>
/// A cached dictionary mapping easings to bézier control points
/// </summary>
private static readonly Dictionary<(string, EasingMode), (Vector2, Vector2)> _compositionEasingFunctions = new Dictionary<(string, EasingMode), (Vector2, Vector2)>();
#pragma warning restore SA1009 // Closing parenthesis must be spaced correctly
#pragma warning restore SA1008 // Opening parenthesis must be spaced correctly
/// <summary>
/// Gets or sets the default EasingType used for storyboard animations
/// </summary>
public static EasingType DefaultEasingType { get; set; } = EasingType.Cubic;
/// <summary>
/// Begins a Storyboard animation and returns a task that completes when the
/// animation is complete
/// </summary>
/// <param name="storyboard">The storyboard to be started</param>
/// <returns>Task that completes when the animation is complete</returns>
public static Task BeginAsync(this Storyboard storyboard)
{
var taskSource = new TaskCompletionSource<object>();
EventHandler<object> completed = null;
completed += (s, e) =>
{
storyboard.Completed -= completed;
taskSource.SetResult(null);
};
storyboard.Completed += completed;
storyboard.Begin();
return taskSource.Task;
}
/// <summary>
/// Gets the EasingFunction from an EasingType and optional EasingMode to be used with Storyboard animations
/// </summary>
/// <param name="easingType">The EasingType used to determine the EasingFunction</param>
/// <param name="easingMode">The EasingMode used to determine the EasingFunction. Defaults to <see cref="EasingMode.EaseOut"/></param>
/// <returns>Return the appropriate EasingFuntion or null if the EasingType is Linear</returns>
public static EasingFunctionBase GetEasingFunction(EasingType easingType, EasingMode easingMode = EasingMode.EaseOut)
{
if (easingType == EasingType.Default)
{
easingType = DefaultEasingType;
}
switch (easingType)
{
case EasingType.Linear:
return null;
case EasingType.Cubic:
return new CubicEase { EasingMode = easingMode };
case EasingType.Back:
return new BackEase { EasingMode = easingMode };
case EasingType.Bounce:
return new BounceEase { EasingMode = easingMode };
case EasingType.Elastic:
return new ElasticEase { EasingMode = easingMode };
case EasingType.Circle:
return new CircleEase { EasingMode = easingMode };
case EasingType.Quadratic:
return new QuadraticEase { EasingMode = easingMode };
case EasingType.Quartic:
return new QuarticEase { EasingMode = easingMode };
case EasingType.Quintic:
return new QuinticEase { EasingMode = easingMode };
case EasingType.Sine:
return new SineEase { EasingMode = easingMode };
default:
throw new NotSupportedException($"{easingType.ToString()} EasingType is not currently supported");
}
}
/// <summary>
/// Generates an <see cref="CompositionEasingFunction"/> to be used with Composition animations
/// </summary>
/// <param name="easingType">The <see cref="EasingType"/> used to generate the CompositionEasingFunction</param>
/// <param name="compositor">The <see cref="Compositor"/></param>
/// <param name="easingMode">The <see cref="EasingMode"/> used to generate the CompositionEasingFunction. Defaults to <see cref="EasingMode.EaseOut"/></param>
/// <returns><see cref="CompositionEasingFunction"/></returns>
public static CompositionEasingFunction GetCompositionEasingFunction(EasingType easingType, Compositor compositor, EasingMode easingMode = EasingMode.EaseOut)
{
if (DesignTimeHelpers.IsRunningInLegacyDesignerMode)
{
return compositor.CreateLinearEasingFunction();
}
return GenerateCompositionEasingFunctionFromEasingType(easingType, compositor, easingMode);
}
/// <summary>
/// Creates the cache of easing functions if the cache does not already exist.
/// </summary>
private static void EnsureEasingsCached()
{
if (_compositionEasingFunctions.Count > 0)
{
return;
}
// We don't cache actual composition easing functions here as they can be disposed
// and we don't want to deal with caching a disposed easing function
void Add(EasingType type, EasingMode mode, Vector2 p1, Vector2 p2)
{
// In order to generate a usable hash code for our ValueTuple without collisions
// we can't use enum values for both type & mode, so we have to string one of them.
#pragma warning disable SA1008 // Opening parenthesis must be spaced correctly
_compositionEasingFunctions[(type.ToString(), mode)] = (p1, p2);
#pragma warning restore SA1008 // Opening parenthesis must be spaced correctly
}
Add(EasingType.Cubic, EasingMode.EaseOut, new Vector2(0.215f, 0.61f), new Vector2(0.355f, 1f));
Add(EasingType.Cubic, EasingMode.EaseIn, new Vector2(0.55f, 0.055f), new Vector2(0.675f, 0.19f));
Add(EasingType.Cubic, EasingMode.EaseInOut, new Vector2(0.645f, 0.045f), new Vector2(0.355f, 1f));
Add(EasingType.Back, EasingMode.EaseOut, new Vector2(0.175f, 0.885f), new Vector2(0.32f, 1.275f));
Add(EasingType.Back, EasingMode.EaseIn, new Vector2(0.6f, -0.28f), new Vector2(0.735f, 0.045f));
Add(EasingType.Back, EasingMode.EaseInOut, new Vector2(0.68f, -0.55f), new Vector2(0.265f, 1.55f));
Add(EasingType.Bounce, EasingMode.EaseOut, new Vector2(0.58f, 1.93f), new Vector2(.08f, .36f));
Add(EasingType.Bounce, EasingMode.EaseIn, new Vector2(0.93f, 0.7f), new Vector2(0.4f, -0.93f));
Add(EasingType.Bounce, EasingMode.EaseInOut, new Vector2(0.65f, -0.85f), new Vector2(0.35f, 1.85f));
Add(EasingType.Elastic, EasingMode.EaseOut, new Vector2(0.37f, 2.68f), new Vector2(0f, 0.22f));
Add(EasingType.Elastic, EasingMode.EaseIn, new Vector2(1, .78f), new Vector2(.63f, -1.68f));
Add(EasingType.Elastic, EasingMode.EaseInOut, new Vector2(0.9f, -1.2f), new Vector2(0.1f, 2.2f));
Add(EasingType.Circle, EasingMode.EaseOut, new Vector2(0.075f, 0.82f), new Vector2(0.165f, 1f));
Add(EasingType.Circle, EasingMode.EaseIn, new Vector2(0.6f, 0.04f), new Vector2(0.98f, 0.335f));
Add(EasingType.Circle, EasingMode.EaseInOut, new Vector2(0.785f, 0.135f), new Vector2(0.15f, 0.86f));
Add(EasingType.Quadratic, EasingMode.EaseOut, new Vector2(0.25f, 0.46f), new Vector2(0.45f, 0.94f));
Add(EasingType.Quadratic, EasingMode.EaseIn, new Vector2(0.55f, 0.085f), new Vector2(0.68f, 0.53f));
Add(EasingType.Quadratic, EasingMode.EaseInOut, new Vector2(0.445f, 0.03f), new Vector2(0.515f, 0.955f));
Add(EasingType.Quartic, EasingMode.EaseOut, new Vector2(0.165f, 0.84f), new Vector2(0.44f, 1f));
Add(EasingType.Quartic, EasingMode.EaseIn, new Vector2(0.895f, 0.03f), new Vector2(0.685f, 0.22f));
Add(EasingType.Quartic, EasingMode.EaseInOut, new Vector2(0.77f, 0.0f), new Vector2(0.175f, 1.0f));
Add(EasingType.Quintic, EasingMode.EaseOut, new Vector2(0.23f, 1f), new Vector2(0.32f, 1f));
Add(EasingType.Quintic, EasingMode.EaseIn, new Vector2(0.755f, 0.05f), new Vector2(0.855f, 0.06f));
Add(EasingType.Quintic, EasingMode.EaseInOut, new Vector2(0.86f, 0.0f), new Vector2(0.07f, 1.0f));
Add(EasingType.Sine, EasingMode.EaseOut, new Vector2(0.39f, 0.575f), new Vector2(0.565f, 1f));
Add(EasingType.Sine, EasingMode.EaseIn, new Vector2(0.47f, 0.0f), new Vector2(0.745f, 0.715f));
Add(EasingType.Sine, EasingMode.EaseInOut, new Vector2(0.445f, 0.05f), new Vector2(0.55f, 0.95f));
}
private static CompositionEasingFunction GenerateCompositionEasingFunctionFromEasingType(EasingType easingType, Compositor compositor, EasingMode easingMode = EasingMode.EaseOut)
{
if (easingType == EasingType.Default)
{
easingType = DefaultEasingType;
}
if (easingType == EasingType.Linear)
{
return compositor.CreateLinearEasingFunction();
}
// Pay-per-play caching of easing functions
EnsureEasingsCached();
if (_compositionEasingFunctions.TryGetValue((easingType.ToString(), easingMode), out (Vector2, Vector2) points))
{
return compositor.CreateCubicBezierEasingFunction(points.Item1, points.Item2);
}
throw new NotSupportedException($"{easingType.ToString()} EasingType and {easingMode.ToString()} EasingMode combination is not currently supported");
}
private static string GetAnimationPath(CompositeTransform transform, UIElement element, string property)
{
if (element.RenderTransform == transform)
{
return $"(UIElement.RenderTransform).(CompositeTransform.{property})";
}
var group = element.RenderTransform as TransformGroup;
if (group == null)
{
return string.Empty;
}
for (var index = 0; index < group.Children.Count; index++)
{
if (group.Children[index] == transform)
{
return $"(UIElement.RenderTransform).(TransformGroup.Children)[{index}].(CompositeTransform.{property})";
}
}
return string.Empty;
}
private static CompositeTransform GetAttachedCompositeTransform(UIElement element)
{
// We need to use an index to keep track of our CompositeTransform as animation engine
// recreates new transform objects when animating properties
// Already attached?
var compositeTransformIndex = AnimationTools.GetAnimationCompositeTransformIndex(element);
if (compositeTransformIndex > -2)
{
if (compositeTransformIndex == -1 && element.RenderTransform is CompositeTransform)
{
return (CompositeTransform)element.RenderTransform;
}
var group = element.RenderTransform as TransformGroup;
if (group?.Children.Count > compositeTransformIndex && group.Children[compositeTransformIndex] is CompositeTransform)
{
return (CompositeTransform)group.Children[compositeTransformIndex];
}
}
// Let's create a new CompositeTransform
var result = new CompositeTransform();
var currentTransform = element.RenderTransform;
if (currentTransform != null)
{
// We found a RenderTransform
// Is it a TransformGroup?
var currentTransformGroup = currentTransform as TransformGroup;
if (currentTransformGroup != null)
{
currentTransformGroup.Children.Add(result);
AnimationTools.SetAnimationCompositeTransformIndex(element, currentTransformGroup.Children.Count - 1);
}
else
{
// Let's create our own TransformGroup
var group = new TransformGroup();
group.Children.Add(currentTransform);
group.Children.Add(result);
element.RenderTransform = group;
AnimationTools.SetAnimationCompositeTransformIndex(element, 1);
}
}
else
{
element.RenderTransform = result;
AnimationTools.SetAnimationCompositeTransformIndex(element, -1);
}
return result;
}
}
}

Просмотреть файл

@ -1,43 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Windows.UI.Xaml;
namespace Microsoft.Toolkit.Uwp.UI.Animations
{
/// <summary>
/// Internal tool to link composite transforms to elements
/// </summary>
internal class AnimationTools : DependencyObject
{
/// <summary>
/// Attached property used to link composite transform with UIElement
/// </summary>
public static readonly DependencyProperty AnimationCompositeTransformIndexProperty = DependencyProperty.RegisterAttached(
"AnimationCompositeTransformIndex",
typeof(int),
typeof(AnimationTools),
new PropertyMetadata(-2));
/// <summary>
/// Attach a composite transform index to an UIElement.
/// </summary>
/// <param name="element">UIElement to use</param>
/// <param name="value">Composite transform index</param>
public static void SetAnimationCompositeTransformIndex(UIElement element, int value)
{
element.SetValue(AnimationCompositeTransformIndexProperty, value);
}
/// <summary>
/// Get the composite transform index attached to an UIElement.
/// </summary>
/// <param name="element">UIElement to use</param>
/// <returns>Composite transform index.</returns>
public static int GetAnimationCompositeTransformIndex(UIElement element)
{
return (int)element.GetValue(AnimationCompositeTransformIndexProperty);
}
}
}

Просмотреть файл

@ -21,16 +21,12 @@
<ItemGroup>
<Compile Remove="Behaviors\**" />
<Compile Remove="Effects\**" />
<Compile Remove="Extensions\**" />
<EmbeddedResource Remove="Behaviors\**" />
<EmbeddedResource Remove="Effects\**" />
<EmbeddedResource Remove="Extensions\**" />
<None Remove="Behaviors\**" />
<None Remove="Effects\**" />
<None Remove="Extensions\**" />
<Page Remove="Behaviors\**" />
<Page Remove="Effects\**" />
<Page Remove="Extensions\**" />
</ItemGroup>
<ItemGroup>