Skip to content

Commit 6c6e698

Browse files
committed
Code/Documentation cleanup
1 parent ecd285f commit 6c6e698

4 files changed

Lines changed: 306 additions & 205 deletions

File tree

Runtime/Axis.cs

Lines changed: 75 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class Axis
4545
private float neutralTime = 0f;
4646
private float negativeTime = 0f;
4747
private bool set = false;
48-
private readonly InputAction action;
48+
private InputAction action;
4949

5050
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
5151

@@ -65,9 +65,7 @@ public Axis(InputAction action)
6565
throw new System.ArgumentNullException("action");
6666
if (action.expectedControlType != "Axis")
6767
throw new System.ArgumentException("InputAction " + action.name + " does not have an expected Axis control type.");
68-
action.performed += ActionPerformed;
69-
action.canceled += ActionCanceled;
70-
this.action = action;
68+
SetInputAction(action);
7169
}
7270

7371
~Axis()
@@ -82,155 +80,171 @@ public Axis(InputAction action)
8280
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
8381

8482
/// <summary>
85-
/// Set <see cref="Value"/>.
83+
/// Sets <see cref="Value"/>.
8684
/// <para>The <see cref="Axis"/> logs the <see cref="Time.frameCount"/> and <see cref="Time.unscaledTime"/> whenever <see cref="Value"/> is set.</para>
8785
/// </summary>
88-
/// <param name="value">The value to set <see cref="Value"/> to.</param>
86+
/// <param name="value">The value to set.</param>
8987
public void SetValue(float value)
9088
{
9189
SetValue(value, true);
9290
}
9391

9492
/// <summary>
95-
/// Set the <see cref="Axis"/> value.
93+
/// Sets <see cref="Axis"/> value.
9694
/// <para>The <see cref="Axis"/> logs the <see cref="Time.frameCount"/> and <see cref="Time.unscaledTime"/> whenever <see cref="Value"/> is set.</para>
9795
/// <para>It is strongly recommended not to set the <paramref name="invoke"/> parameter to false, unless you know what you are doing.
9896
/// Other classes such as <see cref="TwinAxes"/> use the <see cref="OnValueChanged"/> event to monitor their instances of <see cref="Axis"/> for value changes.</para>
9997
/// </summary>
100-
/// <param name="value">The value to set <see cref="Value"/> to.</param>
101-
/// <param name="invoke">Invokes <see cref="OnValueChanged"/> if true.</param>
98+
/// <param name="value">The value to set.</param>
99+
/// <param name="invoke">Invokes <see cref="OnValueChanged"/> if set to true.</param>
102100
public void SetValue(float value, bool invoke = true)
103101
{
104-
if (value > 0f)
102+
if(value != Value)
105103
{
106-
if (!Positive)
104+
if (value > 0f)
107105
{
108-
positiveFrame = Time.frameCount;
109-
positiveTime = Time.unscaledTime;
106+
if (!Positive)
107+
{
108+
positiveFrame = Time.frameCount;
109+
positiveTime = Time.unscaledTime;
110+
}
110111
}
111-
}
112-
else if (value < 0f)
113-
{
114-
if (!Negative)
112+
else if (value < 0f)
115113
{
116-
negativeFrame = Time.frameCount;
117-
negativeTime = Time.unscaledTime;
114+
if (!Negative)
115+
{
116+
negativeFrame = Time.frameCount;
117+
negativeTime = Time.unscaledTime;
118+
}
118119
}
119-
}
120-
else
121-
{
122-
if (!Neutral)
120+
else
123121
{
124-
neutralFrame = Time.frameCount;
125-
neutralTime = Time.unscaledTime;
122+
if (!Neutral)
123+
{
124+
neutralFrame = Time.frameCount;
125+
neutralTime = Time.unscaledTime;
126+
}
126127
}
128+
Value = value;
129+
if (!set)
130+
set = true;
131+
if (invoke)
132+
OnValueChanged?.Invoke(Value);
127133
}
128-
Value = value;
129-
if (!set)
130-
set = true;
131-
if (invoke)
132-
OnValueChanged?.Invoke(Value);
133134
}
134135

135136
/// <summary>
136-
/// Set <see cref="Value"/> to neutral (0f).
137+
/// Sets <see cref="Value"/> to "neutral" (0f).
137138
/// <para>The <see cref="Axis"/> logs the <see cref="Time.frameCount"/> and <see cref="Time.unscaledTime"/> whenever <see cref="Value"/> is set.</para>
138139
/// </summary>
139140
public void SetNeutral()
140141
{
141142
SetValue(0f);
142143
}
143144

145+
/// <summary>
146+
/// Tie the <see cref="Axis"/> to an <see cref="InputAction"/>.
147+
/// </summary>
148+
/// <param name="action">The <see cref="InputAction"/> to attach to the <see cref="Axis"/>.</param>
149+
public void RegisterInputAction(InputAction action)
150+
{
151+
if (action == null)
152+
throw new System.ArgumentNullException("action");
153+
if (action.expectedControlType != "Axis")
154+
throw new System.ArgumentException("InputAction " + action.name + " does not have an expected Axis control type.");
155+
SetInputAction(action);
156+
}
157+
144158
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
145159

