1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-25 22:18:34 +01:00

Added GamepadExtensions.GetAnalogValue to get the analog value of any gamepad button

This commit is contained in:
Ell 2022-03-14 15:15:30 +01:00
parent b77edd80d5
commit 5d97ab033f
3 changed files with 65 additions and 12 deletions

View file

@ -15,6 +15,7 @@ Additions
- Added SoundEffectInstanceHandler.Stop
- Added TextureRegion.OffsetCopy
- Added RectangleF.DistanceSquared and RectangleF.Distance
- Added GamepadExtensions.GetAnalogValue to get the analog value of any gamepad button
Improvements
- Generify GenericFont's string drawing
@ -22,6 +23,7 @@ Improvements
- Added float version of GetRandomWeightedEntry
- Allow LinkCode to specify a color to draw with
- Allow better control over the order and layout of a Keybind's combinations
- Allow setting a gamepad button deadzone in InputHandler
Fixes
- **Fixed a formatting Code only knowing about the last Token that it is applied in**

View file

@ -0,0 +1,46 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace MLEM.Input {
/// <summary>
/// A set of extension methods for dealing with <see cref="GamePad"/>, <see cref="GamePadState"/> and <see cref="Buttons"/>.
/// </summary>
public static class GamepadExtensions {
/// <summary>
/// Returns the given <see cref="Buttons"/>'s value as an analog value between 0 and 1, where 1 is fully down and 0 is not down at all.
/// For non-analog buttons, like <see cref="Buttons.X"/> or <see cref="Buttons.Start"/>, only 0 and 1 will be returned and no inbetween values are possible.
/// </summary>
/// <param name="state">The gamepad state to query.</param>
/// <param name="button">The button to query.</param>
/// <returns>The button's state as an analog value.</returns>
public static float GetAnalogValue(this GamePadState state, Buttons button) {
switch (button) {
case Buttons.LeftThumbstickDown:
return -MathHelper.Clamp(state.ThumbSticks.Left.Y, -1, 0);
case Buttons.LeftThumbstickUp:
return MathHelper.Clamp(state.ThumbSticks.Left.Y, 0, 1);
case Buttons.LeftThumbstickLeft:
return -MathHelper.Clamp(state.ThumbSticks.Left.X, -1, 0);
case Buttons.LeftThumbstickRight:
return MathHelper.Clamp(state.ThumbSticks.Left.X, 0, 1);
case Buttons.RightTrigger:
return state.Triggers.Right;
case Buttons.LeftTrigger:
return state.Triggers.Left;
case Buttons.RightThumbstickDown:
return -MathHelper.Clamp(state.ThumbSticks.Right.Y, -1, 0);
case Buttons.RightThumbstickUp:
return MathHelper.Clamp(state.ThumbSticks.Right.Y, 0, 1);
case Buttons.RightThumbstickLeft:
return -MathHelper.Clamp(state.ThumbSticks.Right.X, -1, 0);
case Buttons.RightThumbstickRight:
return MathHelper.Clamp(state.ThumbSticks.Right.X, 0, 1);
default:
return state.IsButtonDown(button) ? 1 : 0;
}
}
}
}

View file

@ -59,6 +59,12 @@ namespace MLEM.Input {
/// Set this field to false to enable <see cref="InputsDown"/> and <see cref="InputsPressed"/> being calculated.
/// </summary>
public bool StoreAllActiveInputs;
/// <summary>
/// This field represents the deadzone that gamepad <see cref="Buttons"/> have when input is queried for them using this input handler.
/// A deadzone is the percentage (between 0 and 1) that an analog value has to reach for it to be considered down (<see cref="IsGamepadButtonDown"/>) or pressed (<see cref="IsGamepadButtonPressed"/>).
/// Querying of analog values is done using <see cref="GamepadExtensions.GetAnalogValue"/>.
/// </summary>
public float GamepadButtonDeadzone;
/// <summary>
/// An array of all <see cref="Keys"/>, <see cref="Buttons"/> and <see cref="MouseButton"/> values that are currently down.
@ -234,13 +240,13 @@ namespace MLEM.Input {
this.ConnectedGamepads = GamePad.MaximumGamePadCount;
for (var i = 0; i < GamePad.MaximumGamePadCount; i++) {
this.lastGamepads[i] = this.gamepads[i];
var state = GamePadState.Default;
this.gamepads[i] = GamePadState.Default;
if (GamePad.GetCapabilities(i).IsConnected) {
if (active) {
state = GamePad.GetState(i);
this.gamepads[i] = GamePad.GetState(i);
if (this.StoreAllActiveInputs) {
foreach (var button in EnumHelper.Buttons) {
if (state.IsButtonDown(button))
if (this.IsGamepadButtonDown(button, i))
this.inputsDownAccum.Add(button);
}
}
@ -249,7 +255,6 @@ namespace MLEM.Input {
if (this.ConnectedGamepads > i)
this.ConnectedGamepads = i;
}
this.gamepads[i] = state;
}
if (this.HandleGamepadRepeats) {
@ -446,48 +451,48 @@ namespace MLEM.Input {
public bool IsGamepadButtonDown(Buttons button, int index = -1) {
if (index < 0) {
for (var i = 0; i < this.ConnectedGamepads; i++) {
if (this.GetGamepadState(i).IsButtonDown(button))
if (this.GetGamepadState(i).GetAnalogValue(button) >= this.GamepadButtonDeadzone)
return true;
}
return false;
}
return this.GetGamepadState(index).IsButtonDown(button);
return this.GetGamepadState(index).GetAnalogValue(button) >= this.GamepadButtonDeadzone;
}
/// <inheritdoc cref="GamePadState.IsButtonUp"/>
public bool IsGamepadButtonUp(Buttons button, int index = -1) {
if (index < 0) {
for (var i = 0; i < this.ConnectedGamepads; i++) {
if (this.GetGamepadState(i).IsButtonUp(button))
if (this.GetGamepadState(i).GetAnalogValue(button) < this.GamepadButtonDeadzone)
return true;
}
return false;
}
return this.GetGamepadState(index).IsButtonUp(button);
return this.GetGamepadState(index).GetAnalogValue(button) < this.GamepadButtonDeadzone;
}
/// <inheritdoc cref="GamePadState.IsButtonDown"/>
public bool WasGamepadButtonDown(Buttons button, int index = -1) {
if (index < 0) {
for (var i = 0; i < this.ConnectedGamepads; i++) {
if (this.GetLastGamepadState(i).IsButtonDown(button))
if (this.GetLastGamepadState(i).GetAnalogValue(button) >= this.GamepadButtonDeadzone)
return true;
}
return false;
}
return this.GetLastGamepadState(index).IsButtonDown(button);
return this.GetLastGamepadState(index).GetAnalogValue(button) >= this.GamepadButtonDeadzone;
}
/// <inheritdoc cref="GamePadState.IsButtonUp"/>
public bool WasGamepadButtonUp(Buttons button, int index = -1) {
if (index < 0) {
for (var i = 0; i < this.ConnectedGamepads; i++) {
if (this.GetLastGamepadState(i).IsButtonUp(button))
if (this.GetLastGamepadState(i).GetAnalogValue(button) < this.GamepadButtonDeadzone)
return true;
}
return false;
}
return this.GetLastGamepadState(index).IsButtonUp(button);
return this.GetLastGamepadState(index).GetAnalogValue(button) < this.GamepadButtonDeadzone;
}
/// <summary>