You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
397 lines
14 KiB
397 lines
14 KiB
using System;
|
|
using System.IO;
|
|
using System.Numerics;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Data;
|
|
using Avalonia.Interactivity;
|
|
using Avalonia.Layout;
|
|
using Avalonia.Logging;
|
|
using Avalonia.Media;
|
|
using Avalonia.Metadata;
|
|
using Avalonia.Platform;
|
|
using Avalonia.Rendering.Composition;
|
|
using SkiaSharp;
|
|
using SkiaSharp.SceneGraph;
|
|
using SkiaSharp.Skottie;
|
|
|
|
namespace Avalonia.Skia.Lottie;
|
|
|
|
public class Lottie : Control
|
|
{
|
|
private Animation? _animation;
|
|
|
|
private int _repeatCount;
|
|
|
|
private readonly Uri _baseUri;
|
|
|
|
private string? _preloadPath;
|
|
|
|
private CompositionCustomVisual? _customVisual;
|
|
|
|
public const int Infinity = -1;
|
|
|
|
public static readonly StyledProperty<string?> PathProperty = AvaloniaProperty.Register<Lottie, string>("Path", (string)null, false, (BindingMode)1, (Func<string, bool>)null, (Func<AvaloniaObject, string, string>)null, false);
|
|
|
|
public static readonly StyledProperty<Stretch> StretchProperty = AvaloniaProperty.Register<Lottie, Stretch>("Stretch", (Stretch)2, false, (BindingMode)1, (Func<Stretch, bool>)null, (Func<AvaloniaObject, Stretch, Stretch>)null, false);
|
|
|
|
public static readonly StyledProperty<StretchDirection> StretchDirectionProperty = AvaloniaProperty.Register<Lottie, StretchDirection>("StretchDirection", (StretchDirection)2, false, (BindingMode)1, (Func<StretchDirection, bool>)null, (Func<AvaloniaObject, StretchDirection, StretchDirection>)null, false);
|
|
|
|
public static readonly StyledProperty<int> RepeatCountProperty = AvaloniaProperty.Register<Lottie, int>("RepeatCount", -1, false, (BindingMode)1, (Func<int, bool>)null, (Func<AvaloniaObject, int, int>)null, false);
|
|
|
|
[Content]
|
|
public string? Path
|
|
{
|
|
get
|
|
{
|
|
return ((AvaloniaObject)this).GetValue<string>(PathProperty);
|
|
}
|
|
set
|
|
{
|
|
((AvaloniaObject)this).SetValue<string>(PathProperty, value, (BindingPriority)0);
|
|
}
|
|
}
|
|
|
|
public Stretch Stretch
|
|
{
|
|
get
|
|
{
|
|
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
|
|
return ((AvaloniaObject)this).GetValue<Stretch>(StretchProperty);
|
|
}
|
|
set
|
|
{
|
|
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
|
|
((AvaloniaObject)this).SetValue<Stretch>(StretchProperty, value, (BindingPriority)0);
|
|
}
|
|
}
|
|
|
|
public StretchDirection StretchDirection
|
|
{
|
|
get
|
|
{
|
|
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
|
|
return ((AvaloniaObject)this).GetValue<StretchDirection>(StretchDirectionProperty);
|
|
}
|
|
set
|
|
{
|
|
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
|
|
((AvaloniaObject)this).SetValue<StretchDirection>(StretchDirectionProperty, value, (BindingPriority)0);
|
|
}
|
|
}
|
|
|
|
public int RepeatCount
|
|
{
|
|
get
|
|
{
|
|
return ((AvaloniaObject)this).GetValue<int>(RepeatCountProperty);
|
|
}
|
|
set
|
|
{
|
|
((AvaloniaObject)this).SetValue<int>(RepeatCountProperty, value, (BindingPriority)0);
|
|
}
|
|
}
|
|
|
|
public Lottie(Uri baseUri)
|
|
{
|
|
_baseUri = baseUri;
|
|
}
|
|
|
|
public Lottie(IServiceProvider serviceProvider)
|
|
{
|
|
_baseUri = serviceProvider.GetContextBaseUri();
|
|
}
|
|
|
|
protected override void OnLoaded(RoutedEventArgs routedEventArgs)
|
|
{
|
|
//IL_006f: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0074: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0077: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_007c: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0086: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_008b: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_008e: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0093: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
|
|
((Control)this).OnLoaded(routedEventArgs);
|
|
CompositionVisual elementVisual = ElementComposition.GetElementVisual((Visual)(object)this);
|
|
Compositor val = ((elementVisual != null) ? ((CompositionObject)elementVisual).Compositor : null);
|
|
if (val != null)
|
|
{
|
|
_customVisual = val.CreateCustomVisual((CompositionCustomVisualHandler)(object)new LottieCompositionCustomVisualHandler());
|
|
ElementComposition.SetElementChildVisual((Visual)(object)this, (CompositionVisual)(object)_customVisual);
|
|
((Layoutable)this).LayoutUpdated += OnLayoutUpdated;
|
|
if (_preloadPath != null)
|
|
{
|
|
DisposeImpl();
|
|
Load(_preloadPath);
|
|
CompositionCustomVisual? customVisual = _customVisual;
|
|
Rect bounds = ((Visual)this).Bounds;
|
|
Size size = ((Rect)(ref bounds)).Size;
|
|
float x = (float)((Size)(ref size)).Width;
|
|
bounds = ((Visual)this).Bounds;
|
|
size = ((Rect)(ref bounds)).Size;
|
|
((CompositionVisual)customVisual).Size = Vector.op_Implicit(new Vector2(x, (float)((Size)(ref size)).Height));
|
|
_customVisual.SendHandlerMessage((object)new LottiePayload(LottieCommand.Update, _animation, Stretch, StretchDirection));
|
|
Start();
|
|
_preloadPath = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
protected override void OnUnloaded(RoutedEventArgs routedEventArgs)
|
|
{
|
|
((Control)this).OnUnloaded(routedEventArgs);
|
|
((Layoutable)this).LayoutUpdated -= OnLayoutUpdated;
|
|
Stop();
|
|
DisposeImpl();
|
|
}
|
|
|
|
private void OnLayoutUpdated(object? sender, EventArgs e)
|
|
{
|
|
//IL_0010: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0015: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0018: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_001d: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0027: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_002c: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_002f: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0034: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0042: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_005a: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0065: Unknown result type (might be due to invalid IL or missing references)
|
|
if (_customVisual != null)
|
|
{
|
|
CompositionCustomVisual? customVisual = _customVisual;
|
|
Rect bounds = ((Visual)this).Bounds;
|
|
Size size = ((Rect)(ref bounds)).Size;
|
|
float x = (float)((Size)(ref size)).Width;
|
|
bounds = ((Visual)this).Bounds;
|
|
size = ((Rect)(ref bounds)).Size;
|
|
((CompositionVisual)customVisual).Size = Vector.op_Implicit(new Vector2(x, (float)((Size)(ref size)).Height));
|
|
_customVisual.SendHandlerMessage((object)new LottiePayload(LottieCommand.Update, _animation, Stretch, StretchDirection));
|
|
}
|
|
}
|
|
|
|
protected override Size MeasureOverride(Size availableSize)
|
|
{
|
|
//IL_000a: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0010: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_002b: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0030: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_003f: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0044: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_004d: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_001c: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0022: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0052: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0054: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0059: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_005a: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_005c: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0061: Unknown result type (might be due to invalid IL or missing references)
|
|
if (_animation == null)
|
|
{
|
|
return default(Size);
|
|
}
|
|
_003F val;
|
|
if (_animation == null)
|
|
{
|
|
val = default(Size);
|
|
}
|
|
else
|
|
{
|
|
SKSize size = _animation.Size;
|
|
double num = ((SKSize)(ref size)).Width;
|
|
size = _animation.Size;
|
|
val = new Size(num, (double)((SKSize)(ref size)).Height);
|
|
}
|
|
Size val2 = (Size)val;
|
|
return MediaExtensions.CalculateSize(Stretch, availableSize, val2, StretchDirection);
|
|
}
|
|
|
|
protected override Size ArrangeOverride(Size finalSize)
|
|
{
|
|
//IL_000a: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0010: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_002b: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0030: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_003f: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0044: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_004d: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_001c: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0022: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0052: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0054: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0059: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_005a: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_005c: Unknown result type (might be due to invalid IL or missing references)
|
|
if (_animation == null)
|
|
{
|
|
return default(Size);
|
|
}
|
|
_003F val;
|
|
if (_animation == null)
|
|
{
|
|
val = default(Size);
|
|
}
|
|
else
|
|
{
|
|
SKSize size = _animation.Size;
|
|
double num = ((SKSize)(ref size)).Width;
|
|
size = _animation.Size;
|
|
val = new Size(num, (double)((SKSize)(ref size)).Height);
|
|
}
|
|
Size val2 = (Size)val;
|
|
return MediaExtensions.CalculateSize(Stretch, finalSize, val2, (StretchDirection)2);
|
|
}
|
|
|
|
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
|
|
{
|
|
((Control)this).OnPropertyChanged(change);
|
|
string name = change.Property.Name;
|
|
if (!(name == "Path"))
|
|
{
|
|
if (name == "RepeatCount")
|
|
{
|
|
_repeatCount = AvaloniaPropertyChangedExtensions.GetNewValue<int>(change);
|
|
Stop();
|
|
Start();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
string newValue = AvaloniaPropertyChangedExtensions.GetNewValue<string>(change);
|
|
if (_preloadPath == null && _customVisual == null)
|
|
{
|
|
_preloadPath = newValue;
|
|
}
|
|
else
|
|
{
|
|
Load(newValue);
|
|
}
|
|
}
|
|
}
|
|
|
|
private Animation? Load(Stream stream)
|
|
{
|
|
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0007: Expected O, but got Unknown
|
|
//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0040: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0045: Unknown result type (might be due to invalid IL or missing references)
|
|
SKManagedStream val = new SKManagedStream(stream);
|
|
try
|
|
{
|
|
Animation val2 = default(Animation);
|
|
ParametrizedLogger valueOrDefault;
|
|
if (Animation.TryCreate((SKStream)(object)val, ref val2))
|
|
{
|
|
val2.Seek(0.0, (InvalidationController)null);
|
|
ParametrizedLogger? val3 = Logger.TryGet((LogEventLevel)2, "Control");
|
|
if (val3.HasValue)
|
|
{
|
|
valueOrDefault = val3.GetValueOrDefault();
|
|
((ParametrizedLogger)(ref valueOrDefault)).Log((object)this, $"Version: {val2.Version} Duration: {val2.Duration} Fps:{val2.Fps} InPoint: {val2.InPoint} OutPoint: {val2.OutPoint}");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ParametrizedLogger? val3 = Logger.TryGet((LogEventLevel)3, "Control");
|
|
if (val3.HasValue)
|
|
{
|
|
valueOrDefault = val3.GetValueOrDefault();
|
|
((ParametrizedLogger)(ref valueOrDefault)).Log((object)this, "Failed to load animation.");
|
|
}
|
|
}
|
|
return val2;
|
|
}
|
|
finally
|
|
{
|
|
((IDisposable)val)?.Dispose();
|
|
}
|
|
}
|
|
|
|
private Animation? Load(string path, Uri? baseUri)
|
|
{
|
|
Uri uri = (path.StartsWith("/") ? new Uri(path, UriKind.Relative) : new Uri(path, UriKind.RelativeOrAbsolute));
|
|
if (uri.IsAbsoluteUri && uri.IsFile)
|
|
{
|
|
using (FileStream stream = File.OpenRead(uri.LocalPath))
|
|
{
|
|
return Load(stream);
|
|
}
|
|
}
|
|
using Stream stream2 = AssetLoader.Open(uri, baseUri);
|
|
if (stream2 == null)
|
|
{
|
|
return null;
|
|
}
|
|
return Load(stream2);
|
|
}
|
|
|
|
private void Load(string? path)
|
|
{
|
|
//IL_006d: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0072: Unknown result type (might be due to invalid IL or missing references)
|
|
Stop();
|
|
if (path == null)
|
|
{
|
|
DisposeImpl();
|
|
return;
|
|
}
|
|
DisposeImpl();
|
|
try
|
|
{
|
|
_repeatCount = RepeatCount;
|
|
_animation = Load(path, _baseUri);
|
|
if (_animation != null)
|
|
{
|
|
((Layoutable)this).InvalidateArrange();
|
|
((Layoutable)this).InvalidateMeasure();
|
|
Start();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ParametrizedLogger? val = Logger.TryGet((LogEventLevel)3, "Control");
|
|
if (val.HasValue)
|
|
{
|
|
ParametrizedLogger valueOrDefault = val.GetValueOrDefault();
|
|
((ParametrizedLogger)(ref valueOrDefault)).Log((object)this, "Failed to load animation: " + ex);
|
|
}
|
|
_animation = null;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
//IL_0013: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_001e: Unknown result type (might be due to invalid IL or missing references)
|
|
CompositionCustomVisual? customVisual = _customVisual;
|
|
if (customVisual != null)
|
|
{
|
|
customVisual.SendHandlerMessage((object)new LottiePayload(LottieCommand.Start, _animation, Stretch, StretchDirection, _repeatCount));
|
|
}
|
|
}
|
|
|
|
private void Stop()
|
|
{
|
|
CompositionCustomVisual? customVisual = _customVisual;
|
|
if (customVisual != null)
|
|
{
|
|
customVisual.SendHandlerMessage((object)new LottiePayload(LottieCommand.Stop));
|
|
}
|
|
}
|
|
|
|
private void DisposeImpl()
|
|
{
|
|
CompositionCustomVisual? customVisual = _customVisual;
|
|
if (customVisual != null)
|
|
{
|
|
customVisual.SendHandlerMessage((object)new LottiePayload(LottieCommand.Dispose));
|
|
}
|
|
}
|
|
}
|
|
|