146160
/// <summary>
147161
/// Returns for how many frames <see cref="Value"/> has been positive, if it currently is. Returns -1 otherwise.
148162
/// </summary>
149163
/// <returns>
150164
/// How many frames <see cref="Value"/> has been positive, if it currently is. If <see cref="Value"/> is currently not positive, this method will return -1.
151-
/// This method will also always return -1 if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.
165+
/// <para>This method will also always return -1 if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.</para>
152166
/// </returns>
153167
public int PositiveDurationFrames()
154168
{
155169
if (set && Positive && positiveFrame >= negativeFrame && positiveFrame >= neutralFrame)
156170
return Time.frameCount - positiveFrame;
157-
158-
else return -1;
171+
else
172+
return -1;
159173
}
160174

161175
/// <summary>
162176
/// Returns for how many seconds <see cref="Value"/> has been positive, if it currently is. Returns -1f otherwise.
163177
/// </summary>
164178
/// <returns>
165179
/// How many seconds <see cref="Value"/> has been positive, if it currently is. If <see cref="Value"/> is currently not positive, this method will return -1f.
166-
/// This method will also always return -1f if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.
180+
/// <para>This method will also always return -1f if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.</para>
167181
/// </returns>
168182
public float PositiveDurationTime()
169183
{
170184
if (set && Positive && positiveTime >= negativeTime && positiveTime >= neutralTime)
171185
return Time.unscaledTime - positiveTime;
172-
173-
else return -1f;
186+
else
187+
return -1f;
174188
}
175189

176190
/// <summary>
177191
/// Returns for how many frames <see cref="Value"/> has been neutral (0f), if it currently is. Returns -1 otherwise.
178192
/// </summary>
179193
/// <returns>
180194
/// How many frames <see cref="Value"/> has been neutral, if it currently is. If <see cref="Value"/> is currently not neutral, this method will return -1.
181-
/// This method will also always return -1 if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.
195+
/// <para>This method will also always return -1 if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.</para>
182196
/// </returns>
183197
public int NeutralDurationFrames()
184198
{
185199
if (set && Neutral && neutralFrame >= negativeFrame && neutralFrame >= positiveFrame)
186200
return Time.frameCount - neutralFrame;
187-
188-
else return -1;
201+
else
202+
return -1;
189203
}
190204

191205
/// <summary>
192206
/// Returns for how many seconds <see cref="Value"/> has been neutral (0f), if it currently is. Returns -1f otherwise.
193207
/// </summary>
194208
/// <returns>
195209
/// How many seconds <see cref="Value"/> has been neutral, if it currently is. If <see cref="Value"/> is currently not neutral, this method will return -1f.
196-
/// This method will also always return -1f if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.
210+
/// <para>This method will also always return -1f if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.</para>
197211
/// </returns>
198212
public float NeutralDurationTime()
199213
{
200214
if (set && Neutral && neutralTime >= negativeTime && neutralTime >= positiveTime)
201215
return Time.unscaledTime - neutralTime;
202-
203-
else return -1f;
216+
else
217+
return -1f;
204218
}
205219

206220
/// <summary>
207221
/// Returns for how many frames <see cref="Value"/> has been negative, if it currently is. Returns -1 otherwise.
208222
/// </summary>
209223
/// <returns>
210224
/// How many frames <see cref="Value"/> has been negative, if it currently is. If <see cref="Value"/> is currently not negative, this method will return -1.
211-
/// This method will also always return -1 if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.
225+
/// <para>This method will also always return -1 if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.</para>
212226
/// </returns>
213227
public int NegativeDurationFrames()
214228
{
215229
if (set && Negative && negativeFrame >= positiveFrame && negativeFrame >= neutralFrame)
216230
return Time.frameCount - negativeFrame;
217-
218-
else return -1;
231+
else
232+
return -1;
219233
}
220234

221235
/// <summary>
222236
/// Returns for how many seconds <see cref="Value"/> has been negative, if it currently is. Returns -1f otherwise.
223237
/// </summary>
224238
/// <returns>
225239
/// How many seconds <see cref="Value"/> has been negative, if it currently is. If <see cref="Value"/> is currently not negative, this method will return -1f.
226-
/// This method will also always return -1f if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.
240+
/// <para>This method will also always return -1f if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.</para>
227241
/// </returns>
228242
public float NegativeDurationTime()
229243
{
230244
if (set && Negative && negativeTime >= positiveTime && negativeTime >= neutralTime)
231245
return Time.unscaledTime - neutralTime;
232-
233-
else return -1f;
246+
else
247+
return -1f;
234248
}
235249

236250
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -244,5 +258,17 @@ private void ActionCanceled(InputAction.CallbackContext context)
244258
{
245259
SetNeutral();
246260
}
261+
262+
private void SetInputAction(InputAction action)
263+
{
264+
if (this.action != null)
265+
{
266+
this.action.performed -= ActionPerformed;
267+
this.action.canceled -= ActionCanceled;
268+
}
269+
action.performed += ActionPerformed;
270+
action.canceled += ActionCanceled;
271+
this.action = action;
272+
}
247273
}
248274
}

0 commit comments

Comments
 (0)