public static void BindAllCurrentDeviceClassMappings(VRModuleDeviceClass deviceClass)
        {
            for (int i = 0, imax = ViveRoleEnum.ValidViveRoleTable.Count; i < imax; ++i)
            {
                var roleMap  = ViveRole.GetMap(ViveRoleEnum.ValidViveRoleTable.GetValueByIndex(i));
                var roleInfo = roleMap.RoleValueInfo;
                for (int rv = roleInfo.MinValidRoleValue, rvmax = roleInfo.MaxValidRoleValue; rv <= rvmax; ++rv)
                {
                    if (!roleInfo.IsValidRoleValue(rv))
                    {
                        continue;
                    }
                    if (roleMap.IsRoleValueBound(rv))
                    {
                        continue;
                    }

                    var mappedDevice      = roleMap.GetMappedDeviceByRoleValue(rv);
                    var mappedDeviceState = VRModule.GetCurrentDeviceState(mappedDevice);
                    if (mappedDeviceState.deviceClass != deviceClass)
                    {
                        continue;
                    }

                    roleMap.BindDeviceToRoleValue(mappedDeviceState.serialNumber, rv);
                }
            }
        }
 public static void UnbindAllCurrentBindings()
 {
     for (int i = 0, imax = ViveRoleEnum.ValidViveRoleTable.Count; i < imax; ++i)
     {
         var roleMap = ViveRole.GetMap(ViveRoleEnum.ValidViveRoleTable.GetValueByIndex(i));
         roleMap.UnbindAll();
     }
 }
        public static int ApplyBindingConfigToRoleMap(params Type[] roleTypeFilter)
        {
            var appliedCount = 0;
            var filterUsed   = roleTypeFilter != null && roleTypeFilter.Length > 0;

            foreach (var roleData in s_bindingConfig.roles)
            {
                Type roleType;
                if (string.IsNullOrEmpty(roleData.type) || !ViveRoleEnum.ValidViveRoleTable.TryGetValue(roleData.type, out roleType))
                {
                    continue;
                }

                if (filterUsed)
                {
                    // apply filter
                    var filtered = false;
                    foreach (var t in roleTypeFilter)
                    {
                        if (roleType == t)
                        {
                            filtered = true; break;
                        }
                    }
                    if (!filtered)
                    {
                        continue;
                    }
                }

                var roleMap = ViveRole.GetMap(roleType);
                roleMap.UnbindAll();

                foreach (var binding in roleData.bindings)
                {
                    // bind device according to role_name first
                    // if role_name is invalid then role_value is used
                    int roleValue;
                    if (string.IsNullOrEmpty(binding.role_name) || !roleMap.RoleValueInfo.TryGetRoleValueByName(binding.role_name, out roleValue))
                    {
                        roleValue = binding.role_value;
                    }

                    roleMap.BindDeviceToRoleValue(binding.device_sn, roleValue);
                    ++appliedCount;
                }
            }

            return(appliedCount);
        }
