Skip to content

Commit

Permalink
Added support for dynamic assets (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
matmork authored Sep 6, 2024
2 parents 511f344 + c779d41 commit 98148d6
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 10 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.3</VersionPrefix>
<VersionPrefix>1.0.4</VersionPrefix>
<PackageProjectUrl>https://github.com/matmork/rive-maui</PackageProjectUrl>
<RepositoryUrl>https://github.com/matmork/rive-maui</RepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
Expand Down
19 changes: 19 additions & 0 deletions src/Rive.Maui/DynamicAsseet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Rive.Maui;

public class DynamicAsset
{
public DynamicAsset()
{
}

public DynamicAsset(string name, string filename, string extension)
{
Name = name;
Filename = filename;
Extension = extension;
}

public required string Name { get; set; }
public required string Filename { get; set; }
public required string Extension { get; set; }
}
49 changes: 49 additions & 0 deletions src/Rive.Maui/Platforms/Android/AssetLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Android.Content;
using Rive.Android.Core;

namespace Rive.Maui;

public class AssetLoader : FileAssetLoader
{
private readonly Context _context;
private readonly List<DynamicAsset> _dynamicAssets;

public AssetLoader(Context context, List<DynamicAsset> dynamicAssets)
{
_context = context;
_dynamicAssets = dynamicAssets;
}

public override bool LoadContents(FileAsset asset, byte[] inBandBytes)
{
var dynamicAsset = _dynamicAssets.FirstOrDefault(x => string.Equals(x.Name, asset.Name, StringComparison.OrdinalIgnoreCase));
if (dynamicAsset == null)
return false;

var resourceIdentifier = _context.Resources?.GetIdentifier(dynamicAsset.Filename, "drawable", _context.PackageName) ?? 0;
if (resourceIdentifier == 0)
return false;

using var stream = _context.Resources?.OpenRawResource(resourceIdentifier);
if (stream == null)
return false;

using var memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);

switch (asset)
{
case ImageAsset imageAsset:
imageAsset.Decode(memoryStream.ToArray());
return true;
case FontAsset fontAsset:
fontAsset.Decode(memoryStream.ToArray());
return true;
case AudioAsset audioAsset:
audioAsset.Decode(memoryStream.ToArray());
return true;
default:
return false;
}
}
}
5 changes: 5 additions & 0 deletions src/Rive.Maui/Platforms/Android/RivePlayerRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ private void CreatePlatformView()
_tmpView = new View(Context);
_riveAnimationView = new RiveAnimationView(Context, null);

if (Element.DynamicAssets?.Count > 0)
{
_riveAnimationView.SetAssetLoader(new AssetLoader(Context, Element.DynamicAssets));
}

_riveAnimationView.LayoutParameters = new LayoutParams(
LayoutParams.MatchParent,
LayoutParams.MatchParent
Expand Down
57 changes: 51 additions & 6 deletions src/Rive.Maui/Platforms/iOS/CustomRiveView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,62 @@ public void SetRiveResource(string? resourceName)

_resourceName = resourceName;

var resourceUrl = NSBundle.MainBundle.GetUrlForResource(resourceName, ".riv");
if (resourceUrl == null)
return;

var resourceData = NSData.FromUrl(resourceUrl);
var resourceData = GetNSDataForResource(resourceName, ".riv");
if (resourceData == null)
return;

_riveFile = new RiveFile(resourceData, true, out _);
if (Control.TryGetTarget(out var rivePlayer) && rivePlayer.DynamicAssets?.Count > 0)
{
var loader = new LoadAsset((asset, _, factory) =>
{
var dynamicAsset =
rivePlayer.DynamicAssets.FirstOrDefault(x => string.Equals(x.Name, asset.Name, StringComparison.OrdinalIgnoreCase));
if (dynamicAsset == null)
return false;

var newData = GetNSDataForResource(dynamicAsset.Filename, dynamicAsset.Extension);
if (newData == null)
return false;

switch (asset)
{
case RiveImageAsset imageAsset:
{
var riveRenderImage = factory.DecodeImage(newData);
imageAsset.RenderImage(riveRenderImage);
return true;
}
case RiveFontAsset fontAsset:
{
var riveFont = factory.DecodeFont(newData);
fontAsset.Font(riveFont);
return true;
}
case RiveAudioAsset audioAsset:
{
var riveAudio = factory.DecodeAudio(newData);
audioAsset.Audio(riveAudio);
return true;
}
default:
return false;
}
});

_riveFile = new RiveFile(resourceData, true, loader, out _);
}
else
{
_riveFile = new RiveFile(resourceData, true, out _);
}

UpdateAnimation();

NSData? GetNSDataForResource(string name, string extension)
{
var resourceUrl = NSBundle.MainBundle.GetUrlForResource(name, extension);
return resourceUrl == null ? null : NSData.FromUrl(resourceUrl);
}
}

public void UpdateAnimation()
Expand Down
2 changes: 1 addition & 1 deletion src/Rive.Maui/Platforms/iOS/RivePlayerRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ private void CreatePlatformView(RivePlayer control)
Direction = control.Direction.AsRive()
};

platformView.SetRiveResource(control.ResourceName);
platformView.Control.SetTarget(control);
platformView.Frame = control.Bounds;
platformView.SetRiveResource(control.ResourceName);

_riveAnimationView = platformView;
SetNativeControl(_riveAnimationView);
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.3</VersionPrefix>
<VersionPrefix>1.0.4</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 @@ -102,6 +102,12 @@ public event EventHandler<string> EventReceived
typeof(RivePlayer)
);

public static readonly BindableProperty DynamicAssetsProperty = BindableProperty.Create(
nameof(DynamicAssets),
typeof(List<DynamicAsset>),
typeof(RivePlayer)
);

public string? ArtboardName
{
get => (string?)GetValue(ArtboardNameProperty);
Expand Down Expand Up @@ -174,6 +180,12 @@ public StateMachineInputCollection StateMachineInputs
set => SetValue(StateMachineInputsProperty, value);
}

public List<DynamicAsset>? DynamicAssets
{
get => (List<DynamicAsset>?)GetValue(DynamicAssetsProperty);
set => SetValue(DynamicAssetsProperty, 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.3</VersionPrefix>
<VersionPrefix>1.0.4</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 98148d6

Please sign in to comment.