Exemplo n.º 1
0
        public InputHandler()
        {
            int i;

            //Load up some default settings and the logical player index mapping.
            inputConfiguration   = new InputConfiguration();
            logicalPlayerMapping = new PlayerIndex?[MaxPlayers];

            //Initialize the last button information.
            lastKeyPressed    = new LastButtonInfo <Keys>();
            lastButtonPressed = new LastButtonInfo <Buttons> [MaxPlayers];

            for (i = 0; i < lastButtonPressed.Length; i++)
            {
                lastButtonPressed[i] = new LastButtonInfo <Buttons>();
            }

            pressedKeys = new List <Keys>(10);

            // Gamepads
            controllerHandled = new bool[MaxPlayers];
            prevGamePadStates = new GamePadState[MaxPlayers];
            currGamePadStates = new GamePadState[MaxPlayers];
        }
Exemplo n.º 2
0
        private void UpdateGamePads(float deltaTime)
        {
            for (int player = 0; player < MaxPlayers; player++)
            {
                PlayerIndex playerIndex = (PlayerIndex)player;

                //Update the current and previous states of the controllers...
                prevGamePadStates[player] = currGamePadStates[player];
                currGamePadStates[player] = GamePad.GetState(playerIndex, inputConfiguration.AnalogDeadZone);

                GamePadState             newGamePadState      = currGamePadStates[player];
                GamePadState             previousGamePadState = prevGamePadStates[player];
                LastButtonInfo <Buttons> lastGamePadButton    = lastButtonPressed[player];

                if (!newGamePadState.IsConnected)
                {
                    lastGamePadButton.TimePressed = 0;
                }
                else
                {
                    //Record a button if it was pressed.
                    Buttons?pressedButton = null;

                    foreach (Buttons button in buttons)
                    {
                        if (IsDown(ref newGamePadState, button) && !IsDown(ref previousGamePadState, button))
                        {
                            //Found a new button press!
                            pressedButton = button;
                            break;
                        }
                    }

                    lastGamePadButton.IsRepetitionPress = false;

                    //If we didn't find a new button press, then figure out if we need to add a button
                    //repetition if a button has been held...
                    if (!pressedButton.HasValue)
                    {
                        //If the last button pressed is still pressed then add to the timers and consider whether or
                        //not we have to generate a repetition press.
                        if (IsDown(lastGamePadButton.Button, playerIndex))
                        {
                            lastGamePadButton.TimePressed += deltaTime;

                            //Generate a repetition press if the button has been pressed for long enough.
                            if (lastGamePadButton.TimePressed >= inputConfiguration.RepetitionTimeDelay)
                            {
                                lastGamePadButton.IsRepetitionPress = true;
                                lastGamePadButton.TimePressed      -= inputConfiguration.RepetitionTimeInterval;
                            }
                        }
                        else //Last pressed button is no longer down, just reset the timer.
                        {
                            lastGamePadButton.TimePressed = 0;
                        }
                    }
                    else
                    {
                        lastGamePadButton.Button      = pressedButton.Value;
                        lastGamePadButton.TimePressed = 0;
                    }
                }
            }
        }
Exemplo n.º 3
0
    public InputManager(bool gamerServicesEnabled)
    {

      _gamerServicesEnabled = gamerServicesEnabled;



      MaxNumberOfPlayers = 4;
      int gamePadArrayLength = 4;
#else
      // MonoGame supports a different number of game pads on each platform. Currently, there is no
      // way to query the number of supported game pads. It is also different within the same
      // platform: WindowStore on desktop 4 game pads vs. WindowsStore on phone 1 game pad
      // Workaround:
      MaxNumberOfPlayers = 0;
      for (int i = 0; i < 4; i++)  // Some MonoGame platforms support up to 16 game pads but we only go up to 4.
      {
        try
        {
          GamePad.GetCapabilities(i);
          MaxNumberOfPlayers = i + 1;
        }
        catch (InvalidOperationException)
        {
          break;
        }
      }

      // Since the PlayerIndex/LogicalPlayerIndex enumeration has 4 values, we create arrays with
      // at least 4 elements. The only platforms with less than 4 gamepads are iOS (0) and 
      // WP8.1 (1).
      var gamePadArrayLength = Math.Max(4, MaxNumberOfPlayers);


      // Create default settings.
      Settings = new InputSettings();


      // Create map: logical player --> game controller.
      _logicalPlayers = new PlayerIndex?[gamePadArrayLength];


      // Create last-button-info that stores the last pressed button for double-click detection
      // and button repetition (virtual button presses).
      _lastKey = new LastButtonInfo<Keys>();
      _lastMouseButton = new LastMouseButtonInfo<MouseButtons>();

      _lastGamePadButtons = new LastButtonInfo<Buttons>[gamePadArrayLength];
      for (int i = 0; i < _lastGamePadButtons.Length; i++)
        _lastGamePadButtons[i] = new LastButtonInfo<Buttons>();


      // Keyboard
      _pressedKeys = new List<Keys>(10);                // Initial capacity: 10 (fingers)
      _pressedKeysAsReadOnly = new ReadOnlyCollection<Keys>(_pressedKeys); // Public read-only wrapper



      // Gamepads
      _areGamePadsHandled = new bool[gamePadArrayLength];
      _previousGamePadStates = new GamePadState[gamePadArrayLength];
      _newGamePadStates = new GamePadState[gamePadArrayLength];

      // Touch
      try
      {
        TouchPanel.EnabledGestures = GestureType.None;
      }
      catch (NullReferenceException exception)
      {
        // This happens in UWP interop projects. Mouse/touch/keyboard do not work in this case.
        throw new NotSupportedException(
          "MonoGame threw an exception. Please note that the DigitalRune InputManager and the " +
          "MonoGame mouse/touch/keyboard classes cannot be used in some interop scenarios.", 
          exception);
      }

      _gestures = new List<GestureSample>();


      // Input commands
      Commands = new InputCommandCollection(this);
    }