/// <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(InputState input) { }
/// <summary> /// Responds to user input, changing the selected entry and accepting /// or cancelling the menu. /// </summary> public override void HandleInput(InputState input) { // Move to the previous menu entry? if(input.IsMenuUp(ControllingPlayer)) { selectedEntry--; if(selectedEntry < 0) selectedEntry = menuEntries.Count - 1; } // Move to the next menu entry? if(input.IsMenuDown(ControllingPlayer)) { selectedEntry++; if(selectedEntry >= menuEntries.Count) selectedEntry = 0; } // Accept or cancel the menu? We pass in our ControllingPlayer, which may // either be null (to accept input from any player) or a specific index. // If we pass a null controlling player, the InputState helper returns to // us which player actually provided the input. We pass that through to // OnSelectEntry and OnCancel, so they can tell which player triggered them. PlayerIndex playerIndex; if(input.IsMenuSelect(ControllingPlayer, out playerIndex)) { OnSelectEntry(selectedEntry, playerIndex); } else if(input.IsMenuCancel(ControllingPlayer, out playerIndex)) { OnCancel(playerIndex); } }
/// <summary> /// Responds to user input, accepting or cancelling the message box. /// </summary> public override void HandleInput(InputState input) { PlayerIndex playerIndex; // We pass in our ControllingPlayer, which may either be null (to // accept input from any player) or a specific index. If we pass a null // controlling player, the InputState helper returns to us which player // actually provided the input. We pass that through to our Accepted and // Cancelled events, so they can tell which player triggered them. if(input.IsMenuSelect(ControllingPlayer, out playerIndex)) { // Raise the accepted event, then exit the message box. if(Accepted != null) Accepted(this, new PlayerIndexEventArgs(playerIndex)); ExitScreen(); } else if(input.IsMenuCancel(ControllingPlayer, out playerIndex)) { // Raise the cancelled event, then exit the message box. if(Cancelled != null) Cancelled(this, new PlayerIndexEventArgs(playerIndex)); ExitScreen(); } }
/// <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(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]; if(input.IsPauseGame(ControllingPlayer) || gamePadDisconnected) { ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer); } else { // Otherwise move the player position. Vector2 movement = Vector2.Zero; foreach(Keys key in User.KeyMapping[Action.MoveLeft]) if(keyboardState.IsKeyDown(key)) { movement.X--; break; } foreach(Keys key in User.KeyMapping[Action.MoveRight]) if(keyboardState.IsKeyDown(key)) { movement.X++; break; } foreach(Keys key in User.KeyMapping[Action.Jump]) if(keyboardState.IsKeyDown(key)) { movement.Y--; break; } foreach(Keys key in User.KeyMapping[Action.Duck]) if(keyboardState.IsKeyDown(key)) { movement.Y++; break; } Vector2 thumbstick = gamePadState.ThumbSticks.Left; movement.X += thumbstick.X; movement.Y -= thumbstick.Y; if(movement.Length() > 1) movement.Normalize(); playerPosition += movement * 2; } }