forked from MoonsideGames/MoonWorks
				
			input refactor
							parent
							
								
									12c5f37ae9
								
							
						
					
					
						commit
						a3307c0066
					
				|  | @ -1,4 +1,4 @@ | ||||||
| namespace MoonWorks.Graphics | namespace MoonWorks | ||||||
| { | { | ||||||
|     public static class Conversions |     public static class Conversions | ||||||
|     { |     { | ||||||
|  | @ -6,5 +6,10 @@ namespace MoonWorks.Graphics | ||||||
|         { |         { | ||||||
|             return (byte)(b ? 1 : 0); |             return (byte)(b ? 1 : 0); | ||||||
|         } |         } | ||||||
|  | 
 | ||||||
|  |         public static bool ByteToBool(byte b) | ||||||
|  |         { | ||||||
|  |             return b == 0 ? false : true; | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,18 +1,31 @@ | ||||||
| namespace MoonWorks.Input | namespace MoonWorks.Input | ||||||
| { | { | ||||||
|     public enum ButtonState |     public class ButtonState | ||||||
|     { |     { | ||||||
|         /// <summary> |         private ButtonStatus ButtonStatus { get; set; } | ||||||
|         /// Indicates that the input is not pressed. | 
 | ||||||
|         /// </summary> |         public bool IsPressed => ButtonStatus == ButtonStatus.Pressed; | ||||||
|         Released, |         public bool IsHeld => ButtonStatus == ButtonStatus.Held; | ||||||
|         /// <summary> |         public bool IsDown => ButtonStatus == ButtonStatus.Pressed || ButtonStatus == ButtonStatus.Held; | ||||||
|         /// Indicates that the input was pressed this frame. |         public bool IsReleased => ButtonStatus == ButtonStatus.Released; | ||||||
|         /// </summary> | 
 | ||||||
|         Pressed, |         internal void Update(bool isPressed) | ||||||
|         /// <summary> |         { | ||||||
|         /// Indicates that the input has been held for multiple frames. |             if (isPressed) | ||||||
|         /// </summary> |             { | ||||||
|         Held |                 if (ButtonStatus == ButtonStatus.Pressed) | ||||||
|  |                 { | ||||||
|  |                     ButtonStatus = ButtonStatus.Held; | ||||||
|  |                 } | ||||||
|  |                 else if (ButtonStatus == ButtonStatus.Released) | ||||||
|  |                 { | ||||||
|  |                     ButtonStatus = ButtonStatus.Pressed; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             else | ||||||
|  |             { | ||||||
|  |                 ButtonStatus = ButtonStatus.Released; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -0,0 +1,18 @@ | ||||||
|  | namespace MoonWorks.Input | ||||||
|  | { | ||||||
|  |     internal enum ButtonStatus | ||||||
|  |     { | ||||||
|  |         /// <summary> | ||||||
|  |         /// Indicates that the input is not pressed. | ||||||
|  |         /// </summary> | ||||||
|  |         Released, | ||||||
|  |         /// <summary> | ||||||
|  |         /// Indicates that the input was pressed this frame. | ||||||
|  |         /// </summary> | ||||||
|  |         Pressed, | ||||||
|  |         /// <summary> | ||||||
|  |         /// Indicates that the input has been held for multiple frames. | ||||||
|  |         /// </summary> | ||||||
|  |         Held | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -7,21 +7,21 @@ namespace MoonWorks.Input | ||||||
|     { |     { | ||||||
|         internal IntPtr Handle; |         internal IntPtr Handle; | ||||||
| 
 | 
 | ||||||
|         public ButtonState A { get; private set; } |         public ButtonState A { get; } = new ButtonState(); | ||||||
|         public ButtonState B { get; private set; } |         public ButtonState B { get; } = new ButtonState(); | ||||||
|         public ButtonState X { get; private set; } |         public ButtonState X { get; } = new ButtonState(); | ||||||
|         public ButtonState Y { get; private set; } |         public ButtonState Y { get; } = new ButtonState(); | ||||||
|         public ButtonState Back { get; private set; } |         public ButtonState Back { get; } = new ButtonState(); | ||||||
|         public ButtonState Guide { get; private set; } |         public ButtonState Guide { get; } = new ButtonState(); | ||||||
|         public ButtonState Start { get; private set; } |         public ButtonState Start { get; } = new ButtonState(); | ||||||
|         public ButtonState LeftStick { get; private set; } |         public ButtonState LeftStick { get; } = new ButtonState(); | ||||||
|         public ButtonState RightStick { get; private set; } |         public ButtonState RightStick { get; } = new ButtonState(); | ||||||
|         public ButtonState LeftShoulder { get; private set; } |         public ButtonState LeftShoulder { get; } = new ButtonState(); | ||||||
|         public ButtonState RightShoulder { get; private set; } |         public ButtonState RightShoulder { get; } = new ButtonState(); | ||||||
|         public ButtonState DpadUp { get; private set; } |         public ButtonState DpadUp { get; } = new ButtonState(); | ||||||
|         public ButtonState DpadDown { get; private set; } |         public ButtonState DpadDown { get; } = new ButtonState(); | ||||||
|         public ButtonState DpadLeft { get; private set; } |         public ButtonState DpadLeft { get; } = new ButtonState(); | ||||||
|         public ButtonState DpadRight { get; private set; } |         public ButtonState DpadRight { get; } = new ButtonState(); | ||||||
| 
 | 
 | ||||||
|         public float LeftX { get; private set; } |         public float LeftX { get; private set; } | ||||||
|         public float LeftY { get; private set; } |         public float LeftY { get; private set; } | ||||||
|  | @ -37,21 +37,21 @@ namespace MoonWorks.Input | ||||||
| 
 | 
 | ||||||
|         internal void Update() |         internal void Update() | ||||||
|         { |         { | ||||||
|             A = UpdateState(A, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_A); |             A.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_A)); | ||||||
|             B = UpdateState(B, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_B); |             B.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_B)); | ||||||
|             X = UpdateState(X, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_X); |             X.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_X)); | ||||||
|             Y = UpdateState(Y, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_Y); |             Y.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_Y)); | ||||||
|             Back = UpdateState(Back, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_BACK); |             Back.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_BACK)); | ||||||
|             Guide = UpdateState(Guide, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_GUIDE); |             Guide.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_GUIDE)); | ||||||
|             Start = UpdateState(Start, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_START); |             Start.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_START)); | ||||||
|             LeftStick = UpdateState(LeftStick, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSTICK); |             LeftStick.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSTICK)); | ||||||
|             RightStick = UpdateState(RightStick, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSTICK); |             RightStick.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSTICK)); | ||||||
|             LeftShoulder = UpdateState(LeftShoulder, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSHOULDER); |             LeftShoulder.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSHOULDER)); | ||||||
|             RightShoulder = UpdateState(RightShoulder, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSHOULDER); |             RightShoulder.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)); | ||||||
|             DpadUp = UpdateState(DpadUp, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_UP); |             DpadUp.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_UP)); | ||||||
|             DpadDown = UpdateState(DpadDown, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_DOWN); |             DpadDown.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_DOWN)); | ||||||
|             DpadLeft = UpdateState(DpadLeft, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_LEFT); |             DpadLeft.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_LEFT)); | ||||||
|             DpadRight = UpdateState(DpadRight, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_RIGHT); |             DpadRight.Update(IsPressed(SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_RIGHT)); | ||||||
| 
 | 
 | ||||||
