Skip to content

Commit 90a38fe

Browse files
committed
Added more update events to Axis and tweaked documentation
1 parent f37cd38 commit 90a38fe

2 files changed

Lines changed: 76 additions & 75 deletions

File tree

Runtime/Axis.cs

Lines changed: 75 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,42 @@ public class Axis
1515

1616
/// <summary>
1717
/// True if <see cref="Value"/> is positive (greater than 0f).\
18-
/// <para>Will always be false if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.</para>
18+
/// <para>Will always be false if <see cref="SetValue"/> has never been called.</para>
1919
/// </summary>
2020
public bool Positive => set && Value > 0f;
2121

2222
/// <summary>
2323
/// True if <see cref="Value"/> is neutral (0f).
24-
/// <para>Will always be false if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.</para>
24+
/// <para>Will always be false if <see cref="SetValue"/> has never been called.</para>
2525
/// </summary>
2626
public bool Neutral => set && Value == 0f;
2727

2828
/// <summary>
2929
/// True if <see cref="Value"/> is negative (less than 0f).
30-
/// <para>Will always be false if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.</para>
30+
/// <para>Will always be false if <see cref="SetValue"/> has never been called.</para>
3131
/// </summary>
3232
public bool Negative => set && Value < 0f;
3333

3434
/// <summary>
35-
/// Invoked when <see cref="Value"/> is set through <see cref="SetValue(float)"/> or <see cref="SetNeutral"/>.
35+
/// Invoked when <see cref="Value"/> is set..
3636
/// </summary>
3737
public event System.Action<float> OnValueChanged;
3838

39+
/// <summary>
40+
/// Invoked when <see cref="Value"/> shifts to a positive value (greater than 0f).
41+
/// </summary>
42+
public event System.Action OnPositive;
43+
44+
/// <summary>
45+
/// Invoked when <see cref="Value"/> shifts to a negative value (less than 0f).
46+
/// </summary>
47+
public event System.Action OnNegative;
48+
49+
/// <summary>
50+
/// Invoked when <see cref="Value"/> shifts to a neutral value (0f).
51+
/// </summary>
52+
public event System.Action OnNeutral;
53+
3954
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
4055

4156
private int positiveFrame = 0;
@@ -81,22 +96,13 @@ public Axis(InputAction action)
8196

8297
/// <summary>
8398
/// Sets <see cref="Value"/>.
84-
/// <para>The <see cref="Axis"/> logs the <see cref="Time.frameCount"/> and <see cref="Time.unscaledTime"/> whenever <see cref="Value"/> is set.</para>
85-
/// </summary>
86-
/// <param name="value">The value to set.</param>
87-
public void SetValue(float value)
88-
{
89-
SetValue(value, true);
90-
}
91-
92-
/// <summary>
93-
/// Sets <see cref="Axis"/> value.
94-
/// <para>The <see cref="Axis"/> logs the <see cref="Time.frameCount"/> and <see cref="Time.unscaledTime"/> whenever <see cref="Value"/> is set.</para>
95-
/// <para>It is strongly recommended not to set the <paramref name="invoke"/> parameter to false, unless you know what you are doing.
96-
/// 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>
99+
/// <para>The <see cref="Axis"/> logs the <see cref="Time.frameCount"/> and <see cref="Time.unscaledTime"/>
100+
/// whenever <see cref="Value"/> is set.</para>
97101
/// </summary>
98102
/// <param name="value">The value to set.</param>
99-
/// <param name="invoke">Invokes <see cref="OnValueChanged"/> if set to true.</param>
103+
/// <param name="invoke">Set to false to prevent the class' events from being invoked by this method.
104+
/// <para><see cref="TwinAxes"/> relies on <see cref="OnValueChanged"/> to monitor its instances of <see cref="Axis"/>
105+
/// for value changes, so take care.</para></param>
100106
public void SetValue(float value, bool invoke = true)
101107
{
102108
if(value != Value)
@@ -107,6 +113,8 @@ public void SetValue(float value, bool invoke = true)
107113
{
108114
positiveFrame = Time.frameCount;
109115
positiveTime = Time.unscaledTime;
116+
if(invoke)
117+
OnPositive?.Invoke();
110118
}
111119
}
112120
else if (value < 0f)
@@ -115,6 +123,8 @@ public void SetValue(float value, bool invoke = true)
115123
{
116124
negativeFrame = Time.frameCount;
117125
negativeTime = Time.unscaledTime;
126+
if (invoke)
127+
OnNegative?.Invoke();
118128
}
119129
}
120130
else
@@ -123,6 +133,8 @@ public void SetValue(float value, bool invoke = true)
123133
{
124134
neutralFrame = Time.frameCount;
125135
neutralTime = Time.unscaledTime;
136+
if (invoke)
137+
OnNeutral?.Invoke();
126138
}
127139
}
128140
Value = value;
@@ -134,119 +146,108 @@ public void SetValue(float value, bool invoke = true)
134146
}
135147

