public void Update(bool hasFocus) { m_previousGamepadStateMaybe = m_currentGamepadStateMaybe; m_currentGamepadStateMaybe = null; if (hasFocus) { if (m_padIndex != null) { GamePadState testState = GamePad.GetState((PlayerIndex)m_padIndex); if (testState.IsConnected) { m_currentGamepadStateMaybe = testState; } } // Get any input else { for (int i = 0; i < 1; ++i) { GamePadState testState = GamePad.GetState((PlayerIndex)i); if (testState.IsConnected) { m_currentGamepadStateMaybe = testState; break; } } } } }
private static bool IsGamePadConnected(int i) { GamePadState?state = null; try { state = GamePad.GetState((PlayerIndex)i); } catch { } return(state.HasValue && state.Value.IsConnected); }
public static GamePadState GetState(PlayerIndex playerIndex, GamePadDeadZone deadZoneMode) { GamePadState?result = null; try { if (!IsKeyboardPlayerIndex(playerIndex)) { result = GamePad.GetState(playerIndex, deadZoneMode); } else { var keyboardState = Keyboard.GetState(); var pressedButtons = new List <Buttons>(); foreach (Keys key in keyboardState.GetPressedKeys()) { if (GamePadEx.KeyMappings.ContainsKey(key)) { pressedButtons.Add(GamePadEx.KeyMappings[key]); } } var leftThumbstick = Vector2.Zero; if (pressedButtons.Contains(Buttons.DPadUp)) { leftThumbstick.Y = THUMBSTICK_MAX; } else if (pressedButtons.Contains(Buttons.DPadDown)) { leftThumbstick.Y = THUMBSTICK_MIN; } if (pressedButtons.Contains(Buttons.DPadRight)) { leftThumbstick.X = THUMBSTICK_MAX; } else if (pressedButtons.Contains(Buttons.DPadLeft)) { leftThumbstick.X = THUMBSTICK_MIN; } Buttons buttons = 0; foreach (var btn in pressedButtons) { buttons = buttons | btn; } result = new GamePadState( leftThumbstick, Vector2.Zero, // Right Thumbstick 0.0f, // Left Trigger 0.0f, // Right Trigger buttons); // pressedButtons[0]); // .ToArray()); // TODO: FIX ME!!!! } } catch { } return(result.HasValue ? result.Value : GamePadState.Default); }
public InputControllerImpl(Sprint3 game, PlayerIndex?playerIndex) : base(game) { this.previousStateK = Keyboard.GetState(); if (playerIndex != null) { this.previousStateG = GamePad.GetState(playerIndex.Value); } if (playerIndex.HasValue) { this.playerIndex = playerIndex.Value; } }
protected override void OnUpdate() { base.OnUpdate(); var state = GamePad.GetState(PlayerIndex.One); var state2 = GamePad.GetState(PlayerIndex.Two); var state3 = GamePad.GetState(PlayerIndex.Three); var state4 = GamePad.GetState(PlayerIndex.Four); bool thumbStickPressed = state.Buttons.RightStick == ButtonState.Pressed; if (state.Triggers.Left > 0.5f || state.Triggers.Right > 0.5f) { if (state.DPad.Up == ButtonState.Pressed) { HandlePressUp(state.Triggers.Left <= 0.5f); } else if (state.DPad.Down == ButtonState.Pressed) { HandlePressDown(state.Triggers.Left <= 0.5f); } if (state.DPad.Right == ButtonState.Pressed) { HandlePressRight(); } else if (state.DPad.Left == ButtonState.Pressed) { HandlePressLeft(); } } if (thumbStickPressed) { if (_RightStickPressTime == null) { _RightStickPressTime = Time.time; } if (Time.time - _RightStickPressTime > TIME_TO_RECENTER) { (VR.Mode as VookaSeatedMode).Recenter(); _RightStickPressTime = null; } } else { _RightStickPressTime = null; } _PrevState = state; }
/// <summary>Get the equivalent state.</summary> public GamePadState GetState() { if (this.State == null) { this.State = new GamePadState( leftThumbStick: this.LeftStickPos, rightThumbStick: this.RightStickPos, leftTrigger: this.LeftTrigger, rightTrigger: this.RightTrigger, buttons: this.GetButtonBitmask() // MonoGame requires one bitmask here; don't specify multiple values ); } return(this.State.Value); }
public void FixedUpdate() { GamePadState state = GamePad.GetState(PlayerIndex.One); if (_PrevState != null && state.IsConnected) { foreach (var consumer in _Consumers) { if (consumer(state, _PrevState.Value)) { break; } } } _PrevState = state; }
private CursorInputArgs(GamePadState?gamePadState, MouseState?mouseState, KeyboardState?keyboardState) { if (gamePadState.HasValue) { pressedGamePadButtons = allGamePadButtons.Where(b => gamePadState.Value.IsButtonDown(b)).ToArray(); inputSource = InputSource.Gamepad; } else if (mouseState.HasValue) { List <MouseButtons> pressedButtons = new List <MouseButtons>(); if (mouseState.Value.LeftButton == ButtonState.Pressed) { pressedButtons.Add(MouseButtons.LeftButton); } if (mouseState.Value.MiddleButton == ButtonState.Pressed) { pressedButtons.Add(MouseButtons.MiddleButton); } if (mouseState.Value.RightButton == ButtonState.Pressed) { pressedButtons.Add(MouseButtons.RightButton); } if (mouseState.Value.XButton1 == ButtonState.Pressed) { pressedButtons.Add(MouseButtons.XButton1); } if (mouseState.Value.XButton2 == ButtonState.Pressed) { pressedButtons.Add(MouseButtons.XButton2); } pressedMouseButtons = pressedButtons.ToArray(); inputSource = InputSource.Mouse; } else if (keyboardState.HasValue) { pressedKeys = allKeys.Where(k => keyboardState.Value.IsKeyDown(k)).ToArray(); inputSource = InputSource.Keyboard; } }
/// <summary>Reset the tracked state.</summary> /// <param name="state">The state from which to reset, or <c>null</c> to get the latest state.</param> public GamePadStateBuilder Reset(GamePadState?state = null) { this.State = state ??= GamePad.GetState(PlayerIndex.One); this.IsConnected = state.Value.IsConnected; if (!this.IsConnected) { return(this); } GamePadDPad pad = state.Value.DPad; GamePadButtons buttons = state.Value.Buttons; GamePadTriggers triggers = state.Value.Triggers; GamePadThumbSticks sticks = state.Value.ThumbSticks; this.ButtonStates = new Dictionary <SButton, ButtonState> { [SButton.DPadUp] = pad.Up, [SButton.DPadDown] = pad.Down, [SButton.DPadLeft] = pad.Left, [SButton.DPadRight] = pad.Right, [SButton.ControllerA] = buttons.A, [SButton.ControllerB] = buttons.B, [SButton.ControllerX] = buttons.X, [SButton.ControllerY] = buttons.Y, [SButton.LeftStick] = buttons.LeftStick, [SButton.RightStick] = buttons.RightStick, [SButton.LeftShoulder] = buttons.LeftShoulder, [SButton.RightShoulder] = buttons.RightShoulder, [SButton.ControllerBack] = buttons.Back, [SButton.ControllerStart] = buttons.Start, [SButton.BigButton] = buttons.BigButton }; this.LeftTrigger = triggers.Left; this.RightTrigger = triggers.Right; this.LeftStickPos = sticks.Left; this.RightStickPos = sticks.Right; return(this); }
/// <summary>Override the states for a set of buttons.</summary> /// <param name="overrides">The button state overrides.</param> public GamePadStateBuilder OverrideButtons(IDictionary <SButton, SButtonState> overrides) { if (!this.IsConnected) { return(this); } foreach (var pair in overrides) { bool changed = true; bool isDown = pair.Value.IsDown(); switch (pair.Key) { // left thumbstick case SButton.LeftThumbstickUp: this.LeftStickPos.Y = isDown ? 1 : 0; break; case SButton.LeftThumbstickDown: this.LeftStickPos.Y = isDown ? 1 : 0; break; case SButton.LeftThumbstickLeft: this.LeftStickPos.X = isDown ? 1 : 0; break; case SButton.LeftThumbstickRight: this.LeftStickPos.X = isDown ? 1 : 0; break; // right thumbstick case SButton.RightThumbstickUp: this.RightStickPos.Y = isDown ? 1 : 0; break; case SButton.RightThumbstickDown: this.RightStickPos.Y = isDown ? 1 : 0; break; case SButton.RightThumbstickLeft: this.RightStickPos.X = isDown ? 1 : 0; break; case SButton.RightThumbstickRight: this.RightStickPos.X = isDown ? 1 : 0; break; // triggers case SButton.LeftTrigger: this.LeftTrigger = isDown ? 1 : 0; break; case SButton.RightTrigger: this.RightTrigger = isDown ? 1 : 0; break; // buttons default: if (this.ButtonStates.ContainsKey(pair.Key)) { this.ButtonStates[pair.Key] = isDown ? ButtonState.Pressed : ButtonState.Released; } else { changed = false; } break; } if (changed) { this.State = null; } } return(this); }
// Start is called before the first frame update void Start() { jumped = false; state = null; prevState = null; }
/********* ** Public methods *********/ /// <summary>Construct an instance.</summary> /// <param name="state">The initial state, or <c>null</c> to get the latest state.</param> public GamePadStateBuilder(GamePadState?state = null) { this.Reset(state); }
public void Update() { if (ControlsDisabled) { foreach (Keys key in pressedKeys.Keys.ToList()) { pressedKeys[key] = false; } foreach (Buttons button in pressedButtons.Keys.ToList()) { pressedButtons[button] = false; } prevGamepadState = null; prevKeyboardState = null; return; } currentKeyboardState = Keyboard.GetState(); mouseState = Mouse.GetState(); currentGamepadState = GamePad.GetState(PlayerIndex.One); foreach (KeyValuePair <KeyMapping, Action <Vector2> > mapping in keyPressActions) { Keys?key = mapping.Key.Key; if (key.HasValue) { if (currentKeyboardState.IsKeyDown(key.Value)) { if (Timer.IsSet("INPUTPRESSED_" + key.Value.ToString())) { continue; } if (mapping.Key.PressCooldown != 0) { Timer.SetTimer("INPUTPRESSED_" + key.Value.ToString(), mapping.Key.PressCooldown); } if (mapping.Key.SinglePressOnly && (prevKeyboardState != null && (prevKeyboardState == currentKeyboardState || pressedKeys[key.Value]))) { continue; } pressedKeys[key.Value] = true; mapping.Value.Invoke(Vector2.Zero); } else { if (pressedKeys[key.Value] && keyReleaseActions.ContainsKey(key.Value)) { keyReleaseActions[key.Value].Invoke(); } pressedKeys[key.Value] = false; } } Buttons?button = mapping.Key.Button; if (button.HasValue) { if (currentGamepadState.IsButtonDown(button.Value)) { if (mapping.Key.SinglePressOnly && (prevGamepadState != null && (prevGamepadState == currentGamepadState || pressedButtons[button.Value]))) { continue; } if (Timer.IsSet("INPUTPRESSED_" + button.Value.ToString())) { continue; } if (mapping.Key.PressCooldown != 0) { Timer.SetTimer("INPUTPRESSED_" + button.Value.ToString(), mapping.Key.PressCooldown); } pressedButtons[button.Value] = true; if (button.Value == Buttons.LeftThumbstickLeft || button.Value == Buttons.LeftThumbstickRight) { leftThumbstick.X = currentGamepadState.ThumbSticks.Left.X; mapping.Value.Invoke(leftThumbstick); } else if (button.Value == Buttons.LeftThumbstickUp || button.Value == Buttons.LeftThumbstickDown) { leftThumbstick.Y = currentGamepadState.ThumbSticks.Left.Y; mapping.Value.Invoke(leftThumbstick); } else if (button.Value == Buttons.RightThumbstickLeft || button.Value == Buttons.RightThumbstickRight) { rightThumbStick.X = currentGamepadState.ThumbSticks.Right.X; mapping.Value.Invoke(rightThumbStick); } else if (button.Value == Buttons.RightThumbstickUp || button.Value == Buttons.RightThumbstickDown) { rightThumbStick.Y = currentGamepadState.ThumbSticks.Right.Y; mapping.Value.Invoke(rightThumbStick); } else { mapping.Value.Invoke(Vector2.Zero); } } else { if (pressedButtons[button.Value] && buttonReleaseActions.ContainsKey(button.Value)) { buttonReleaseActions[button.Value].Invoke(); } pressedButtons[button.Value] = false; } } } if (mouseState.ScrollWheelValue > prevMouseScrollWheelValue) { if (mouseWheelUpAction != null && (mouseState.ScrollWheelValue - prevMouseScrollWheelValue) >= scrollThreshold) { mouseWheelUpAction.Invoke(); prevMouseScrollWheelValue = mouseState.ScrollWheelValue; } } else if (mouseState.ScrollWheelValue < prevMouseScrollWheelValue) { if (mouseWheelDownAction != null && (prevMouseScrollWheelValue - mouseState.ScrollWheelValue) >= scrollThreshold) { mouseWheelDownAction.Invoke(); prevMouseScrollWheelValue = mouseState.ScrollWheelValue; } } if (prevMouseState?.LeftButton != ButtonState.Pressed && mouseState.LeftButton == ButtonState.Pressed) { LeftClickDownAction?.Invoke(mouseState.Position.ToVector2()); } else if (prevMouseState?.LeftButton == ButtonState.Pressed && mouseState.LeftButton != ButtonState.Pressed) { LeftClickUpAction?.Invoke(mouseState.Position.ToVector2()); } if (mouseState.LeftButton == ButtonState.Pressed) { LeftClickPressedAction?.Invoke(mouseState.Position.ToVector2()); } if (prevMouseState?.RightButton != ButtonState.Pressed && mouseState.RightButton == ButtonState.Pressed) { RightClickDownAction?.Invoke(mouseState.Position.ToVector2()); } else if (prevMouseState?.RightButton == ButtonState.Pressed && mouseState.RightButton != ButtonState.Pressed) { RightClickUpAction?.Invoke(mouseState.Position.ToVector2()); } if (mouseState.RightButton == ButtonState.Pressed) { RightClickPressedAction?.Invoke(mouseState.Position.ToVector2()); } if (prevMouseState != null && prevMouseState?.Position != mouseState.Position) { MouseMovedAction?.Invoke(prevMouseState.Value.Position.ToVector2(), mouseState.Position.ToVector2()); } prevKeyboardState = currentKeyboardState; prevGamepadState = currentGamepadState; prevMouseState = mouseState; }
// Device specific bool GetButton(Button button, GamePadState?gamepadStateMaybe) { if (gamepadStateMaybe == null) { return(false); } GamePadState gamepadState = (GamePadState)gamepadStateMaybe; const ButtonState PRESSED_STATE = ButtonState.Pressed; switch (button) { case (Button.A): return(gamepadState.Buttons.A == PRESSED_STATE); case (Button.B): return(gamepadState.Buttons.B == PRESSED_STATE); case (Button.X): return(gamepadState.Buttons.X == PRESSED_STATE); case (Button.Y): return(gamepadState.Buttons.Y == PRESSED_STATE); case (Button.LB): return(gamepadState.Buttons.LeftShoulder == PRESSED_STATE); case (Button.RB): return(gamepadState.Buttons.RightShoulder == PRESSED_STATE); case (Button.L3): return(gamepadState.Buttons.LeftStick == PRESSED_STATE); case (Button.R3): return(gamepadState.Buttons.RightStick == PRESSED_STATE); case (Button.Start): return(gamepadState.Buttons.Start == PRESSED_STATE); case (Button.Select): return(gamepadState.Buttons.Back == PRESSED_STATE); case (Button.DPadUp): return(gamepadState.DPad.Up == PRESSED_STATE); case (Button.DPadDown): return(gamepadState.DPad.Down == PRESSED_STATE); case (Button.DPadLeft): return(gamepadState.DPad.Left == PRESSED_STATE); case (Button.DPadRight): return(gamepadState.DPad.Right == PRESSED_STATE); default: Debug.LogError("Unhandled input button type " + button); break; } return(false); }
/// <summary> /// Execute commands based on the keys and gamepad informtion sent to the InputComponent /// </summary> /// <param name="ientity">IEntity this component belongs to.</param> /// <param name="keyboardState">Current keyboard state</param> /// <param name="gamePadState">Current gamepad state</param> /// <param name="timespan">time of update trigger</param> public void Update(IEntity ientity, KeyboardState?keyboardState, GamePadState?gamePadState, TimeSpan?timespan) { //Stop if no keyboard or gamepad data if (!keyboardState.HasValue && !gamePadState.HasValue) { return; } //if KeyboardState data passed if (keyboardState.HasValue) { var keys = keyboardState.Value.GetPressedKeys(); //Get keys pressed //Run all commands that keys are pressed for. foreach (var k in keys) { this.KeyboardInputManager.AssignedCommands[k]?.Execute(ientity, null, timespan); } } //if GamePadStateData is passed if (gamePadState.HasValue) { var lthumb = gamePadState.Value.ThumbSticks.Left; //Left thumbstick data var rthumb = gamePadState.Value.ThumbSticks.Right; //Right thumbstick data var ltrigger = gamePadState.Value.Triggers.Left; //Left trigger data var rtrigger = gamePadState.Value.Triggers.Right; //Right trigger data //Execute Left thumbstick commands if (lthumb.X > GamePadInputManager.DeadZonePX) { GamePadInputManager.StickOneLeft?.Execute(ientity, gamePadState, timespan); } if (lthumb.X < GamePadInputManager.DeadZoneNX) { GamePadInputManager.StickOneRight?.Execute(ientity, gamePadState, timespan); } if (lthumb.Y > GamePadInputManager.DeadZonePY) { GamePadInputManager.StickOneUp?.Execute(ientity, gamePadState, timespan); } if (lthumb.Y < GamePadInputManager.DeadZoneNY) { GamePadInputManager.StickOneDown?.Execute(ientity, gamePadState, timespan); } //Execute Right thumbstick commands if (rthumb.X > GamePadInputManager.DeadZonePX) { GamePadInputManager.StickTwoLeft?.Execute(ientity, gamePadState, timespan); } if (rthumb.X < GamePadInputManager.DeadZoneNX) { GamePadInputManager.StickTwoRight?.Execute(ientity, gamePadState, timespan); } if (rthumb.Y > GamePadInputManager.DeadZonePY) { GamePadInputManager.StickTwoUp?.Execute(ientity, gamePadState, timespan); } if (rthumb.Y < GamePadInputManager.DeadZoneNY) { GamePadInputManager.StickTwoDown?.Execute(ientity, gamePadState, timespan); } //Execute trigger commands if (ltrigger > GamePadInputManager.DeadZoneLeftTrigger) { GamePadInputManager.LeftTrigger?.Execute(ientity, gamePadState, timespan); } if (rtrigger > GamePadInputManager.DeadZoneRightTrigger) { GamePadInputManager.RightTrigger?.Execute(ientity, gamePadState, timespan); } //Execute button commands if (gamePadState.Value.IsButtonDown(Buttons.A)) { GamePadInputManager.AssignedCommands[Buttons.A]?.Execute(ientity, null, timespan); } if (gamePadState.Value.IsButtonDown(Buttons.B)) { GamePadInputManager.AssignedCommands[Buttons.B]?.Execute(ientity, null, timespan); } if (gamePadState.Value.IsButtonDown(Buttons.Back)) { GamePadInputManager.AssignedCommands[Buttons.Back]?.Execute(ientity, null, timespan); } if (gamePadState.Value.IsButtonDown(Buttons.BigButton)) { GamePadInputManager.AssignedCommands[Buttons.BigButton]?.Execute(ientity, null, timespan); } if (gamePadState.Value.IsButtonDown(Buttons.DPadDown)) { GamePadInputManager.AssignedCommands[Buttons.DPadDown]?.Execute(ientity, null, timespan); } if (gamePadState.Value.IsButtonDown(Buttons.DPadUp)) { GamePadInputManager.AssignedCommands[Buttons.DPadUp]?.Execute(ientity, null, timespan); } if (gamePadState.Value.IsButtonDown(Buttons.DPadLeft)) { GamePadInputManager.AssignedCommands[Buttons.DPadLeft]?.Execute(ientity, null, timespan); } if (gamePadState.Value.IsButtonDown(Buttons.DPadRight)) { GamePadInputManager.AssignedCommands[Buttons.DPadRight]?.Execute(ientity, null, timespan); } if (gamePadState.Value.IsButtonDown(Buttons.LeftShoulder)) { GamePadInputManager.AssignedCommands[Buttons.LeftShoulder]?.Execute(ientity, null, timespan); } if (gamePadState.Value.IsButtonDown(Buttons.LeftStick)) { GamePadInputManager.AssignedCommands[Buttons.LeftStick]?.Execute(ientity, null, timespan); } if (gamePadState.Value.IsButtonDown(Buttons.RightShoulder)) { GamePadInputManager.AssignedCommands[Buttons.RightShoulder]?.Execute(ientity, null, timespan); } if (gamePadState.Value.IsButtonDown(Buttons.RightStick)) { GamePadInputManager.AssignedCommands[Buttons.RightStick]?.Execute(ientity, null, timespan); } if (gamePadState.Value.IsButtonDown(Buttons.Start)) { GamePadInputManager.AssignedCommands[Buttons.Start]?.Execute(ientity, null, timespan); } if (gamePadState.Value.IsButtonDown(Buttons.X)) { GamePadInputManager.AssignedCommands[Buttons.X]?.Execute(ientity, null, timespan); } if (gamePadState.Value.IsButtonDown(Buttons.Y)) { GamePadInputManager.AssignedCommands[Buttons.Y]?.Execute(ientity, null, timespan); } } }
public override void ScanInputs() { KeyboardState currentStateK = Keyboard.GetState(); GamePadState? currentStateG = null; if (this.playerIndex != null) { currentStateG = GamePad.GetState(this.playerIndex.Value); } List <Keys> keys = new List <Keys>(); List <Keys> keysPrevious = new List <Keys>(); if (this.playerIndex == null) { foreach (Keys k in currentStateK.GetPressedKeys()) { keys.Add(k); } } else { foreach (Keys k in this.mirrors.Keys) { if (currentStateG.Value.IsButtonDown(this.mirrors[k])) { keys.Add(k); } } } if (this.playerIndex == null) { foreach (Keys k in previousStateK.GetPressedKeys()) { keysPrevious.Add(k); } } else { foreach (Keys k in this.mirrors.Keys) { if (previousStateG.Value.IsButtonDown(this.mirrors[k])) { keysPrevious.Add(k); } } } if (currentStateG.HasValue) { foreach (Keys k in this.mirrors.Keys) { if (currentStateG.Value.IsButtonDown(this.mirrors[k])) { keys.Add(k); } if (previousStateG.Value.IsButtonDown(this.mirrors[k])) { keysPrevious.Add(k); } } } foreach (Keys pressed in keys) { if (this.callbacks.ContainsKey(pressed)) { bool isDownNow = currentStateK.IsKeyDown(pressed); bool isDownPrev = previousStateK.IsKeyDown(pressed); if (this.playerIndex != null) { if (!isDownNow && mirrors.ContainsKey(pressed)) { isDownNow = currentStateG.Value.IsButtonDown(mirrors[pressed]); } if (!isDownPrev && mirrors.ContainsKey(pressed)) { isDownPrev = previousStateG.Value.IsButtonDown(mirrors[pressed]); } } if (isDownNow) { if (!isDownPrev) { if (this.callbacks.ContainsKey(pressed)) { this.callbacks[pressed].OnPress(pressed); } } else { if (this.callbacks.ContainsKey(pressed)) { this.callbacks[pressed].OnHold(pressed); } } } } } foreach (Keys pressed in keysPrevious) { if (this.callbacks.ContainsKey(pressed)) { if (this.playerIndex == null) { if (!currentStateK.IsKeyDown(pressed)) { //released this.callbacks[pressed].OnRelease(pressed); } } else if (this.mirrors.ContainsKey(pressed)) { if (!currentStateG.Value.IsButtonDown(this.mirrors[pressed])) { this.callbacks[pressed].OnRelease(pressed); } } } } this.previousStateK = currentStateK; this.previousStateG = currentStateG; }