From 9f4b69e6aa1ff561656a08055e57ee6c0a8ee98f Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 24 Mar 2022 16:41:00 -0700 Subject: [PATCH] keyboard uses Button instead of ButtonState --- src/Input/Keyboard.cs | 15 ++++++++++----- src/Math/MathHelper.cs | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/Input/Keyboard.cs b/src/Input/Keyboard.cs index 43ef24b..b1acdc7 100644 --- a/src/Input/Keyboard.cs +++ b/src/Input/Keyboard.cs @@ -7,7 +7,7 @@ namespace MoonWorks.Input { public class Keyboard { - private ButtonState[] Keys { get; } + private Button[] Keys { get; } private int numKeys; private static readonly char[] TextInputCharacters = new char[] @@ -36,10 +36,10 @@ namespace MoonWorks.Input { SDL.SDL_GetKeyboardState(out numKeys); - Keys = new ButtonState[numKeys]; + Keys = new Button[numKeys]; 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))) { var keyDown = Marshal.ReadByte(keyboardState, keycode); - Keys[keycode] = Keys[keycode].Update(Conversions.ByteToBool(keyDown)); + Keys[keycode].Update(Conversions.ByteToBool(keyDown)); if (Conversions.ByteToBool(keyDown)) { @@ -86,9 +86,14 @@ namespace MoonWorks.Input return Keys[(int) keycode].IsReleased; } - public ButtonState ButtonState(KeyCode keycode) + public Button Button(KeyCode keycode) { return Keys[(int) keycode]; } + + public ButtonState ButtonState(KeyCode keycode) + { + return Keys[(int) keycode].State; + } } } diff --git a/src/Math/MathHelper.cs b/src/Math/MathHelper.cs index da01c7d..0431d80 100644 --- a/src/Math/MathHelper.cs +++ b/src/Math/MathHelper.cs @@ -331,16 +331,36 @@ namespace MoonWorks.Math return angle; } + /// + /// Rescales a value within a given range to a new range. + /// public static float Normalize(float value, short min, short max, short newMin, short newMax) { return ((value - min) * (newMax - newMin)) / (max - min) + newMin; } + /// + /// Rescales a value within a given range to a new range. + /// public static float Normalize(float value, float min, float max, float newMin, float newMax) { return ((value - min) * (newMax - newMin)) / (max - min) + newMin; } + /// + /// Step from start towards end by change. + /// + /// Start value. + /// End value. + /// Change value. + /// The final delta. + 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 #region Internal Static Methods