Animatable.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.Linq;
  4. using Avalonia.Data;
  5. using System;
  6. using System.Reactive.Linq;
  7. using Avalonia.Collections;
  8. using Avalonia.Animation.Transitions;
  9. using System.Collections.Generic;
  10. using System.Threading;
  11. using System.Collections.Concurrent;
  12. namespace Avalonia.Animation
  13. {
  14. /// <summary>
  15. /// Base class for control which can have property transitions.
  16. /// </summary>
  17. public class Animatable : AvaloniaObject
  18. {
  19. /// <summary>
  20. /// Initializes this <see cref="Animatable"/> object.
  21. /// </summary>
  22. public Animatable()
  23. {
  24. Transitions = new Transitions.Transitions();
  25. AnimatableTimer = Timing.AnimationStateTimer
  26. .Select(p =>
  27. {
  28. if (PlayState == PlayState.Pause)
  29. {
  30. return PlayState.Pause;
  31. }
  32. else return p;
  33. })
  34. .Publish()
  35. .RefCount();
  36. }
  37. /// <summary>
  38. /// The specific animations timer for this control.
  39. /// </summary>
  40. /// <returns></returns>
  41. public IObservable<PlayState> AnimatableTimer;
  42. /// <summary>
  43. /// Defines the <see cref="PlayState"/> property.
  44. /// </summary>
  45. public static readonly StyledProperty<PlayState> PlayStateProperty =
  46. AvaloniaProperty.Register<Animatable, PlayState>(nameof(PlayState), PlayState.Run);
  47. /// <summary>
  48. /// Gets or sets the property transitions for the control.
  49. /// </summary>
  50. public PlayState PlayState
  51. {
  52. get { return GetValue(PlayStateProperty); }
  53. set { SetValue(PlayStateProperty, value); }
  54. }
  55. /// <summary>
  56. /// Defines the <see cref="Transitions"/> property.
  57. /// </summary>
  58. public static readonly StyledProperty<IEnumerable<ITransition>> TransitionsProperty =
  59. AvaloniaProperty.Register<Animatable, IEnumerable<ITransition>>(nameof(Transitions));
  60. /// <summary>
  61. /// Gets or sets the property transitions for the control.
  62. /// </summary>
  63. public IEnumerable<ITransition> Transitions
  64. {
  65. get { return GetValue(TransitionsProperty); }
  66. set { SetValue(TransitionsProperty, value); }
  67. }
  68. /// <summary>
  69. /// Reacts to a change in a <see cref="AvaloniaProperty"/> value in
  70. /// order to animate the change if a <see cref="ITransition"/> is set for the property.
  71. /// </summary>
  72. /// <param name="e">The event args.</param>
  73. protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs e)
  74. {
  75. if (e.Priority != BindingPriority.Animation && Transitions != null)
  76. {
  77. var match = Transitions.FirstOrDefault(x => x.Property == e.Property);
  78. if (match != null)
  79. {
  80. match.Apply(this, e.OldValue, e.NewValue);
  81. }
  82. }
  83. }
  84. }
  85. }