Browse Source

Allow users to supply custom clocks in XAML or code before animations are applied. Change AnimationsPage to show an example with a custom clock and the animations on that page.

Jeremy Koritzinsky 7 years ago
parent
commit
51faa94534

+ 5 - 2
samples/RenderDemo/Pages/AnimationsPage.xaml

@@ -107,9 +107,12 @@
   </UserControl.Styles>
   <Grid>
     <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" ClipToBounds="False">
+      <StackPanel.Clock>
+          <Clock />
+      </StackPanel.Clock>
       <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
         <TextBlock VerticalAlignment="Center">Hover to activate Transform Keyframe Animations.</TextBlock>
-        <Button Content="{Binding PlayStateText}" Command="{Binding ToggleGlobalPlayState}"/>
+        <Button Content="{Binding PlayStateText}" Command="{Binding TogglePlayState}" Click="ToggleClock" />
       </StackPanel>
       <WrapPanel ClipToBounds="False">
         <Border Classes="Test Rect1" Background="DarkRed"/>
@@ -120,4 +123,4 @@
       </WrapPanel>
     </StackPanel>
   </Grid>
-</UserControl>
+</UserControl>

+ 16 - 0
samples/RenderDemo/Pages/AnimationsPage.xaml.cs

@@ -5,6 +5,7 @@ using Avalonia.Controls;
 using Avalonia.Controls.Shapes;
 using Avalonia.Data;
 using Avalonia.Input;
+using Avalonia.Interactivity;
 using Avalonia.Markup.Xaml;
 using Avalonia.Media;
 using RenderDemo.ViewModels;
@@ -23,5 +24,20 @@ namespace RenderDemo.Pages
         {
             AvaloniaXamlLoader.Load(this);
         }
+
+        private void ToggleClock(object sender, RoutedEventArgs args)
+        {
+            var button = sender as Button;
+            var clock = button.Clock;
+
+            if (clock.PlayState == PlayState.Run)
+            {
+                clock.PlayState = PlayState.Pause;
+            }
+            else if (clock.PlayState == PlayState.Pause)
+            {
+                clock.PlayState = PlayState.Run;
+            }
+        }
     }
 }

+ 6 - 20
samples/RenderDemo/ViewModels/AnimationsPageViewModel.cs

@@ -6,27 +6,15 @@ namespace RenderDemo.ViewModels
 {
     public class AnimationsPageViewModel : ReactiveObject
     {
-        private string _playStateText = "Pause all animations";
+        private bool _isPlaying = true;
 
-        public AnimationsPageViewModel()
-        {
-            ToggleGlobalPlayState = ReactiveCommand.Create(() => TogglePlayState());
-        }
+        private string _playStateText = "Pause all animations";
 
-        void TogglePlayState()
+        public void TogglePlayState()
         {
-            switch (Animation.GlobalPlayState)
-            {
-                case PlayState.Run:
-                    PlayStateText = "Resume all animations";
-                    Animation.GlobalPlayState = PlayState.Pause;
-                    break;
-
-                case PlayState.Pause:
-                    PlayStateText = "Pause all animations";
-                    Animation.GlobalPlayState = PlayState.Run;
-                    break;
-            }
+            PlayStateText = _isPlaying
+                ? "Resume animations on this page" : "Pause animations on this page";
+            _isPlaying = !_isPlaying;
         }
 
         public string PlayStateText
@@ -34,7 +22,5 @@ namespace RenderDemo.ViewModels
             get { return _playStateText; }
             set { this.RaiseAndSetIfChanged(ref _playStateText, value); }
         }
-
-        public ReactiveCommand ToggleGlobalPlayState { get; }
     }
 }

+ 0 - 10
src/Avalonia.Animation/Animation.cs

@@ -17,16 +17,6 @@ namespace Avalonia.Animation
     /// </summary>
     public class Animation : AvaloniaList<KeyFrame>, IAnimation
     {
-
-        /// <summary>
-        /// Gets or sets the animation play state for all animations
-        /// </summary> 
-        public static PlayState GlobalPlayState
-        {
-            get => AvaloniaLocator.Current.GetService<Clock>().PlayState;
-            set => AvaloniaLocator.Current.GetService<Clock>().PlayState = value;
-        }
-
         /// <summary>
         /// Gets or sets the active time of this animation.
         /// </summary>

+ 7 - 1
src/Avalonia.Animation/Animator`1.cs

@@ -103,7 +103,13 @@ namespace Avalonia.Animation
         /// </summary>
         private IDisposable RunKeyFrames(Animation animation, Animatable control, IClock clock, Action onComplete)
         {
-            var instance = new AnimationInstance<T>(animation, control, this, clock, onComplete, DoInterpolation);
+            var instance = new AnimationInstance<T>(
+                animation,
+                control,
+                this,
+                clock ?? control.Clock ?? Clock.GlobalClock,
+                onComplete,
+                DoInterpolation);
             return control.Bind<T>((AvaloniaProperty<T>)Property, instance, BindingPriority.Animation);
         }
 

+ 1 - 1
src/Avalonia.Styling/Styling/Style.cs

@@ -122,7 +122,7 @@ namespace Avalonia.Styling
                                 obsMatch = Observable.Return(true);
                             }
 
-                            var sub = animation.Apply(animatable, animatable.Clock ?? Clock.GlobalClock, obsMatch);
+                            var sub = animation.Apply(animatable, null, obsMatch);
                             subs.Add(sub);
                         } 
                     }

+ 2 - 2
src/Avalonia.Visuals/Animation/TransformAnimator.cs

@@ -44,7 +44,7 @@ namespace Avalonia.Animation
                 // It's a transform object so let's target that.
                 if (renderTransformType == Property.OwnerType)
                 {
-                    return childKeyFrames.Apply(animation, ctrl.RenderTransform, clock, obsMatch, onComplete);
+                    return childKeyFrames.Apply(animation, ctrl.RenderTransform, clock ?? control.Clock, obsMatch, onComplete);
                 }
                 // It's a TransformGroup and try finding the target there.
                 else if (renderTransformType == typeof(TransformGroup))
@@ -53,7 +53,7 @@ namespace Avalonia.Animation
                     {
                         if (transform.GetType() == Property.OwnerType)
                         {
-                            return childKeyFrames.Apply(animation, transform, clock, obsMatch, onComplete);
+                            return childKeyFrames.Apply(animation, transform, clock ?? control.Clock, obsMatch, onComplete);
                         }
                     }
                 }