NamedIndex.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. using System;
  3. using System.Runtime.CompilerServices;
  4. using UnityEngine;
  5. namespace Animancer.TransitionLibraries
  6. {
  7. /// <summary>[<see cref="SerializableAttribute"/>] A <see cref="StringAsset"/> and <see cref="int"/> pair.</summary>
  8. /// <remarks>
  9. /// <strong>Documentation:</strong>
  10. /// <see href="https://kybernetik.com.au/animancer/docs/manual/transitions/libraries">
  11. /// Transition Libraries</see>
  12. /// </remarks>
  13. /// https://kybernetik.com.au/animancer/api/Animancer.TransitionLibraries/NamedIndex
  14. [Serializable]
  15. public struct NamedIndex :
  16. IComparable<NamedIndex>,
  17. IEquatable<NamedIndex>
  18. {
  19. /************************************************************************************************************************/
  20. [SerializeField]
  21. private StringAsset _Name;
  22. /// <summary>The name.</summary>
  23. public readonly StringAsset Name
  24. => _Name;
  25. /************************************************************************************************************************/
  26. [SerializeField]
  27. private int _Index;
  28. /// <summary>The index.</summary>
  29. public readonly int Index
  30. => _Index;
  31. /************************************************************************************************************************/
  32. /// <summary>Creates a new <see cref="NamedIndex"/>.</summary>
  33. public NamedIndex(StringAsset name, int index)
  34. {
  35. _Name = name;
  36. _Index = index;
  37. }
  38. /************************************************************************************************************************/
  39. /// <summary>Creates a new <see cref="NamedIndex"/>.</summary>
  40. public readonly NamedIndex With(StringAsset name)
  41. => new(name, _Index);
  42. /// <summary>Creates a new <see cref="NamedIndex"/>.</summary>
  43. public readonly NamedIndex With(int index)
  44. => new(_Name, index);
  45. /************************************************************************************************************************/
  46. /// <summary>Describes this value.</summary>
  47. public override readonly string ToString()
  48. => $"[{_Index}]{_Name}";
  49. /************************************************************************************************************************/
  50. #region Equality
  51. /************************************************************************************************************************/
  52. /// <summary>Compares the <see cref="Index"/> then <see cref="Name"/>.</summary>
  53. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  54. public readonly int CompareTo(NamedIndex other)
  55. {
  56. var result = _Index.CompareTo(other._Index);
  57. if (result != 0)
  58. return result;
  59. else
  60. return StringAsset.Compare(_Name, other._Name);
  61. }
  62. /************************************************************************************************************************/
  63. /// <summary>Are all fields in this object equal to the equivalent in `obj`?</summary>
  64. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  65. public override readonly bool Equals(object obj)
  66. => obj is NamedIndex value
  67. && Equals(value);
  68. /// <summary>Are all fields in this object equal to the equivalent fields in `other`?</summary>
  69. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  70. public readonly bool Equals(NamedIndex other)
  71. => _Index == other._Index
  72. && _Name == other._Name;
  73. /// <summary>Are all fields in `a` equal to the equivalent fields in `b`?</summary>
  74. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  75. public static bool operator ==(NamedIndex a, NamedIndex b)
  76. => a.Equals(b);
  77. /// <summary>Are any fields in `a` not equal to the equivalent fields in `b`?</summary>
  78. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  79. public static bool operator !=(NamedIndex a, NamedIndex b)
  80. => !(a == b);
  81. /************************************************************************************************************************/
  82. /// <summary>Returns a hash code based on the values of this object's fields.</summary>
  83. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  84. public override readonly int GetHashCode()
  85. => AnimancerUtilities.Hash(-871379578,
  86. _Index.SafeGetHashCode(),
  87. _Name.SafeGetHashCode());
  88. /************************************************************************************************************************/
  89. #endregion
  90. /************************************************************************************************************************/
  91. }
  92. }