add vibration support + fix controller axes

pull/14/head
cosmonaut 2021-03-09 21:48:31 -08:00
parent 87d615783e
commit 7ecb27c98a
3 changed files with 33 additions and 4 deletions

View File

@ -1,4 +1,5 @@
using System; using System;
using MoonWorks.Math;
using SDL2; using SDL2;
namespace MoonWorks.Input namespace MoonWorks.Input
@ -35,6 +36,16 @@ namespace MoonWorks.Input
Handle = handle; Handle = handle;
} }
public bool SetVibration(float leftMotor, float rightMotor, uint durationInMilliseconds)
{
return SDL.SDL_GameControllerRumble(
Handle,
(ushort)(MathHelper.Clamp(leftMotor, 0f, 1f) * 0xFFFF),
(ushort)(MathHelper.Clamp(rightMotor, 0f, 1f) * 0xFFFF),
durationInMilliseconds
) == 0;
}
internal void Update() internal void Update()
{ {
A.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_A)); A.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_A));
@ -69,19 +80,19 @@ namespace MoonWorks.Input
private float UpdateAxis(SDL.SDL_GameControllerAxis axis) private float UpdateAxis(SDL.SDL_GameControllerAxis axis)
{ {
var axisValue = SDL.SDL_GameControllerGetAxis(Handle, axis); var axisValue = SDL.SDL_GameControllerGetAxis(Handle, axis);
return Normalize(axisValue, short.MinValue, short.MaxValue); return Normalize(axisValue, short.MinValue, short.MaxValue, -1, 1);
} }
// Triggers only go from 0 to short.MaxValue // Triggers only go from 0 to short.MaxValue
private float UpdateTrigger(SDL.SDL_GameControllerAxis trigger) private float UpdateTrigger(SDL.SDL_GameControllerAxis trigger)
{ {
var triggerValue = SDL.SDL_GameControllerGetAxis(Handle, trigger); var triggerValue = SDL.SDL_GameControllerGetAxis(Handle, trigger);
return Normalize(triggerValue, 0, short.MaxValue); return Normalize(triggerValue, 0, short.MaxValue, 0, 1);
} }
private float Normalize(float value, short min, short max) private float Normalize(float value, short min, short max, short newMin, short newMax)
{ {
return (value - min) / (max - min); return ((value - min) * (newMax - newMin)) / (max - min) + newMin;
} }
} }
} }

View File

@ -36,6 +36,11 @@ namespace MoonWorks.Input
} }
} }
public bool GamepadExists(int slot)
{
return slot < gamepads.Count;
}
public Gamepad GetGamepad(int slot) public Gamepad GetGamepad(int slot)
{ {
return gamepads[slot]; return gamepads[slot];

View File

@ -492,6 +492,19 @@ namespace MoonWorks.Math
result.Z = MathHelper.Clamp(value1.Z, min.Z, max.Z); result.Z = MathHelper.Clamp(value1.Z, min.Z, max.Z);
} }
/// <summary>
/// Clamps the magnitude of the specified vector.
/// </summary>
/// <param name="value">The vector to clamp.</param>
/// <param name="maxLength">The maximum length of the vector.</param>
/// <returns></returns>
public static Vector3 ClampMagnitude(
Vector3 value,
float maxLength
) {
return (value.LengthSquared() > maxLength * maxLength) ? (Vector3.Normalize(value) * maxLength) : value;
}
/// <summary> /// <summary>
/// Computes the cross product of two vectors. /// Computes the cross product of two vectors.
/// </summary> /// </summary>