Exemple #1
0
        static InputDeviceStyle DetectDeviceStyle(UnityInput.InputDevice unityDevice)
        {
            switch (unityDevice)
            {
            case UnityInput.XInput.XInputController _:
                return(InputDeviceStyle.XboxOne);

            case UnityInput.DualShock.DualShockGamepad _:
                return(InputDeviceStyle.PlayStation4);

                                #if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_WSA
            case UnityInput.Switch.SwitchProControllerHID _:
                return(InputDeviceStyle.NintendoSwitch);
                                #endif
                                #if UNITY_EDITOR || UNITY_IOS || UNITY_TVOS
            case UnityInput.iOS.iOSGameController _:
                return(InputDeviceStyle.AppleMFi);
                                #endif
                                #if (UNITY_STANDALONE || UNITY_EDITOR) && UNITY_ENABLE_STEAM_CONTROLLER_SUPPORT
            case UnityInput.Steam.SteamController _:
                return(InputDeviceStyle.Steam);
                                #endif
            default:
                return(InputDeviceStyle.Unknown);
            }
        }
Exemple #2
0
 void OnDeviceChanged(UnityEngine.InputSystem.InputDevice device, InputDeviceChange change)
 {
     if (change == InputDeviceChange.Added)
     {
         leftTouchController  = (OculusTouchController)OculusTouchController.leftHand;
         rightTouchController = (OculusTouchController)OculusTouchController.rightHand;
         hmd = InputSystem.GetDevice <OculusHMD>();
     }
 }
Exemple #3
0
        void DetachDevice(UnityInput.InputDevice unityDevice)
        {
            NewUnityInputDevice inputDevice;

            if (internalDevices.TryGetValue(unityDevice.deviceId, out inputDevice))
            {
                internalDevices.Remove(unityDevice.deviceId);
                InputManager.DetachDevice(inputDevice);
            }
        }
Exemple #4
0
    private DemoPlayerController FindPlayerControllerForDevice(InputDevice device)
    {
        var user = InputUser.FindUserPairedToDevice(device);

        if (user == null)
        {
            return(null);
        }

        return(FindPlayerControllerForUser(user.Value));
    }
Exemple #5
0
        void OnInputSystemOnDeviceChange(UnityInput.InputDevice unityDevice, UnityInput.InputDeviceChange inputDeviceChange)
        {
            // TODO: Not 100% sure if we also need to handle some of the other events.
            switch (inputDeviceChange)
            {
            case UnityInput.InputDeviceChange.Added:
                AttachDevice(unityDevice);
                break;

            case UnityInput.InputDeviceChange.Removed:
                DetachDevice(unityDevice);
                break;
            }
        }
        public Controller(int number, string name, Color color, Inputs.InputDevice device, Inputs.InputControlScheme scheme)
        {
            _name   = name;
            _color  = color;
            _number = number;
            _device = device;
            //_user = Inputs.Users.InputUser.CreateUserWithoutPairedDevices();
            //Inputs.Users.InputUser.PerformPairingWithDevice(_device, _user);

            // Each player gets a separate action setup. This makes the state of actions and bindings
            // local to each player and also ensures we're not stepping on the action setup used by
            // DemoGame itself for the main menu (where we are not using control schemes and just blindly
            // bind to whatever devices are available locally).
            _scheme                = scheme;
            _actionMap             = new ActionMap();
            _actionMap.bindingMask = new Inputs.InputBinding {
                groups = _scheme.bindingGroup
            };
            Enable();
        }
Exemple #7
0
        void AttachDevice(UnityInput.InputDevice unityDevice)
        {
            var unityGamepad = unityDevice as UnityInput.Gamepad;

            if (unityGamepad != null)
            {
                if (internalDevices.ContainsKey(unityDevice.deviceId))
                {
                    return;
                }

                var inputDevice = new NewUnityInputDevice(unityGamepad);
                internalDevices.Add(unityDevice.deviceId, inputDevice);
                InputManager.AttachDevice(inputDevice);
            }

            // else
            // {
            //  Debug.Log( $"Discarding device: {unityDevice.displayName}" );
            // }
        }
