No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SerializedInputBinding.cs 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #if UNITY_EDITOR && UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
  2. using System;
  3. using System.Linq;
  4. using UnityEditor;
  5. namespace UnityEngine.InputSystem.Editor
  6. {
  7. /// <summary>
  8. /// A read-only view of the data in a SerializedProperty representing an InputBinding.
  9. /// <remarks>
  10. /// After construction this class loses all connection to the original SerializedProperty. You cannot
  11. /// use it for edit operations.
  12. /// </remarks>
  13. /// </summary>
  14. internal readonly struct SerializedInputBinding
  15. {
  16. public SerializedInputBinding(SerializedProperty serializedProperty)
  17. {
  18. wrappedProperty = serializedProperty ?? throw new ArgumentNullException(nameof(serializedProperty));
  19. id = serializedProperty.FindPropertyRelative("m_Id").stringValue;
  20. name = serializedProperty.FindPropertyRelative("m_Name").stringValue;
  21. path = serializedProperty.FindPropertyRelative("m_Path").stringValue;
  22. interactions = serializedProperty.FindPropertyRelative("m_Interactions").stringValue;
  23. processors = serializedProperty.FindPropertyRelative("m_Processors").stringValue;
  24. action = serializedProperty.FindPropertyRelative("m_Action").stringValue;
  25. propertyPath = wrappedProperty.propertyPath;
  26. var bindingGroups = serializedProperty.FindPropertyRelative(nameof(InputBinding.m_Groups)).stringValue;
  27. controlSchemes = bindingGroups != null
  28. ? bindingGroups.Split(InputBinding.kSeparatorString, StringSplitOptions.RemoveEmptyEntries)
  29. : Array.Empty<string>();
  30. flags = (InputBinding.Flags)serializedProperty.FindPropertyRelative(nameof(InputBinding.m_Flags)).intValue;
  31. indexOfBinding = serializedProperty.GetIndexOfArrayElement();
  32. isComposite = (flags & InputBinding.Flags.Composite) == InputBinding.Flags.Composite;
  33. isPartOfComposite = (flags & InputBinding.Flags.PartOfComposite) == InputBinding.Flags.PartOfComposite;
  34. compositePath = string.Empty;
  35. if (isPartOfComposite)
  36. compositePath = GetCompositePath(serializedProperty);
  37. }
  38. public string name { get; }
  39. public string id { get; }
  40. public string path { get; }
  41. public string interactions { get; }
  42. public string processors { get; }
  43. public string action { get; }
  44. public string propertyPath { get; }
  45. public string[] controlSchemes { get; }
  46. public InputBinding.Flags flags { get; }
  47. /// <summary>
  48. /// The index of this binding in the array that it is stored in.
  49. /// </summary>
  50. public int indexOfBinding { get; }
  51. public bool isComposite { get; }
  52. public bool isPartOfComposite { get; }
  53. /// <summary>
  54. /// Get the composite path of this input binding, which must itself be a composite part.
  55. /// </summary>
  56. /// <remarks>
  57. /// The composite path of a composite part is simply the path of the composite binding that the
  58. /// part belongs to.
  59. /// </remarks>
  60. public string compositePath { get; }
  61. public SerializedProperty wrappedProperty { get; }
  62. private static string GetCompositePath(SerializedProperty serializedProperty)
  63. {
  64. var bindingArrayProperty = serializedProperty.GetArrayPropertyFromElement();
  65. var partBindingIndex = InputActionSerializationHelpers.GetIndex(bindingArrayProperty, serializedProperty);
  66. var compositeStartIndex =
  67. InputActionSerializationHelpers.GetCompositeStartIndex(bindingArrayProperty, partBindingIndex);
  68. var compositeBindingProperty = bindingArrayProperty.GetArrayElementAtIndex(compositeStartIndex);
  69. return compositeBindingProperty.FindPropertyRelative("m_Path").stringValue;
  70. }
  71. public bool Equals(SerializedInputBinding other)
  72. {
  73. return name == other.name
  74. && path == other.path
  75. && interactions == other.interactions
  76. && processors == other.processors
  77. && action == other.action
  78. && flags == other.flags
  79. && indexOfBinding == other.indexOfBinding
  80. && isComposite == other.isComposite
  81. && isPartOfComposite == other.isPartOfComposite
  82. && compositePath == other.compositePath
  83. && controlSchemes.SequenceEqual(other.controlSchemes)
  84. && propertyPath == other.propertyPath;
  85. }
  86. public override bool Equals(object obj)
  87. {
  88. return obj is SerializedInputBinding other && Equals(other);
  89. }
  90. public override int GetHashCode()
  91. {
  92. var hashCode = new HashCode();
  93. hashCode.Add(name);
  94. hashCode.Add(path);
  95. hashCode.Add(interactions);
  96. hashCode.Add(processors);
  97. hashCode.Add(action);
  98. hashCode.Add((int)flags);
  99. hashCode.Add(indexOfBinding);
  100. hashCode.Add(isComposite);
  101. hashCode.Add(isPartOfComposite);
  102. hashCode.Add(compositePath);
  103. hashCode.Add(controlSchemes);
  104. hashCode.Add(propertyPath);
  105. return hashCode.ToHashCode();
  106. }
  107. }
  108. }
  109. #endif