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.
 
 
 
 

368 lines
14 KiB

using System;
using Avalonia.Media;
using Avalonia.Platform;
using Avalonia.Rendering.Composition;
using SkiaSharp;
using SkiaSharp.SceneGraph;
using SkiaSharp.Skottie;
namespace Avalonia.Skia.Lottie;
internal class LottieCompositionCustomVisualHandler : CompositionCustomVisualHandler
{
private TimeSpan _primaryTimeElapsed;
private TimeSpan _animationElapsed;
private TimeSpan? _lastServerTime;
private bool _running;
private Animation? _animation;
private Stretch? _stretch;
private StretchDirection? _stretchDirection;
private InvalidationController? _ic;
private readonly object _sync = new object();
private int _repeatCount;
private int _count;
public override void OnMessage(object message)
{
//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
//IL_0058: Unknown result type (might be due to invalid IL or missing references)
//IL_005d: Unknown result type (might be due to invalid IL or missing references)
//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
//IL_0075: Unknown result type (might be due to invalid IL or missing references)
//IL_007a: Unknown result type (might be due to invalid IL or missing references)
//IL_012a: Unknown result type (might be due to invalid IL or missing references)
//IL_012b: Unknown result type (might be due to invalid IL or missing references)
//IL_012d: Unknown result type (might be due to invalid IL or missing references)
//IL_012e: Unknown result type (might be due to invalid IL or missing references)
//IL_0131: Unknown result type (might be due to invalid IL or missing references)
//IL_013e: Unknown result type (might be due to invalid IL or missing references)
//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
if (!(message is LottiePayload lottiePayload))
{
return;
}
switch (lottiePayload.LottieCommand)
{
case LottieCommand.Start:
{
Animation animation = lottiePayload.Animation;
if (animation == null)
{
break;
}
Stretch? stretch = lottiePayload.Stretch;
if (!stretch.HasValue)
{
break;
}
Stretch valueOrDefault = stretch.GetValueOrDefault();
StretchDirection? stretchDirection = lottiePayload.StretchDirection;
if (stretchDirection.HasValue)
{
StretchDirection valueOrDefault2 = stretchDirection.GetValueOrDefault();
int? repeatCount = lottiePayload.RepeatCount;
if (repeatCount.HasValue)
{
int valueOrDefault3 = repeatCount.GetValueOrDefault();
_running = true;
_lastServerTime = null;
_stretch = valueOrDefault;
_stretchDirection = valueOrDefault2;
_animation = animation;
_repeatCount = valueOrDefault3;
_count = 0;
_animationElapsed = TimeSpan.Zero;
((CompositionCustomVisualHandler)this).RegisterForNextAnimationFrameUpdate();
}
}
break;
}
case LottieCommand.Update:
{
Stretch? stretch = lottiePayload.Stretch;
if (stretch.HasValue)
{
Stretch valueOrDefault = stretch.GetValueOrDefault();
StretchDirection? stretchDirection = lottiePayload.StretchDirection;
if (stretchDirection.HasValue)
{
StretchDirection valueOrDefault2 = stretchDirection.GetValueOrDefault();
Stretch value = valueOrDefault;
StretchDirection value2 = valueOrDefault2;
_stretch = value;
_stretchDirection = value2;
((CompositionCustomVisualHandler)this).RegisterForNextAnimationFrameUpdate();
}
}
break;
}
case LottieCommand.Stop:
_running = false;
_animationElapsed = TimeSpan.Zero;
_count = 0;
break;
case LottieCommand.Dispose:
DisposeImpl();
break;
}
}
public override void OnAnimationFrameUpdate()
{
if (_running)
{
if (_repeatCount == 0 || (_repeatCount > 0 && _count >= _repeatCount))
{
_running = false;
_animationElapsed = TimeSpan.Zero;
}
((CompositionCustomVisualHandler)this).Invalidate();
((CompositionCustomVisualHandler)this).RegisterForNextAnimationFrameUpdate();
}
}
private void DisposeImpl()
{
lock (_sync)
{
Animation? animation = _animation;
if (animation != null)
{
((SKNativeObject)animation).Dispose();
}
_animation = null;
InvalidationController? ic = _ic;
if (ic != null)
{
ic.End();
}
InvalidationController? ic2 = _ic;
if (ic2 != null)
{
((SKNativeObject)ic2).Dispose();
}
_ic = null;
}
}
private double GetFrameTime()
{
if (_animation == null)
{
return 0.0;
}
double totalSeconds = _animationElapsed.TotalSeconds;
if (_animationElapsed.TotalSeconds > _animation.Duration.TotalSeconds)
{
_animationElapsed = TimeSpan.Zero;
InvalidationController? ic = _ic;
if (ic != null)
{
ic.End();
}
InvalidationController? ic2 = _ic;
if (ic2 != null)
{
ic2.Begin();
}
_count++;
}
return totalSeconds;
}
private void Draw(SKCanvas canvas)
{
//IL_0014: Unknown result type (might be due to invalid IL or missing references)
//IL_001e: Expected O, but got Unknown
//IL_0067: Unknown result type (might be due to invalid IL or missing references)
//IL_006c: Unknown result type (might be due to invalid IL or missing references)
//IL_0076: Unknown result type (might be due to invalid IL or missing references)
//IL_007b: Unknown result type (might be due to invalid IL or missing references)
//IL_009a: Unknown result type (might be due to invalid IL or missing references)
Animation animation = _animation;
if (animation == null)
{
return;
}
if (_ic == null)
{
_ic = new InvalidationController();
_ic.Begin();
}
InvalidationController ic = _ic;
if (_repeatCount != 0)
{
double num = GetFrameTime();
if (!_running)
{
num = (float)animation.Duration.TotalSeconds;
}
SKSize size = animation.Size;
float width = ((SKSize)(ref size)).Width;
size = animation.Size;
SKRect val = default(SKRect);
((SKRect)(ref val))._002Ector(0f, 0f, width, ((SKSize)(ref size)).Height);
animation.SeekFrameTime(num, ic);
canvas.Save();
animation.Render(canvas, val);
canvas.Restore();
ic.Reset();
}
}
public unsafe override void OnRender(ImmediateDrawingContext context)
{
//IL_0092: Unknown result type (might be due to invalid IL or missing references)
//IL_0097: Unknown result type (might be due to invalid IL or missing references)
//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
//IL_0130: Unknown result type (might be due to invalid IL or missing references)
//IL_0133: Unknown result type (might be due to invalid IL or missing references)
//IL_0138: Unknown result type (might be due to invalid IL or missing references)
//IL_013a: Unknown result type (might be due to invalid IL or missing references)
//IL_013c: Unknown result type (might be due to invalid IL or missing references)
//IL_0141: Unknown result type (might be due to invalid IL or missing references)
//IL_0143: Unknown result type (might be due to invalid IL or missing references)
//IL_0145: Unknown result type (might be due to invalid IL or missing references)
//IL_0147: Unknown result type (might be due to invalid IL or missing references)
//IL_014c: Unknown result type (might be due to invalid IL or missing references)
//IL_0150: Unknown result type (might be due to invalid IL or missing references)
//IL_0152: Unknown result type (might be due to invalid IL or missing references)
//IL_0157: Unknown result type (might be due to invalid IL or missing references)
//IL_015c: Unknown result type (might be due to invalid IL or missing references)
//IL_0160: Unknown result type (might be due to invalid IL or missing references)
//IL_0162: Unknown result type (might be due to invalid IL or missing references)
//IL_0167: Unknown result type (might be due to invalid IL or missing references)
//IL_0169: Unknown result type (might be due to invalid IL or missing references)
//IL_016b: Unknown result type (might be due to invalid IL or missing references)
//IL_0170: Unknown result type (might be due to invalid IL or missing references)
//IL_0176: Unknown result type (might be due to invalid IL or missing references)
//IL_017b: Unknown result type (might be due to invalid IL or missing references)
//IL_017d: Unknown result type (might be due to invalid IL or missing references)
//IL_0182: Unknown result type (might be due to invalid IL or missing references)
//IL_0187: Unknown result type (might be due to invalid IL or missing references)
//IL_018c: Unknown result type (might be due to invalid IL or missing references)
//IL_0190: Unknown result type (might be due to invalid IL or missing references)
//IL_0196: Unknown result type (might be due to invalid IL or missing references)
//IL_0199: Unknown result type (might be due to invalid IL or missing references)
//IL_019e: Unknown result type (might be due to invalid IL or missing references)
//IL_01a3: Unknown result type (might be due to invalid IL or missing references)
//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
//IL_01fc: Unknown result type (might be due to invalid IL or missing references)
//IL_0201: Unknown result type (might be due to invalid IL or missing references)
//IL_0204: Unknown result type (might be due to invalid IL or missing references)
//IL_0206: Unknown result type (might be due to invalid IL or missing references)
//IL_020b: Unknown result type (might be due to invalid IL or missing references)
//IL_020e: Unknown result type (might be due to invalid IL or missing references)
//IL_0210: Unknown result type (might be due to invalid IL or missing references)
//IL_0212: Unknown result type (might be due to invalid IL or missing references)
//IL_0217: Unknown result type (might be due to invalid IL or missing references)
//IL_021c: Unknown result type (might be due to invalid IL or missing references)
lock (_sync)
{
if (_running)
{
if (_lastServerTime.HasValue)
{
TimeSpan timeSpan = ((CompositionCustomVisualHandler)this).CompositionNow - _lastServerTime.Value;
_primaryTimeElapsed += timeSpan;
_animationElapsed += timeSpan;
}
_lastServerTime = ((CompositionCustomVisualHandler)this).CompositionNow;
}
Animation animation = _animation;
if (animation == null)
{
return;
}
Stretch? stretch = _stretch;
if (!stretch.HasValue)
{
return;
}
Stretch valueOrDefault = stretch.GetValueOrDefault();
StretchDirection? stretchDirection = _stretchDirection;
if (!stretchDirection.HasValue)
{
return;
}
StretchDirection valueOrDefault2 = stretchDirection.GetValueOrDefault();
ISkiaSharpApiLeaseFeature val = OptionalFeatureProviderExtensions.TryGetFeature<ISkiaSharpApiLeaseFeature>((IOptionalFeatureProvider)(object)context);
if (val == null)
{
return;
}
Rect renderBounds = ((CompositionCustomVisualHandler)this).GetRenderBounds();
Rect val2 = default(Rect);
((Rect)(ref val2))._002Ector(((Rect)(ref renderBounds)).Size);
SKSize size = animation.Size;
double num = ((SKSize)(ref size)).Width;
size = animation.Size;
Size val3 = default(Size);
((Size)(ref val3))._002Ector(num, (double)((SKSize)(ref size)).Height);
if (((Size)(ref val3)).Width <= 0.0 || ((Size)(ref val3)).Height <= 0.0)
{
return;
}
Vector val4 = MediaExtensions.CalculateScaling(valueOrDefault, ((Rect)(ref renderBounds)).Size, val3, valueOrDefault2);
Size val5 = val3 * val4;
Rect val6 = ((Rect)(ref val2)).CenterRect(new Rect(val5));
Rect val7 = ((Rect)(ref val6)).Intersect(val2);
val6 = new Rect(val3);
Rect val8 = ((Rect)(ref val6)).CenterRect(new Rect(((Rect)(ref val7)).Size / val4));
SKRect val9 = SKRect.Create(default(SKPoint), animation.Size);
Matrix val10 = Matrix.CreateScale(((Rect)(ref val7)).Width / ((Rect)(ref val8)).Width, ((Rect)(ref val7)).Height / ((Rect)(ref val8)).Height);
Matrix val11 = Matrix.CreateTranslation(0.0 - ((Rect)(ref val8)).X + ((Rect)(ref val7)).X - (double)((SKRect)(ref val9)).Top, 0.0 - ((Rect)(ref val8)).Y + ((Rect)(ref val7)).Y - (double)((SKRect)(ref val9)).Left);
PushedState val12 = context.PushClip(val7);
try
{
PushedState val13 = context.PushPostTransform(val11 * val10);
try
{
ISkiaSharpApiLease val14 = val.Lease();
try
{
SKCanvas val15 = ((val14 != null) ? val14.SkCanvas : null);
if (val15 != null)
{
Draw(val15);
}
}
finally
{
((IDisposable)val14)?.Dispose();
}
}
finally
{
((IDisposable)(*(PushedState*)(&val13))/*cast due to .constrained prefix*/).Dispose();
}
}
finally
{
((IDisposable)(*(PushedState*)(&val12))/*cast due to .constrained prefix*/).Dispose();
}
}
}
}