Exemple #8
0
    /// <summary>
    /// Called when there's a change in the input user setup in the system.
    /// </summary>
    /// <param name="user"></param>
    /// <param name="change"></param>
    /// <param name="device"></param>
    private void OnUserChange(InputUser user, InputUserChange change, InputDevice device)
    {
        var player = FindPlayerControllerForUser(user);

        switch (change)
        {
        // A player has switched accounts. This will only happen on platforms that have user account
        // management (PS4, Xbox, Switch). On PS4, for example, this can happen at any time by the
        // player pressing the PS4 button and switching accounts. We simply update the information
        // we display for the player's active user account.
        case InputUserChange.AccountChanged:
        {
            if (player != null)
            {
                player.OnUserAccountChanged();
            }
            break;
        }

        // If the user has canceled account selection, we remove the user if there's no devices
        // already paired to it. This usually happens when a player initiates a join on a device on
        // Xbox or Switch, has the account picker come up, but then cancels instead of making an
        // account selection. In this case, we want to cancel the join.
        // NOTE: We are only adding DemoPlayerControllers once device pairing is complete
        case InputUserChange.AccountSelectionCanceled:
        {
            if (user.pairedDevices.Count == 0)
            {
                Debug.Assert(FindPlayerControllerForUser(user) == null);
                user.UnpairDevicesAndRemoveUser();
            }
            break;
        }

        // An InputUser gained a new device. If we're in the lobby and don't yet have a player
        // for the user, it means a new player has joined. We don't join players until they have
        // a device paired to them which is why we ignore InputUserChange.Added and only react
        // to InputUserChange.DevicePaired instead.
        case InputUserChange.DevicePaired:
        {
            if (state == State.InLobby && player == null)
            {
                OnPlayerJoins(user);
            }
            else if (player != null)
            {
                player.OnDevicesOrBindingsHaveChanged();
            }
            break;
        }

        // Some player ran out of battery or unplugged a wired device.
        case InputUserChange.DeviceLost:
        {
            Debug.Assert(player != null);
            player.OnDeviceLost();

            ////REVIEW: should we unjoin a user when losing devices in the lobby?
            ////TODO: we need a way for other players to be able to resolve the situation

            // If we're currently in-game, we pause the game until the player has re-gained control.
            if (isInGame)
            {
                PauseGame();
            }

            break;
        }

        // Some player has customized controls or had previously customized controls loaded.
        case InputUserChange.BindingsChanged:
        {
            player.OnDevicesOrBindingsHaveChanged();
            break;
        }
        }
    }
 public void AssociateInputDeviceWithUser(InputDevice device, ulong userHandle, string userName = null, string userId = null)
 {
     AssociateInputDeviceWithUser(device.deviceId, userHandle, userName, userId);
 }
