using System;
using System.Runtime.InteropServices;
using Rive.Utils;
namespace Rive
{
///
/// A view model instance property for artboard properties.
///
public sealed class ViewModelInstanceArtboardProperty : ViewModelInstancePrimitiveProperty
{
public ViewModelInstanceArtboardProperty(IntPtr instanceValuePtr, ViewModelInstance instance) : base(instanceValuePtr, instance)
{
}
///
/// Sets the artboard value for this property.
///
public BindableArtboard Value
{
set
{
ThrowIfOwnerDisposed();
SetArtboardInternal(value);
}
}
///
/// Raised when the artboard property is changed in the Rive graphic.
///
public event Action OnValueChanged
{
add => AddPropertyCallback(value, ref m_onValueChanged);
remove => RemovePropertyCallback(value, ref m_onValueChanged);
}
private Action m_onValueChanged;
///
/// Sets the artboard for the property.
///
private void SetArtboardInternal(BindableArtboard artboard)
{
if (artboard != null && artboard.NativeBindableArtboard == IntPtr.Zero)
{
DebugLogger.Instance.LogError("Trying to assign an invalid artboard.");
return;
}
bool wasSuccess = setViewModelInstanceArtboardValue(
InstancePropertyPtr,
artboard?.NativeBindableArtboard ?? IntPtr.Zero,
artboard?.ViewModelInstanceHandle ?? ViewModelInstanceSafeHandle.Null);
if (!wasSuccess)
{
DebugLogger.Instance.LogError("Failed to set artboard.");
}
}
internal override void RaiseChangedEvent()
{
m_onValueChanged?.Invoke();
}
internal override void ClearAllCallbacks()
{
m_onValueChanged = null;
base.ClearAllCallbacks();
}
internal override void ClearDelegatesOnly()
{
m_onValueChanged = null;
}
[DllImport(NativeLibrary.name)]
private static extern bool setViewModelInstanceArtboardValue(
IntPtr instanceProperty,
IntPtr artboard,
ViewModelInstanceSafeHandle viewModelInstance);
}
}