Ejemplo n.º 1
0
        /// <summary>
        ///   Initializes a new game pad state from a DirectInput joystick state
        /// </summary>
        /// <param name="converter">DirectInput converter used to fill the state</param>
        /// <param name="joystickState">
        ///   Joystick state from which the game pad state will be built
        /// </param>
        internal GamePadState(
            DirectInputConverter converter, ref JoystickState joystickState
            )
        {
            // Take over the joystick's axes
            {
                this.AvailableAxes = converter.AvailableAxes;
                DirectInputConverter.IAxisReader[] axisReaders = converter.AxisReaders;

                if (axisReaders[0] != null)
                {
                    this.X = axisReaders[0].GetValue(ref joystickState);
                }
                else
                {
                    this.X = 0.0f;
                }
                if (axisReaders[1] != null)
                {
                    this.Y = -axisReaders[1].GetValue(ref joystickState);
                }
                else
                {
                    this.Y = 0.0f;
                }
                if (axisReaders[2] != null)
                {
                    this.Z = axisReaders[2].GetValue(ref joystickState);
                }
                else
                {
                    this.Z = 0.0f;
                }

                if (axisReaders[3] != null)
                {
                    this.VelocityX = axisReaders[3].GetValue(ref joystickState);
                }
                else
                {
                    this.VelocityX = 0.0f;
                }
                if (axisReaders[4] != null)
                {
                    this.VelocityY = -axisReaders[4].GetValue(ref joystickState);
                }
                else
                {
                    this.VelocityY = 0.0f;
                }
                if (axisReaders[5] != null)
                {
                    this.VelocityZ = axisReaders[5].GetValue(ref joystickState);
                }
                else
                {
                    this.VelocityZ = 0.0f;
                }

                if (axisReaders[6] != null)
                {
                    this.AccelerationX = axisReaders[6].GetValue(ref joystickState);
                }
                else
                {
                    this.AccelerationX = 0.0f;
                }
                if (axisReaders[7] != null)
                {
                    this.AccelerationY = -axisReaders[7].GetValue(ref joystickState);
                }
                else
                {
                    this.AccelerationY = 0.0f;
                }
                if (axisReaders[8] != null)
                {
                    this.AccelerationZ = axisReaders[8].GetValue(ref joystickState);
                }
                else
                {
                    this.AccelerationZ = 0.0f;
                }

                if (axisReaders[9] != null)
                {
                    this.ForceX = axisReaders[9].GetValue(ref joystickState);
                }
                else
                {
                    this.ForceX = 0.0f;
                }
                if (axisReaders[10] != null)
                {
                    this.ForceY = -axisReaders[10].GetValue(ref joystickState);
                }
                else
                {
                    this.ForceY = 0.0f;
                }
                if (axisReaders[11] != null)
                {
                    this.ForceZ = axisReaders[11].GetValue(ref joystickState);
                }
                else
                {
                    this.ForceZ = 0.0f;
                }

                if (axisReaders[12] != null)
                {
                    this.RotationX = axisReaders[12].GetValue(ref joystickState);
                }
                else
                {
                    this.RotationX = 0.0f;
                }
                if (axisReaders[13] != null)
                {
                    this.RotationY = -axisReaders[13].GetValue(ref joystickState);
                }
                else
                {
                    this.RotationY = 0.0f;
                }
                if (axisReaders[14] != null)
                {
                    this.RotationZ = axisReaders[14].GetValue(ref joystickState);
                }
                else
                {
                    this.RotationZ = 0.0f;
                }

                if (axisReaders[15] != null)
                {
                    this.AngularVelocityX = axisReaders[15].GetValue(ref joystickState);
                }
                else
                {
                    this.AngularVelocityX = 0.0f;
                }
                if (axisReaders[16] != null)
                {
                    this.AngularVelocityY = -axisReaders[16].GetValue(ref joystickState);
                }
                else
                {
                    this.AngularVelocityY = 0.0f;
                }
                if (axisReaders[17] != null)
                {
                    this.AngularVelocityZ = axisReaders[17].GetValue(ref joystickState);
                }
                else
                {
                    this.AngularVelocityZ = 0.0f;
                }

                if (axisReaders[18] != null)
                {
                    this.AngularAccelerationX = axisReaders[18].GetValue(ref joystickState);
                }
                else
                {
                    this.AngularAccelerationX = 0.0f;
                }
                if (axisReaders[19] != null)
                {
                    this.AngularAccelerationY = -axisReaders[19].GetValue(ref joystickState);
                }
                else
                {
                    this.AngularAccelerationY = 0.0f;
                }
                if (axisReaders[20] != null)
                {
                    this.AngularAccelerationZ = axisReaders[20].GetValue(ref joystickState);
                }
                else
                {
                    this.AngularAccelerationZ = 0.0f;
                }

                if (axisReaders[21] != null)
                {
                    this.TorqueX = axisReaders[21].GetValue(ref joystickState);
                }
                else
                {
                    this.TorqueX = 0.0f;
                }
                if (axisReaders[22] != null)
                {
                    this.TorqueY = -axisReaders[22].GetValue(ref joystickState);
                }
                else
                {
                    this.TorqueY = 0.0f;
                }
                if (axisReaders[23] != null)
                {
                    this.TorqueZ = axisReaders[23].GetValue(ref joystickState);
                }
                else
                {
                    this.TorqueZ = 0.0f;
                }
            }

            // Take over the joystick's buttons
            {
                this.ButtonCount = converter.ButtonCount;
                bool[] buttonPressed = joystickState.GetButtons();

                this.buttonState1 = 0;
                for (int index = 0; index < Math.Min(64, ButtonCount); ++index)
                {
                    if (buttonPressed[index])
                    {
                        this.buttonState1 |= (1UL << index);
                    }
                }

                this.buttonState2 = 0;
                for (int index = 0; index < (ButtonCount - 64); ++index)
                {
                    if (buttonPressed[index + 64])
                    {
                        this.buttonState2 |= (1UL << index);
                    }
                }
            }

            // Take over the joystick's sliders
            {
                this.AvailableSliders = converter.AvailableSliders;
                DirectInputConverter.ISliderReader[] sliderReaders = converter.SliderReaders;

                if (sliderReaders[0] != null)
                {
                    this.Slider1 = sliderReaders[0].GetValue(ref joystickState);
                }
                else
                {
                    this.Slider1 = 0.0f;
                }
                if (sliderReaders[1] != null)
                {
                    this.Slider2 = sliderReaders[1].GetValue(ref joystickState);
                }
                else
                {
                    this.Slider2 = 0.0f;
                }

                if (sliderReaders[2] != null)
                {
                    this.VelocitySlider1 = sliderReaders[2].GetValue(ref joystickState);
                }
                else
                {
                    this.VelocitySlider1 = 0.0f;
                }
                if (sliderReaders[3] != null)
                {
                    this.VelocitySlider2 = sliderReaders[3].GetValue(ref joystickState);
                }
                else
                {
                    this.VelocitySlider2 = 0.0f;
                }

                if (sliderReaders[4] != null)
                {
                    this.AccelerationSlider1 = sliderReaders[4].GetValue(ref joystickState);
                }
                else
                {
                    this.AccelerationSlider1 = 0.0f;
                }
                if (sliderReaders[5] != null)
                {
                    this.AccelerationSlider2 = sliderReaders[5].GetValue(ref joystickState);
                }
                else
                {
                    this.AccelerationSlider2 = 0.0f;
                }

                if (sliderReaders[6] != null)
                {
                    this.ForceSlider1 = sliderReaders[6].GetValue(ref joystickState);
                }
                else
                {
                    this.ForceSlider1 = 0.0f;
                }
                if (sliderReaders[7] != null)
                {
                    this.ForceSlider2 = sliderReaders[7].GetValue(ref joystickState);
                }
                else
                {
                    this.ForceSlider2 = 0.0f;
                }
            }

            // Take over the joystick's Point-of-View controllers
            {
                this.PovCount = converter.PovCount;

                int[] povs = null;
                if (this.PovCount >= 1)
                {
                    povs      = joystickState.GetPointOfViewControllers();
                    this.Pov1 = povs[0];
                }
                else
                {
                    this.Pov1 = -1;
                }
                if (this.PovCount >= 2)
                {
                    this.Pov2 = povs[1];
                }
                else
                {
                    this.Pov2 = -1;
                }
                if (this.PovCount >= 3)
                {
                    this.Pov3 = povs[2];
                }
                else
                {
                    this.Pov3 = -1;
                }
                if (this.PovCount >= 4)
                {
                    this.Pov4 = povs[3];
                }
                else
                {
                    this.Pov4 = -1;
                }
            }
        }
    /// <summary>
    ///   Initializes a new game pad state from a DirectInput joystick state
    /// </summary>
    /// <param name="converter">DirectInput converter used to fill the state</param>
    /// <param name="joystickState">
    ///   Joystick state from which the game pad state will be built
    /// </param>
    internal GamePadState(
      DirectInputConverter converter, ref JoystickState joystickState
      )
    {
      // Take over the joystick's axes
      {
        this.AvailableAxes = converter.AvailableAxes;
        DirectInputConverter.IAxisReader[] axisReaders = converter.AxisReaders;

        if (axisReaders[0] != null)
        {
          this.X = axisReaders[0].GetValue(ref joystickState);
        }
        else
        {
          this.X = 0.0f;
        }
        if (axisReaders[1] != null)
        {
          this.Y = -axisReaders[1].GetValue(ref joystickState);
        }
        else
        {
          this.Y = 0.0f;
        }
        if (axisReaders[2] != null)
        {
          this.Z = axisReaders[2].GetValue(ref joystickState);
        }
        else
        {
          this.Z = 0.0f;
        }

        if (axisReaders[3] != null)
        {
          this.VelocityX = axisReaders[3].GetValue(ref joystickState);
        }
        else
        {
          this.VelocityX = 0.0f;
        }
        if (axisReaders[4] != null)
        {
          this.VelocityY = -axisReaders[4].GetValue(ref joystickState);
        }
        else
        {
          this.VelocityY = 0.0f;
        }
        if (axisReaders[5] != null)
        {
          this.VelocityZ = axisReaders[5].GetValue(ref joystickState);
        }
        else
        {
          this.VelocityZ = 0.0f;
        }

        if (axisReaders[6] != null)
        {
          this.AccelerationX = axisReaders[6].GetValue(ref joystickState);
        }
        else
        {
          this.AccelerationX = 0.0f;
        }
        if (axisReaders[7] != null)
        {
          this.AccelerationY = -axisReaders[7].GetValue(ref joystickState);
        }
        else
        {
          this.AccelerationY = 0.0f;
        }
        if (axisReaders[8] != null)
        {
          this.AccelerationZ = axisReaders[8].GetValue(ref joystickState);
        }
        else
        {
          this.AccelerationZ = 0.0f;
        }

        if (axisReaders[9] != null)
        {
          this.ForceX = axisReaders[9].GetValue(ref joystickState);
        }
        else
        {
          this.ForceX = 0.0f;
        }
        if (axisReaders[10] != null)
        {
          this.ForceY = -axisReaders[10].GetValue(ref joystickState);
        }
        else
        {
          this.ForceY = 0.0f;
        }
        if (axisReaders[11] != null)
        {
          this.ForceZ = axisReaders[11].GetValue(ref joystickState);
        }
        else
        {
          this.ForceZ = 0.0f;
        }

        if (axisReaders[12] != null)
        {
          this.RotationX = axisReaders[12].GetValue(ref joystickState);
        }
        else
        {
          this.RotationX = 0.0f;
        }
        if (axisReaders[13] != null)
        {
          this.RotationY = -axisReaders[13].GetValue(ref joystickState);
        }
        else
        {
          this.RotationY = 0.0f;
        }
        if (axisReaders[14] != null)
        {
          this.RotationZ = axisReaders[14].GetValue(ref joystickState);
        }
        else
        {
          this.RotationZ = 0.0f;
        }

        if (axisReaders[15] != null)
        {
          this.AngularVelocityX = axisReaders[15].GetValue(ref joystickState);
        }
        else
        {
          this.AngularVelocityX = 0.0f;
        }
        if (axisReaders[16] != null)
        {
          this.AngularVelocityY = -axisReaders[16].GetValue(ref joystickState);
        }
        else
        {
          this.AngularVelocityY = 0.0f;
        }
        if (axisReaders[17] != null)
        {
          this.AngularVelocityZ = axisReaders[17].GetValue(ref joystickState);
        }
        else
        {
          this.AngularVelocityZ = 0.0f;
        }

        if (axisReaders[18] != null)
        {
          this.AngularAccelerationX = axisReaders[18].GetValue(ref joystickState);
        }
        else
        {
          this.AngularAccelerationX = 0.0f;
        }
        if (axisReaders[19] != null)
        {
          this.AngularAccelerationY = -axisReaders[19].GetValue(ref joystickState);
        }
        else
        {
          this.AngularAccelerationY = 0.0f;
        }
        if (axisReaders[20] != null)
        {
          this.AngularAccelerationZ = axisReaders[20].GetValue(ref joystickState);
        }
        else
        {
          this.AngularAccelerationZ = 0.0f;
        }

        if (axisReaders[21] != null)
        {
          this.TorqueX = axisReaders[21].GetValue(ref joystickState);
        }
        else
        {
          this.TorqueX = 0.0f;
        }
        if (axisReaders[22] != null)
        {
          this.TorqueY = -axisReaders[22].GetValue(ref joystickState);
        }
        else
        {
          this.TorqueY = 0.0f;
        }
        if (axisReaders[23] != null)
        {
          this.TorqueZ = axisReaders[23].GetValue(ref joystickState);
        }
        else
        {
          this.TorqueZ = 0.0f;
        }
      }

      // Take over the joystick's buttons
      {
        this.ButtonCount = converter.ButtonCount;
        bool[] buttonPressed = joystickState.GetButtons();

        this.buttonState1 = 0;
        for (int index = 0; index < Math.Min(64, ButtonCount); ++index)
        {
          if (buttonPressed[index])
          {
            this.buttonState1 |= (1UL << index);
          }
        }

        this.buttonState2 = 0;
        for (int index = 0; index < (ButtonCount - 64); ++index)
        {
          if (buttonPressed[index + 64])
          {
            this.buttonState2 |= (1UL << index);
          }
        }
      }

      // Take over the joystick's sliders
      {
        this.AvailableSliders = converter.AvailableSliders;
        DirectInputConverter.ISliderReader[] sliderReaders = converter.SliderReaders;

        if (sliderReaders[0] != null)
        {
          this.Slider1 = sliderReaders[0].GetValue(ref joystickState);
        }
        else
        {
          this.Slider1 = 0.0f;
        }
        if (sliderReaders[1] != null)
        {
          this.Slider2 = sliderReaders[1].GetValue(ref joystickState);
        }
        else
        {
          this.Slider2 = 0.0f;
        }

        if (sliderReaders[2] != null)
        {
          this.VelocitySlider1 = sliderReaders[2].GetValue(ref joystickState);
        }
        else
        {
          this.VelocitySlider1 = 0.0f;
        }
        if (sliderReaders[3] != null)
        {
          this.VelocitySlider2 = sliderReaders[3].GetValue(ref joystickState);
        }
        else
        {
          this.VelocitySlider2 = 0.0f;
        }

        if (sliderReaders[4] != null)
        {
          this.AccelerationSlider1 = sliderReaders[4].GetValue(ref joystickState);
        }
        else
        {
          this.AccelerationSlider1 = 0.0f;
        }
        if (sliderReaders[5] != null)
        {
          this.AccelerationSlider2 = sliderReaders[5].GetValue(ref joystickState);
        }
        else
        {
          this.AccelerationSlider2 = 0.0f;
        }

        if (sliderReaders[6] != null)
        {
          this.ForceSlider1 = sliderReaders[6].GetValue(ref joystickState);
        }
        else
        {
          this.ForceSlider1 = 0.0f;
        }
        if (sliderReaders[7] != null)
        {
          this.ForceSlider2 = sliderReaders[7].GetValue(ref joystickState);
        }
        else
        {
          this.ForceSlider2 = 0.0f;
        }
      }

      // Take over the joystick's Point-of-View controllers
      {
        this.PovCount = converter.PovCount;

        int[] povs = null;
        if (this.PovCount >= 1)
        {
          povs = joystickState.GetPointOfViewControllers();
          this.Pov1 = povs[0];
        }
        else
        {
          this.Pov1 = -1;
        }
        if (this.PovCount >= 2)
        {
          this.Pov2 = povs[1];
        }
        else
        {
          this.Pov2 = -1;
        }
        if (this.PovCount >= 3)
        {
          this.Pov3 = povs[2];
        }
        else
        {
          this.Pov3 = -1;
        }
        if (this.PovCount >= 4)
        {
          this.Pov4 = povs[3];
        }
        else
        {
          this.Pov4 = -1;
        }
      }
    }