// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
using System;
namespace Animancer
{
/// A wrapper for managing a in an .
/// This type is mostly intended for internal use within Mixers.
/// https://kybernetik.com.au/animancer/api/Animancer/NodeParameter_1
public struct NodeParameter
{
/************************************************************************************************************************/
/// The node that owns this parameter.
public AnimancerNode Node { get; private set; }
/// The callback to invoke when the parameter changes.
public event Action OnParameterChanged;
/************************************************************************************************************************/
/// Has this been constructed properly?
public readonly bool IsInitialized
=> Node != null;
/************************************************************************************************************************/
private StringReference _Key;
///
/// This will be used as a key in the
/// so any changes to that parameter will invoke .
///
public StringReference Key
{
readonly get => _Key;
set
{
if (_Key.EqualsWhereEmptyIsNull(value))
return;
UnBind();
_Key = value;
Bind();
}
}
/************************************************************************************************************************/
/// Sets the and returns true if needs to be called.
public bool SetKeyCheckNeedsInitialize(StringReference key)
{
if (_Key.EqualsWhereEmptyIsNull(key))
return false;
if (IsInitialized)
{
UnBind();
_Key = key;
Bind();
return false;
}
else
{
_Key = key;
return true;
}
}
/// Initializes and binds the parameter.
public void Initialize(AnimancerNode node, Action onParameterChanged)
{
Node = node;
OnParameterChanged = onParameterChanged;
Bind();
}
/************************************************************************************************************************/
/// Registers to the .
public readonly void Bind()
{
if (Node.Graph != null && !_Key.IsNullOrEmpty())
Node.Graph.Parameters.AddOnValueChanged(_Key, OnParameterChanged, true);
}
/// Registers to the if .
public readonly void BindIfInitialized()
{
if (IsInitialized)
Bind();
}
/************************************************************************************************************************/
/// Unregisters from the .
public readonly void UnBind()
{
if (Node.Graph != null && !_Key.IsNullOrEmpty())
Node.Graph.Parameters.RemoveOnValueChanged(_Key, OnParameterChanged);
}
/// Unregisters from the if .
public readonly void UnBindIfInitialized()
{
if (IsInitialized)
UnBind();
}
/************************************************************************************************************************/
}
}