9 changed files with 101 additions and 70 deletions
@ -0,0 +1,47 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Reactive.Linq; |
|||
using System.Text; |
|||
using Avalonia.Reactive; |
|||
|
|||
namespace Avalonia.Animation |
|||
{ |
|||
public class Clock : IObservable<TimeSpan> |
|||
{ |
|||
public static Clock GlobalClock => AvaloniaLocator.Current.GetService<Clock>(); |
|||
|
|||
private ClockObservable _observable; |
|||
|
|||
private IObservable<TimeSpan> _connectedObservable; |
|||
|
|||
public Clock() |
|||
{ |
|||
_observable = new ClockObservable(); |
|||
_connectedObservable = _observable.Publish().RefCount(); |
|||
} |
|||
|
|||
public bool HasSubscriptions => _observable.HasSubscriptions; |
|||
|
|||
public TimeSpan CurrentTime { get; private set; } |
|||
|
|||
public void Pulse(long tickCount) |
|||
{ |
|||
var time = TimeSpan.FromMilliseconds(tickCount); |
|||
_observable.Pulse(time); |
|||
CurrentTime = time; |
|||
} |
|||
|
|||
public IDisposable Subscribe(IObserver<TimeSpan> observer) |
|||
{ |
|||
return _connectedObservable.Subscribe(observer); |
|||
} |
|||
|
|||
private class ClockObservable : LightweightObservableBase<TimeSpan> |
|||
{ |
|||
public bool HasSubscriptions { get; private set; } |
|||
public void Pulse(TimeSpan tickCount) => PublishNext(tickCount); |
|||
protected override void Initialize() => HasSubscriptions = true; |
|||
protected override void Deinitialize() => HasSubscriptions = false; |
|||
} |
|||
} |
|||
} |
|||
@ -1,51 +0,0 @@ |
|||
// Copyright (c) The Avalonia Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System; |
|||
using System.Linq; |
|||
using System.Reactive.Linq; |
|||
using Avalonia.Reactive; |
|||
using Avalonia.Threading; |
|||
|
|||
namespace Avalonia.Animation |
|||
{ |
|||
/// <summary>
|
|||
/// Provides global timing functions for animations.
|
|||
/// </summary>
|
|||
public class Timing |
|||
{ |
|||
static TimerObservable _timer = new TimerObservable(); |
|||
|
|||
/// <summary>
|
|||
/// Initializes static members of the <see cref="Timing"/> class.
|
|||
/// </summary>
|
|||
static Timing() |
|||
{ |
|||
AnimationsTimer = _timer |
|||
.Publish() |
|||
.RefCount(); |
|||
} |
|||
|
|||
public static bool HasSubscriptions => _timer.HasSubscriptions; |
|||
|
|||
internal static TimeSpan GetTickCount() => TimeSpan.FromMilliseconds(Environment.TickCount); |
|||
|
|||
/// <summary>
|
|||
/// Gets the animation timer.
|
|||
/// </summary>
|
|||
internal static IObservable<TimeSpan> AnimationsTimer |
|||
{ |
|||
get; |
|||
} |
|||
|
|||
public static void Pulse(long tickCount) => _timer.Pulse(tickCount); |
|||
|
|||
private class TimerObservable : LightweightObservableBase<TimeSpan> |
|||
{ |
|||
public bool HasSubscriptions { get; private set; } |
|||
public void Pulse(long tickCount) => PublishNext(TimeSpan.FromMilliseconds(tickCount)); |
|||
protected override void Initialize() => HasSubscriptions = true; |
|||
protected override void Deinitialize() => HasSubscriptions = false; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
using Avalonia.Rendering; |
|||
|
|||
namespace Avalonia.Animation |
|||
{ |
|||
public class RenderLoopClock : Clock, IRenderLoopTask |
|||
{ |
|||
bool IRenderLoopTask.NeedsUpdate => HasSubscriptions; |
|||
|
|||
void IRenderLoopTask.Render() |
|||
{ |
|||
} |
|||
|
|||
void IRenderLoopTask.Update(long tickCount) |
|||
{ |
|||
Pulse(tickCount); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue