using System;
using Avalonia.Animation;
using Avalonia.Animation.Easings;
namespace Avalonia.Rendering.Composition.Animations
{
///
/// A time-based animation with one or more key frames.
/// These frames are markers, allowing developers to specify values at specific times for the animating property.
/// KeyFrame animations can be further customized by specifying how the animation interpolates between keyframes.
///
public abstract class KeyFrameAnimation : CompositionAnimation
{
private TimeSpan _duration = TimeSpan.FromMilliseconds(1);
internal KeyFrameAnimation(Compositor compositor) : base(compositor)
{
}
///
/// The delay behavior of the key frame animation.
///
public AnimationDelayBehavior DelayBehavior { get; set; }
///
/// Delay before the animation starts after is called.
///
public System.TimeSpan DelayTime { get; set; }
///
/// The direction the animation is playing.
/// The Direction property allows you to drive your animation from start to end or end to start or alternate
/// between start and end or end to start if animation has an greater than one.
/// This gives an easy way for customizing animation definitions.
///
public PlaybackDirection Direction { get; set; }
///
/// The duration of the animation.
/// Minimum allowed value is 1ms and maximum allowed value is 24 days.
///
public TimeSpan Duration
{
get => _duration;
set
{
if (_duration < TimeSpan.FromMilliseconds(1) || _duration > TimeSpan.FromDays(1))
throw new ArgumentException("Minimum allowed value is 1ms and maximum allowed value is 24 days.");
_duration = value;
}
}
///
/// The iteration behavior for the key frame animation.
///
public AnimationIterationBehavior IterationBehavior { get; set; }
///
/// The number of times to repeat the key frame animation.
///
public int IterationCount { get; set; } = 1;
///
/// Specifies how to set the property value when animation is stopped
///
public AnimationStopBehavior StopBehavior { get; set; }
private protected abstract IKeyFrames KeyFrames { get; }
///
/// Inserts an expression keyframe.
///
///
/// The time the key frame should occur at, expressed as a percentage of the animation Duration. Allowed value is from 0.0 to 1.0.
///
/// The expression used to calculate the value of the key frame.
/// The easing function to use when interpolating between frames.
public void InsertExpressionKeyFrame(float normalizedProgressKey, string value,
Easing? easingFunction = null) =>
KeyFrames.InsertExpressionKeyFrame(normalizedProgressKey, value, easingFunction ?? Compositor.DefaultEasing);
}
///
/// Specifies the animation delay behavior.
///
public enum AnimationDelayBehavior
{
///
/// If a DelayTime is specified, it delays starting the animation according to delay time and after delay
/// has expired it applies animation to the object property.
///
SetInitialValueAfterDelay,
///
/// Applies the initial value of the animation (i.e. the value at Keyframe 0) to the object before the delay time
/// is elapsed (when there is a DelayTime specified), it then delays starting the animation according to the DelayTime.
///
SetInitialValueBeforeDelay
}
///
/// Specifies if the animation should loop.
///
public enum AnimationIterationBehavior
{
///
/// The animation should loop the specified number of times.
///
Count,
///
/// The animation should loop forever.
///
Forever
}
///
/// Specifies the behavior of an animation when it stops.
///
public enum AnimationStopBehavior
{
///
/// Leave the animation at its current value.
///
LeaveCurrentValue,
///
/// Reset the animation to its initial value.
///
SetToInitialValue,
///
/// Set the animation to its final value.
///
SetToFinalValue
}
}