136148
/// <summary>
137-
/// Sets <see cref="Value"/> to "neutral" (0f).
138-
/// <para>The <see cref="Axis"/> logs the <see cref="Time.frameCount"/> and <see cref="Time.unscaledTime"/> whenever <see cref="Value"/> is set.</para>
139-
/// </summary>
140-
public void SetNeutral()
141-
{
142-
SetValue(0f);
143-
}
144-
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-
158-
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
159-
160-
/// <summary>
161-
/// Returns for how many frames <see cref="Value"/> has been positive, if it currently is. Returns -1 otherwise.
149+
/// Calculates the amount of update frames <see cref="Value"/> has been positive for.
162150
/// </summary>
163151
/// <returns>
164-
/// 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.
165-
/// <para>This method will also always return -1 if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.</para>
152+
/// How many update frames <see cref="Value"/> has been positive, if it currently is.
153+
/// <para>-1 will be returned if <see cref="Value"/> is currently not positive, or if <see cref="SetValue"/> has never been called.</para>
166154
/// </returns>
167-
public int PositiveDurationFrames()
155+
public int GetPositiveDurationFrames()
168156
{
169157
if (set && Positive && positiveFrame >= negativeFrame && positiveFrame >= neutralFrame)
170158
return Time.frameCount - positiveFrame;
171-
else
159+
else
172160
return -1;
173161
}
174162

175163
/// <summary>
176-
/// Returns for how many seconds <see cref="Value"/> has been positive, if it currently is. Returns -1f otherwise.
164+
/// Calculates the amount of seconds <see cref="Value"/> has been positive for.
177165
/// </summary>
178166
/// <returns>
179-
/// 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.
180-
/// <para>This method will also always return -1f if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.</para>
167+
/// How many seconds <see cref="Value"/> has been positive, if it currently is.
168+
/// <para>-1 will be returned if <see cref="Value"/> is currently not positive, or if <see cref="SetValue"/> has never been called.</para>
181169
/// </returns>
182-
public float PositiveDurationTime()
170+
public float GetPositiveDurationTime()
183171
{
184172
if (set && Positive && positiveTime >= negativeTime && positiveTime >= neutralTime)
185173
return Time.unscaledTime - positiveTime;
186-
else
174+
else
187175
return -1f;
188176
}
189177

190178
/// <summary>
191-
/// Returns for how many frames <see cref="Value"/> has been neutral (0f), if it currently is. Returns -1 otherwise.
179+
/// Calculates the amount of update frames <see cref="Value"/> has been neutral (0f) for.
192180
/// </summary>
193181
/// <returns>
194-
/// 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.
195-
/// <para>This method will also always return -1 if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.</para>
182+
/// How many update frames <see cref="Value"/> has been neutral, if it currently is.
183+
/// <para>-1 will be returned if <see cref="Value"/> is currently not neutral, or if <see cref="SetValue"/> has never been called.</para>
196184
/// </returns>
197-
public int NeutralDurationFrames()
185+
public int GetNeutralDurationFrames()
198186
{
199187
if (set && Neutral && neutralFrame >= negativeFrame && neutralFrame >= positiveFrame)
200188
return Time.frameCount - neutralFrame;
201-
else
189+
else
202190
return -1;
203191
}
204192

