Skip to content

Commit

Permalink
Merge pull request #16 from Sergio0694/dev
Browse files Browse the repository at this point in the history
New APIs, bug fixes
  • Loading branch information
Sergio0694 authored Mar 29, 2019
2 parents 908e391 + 5e09071 commit 12250e3
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,37 @@ internal abstract class AnimationBuilderBase<T> : IAnimationBuilder
[MustUseReturnValue, NotNull]
protected abstract IAnimationBuilder OnClip(Side side, double? from, double to, Easing ease);

/// <inheritdoc/>
public IAnimationBuilder Size(Axis axis, double to, Easing ease = Easing.Linear) => OnSize(axis, null, to, ease);

/// <inheritdoc/>
public IAnimationBuilder Size(Axis axis, double from, double to, Easing ease = Easing.Linear) => OnSize(axis, from, to, ease);

/// <summary>
/// Schedules a size animation on a single axis
/// </summary>
/// <param name="axis">The target axis to animate</param>
/// <param name="from">The optional starting value</param>
/// <param name="to">The target value</param>
/// <param name="ease">The easing function to use for the size animation</param>
[MustUseReturnValue, NotNull]
protected abstract IAnimationBuilder OnSize(Axis axis, double? from, double to, Easing ease);

/// <inheritdoc/>
public IAnimationBuilder Size(Vector2 to, Easing ease = Easing.Linear) => OnSize(null, to, ease);

/// <inheritdoc/>
public IAnimationBuilder Size(Vector2 from, Vector2 to, Easing ease = Easing.Linear) => OnSize(from, to, ease);

/// <summary>
/// Schedules a 2D size animation
/// </summary>
/// <param name="from">The optional starting position</param>
/// <param name="to">The target position</param>
/// <param name="ease">The easing function to use for the size animation</param>
[MustUseReturnValue, NotNull]
protected abstract IAnimationBuilder OnSize(Vector2? from, Vector2 to, Easing ease = Easing.Linear);

#endregion

/// <inheritdoc/>
Expand Down
166 changes: 65 additions & 101 deletions UICompositionAnimations/Animations/CompositionAnimationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,92 +29,19 @@ public CompositionAnimationBuilder([NotNull] UIElement target) : base(target)
}

/// <inheritdoc/>
protected override IAnimationBuilder OnOpacity(double? from, double to, Easing ease)
{
AnimationFactories.Add(duration =>
{
TargetVisual.StopAnimation(nameof(Visual.Opacity));
CompositionEasingFunction easingFunction = TargetVisual.GetEasingFunction(ease);
ScalarKeyFrameAnimation animation = TargetVisual.Compositor.CreateScalarKeyFrameAnimation((float?)from, (float)to, duration, null, easingFunction);
TargetVisual.StartAnimation(nameof(Visual.Opacity), animation);
});

return this;
}
protected override IAnimationBuilder OnOpacity(double? from, double to, Easing ease) => OnScalarAnimation(nameof(Visual.Opacity), from, to, ease);

/// <inheritdoc/>
protected override IAnimationBuilder OnTranslation(Axis axis, double? from, double to, Easing ease)
{
AnimationFactories.Add(duration =>
{
// Stop the animation and get the easing function
string property = $"Translation.{axis}";
TargetVisual.StopAnimation(property);
CompositionEasingFunction easingFunction = TargetVisual.GetEasingFunction(ease);
// Create and return the animation
ScalarKeyFrameAnimation animation = TargetVisual.Compositor.CreateScalarKeyFrameAnimation((float?)from, (float)to, duration, null, easingFunction);
TargetVisual.StartAnimation(property, animation);
});

return this;
}
protected override IAnimationBuilder OnTranslation(Axis axis, double? from, double to, Easing ease) => OnScalarAnimation($"Translation.{axis}", from, to, ease);

/// <inheritdoc/>
protected override IAnimationBuilder OnTranslation(Vector2? from, Vector2 to, Easing ease = Easing.Linear)
{
AnimationFactories.Add(duration =>
{
// Stop the animation and get the easing function
TargetVisual.StopAnimation("Translation");
CompositionEasingFunction easingFunction = TargetVisual.GetEasingFunction(ease);
// Create and return the animation
Vector3? from3 = from == null ? default : new Vector3(from.Value, 0);
Vector3 to3 = new Vector3(to, 0);
Vector3KeyFrameAnimation animation = TargetVisual.Compositor.CreateVector3KeyFrameAnimation(from3, to3, duration, null, easingFunction);
TargetVisual.StartAnimation("Translation", animation);
});

return this;
}
protected override IAnimationBuilder OnTranslation(Vector2? from, Vector2 to, Easing ease = Easing.Linear) => OnVector3Animation("Translation", from, to, ease);

