Exemplo n.º 1
1
        public Joystick[] GetSticks()
        {
            List<SlimDX.DirectInput.Joystick> sticks = new List<SlimDX.DirectInput.Joystick>(); // Creates the list of joysticks connected to the computer via USB.

            foreach (DeviceInstance device in Input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                // Creates a joystick for each game device in USB Ports
                try
                {
                    stick = new SlimDX.DirectInput.Joystick(Input, device.InstanceGuid);
                    stick.Acquire();

                    // Gets the joysticks properties and sets the range for them.
                    foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                            stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
                    }

                    // Adds how ever many joysticks are connected to the computer into the sticks list.
                    sticks.Add(stick);
                }
                catch (DirectInputException)
                {
                }
            }
            Console.WriteLine(sticks.Count);
            return sticks.ToArray();
        }
Exemplo n.º 2
0
        private Joystick[] obtenerDispositivos()
        {
            var sticks = new List<SlimDX.DirectInput.Joystick>();
            DirectInput dinput = new DirectInput();
            foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                // crear los dispositivos
                try
                {
                    var stick = new SlimDX.DirectInput.Joystick(dinput, device.InstanceGuid);
                    stick.Acquire();

                    foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                            stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
                    }

                    sticks.Add(stick);
                }
                catch (DirectInputException)
                {
                }
            }
            return sticks.ToArray();
        }
Exemplo n.º 3
0
        public Joystick[] GetSticks()
        {
            List <SlimDX.DirectInput.Joystick> sticks = new List <SlimDX.DirectInput.Joystick>();

            // List<Microsoft.DirectX.DirectInput.Joystick> mSticks = new List<Microsoft.DirectX.DirectInput.Joystick>();
            foreach (DeviceInstance device in input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    stick = new SlimDX.DirectInput.Joystick(input, device.InstanceGuid);
                    stick.Acquire();
                    sticks.Add(stick);
                    Invoke(new MethodInvoker(delegate
                    {
                        debug.Text = "Controller Connected.";

                        /*foreach (bool btn in buttons) {
                         *  controlOut.Text += (btn ? "t " : "f ") ;
                         * }*/
                    }));
                }
                catch (Exception e) { debug.Text += e.ToString(); }
            }
            return(sticks.ToArray());
        }
Exemplo n.º 4
0
        private Joystick[] obtenerDispositivos()
        {
            var         sticks = new List <SlimDX.DirectInput.Joystick>();
            DirectInput dinput = new DirectInput();

            foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                // crear los dispositivos
                try
                {
                    var stick = new SlimDX.DirectInput.Joystick(dinput, device.InstanceGuid);
                    stick.Acquire();

                    foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
                        }
                    }

                    sticks.Add(stick);
                }
                catch (DirectInputException)
                {
                }
            }
            return(sticks.ToArray());
        }
Exemplo n.º 5
0
        public DirectInput_Device(DirectInput directInput, DeviceInstance device)
        {
            this.StatusIcon    = Properties.Resources.GenericGamepad.ToImageSource();
            this.directInput   = directInput;
            this.device        = device;
            this.deviceWrapper = new DIdevice();
            gamepad            = new SlimDX.DirectInput.Joystick(directInput, device.InstanceGuid);

            this.DeviceName = gamepad.Information.ProductName + "(" + device.InstanceGuid + ")";
            gamepad.Acquire();


            foreach (DeviceObjectInstance deviceObject in gamepad.GetObjects())
            {
                if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                {
                    gamepad.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
                }
            }

            foreach (var property in deviceWrapper.GetType().GetProperties())
            {
                PropertyInfo propertyS = deviceWrapper.GetType().GetProperty(property.Name);
                var          value     = property.GetValue(deviceWrapper, null);
                this.Channels.Add(value as DeviceChannel);
            }

            poolingThread = new Thread(ListenerThread);
            poolingThread.Start();
        }
Exemplo n.º 6
0
        public Joystick[] GetWheels()
        {
            List <SlimDX.DirectInput.Joystick> wheels = new List <Joystick>();

            foreach (DeviceInstance device in Input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    wheel = new Joystick(Input, device.InstanceGuid);
                    wheel.Acquire();
                    foreach (DeviceObjectInstance deviceObject in wheel.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            wheel.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 0);
                        }
                    }
                    wheels.Add(wheel);
                }
                catch (DirectInputException)
                {
                }
            }
            return(wheels.ToArray());
        }
