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