/// <inheritdoc/>
protected override IAnimationBuilder OnOffset(Axis axis, double? from, double to, Easing ease)
{
AnimationFactories.Add(duration =>
{
// Stop the animation and get the easing function
string property = $"{nameof(Visual.Offset)}.{axis}";
TargetVisual.StopAnimation(property);
CompositionEasingFunction easingFunction = TargetVisual.GetEasingFunction(ease);
// Create and return the animation
ScalarKeyFrameAnimation animation = TargetVisual.Compositor.CreateScalarKeyFrameAnimation((float?)from, (float)to, duration, null, easingFunction);
TargetVisual.StartAnimation(property, animation);
});

return this;
}
protected override IAnimationBuilder OnOffset(Axis axis, double? from, double to, Easing ease) => OnScalarAnimation($"{nameof(Visual.Offset)}.{axis}", from, to, ease);

/// <inheritdoc/>
protected override IAnimationBuilder OnOffset(Vector2? from, Vector2 to, Easing ease = Easing.Linear)
{
AnimationFactories.Add(duration =>
{
// Stop the animation and get the easing function
TargetVisual.StopAnimation(nameof(Visual.Offset));
CompositionEasingFunction easingFunction = TargetVisual.GetEasingFunction(ease);
// Create and return the animation
Vector3? from3 = from == null ? default : new Vector3(from.Value, 0);
Vector3 to3 = new Vector3(to, 0);
Vector3KeyFrameAnimation animation = TargetVisual.Compositor.CreateVector3KeyFrameAnimation(from3, to3, duration, null, easingFunction);
TargetVisual.StartAnimation(nameof(Visual.Offset), animation);
});

return this;
}
protected override IAnimationBuilder OnOffset(Vector2? from, Vector2 to, Easing ease = Easing.Linear) => OnVector3Animation(nameof(Visual.Offset), from, to, ease);

/// <inheritdoc/>
protected override IAnimationBuilder OnScale(double? from, double to, Easing ease)
Expand All @@ -124,20 +51,9 @@ protected override IAnimationBuilder OnScale(double? from, double to, Easing eas
element.GetVisual().CenterPoint = new Vector3((float)(element.ActualWidth / 2), (float)(element.ActualHeight / 2), 0);

// Add the scale animation
AnimationFactories.Add(duration =>
{
// Stop the animation and get the easing function
TargetVisual.StopAnimation(nameof(Visual.Scale));
CompositionEasingFunction easingFunction = TargetVisual.GetEasingFunction(ease);
// Create and return the animation
Vector3? from3 = from == null ? default : new Vector3((float)from.Value, (float)from.Value, 0);
Vector3 to3 = new Vector3((float)to, (float)to, 0);
Vector3KeyFrameAnimation animation = TargetVisual.Compositor.CreateVector3KeyFrameAnimation(from3, to3, duration, null, easingFunction);
TargetVisual.StartAnimation(nameof(Visual.Scale), animation);
});

return this;
Vector2? from2 = from == null ? default(Vector2?) : new Vector2((float)from.Value, (float)from.Value);
Vector2 to2 = new Vector2((float)to, (float)to);
return OnVector3Animation(nameof(Visual.Scale), from2, to2, ease);
}

