|
|
|
@ -25,6 +25,8 @@ namespace Avalonia.Animation |
|
|
|
/// </summary>
|
|
|
|
private static readonly TimeSpan Tick = TimeSpan.FromSeconds(1.0 / FramesPerSecond); |
|
|
|
|
|
|
|
static ulong _frameCount = 0; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes static members of the <see cref="Timing"/> class.
|
|
|
|
/// </summary>
|
|
|
|
@ -33,7 +35,11 @@ namespace Avalonia.Animation |
|
|
|
Stopwatch = new Stopwatch(); |
|
|
|
Stopwatch.Start(); |
|
|
|
Timer = Observable.Interval(Tick, AvaloniaScheduler.Instance) |
|
|
|
.Select(_ => Stopwatch.Elapsed) |
|
|
|
.Select(_ => |
|
|
|
{ |
|
|
|
_frameCount += 1; |
|
|
|
return _frameCount; |
|
|
|
}) |
|
|
|
.Publish() |
|
|
|
.RefCount(); |
|
|
|
} |
|
|
|
@ -54,39 +60,17 @@ namespace Avalonia.Animation |
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// The animation timer ticks <see cref="FramesPerSecond"/> times per second. The
|
|
|
|
/// parameter passed to a subsciber is the time span since the animation system was
|
|
|
|
/// parameter passed to a subsciber is the number of frames since the animation system was
|
|
|
|
/// initialized.
|
|
|
|
/// </remarks>
|
|
|
|
/// <value>
|
|
|
|
/// The animation timer.
|
|
|
|
/// </value>
|
|
|
|
public static IObservable<TimeSpan> Timer |
|
|
|
public static IObservable<ulong> Timer |
|
|
|
{ |
|
|
|
get; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a timer that fires every frame for the specified duration.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="duration">The duration of the animation.</param>
|
|
|
|
/// <returns>
|
|
|
|
/// An observable that notifies the subscriber of the progress along the animation.
|
|
|
|
/// </returns>
|
|
|
|
/// <remarks>
|
|
|
|
/// The parameter passed to the subscriber is the progress along the animation, with
|
|
|
|
/// 0 being the start and 1 being the end. The observable is guaranteed to fire 0
|
|
|
|
/// immediately on subscribe and 1 at the end of the duration.
|
|
|
|
/// </remarks>
|
|
|
|
public static IObservable<double> GetTimer(TimeSpan duration) |
|
|
|
{ |
|
|
|
var startTime = Stopwatch.Elapsed.Ticks; |
|
|
|
var endTime = startTime + duration.Ticks; |
|
|
|
return Timer |
|
|
|
.TakeWhile(x => x.Ticks < endTime) |
|
|
|
.Select(x => (x.Ticks - startTime) / (double)duration.Ticks) |
|
|
|
.StartWith(0.0) |
|
|
|
.Concat(Observable.Return(1.0)); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a timer that fires every frame for the specified duration with delay.
|
|
|
|
@ -99,13 +83,14 @@ namespace Avalonia.Animation |
|
|
|
/// 0 being the start and 1 being the end. The observable is guaranteed to fire 0
|
|
|
|
/// immediately on subscribe and 1 at the end of the duration.
|
|
|
|
/// </remarks>
|
|
|
|
public static IObservable<double> GetTimer(TimeSpan duration, TimeSpan delay) |
|
|
|
public static IObservable<double> GetTimer(Animatable control, TimeSpan duration, TimeSpan delay) |
|
|
|
{ |
|
|
|
var startTime = Stopwatch.Elapsed.Ticks + delay.Ticks; |
|
|
|
var endTime = startTime + duration.Ticks; |
|
|
|
var startTime = _frameCount; |
|
|
|
var _duration = (ulong)(duration.Ticks/Tick.Ticks); |
|
|
|
var endTime = startTime + _duration; |
|
|
|
return Timer |
|
|
|
.TakeWhile(x => x.Ticks < endTime) |
|
|
|
.Select(x => (x.Ticks - startTime) / (double)duration.Ticks) |
|
|
|
.TakeWhile(x => x < endTime) |
|
|
|
.Select(x => (double)(x - startTime) / _duration) |
|
|
|
.StartWith(0.0) |
|
|
|
.Concat(Observable.Return(1.0)); |
|
|
|
} |
|
|
|
|