private static void HandleJoyConPair(JoyConState joyConState, JoyConPair joyCon, ref VirtualJoystickData data)
        {
            if (joyConState is JoyConLeftState)
            {
                var state = (JoyConLeftState)joyConState;

                data.ButtonUp    = state.ButtonUp;
                data.ButtonDown  = state.ButtonDown;
                data.ButtonLeft  = state.ButtonLeft;
                data.ButtonRight = state.ButtonRight;

                data.BackButtonLeft     = state.BackButton;
                data.RearBackButtonLeft = state.RearBackButton;
                data.SelectButton       = state.MinusButton;
                data.StickLeftButton    = state.StickButton;

                data.LeftAxisX = JoyConConstants.LeftJoyConXMultiplier * state.StickHorizontal +
                                 JoyConConstants.LeftJoyConOffsetX;
                data.LeftAxisY = JoyConConstants.LeftJoyConYMultiplier * -state.StickVertical +
                                 JoyConConstants.LeftJoyConOffsetY;

                //Merge With Other JoyCon
                if (joyCon?.RightJoyCon.CurrentState != null)
                {
                    HandleJoyConPair(joyCon.RightJoyCon.CurrentState, null, ref data);
                }
            }
            else if (joyConState is JoyConRightState)
            {
                var state = (JoyConRightState)joyConState;

                data.ButtonY = state.ButtonX;
                data.ButtonX = state.ButtonY;
                data.ButtonB = state.ButtonA;
                data.ButtonA = state.ButtonB;

                data.BackButtonRight     = state.BackButton;
                data.RearBackButtonRight = state.RearBackButton;
                data.StartButton         = state.PlusButton;
                data.StickRightButton    = state.StickButton;

                data.RightAxisX = JoyConConstants.RightJoyConXMultiplier * state.StickHorizontal +
                                  JoyConConstants.RightJoyConOffsetX;
                data.RightAxisY = JoyConConstants.RightJoyConYMultiplier * state.StickVertical +
                                  JoyConConstants.RightJoyConOffsetY;

                //Merge With Other JoyCon
                if (joyCon?.LeftJoyCon.CurrentState != null)
                {
                    HandleJoyConPair(joyCon.LeftJoyCon.CurrentState, null, ref data);
                }
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Beispiel #2
0
        private void Poll()
        {
            var report = _device.ReadReport();

            if (report == null)
            {
                Logger.Debug("JoyCon Canceled " + this);
                return;
            }

            _device.Write(new byte[] { 0x01, 0x00 });
            var deviceData = _device.Read();

            if (deviceData.Status != HidDeviceData.ReadStatus.Success)
            {
                return;
            }

            var joyConState = JoyConInputUtils.ReadInput(deviceData.Data, Type);

            if (joyConState != null && !joyConState.Equals(CurrentState))
            {
                CurrentState = joyConState;
                Task.Run(() => DataUpdated?.Invoke(this, new JoyConDataUpdateEventArgs(this, joyConState)));

                if (IsPaired)
                {
                    //Splitting state changed?
                    if (_currentInSplittingState != (joyConState.SideLeftButton || joyConState.SideRightButton))
                    {
                        _currentInSplittingState = joyConState.SideLeftButton || joyConState.SideRightButton;
                        //Invoke splitting event async
                        Task.Run(() => Splitting?.Invoke(this, new JoyConSplittingEventArgs(
                                                             this, _currentInSplittingState ? SplittingType.ReadyToSplit : SplittingType.CancelSplitting
                                                             )));
                    }
                }
                else
                {
                    //Pairing state changed?
                    if (_currentInPairingState != joyConState.RearBackButton)
                    {
                        _currentInPairingState = joyConState.RearBackButton;
                        //Invoke ready to pair or paring cancel event async
                        Task.Run(() => Pairing?.Invoke(this, new JoyConPairingEventArgs(
                                                           this, _currentInPairingState ? PairingType.ReadyToPair : PairingType.CancelPairing
                                                           )));
                    }
                }
            }
        }
        public static VirtualJoystickData ToVirtualJoystickData(this JoyConState joyConState, IJoyCon joyCon)
        {
            var data = new VirtualJoystickData();

            if (joyCon is JoyConPair)
            {
                HandleJoyConPair(joyConState, (JoyConPair)joyCon, ref data);
            }
            else
            {
                HandleJoyCon(joyConState, ref data);
            }

            return(data);
        }
        private static void HandleJoyCon(JoyConState joyConState, ref VirtualJoystickData data)
        {
            if (joyConState is JoyConLeftState)
            {
                var state = (JoyConLeftState)joyConState;

                data.ButtonB = state.ButtonDown;
                data.ButtonX = state.ButtonUp;
                data.ButtonA = state.ButtonLeft;
                data.ButtonY = state.ButtonRight;

                data.BackButtonLeft  = state.SideLeftButton;
                data.BackButtonRight = state.SideRightButton;
                data.StartButton     = state.MinusButton;
                data.StickLeftButton = state.StickButton;

                data.LeftAxisX = JoyConConstants.LeftJoyConXMultiplier * state.StickHorizontal +
                                 JoyConConstants.LeftJoyConOffsetX;
                data.LeftAxisY = JoyConConstants.LeftJoyConYMultiplier * state.StickVertical +
                                 JoyConConstants.LeftJoyConOffsetY;
            }
            else if (joyConState is JoyConRightState)
            {
                var state = (JoyConRightState)joyConState;

                data.ButtonB = state.ButtonX;
                data.ButtonY = state.ButtonY;
                data.ButtonA = state.ButtonA;
                data.ButtonX = state.ButtonB;

                data.BackButtonLeft  = state.SideLeftButton;
                data.BackButtonRight = state.SideRightButton;
                data.StartButton     = state.PlusButton;
                data.StickLeftButton = state.StickButton;

                data.LeftAxisX = JoyConConstants.LeftJoyConXMultiplier * state.StickHorizontal +
                                 JoyConConstants.LeftJoyConOffsetX;
                data.LeftAxisY = JoyConConstants.LeftJoyConYMultiplier * state.StickVertical +
                                 JoyConConstants.LeftJoyConOffsetY;
            }
            else
            {
                throw new NotImplementedException();
            }
        }