Example #4
0
        // update type and value changes
        public void Update()
        {
            if (m_lockUpdate && (m_isTypeDirty || m_isValueDirty))
            {
                throw new Exception("Can't change value during onChange event callback");
            }

            var oldRoleType        = m_roleType;
            var oldRoleValue       = m_roleValue;
            var roleTypeChanged    = false;
            var roleValueChanged   = false;
            var deviceIndexChanged = false;

            if (m_isTypeDirty || m_roleType == null)
            {
                m_isTypeDirty = false;

                if (string.IsNullOrEmpty(m_roleTypeFullName) || !ViveRoleEnum.ValidViveRoleTable.TryGetValue(m_roleTypeFullName, out m_roleType))
                {
                    m_roleType = DefaultRoleType;
                }

                roleTypeChanged = oldRoleType != m_roleType;
            }

            // maintain m_roleMap cache
            // m_roleMap could be null on first update
            if (roleTypeChanged || m_roleMap == null)
            {
                if (m_onDeviceIndexChanged != null)
                {
                    if (m_roleMap != null)
                    {
                        m_roleMap.onRoleValueMappingChanged -= OnMappingChanged;
                        m_roleMap = ViveRole.GetMap(m_roleType);
                        m_roleMap.onRoleValueMappingChanged += OnMappingChanged;
                    }
                    else
                    {
                        m_roleMap     = ViveRole.GetMap(m_roleType);
                        m_deviceIndex = m_roleMap.GetMappedDeviceByRoleValue(m_roleValue); // update deviceIndex before first time listening to MappingChanged event
                        m_roleMap.onRoleValueMappingChanged += OnMappingChanged;
                    }
                }
                else
                {
                    m_roleMap = ViveRole.GetMap(m_roleType);
                }
            }

            if (m_isValueDirty || roleTypeChanged)
            {
                m_isValueDirty = false;

                var info = m_roleMap.RoleValueInfo;
                if (string.IsNullOrEmpty(m_roleValueName) || !info.TryGetRoleValueByName(m_roleValueName, out m_roleValue))
                {
                    m_roleValue = info.IsValidRoleValue(m_roleValueInt) ? m_roleValueInt : info.InvalidRoleValue;
                }

                roleValueChanged = oldRoleValue != m_roleValue;
            }

            if (roleTypeChanged || roleValueChanged)
            {
                if (m_onDeviceIndexChanged != null)
                {
                    var oldDeviceIndex = m_deviceIndex;
                    m_deviceIndex = m_roleMap.GetMappedDeviceByRoleValue(m_roleValue);

                    if (VRModule.IsValidDeviceIndex(oldDeviceIndex) || VRModule.IsValidDeviceIndex(m_deviceIndex))
                    {
                        deviceIndexChanged = oldDeviceIndex != m_deviceIndex;
                    }
                }

                m_lockUpdate = true;

                if (m_onChanged != null)
                {
                    m_onChanged();
                }

                if (m_onRoleChanged != null)
                {
                    m_onRoleChanged();
                }

                if (m_onRoleChangedEx != null)
                {
                    m_onRoleChangedEx(oldRoleType, oldRoleValue);
                }

                if (deviceIndexChanged)
                {
                    m_onDeviceIndexChanged(m_deviceIndex);
                }

                m_lockUpdate = false;
            }
        }
 public RCtrlState(Type roleEnumType, int roleValue)
 {
     m_map          = ViveRole.GetMap(roleEnumType);
     m_roleValue    = roleValue;
     m_roleEnumType = roleEnumType;
 }
        public static void LoadBindingConfigFromRoleMap(params Type[] roleTypeFilter)
        {
            var roleDataList = ListPool <RoleData> .Get();

            var filterUsed = roleTypeFilter != null && roleTypeFilter.Length > 0;

            if (filterUsed)
            {
                roleDataList.AddRange(s_bindingConfig.roles);
            }

            for (int i = 0, imax = ViveRoleEnum.ValidViveRoleTable.Count; i < imax; ++i)
            {
                var roleType = ViveRoleEnum.ValidViveRoleTable.GetValueByIndex(i);
                var roleName = ViveRoleEnum.ValidViveRoleTable.GetKeyByIndex(i);
                var roleMap  = ViveRole.GetMap(roleType);

                if (filterUsed)
                {
                    // apply filter
                    var filtered = false;
                    foreach (var t in roleTypeFilter)
                    {
                        if (roleType == t)
                        {
                            filtered = true; break;
                        }
                    }
                    if (!filtered)
                    {
                        continue;
                    }
                }

                if (roleMap.BindingCount > 0)
                {
                    var bindingTable = roleMap.BindingTable;

                    var roleData = new RoleData()
                    {
                        type     = roleName,
                        bindings = new Binding[bindingTable.Count],
                    };

                    for (int j = 0, jmax = bindingTable.Count; j < jmax; ++j)
                    {
                        var binding = new Binding();
                        binding.device_sn  = bindingTable.GetKeyByIndex(j);
                        binding.role_value = bindingTable.GetValueByIndex(j);
                        binding.role_name  = roleMap.RoleValueInfo.GetNameByRoleValue(binding.role_value);

                        // save the device_model for better recognition of the device
                        if (VRModule.IsDeviceConnected(binding.device_sn))
                        {
                            binding.device_model = VRModule.GetCurrentDeviceState(VRModule.GetConnectedDeviceIndex(binding.device_sn)).deviceModel;
                            s_modelHintTable[binding.device_sn] = binding.device_model;
                        }
                        else if (!s_modelHintTable.TryGetValue(binding.device_sn, out binding.device_model))
                        {
                            binding.device_model = VRModuleDeviceModel.Unknown;
                        }

                        roleData.bindings[j] = binding;
                    }

                    if (filterUsed)
                    {
                        // merge with existing role data
                        var roleDataIndex = roleDataList.FindIndex((item) => item.type == roleName);
                        if (roleDataIndex >= 0)
                        {
                            roleDataList[roleDataIndex] = roleData;
                        }
                        else
                        {
                            roleDataList.Add(roleData);
                        }
                    }
                    else
                    {
                        roleDataList.Add(roleData);
                    }
                }
                else
                {
                    if (roleDataList.Count > 0)
                    {
                        // don't write to config if no bindings
                        roleDataList.RemoveAll((item) => item.type == roleName);
                    }
                }
            }

            s_bindingConfig.roles = roleDataList.ToArray();

            ListPool <RoleData> .Release(roleDataList);
        }
Example #7
0
        public uint GetDeviceIndex()
        {
            Update();

            return(ViveRole.GetMap(m_roleType).GetMappedDeviceByRoleValue(m_roleValue));
        }