Пример #1
0
        private void _UpdateThread()
        {
            while (IsAcquired)
            {
                lock (_device) {
                    _UpdateState();
                }
                _waitHandle.WaitOne();
            }

            _device.Dispose();
            _device = null;

            _waitHandle.Close();
            _waitHandle.Dispose();
            _waitHandle = null;
        }
Пример #2
0
 /// <summary>
 /// Отключение
 /// </summary>
 public void Disconnect()
 {
     try
     {
         lock (_threadLock)
         {
             _quitThread = true;
         }
         _joystickThread.Join();
         _joystick.Unacquire();
         _joystick.Dispose();
         _directInput.Dispose();
     }
     catch (Exception)
     {
     }
 }
Пример #3
0
        static EntryField()
        {
            TKKeys    = Enum.GetValues(typeof(TKKey)).Cast <TKKey>().ToList();
            TKButtons = Enum.GetValues(typeof(XInputGamePadButton)).Cast <XInputGamePadButton>().ToList();

            TKKeysHeld    = new HashSet <TKKey>();
            TKButtonsHeld = TKButtons.ToDictionary(b => b, b => false);

            TKButtonsInitialState = new Dictionary <int, Dictionary <XInputGamePadButton, bool?> >();

            var inputPoll = new Timer(50);

            inputPoll.Elapsed += (sender, args) =>
            {
                var inputPressed = false;

                try
                {
                    var keyState = TKKeyboard.GetState();
                    if (keyState.IsConnected)
                    {
                        foreach (var key in TKKeys)
                        {
                            if (keyState.IsKeyDown(key))
                            {
                                if (!TKKeysHeld.Contains(key))
                                {
                                    OnTKKeyPress(key);
                                }
                                TKKeysHeld.Add(key);
                                inputPressed = true;
                            }
                            else
                            {
                                TKKeysHeld.Remove(key);
                            }
                        }
                    }
                }
                catch { }

                if (CreateController())
                {
                    var buttons = CaptureControllerInput();
                    if (DXButtonInitialState == null)
                    {
                        DXButtonInitialState = buttons.Cast <bool?>().ToArray();
                    }

                    for (int i = 0; buttons != null && i < buttons.Length; i++)
                    {
                        if (buttons[i] != DXButtonInitialState[i])
                        {
                            DXButtonInitialState[i] = null;
                        }

                        if (DXButtonInitialState[i] == null)
                        {
                            if (buttons[i])
                            {
                                if (!DXButtonsHeld[i])
                                {
                                    OnButtonPress(i);
                                }
                                DXButtonsHeld[i] = true;
                                inputPressed     = true;
                            }
                            else
                            {
                                DXButtonsHeld[i] = false;
                            }
                        }
                    }
                }

                for (int i = 0; i <= 3; i++)
                {
                    var isFirstRun = TKButtonsInitialState.Count <= i || TKButtonsInitialState[i] == null;
                    if (isFirstRun)
                    {
                        TKButtonsInitialState[i] = new Dictionary <XInputGamePadButton, bool?>();
                    }

                    var initialState = TKButtonsInitialState[i];

                    try
                    {
                        var gamePadState = GamePad.GetState(i);

                        if (gamePadState.IsConnected)
                        {
                            foreach (var button in TKButtons)
                            {
                                var buttonIsPressed = gamePadState.IsPressed(button);

                                if (isFirstRun)
                                {
                                    initialState.TryGetValue(button, out var state);
                                    initialState[button] = (state ?? false) || buttonIsPressed;
                                    continue;
                                }

                                if (initialState[button] != null &&
                                    initialState[button] != buttonIsPressed)
                                {
                                    initialState[button] = null;
                                }

                                if (initialState[button] == null)
                                {
                                    if (buttonIsPressed)
                                    {
                                        if (!TKButtonsHeld[button])
                                        {
                                            OnTKButtonPress(button);
                                        }
                                        TKButtonsHeld[button] = true;
                                        inputPressed          = true;
                                    }
                                    else
                                    {
                                        TKButtonsHeld[button] = false;
                                    }
                                }
                            }
                        }
                    }
                    catch
                    {
                        TKButtonsInitialState[i] = null;
                    }
                }

                if (!inputPressed && FieldSet)
                {
                    InputComplete();
                }

                inputPoll.Start();
            };
            inputPoll.AutoReset = false;
            inputPoll.Start();

            Application.Current.Exit += (sender, args) =>
            {
                DXJoystick?.Dispose();
                DirectInput?.Dispose();
                inputPoll.Close();
            };
        }