@@ -8,108 +8,109 @@ using Windows.UI.Composition;
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Media.Animation;
|
||||
|
||||
namespace Wino.Extensions;
|
||||
|
||||
public static class AnimationExtensions
|
||||
namespace Wino.Extensions
|
||||
{
|
||||
#region Composition
|
||||
|
||||
public static ScalarKeyFrameAnimation CreateScalarKeyFrameAnimation(this Compositor compositor, float? from, float to,
|
||||
double duration, double delay, CompositionEasingFunction easing, AnimationIterationBehavior iterationBehavior)
|
||||
public static class AnimationExtensions
|
||||
{
|
||||
var animation = compositor.CreateScalarKeyFrameAnimation();
|
||||
#region Composition
|
||||
|
||||
animation.Duration = TimeSpan.FromMilliseconds(duration);
|
||||
if (!delay.Equals(0)) animation.DelayTime = TimeSpan.FromMilliseconds(delay);
|
||||
if (from.HasValue) animation.InsertKeyFrame(0.0f, from.Value, easing);
|
||||
animation.InsertKeyFrame(1.0f, to, easing);
|
||||
animation.IterationBehavior = iterationBehavior;
|
||||
|
||||
return animation;
|
||||
}
|
||||
|
||||
public static Vector2KeyFrameAnimation CreateVector2KeyFrameAnimation(this Compositor compositor, Vector2? from, Vector2 to,
|
||||
double duration, double delay, CompositionEasingFunction easing, AnimationIterationBehavior iterationBehavior)
|
||||
{
|
||||
var animation = compositor.CreateVector2KeyFrameAnimation();
|
||||
|
||||
animation.Duration = TimeSpan.FromMilliseconds(duration);
|
||||
animation.DelayTime = TimeSpan.FromMilliseconds(delay);
|
||||
if (from.HasValue) animation.InsertKeyFrame(0.0f, from.Value, easing);
|
||||
animation.InsertKeyFrame(1.0f, to, easing);
|
||||
animation.IterationBehavior = iterationBehavior;
|
||||
|
||||
return animation;
|
||||
}
|
||||
|
||||
public static Vector3KeyFrameAnimation CreateVector3KeyFrameAnimation(this Compositor compositor, Vector2? from, Vector2 to,
|
||||
double duration, double delay, CompositionEasingFunction easing, AnimationIterationBehavior iterationBehavior)
|
||||
{
|
||||
var animation = compositor.CreateVector3KeyFrameAnimation();
|
||||
|
||||
animation.Duration = TimeSpan.FromMilliseconds(duration);
|
||||
animation.DelayTime = TimeSpan.FromMilliseconds(delay);
|
||||
if (from.HasValue) animation.InsertKeyFrame(0.0f, new Vector3(from.Value, 1.0f), easing);
|
||||
animation.InsertKeyFrame(1.0f, new Vector3(to, 1.0f), easing);
|
||||
animation.IterationBehavior = iterationBehavior;
|
||||
|
||||
return animation;
|
||||
}
|
||||
|
||||
public static Vector3KeyFrameAnimation CreateVector3KeyFrameAnimation(this Compositor compositor, Vector3? from, Vector3 to,
|
||||
double duration, double delay, CompositionEasingFunction easing, AnimationIterationBehavior iterationBehavior)
|
||||
{
|
||||
var animation = compositor.CreateVector3KeyFrameAnimation();
|
||||
|
||||
animation.Duration = TimeSpan.FromMilliseconds(duration);
|
||||
animation.DelayTime = TimeSpan.FromMilliseconds(delay);
|
||||
if (from.HasValue) animation.InsertKeyFrame(0.0f, from.Value, easing);
|
||||
animation.InsertKeyFrame(1.0f, to, easing);
|
||||
animation.IterationBehavior = iterationBehavior;
|
||||
|
||||
return animation;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Xaml Storyboard
|
||||
|
||||
public static void Animate(this DependencyObject target, double? from, double to,
|
||||
string propertyPath, int duration = 400, int startTime = 0,
|
||||
EasingFunctionBase easing = null, Action completed = null, bool enableDependentAnimation = false)
|
||||
{
|
||||
if (easing == null)
|
||||
public static ScalarKeyFrameAnimation CreateScalarKeyFrameAnimation(this Compositor compositor, float? from, float to,
|
||||
double duration, double delay, CompositionEasingFunction easing, AnimationIterationBehavior iterationBehavior)
|
||||
{
|
||||
easing = new ExponentialEase();
|
||||
var animation = compositor.CreateScalarKeyFrameAnimation();
|
||||
|
||||
animation.Duration = TimeSpan.FromMilliseconds(duration);
|
||||
if (!delay.Equals(0)) animation.DelayTime = TimeSpan.FromMilliseconds(delay);
|
||||
if (from.HasValue) animation.InsertKeyFrame(0.0f, from.Value, easing);
|
||||
animation.InsertKeyFrame(1.0f, to, easing);
|
||||
animation.IterationBehavior = iterationBehavior;
|
||||
|
||||
return animation;
|
||||
}
|
||||
|
||||
var db = new DoubleAnimation
|
||||
public static Vector2KeyFrameAnimation CreateVector2KeyFrameAnimation(this Compositor compositor, Vector2? from, Vector2 to,
|
||||
double duration, double delay, CompositionEasingFunction easing, AnimationIterationBehavior iterationBehavior)
|
||||
{
|
||||
EnableDependentAnimation = enableDependentAnimation,
|
||||
To = to,
|
||||
From = from,
|
||||
EasingFunction = easing,
|
||||
Duration = TimeSpan.FromMilliseconds(duration)
|
||||
};
|
||||
Storyboard.SetTarget(db, target);
|
||||
Storyboard.SetTargetProperty(db, propertyPath);
|
||||
var animation = compositor.CreateVector2KeyFrameAnimation();
|
||||
|
||||
var sb = new Storyboard
|
||||
{
|
||||
BeginTime = TimeSpan.FromMilliseconds(startTime)
|
||||
};
|
||||
animation.Duration = TimeSpan.FromMilliseconds(duration);
|
||||
animation.DelayTime = TimeSpan.FromMilliseconds(delay);
|
||||
if (from.HasValue) animation.InsertKeyFrame(0.0f, from.Value, easing);
|
||||
animation.InsertKeyFrame(1.0f, to, easing);
|
||||
animation.IterationBehavior = iterationBehavior;
|
||||
|
||||
if (completed != null)
|
||||
return animation;
|
||||
}
|
||||
|
||||
public static Vector3KeyFrameAnimation CreateVector3KeyFrameAnimation(this Compositor compositor, Vector2? from, Vector2 to,
|
||||
double duration, double delay, CompositionEasingFunction easing, AnimationIterationBehavior iterationBehavior)
|
||||
{
|
||||
sb.Completed += (s, e) =>
|
||||
var animation = compositor.CreateVector3KeyFrameAnimation();
|
||||
|
||||
animation.Duration = TimeSpan.FromMilliseconds(duration);
|
||||
animation.DelayTime = TimeSpan.FromMilliseconds(delay);
|
||||
if (from.HasValue) animation.InsertKeyFrame(0.0f, new Vector3(from.Value, 1.0f), easing);
|
||||
animation.InsertKeyFrame(1.0f, new Vector3(to, 1.0f), easing);
|
||||
animation.IterationBehavior = iterationBehavior;
|
||||
|
||||
return animation;
|
||||
}
|
||||
|
||||
public static Vector3KeyFrameAnimation CreateVector3KeyFrameAnimation(this Compositor compositor, Vector3? from, Vector3 to,
|
||||
double duration, double delay, CompositionEasingFunction easing, AnimationIterationBehavior iterationBehavior)
|
||||
{
|
||||
var animation = compositor.CreateVector3KeyFrameAnimation();
|
||||
|
||||
animation.Duration = TimeSpan.FromMilliseconds(duration);
|
||||
animation.DelayTime = TimeSpan.FromMilliseconds(delay);
|
||||
if (from.HasValue) animation.InsertKeyFrame(0.0f, from.Value, easing);
|
||||
animation.InsertKeyFrame(1.0f, to, easing);
|
||||
animation.IterationBehavior = iterationBehavior;
|
||||
|
||||
return animation;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Xaml Storyboard
|
||||
|
||||
public static void Animate(this DependencyObject target, double? from, double to,
|
||||
string propertyPath, int duration = 400, int startTime = 0,
|
||||
EasingFunctionBase easing = null, Action completed = null, bool enableDependentAnimation = false)
|
||||
{
|
||||
if (easing == null)
|
||||
{
|
||||
completed();
|
||||
easing = new ExponentialEase();
|
||||
}
|
||||
|
||||
var db = new DoubleAnimation
|
||||
{
|
||||
EnableDependentAnimation = enableDependentAnimation,
|
||||
To = to,
|
||||
From = from,
|
||||
EasingFunction = easing,
|
||||
Duration = TimeSpan.FromMilliseconds(duration)
|
||||
};
|
||||
Storyboard.SetTarget(db, target);
|
||||
Storyboard.SetTargetProperty(db, propertyPath);
|
||||
|
||||
var sb = new Storyboard
|
||||
{
|
||||
BeginTime = TimeSpan.FromMilliseconds(startTime)
|
||||
};
|
||||
|
||||
if (completed != null)
|
||||
{
|
||||
sb.Completed += (s, e) =>
|
||||
{
|
||||
completed();
|
||||
};
|
||||
}
|
||||
|
||||
sb.Children.Add(db);
|
||||
sb.Begin();
|
||||
}
|
||||
|
||||
sb.Children.Add(db);
|
||||
sb.Begin();
|
||||
#endregion
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user