/// <summary>Converts the provided joystick state into a game pad state</summary>
    /// <param name="joystickState">Joystick state that will be converted</param>
    /// <returns>A game pad state matching the provided joystick state</returns>
    public GamePadState Convert(ref JoystickState joystickState) {
      this.convertDelegate(this.gamePadState, ref joystickState);

      return new GamePadState(
        new GamePadThumbSticks(
          this.gamePadState.LeftThumbStick,
          this.gamePadState.RightThumbStick
        ),
        new GamePadTriggers(
          this.gamePadState.LeftTrigger,
          this.gamePadState.RightTrigger
        ),
        new XnaGamePadButtons(
          this.gamePadState.Buttons
        ),
        new GamePadDPad(
          (this.gamePadState.Buttons & Buttons.DPadUp) != 0 ?
            ButtonState.Pressed : ButtonState.Released,
          (this.gamePadState.Buttons & Buttons.DPadDown) != 0 ?
            ButtonState.Pressed : ButtonState.Released,
          (this.gamePadState.Buttons & Buttons.DPadLeft) != 0 ?
            ButtonState.Pressed : ButtonState.Released,
          (this.gamePadState.Buttons & Buttons.DPadRight) != 0 ?
            ButtonState.Pressed : ButtonState.Released
        )
      );
    }
 protected override int Read(ref JoystickState state)
 {
     return state.AccelerationY;
 }
 protected override int Read(ref JoystickState state)
 {
     return state.Z;
 }
 protected override int Read(ref JoystickState state)
 {
     return state.VelocityY;
 }
 /// <summary>Reads the raw value from the joystick state</summary>
 /// <param name="state">Joystick state the value is read from</param>
 /// <returns>The raw value of the axis in the joystick state</returns>
 protected abstract int Read(ref JoystickState state);
            /// <summary>Retrieves the current value of the axis</summary>
            /// <param name="state">Joystick state the axis is taken from</param>
            /// <returns>The value of the axis in the joystick state</returns>
            public float GetValue(ref JoystickState state)
            {
                int raw = Read(ref state);

                if (raw < this.center) {
                  return (float)(this.center - raw) / this.min;
                } else {
                  return (float)(raw - this.center) / this.max;
                }
            }
 protected override int Read(ref JoystickState state)
 {
     return state.AngularVelocityZ;
 }
        /// <summary>Converts a joystick state into an XNA game pad state</summary>
        /// <param name="joystickState">Joystick state that will be converted</param>
        /// <returns>The equivalent XNA game pad state</returns>
        internal GamePadState Convert(ref JoystickState joystickState)
        {
            // Read the current states of both thumb sticks
              Vector2 leftThumbstick, rightThumbstick;
              {
            if (this.axisReaders[0] != null) {
              leftThumbstick.X = this.axisReaders[0].GetValue(ref joystickState);
            } else {
              leftThumbstick.X = 0.0f;
            }
            if (this.axisReaders[1] != null) {
              leftThumbstick.Y = -this.axisReaders[1].GetValue(ref joystickState);
            } else {
              leftThumbstick.Y = 0.0f;
            }

            if (this.axisReaders[12] != null) {
              rightThumbstick.X = this.axisReaders[12].GetValue(ref joystickState);
            } else {
              rightThumbstick.X = 0.0f;
            }
            if (this.axisReaders[13] != null) {
              rightThumbstick.Y = -this.axisReaders[13].GetValue(ref joystickState);
            } else {
              rightThumbstick.Y = 0.0f;
            }
              }

              // Read the current states of the triggers
              float leftTrigger, rightTrigger;
              {
            if (this.sliderReaders[0] != null) {
              leftTrigger = this.sliderReaders[0].GetValue(ref joystickState);
            } else {
              leftTrigger = 0.0f;
            }
            if (this.sliderReaders[1] != null) {
              rightTrigger = this.sliderReaders[1].GetValue(ref joystickState);
            } else {
              rightTrigger = 0.0f;
            }
              }

              // Convert the button states
              Buttons buttons = 0;
              {
            Buttons[] buttonOrder = ExtendedGamePadState.ButtonOrder;
            bool[] buttonPressed = joystickState.GetButtons();

            int count = Math.Min(buttonOrder.Length, this.buttonCount);
            for (int index = 0; index < count; ++index) {
              if (buttonPressed[index]) {
            buttons |= buttonOrder[index];
              }
            }
              }

              // Convert the first PoV controller into a directional pad
              GamePadDPad dpad;
              {
            if (this.povCount > 0) {
              int[] povs = joystickState.GetPointOfViewControllers();
              dpad = ExtendedGamePadState.DpadFromPov(povs[0]);
            } else {
              dpad = new GamePadDPad();
            }
              }

              // All informations gathered, construct the game pad state
              return new GamePadState(
            new GamePadThumbSticks(leftThumbstick, rightThumbstick),
            new GamePadTriggers(leftTrigger, rightTrigger),
            new XnaGamePadButtons(buttons),
            dpad
              );
        }
