private void MappingTrackers()
        {
            var role  = RoleMap.RoleInfo.MinValidRole;
            var index = (uint)1;

            while (true)
            {
                while (!RoleMap.RoleInfo.IsValidRole(role) || RoleMap.IsRoleMapped(role) || RoleMap.IsRoleBound(role))
                {
                    if (++role > RoleMap.RoleInfo.MaxValidRole)
                    {
                        return;
                    }
                }

                while (ViveRole.GetDeviceClass(index) != ETrackedDeviceClass.GenericTracker || RoleMap.IsDeviceMapped(index) || RoleMap.IsDeviceConnectedAndBound(index))
                {
                    if (++index >= ViveRole.MAX_DEVICE_COUNT)
                    {
                        return;
                    }
                }

                MappingRole(role++, index++);

                if (role > RoleMap.RoleInfo.MaxValidRole || index >= ViveRole.MAX_DEVICE_COUNT)
                {
                    return;
                }
            }
        }
        public override void OnConnectedDeviceChanged(uint deviceIndex, ETrackedDeviceClass deviceClass, string deviceSN, bool connected)
        {
            if (RoleMap.IsDeviceBound(deviceSN))
            {
                return;
            }

            if (connected)
            {
                if (deviceClass != ETrackedDeviceClass.GenericTracker)
                {
                    return;
                }

                // find unmapped role
                var role = RoleMap.RoleInfo.MinValidRole;
                while (!RoleMap.RoleInfo.IsValidRole(role) || RoleMap.IsRoleMapped(role))
                {
                    if (++role > RoleMap.RoleInfo.MaxValidRole)
                    {
                        return;
                    }
                }

                MappingRole(role, deviceIndex);
            }
            else
            {
                UnmappingDevice(deviceIndex);
            }
        }
Esempio n. 3
0
        private void MappingOthers()
        {
            // try mapping the rest of the devices
            var role        = HandRole.Controller3;
            var deviceIndex = 0u;

            while (role <= HandRole.Controller12 && deviceIndex < ViveRole.MAX_DEVICE_COUNT)
            {
                while (RoleMap.IsRoleMapped(role) || RoleMap.IsRoleBound(role))
                {
                    if (++role > HandRole.Controller12)
                    {
                        return;
                    }
                }

                while (!IsController(deviceIndex) || RoleMap.IsDeviceMapped(deviceIndex))
                {
                    if (++deviceIndex >= ViveRole.MAX_DEVICE_COUNT)
                    {
                        return;
                    }
                }

                MappingRole(role++, deviceIndex++);
            }
        }
Esempio n. 4
0
        private void MappingOtherControllers()
        {
            // mapping other controllers in order of device index
            var deviceIndex       = 0u;
            var firstFoundTracker = VRModule.INVALID_DEVICE_INDEX;
            var rightIndex        = RoleMap.GetMappedDeviceByRole(HandRole.RightHand);
            var leftIndex         = RoleMap.GetMappedDeviceByRole(HandRole.LeftHand);

            for (var role = RoleInfo.MinValidRole; role <= RoleInfo.MaxValidRole; ++role)
            {
                if (!RoleInfo.IsValidRole(role))
                {
                    continue;
                }
                if (role == HandRole.RightHand || role == HandRole.LeftHand)
                {
                    continue;
                }
                if (RoleMap.IsRoleBound(role))
                {
                    continue;
                }

                // find next valid device
                if (VRModule.IsValidDeviceIndex(deviceIndex))
                {
                    while (!IsController(deviceIndex) || RoleMap.IsDeviceConnectedAndBound(deviceIndex) || deviceIndex == rightIndex || deviceIndex == leftIndex)
                    {
                        if (!VRModule.IsValidDeviceIndex(firstFoundTracker) && IsTracker(deviceIndex))
                        {
                            firstFoundTracker = deviceIndex;
                        }
                        if (!VRModule.IsValidDeviceIndex(++deviceIndex))
                        {
                            break;
                        }
                    }
                }

                if (VRModule.IsValidDeviceIndex(deviceIndex))
                {
                    MappingRole(role, deviceIndex++);
                }
                else
                {
                    UnmappingRole(role);
                }
            }

            // if external camera is not mapped, try mapping first found tracker
            if (!RoleMap.IsRoleMapped(HandRole.ExternalCamera) && VRModule.IsValidDeviceIndex(firstFoundTracker) && !RoleMap.IsDeviceConnectedAndBound(firstFoundTracker))
            {
                MappingRole(HandRole.ExternalCamera, firstFoundTracker);
            }
        }
        public override void OnBindingChanged(TrackerRole role, string deviceSN, bool bound)
        {
            if (bound)
            {
                // it's possible that some device will be pushed out when binding
                MappingTrackers();
            }
            else
            {
                if (RoleMap.IsRoleMapped(role))
                {
                    UnmappingRole(role);

                    MappingTrackers();
                }
            }
        }
Esempio n. 6
0
        public override void OnBindingChanged(HandRole role, string deviceSN, bool bound)
        {
            if (!bound)
            {
                if (RoleMap.IsRoleMapped(role) && !IsController(RoleMap.GetMappedDeviceByRole(role)))
                {
                    UnmappingRole(role);
                }
            }

            if (handsAreMappedOrBound)
            {
                MappingOthers();
            }
            else
            {
                MappingHandsAndOthers();
            }
        }
Esempio n. 7
0
        public void Refresh()
        {
            // find tracked right/left hand index
            var deviceCount = VRModule.GetDeviceStateCount();
            var rightIndex  = VRModule.INVALID_DEVICE_INDEX;
            var leftIndex   = VRModule.INVALID_DEVICE_INDEX;

            for (uint deviceIndex = 0u; deviceIndex < deviceCount; ++deviceIndex)
            {
                var deviceState = VRModule.GetDeviceState(deviceIndex);
                if (deviceState.isConnected && deviceState.deviceClass == VRModuleDeviceClass.TrackedHand)
                {
                    if (deviceState.deviceModel.IsRight())
                    {
                        rightIndex = deviceIndex;
                    }
                    else if (deviceState.deviceModel.IsLeft())
                    {
                        leftIndex = deviceIndex;
                    }
                }
            }

            if (!RoleMap.IsRoleMapped(TrackedHandRole.RightHand))
            {
                if (rightIndex < deviceCount)
                {
                    MappingRoleIfUnbound(TrackedHandRole.RightHand, rightIndex);
                }
            }
            else if (!RoleMap.IsRoleBound(TrackedHandRole.RightHand))
            {
                if (rightIndex < deviceCount)
                {
                    MappingRoleIfUnbound(TrackedHandRole.RightHand, rightIndex);
                }
                else
                {
                    UnmappingRole(TrackedHandRole.RightHand);
                }
            }

            if (!RoleMap.IsRoleMapped(TrackedHandRole.LeftHand))
            {
                if (leftIndex < deviceCount)
                {
                    MappingRoleIfUnbound(TrackedHandRole.LeftHand, leftIndex);
                }
            }
            else if (!RoleMap.IsRoleBound(TrackedHandRole.LeftHand))
            {
                if (leftIndex < deviceCount)
                {
                    MappingRoleIfUnbound(TrackedHandRole.LeftHand, leftIndex);
                }
                else
                {
                    UnmappingRole(TrackedHandRole.LeftHand);
                }
            }
        }
Esempio n. 8
0
        // unmapping all and mapping only right/left hands
        private void MappingHandsAndOthers()
        {
            UnmappingAll();

            var system     = OpenVR.System;
            var rightIndex = ViveRole.INVALID_DEVICE_INDEX;
            var leftIndex  = ViveRole.INVALID_DEVICE_INDEX;

            var trackedControllerCount = 0;

            if (system != null)
            {
                leftIndex  = system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
                rightIndex = system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand);

                if (RoleMap.IsDeviceMapped(leftIndex))
                {
                    leftIndex = ViveRole.INVALID_DEVICE_INDEX;
                }
                if (RoleMap.IsDeviceMapped(rightIndex))
                {
                    rightIndex = ViveRole.INVALID_DEVICE_INDEX;
                }

                trackedControllerCount = (int)system.GetSortedTrackedDeviceIndicesOfClass(ETrackedDeviceClass.Controller, m_sortedDevices, 0);
            }

            if (ViveRole.IsValidIndex(rightIndex))
            {
                MappingRoleIfUnbound(HandRole.RightHand, rightIndex);
            }

            if (ViveRole.IsValidIndex(leftIndex) && leftIndex != rightIndex)
            {
                MappingRoleIfUnbound(HandRole.LeftHand, leftIndex);
            }

            if (!RoleMap.IsRoleMapped(HandRole.RightHand) && !RoleMap.IsRoleBound(HandRole.RightHand))
            {
                // find most right side controller
                for (var i = 0; i < trackedControllerCount; ++i)
                {
                    if (RoleMap.IsDeviceMapped(m_sortedDevices[i]))
                    {
                        continue;
                    }
                    MappingRole(HandRole.RightHand, m_sortedDevices[i]);
                    break;
                }
            }

            if (!RoleMap.IsRoleMapped(HandRole.LeftHand) && !RoleMap.IsRoleBound(HandRole.LeftHand))
            {
                // find most left side controller
                for (var i = trackedControllerCount - 1; i >= 0; --i)
                {
                    if (RoleMap.IsDeviceMapped(m_sortedDevices[i]))
                    {
                        continue;
                    }
                    MappingRole(HandRole.LeftHand, m_sortedDevices[i]);
                    break;
                }
            }

            MappingOthers();
        }