Exemplo n.º 1
0
 public XboxControllerFactory()
 {
     UpdateFrequency           = 50;
     m_CancellationTokenSource = new CancellationTokenSource();
     m_ButtonStates            = new Dictionary <string, bool>();
     m_LastValues             = new StickValues();
     m_ControllerListenerTask = Task.Factory.StartNew(ControllerListenerLoopAsync, TaskCreationOptions.LongRunning);
 }
Exemplo n.º 2
0
        private async void ControllerListenerLoopAsync()
        {
            CancellationToken cancellationTokenSource = m_CancellationTokenSource.Token;
            XBoxController    connectedController     = XBoxController.GetConnectedControllers().FirstOrDefault();

            while (!cancellationTokenSource.IsCancellationRequested)
            {
                await Task.Delay(20);

                if (connectedController.IsConnected)
                {
                    StickValues newValues = new StickValues(connectedController.ThumbLeftX, connectedController.ThumbLeftY, connectedController.ThumbRightX, connectedController.ThumbRightY);
                    if (m_LastUpdate < Environment.TickCount)
                    {
                        m_LastUpdate = Environment.TickCount + UpdateFrequency;
                        ControllerUpdate?.Invoke(this, newValues);
                    }
                    if (newValues != m_LastValues)
                    {
                        m_LastValues = newValues;
                        ControllerChanged?.Invoke(this, newValues);
                    }
                    CheckButton("A", connectedController.ButtonAPressed);
                    CheckButton("B", connectedController.ButtonBPressed);
                    CheckButton("X", connectedController.ButtonXPressed);
                    CheckButton("Y", connectedController.ButtonYPressed);
                    CheckButton("Back", connectedController.ButtonBackPressed);
                    CheckButton("Start", connectedController.ButtonStartPressed);
                    CheckButton("Up", connectedController.ButtonUpPressed);
                    CheckButton("Down", connectedController.ButtonDownPressed);
                    CheckButton("Left", connectedController.ButtonLeftPressed);
                    CheckButton("Right", connectedController.ButtonRightPressed);
                    CheckButton("LB", connectedController.ButtonShoulderLeftPressed);
                    CheckButton("RB", connectedController.ButtonShoulderRightPressed);
                    CheckButton("LT", connectedController.TriggerLeftPressed);
                    CheckButton("RT", connectedController.TriggerRightPressed);
                    CheckButton("LS", connectedController.ThumbpadLeftPressed);
                    CheckButton("RS", connectedController.ThumbpadRightPressed);
                }
            }
        }