Browse Source

Clean up cue handling.

pull/1768/head
Jeremy Koritzinsky 8 years ago
parent
commit
1a84fe9186
  1. 31
      src/Avalonia.Animation/Animation.cs
  2. 4
      src/Avalonia.Animation/AnimatorKeyFrame.cs
  3. 23
      src/Avalonia.Animation/Animator`1.cs

31
src/Avalonia.Animation/Animation.cs

@ -83,7 +83,7 @@ namespace Avalonia.Animation
private void InterpretKeyframes() private void InterpretKeyframes()
{ {
var handlerList = new List<(Type type, AvaloniaProperty property)>(); var handlerList = new List<(Type type, AvaloniaProperty property)>();
var kfList = new List<AnimatorKeyFrame>(); var animatorKeyFrames = new List<AnimatorKeyFrame>();
foreach (var keyframe in this) foreach (var keyframe in this)
{ {
@ -99,38 +99,43 @@ namespace Avalonia.Animation
if (!handlerList.Contains((handler, setter.Property))) if (!handlerList.Contains((handler, setter.Property)))
handlerList.Add((handler, setter.Property)); handlerList.Add((handler, setter.Property));
var cue = keyframe.Cue;
if (keyframe.TimingMode == KeyFrameTimingMode.TimeSpan)
{
cue = new Cue(keyframe.KeyTime.Ticks / Duration.Ticks);
}
var newKF = new AnimatorKeyFrame() var newKF = new AnimatorKeyFrame()
{ {
Handler = handler, AnimatorType = handler,
Property = setter.Property, Property = setter.Property,
Cue = keyframe.Cue, Cue = cue,
KeyTime = keyframe.KeyTime,
TimingMode = keyframe.TimingMode,
Value = setter.Value Value = setter.Value
}; };
kfList.Add(newKF); animatorKeyFrames.Add(newKF);
} }
} }
var newAnimatorInstances = new List<(Type handler, AvaloniaProperty prop, IAnimator inst)>(); var newAnimatorInstances = new List<IAnimator>();
foreach (var (handlerType, property) in handlerList) foreach (var (handlerType, property) in handlerList)
{ {
var newInstance = (IAnimator)Activator.CreateInstance(handlerType); var newInstance = (IAnimator)Activator.CreateInstance(handlerType);
newInstance.Property = property; newInstance.Property = property;
newAnimatorInstances.Add((handlerType, property, newInstance)); newAnimatorInstances.Add(newInstance);
} }
foreach (var kf in kfList) foreach (var keyframe in animatorKeyFrames)
{ {
var parent = newAnimatorInstances.First(p => p.handler == kf.Handler && var animator = newAnimatorInstances.First(a => a.GetType() == keyframe.AnimatorType &&
p.prop == kf.Property); a.Property == keyframe.Property);
parent.inst.Add(kf); animator.Add(keyframe);
} }
foreach(var instance in newAnimatorInstances) foreach(var instance in newAnimatorInstances)
_animators.Add(instance.inst); _animators.Add(instance);
} }

4
src/Avalonia.Animation/AnimatorKeyFrame.cs

@ -13,10 +13,8 @@ namespace Avalonia.Animation
/// </summary> /// </summary>
public class AnimatorKeyFrame public class AnimatorKeyFrame
{ {
public Type Handler; public Type AnimatorType;
public Cue Cue; public Cue Cue;
public TimeSpan KeyTime;
internal KeyFrameTimingMode TimingMode;
public AvaloniaProperty Property; public AvaloniaProperty Property;
public object Value; public object Value;
} }

23
src/Avalonia.Animation/Animator`1.cs

@ -42,7 +42,7 @@ namespace Avalonia.Animation
return obsMatch return obsMatch
// Ignore triggers when global timers are paused. // Ignore triggers when global timers are paused.
.Where(p => Timing.GetGlobalPlayState() != PlayState.Pause) .Where(p => p && Timing.GetGlobalPlayState() != PlayState.Pause)
.Subscribe(_ => .Subscribe(_ =>
{ {
var timerObs = RunKeyFrames(animation, control); var timerObs = RunKeyFrames(animation, control);
@ -97,14 +97,14 @@ namespace Avalonia.Animation
/// </summary> /// </summary>
private IDisposable RunKeyFrames(Animation animation, Animatable control) private IDisposable RunKeyFrames(Animation animation, Animatable control)
{ {
var _kfStateMach = new AnimatorStateMachine<T>(); var stateMachine = new AnimatorStateMachine<T>();
_kfStateMach.Initialize(animation, control, this); stateMachine.Initialize(animation, control, this);
Timing.AnimationStateTimer Timing.AnimationStateTimer
.TakeWhile(_ => !_kfStateMach._unsubscribe) .TakeWhile(_ => !stateMachine._unsubscribe)
.Subscribe(p => _kfStateMach.Step(p, DoInterpolation)); .Subscribe(p => stateMachine.Step(p, DoInterpolation));
return control.Bind(Property, _kfStateMach, BindingPriority.Animation); return control.Bind(Property, stateMachine, BindingPriority.Animation);
} }
/// <summary> /// <summary>
@ -131,15 +131,8 @@ namespace Avalonia.Animation
} }
T convertedValue = (T)typeConv.ConvertTo(k.Value, typeof(T)); T convertedValue = (T)typeConv.ConvertTo(k.Value, typeof(T));
Cue _normalizedCue = k.Cue; _convertedKeyframes.Add(k.Cue.CueValue, (convertedValue, false));
if (k.TimingMode == KeyFrameTimingMode.TimeSpan)
{
_normalizedCue = new Cue(k.KeyTime.Ticks / animation.Duration.Ticks);
}
_convertedKeyframes.Add(_normalizedCue.CueValue, (convertedValue, false));
} }
AddNeutralKeyFramesIfNeeded(); AddNeutralKeyFramesIfNeeded();

Loading…
Cancel
Save