/// <summary>Initializes a new DirectInput state converter</summary>
        /// <param name="joystick">Joystick for which the converter will be used</param>
        internal DirectInputConverter(Joystick joystick)
        {
            this.sliderReaders = new ISliderReader[8];
              this.axisReaders = new IAxisReader[24];

              this.buttonCount = joystick.Capabilities.ButtonCount;
              this.povCount = joystick.Capabilities.PovCount;

              mapAxes(joystick);
              mapSliders(joystick);
        }
        /// <summary>Initializes a new DirectInput-based game pad</summary>
        /// <param name="joystick">The DirectInput joystick this instance will query</param>
        /// <param name="checkAttachedDelegate">
        ///   Delegate through which the instance can check if the device is attached
        /// </param>
        public DirectInputGamePad(
      Joystick joystick, CheckAttachedDelegate checkAttachedDelegate
    )
        {
            this.joystick = joystick;
              this.checkAttachedDelegate = checkAttachedDelegate;
              this.states = new LeakyQueue<JoystickState>();
              this.converter = new DirectInputConverter(this.joystick);

              // Ensure the leaky queue has created its array
              ensureSlotAvailable();
        }
    /// <summary>Initializes a new DirectInput-based game pad</summary>
    /// <param name="joystick">The DirectInput joystick this instance will query</param>
    /// <param name="checkAttachedDelegate">
    ///   Delegate through which the instance can check if the device is attached
    /// </param>
    public DirectInputGamePad(
      Joystick joystick, CheckAttachedDelegate checkAttachedDelegate
    ) {
      this.joystick = joystick;
      this.checkAttachedDelegate = checkAttachedDelegate;

      this.states = new Queue<GamePadState>();

      this.currentJoystickState = new JoystickState();
      this.current = new GamePadState();

      this.converter = new GamePadConverter(this.joystick);
    }
    /// <summary>Initializes a new game pad converter</summary>
    /// <param name="joystick">Joystick whose data will be converted</param>
    public GamePadConverter(Joystick joystick) {
      this.joystick = joystick;
      this.gamePadState = new FlatGamePadState();

      this.axisCount = Math.Min(this.joystick.Capabilities.AxesCount, 4);
      this.hasPovHat = (this.joystick.Capabilities.PovCount > 0);
      if (this.hasPovHat) {
        this.buttonCount = Math.Min(this.joystick.Capabilities.ButtonCount, 10);
      } else {
        this.buttonCount = Math.Min(this.joystick.Capabilities.ButtonCount, 14);
      }

      this.convertDelegate = convertButtonsAndPov;
      addAxisConverters();
      addSliderConverters();
    }
        /// <summary>Creates an axis reader for the specified object</summary>
        /// <param name="joystick">Joystick providing the control object</param>
        /// <param name="axis">Axis a reader will be created for</param>
        /// <param name="control">Control description for the axis</param>
        /// <returns>A new axis reader for the specified axis</returns>
        private static IAxisReader createAxisReader(
      Joystick joystick, ExtendedAxes axis, DeviceObjectInstance control
    )
        {
            int id = (int)control.ObjectType;
              ObjectProperties properties = joystick.GetObjectPropertiesById(id);

              int min = properties.LowerRange;
              int max = properties.UpperRange;

              switch (axis) {
            case ExtendedAxes.X: {
              return new XAxisReader(min, max);
            }
            case ExtendedAxes.Y: {
              return new YAxisReader(min, max);
            }
            case ExtendedAxes.Z: {
              return new ZAxisReader(min, max);
            }
            case ExtendedAxes.VelocityX: {
              return new VelocityXAxisReader(min, max);
            }
            case ExtendedAxes.VelocityY: {
              return new VelocityYAxisReader(min, max);
            }
            case ExtendedAxes.VelocityZ: {
              return new VelocityZAxisReader(min, max);
            }
            case ExtendedAxes.AccelerationX: {
              return new AccelerationXAxisReader(min, max);
            }
            case ExtendedAxes.AccelerationY: {
              return new AccelerationYAxisReader(min, max);
            }
            case ExtendedAxes.AccelerationZ: {
              return new AccelerationZAxisReader(min, max);
            }
            case ExtendedAxes.ForceX: {
              return new ForceXAxisReader(min, max);
            }
            case ExtendedAxes.ForceY: {
              return new ForceYAxisReader(min, max);
            }
            case ExtendedAxes.ForceZ: {
              return new ForceZAxisReader(min, max);
            }
            case ExtendedAxes.RotationX: {
              return new RotationXAxisReader(min, max);
            }
            case ExtendedAxes.RotationY: {
              return new RotationYAxisReader(min, max);
            }
            case ExtendedAxes.RotationZ: {
              return new RotationZAxisReader(min, max);
            }
            case ExtendedAxes.AngularVelocityX: {
              return new AngularVelocityXAxisReader(min, max);
            }
            case ExtendedAxes.AngularVelocityY: {
              return new AngularVelocityYAxisReader(min, max);
            }
            case ExtendedAxes.AngularVelocityZ: {
              return new AngularVelocityZAxisReader(min, max);
            }
            case ExtendedAxes.AngularAccelerationX: {
              return new AngularAccelerationXAxisReader(min, max);
            }
            case ExtendedAxes.AngularAccelerationY: {
              return new AngularAccelerationYAxisReader(min, max);
            }
            case ExtendedAxes.AngularAccelerationZ: {
              return new AngularAccelerationZAxisReader(min, max);
            }
            case ExtendedAxes.TorqueX: {
              return new TorqueXAxisReader(min, max);
            }
            case ExtendedAxes.TorqueY: {
              return new TorqueYAxisReader(min, max);
            }
            case ExtendedAxes.TorqueZ: {
              return new TorqueZAxisReader(min, max);
            }
            default: {
              return null;
            }
              }
        }
        /// <summary>Creates a slider reader for the specified object</summary>
        /// <param name="joystick">Joystick providing the control object</param>
        /// <param name="slider">Slider a reader will be created for</param>
        /// <param name="control">Control description for the axis</param>
        /// <returns>A new slider reader for the specified axis</returns>
        private static ISliderReader createSliderReader(
      Joystick joystick, ExtendedSliders slider, DeviceObjectInstance control
    )
        {
            int id = (int)control.ObjectType;
              ObjectProperties properties = joystick.GetObjectPropertiesById(id);

              int min = properties.LowerRange;
              int max = properties.UpperRange;

              switch (slider) {
            case ExtendedSliders.Slider1: {
              return new SliderReader(0, min, max);
            }
            case ExtendedSliders.Slider2: {
              return new SliderReader(1, min, max);
            }
            case ExtendedSliders.Velocity1: {
              return new VelocitySliderReader(0, min, max);
            }
            case ExtendedSliders.Velocity2: {
              return new VelocitySliderReader(1, min, max);
            }
            case ExtendedSliders.Acceleration1: {
              return new AccelerationSliderReader(0, min, max);
            }
            case ExtendedSliders.Acceleration2: {
              return new AccelerationSliderReader(1, min, max);
            }
            case ExtendedSliders.Force1: {
              return new ForceSliderReader(0, min, max);
            }
            case ExtendedSliders.Force2: {
              return new ForceSliderReader(1, min, max);
            }
            default: {
              return null;
            }
              }
        }
        /// <summary>Maps the sliders of the joystick</summary>
        /// <param name="joystick">Joystick for which sliders are mapped</param>
        private void mapSliders(Joystick joystick)
        {
            IList<DeviceObjectInstance> sliders = joystick.GetObjects(
            ObjectDeviceType.AbsoluteAxis
              );
              var unmappedReaders = new Queue<ISliderReader>();

              for (int index = 0; index < sliders.Count; ++index) {
            if (sliders[index].ObjectTypeGuid == ObjectGuid.Slider) {
              ExtendedSliders slider = identifySlider(
            sliders[index].Aspect, sliders[index].ObjectTypeGuid
              );

              // If this slider could not be identified but were still missing one
              // of the two standard sliders, remember it so we can use it in case
              // no exact matches are found for sliders 1 and 2.
              if (slider == 0) {
            if (unmappedReaders.Count < 2) {
              unmappedReaders.Enqueue(createSliderReader(joystick, slider, sliders[index]));
            }
              } else { // Slider was identified, assign it to the next free slot
            if ((this.availableSliders & slider) != 0) {
              ++slider;
              if ((this.availableSliders & slider) != 0) {
                slider = 0;
              }
            }
              }

              // If we identified this slider (exactly or by fallback), build a reader
              // for it and store it in the appropriate slot.
              if (slider != 0) {
            this.availableSliders |= slider;
            this.sliderReaders[indexFromSlider(slider)] = createSliderReader(
              joystick, slider, sliders[index]
            );
              }
            } // if
              } // for

              // If sliders 1 and/or 2 are still not provided, use the unidentified
              // sliders we remembered earlier as a fallback solution. These could very
              // well be null, too, in which case the sliders simply aren't there.
              if ((this.sliderReaders[0] == null) && (unmappedReaders.Count > 0)) {
            this.availableSliders |= ExtendedSliders.Slider1;
            this.sliderReaders[0] = unmappedReaders.Dequeue();
              }
              if ((this.sliderReaders[1] == null) && (unmappedReaders.Count > 0)) {
            this.availableSliders |= ExtendedSliders.Slider2;
            this.sliderReaders[1] = unmappedReaders.Dequeue();
              }
        }
        /// <summary>Maps the axes of the joystick</summary>
        /// <param name="joystick">Joystick for which axes are mapped</param>
        private void mapAxes(Joystick joystick)
        {
            IList<DeviceObjectInstance> axes = joystick.GetObjects(
            ObjectDeviceType.AbsoluteAxis
              );
              var unmappedReaders = new Queue<IAxisReader>();

              for (int index = 0; index < axes.Count; ++index) {
            if (axes[index].ObjectTypeGuid != ObjectGuid.Slider) {
              ExtendedAxes axis = identifyAxis(
            axes[index].Aspect, axes[index].ObjectTypeGuid
              );

              // If this axis could not be identified but we're still missing one of
              // the standard axes (X, Y, Rx, Ry), remember it so we can later map it
              // to the next unassigned standard axes in case some are left unassigned.
              if (axis == 0) {
            if (unmappedReaders.Count < 4) {
              unmappedReaders.Enqueue(createAxisReader(joystick, axis, axes[index]));
            }
              } else { // Axis identified, build reader and store it
            this.availableAxes |= axis;
            this.axisReaders[indexFromAxis(axis)] = createAxisReader(
              joystick, axis, axes[index]
            );
              }
            } // if
              } // for

              // If the four standard axes are still not completely provided, use
              // the unidentified axis we remembered earlier as a fallback solution.
              if ((this.axisReaders[0] == null) && (unmappedReaders.Count > 0)) {
            this.availableAxes |= ExtendedAxes.X;
            this.axisReaders[0] = unmappedReaders.Dequeue();
              }
              if ((this.axisReaders[1] == null) && (unmappedReaders.Count > 0)) {
            this.availableAxes |= ExtendedAxes.Y;
            this.axisReaders[1] = unmappedReaders.Dequeue();
              }
              if ((this.axisReaders[12] == null) && (unmappedReaders.Count > 0)) {
            this.availableAxes |= ExtendedAxes.RotationX;
            this.axisReaders[12] = unmappedReaders.Dequeue();
              }
              if ((this.axisReaders[13] == null) && (unmappedReaders.Count > 0)) {
            this.availableAxes |= ExtendedAxes.RotationY;
            this.axisReaders[13] = unmappedReaders.Dequeue();
              }
        }
Exemple #9
0
 //Disposes of SDL
 static void Cleanup()
 {
     Joystick.Cleanup();
     running = false;
 }