/// <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.IsNewButtonPress; keyTest = state.IsNewKeyPress; } // 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; }
/// <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> /// Polls input states and handles player input. /// </summary> public override void HandleInput(InputState input) { int playerIndex = (int)ControllingPlayer.Value; keyboardState = input.CurrentKeyboardStates[playerIndex]; gamePadState = input.CurrentGamePadStates[playerIndex]; touchState = TouchPanel.GetState(); accelerometerState = Accelerometer.GetState(); if (level.Player.Alive && level.EndReached) LoadNextLevel(); if (!level.Player.Alive) if (level.Player.Lives > 0) level.NewLife(); else { ScreenManager.AddScreen(new GameOverScreen(), ControllingPlayer); ReloadLevel(); } bool gamePadDisconnected = !gamePadState.IsConnected && input.GamePadWasConnected[playerIndex]; if (input.GamePadWasConnected[playerIndex]) controlCheck.wasConnected = true; if (input.IsPauseGame(ControllingPlayer) || gamePadDisconnected) { ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer); } if (!gamePadState.IsConnected) { controlCheck.wasConnected = false; input.GamePadWasConnected[playerIndex] = false; } }
/// <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> /// Polls input states and handles player input. /// </summary> public override void HandleInput(InputState input) { int playerIndex = (int)ControllingPlayer.Value; keyboardState = input.CurrentKeyboardStates[playerIndex]; gamePadState = input.CurrentGamePadStates[playerIndex]; //if (keyboardState.IsKeyDown(Keys.Escape) || // gamePadState.Buttons.Back == ButtonState.Pressed) //this.Exit(); if (level.Player.Alive && level.EndReached) LoadNextLevel(); if (!level.Player.Alive) if (level.Player.Lives > 0) level.NewLife(); else { ScreenManager.AddScreen(new GameOverScreen(), ControllingPlayer); ReloadLevel(); } bool gamePadDisconnected = !gamePadState.IsConnected && input.GamePadWasConnected[playerIndex]; if (input.IsPauseGame(ControllingPlayer) || gamePadDisconnected) { ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer); } }
public override void HandleInput(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(input); }