205193
/// <summary>
206-
/// Returns for how many seconds <see cref="Value"/> has been neutral (0f), if it currently is. Returns -1f otherwise.
194+
/// Calculates the amount of seconds <see cref="Value"/> has been neutral (0f) for.
207195
/// </summary>
208196
/// <returns>
209-
/// 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.
210-
/// <para>This method will also always return -1f if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.</para>
197+
/// How many seconds <see cref="Value"/> has been neutral, if it currently is.
198+
/// <para>-1 will be returned if <see cref="Value"/> is currently not neutral, or if <see cref="SetValue"/> has never been called.</para>
211199
/// </returns>
212-
public float NeutralDurationTime()
200+
public float GetNeutralDurationTime()
213201
{
214202
if (set && Neutral && neutralTime >= negativeTime && neutralTime >= positiveTime)
215-
return Time.unscaledTime - neutralTime;
216-
else
203+
return Time.unscaledTime - neutralTime;
204+
else
217205
return -1f;
218206
}
219207

220208
/// <summary>
221-
/// Returns for how many frames <see cref="Value"/> has been negative, if it currently is. Returns -1 otherwise.
209+
/// Calculates the amount of update frames <see cref="Value"/> has been negative for.
222210
/// </summary>
223211
/// <returns>
224-
/// 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.
225-
/// <para>This method will also always return -1 if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.</para>
212+
/// How many update frames <see cref="Value"/> has been negative, if it currently is.
213+
/// <para>-1 will be returned if <see cref="Value"/> is currently not negative, or if <see cref="SetValue"/> has never been called.</para>
226214
/// </returns>
227-
public int NegativeDurationFrames()
215+
public int GetNegativeDurationFrames()
228216
{
229217
if (set && Negative && negativeFrame >= positiveFrame && negativeFrame >= neutralFrame)
230218
return Time.frameCount - negativeFrame;
231-
else
219+
else
232220
return -1;
233221
}
234222

235223
/// <summary>
236-
/// Returns for how many seconds <see cref="Value"/> has been negative, if it currently is. Returns -1f otherwise.
224+
/// Calculates the amount of seconds <see cref="Value"/> has been negative for.
237225
/// </summary>
238226
/// <returns>
239-
/// 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.
240-
/// <para>This method will also always return -1f if <see cref="SetValue(float)"/> or <see cref="SetNeutral"/> have never been called.</para>
227+
/// How many seconds <see cref="Value"/> has been negative, if it currently is.
228+
/// <para>-1 will be returned if <see cref="Value"/> is currently not negative, or if <see cref="SetValue"/> has never been called.</para>
241229
/// </returns>
242-
public float NegativeDurationTime()
230+
public float GetNegativeDurationTime()
243231
{
244232
if (set && Negative && negativeTime >= positiveTime && negativeTime >= neutralTime)
245233
return Time.unscaledTime - neutralTime;
246-
else
234+
else
247235
return -1f;
248236
}
249237

238+
/// <summary>
239+
/// Tie the <see cref="Axis"/> to an <see cref="InputAction"/>.
240+
/// </summary>
241+
/// <param name="action">The <see cref="InputAction"/> to attach to the <see cref="Axis"/>.</param>
242+
public void RegisterInputAction(InputAction action)
243+
{
244+
if (action == null)
245+
throw new System.ArgumentNullException("action");
246+
if (action.expectedControlType != "Axis")
247+
throw new System.ArgumentException("InputAction " + action.name + " does not have an expected Axis control type.");
248+
SetInputAction(action);
249+
}
250+
250251
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
251252

252253
private void ActionPerformed(InputAction.CallbackContext context)
@@ -256,7 +257,7 @@ private void ActionPerformed(InputAction.CallbackContext context)
256257

257258
private void ActionCanceled(InputAction.CallbackContext context)
258259
{
259-
SetNeutral();
260+
SetValue(0f);
260261
}
261262

262263
private void SetInputAction(InputAction action)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.stephanhooft.input-processing",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"displayName": "Input Processing",
55
"description": "A package that contains helper classes to store and evaluate user input.",
66
"unity": "2020.3",

0 commit comments

Comments
 (0)