KeyFrames.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Animation.Easings;
  4. using Avalonia.Rendering.Composition.Expressions;
  5. // Special license applies <see href="https://raw.githubusercontent.com/AvaloniaUI/Avalonia/master/src/Avalonia.Base/Rendering/Composition/License.md">License.md</see>
  6. namespace Avalonia.Rendering.Composition.Animations
  7. {
  8. /// <summary>
  9. /// Collection of composition animation key frames
  10. /// </summary>
  11. /// <typeparam name="T"></typeparam>
  12. class KeyFrames<T> : List<KeyFrame<T>>, IKeyFrames
  13. {
  14. void Validate(float key)
  15. {
  16. if (key < 0 || key > 1)
  17. throw new ArgumentException("Key frame key");
  18. if (Count > 0 && this[Count - 1].NormalizedProgressKey > key)
  19. throw new ArgumentException("Key frame key " + key + " is less than the previous one");
  20. }
  21. public void InsertExpressionKeyFrame(float normalizedProgressKey, string value, IEasing easingFunction)
  22. {
  23. Validate(normalizedProgressKey);
  24. Add(new KeyFrame<T>
  25. {
  26. NormalizedProgressKey = normalizedProgressKey,
  27. Expression = Expression.Parse(value),
  28. EasingFunction = easingFunction
  29. });
  30. }
  31. public void Insert(float normalizedProgressKey, T value, IEasing easingFunction)
  32. {
  33. Validate(normalizedProgressKey);
  34. Add(new KeyFrame<T>
  35. {
  36. NormalizedProgressKey = normalizedProgressKey,
  37. Value = value,
  38. EasingFunction = easingFunction
  39. });
  40. }
  41. public ServerKeyFrame<T>[] Snapshot()
  42. {
  43. var frames = new ServerKeyFrame<T>[Count];
  44. for (var c = 0; c < Count; c++)
  45. {
  46. var f = this[c];
  47. frames[c] = new ServerKeyFrame<T>
  48. {
  49. Expression = f.Expression,
  50. Value = f.Value,
  51. EasingFunction = f.EasingFunction,
  52. Key = f.NormalizedProgressKey
  53. };
  54. }
  55. return frames;
  56. }
  57. }
  58. /// <summary>
  59. /// Composition animation key frame
  60. /// </summary>
  61. struct KeyFrame<T>
  62. {
  63. public float NormalizedProgressKey;
  64. public T Value;
  65. public Expression Expression;
  66. public IEasing EasingFunction;
  67. }
  68. /// <summary>
  69. /// Server-side composition animation key frame
  70. /// </summary>
  71. struct ServerKeyFrame<T>
  72. {
  73. public T Value;
  74. public Expression? Expression;
  75. public IEasing EasingFunction;
  76. public float Key;
  77. }
  78. interface IKeyFrames
  79. {
  80. public void InsertExpressionKeyFrame(float normalizedProgressKey, string value, IEasing easingFunction);
  81. }
  82. }