|             LeftX = UpdateAxis(SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTX); |             LeftX = UpdateAxis(SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTX); | ||||||
|             LeftY = UpdateAxis(SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTY); |             LeftY = UpdateAxis(SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTY); | ||||||
|  | @ -61,23 +61,9 @@ namespace MoonWorks.Input | ||||||
|             TriggerRight = UpdateTrigger(SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERRIGHT); |             TriggerRight = UpdateTrigger(SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERRIGHT); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         private ButtonState UpdateState(ButtonState state, SDL.SDL_GameControllerButton button) |         private bool IsPressed(SDL.SDL_GameControllerButton button) | ||||||
|         { |         { | ||||||
|             var isPressed = SDL.SDL_GameControllerGetButton(Handle, button); |             return MoonWorks.Conversions.ByteToBool(SDL.SDL_GameControllerGetButton(Handle, button)); | ||||||
| 
 |  | ||||||
|             if (isPressed == 1) |  | ||||||
|             { |  | ||||||
|                 if (state == ButtonState.Pressed) |  | ||||||
|                 { |  | ||||||
|                     return ButtonState.Held; |  | ||||||
|                 } |  | ||||||
|                 else if (state == ButtonState.Released) |  | ||||||
|                 { |  | ||||||
|                     return ButtonState.Pressed; |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             return ButtonState.Released; |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         private float UpdateAxis(SDL.SDL_GameControllerAxis axis) |         private float UpdateAxis(SDL.SDL_GameControllerAxis axis) | ||||||
|  |  | ||||||
|  | @ -1,14 +0,0 @@ | ||||||
| namespace MoonWorks.Input |  | ||||||
| { |  | ||||||
|     internal class Key |  | ||||||
|     { |  | ||||||
|         public Keycode Keycode { get; } |  | ||||||
|         public ButtonState InputState { get; internal set; } |  | ||||||
| 
 |  | ||||||
|         public Key(Keycode keycode) |  | ||||||
|         { |  | ||||||
|             Keycode = keycode; |  | ||||||
|             InputState = ButtonState.Released; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
|  | @ -6,17 +6,17 @@ namespace MoonWorks.Input | ||||||
| { | { | ||||||
|     public class Keyboard |     public class Keyboard | ||||||
|     { |     { | ||||||
|         private Key[] Keys { get; } |         private ButtonState[] Keys { get; } | ||||||
|         private int numKeys; |         private int numKeys; | ||||||
| 
 | 
 | ||||||
|         internal Keyboard() |         internal Keyboard() | ||||||
|         { |         { | ||||||
|             SDL.SDL_GetKeyboardState(out numKeys); |             SDL.SDL_GetKeyboardState(out numKeys); | ||||||
| 
 | 
 | ||||||
|             Keys = new Key[numKeys]; |             Keys = new ButtonState[numKeys]; | ||||||
|             foreach (Keycode keycode in Enum.GetValues(typeof(Keycode))) |             foreach (Keycode keycode in Enum.GetValues(typeof(Keycode))) | ||||||
|             { |             { | ||||||
|                 Keys[(int)keycode] = new Key(keycode); |                 Keys[(int)keycode] = new ButtonState(); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  | @ -27,47 +27,28 @@ 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].Update(Conversions.ByteToBool(keyDown)); | ||||||
|                 if (keyDown == 1) |  | ||||||
|                 { |  | ||||||
|                     if (Keys[keycode].InputState == ButtonState.Released) |  | ||||||
|                     { |  | ||||||
|                         Keys[keycode].InputState = ButtonState.Pressed; |  | ||||||
|                     } |  | ||||||
|                     else if (Keys[keycode].InputState == ButtonState.Pressed) |  | ||||||
|                     { |  | ||||||
|                         Keys[keycode].InputState = ButtonState.Held; |  | ||||||
|                     } |  | ||||||
|                 } |  | ||||||
|                 else |  | ||||||
|                 { |  | ||||||
|                     Keys[keycode].InputState = ButtonState.Released; |  | ||||||
|                 } |  | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public bool IsDown(Keycode keycode) |         public bool IsDown(Keycode keycode) | ||||||
|         { |         { | ||||||
|             var key = Keys[(int)keycode]; |             return Keys[(int)keycode].IsDown; | ||||||
|             return (key.InputState == ButtonState.Pressed) || (key.InputState == ButtonState.Held); |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public bool IsPressed(Keycode keycode) |         public bool IsPressed(Keycode keycode) | ||||||
|         { |         { | ||||||
|             var key = Keys[(int)keycode]; |             return Keys[(int)keycode].IsPressed; | ||||||
|             return key.InputState == ButtonState.Pressed; |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public bool IsHeld(Keycode keycode) |         public bool IsHeld(Keycode keycode) | ||||||
|         { |         { | ||||||
|             var key = Keys[(int)keycode]; |             return Keys[(int)keycode].IsHeld; | ||||||
|             return key.InputState == ButtonState.Held; |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public bool IsUp(Keycode keycode) |         public bool IsReleased(Keycode keycode) | ||||||
|         { |         { | ||||||
|             var key = Keys[(int)keycode]; |             return Keys[(int)keycode].IsReleased; | ||||||
|             return key.InputState == ButtonState.Released; |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -4,9 +4,9 @@ namespace MoonWorks.Input | ||||||
| { | { | ||||||
|     public class Mouse |     public class Mouse | ||||||
|     { |     { | ||||||
|         public ButtonState LeftButton { get; private set; } |         public ButtonState LeftButton { get; } = new ButtonState(); | ||||||
|         public ButtonState MiddleButton { get; private set; } |         public ButtonState MiddleButton { get; } = new ButtonState(); | ||||||
|         public ButtonState RightButton { get; private set; } |         public ButtonState RightButton { get; } = new ButtonState(); | ||||||
| 
 | 
 | ||||||
|         public int X { get; private set; } |         public int X { get; private set; } | ||||||
|         public int Y { get; private set; } |         public int Y { get; private set; } | ||||||
|  | @ -30,7 +30,7 @@ namespace MoonWorks.Input | ||||||
| 
 | 
 | ||||||
|         internal void Update() |         internal void Update() | ||||||
|         { |         { | ||||||
|             var buttons = SDL.SDL_GetMouseState(out var x, out var y); |             var buttonMask = SDL.SDL_GetMouseState(out var x, out var y); | ||||||
|             var _ = SDL.SDL_GetRelativeMouseState(out var deltaX, out var deltaY); |             var _ = SDL.SDL_GetRelativeMouseState(out var deltaX, out var deltaY); | ||||||
| 
 | 
 | ||||||
|             X = x; |             X = x; | ||||||
|  | @ -38,28 +38,14 @@ namespace MoonWorks.Input | ||||||
|             DeltaX = deltaX; |             DeltaX = deltaX; | ||||||
|             DeltaY = deltaY; |             DeltaY = deltaY; | ||||||
| 
 | 
 | ||||||
|             LeftButton = UpdateState(LeftButton, buttons, SDL.SDL_BUTTON_LMASK); |             LeftButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_LMASK)); | ||||||
|             MiddleButton = UpdateState(MiddleButton, buttons, SDL.SDL_BUTTON_MMASK); |             MiddleButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_MMASK)); | ||||||
|             RightButton = UpdateState(RightButton, buttons, SDL.SDL_BUTTON_RMASK); |             RightButton.Update(IsPressed(buttonMask, SDL.SDL_BUTTON_RMASK)); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         private ButtonState UpdateState(ButtonState state, uint buttonMask, uint buttonFlag) |         private bool IsPressed(uint buttonMask, uint buttonFlag) | ||||||
|         { |         { | ||||||
|             var isPressed = buttonMask & buttonFlag; |             return (buttonMask & buttonFlag) != 0; | ||||||
| 
 |  | ||||||
|             if (isPressed != 0) |  | ||||||
|             { |  | ||||||
|                 if (state == ButtonState.Pressed) |  | ||||||
|                 { |  | ||||||
|                     return ButtonState.Held; |  | ||||||
|                 } |  | ||||||
|                 else if (state == ButtonState.Released) |  | ||||||
|                 { |  | ||||||
|                     return ButtonState.Pressed; |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             return ButtonState.Released; |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue