public void RevertCustomizations(ControlScheme controlScheme)
 {
     if (m_ControlSchemeCopies != null)
     {
         for (var i = 0; i < m_ControlSchemeCopies.Count; ++i)
         {
             if (m_ControlSchemeCopies[i] == controlScheme)
             {
                 m_ControlSchemeCopies[i] = m_ControlSchemes[i].Clone();
                 break;
             }
         }
     }
 }
Beispiel #2
0
        private void Assign(ControlScheme controlScheme, List <InputDevice> devices)
        {
            m_ControlScheme = controlScheme;

            // Create state for every device.
            var deviceStates = new List <InputState>();

            foreach (var device in devices)
            {
                deviceStates.Add(new InputState(device));
            }
            m_DeviceStates = deviceStates;
            RefreshBindings();

            ResetControlsForCurrentReceivers();

            Reset();

            if (onStatusChange != null)
            {
                onStatusChange.Invoke();
            }
        }
Beispiel #3
0
        /// <summary>
        /// Find the best control scheme for the available devices and initialize the action map input.
        ///
        /// It's important to note that an action map that can use either of two devices should have two of the same device
        /// listed in the control scheme. Otherwise, if the ActionMapInput is initialized with those two devices, the first
        /// device state found will override the other's device state. This becomes apparent when GetDeviceStateForDeviceSlot
        /// is called.
        ///
        /// Ex. Having a left and right VR controller where the action map can accept either controller's trigger button
        /// would cause issues if only one device was listed in the action map. Usually, this shows up as a ping-ponging
        /// issue where the ActionMapInput keeps getting re-initialized and binds different devices.
        /// </summary>
        /// <param name="availableDevices">Available devices in the system</param>
        /// <param name="requiredDevice">Required device for scheme</param>
        /// <returns></returns>
        public bool TryInitializeWithDevices(IEnumerable <InputDevice> availableDevices, IEnumerable <InputDevice> requiredDevices = null)
        {
            int bestScheme = -1;
            List <InputDevice> bestFoundDevices = null;
            float mostRecentTime = -1;

            List <InputDevice> foundDevices = new List <InputDevice>();

            for (int scheme = 0; scheme < actionMap.controlSchemes.Count; scheme++)
            {
                float timeForScheme = -1;
                foundDevices.Clear();
                var  deviceSlots = actionMap.controlSchemes[scheme].deviceSlots;
                bool matchesAll  = true;
                foreach (var deviceSlot in deviceSlots)
                {
                    InputDevice foundDevice     = null;
                    float       foundDeviceTime = -1;
                    foreach (var device in availableDevices)
                    {
                        if (deviceSlot.type.value.IsInstanceOfType(device) && device.lastEventTime > foundDeviceTime &&
                            (deviceSlot.tagIndex == -1 || deviceSlot.tagIndex == device.tagIndex)
                            )
                        {
                            foundDevice     = device;
                            foundDeviceTime = device.lastEventTime;
                        }
                    }
                    if (foundDevice != null)
                    {
                        foundDevices.Add(foundDevice);
                        timeForScheme = Mathf.Max(timeForScheme, foundDeviceTime);
                    }
                    else
                    {
                        matchesAll = false;
                        break;
                    }
                }

                // Don't switch schemes in the case where we require a specific device for an event that is getting processed.
                if (matchesAll && requiredDevices != null && requiredDevices.Any())
                {
                    foreach (var device in requiredDevices)
                    {
                        if (!foundDevices.Contains(device))
                        {
                            matchesAll = false;
                            break;
                        }
                    }
                }

                if (!matchesAll)
                {
                    continue;
                }

                // If we reach this point we know that control scheme both matches required and matches all.
                if (timeForScheme > mostRecentTime)
                {
                    bestScheme       = scheme;
                    bestFoundDevices = new List <InputDevice>(foundDevices);
                    mostRecentTime   = timeForScheme;
                }
            }

            if (bestScheme == -1)
            {
                return(false);
            }

            ControlScheme matchingControlScheme = actionMap.controlSchemes[bestScheme];

            Assign(matchingControlScheme, bestFoundDevices);
            return(true);
        }
        public bool TryInitializeWithDevices(IEnumerable <InputDevice> availableDevices)
        {
            int bestScheme = -1;
            List <InputDevice> bestFoundDevices = null;
            float mostRecentTime = -1;

            List <InputDevice> foundDevices = new List <InputDevice>();

            for (int scheme = 0; scheme < actionMap.controlSchemes.Count; scheme++)
            {
                float timeForScheme = -1;
                foundDevices.Clear();
                var  types      = actionMap.controlSchemes[scheme].deviceTypes;
                bool matchesAll = true;
                foreach (var type in types)
                {
                    InputDevice foundDevice     = null;
                    float       foundDeviceTime = -1;
                    foreach (var device in availableDevices)
                    {
                        if (type.IsInstanceOfType(device) && device.lastEventTime > foundDeviceTime)
                        {
                            foundDevice     = device;
                            foundDeviceTime = device.lastEventTime;
                        }
                    }
                    if (foundDevice != null)
                    {
                        foundDevices.Add(foundDevice);
                        timeForScheme = Mathf.Max(timeForScheme, foundDeviceTime);
                    }
                    else
                    {
                        matchesAll = false;
                        break;
                    }
                }
                if (!matchesAll)
                {
                    continue;
                }

                // If we reach this point we know that control scheme both matches required and matches all.
                if (timeForScheme > mostRecentTime)
                {
                    bestScheme       = scheme;
                    bestFoundDevices = new List <InputDevice>(foundDevices);
                    mostRecentTime   = timeForScheme;
                }
            }

            if (bestScheme == -1)
            {
                return(false);
            }

            ControlScheme matchingControlScheme = actionMap.controlSchemes[bestScheme];

            Assign(matchingControlScheme, bestFoundDevices);
            return(true);
        }