forked from YARC-Official/YARG
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework input handling to allow for axis support (YARC-Official#177)
* Allow processing and mapping of axis controls as buttons * Adjust input strategies to work in terms of floats * Encapsulate control mapping handling * Rework mapping system NOTE: Some binding strings have changed in order to keep the styles consistent. Users will need to rebind the affected controls. * Add helper methods for getting mapping values * Detect multiple controls being active when binding controls * Fix cancellation handling
- Loading branch information
1 parent
593671c
commit 8c3acfc
Showing
11 changed files
with
330 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using UnityEngine.InputSystem; | ||
using UnityEngine.InputSystem.Controls; | ||
using UnityEngine.InputSystem.LowLevel; | ||
|
||
namespace YARG.Input { | ||
public enum BindingType { | ||
BUTTON, | ||
AXIS | ||
} | ||
|
||
public class ControlBinding { | ||
public const float DEFAULT_PRESS_THRESHOLD = 0.75f; // TODO: Remove once control calibration is added | ||
|
||
public BindingType Type { get; } | ||
public string DisplayName { get; } | ||
public string BindingKey { get; } | ||
|
||
private InputControl<float> _control; | ||
public InputControl<float> Control { | ||
get => _control; | ||
set { | ||
_control = value; | ||
if (value is ButtonControl button) { | ||
pressPoint = button.pressPointOrDefault; | ||
} | ||
} | ||
} | ||
|
||
private (float previous, float current) _state; | ||
public (float previous, float current) State => _state; | ||
|
||
private float pressPoint = DEFAULT_PRESS_THRESHOLD; | ||
|
||
public ControlBinding(BindingType type, string displayName, string bindingKey) { | ||
Type = type; | ||
DisplayName = displayName; | ||
BindingKey = bindingKey; | ||
} | ||
|
||
public bool IsPressed() { | ||
// Ignore if unmapped | ||
if (_control == null) { | ||
return false; | ||
} | ||
|
||
return _state.current >= pressPoint; | ||
} | ||
|
||
public bool WasPressed() { | ||
// Ignore if unmapped | ||
if (_control == null) { | ||
return false; | ||
} | ||
|
||
return _state.previous < pressPoint && _state.current >= pressPoint; | ||
} | ||
|
||
public bool WasReleased() { | ||
// Ignore if unmapped | ||
if (_control == null) { | ||
return false; | ||
} | ||
|
||
return _state.previous >= pressPoint && _state.current < pressPoint; | ||
} | ||
|
||
public void UpdateState(InputEventPtr eventPtr) { | ||
// Ignore if unmapped | ||
if (_control == null) { | ||
return; | ||
} | ||
|
||
// Progress state history forward | ||
_state.previous = _state.current; | ||
// Don't check pressed state unless there was a value change | ||
// Controls not changed in a delta state event (which MIDI devices use) will report the wrong value | ||
if (_control.HasValueChangeInEvent(eventPtr)) { | ||
_state.current = _control.ReadValueFromEvent(eventPtr); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.