keyboard uses Button instead of ButtonState

main
cosmonaut 2022-03-24 16:41:00 -07:00
parent 80c34c6b16
commit 9f4b69e6aa
2 changed files with 30 additions and 5 deletions

View File

@ -7,7 +7,7 @@ namespace MoonWorks.Input
{ {
public class Keyboard public class Keyboard
{ {
private ButtonState[] Keys { get; } private Button[] Keys { get; }
private int numKeys; private int numKeys;
private static readonly char[] TextInputCharacters = new char[] private static readonly char[] TextInputCharacters = new char[]
@ -36,10 +36,10 @@ namespace MoonWorks.Input
{ {
SDL.SDL_GetKeyboardState(out numKeys); SDL.SDL_GetKeyboardState(out numKeys);
Keys = new ButtonState[numKeys]; Keys = new Button[numKeys];
foreach (KeyCode keycode in Enum.GetValues(typeof(KeyCode))) foreach (KeyCode keycode in Enum.GetValues(typeof(KeyCode)))
{ {
Keys[(int) keycode] = new ButtonState(); Keys[(int) keycode] = new Button();
} }
} }
@ -50,7 +50,7 @@ namespace MoonWorks.Input
foreach (int keycode in Enum.GetValues(typeof(KeyCode))) foreach (int keycode in Enum.GetValues(typeof(KeyCode)))
{ {
var keyDown = Marshal.ReadByte(keyboardState, keycode); var keyDown = Marshal.ReadByte(keyboardState, keycode);
Keys[keycode] = Keys[keycode].Update(Conversions.ByteToBool(keyDown)); Keys[keycode].Update(Conversions.ByteToBool(keyDown));
if (Conversions.ByteToBool(keyDown)) if (Conversions.ByteToBool(keyDown))
{ {
@ -86,9 +86,14 @@ namespace MoonWorks.Input
return Keys[(int) keycode].IsReleased; return Keys[(int) keycode].IsReleased;
} }
public ButtonState ButtonState(KeyCode keycode) public Button Button(KeyCode keycode)
{ {
return Keys[(int) keycode]; return Keys[(int) keycode];
} }
public ButtonState ButtonState(KeyCode keycode)
{
return Keys[(int) keycode].State;
}
} }
} }

View File

@ -331,16 +331,36 @@ namespace MoonWorks.Math
return angle; return angle;
} }
/// <summary>
/// Rescales a value within a given range to a new range.
/// </summary>
public static float Normalize(float value, short min, short max, short newMin, short newMax) public static float Normalize(float value, short min, short max, short newMin, short newMax)
{ {
return ((value - min) * (newMax - newMin)) / (max - min) + newMin; return ((value - min) * (newMax - newMin)) / (max - min) + newMin;
} }
/// <summary>
/// Rescales a value within a given range to a new range.
/// </summary>
public static float Normalize(float value, float min, float max, float newMin, float newMax) public static float Normalize(float value, float min, float max, float newMin, float newMax)
{ {
return ((value - min) * (newMax - newMin)) / (max - min) + newMin; return ((value - min) * (newMax - newMin)) / (max - min) + newMin;
} }
/// <summary>
/// Step from start towards end by change.
/// </summary>
/// <param name="start">Start value.</param>
/// <param name="end">End value.</param>
/// <param name="change">Change value.</param>
/// <returns>The final delta.</returns>
public static float Approach(float start, float end, float change)
{
return (start < end ?
System.Math.Min(start + change, end) :
System.Math.Max(start - change, end)) - start;
}
#endregion #endregion
#region Internal Static Methods #region Internal Static Methods