/// <summary> /// Evaluates the action against a given InputState. /// </summary> /// <param name="state">The InputState to test for the action.</param> /// <param name="controllingPlayer">The player to test, or null to allow any player.</param> /// <param name="player">If controllingPlayer is null, this is the player that performed the action.</param> /// <returns>True if the action occurred, false otherwise.</returns> public bool Evaluate(InputState state, PlayerIndex? controllingPlayer, out PlayerIndex player) { // Figure out which delegate methods to map from the state which takes care of our "newPressOnly" logic ButtonPress buttonTest; KeyPress keyTest; if (newPressOnly) { buttonTest = state.IsNewButtonPress; keyTest = state.IsNewKeyPress; } else { buttonTest = state.IsButtonPressed; keyTest = state.IsKeyPressed; } // Now we simply need to invoke the appropriate methods for each button and key in our collections foreach (Buttons button in buttons) { if (buttonTest(button, controllingPlayer, out player)) return true; } foreach (Keys key in keys) { if (keyTest(key, controllingPlayer, out player)) return true; } // If we got here, the action is not matched player = PlayerIndex.One; return false; }
public override void HandleInput(GameTime gameTime, InputState input) { // Test for the menuCancel action PlayerIndex player; if (menuCancel.Evaluate(input, ControllingPlayer, out player)) { OnCancel(); } // Read in our gestures foreach (GestureSample gesture in input.Gestures) { // If we have a tap if (gesture.GestureType == GestureType.Tap) { // Test the tap against the buttons until one of the buttons handles the tap foreach (Button b in menuButtons) { if (b.HandleTap(gesture.Position)) break; } } } base.HandleInput(gameTime, input); }
/// <summary> /// Allows the screen to handle user input. Unlike Update, this method /// is only called when the screen is active, and not when some other /// screen has taken the focus. /// </summary> public virtual void HandleInput(GameTime gameTime, InputState input) { }
/// <summary> /// Lets the game respond to player input. Unlike the Update method, /// this will only be called when the gameplay screen is active. /// </summary> public override void HandleInput(GameTime gameTime, InputState input) { if (input == null) throw new ArgumentNullException("input"); // Look up inputs for the active player profile. int playerIndex = (int)ControllingPlayer.Value; KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex]; GamePadState gamePadState = input.CurrentGamePadStates[playerIndex]; // The game pauses either if the user presses the pause button, or if // they unplug the active gamepad. This requires us to keep track of // whether a gamepad was ever plugged in, because we don't want to pause // on PC if they are playing with a keyboard and have no gamepad at all! bool gamePadDisconnected = !gamePadState.IsConnected && input.GamePadWasConnected[playerIndex]; PlayerIndex player; if (pauseAction.Evaluate(input, ControllingPlayer, out player) || gamePadDisconnected) { #if WINDOWS_PHONE ScreenManager.AddScreen(new PhonePauseScreen(), ControllingPlayer); #else ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer); #endif } else { // Otherwise move the player position. Vector2 movement = Vector2.Zero; if (keyboardState.IsKeyDown(Keys.Left)) movement.X--; if (keyboardState.IsKeyDown(Keys.Right)) movement.X++; if (keyboardState.IsKeyDown(Keys.Up)) movement.Y--; if (keyboardState.IsKeyDown(Keys.Down)) movement.Y++; Vector2 thumbstick = gamePadState.ThumbSticks.Left; movement.X += thumbstick.X; movement.Y -= thumbstick.Y; if (input.TouchState.Count > 0) { Vector2 touchPosition = input.TouchState[0].Position; //Vector2 direction = touchPosition - playerPosition; //direction.Normalize(); //movement += direction; } if (movement.Length() > 1) movement.Normalize(); //playerPosition += movement * 8f; } }