Beispiel #9
0
        public float AxisPressed(string axis, JoystickState state)
        {
            var type = Read(axis);

            return(type.index != -1 ? state.Axes[type.index] : 0f);
        }
        /// <summary>Generates events for the changes between two states</summary>
        /// <param name="previous">Previous state for the comparison</param>
        /// <param name="current">Current state for the comparison</param>
        private void generateEvents(JoystickState previous, JoystickState current)
        {
            bool haveExtendedEventSubscribers = HaveExtendedEventSubscribers;
              if (!HaveEventSubscribers && !haveExtendedEventSubscribers) {
            return;
              }

              bool[] previousButtons = previous.GetButtons();
              bool[] currentButtons = current.GetButtons();

              if (haveExtendedEventSubscribers) {
            generateAllEvents(previousButtons, currentButtons);
              } else {
            generateStandardEventsOnly(previousButtons, currentButtons);
              }
        }
    /// <summary>Converts all digital buttons and the controller's PoV hat</summary>
    /// <param name="gamePadState">Game pad state that will receive the results</param>
    /// <param name="joystickState">Joystick state the values are taken from</param>
    private void convertButtonsAndPov(
      FlatGamePadState gamePadState, ref JoystickState joystickState
    ) {
      Buttons pressedButtons = 0;

      // Try to match up the joystick's buttons with those that would be on
      // an XBox 360 controller.
      bool[] buttons = joystickState.GetButtons();
      for (int index = 0; index < this.buttonCount; ++index) {
        if (buttons[index]) {
          pressedButtons |= buttonOrder[index];
        }
      }

      // If this controller has a Point-of-View hat, we interpret the hat as
      // 4 additional buttons. If not, the buttonCount property is limited to
      // 14, allowing an additional 4 normal buttons on the controller to act
      // as if they were a PoV hat.
      if (this.hasPovHat) {
        int pov = joystickState.GetPointOfViewControllers()[0];

        // PoV hats report either -1 or 65535 when they're centered and their
        // position in degrees times 100 if they're not centered.
        if ((ushort)(pov) != 0xFFFF) {
          bool right = ((pov > 0) && (pov < 18000));
          bool down = ((pov > 9000) && (pov < 27000));
          bool left = ((pov > 18000) && (pov < 36000));
          bool up = ((pov > 27000) || (pov < 9000));

          if (right) { pressedButtons |= Buttons.DPadRight; }
          if (down) { pressedButtons |= Buttons.DPadDown; }
          if (left) { pressedButtons |= Buttons.DPadLeft; }
          if (up) { pressedButtons |= Buttons.DPadUp; }
        }
      }

      this.gamePadState.Buttons = pressedButtons;
    }
        /// <summary>
        ///   Ensures that another slot if available in the joystick state queue
        /// </summary>
        private void ensureSlotAvailable()
        {
            this.states.EnsureSlotAvailable();

              JoystickState[] items = this.states.Items;
              int tailIndex = this.states.TailIndex;
              if (items[tailIndex] == null) {
            items[tailIndex] = new JoystickState();
              }
        }
 protected override int Read(ref JoystickState state)
 {
     return state.GetForceSliders()[base.Index];
 }
 protected override int Read(ref JoystickState state)
 {
     return state.GetAccelerationSliders()[base.Index];
 }
 /// <summary>Reads the raw value from the joystick state</summary>
 /// <param name="state">Joystick state the value is read from</param>
 /// <returns>The raw value of the axis in the joystick state</returns>
 protected virtual int Read(ref JoystickState state)
 {
     return state.GetSliders()[this.Index];
 }
            /// <summary>Retrieves the current value of the axis</summary>
            /// <param name="state">Joystick state the axis is taken from</param>
            /// <returns>The value of the axis in the joystick state</returns>
            public float GetValue(ref JoystickState state)
            {
                int raw = Read(ref state);

                return (float)(raw - min) / this.range;
            }