Skip to content

Commit

Permalink
Added play to percentage property (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
matmork authored Sep 11, 2024
2 parents 98148d6 + 03bf670 commit 56fc9ff
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Rive.Android/Rive.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageId>Rive.Android</PackageId>
<Title>Rive Android</Title>
<Description>Wrapper around the Android runtime</Description>
<VersionPrefix>1.0.4</VersionPrefix>
<VersionPrefix>1.0.5</VersionPrefix>
<PackageProjectUrl>https://github.com/matmork/rive-maui</PackageProjectUrl>
<RepositoryUrl>https://github.com/matmork/rive-maui</RepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
Expand Down
6 changes: 6 additions & 0 deletions src/Rive.Maui/Platforms/Android/RivePlayerRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Rive.Android;
using Microsoft.Maui.Controls.Handlers.Compatibility;
using Microsoft.Maui.Controls.Platform;
using Rive.Android.Core;
using View = Android.Views.View;

namespace Rive.Maui;
Expand All @@ -13,6 +14,7 @@ public partial class RivePlayerRenderer(Context context) : ViewRenderer<RivePlay
private StateListener? _stateListener;
private EventListener? _eventListener;
private string? _resourceName;
internal LinearAnimationInstance? _animation;

protected override void OnElementChanged(ElementChangedEventArgs<RivePlayer> e)
{
Expand Down Expand Up @@ -62,6 +64,9 @@ private void Destroy()
Element?.StateMachineInputs.Dispose();
_riveAnimationView?.Dispose();
_riveAnimationView = null;

_animation?.Dispose();
_animation = null;
}

protected override void DisconnectHandler(View oldPlatformView)
Expand Down Expand Up @@ -202,6 +207,7 @@ public static void MapPlay(RivePlayerRenderer handler, RivePlayer view, object?
if (!string.IsNullOrWhiteSpace(view.AnimationName))
{
handler._riveAnimationView?.Play(view.AnimationName, riveLoop, riveDirection, false, true);
handler._animation = handler._riveAnimationView?.Animations.FirstOrDefault(x => x.Name == view.AnimationName);
return;
}

Expand Down
8 changes: 8 additions & 0 deletions src/Rive.Maui/Platforms/Android/StateListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ internal class StateListener : Object, RiveFileController.IListener

public void NotifyAdvance(float elapsed)
{
if (RivePlayerHandlerReference.TryGetTarget(out var handler) && handler is { _animation: not null, Element.PlayToPercentage: > 0 })
{
var percentElapsed = Math.Floor(handler._animation.Time / handler._animation.EffectiveDurationInSeconds * 100);
if (percentElapsed >= handler.Element.PlayToPercentage)
{
handler._riveAnimationView?.Pause();
}
}
}

public void NotifyLoop(IPlayableInstance animation)
Expand Down
10 changes: 10 additions & 0 deletions src/Rive.Maui/Platforms/iOS/CustomRiveView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ private void Tick()
}
else
{
if (Control.TryGetTarget(out var control) && control.PlayToPercentage > 0 && _riveAnimation != null)
{
var percentElapsed = Math.Floor(_riveAnimation.Time / _riveAnimation.EffectiveDurationInSeconds() * 100);
if (percentElapsed >= control.PlayToPercentage)
{
Pause();
return;
}
}

_riveAnimation?.AdvanceBy(elapsedTime);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rive.Maui/Rive.Maui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageId>Rive.Maui</PackageId>
<Title>Rive Maui</Title>
<Description>Rive animations in Maui using iOS/Android runtimes</Description>
<VersionPrefix>1.0.4</VersionPrefix>
<VersionPrefix>1.0.5</VersionPrefix>
<PackageProjectUrl>https://github.com/matmork/rive-maui</PackageProjectUrl>
<RepositoryUrl>https://github.com/matmork/rive-maui</RepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
Expand Down
12 changes: 12 additions & 0 deletions src/Rive.Maui/RivePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ public event EventHandler<string> EventReceived
typeof(RivePlayer)
);

public static readonly BindableProperty PlayToPercentageProperty = BindableProperty.Create(
nameof(PlayToPercentage),
typeof(double),
typeof(RivePlayer)
);

public string? ArtboardName
{
get => (string?)GetValue(ArtboardNameProperty);
Expand Down Expand Up @@ -186,6 +192,12 @@ public List<DynamicAsset>? DynamicAssets
set => SetValue(DynamicAssetsProperty, value);
}

public double PlayToPercentage
{
get => (double)GetValue(PlayToPercentageProperty);
set => SetValue(PlayToPercentageProperty, value);
}

public ICommand PlayCommand
=> new Command(Play);

Expand Down
2 changes: 1 addition & 1 deletion src/Rive.iOS/Rive.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageId>Rive.iOS</PackageId>
<Title>Rive iOS</Title>
<Description>Wrapper around the iOS runtime</Description>
<VersionPrefix>1.0.4</VersionPrefix>
<VersionPrefix>1.0.5</VersionPrefix>
<PackageProjectUrl>https://github.com/matmork/rive-maui</PackageProjectUrl>
<RepositoryUrl>https://github.com/matmork/rive-maui</RepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
Expand Down

0 comments on commit 56fc9ff

Please sign in to comment.