A game about forced loneliness, made by TACStudios
1using System;
2
3namespace UnityEngine.InputSystem
4{
5 /// <summary>
6 /// A serializable property type that can either reference an action externally defined
7 /// in an <see cref="InputActionAsset"/> or define a new action directly on the property.
8 /// </summary>
9 /// <remarks>
10 /// This struct is meant to be used for serialized fields in <c>MonoBehaviour</c> and
11 /// <c>ScriptableObject</c> classes. It has a custom property drawer attached to it
12 /// that allows to switch between using the property as a reference and using it
13 /// to define an action in place.
14 ///
15 /// <example>
16 /// <code>
17 /// public class MyBehavior : MonoBehaviour
18 /// {
19 /// // This can be edited in the inspector to either reference an existing
20 /// // action or to define an action directly on the component.
21 /// public InputActionProperty myAction;
22 /// }
23 /// </code>
24 /// </example>
25 /// </remarks>
26 /// <seealso cref="InputAction"/>
27 /// <seealso cref="InputActionReference"/>
28 [Serializable]
29 public struct InputActionProperty : IEquatable<InputActionProperty>, IEquatable<InputAction>, IEquatable<InputActionReference>
30 {
31 /// <summary>
32 /// The effective action held on to by the property.
33 /// </summary>
34 /// <value>The effective action object contained in the property.</value>
35 /// <remarks>
36 /// This property will return <c>null</c> if the property is using a <see cref="reference"/> and
37 /// the referenced action cannot be found. Also, it will be <c>null</c> if the property
38 /// has been manually initialized with a <c>null</c> <see cref="InputAction"/> using
39 /// <see cref="InputActionProperty(InputAction)"/>.
40 /// </remarks>
41 public InputAction action => m_UseReference ? m_Reference != null ? m_Reference.action : null : m_Action;
42
43 /// <summary>
44 /// If the property is set to use a reference to the action, this property returns
45 /// the reference. Otherwise it returns <c>null</c>.
46 /// </summary>
47 /// <value>Reference to external input action, if defined.</value>
48 public InputActionReference reference => m_UseReference ? m_Reference : null;
49
50 /// <summary>
51 /// The serialized loose action created in code serialized with this property.
52 /// </summary>
53 /// <value>The serialized action field.</value>
54 internal InputAction serializedAction => m_Action;
55
56 /// <summary>
57 /// The serialized reference to an external action.
58 /// </summary>
59 /// <value>The serialized reference field.</value>
60 internal InputActionReference serializedReference => m_Reference;
61
62 /// <summary>
63 /// Initialize the property to contain the given action.
64 /// </summary>
65 /// <param name="action">An action.</param>
66 /// <remarks>
67 /// When the struct is serialized, it will serialize the given action as part of it.
68 /// The <see cref="reference"/> property will return <c>null</c>.
69 /// </remarks>
70 public InputActionProperty(InputAction action)
71 {
72 m_UseReference = false;
73 m_Action = action;
74 m_Reference = null;
75 }
76
77 /// <summary>
78 /// Initialize the property to use the given action reference.
79 /// </summary>
80 /// <param name="reference">Reference to an <see cref="InputAction"/>.</param>
81 /// <remarks>
82 /// When the struct is serialized, it will only serialize a reference to
83 /// the given <paramref name="reference"/> object.
84 /// </remarks>
85 public InputActionProperty(InputActionReference reference)
86 {
87 m_UseReference = true;
88 m_Action = null;
89 m_Reference = reference;
90 }
91
92 /// <summary>
93 /// Compare two action properties to see whether they refer to the same action.
94 /// </summary>
95 /// <param name="other">Another action property.</param>
96 /// <returns>True if both properties refer to the same action.</returns>
97 public bool Equals(InputActionProperty other)
98 {
99 return m_Reference == other.m_Reference &&
100 m_UseReference == other.m_UseReference &&
101 m_Action == other.m_Action;
102 }
103
104 /// <summary>
105 /// Check whether the property refers to the same action.
106 /// </summary>
107 /// <param name="other">An action.</param>
108 /// <returns>True if <see cref="action"/> is the same as <paramref name="other"/>.</returns>
109 public bool Equals(InputAction other)
110 {
111 return ReferenceEquals(action, other);
112 }
113
114 /// <summary>
115 /// Check whether the property references the same action.
116 /// </summary>
117 /// <param name="other">An action reference.</param>
118 /// <returns>True if the property and <paramref name="other"/> reference the same action.</returns>
119 public bool Equals(InputActionReference other)
120 {
121 return m_Reference == other;
122 }
123
124 /// <summary>
125 /// Check whether the given object is an InputActionProperty referencing the same action.
126 /// </summary>
127 /// <param name="obj">An object or <c>null</c>.</param>
128 /// <returns>True if the given <paramref name="obj"/> is an InputActionProperty equivalent to this one.</returns>
129 /// <seealso cref="Equals(InputActionProperty)"/>
130 public override bool Equals(object obj)
131 {
132 if (m_UseReference)
133 return Equals(obj as InputActionReference);
134 return Equals(obj as InputAction);
135 }
136
137 /// <summary>
138 /// Compute a hash code for the object.
139 /// </summary>
140 /// <returns>A hash code.</returns>
141 public override int GetHashCode()
142 {
143 if (m_UseReference)
144 return m_Reference != null ? m_Reference.GetHashCode() : 0;
145 return m_Action != null ? m_Action.GetHashCode() : 0;
146 }
147
148 /// <summary>
149 /// Compare the two properties for equivalence.
150 /// </summary>
151 /// <param name="left">The first property.</param>
152 /// <param name="right">The second property.</param>
153 /// <returns>True if the two action properties are equivalent.</returns>
154 /// <seealso cref="Equals(InputActionProperty)"/>
155 public static bool operator==(InputActionProperty left, InputActionProperty right)
156 {
157 return left.Equals(right);
158 }
159
160 /// <summary>
161 /// Compare the two properties for not being equivalent.
162 /// </summary>
163 /// <param name="left">The first property.</param>
164 /// <param name="right">The second property.</param>
165 /// <returns>True if the two action properties are not equivalent.</returns>
166 /// <seealso cref="Equals(InputActionProperty)"/>
167 public static bool operator!=(InputActionProperty left, InputActionProperty right)
168 {
169 return !left.Equals(right);
170 }
171
172 [SerializeField] private bool m_UseReference;
173 [SerializeField] private InputAction m_Action;
174 [SerializeField] private InputActionReference m_Reference;
175 }
176}