Exemple #10
0
 public void SetDeviceCommandCallback(InputDevice device, DeviceCommandCallback callback)
 {
     SetDeviceCommandCallback(device.deviceId, callback);
 }
            private unsafe void OnEvent(InputEventPtr eventPtr, InputDevice device)
            {
                // Ignore if not a state event.
                if (!eventPtr.IsA <StateEvent>() && !eventPtr.IsA <DeltaStateEvent>())
                {
                    return;
                }

                // Go through controls and see if there's anything interesting in the event.
                var controls              = device.allControls;
                var controlCount          = controls.Count;
                var haveChangedCandidates = false;

                for (var i = 0; i < controlCount; ++i)
                {
                    var control = controls[i];

                    // Skip controls that have no state in the event.
                    var statePtr = control.GetStatePtrFromStateEvent(eventPtr);
                    if (statePtr == null)
                    {
                        continue;
                    }

                    // If the control that cancels has been actuated, abort the operation now.
                    if (!string.IsNullOrEmpty(m_CancelBinding) && InputControlPath.Matches(m_CancelBinding, control) &&
                        !control.CheckStateIsAtDefault(statePtr) && control.HasValueChangeInState(statePtr))
                    {
                        OnCancel();
                        break;
                    }

                    // Skip noisy controls.
                    if (control.noisy && (m_Flags & Flags.DontIgnoreNoisyControls) == 0)
                    {
                        continue;
                    }

                    // If controls have to match a certain path, check if this one does.
                    if (m_IncludePathCount > 0 && !HavePathMatch(control, m_IncludePaths, m_IncludePathCount))
                    {
                        continue;
                    }

                    // If controls must not match certain path, make sure the control doesn't.
                    if (m_ExcludePathCount > 0 && HavePathMatch(control, m_ExcludePaths, m_ExcludePathCount))
                    {
                        continue;
                    }

                    // If we're expecting controls of a certain type, skip if control isn't of
                    // the right type.
                    if (m_ControlType != null && !m_ControlType.IsInstanceOfType(control))
                    {
                        continue;
                    }

                    // If we're expecting controls to be based on a specific layout, skip if control
                    // isn't based on that layout.
                    if (!m_ExpectedLayout.IsEmpty() &&
                        m_ExpectedLayout != control.m_Layout &&
                        !InputControlLayout.s_Layouts.IsBasedOn(m_ExpectedLayout, control.m_Layout))
                    {
                        continue;
                    }

                    // Skip controls that are in their default state.
                    // NOTE: This is the cheapest check with respect to looking at actual state. So
                    //       do this first before looking further at the state.
                    if (control.CheckStateIsAtDefault(statePtr))
                    {
                        continue;
                    }

                    // Skip controls that have no effective value change.
                    // NOTE: This will run the full processor stack and is more involved.
                    if (!control.HasValueChangeInState(statePtr))
                    {
                        continue;
                    }

                    // If we have a magnitude threshold, see if control passes it.
                    var magnitude = -1f;
                    if (m_MagnitudeThreshold >= 0f)
                    {
                        magnitude = control.EvaluateMagnitude(statePtr);
                        if (magnitude >= 0 && magnitude < m_MagnitudeThreshold)
                        {
                            continue; // No, so skip.
                        }
                    }

                    // Compute score.
                    float score;
                    if (m_OnComputeScore != null)
                    {
                        score = m_OnComputeScore(control, eventPtr);
                    }
                    else
                    {
                        score = magnitude;

                        // We don't want synthetic controls to not be bindable at all but they should
                        // generally cede priority to controls that aren't synthetic. So we bump all
                        // scores of controls that aren't synthetic.
                        if (!control.synthetic)
                        {
                            score += 1f;
                        }
                    }

                    // Control is a candidate.
                    // See if we already singled the control out as a potential candidate.
                    var candidateIndex = m_Candidates.IndexOf(control);
                    if (candidateIndex != -1)
                    {
                        // Yes, we did. So just check whether it became a better candidate than before.
                        if (m_Scores[candidateIndex] < score)
                        {
                            haveChangedCandidates    = true;
                            m_Scores[candidateIndex] = score;

                            if (m_WaitSecondsAfterMatch > 0)
                            {
                                m_LastMatchTime = InputRuntime.s_Instance.currentTime;
                            }
                        }
                    }
                    else
                    {
                        // No, so add it.
                        var candidateCount = m_Candidates.Count;
                        m_Candidates.Add(control);
                        ArrayHelpers.AppendWithCapacity(ref m_Scores, ref candidateCount, score);
                        haveChangedCandidates = true;

                        if (m_WaitSecondsAfterMatch > 0)
                        {
                            m_LastMatchTime = InputRuntime.s_Instance.currentTime;
                        }
                    }
                }

                if (haveChangedCandidates && !canceled)
                {
                    // If we have a callback that wants to control matching, leave it to the callback to decide
                    // whether the rebind is complete or not. Otherwise, just complete.
                    if (m_OnPotentialMatch != null)
                    {
                        SortCandidatesByScore();
                        m_OnPotentialMatch(this);
                    }
                    else if (m_WaitSecondsAfterMatch <= 0)
                    {
                        OnComplete();
                    }
                    else
                    {
                        SortCandidatesByScore();
                    }
                }
            }
Exemple #12
0
        private void SendDevice(InputDevice device)
        {
            var message = NewDeviceMsg.Create(device);

            Send(message);
        }
 /// <summary>
 /// Set reference to point to one specific device.
 /// </summary>
 /// <param name="device"></param>
 public void Set(InputDevice device)
 {
     throw new NotImplementedException();
 }