Animation.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reactive.Disposables;
  7. using System.Reactive.Linq;
  8. using System.Threading.Tasks;
  9. using Avalonia.Animation.Easings;
  10. using Avalonia.Collections;
  11. namespace Avalonia.Animation
  12. {
  13. /// <summary>
  14. /// Tracks the progress of an animation.
  15. /// </summary>
  16. public class Animation : AvaloniaList<KeyFrame>, IAnimation
  17. {
  18. /// <summary>
  19. /// Gets or sets the active time of this animation.
  20. /// </summary>
  21. public TimeSpan Duration { get; set; }
  22. /// <summary>
  23. /// Gets or sets the repeat count for this animation.
  24. /// </summary>
  25. public RepeatCount RepeatCount { get; set; }
  26. /// <summary>
  27. /// Gets or sets the playback direction for this animation.
  28. /// </summary>
  29. public PlaybackDirection PlaybackDirection { get; set; }
  30. /// <summary>
  31. /// Gets or sets the value fill mode for this animation.
  32. /// </summary>
  33. public FillMode FillMode { get; set; }
  34. /// <summary>
  35. /// Gets or sets the easing function to be used for this animation.
  36. /// </summary>
  37. public Easing Easing { get; set; } = new LinearEasing();
  38. /// <summary>
  39. /// Gets or sets the speed multiple for this animation.
  40. /// </summary>
  41. public double SpeedRatio { get; set; } = 1d;
  42. /// <summary>
  43. /// Gets or sets the delay time for this animation.
  44. /// </summary>
  45. /// <remarks>
  46. /// Describes a delay to be added before the animation starts, and optionally between
  47. /// repeats of the animation if <see cref="DelayBetweenIterations"/> is set.
  48. /// </remarks>
  49. public TimeSpan Delay { get; set; }
  50. /// <summary>
  51. /// Gets or sets a value indicating whether <see cref="Delay"/> will be applied between
  52. /// iterations of the animation.
  53. /// </summary>
  54. /// <remarks>
  55. /// If this property is not set, then <see cref="Delay"/> will only be applied to the first
  56. /// iteration of the animation.
  57. /// </remarks>
  58. public bool DelayBetweenIterations { get; set; }
  59. private readonly static List<(Func<AvaloniaProperty, bool> Condition, Type Animator)> Animators = new List<(Func<AvaloniaProperty, bool>, Type)>
  60. {
  61. ( prop => typeof(double).IsAssignableFrom(prop.PropertyType), typeof(DoubleAnimator) )
  62. };
  63. public static void RegisterAnimator<TAnimator>(Func<AvaloniaProperty, bool> condition)
  64. where TAnimator : IAnimator
  65. {
  66. Animators.Insert(0, (condition, typeof(TAnimator)));
  67. }
  68. private static Type GetAnimatorType(AvaloniaProperty property)
  69. {
  70. foreach (var (condition, type) in Animators)
  71. {
  72. if (condition(property))
  73. {
  74. return type;
  75. }
  76. }
  77. return null;
  78. }
  79. private (IList<IAnimator> Animators, IList<IDisposable> subscriptions) InterpretKeyframes(Animatable control)
  80. {
  81. var handlerList = new List<(Type type, AvaloniaProperty property)>();
  82. var animatorKeyFrames = new List<AnimatorKeyFrame>();
  83. var subscriptions = new List<IDisposable>();
  84. foreach (var keyframe in this)
  85. {
  86. foreach (var setter in keyframe)
  87. {
  88. var handler = GetAnimatorType(setter.Property);
  89. if (handler == null)
  90. {
  91. throw new InvalidOperationException($"No animator registered for the property {setter.Property}. Add an animator to the Animation.Animators collection that matches this property to animate it.");
  92. }
  93. if (!handlerList.Contains((handler, setter.Property)))
  94. handlerList.Add((handler, setter.Property));
  95. var cue = keyframe.Cue;
  96. if (keyframe.TimingMode == KeyFrameTimingMode.TimeSpan)
  97. {
  98. cue = new Cue(keyframe.KeyTime.Ticks / Duration.Ticks);
  99. }
  100. var newKF = new AnimatorKeyFrame(handler, cue);
  101. subscriptions.Add(newKF.BindSetter(setter, control));
  102. animatorKeyFrames.Add(newKF);
  103. }
  104. }
  105. var newAnimatorInstances = new List<IAnimator>();
  106. foreach (var (handlerType, property) in handlerList)
  107. {
  108. var newInstance = (IAnimator)Activator.CreateInstance(handlerType);
  109. newInstance.Property = property;
  110. newAnimatorInstances.Add(newInstance);
  111. }
  112. foreach (var keyframe in animatorKeyFrames)
  113. {
  114. var animator = newAnimatorInstances.First(a => a.GetType() == keyframe.AnimatorType &&
  115. a.Property == keyframe.Property);
  116. animator.Add(keyframe);
  117. }
  118. return (newAnimatorInstances, subscriptions);
  119. }
  120. /// <inheritdocs/>
  121. public IDisposable Apply(Animatable control, IClock clock, IObservable<bool> match, Action onComplete)
  122. {
  123. var (animators, subscriptions) = InterpretKeyframes(control);
  124. if (animators.Count == 1)
  125. {
  126. subscriptions.Add(animators[0].Apply(this, control, clock, match, onComplete));
  127. }
  128. else
  129. {
  130. var completionTasks = onComplete != null ? new List<Task>() : null;
  131. foreach (IAnimator animator in animators)
  132. {
  133. Action animatorOnComplete = null;
  134. if (onComplete != null)
  135. {
  136. var tcs = new TaskCompletionSource<object>();
  137. animatorOnComplete = () => tcs.SetResult(null);
  138. completionTasks.Add(tcs.Task);
  139. }
  140. subscriptions.Add(animator.Apply(this, control, clock, match, animatorOnComplete));
  141. }
  142. if (onComplete != null)
  143. {
  144. Task.WhenAll(completionTasks).ContinueWith(_ => onComplete());
  145. }
  146. }
  147. return new CompositeDisposable(subscriptions);
  148. }
  149. /// <inheritdocs/>
  150. public Task RunAsync(Animatable control, IClock clock = null)
  151. {
  152. var run = new TaskCompletionSource<object>();
  153. if (this.RepeatCount == RepeatCount.Loop)
  154. run.SetException(new InvalidOperationException("Looping animations must not use the Run method."));
  155. IDisposable subscriptions = null;
  156. subscriptions = this.Apply(control, clock, Observable.Return(true), () =>
  157. {
  158. run.SetResult(null);
  159. subscriptions?.Dispose();
  160. });
  161. return run.Task;
  162. }
  163. }
  164. }