Skip to content

Commit

Permalink
feat(lottie): [Wasm] Added Progress, IsPlaying and Duration
Browse files Browse the repository at this point in the history
  • Loading branch information
carldebilly committed May 2, 2020
1 parent 179c2cc commit c0887fb
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 146 deletions.
50 changes: 29 additions & 21 deletions src/AddIns/Uno.UI.Lottie/LottieVisualSource.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,19 @@ public override void OnAnimationStart(Animator animation)

private string _lastPath = "";

private AnimatedVisualPlayer _player;

private void Update()
partial void InnerUpdate()
{
if (_player != null)
{
Update(_player);
}
}
var player = _player;

public void Update(AnimatedVisualPlayer player)
{
if (_animation == null)
{
_listener = new LottieListener(this);

_animation = new LottieAnimationView(Android.App.Application.Context);
_animation.EnableMergePathsForKitKatAndAbove(true);
//_animation.UseHardwareAcceleration(UseHardwareAcceleration);

_animation.AddAnimatorListener(_listener);

//_animation.Scale = (float)Scale;
SetProperties();

player.AddView(_animation);
Expand All @@ -98,11 +88,10 @@ void SetProperties()

if (player.AutoPlay)
{
Play(true);
Play(0, 1, true);
}

var duration = TimeSpan.FromMilliseconds(_animation.Duration);
_player?.SetValue(AnimatedVisualPlayer.DurationProperty, duration);
SetDuration();
}

switch (player.Stretch)
Expand All @@ -122,20 +111,26 @@ void SetProperties()
}

_animation.Speed = (float)player.PlaybackRate;

}

_player = player;
}

public void Play(bool looped)
private void SetDuration()
{
var duration = TimeSpan.FromMilliseconds(_animation.Duration);
_player?.SetValue(AnimatedVisualPlayer.DurationProperty, duration);
_player?.SetValue(AnimatedVisualPlayer.IsAnimatedVisualLoadedProperty, duration > TimeSpan.Zero);
}

public void Play(double fromProgress, double toProgress, bool looped)
{
SetIsPlaying(true);
#if __ANDROID_26__
_animation.RepeatCount = looped ? ValueAnimator.Infinite : 0; // Repeat count doesn't include first time.
#else
_animation.Loop(looped);
#endif
_animation.SetMinProgress((float)fromProgress);
_animation.SetMaxProgress((float)toProgress);
_animation.PlayAnimation();
}

Expand Down Expand Up @@ -175,9 +170,22 @@ public void Unload()
}
}

Size IAnimatedVisualSource.Measure(Size availableSize)
private Size CompositionSize
{
return availableSize;
get
{
var composition = _animation?.Composition;
if (composition != null)
{
SetDuration();

var bounds = composition.Bounds;

return new Size(bounds.Width(), bounds.Height());
}

return default;
}
}
}
}
72 changes: 58 additions & 14 deletions src/AddIns/Uno.UI.Lottie/LottieVisualSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Uno.UI;