Exemplo n.º 7
0
        public InputController(InputCore core, DI.Joystick dev, int index)
        {
            if (core == null)
            {
                throw new ArgumentNullException("core");
            }
            if (dev == null)
            {
                throw new ArgumentNullException("dev");
            }
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index", "assertion failed: index >= 0");
            }

            Core   = core;
            Device = dev;

            ApplyExclusivity();
            Device.Acquire();

            ID      = index;
            Buttons = new ButtonActions[ButtonCount];
            Axes    = new AxisActions[AxisCount];

            for (int i = 0, max = Buttons.Length; i < max; ++i)
            {
                Buttons[i] = new ButtonActions(Core, false, i + 1);
            }
            for (int i = 0, max = Axes.Length; i < max; ++i)
            {
                Axes[i] = new AxisActions(Core, false, true, i + 1);
            }
        }
Exemplo n.º 8
0
        //initializing connected joysticks and collecting them into a Jaystick-array
        //WTF mate (Chris Charitidis https://www.youtube.com/watch?v=rtnLGfAj7W0)
        public Joystick[] getSticks()
        {
            List<SlimDX.DirectInput.Joystick> sticks = new List<SlimDX.DirectInput.Joystick>();
            foreach (DeviceInstance device in input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    stick = new SlimDX.DirectInput.Joystick(input, device.InstanceGuid);
                    stick.Acquire();

                    foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            //stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
                        }
                    }

                    sticks.Add(stick);

                }
                catch (DirectInputException)
                {
                    throw;
                }

            }

            return sticks.ToArray();

        }
Exemplo n.º 9
0
        /// <summary>
        /// Získání prvního gamepadu, ketrý je připojený k počítači
        /// </summary>
        /// <returns>zařízení</returns>
        /// <exception>Pokud se nepodařilo najít žádné funkční zařízení</exception>
        private SlimDX.DirectInput.Joystick getGamepad()
        {
            dinput = new DirectInput();
            SlimDX.DirectInput.Joystick gamepad;
            string errorMessage = "Nebylo nalezeno žádné vnější ovládací zařízení.";

            foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    gamepad = new SlimDX.DirectInput.Joystick(dinput, device.InstanceGuid);
                    gamepad.Acquire();

                    foreach (DeviceObjectInstance deviceObject in gamepad.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            gamepad.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
                        }
                    }
                    return(gamepad);
                }
                catch (DirectInputException e)
                {
                    dinput.Dispose();
                    errorMessage = e.Message;
                }
            }
            dinput.Dispose();
            throw new DirectInputException(errorMessage);
        }
Exemplo n.º 10
0
        public Joystick()
        {
            var di = new DirectInput();

            // Get the first device
            try
            {
                var device = di.GetDevices()[0];
                joystick = new SlimDX.DirectInput.Joystick(di, device.InstanceGuid);
            }
            catch (IndexOutOfRangeException e)
            {
                Console.WriteLine("No devices: {0}", e.Message);
                Environment.Exit(1);
            }
            catch (DirectInputException e)
            {
                Console.WriteLine(e.Message);
                Environment.Exit(1);
            }

            foreach (DeviceObjectInstance deviceObject in joystick.GetObjects())
            {
                if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                    joystick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
            }

            joystick.Acquire();
        }
Exemplo n.º 11
0
        public Joystick()
        {
            var di = new DirectInput();

            foreach (var device in di.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    joystick = new SlimDX.DirectInput.Joystick(di, device.InstanceGuid);
                    Connected = true;
                    joystick.RunControlPanel();
                    break;
                }
                catch
                {
                }
            }

            /*foreach (DeviceObjectInstance deviceObject in joystick.GetObjects())
            {
                if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                {
                    joystick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
                }
            }*/

            if (Connected)
                joystick.Acquire();
        }
        public SlimDX.DirectInput.Joystick[] GetSticks()
        {
            List <SlimDX.DirectInput.Joystick> Sticks = new List <SlimDX.DirectInput.Joystick>();

            foreach (DeviceInstance device in Input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    stick = new SlimDX.DirectInput.Joystick(Input, device.InstanceGuid);
                    stick.Acquire();

                    foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
                        }
                    }
                    Sticks.Add(stick);
                    lbJoystick.Text       = "Joystick (Connected)";
                    joystickTimer.Enabled = true;
                }
                catch (DirectInputException ex)
                {
                    lbJoystick.Text = "Joystick (No Connected)";
                }
            }
            return(Sticks.ToArray());
        }
