NodeParameter.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. using System;
  3. namespace Animancer
  4. {
  5. /// <summary>A wrapper for managing a <see cref="Parameter{T}"/> in an <see cref="AnimancerNode"/>.</summary>
  6. /// <remarks>This type is mostly intended for internal use within Mixers.</remarks>
  7. /// https://kybernetik.com.au/animancer/api/Animancer/NodeParameter_1
  8. public struct NodeParameter<T>
  9. {
  10. /************************************************************************************************************************/
  11. /// <summary>The node that owns this parameter.</summary>
  12. public AnimancerNode Node { get; private set; }
  13. /// <summary>The callback to invoke when the parameter changes.</summary>
  14. public event Action<T> OnParameterChanged;
  15. /************************************************************************************************************************/
  16. /// <summary>Has this <see cref="NodeParameter{T}"/> been constructed properly?</summary>
  17. public readonly bool IsInitialized
  18. => Node != null;
  19. /************************************************************************************************************************/
  20. private StringReference _Key;
  21. /// <summary>
  22. /// This will be used as a key in the <see cref="ParameterDictionary"/>
  23. /// so any changes to that parameter will invoke <see cref="OnParameterChanged"/>.
  24. /// </summary>
  25. public StringReference Key
  26. {
  27. readonly get => _Key;
  28. set
  29. {
  30. if (_Key.EqualsWhereEmptyIsNull(value))
  31. return;
  32. UnBind();
  33. _Key = value;
  34. Bind();
  35. }
  36. }
  37. /************************************************************************************************************************/
  38. /// <summary>Sets the <see cref="Key"/> and returns <c>true</c> if <see cref="Initialize"/> needs to be called.</summary>
  39. public bool SetKeyCheckNeedsInitialize(StringReference key)
  40. {
  41. if (_Key.EqualsWhereEmptyIsNull(key))
  42. return false;
  43. if (IsInitialized)
  44. {
  45. UnBind();
  46. _Key = key;
  47. Bind();
  48. return false;
  49. }
  50. else
  51. {
  52. _Key = key;
  53. return true;
  54. }
  55. }
  56. /// <summary>Initializes and binds the parameter.</summary>
  57. public void Initialize(AnimancerNode node, Action<T> onParameterChanged)
  58. {
  59. Node = node;
  60. OnParameterChanged = onParameterChanged;
  61. Bind();
  62. }
  63. /************************************************************************************************************************/
  64. /// <summary>Registers to the <see cref="AnimancerGraph.Parameters"/>.</summary>
  65. public readonly void Bind()
  66. {
  67. if (Node.Graph != null && !_Key.IsNullOrEmpty())
  68. Node.Graph.Parameters.AddOnValueChanged(_Key, OnParameterChanged, true);
  69. }
  70. /// <summary>Registers to the <see cref="AnimancerGraph.Parameters"/> if <see cref="IsInitialized"/>.</summary>
  71. public readonly void BindIfInitialized()
  72. {
  73. if (IsInitialized)
  74. Bind();
  75. }
  76. /************************************************************************************************************************/
  77. /// <summary>Unregisters from the <see cref="AnimancerGraph.Parameters"/>.</summary>
  78. public readonly void UnBind()
  79. {
  80. if (Node.Graph != null && !_Key.IsNullOrEmpty())
  81. Node.Graph.Parameters.RemoveOnValueChanged(_Key, OnParameterChanged);
  82. }
  83. /// <summary>Unregisters from the <see cref="AnimancerGraph.Parameters"/> if <see cref="IsInitialized"/>.</summary>
  84. public readonly void UnBindIfInitialized()
  85. {
  86. if (IsInitialized)
  87. UnBind();
  88. }
  89. /************************************************************************************************************************/
  90. }
  91. }