namespace Microsoft.Toolkit.Uwp.UI.Lottie
{
public partial class LottieVisualSource : DependencyObject, IAnimatedVisualSource
{
private AnimatedVisualPlayer _player;

public static readonly DependencyProperty UriSourceProperty = DependencyProperty.Register(
"UriSource", typeof(Uri), typeof(LottieVisualSource), new PropertyMetadata(default(Uri), OnUriSourceChanged));
"UriSource",
typeof(Uri),
typeof(LottieVisualSource),
new FrameworkPropertyMetadata(
default(Uri),
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsArrange,
OnUriSourceChanged));

public Uri UriSource
{
Expand All @@ -37,7 +47,10 @@ public static LottieVisualSource CreateFromString(string uri)

private static void OnUriSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
(sender as LottieVisualSource)?.Update();
if (sender is LottieVisualSource source)
{
source.Update(source._player);
}
}

public Task SetSourceAsync(Uri sourceUri)
Expand All @@ -52,16 +65,7 @@ public Task SetSourceAsync(Uri sourceUri)

#if !(__WASM__ || __ANDROID__ || __IOS__ || __MACOS__)

private void Update()
{
}

public void Update(AnimatedVisualPlayer player)
{
throw new NotImplementedException();
}

public void Play(bool looped)
public void Play(double fromProgress, double toProgress, bool looped)
{
throw new NotImplementedException();
}
Expand Down Expand Up @@ -100,11 +104,51 @@ public Size Measure(Size availableSize)
{
throw new NotImplementedException();
}

private readonly Size CompositionSize = default;
#endif
public void Update(AnimatedVisualPlayer player)
{
_player = player;
if (_player != null)
{
InnerUpdate();
}
}

partial void InnerUpdate();

private void SetIsPlaying(bool isPlaying)
private void SetIsPlaying(bool isPlaying) => _player?.SetValue(AnimatedVisualPlayer.IsPlayingProperty, isPlaying);

Size IAnimatedVisualSource.Measure(Size availableSize)
{
_player?.SetValue(AnimatedVisualPlayer.IsPlayingProperty, isPlaying);
var compositionSize = CompositionSize;
var stretch = _player.Stretch;

if (stretch == Stretch.None)
{
return compositionSize;
}

var availableWidth = availableSize.Width;
var availableHeight = availableSize.Height;

if (double.IsInfinity(availableWidth))
{
if (double.IsInfinity(availableHeight))
{
return compositionSize;
}

return new Size(availableHeight * compositionSize.Width / compositionSize.Height, availableHeight);
}

if (double.IsInfinity(availableHeight))
{
return new Size(availableWidth, availableWidth * compositionSize.Height / compositionSize.Width);
}

return availableSize;
}
}
}
58 changes: 34 additions & 24 deletions src/AddIns/Uno.UI.Lottie/LottieVisualSource.iOSmacOS.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using Windows.Foundation;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Airbnb.Lottie;

using Uno.UI;
#if __IOS__
using _ViewContentMode = UIKit.UIViewContentMode;
#else
Expand All @@ -18,18 +19,10 @@ partial class LottieVisualSource
public bool UseHardwareAcceleration { get; set; } = true;

private string _lastPath = "";
private AnimatedVisualPlayer _player;

private void Update()
{
if (_player != null)
{
Update(_player);
}
}

public void Update(AnimatedVisualPlayer player)
partial void InnerUpdate()
{
var player = _player;
if (_animation == null)
{
_animation = new LOTAnimationView();
Expand All @@ -53,9 +46,13 @@ void SetProperties()
_animation.SetAnimationNamed(path);
_lastPath = path;

// Force layout to recalculate
player.InvalidateMeasure();
player.InvalidateArrange();

if (player.AutoPlay)
{
Play(true);
Play(0, 1, true);
}
}

Expand Down Expand Up @@ -88,15 +85,33 @@ void SetProperties()

_animation.AnimationSpeed = (nfloat)player.PlaybackRate;
}

_player = player;
}

public void Play(bool looped)
public void Play(double fromProgress, double toProgress, bool looped)
{
_animation.LoopAnimation = looped;
_animation.Play();
SetIsPlaying(true);
if (_animation != null)
{
if (_animation.IsAnimationPlaying)
{
_animation.Stop();
}

_animation.LoopAnimation = looped;

void Start()
{
_animation.PlayFromProgress((nfloat)fromProgress, (nfloat)toProgress, isFinished =>
{
if (looped && isFinished)
{
Start();
}
});
}

Start();
SetIsPlaying(true);
}
}

public void Stop()
Expand Down Expand Up @@ -141,11 +156,6 @@ public void Unload()
}
}

Size IAnimatedVisualSource.Measure(Size availableSize)
{


return availableSize;
}
private Size CompositionSize => _animation.IntrinsicContentSize;
}
}
Loading

0 comments on commit c0887fb

Please sign in to comment.