Skip to content

Commit

Permalink
Merged FrameRate and TargetFps variables, since they meant the same t…
Browse files Browse the repository at this point in the history
…hing.
  • Loading branch information
azchohfi committed Feb 20, 2018
1 parent df5dfbf commit c1fe075
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion LottieUWP.Sample/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="FPS:" VerticalAlignment="Center" Margin="5,0"/>
<TextBox Grid.Column="1" Text="{x:Bind LottieAnimationView.TargetFps, Mode=TwoWay}"/>
<TextBox Grid.Column="1" Text="{x:Bind LottieAnimationView.FrameRate, Mode=TwoWay}"/>
</Grid>
</StackPanel>
<Slider Grid.Row="1" Maximum="10" Value="1" Minimum="0.1" SmallChange="0.1" LargeChange="0.1" ValueChanged="Scale_OnValueChanged" StepFrequency="0.1" />
Expand Down
15 changes: 8 additions & 7 deletions LottieUWP/LottieAnimationView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ public virtual LottieComposition Composition
ImageDrawable = _lottieDrawable;

_composition = value;
FrameRate = _composition.FrameRate;

InvalidateArrange();
InvalidateMeasure();
Expand Down Expand Up @@ -769,20 +770,20 @@ private static void RepeatCountPropertyChangedCallback(DependencyObject dependen
lottieAnimationView._lottieDrawable.RepeatCount = (int)e.NewValue;
}

public int TargetFps
public float FrameRate
{
get { return (int)GetValue(TargetFpsProperty); }
set { SetValue(TargetFpsProperty, value); }
get { return (float)GetValue(FrameRateProperty); }
set { SetValue(FrameRateProperty, value); }
}

// Using a DependencyProperty as the backing store for RepeatCount. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TargetFpsProperty =
DependencyProperty.Register("TargetFps", typeof(int), typeof(LottieAnimationView), new PropertyMetadata(60, TargetFpsPropertyChangedCallback));
public static readonly DependencyProperty FrameRateProperty =
DependencyProperty.Register("FrameRate", typeof(float), typeof(LottieAnimationView), new PropertyMetadata(60f, FrameRatePropertyChangedCallback));

private static void TargetFpsPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
private static void FrameRatePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
if (dependencyObject is LottieAnimationView lottieAnimationView)
lottieAnimationView._lottieDrawable.TargetFps = (int)e.NewValue;
lottieAnimationView._lottieDrawable.FrameRate = (float)e.NewValue;
}

public virtual bool IsAnimating => _lottieDrawable.IsAnimating;
Expand Down
6 changes: 3 additions & 3 deletions LottieUWP/LottieDrawable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -598,10 +598,10 @@ public int RepeatCount
get => _animator.RepeatCount;
}

public int TargetFps
public float FrameRate
{
get => _animator.TargetFps;
set => _animator.TargetFps = value;
get => _animator.FrameRate;
set => _animator.FrameRate = value;
}

public virtual bool IsAnimating => _animator.IsRunning;
Expand Down
22 changes: 18 additions & 4 deletions LottieUWP/Utils/LottieValueAnimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public class LottieValueAnimator : BaseLottieAnimator
private float _minFrame = int.MinValue;
private float _maxFrame = int.MaxValue;
private LottieComposition _composition;
private float _frameRate;
protected bool _isRunning;

/// <summary>
/// Returns a float representing the current value of the animation from 0 to 1
/// regardless of the animation speed, direction, or min and max frames.
Expand Down Expand Up @@ -96,15 +97,17 @@ public override void DoFrame()
long timeSinceFrame = now - _frameTime;
float frameDuration = FrameDurationNs;
float frames = timeSinceFrame / frameDuration;
if (frames == 0)
int wholeFrames = (int)frames;
if (wholeFrames == 0)
{
return;
}
_frame += IsReversed ? -frames : frames;
_frame += IsReversed ? -wholeFrames : wholeFrames;
bool ended = !MiscUtils.Contains(_frame, MinFrame, MaxFrame);
_frame = MiscUtils.Clamp(_frame, MinFrame, MaxFrame);

_frameTime = now;
float partialFramesDuration = (frames - wholeFrames) * frameDuration;
_frameTime = (long)(now - partialFramesDuration);

Debug.WriteLineIf(LottieLog.TraceEnabled, $"Tick milliseconds: {timeSinceFrame}", LottieLog.Tag);

Expand Down Expand Up @@ -148,11 +151,22 @@ private float FrameDurationNs
}
}

public override float FrameRate
{
get => _frameRate;
set
{
_frameRate = value <= 1000 ? (value > 1 ? value : 1) : 1000;
UpdateTimerInterval();
}
}

public LottieComposition Composition
{
set
{
_composition = value;
FrameRate = _composition.FrameRate;
_frame = MinFrame;
_frameTime = SystemnanoTime();
}
Expand Down
18 changes: 7 additions & 11 deletions LottieUWP/ValueAnimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,8 @@ public void RemoveAllListeners()

private IInterpolator _interpolator;
private Timer _timer;
private int _targetFps = 60;

public int TargetFps
{
get => _targetFps;
set
{
_targetFps = value <= 1000 ? (value > 1 ? value : 1) : 1000;
_timer?.Change(TimeSpan.Zero, GetTimerInterval());
}
}
public abstract float FrameRate { get; set; }

protected virtual void OnValueChanged()
{
Expand Down Expand Up @@ -85,9 +76,14 @@ protected void PrivateStart()
}
}

protected void UpdateTimerInterval()
{
_timer?.Change(TimeSpan.Zero, GetTimerInterval());
}

private TimeSpan GetTimerInterval()
{
return TimeSpan.FromTicks((long)Math.Floor((decimal)TimeSpan.TicksPerSecond / TargetFps));
return TimeSpan.FromTicks((long)Math.Floor(TimeSpan.TicksPerSecond / (decimal)FrameRate));
}

protected virtual void RemoveFrameCallback()
Expand Down

0 comments on commit c1fe075

Please sign in to comment.