Exemplo n.º 13
0
    //initializing connected joysticks and collecting them into a Jaystick-array
    //ref.: Chris Charitidis https://www.youtube.com/watch?v=rtnLGfAj7W0
    public static TJoystick[] GetSticks()
    {
        TJoystick.JOYSTICKNUM = 0;          //Reset joystick counter
        DirectInput input = new DirectInput();

        //For each connected device of type GameController, get the device and add it to a list of joysticks
        List <TJoystick> temp_sticks = new List <TJoystick>();

        foreach (DeviceInstance device in input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
        {
            try
            {
                //get joystick device
                Joystick stick = new SlimDX.DirectInput.Joystick(input, device.InstanceGuid);
                stick.Acquire();


                //set max and min values of each axis
                foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                {
                    if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                    {
                        stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-32768, 32767);
                    }
                }

                //add joystick to list
                TJoystick tstick = new TJoystick(stick);
                temp_sticks.Add(tstick);
            } catch (DirectInputException) { throw; }
        }

        return(temp_sticks.ToArray());
    }
Exemplo n.º 14
0
 private static bool[] CaptureControllerInput()
 {
     try
     {
         if (DXJoystick == null || !DXJoystick.Acquire().IsSuccess)
         {
             return(null);
         }
         bool[]          flagArray    = new bool[256];
         DXJoystickState currentState = DXJoystick.GetCurrentState();
         flagArray = currentState.GetButtons();
         int length = flagArray.Length;
         if (currentState.X > 500)
         {
             flagArray[102] = true;
         }
         else if (currentState.X < -500)
         {
             flagArray[103] = true;
         }
         if (currentState.Y > 500)
         {
             flagArray[101] = true;
         }
         else if (currentState.Y < -500)
         {
             flagArray[100] = true;
         }
         return(flagArray);
     }
     catch
     {
         return(null);
     }
 }
Exemplo n.º 15
0
        /// <summary>
        /// Získání prvního gamepadu, ketrý je připojený k počítači
        /// </summary>
        /// <returns>zařízení</returns>
        /// <exception>Pokud se nepodařilo najít žádné funkční zařízení</exception>
        private SlimDX.DirectInput.Joystick getGamepad()
        {
            dinput = new DirectInput();
            SlimDX.DirectInput.Joystick gamepad;
            string errorMessage = "Nebylo nalezeno žádné vnější ovládací zařízení.";

            foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    gamepad = new SlimDX.DirectInput.Joystick(dinput, device.InstanceGuid);
                    gamepad.Acquire();

                    foreach (DeviceObjectInstance deviceObject in gamepad.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            gamepad.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
                        }
                    }
                    return gamepad;
                }
                catch (DirectInputException e)
                {
                    dinput.Dispose();
                    errorMessage = e.Message;
                }
            }
            dinput.Dispose();
            throw new DirectInputException(errorMessage);
        }
Exemplo n.º 16
0
        //=============================================================================================================
        public Joystick[] GetSticks()
        {
            List <SlimDX.DirectInput.Joystick> sticks = new List <SlimDX.DirectInput.Joystick>();

            foreach (DeviceInstance device in Input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    stick = new SlimDX.DirectInput.Joystick(Input, device.InstanceGuid);
                    stick.Acquire();

                    foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
                        }
                    }
                    sticks.Add(stick);
                }
                catch (DirectInputException)
                {
                    textBox_diagnostics.Text = "Nie wykryto Joysticka";
                }
            }
            return(sticks.ToArray());
        }
        public Joystick[] GetSticks()
        {
            List <SlimDX.DirectInput.Joystick> sticks = new List <SlimDX.DirectInput.Joystick>(); // Creates the list of joysticks connected to the computer via USB.

            foreach (DeviceInstance device in Input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                // Creates a joystick for each game device in USB Ports
                try
                {
                    stick = new SlimDX.DirectInput.Joystick(Input, device.InstanceGuid);
                    stick.Acquire();

                    // Gets the joysticks properties and sets the range for them.
                    foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
                        }
                    }

                    // Adds how ever many joysticks are connected to the computer into the sticks list.
                    sticks.Add(stick);
                }
                catch (DirectInputException)
                {
                }
            }
            Console.WriteLine(sticks.Count);
            return(sticks.ToArray());
        }