/// <inheritdoc/>
Expand All @@ -148,15 +64,7 @@ protected override IAnimationBuilder OnRotate(double? from, double to, Easing ea
element.GetVisual().CenterPoint = new Vector3((float)(element.ActualWidth / 2), (float)(element.ActualHeight / 2), 0);

// Add the rotation animation
AnimationFactories.Add(duration =>
{
TargetVisual.StopAnimation(nameof(Visual.RotationAngleInDegrees));
CompositionEasingFunction easingFunction = TargetVisual.GetEasingFunction(ease);
ScalarKeyFrameAnimation animation = TargetVisual.Compositor.CreateScalarKeyFrameAnimation((float?)from, (float)to, duration, null, easingFunction);
TargetVisual.StartAnimation(nameof(Visual.RotationAngleInDegrees), animation);
});

return this;
return OnScalarAnimation(nameof(Visual.RotationAngleInDegrees), from, to, ease);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -186,6 +94,62 @@ protected override IAnimationBuilder OnClip(Side side, double? from, double to,
return this;
}

/// <inheritdoc/>
protected override IAnimationBuilder OnSize(Axis axis, double? from, double to, Easing ease) => OnScalarAnimation($"{nameof(Visual.Size)}.{axis}", from, to, ease);

/// <inheritdoc/>
protected override IAnimationBuilder OnSize(Vector2? from, Vector2 to, Easing ease = Easing.Linear) => OnVector3Animation(nameof(Visual.Size), from, to, ease);

/// <summary>
/// Schedules a scalar animation targeting the current <see cref="Visual"/> item
/// </summary>
/// <param name="property">The target <see cref="Visual"/> property to animate</param>
/// <param name="from">The optional starting value for the scalar animation</param>
/// <param name="to">The target value for the scalar animation</param>
/// <param name="ease">The easing function to use for the scalar animation</param>
[MustUseReturnValue, NotNull]
private IAnimationBuilder OnScalarAnimation(string property, double? from, double to, Easing ease)
{
AnimationFactories.Add(duration =>
{
// Stop the animation and get the easing function
TargetVisual.StopAnimation(property);
CompositionEasingFunction easingFunction = TargetVisual.GetEasingFunction(ease);
// Create and return the animation
ScalarKeyFrameAnimation animation = TargetVisual.Compositor.CreateScalarKeyFrameAnimation((float?)from, (float)to, duration, null, easingFunction);
TargetVisual.StartAnimation(property, animation);
});

return this;
}

/// <summary>
/// Schedules a vector animation targeting the current <see cref="Visual"/> item
/// </summary>
/// <param name="property">The target <see cref="Visual"/> property to animate</param>
/// <param name="from">The optional starting value for the vector animation</param>
/// <param name="to">The target value for the vector animation</param>
/// <param name="ease">The easing function to use for the vector animation</param>
[MustUseReturnValue, NotNull]
private IAnimationBuilder OnVector3Animation(string property, Vector2? from, Vector2 to, Easing ease)
{
AnimationFactories.Add(duration =>
{
// Stop the animation and get the easing function
TargetVisual.StopAnimation(property);
CompositionEasingFunction easingFunction = TargetVisual.GetEasingFunction(ease);
// Create and return the animation
Vector3? from3 = from == null ? default(Vector3?) : new Vector3(from.Value, 0);
Vector3 to3 = new Vector3(to, 0);
Vector3KeyFrameAnimation animation = TargetVisual.Compositor.CreateVector3KeyFrameAnimation(from3, to3, duration, null, easingFunction);
TargetVisual.StartAnimation(property, animation);
});

return this;
}

/// <summary>
/// Creates and starts the pending animations
/// </summary>
Expand Down
36 changes: 36 additions & 0 deletions UICompositionAnimations/Animations/Interfaces/IAnimationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,42 @@ public interface IAnimationBuilder
[MustUseReturnValue, NotNull]
IAnimationBuilder Clip(Side side, double from, double to, Easing ease = Easing.Linear);

/// <summary>
/// Schedules a size animation
/// </summary>
/// <param name="axis">The size axis to animate</param>
/// <param name="to">The target size value to animate to</param>
/// <param name="ease">The easing function to use for the size animation</param>
[MustUseReturnValue, NotNull]
IAnimationBuilder Size(Axis axis, double to, Easing ease = Easing.Linear);

/// <summary>
/// Schedules a size animation
/// </summary>
/// <param name="axis">The size axis to animate</param>
/// <param name="from">The initial size value to animate from</param>
/// <param name="to">The target size value to animate to</param>
/// <param name="ease">The easing function to use for the size animation</param>
[MustUseReturnValue, NotNull]
IAnimationBuilder Size(Axis axis, double from, double to, Easing ease = Easing.Linear);

/// <summary>
/// Schedules a size animation
/// </summary>
/// <param name="to">The target size position to animate to</param>
/// <param name="ease">The easing function to use for the size animation</param>
[MustUseReturnValue, NotNull]
IAnimationBuilder Size(Vector2 to, Easing ease = Easing.Linear);

/// <summary>
/// Schedules a size animation
/// </summary>
/// <param name="from">The initial size position to animate from</param>
/// <param name="to">The target size position to animate to</param>
/// <param name="ease">The easing function to use for the size animation</param>
[MustUseReturnValue, NotNull]
IAnimationBuilder Size(Vector2 from, Vector2 to, Easing ease = Easing.Linear);

/// <summary>
/// Sets the duration of the animation
/// </summary>
Expand Down
32 changes: 31 additions & 1 deletion UICompositionAnimations/Animations/XamlAnimationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,37 @@ protected override IAnimationBuilder OnRotate(double? from, double to, Easing ea
}

/// <inheritdoc/>
protected override IAnimationBuilder OnClip(Side side, double? @from, double to, Easing ease) => throw new NotSupportedException("Can't animate the clip property from XAML");
protected override IAnimationBuilder OnClip(Side side, double? from, double to, Easing ease) => throw new NotSupportedException("Can't animate the clip property from XAML");

/// <inheritdoc/>
protected override IAnimationBuilder OnSize(Axis axis, double? from, double to, Easing ease)
{
if (!(TargetElement is FrameworkElement element)) throw new InvalidOperationException("Can't animate the size of an item that's not a framework element");
AnimationFactories.Add(duration =>
{
switch (axis)
{
case Axis.X: return TargetElement.CreateDoubleAnimation(nameof(FrameworkElement.Width), double.IsNaN(element.Width) ? element.ActualWidth : from, to, duration, ease, true);
case Axis.Y: return TargetElement.CreateDoubleAnimation(nameof(FrameworkElement.Height), double.IsNaN(element.Height) ? element.ActualHeight : from, to, duration, ease, true);
default: throw new ArgumentException($"Invalid axis: {axis}", nameof(axis));
}
});

return this;
}

/// <inheritdoc/>
protected override IAnimationBuilder OnSize(Vector2? from, Vector2 to, Easing ease = Easing.Linear)
{
if (!(TargetElement is FrameworkElement element)) throw new InvalidOperationException("Can't animate the size of an item that's not a framework element");
double?
fromX = double.IsNaN(element.Width) ? (double?)element.ActualWidth : from?.X,
fromY = double.IsNaN(element.Height) ? (double?)element.ActualHeight : from?.Y;
AnimationFactories.Add(duration => TargetTransform.CreateDoubleAnimation(nameof(FrameworkElement.Width), fromX, to.X, duration, ease, true));
AnimationFactories.Add(duration => TargetTransform.CreateDoubleAnimation(nameof(FrameworkElement.Height), fromY, to.Y, duration, ease, true));

return this;
}

/// <summary>
/// Gets the <see cref="Windows.UI.Xaml.Media.Animation.Storyboard"/> represented by the current instance
Expand Down
6 changes: 3 additions & 3 deletions UICompositionAnimations/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("4.0.1.0")]
[assembly: AssemblyFileVersion("4.0.1.0")]
[assembly: AssemblyInformationalVersion("4.0.1")]
[assembly: AssemblyVersion("4.0.2.0")]
[assembly: AssemblyFileVersion("4.0.2.0")]
[assembly: AssemblyInformationalVersion("4.0.2")]
[assembly: ComVisible(false)]

4 changes: 2 additions & 2 deletions UICompositionAnimations/UICompositionAnimations.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<package>
<metadata>
<id>UICompositionAnimations</id>
<version>4.0.1</version>
<version>4.0.2</version>
<title>UICompositionAnimations</title>
<description>A wrapper UWP PCL to work with Windows.UI.Composition and XAML animations, and Win2D effects</description>
<authors>Sergio Pedri</authors>
<owners>Sergio Pedri</owners>
<projectUrl>https://github.com/Sergio0694/UICompositionAnimations</projectUrl>
<license type="expression">GPL-3.0-only</license>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<releaseNotes>New overloads added, minor bug fixes</releaseNotes>
<releaseNotes>New size animation APIs, minor fixes</releaseNotes>
<copyright>Copyright © 2019</copyright>
<tags>uwp composition animations xaml csharp windows winrt universal app ui win2d graphics</tags>
<dependencies>
Expand Down

0 comments on commit 12250e3

Please sign in to comment.