Exemplo n.º 18
0
        /// <summary>
        /// Найти все устройства типа джойстик
        /// </summary>
        /// <returns></returns>
        public bool Connect()
        {
            try
            {
                _quitThread = false;
                // create a device from this controller.
                _joystick = new SlimDX.DirectInput.Joystick(_directInput, _deviceInstance.InstanceGuid);

                foreach (var doi in _joystick.GetObjects(ObjectDeviceType.Axis))
                {
                    _joystick.GetObjectPropertiesById((int)doi.ObjectType).SetRange(MinimumAxisValue, MaximumAxisValue);
                }
                _joystick.Properties.AxisMode = DeviceAxisMode.Absolute;

                _joystick.SetCooperativeLevel(IntPtr.Zero, CooperativeLevel.Background | CooperativeLevel.Nonexclusive);
                // Tell DirectX that this is a Joystick.
                //            _joystick.SetDataFormat(DeviceDataFormat.Joystick);

                _joystickThread = new Thread(GetJoystickData);
                _joystick.SetNotification(_joystickEvent);
                // Finally, acquire the device.
                _buttons = new bool[_joystick.GetObjects(ObjectDeviceType.Button).Count];
                _axis    = new int[/*_joystick.GetObjects(ObjectDeviceType.Axis).Count+3*/ 128];
                //            PointOfView = new int[caps.NumberPointOfViews];
                _joystick.Acquire();

                _joystickThread.Start();
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemplo n.º 19
0
        public Joystick[] GetSticks()
        {
            List <SlimDX.DirectInput.Joystick> sticks = new List <SlimDX.DirectInput.Joystick>();

            foreach (DeviceInstance device in myInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    myStick = new SlimDX.DirectInput.Joystick(myInput, device.InstanceGuid);
                    myStick.Acquire();
                    label16.Text   = "Connected";
                    panel1.Enabled = true;
                    foreach (DeviceObjectInstance deviceObject in myStick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                        {
                            myStick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(Properties.Settings.Default.DataRangeStarts, Properties.Settings.Default.DataRangeEnds);
                        }
                    }
                    sticks.Add(myStick);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            rangeStart = Properties.Settings.Default.DataRangeStarts;
            rangeEnd   = Properties.Settings.Default.DataRangeEnds;
            return(sticks.ToArray());
        }
Exemplo n.º 20
0
		public void Acquire()
		{
			if (!IsAcquired) {
				_waitHandle = new AutoResetEvent(false);
				_device = _CreateDevice();
				// Must be done before acquisition.
				_device.SetNotification(_waitHandle);
				_device.Acquire();
				// Must be set before any notifications come through.
				IsAcquired = true;
				Thread thread = new Thread(_UpdateThread) {Name = "Joystick"};
				thread.Start();
			}
		}
Exemplo n.º 21
0
        private void CaptureThread()
        {
            while (true)
            {
                try
                {
                    if (!this.isCapturing)
                    {
                        return;
                    }

                    if (gamepad.Acquire().IsFailure)
                    {
                        return;
                    }

                    if (gamepad.Poll().IsFailure)
                    {
                        return;
                    }

                    if (SlimDX.Result.Last.IsFailure)
                    {
                        return;
                    }

                    state = gamepad.GetCurrentState();

                    bool[] buttons = state.GetButtons();
                    //for (int i = 0; i < buttons.Length; i++)
                    //    if (buttons[i])
                    //        label2.Text = i.ToString();
                }
                catch (Exception ex) { }


                if (OnStateUpdated != null)
                {
                    OnStateUpdated(state);
                }

                try
                {
                    gamepad.Unacquire();
                }
                catch (Exception ex) { }

                Thread.Sleep(50);
            }
        }
Exemplo n.º 22
0
        void StickHandlerLogic(Joystick js)
        {
            //Enumerate a snapshot of the joystick
            JoystickState state = new JoystickState();

            tab_mate.Acquire();
            state = tab_mate.GetCurrentState();

            bool[] buttons = state.GetButtons();

            for (int button_num = 0; button_num < buttons.Length; button_num++)
            {
                if (buttons[button_num])
                {
                    Console.WriteLine("Button " + button_num + " was pressed!");
                }
            }
        }
Exemplo n.º 23
0
 public void Acquire()
 {
     if (!IsAcquired)
     {
         _waitHandle = new AutoResetEvent(false);
         _device     = _CreateDevice();
         // Must be done before acquisition.
         _device.SetNotification(_waitHandle);
         _device.Acquire();
         // Must be set before any notifications come through.
         IsAcquired = true;
         Thread thread = new Thread(_UpdateThread)
         {
             Name = "Joystick"
         };
         thread.Start();
     }
 }
Exemplo n.º 24
0
        /// <summary>
        /// Constructor, do the following to loop through and select your joystick
        /// SlimDX.DirectInput.DirectInput dinput = new SlimDX.DirectInput.DirectInput();
        /// foreach (SlimDX.DirectInput.DeviceInstance device in dinput.GetDevices(SlimDX.DirectInput.DeviceClass.GameController, SlimDX.DirectInput.DeviceEnumerationFlags.AttachedOnly))
        /// {
        ///     Console.WriteLine("Controller:" + device.InstanceName);
        ///
        ///     DirectX.Joystick.onLog = logMessages;
        ///     DirectX.Joystick.debug = false;
        ///     joystick = new DirectX.Joystick(dinput, device);
        ///
        ///     joystick.axisX.min = 1;
        ///     joystick.axisX.max = 99;
        ///     joystick.axisX.deadZone = 0;
        ///     joystick.axisX.saturation = 10000;
        ///
        ///     joystick.setJoystickValues();
        ///
        ///     controlHelper = new controllHelper(joystick);
        ///     joystick.start();
        ///
        ///     break;
        /// }
        /// </summary>
        /// <param name="dInput"></param>
        /// <param name="joystickInstance"></param>
        public Joystick(DirectInput dInput, DeviceInstance joystickInstance)
        {
            this.joystickInstance = joystickInstance;
            JoystickName          = joystickInstance.ProductName;

            joystickDevice = new SlimDX.DirectInput.Joystick(dInput, joystickInstance.InstanceGuid);  // slimDX replacement
            joystickDevice.SetNotification(onJoystickEvent);
            joystickDevice.Acquire();

            axisY     = new axis();
            axisX     = new axis();
            axisZ     = new axis();
            axisRy    = new axis();
            axisRx    = new axis();
            axisRz    = new axis();
            axisExtra = new axis();

            /* initiate default joystick values.*/
            setJoystickValues();
        }
Exemplo n.º 25
0
        /// <summary>
        /// Handler stavu gamepadu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gamepadHandle(object sender, EventArgs e)
        {
            if (enabled)
            {
                try
                {
                    gamepad.Acquire();
                }
                catch (DirectInputException)
                {
                    dinput.Dispose();
                    periodicChecker.Dispose();
                    errorObserver();
                }
                JoystickState stateNow = new JoystickState();
                stateNow = gamepad.GetCurrentState();
                bool[] buttons = stateNow.GetButtons();
                if ((Math.Abs(state.stickDirectMoveX - stateNow.X) > sensitivityJoystick || Math.Abs(state.stickDirectMoveY - stateNow.Y) > sensitivityJoystick) && stickDirectMoveObserver != null)
                {
                    state.stickDirectMoveX = stateNow.X;
                    state.stickDirectMoveY = stateNow.Y;
                    int x = state.stickDirectMoveX;
                    int y = state.stickDirectMoveY;
                    if (Math.Abs(x) < 10 && Math.Abs(y) < 10)
                    {
                        x = 0;
                        y = 0;
                    }
                    stickDirectMoveObserver(x, y);
                }

                if (state.moveDown != buttons[0] && buttonMoveDownObserver != null)
                {
                    buttonMoveDownObserver(buttons[0]);
                    state.moveDown = buttons[0];
                }

                if (state.moveUp != buttons[3] && buttonMoveUpObserver != null)
                {
                    buttonMoveUpObserver(buttons[3]);
                    state.moveUp = buttons[3];
                }

                if (state.narrow != buttons[2] && buttonNarrowObserver != null)
                {
                    buttonNarrowObserver(buttons[2]);
                    state.narrow = buttons[2];
                }

                if (state.widen != buttons[1] && buttonWidenObserver != null)
                {
                    buttonWidenObserver(buttons[1]);
                    state.widen = buttons[1];
                }

                if (state.defaultPosition != buttons[7] && buttonDefaultPositionObserver != null)
                {
                    buttonDefaultPositionObserver(buttons[7]);
                    state.defaultPosition = buttons[7];
                }

                if (state.rotateLeft != buttons[8] && buttonRotateLeftObserver != null)
                {
                    buttonRotateLeftObserver(buttons[8]);
                    state.rotateLeft = buttons[8];
                }

                if (state.rotateRight != buttons[9] && buttonRotateRightObserver != null)
                {
                    buttonRotateRightObserver(buttons[9]);
                    state.rotateRight = buttons[9];
                }

                if (state.stop != buttons[10] && buttonStopObserver != null)
                {
                    buttonStopObserver(buttons[10]);
                    state.stop = buttons[10];
                }
            }
        }
        /// <summary>Gets a list of the attached joysticks.</summary>
        /// <returns>A list of joysticks.</returns>
        private List<DI.Joystick> GetAttachedJoysticks()
        {
            List<DI.Joystick> joysticks = new List<DI.Joystick>();
            foreach(DI.DeviceInstance device in _directInput.GetDevices(DI.DeviceClass.GameController, DI.DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    DI.Joystick joystick = new DI.Joystick(_directInput, device.InstanceGuid);
                    joystick.Acquire();

                    IList<DI.DeviceObjectInstance> deviceObjects = joystick.GetObjects();
                    for(int i = 0; i < deviceObjects.Count; i++)
                    {
                        DI.DeviceObjectInstance deviceObjectInstance = deviceObjects[i];

                        if((deviceObjectInstance.ObjectType & DI.ObjectDeviceType.Axis) != 0)
                            joystick.GetObjectPropertiesById((int) deviceObjectInstance.ObjectType).SetRange(-1000, 1000);
                    }

                    joysticks.Add(joystick);
                }
                catch(DI.DirectInputException)
                {
                }
            }
            return joysticks;
        }
Exemplo n.º 27
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            for (int i = 0; i < reverserSwitchKeyTexts.Length; i++)
            {
                addKey(reverserSwitchKeyTexts[i]);
                keyCodeTable.Add(new int[2] {
                    0, i
                });
            }
            for (int i = 0; i < cabSwitchKeyTexts.Length; i++)
            {
                addKey(cabSwitchKeyTexts[i]);
                keyCodeTable.Add(new int[2] {
                    -1, i
                });
            }
            for (int i = 0; i < atsKeyTexts.Length; i++)
            {
                addKey(atsKeyTexts[i]);
                keyCodeTable.Add(new int[2] {
                    -2, i
                });
            }
            for (int i = 0; i < gameControlKeyTexts.Length; i++)
            {
                addKey(gameControlKeyTexts[i]);
                keyCodeTable.Add(new int[2] {
                    -3, i
                });
            }

            selectDropDownList(buttonSKeyList, DenshadeGoInterface.settings.ButtonS);
            selectDropDownList(buttonPKeyList, DenshadeGoInterface.settings.ButtonP);
            selectDropDownList(buttonAKeyList, DenshadeGoInterface.settings.ButtonA);
            selectDropDownList(buttonBKeyList, DenshadeGoInterface.settings.ButtonB);
            selectDropDownList(buttonCKeyList, DenshadeGoInterface.settings.ButtonC);

            bool added = false;
            List <SlimDX.DirectInput.Joystick> sticks = new List <SlimDX.DirectInput.Joystick>();

            foreach (DeviceInstance device in DenshadeGoInterface.input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    controllerList.Items.Add(device.ProductName);
                    Joystick stick = new SlimDX.DirectInput.Joystick(DenshadeGoInterface.input, device.InstanceGuid);
                    stick.Acquire();
                    int size = stick.GetCurrentState().GetButtons().Length;
                    buttonSize.Add(size);
                    if (device.ProductName.Equals(DenshadeGoInterface.settings.ControllerName))
                    {
                        added = true;
                        controllerList.SelectedIndex = controllerList.Items.Count - 1;
                        resizeUpDown(size);
                    }
                    stick.Unacquire();
                }
                catch (DirectInputException)
                {
                }
            }
            if (!added)
            {
                buttonSize.Add(128);
                controllerList.Items.Add(DenshadeGoInterface.settings.ControllerName);
                controllerList.SelectedIndex = controllerList.Items.Count - 1;
                resizeUpDown(128);
            }
            loadUpDown();
        }
Exemplo n.º 28
0
        public void Connect(int index)
        {
            List<DeviceInstance> devices = new List<DeviceInstance>();

            Declare();
            dinput = new DirectInput();

            //Get connected devices
            foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                devices.Add(device);
            }

            if (devices.Count() > 0)
            {
                try
                {
                    //Connect to selected device
                    joystick = new SlimDX.DirectInput.Joystick(dinput, devices[index].InstanceGuid);
                    foreach (DeviceObjectInstance deviceObject in joystick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                            joystick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
                    }
                    joystick.Acquire();
                    Connected = true;
                }
                catch (DirectInputException)
                {
                    Connected = false;
                }
                //Get data from selected gamepad
                ThreadTransmit = new Thread(delegate()
                {
                    while (Connected)
                    {
                        Thread.Sleep(50);
                        GetInput();
                    }
                });
                ThreadTransmit.Start();
            }
        }