Example #1
0
            public override void UpdateRenderModel()
            {
                if (!ChangeProp.Set(ref m_index, hook.GetModelDeviceIndex()))
                {
                    return;
                }

                if (VRModule.IsValidDeviceIndex(m_index))
                {
                    // create object for render model
                    if (m_model == null)
                    {
                        var go = new GameObject("Model");
                        go.transform.SetParent(hook.transform, false);
                        m_model = go.AddComponent <VIUSteamVRRenderModel>();
                    }

                    // set render model index
                    m_model.gameObject.SetActive(true);
                    m_model.shaderOverride = hook.overrideShader;
                    m_model.SetDeviceIndex(m_index);
                }
                else
                {
                    // deacitvate object for render model
                    if (m_model != null)
                    {
                        m_model.gameObject.SetActive(false);
                    }
                }
            }
        public static void SortDeviceIndicesByDirection(List <uint> deviceList, RigidPose sortingReference)
        {
            if (deviceList == null || deviceList.Count == 0)
            {
                return;
            }

            for (int i = 0, imax = deviceList.Count; i < imax; ++i)
            {
                var deviceIndex = deviceList[i];
                if (!VRModule.IsValidDeviceIndex(deviceIndex))
                {
                    continue;
                }

                var deviceState = VRModule.GetCurrentDeviceState(deviceIndex);
                if (deviceState.isConnected)
                {
                    var localPos = sortingReference.InverseTransformPoint(deviceState.pose.pos);
                    s_deviceDirPoint[deviceIndex] = GetDirectionPoint(new Vector2(localPos.x, localPos.z));
                }
                else
                {
                    s_deviceDirPoint[deviceIndex] = -1f;
                }
            }

            deviceList.Sort(CompareDirection);
        }
    private void Update()
    {
        if (connectedDeviceChanged)
        {
            connectedDeviceChanged = false;

            OnConnectedDeviceChanged.Invoke();
        }

        if (previousScannedDevice != currentScannedDevice)
        {
            OnScanning.Invoke(0f);
        }
        else if (VRModule.IsValidDeviceIndex(currentScannedDevice) && sentDevice != currentScannedDevice)
        {
            var scannedDuration = Time.time - lastScannedChangedTime;
            if (scannedDuration > scanDuration)
            {
                if (!VRModule.IsValidDeviceIndex(sentDevice) && scannedReticle != null)
                {
                    scannedReticle.gameObject.SetActive(true);
                    scannedReticle.position = VivePose.GetPose(currentScannedDevice, VROrigin).pos;
                }

                OnDeviceScanned.Invoke(sentDevice = currentScannedDevice);
                OnScanning.Invoke(0f);
            }
            else
            {
                OnScanning.Invoke(scannedDuration / scanDuration);
            }
        }
    }
 private void UpdateDefaultModel()
 {
     if (ChangeProp.Set(ref m_currentDeviceIndex, GetCurrentDeviceIndex()))
     {
         if (VRModule.IsValidDeviceIndex(m_currentDeviceIndex))
         {
             if (ChangeProp.Set(ref m_currentLoadedStaticModel, VRModule.GetCurrentDeviceState(m_currentDeviceIndex).deviceModel))
             {
                 ReloadedStaticModel(m_currentLoadedStaticModel);
             }
             else
             {
                 if (m_modelObj != null)
                 {
                     m_modelObj.SetActive(true);
                 }
             }
         }
         else
         {
             if (m_modelObj != null)
             {
                 m_modelObj.SetActive(false);
             }
         }
     }
 }
Example #5
0
 public void OnPointerEnter(PointerEventData eventData)
 {
     if (onPointed != null && VRModule.IsValidDeviceIndex(m_mappedDevice))
     {
         onPointed.Invoke(m_roleValue);
     }
 }
Example #6
0
    void Start()
    {
        var RightHand = ViveRole.GetDeviceIndex(HandRole.RightHand);

        if (!VRModule.IsValidDeviceIndex(RightHand))
        {
            return;
        }
    }
        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);
            }
        }
Example #8
0
 public int GetMappedRoleValueByDevice(uint deviceIndex)
 {
     if (VRModule.IsValidDeviceIndex(deviceIndex))
     {
         return(m_index2role[deviceIndex]);
     }
     else
     {
         return(m_info.InvalidRoleValue);
     }
 }
            public override void UpdateRenderModel()
            {
                var index = hook.GetModelDeviceIndex();

                if (!VRModule.IsValidDeviceIndex(index))
                {
                    if (m_model != null)
                    {
                        m_model.SetActive(false);
                    }
                }
                else
                {
                    var loadModelEnum = VRModuleDeviceModel.Unknown;
                    if (hook.m_overrideModel != OverrideModelEnum.DontOverride)
                    {
                        loadModelEnum = (VRModuleDeviceModel)hook.m_overrideModel;
                    }
                    else
                    {
                        loadModelEnum = VRModule.GetCurrentDeviceState(index).deviceModel;
                    }

                    if (ChangeProp.Set(ref m_loadedModelEnum, loadModelEnum))
                    {
                        CleanUpRenderModel();

                        var prefab = Resources.Load <GameObject>("Models/VIUModel" + m_loadedModelEnum.ToString());
                        if (prefab != null)
                        {
                            m_model = Instantiate(prefab);
                            m_model.transform.SetParent(hook.transform, false);
                            m_model.gameObject.name = "VIUModel" + m_loadedModelEnum.ToString();

                            if (hook.m_overrideShader != null)
                            {
                                var renderer = m_model.GetComponentInChildren <Renderer>();
                                if (renderer != null)
                                {
                                    renderer.material.shader = hook.m_overrideShader;
                                }
                            }
                        }
                    }

                    if (m_model != null)
                    {
                        m_model.SetActive(true);
                    }
                }
            }
        public virtual void OnNewPoses()
        {
            var deviceIndex = m_viveRole.GetDeviceIndex();

            if (VRModule.IsValidDeviceIndex(deviceIndex))
            {
                m_staticExCamPose = VivePose.GetPose(deviceIndex);
            }

            if (isQuadViewActive)
            {
                RigidPose.SetPose(transform, m_staticExCamPose, m_origin);
            }
        }
 public static Vector3 GetAngularVelocity(uint deviceIndex, Transform origin = null)
 {
     if (!VRModule.IsValidDeviceIndex(deviceIndex))
     {
         return(Vector3.zero);
     }
     else if (origin == null)
     {
         return(VRModule.GetCurrentDeviceState(deviceIndex).angularVelocity);
     }
     else
     {
         return(origin.TransformVector(VRModule.GetCurrentDeviceState(deviceIndex).angularVelocity));
     }
 }
Example #12
0
    public virtual void OnNewPoses()
    {
        var deviceIndex = m_viveRole.GetDeviceIndex();
        var deviceValid = VRModule.IsValidDeviceIndex(deviceIndex);

        if (deviceValid)
        {
            m_staticExCamPose = VivePose.GetPose(deviceIndex);
        }

        if (deviceValid || m_staticExCamEnabled)
        {
            Pose.SetPose(transform, m_staticExCamPose, m_origin);
        }
    }
Example #13
0
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.M) && Input.GetKey(KeyCode.RightShift))
        {
            if (!m_staticExCamEnabled && !VRModule.IsValidDeviceIndex(m_viveRole.GetDeviceIndex()))
            {
                m_quadViewEnabled    = false;
                m_staticExCamEnabled = true;
            }

            m_quadViewEnabled = !m_quadViewEnabled;

            UpdateExCamActivity();
        }
    }
        private void UpdateModel()
        {
            if (m_isQuiting)
            {
                return;
            }

            var overrideModelChanged = ChangeProp.Set(ref m_currentOverrideModel, m_overrideModel);

            if (m_currentOverrideModel == OverrideModelEnum.DontOverride)
            {
                switch (VRModule.activeModule)
                {
#if VIU_STEAMVR
                case VRModuleActiveEnum.SteamVR:
                    UpdateSteamVRModel();
                    break;
#endif
#if VIU_WAVEVR
                case VRModuleActiveEnum.WaveVR:
                    UpdateWaveVRModel();
                    break;
#endif
                case VRModuleActiveEnum.Uninitialized:
                    if (m_modelObj != null)
                    {
                        m_modelObj.SetActive(false);
                    }
                    break;

                default:
                    UpdateDefaultModel();
                    break;
                }
            }
            else
            {
                if (overrideModelChanged)
                {
                    ReloadedStaticModel((VRModuleDeviceModel)m_currentOverrideModel);
                }

                if (ChangeProp.Set(ref m_currentDeviceIndex, GetCurrentDeviceIndex()) && m_modelObj != null)
                {
                    m_modelObj.SetActive(VRModule.IsValidDeviceIndex(m_currentDeviceIndex));
                }
            }
        }
        private void UpdateSteamVRModel()
        {
            if (ChangeProp.Set(ref m_currentDeviceIndex, GetCurrentDeviceIndex()))
            {
                if (VRModule.IsValidDeviceIndex(m_currentDeviceIndex))
                {
                    if (m_modelObj != null && m_renderModel == null)
                    {
                        CleanUpModelObj();
                    }

                    if (m_modelObj == null)
                    {
                        // find SteamVR_RenderModel in child object
                        for (int i = 0, imax = transform.childCount; i < imax; ++i)
                        {
                            if ((m_renderModel = GetComponentInChildren <VIUSteamVRRenderModel>()) != null)
                            {
                                m_modelObj = m_renderModel.gameObject;
                                break;
                            }
                        }
                        // create SteamVR_RenderModel in child object if not found
                        if (m_renderModel == null)
                        {
                            m_modelObj = new GameObject("Model");
                            m_modelObj.transform.SetParent(transform, false);
                            m_renderModel = m_modelObj.AddComponent <VIUSteamVRRenderModel>();
                        }

                        if (m_overrideShader != null)
                        {
                            m_renderModel.shaderOverride = m_overrideShader;
                        }
                    }

                    m_modelObj.SetActive(true);
                    m_renderModel.SetDeviceIndex(m_currentDeviceIndex);
                }
                else
                {
                    if (m_modelObj != null)
                    {
                        m_modelObj.SetActive(false);
                    }
                }
            }
        }
Example #16
0
            // both roleValue and deviceIndex must be valid
            // ignore binding state
            private void InternalMapping(int roleValue, uint deviceIndex)
            {
                if (m_lockInternalMapping)
                {
                    throw new Exception("Recursive calling InternalMapping");
                }
                m_lockInternalMapping = true;

                var previousRoleValue = m_index2role[deviceIndex];

                if (roleValue == previousRoleValue)
                {
                    m_lockInternalMapping = false;
                    return;
                }

                if (m_info.IsValidRoleValue(previousRoleValue))
                {
                    m_lockInternalMapping = false;
                    InternalUnmapping(previousRoleValue, deviceIndex);
                    m_lockInternalMapping = true;
                }

                var roleOffset          = m_info.RoleValueToRoleOffset(roleValue);
                var previousDeviceIndex = m_role2index[roleOffset];
                var eventArg            = new MappingChangedEventArg()
                {
                    roleValue           = roleValue,
                    previousDeviceIndex = previousDeviceIndex,
                    currentDeviceIndex  = deviceIndex,
                };

                m_role2index[roleOffset]  = deviceIndex;
                m_index2role[deviceIndex] = roleValue;

                if (VRModule.IsValidDeviceIndex(previousDeviceIndex))
                {
                    m_index2role[previousDeviceIndex] = m_info.InvalidRoleValue;
                }

                if (onRoleValueMappingChanged != null)
                {
                    onRoleValueMappingChanged(this, eventArg);
                }

                m_lockInternalMapping = false;
            }
Example #17
0
 public static Pose GetPose(uint deviceIndex, Transform origin = null)
 {
     if (!VRModule.IsValidDeviceIndex(deviceIndex))
     {
         return(default(Pose));
     }
     else if (origin == null)
     {
         return(VRModule.GetCurrentDeviceState(deviceIndex).pose);
     }
     else
     {
         var rawPose = new Pose(origin) * VRModule.GetCurrentDeviceState(deviceIndex).pose;
         rawPose.pos.Scale(origin.localScale);
         return(rawPose);
     }
 }
        public static VRModuleDeviceModel GetDeviceModelHint(string deviceSN)
        {
            var deviceIndex = VRModule.GetConnectedDeviceIndex(deviceSN);

            if (VRModule.IsValidDeviceIndex(deviceIndex))
            {
                return(VRModule.GetCurrentDeviceState(deviceIndex).deviceModel);
            }

            VRModuleDeviceModel deviceModel;

            if (s_modelHintTable.TryGetValue(deviceSN, out deviceModel))
            {
                return(deviceModel);
            }

            return(VRModuleDeviceModel.Unknown);
        }
        private bool TryGetAndTouchDeviceIndexByType(WVR_DeviceType type, out uint deviceIndex)
        {
            if (type < 0 || (int)type >= s_type2index.Length)
            {
                deviceIndex = INVALID_DEVICE_INDEX;
                return(false);
            }

            deviceIndex = s_type2index[(int)type];
            if (VRModule.IsValidDeviceIndex(deviceIndex))
            {
                m_index2deviceTouched[deviceIndex] = true;
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #20
0
            // deviceSN must be valid
            // device must be bound
            // device can be whether connected or not
            private void InternalUnbind(string deviceSN, int boundRoleValue)
            {
                var deviceIndex = VRModule.GetConnectedDeviceIndex(deviceSN);

                if (!m_sn2role.Remove(deviceSN))
                {
                    throw new Exception("device([" + deviceIndex + "]" + deviceSN + ") already unbound");
                }

                if (VRModule.IsValidDeviceIndex(deviceIndex))
                {
                    InternalRemoveRoleBoundDevice(deviceSN, deviceIndex, boundRoleValue);
                }

                if (m_handler != null)
                {
                    m_handler.OnBindingRoleValueChanged(deviceSN, true, boundRoleValue, false, m_info.InvalidRoleValue);
                }
            }
Example #21
0
    /// <summary>
    /// When scalingActivated is true and both triggers are pressed,
    /// scale the game object base on the change of distance between two controllers
    /// </summary>
    void Update()
    {
        if (scalingActivated &&
            ViveInput.GetPressEx(HandRole.RightHand, ControllerButton.Trigger) &&
            ViveInput.GetPressEx(HandRole.LeftHand, ControllerButton.Trigger))
        // Conditions for active scaling
        {
            var rightHandIndex = ViveRole.GetDeviceIndexEx(HandRole.RightHand);
            var leftHandIndex  = ViveRole.GetDeviceIndexEx(HandRole.LeftHand);

            if (VRModule.IsValidDeviceIndex(rightHandIndex) && VRModule.IsValidDeviceIndex(leftHandIndex))
            {
                var rightHandState = VRModule.GetDeviceState(rightHandIndex);
                var leftHandState  = VRModule.GetDeviceState(leftHandIndex);
                // Get device state for both hands

                float dist = Vector3.Distance(rightHandState.position, leftHandState.position);

                if (prevDist == 0)
                {
                    prevDist = dist;
                }
                else
                {
                    // Scaling the object based on distance change
                    gameObject.transform.localScale *= (1 + scalingFactor * (dist - prevDist));
                    prevDist = dist;
                }
            }
            else
            {
                Debug.Log("Scaling: controller state error");
            }
        }

        if (ViveInput.GetPressUpEx(HandRole.RightHand, ControllerButton.Trigger) ||
            ViveInput.GetPressUpEx(HandRole.LeftHand, ControllerButton.Trigger))
        {
            prevDist = 0.0f;
        }
    }
        private static int CompareDirection(uint d1, uint d2)
        {
            var d1Point = s_deviceDirPoint[d1];
            var d2Point = s_deviceDirPoint[d2];
            var d1Valid = VRModule.IsValidDeviceIndex(d1) && d1Point >= 0f;
            var d2Valid = VRModule.IsValidDeviceIndex(d2) && d2Point >= 0f;

            if (!d1Valid || !d2Valid)
            {
                if (d1Valid)
                {
                    return(-1);
                }
                if (d2Valid)
                {
                    return(1);
                }

                if (d1 < d2)
                {
                    return(-1);
                }
                if (d1 > d2)
                {
                    return(1);
                }

                return(0);
            }

            if (d1Point < d2Point)
            {
                return(-1);
            }
            if (d1Point > d2Point)
            {
                return(1);
            }

            return(0);
        }
Example #23
0
 private void UpdateExCamActivity()
 {
     if (!enabled)
     {
         SetValid(false);
         VivePose.RemoveNewPosesListener(this);
     }
     else if (VRModule.IsValidDeviceIndex(m_viveRole.GetDeviceIndex()))
     {
         SetValid(m_quadViewEnabled);
         VivePose.AddNewPosesListener(this);
     }
     else if (m_staticExCamEnabled)
     {
         SetValid(m_quadViewEnabled);
         VivePose.AddNewPosesListener(this);
     }
     else
     {
         SetValid(false);
         VivePose.RemoveNewPosesListener(this);
     }
 }
        private void MappingTrackers()
        {
            var deviceIndex = 0u;

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

                // find next valid device
                if (VRModule.IsValidDeviceIndex(deviceIndex))
                {
                    while (!IsTracker(deviceIndex) || RoleMap.IsDeviceConnectedAndBound(deviceIndex))
                    {
                        if (!VRModule.IsValidDeviceIndex(++deviceIndex))
                        {
                            break;
                        }
                    }
                }

                if (VRModule.IsValidDeviceIndex(deviceIndex))
                {
                    MappingRole(role, deviceIndex++);
                }
                else
                {
                    UnmappingRole(role);
                }
            }
        }
Example #25
0
            // deviceSN must be valid
            // device can be whether bound or not
            // device can be whether connected or not
            private void InternalBind(string deviceSN, int roleValue)
            {
                var deviceIndex = VRModule.GetConnectedDeviceIndex(deviceSN);

                bool previousIsBound        = false;
                int  previousBoundRoleValue = m_info.InvalidRoleValue;

                if (m_sn2role.TryGetValue(deviceSN, out previousBoundRoleValue))
                {
                    if (previousBoundRoleValue == roleValue)
                    {
                        return;
                    }

                    previousIsBound = true;

                    m_sn2role.Remove(deviceSN);

                    if (VRModule.IsValidDeviceIndex(deviceIndex))
                    {
                        InternalRemoveRoleBoundDevice(deviceSN, deviceIndex, previousBoundRoleValue);
                    }
                }

                m_sn2role[deviceSN] = roleValue;

                if (VRModule.IsValidDeviceIndex(deviceIndex))
                {
                    InternalInsertRoleBoundDevice(deviceSN, deviceIndex, roleValue);
                }

                if (m_handler != null)
                {
                    m_handler.OnBindingRoleValueChanged(deviceSN, previousIsBound, previousBoundRoleValue, true, roleValue);
                }
            }
Example #26
0
            public void MappingRoleValue(int roleValue, uint deviceIndex)
            {
                if (!m_info.IsValidRoleValue(roleValue))
                {
                    throw new ArgumentException("Cannot mapping invalid roleValue(" + m_info.RoleEnumType.Name + "[" + roleValue + "])");
                }

                if (!VRModule.IsValidDeviceIndex(deviceIndex))
                {
                    throw new ArgumentException("Cannot mapping invalid deviceIndex(" + deviceIndex + ")");
                }

                if (IsRoleValueBound(roleValue))
                {
                    throw new ArgumentException("roleValue(" + m_info.RoleEnumType.Name + "[" + roleValue + "]) is already bound, unbind first.");
                }

                if (IsDeviceConnectedAndBound(deviceIndex))
                {
                    throw new ArgumentException("deviceIndex(" + deviceIndex + ") is already bound, unbind first");
                }

                InternalMapping(roleValue, deviceIndex);
            }
Example #27
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 static bool IsValidIndex(uint index)
 {
     return(VRModule.IsValidDeviceIndex(index));
 }
Example #29
0
            // return true if frame skipped
            public override bool Update()
            {
                if (!ChangeProp.Set(ref updatedFrameCount, Time.frameCount))
                {
                    return(true);
                }

                var deviceIndex = m_map.GetMappedDeviceByRoleValue(m_roleValue);

                // treat this frame as updated if both prevDeviceIndex and currentDeviceIndex are invalid
                if (!VRModule.IsValidDeviceIndex(prevDeviceIndex) && !VRModule.IsValidDeviceIndex(deviceIndex))
                {
                    return(false);
                }

                // get device state
                var currState = VRModule.GetCurrentDeviceState(deviceIndex);

                // copy to previous states
                prevDeviceIndex   = deviceIndex;
                prevButtonPressed = currButtonPressed;
                for (int i = CONTROLLER_AXIS_COUNT - 1; i >= 0; --i)
                {
                    prevAxisValue[i] = currAxisValue[i];
                }

                trackedDeviceModel = currState.deviceModel;

                // update button states
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.System, currState.GetButtonPress(VRModuleRawButton.System));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.Menu, currState.GetButtonPress(VRModuleRawButton.ApplicationMenu));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.MenuTouch, currState.GetButtonTouch(VRModuleRawButton.ApplicationMenu));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.Trigger, currState.GetButtonPress(VRModuleRawButton.Trigger));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.TriggerTouch, currState.GetButtonTouch(VRModuleRawButton.Trigger));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.Pad, currState.GetButtonPress(VRModuleRawButton.Touchpad));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.PadTouch, currState.GetButtonTouch(VRModuleRawButton.Touchpad));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.Grip, currState.GetButtonPress(VRModuleRawButton.Grip));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.GripTouch, currState.GetButtonTouch(VRModuleRawButton.Grip));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.CapSenseGrip, currState.GetButtonPress(VRModuleRawButton.CapSenseGrip));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.CapSenseGripTouch, currState.GetButtonTouch(VRModuleRawButton.CapSenseGrip));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.AKey, currState.GetButtonPress(VRModuleRawButton.A));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.AKeyTouch, currState.GetButtonTouch(VRModuleRawButton.A));

                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.Axis3, currState.GetButtonPress(VRModuleRawButton.Axis3));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.Axis3Touch, currState.GetButtonTouch(VRModuleRawButton.Axis3));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.Axis4, currState.GetButtonPress(VRModuleRawButton.Axis4));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.Axis4Touch, currState.GetButtonTouch(VRModuleRawButton.Axis4));

                // update axis values
                currAxisValue[(int)ControllerAxis.PadX]         = currState.GetAxisValue(VRModuleRawAxis.TouchpadX);
                currAxisValue[(int)ControllerAxis.PadY]         = currState.GetAxisValue(VRModuleRawAxis.TouchpadY);
                currAxisValue[(int)ControllerAxis.Trigger]      = currState.GetAxisValue(VRModuleRawAxis.Trigger);
                currAxisValue[(int)ControllerAxis.CapSenseGrip] = currState.GetAxisValue(VRModuleRawAxis.CapSenseGrip);
                currAxisValue[(int)ControllerAxis.IndexCurl]    = currState.GetAxisValue(VRModuleRawAxis.IndexCurl);
                currAxisValue[(int)ControllerAxis.MiddleCurl]   = currState.GetAxisValue(VRModuleRawAxis.MiddleCurl);
                currAxisValue[(int)ControllerAxis.RingCurl]     = currState.GetAxisValue(VRModuleRawAxis.RingCurl);
                currAxisValue[(int)ControllerAxis.PinkyCurl]    = currState.GetAxisValue(VRModuleRawAxis.PinkyCurl);

                // update hair trigger
                var currTriggerValue = currAxisValue[(int)ControllerAxis.Trigger];

                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.FullTrigger, currTriggerValue == 1f);
                if (EnumUtils.GetFlag(prevButtonPressed, (int)ControllerButton.HairTrigger))
                {
                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.HairTrigger, currTriggerValue >= (hairTriggerLimit - hairDelta) && currTriggerValue > 0.0f);
                }
                else
                {
                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.HairTrigger, currTriggerValue > (hairTriggerLimit + hairDelta) || currTriggerValue >= 1.0f);
                }

                if (EnumUtils.GetFlag(currButtonPressed, (int)ControllerButton.HairTrigger))
                {
                    hairTriggerLimit = Mathf.Max(hairTriggerLimit, currTriggerValue);
                }
                else
                {
                    hairTriggerLimit = Mathf.Min(hairTriggerLimit, currTriggerValue);
                }

                // record pad down axis values
                if (GetPressDown(ControllerButton.Pad))
                {
                    padDownAxis = new Vector2(currAxisValue[(int)ControllerAxis.PadX], currAxisValue[(int)ControllerAxis.PadY]);
                }

                if (GetPressDown(ControllerButton.PadTouch))
                {
                    padTouchDownAxis = new Vector2(currAxisValue[(int)ControllerAxis.PadX], currAxisValue[(int)ControllerAxis.PadY]);
                }

                // record press down time and click count
                var timeNow = Time.unscaledTime;

                for (int button = 0; button < CONTROLLER_BUTTON_COUNT; ++button)
                {
                    if (GetPressDown((ControllerButton)button))
                    {
                        if (timeNow - lastPressDownTime[button] < clickInterval)
                        {
                            ++clickCount[button];
                        }
                        else
                        {
                            clickCount[button] = 1;
                        }

                        lastPressDownTime[button] = timeNow;
                    }
                }

                // invoke event listeners
                for (ControllerButton button = 0; button < (ControllerButton)CONTROLLER_BUTTON_COUNT; ++button)
                {
                    if (GetPress(button))
                    {
                        if (GetPressDown(button))
                        {
                            // PressDown event
                            TryInvokeListener(button, ButtonEventType.Down);
                            TryInvokeTypeListener(button, ButtonEventType.Down);
                        }

                        // Press event
                        TryInvokeListener(button, ButtonEventType.Press);
                        TryInvokeTypeListener(button, ButtonEventType.Press);
                    }
                    else if (GetPressUp(button))
                    {
                        // PressUp event
                        TryInvokeListener(button, ButtonEventType.Up);
                        TryInvokeTypeListener(button, ButtonEventType.Up);

                        if (timeNow - lastPressDownTime[(int)button] < clickInterval)
                        {
                            // Click event
                            TryInvokeListener(button, ButtonEventType.Click);
                            TryInvokeTypeListener(button, ButtonEventType.Click);
                        }
                    }
                }

                return(false);
            }
            // return true if frame skipped
            public override bool Update()
            {
                if (!ChangeProp.Set(ref updatedFrameCount, Time.frameCount))
                {
                    return(true);
                }

                var deviceIndex = m_map.GetMappedDeviceByRoleValue(m_roleValue);

                // treat this frame as updated if both prevDeviceIndex and currentDeviceIndex are invalid
                if (!VRModule.IsValidDeviceIndex(prevDeviceIndex) && !VRModule.IsValidDeviceIndex(deviceIndex))
                {
                    return(false);
                }

                // get device state
                var currState = VRModule.GetCurrentDeviceState(deviceIndex);

                // copy to previous states and reset current state
                prevDeviceIndex = deviceIndex;

                prevButtonPressed  = currButtonPressed;
                currButtonPressed  = 0;
                currentInput2DType = currState.input2DType;

                for (int i = CONTROLLER_AXIS_COUNT - 1; i >= 0; --i)
                {
                    prevAxisValue[i] = currAxisValue[i];
                    currAxisValue[i] = 0f;
                }

                // update button states
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.System, currState.GetButtonPress(VRModuleRawButton.System));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.Menu, currState.GetButtonPress(VRModuleRawButton.ApplicationMenu));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.MenuTouch, currState.GetButtonTouch(VRModuleRawButton.ApplicationMenu));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.Trigger, currState.GetButtonPress(VRModuleRawButton.Trigger));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.TriggerTouch, currState.GetButtonTouch(VRModuleRawButton.Trigger));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.Pad, currState.GetButtonPress(VRModuleRawButton.Touchpad));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.PadTouch, currState.GetButtonTouch(VRModuleRawButton.Touchpad));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.Grip, currState.GetButtonPress(VRModuleRawButton.Grip));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.GripTouch, currState.GetButtonTouch(VRModuleRawButton.Grip));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.CapSenseGrip, currState.GetButtonPress(VRModuleRawButton.CapSenseGrip));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.CapSenseGripTouch, currState.GetButtonTouch(VRModuleRawButton.CapSenseGrip));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.ProximitySensor, currState.GetButtonPress(VRModuleRawButton.ProximitySensor));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.AKey, currState.GetButtonPress(VRModuleRawButton.A));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.AKeyTouch, currState.GetButtonTouch(VRModuleRawButton.A));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.Bumper, currState.GetButtonPress(VRModuleRawButton.Bumper));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.BumperTouch, currState.GetButtonTouch(VRModuleRawButton.Bumper));

                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.Axis4, currState.GetButtonPress(VRModuleRawButton.Axis4));
                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.Axis4Touch, currState.GetButtonTouch(VRModuleRawButton.Axis4));

                // update axis values
                currAxisValue[(int)ControllerAxis.PadX]         = currState.GetAxisValue(VRModuleRawAxis.TouchpadX);
                currAxisValue[(int)ControllerAxis.PadY]         = currState.GetAxisValue(VRModuleRawAxis.TouchpadY);
                currAxisValue[(int)ControllerAxis.Trigger]      = currState.GetAxisValue(VRModuleRawAxis.Trigger);
                currAxisValue[(int)ControllerAxis.CapSenseGrip] = currState.GetAxisValue(VRModuleRawAxis.CapSenseGrip);
                currAxisValue[(int)ControllerAxis.IndexCurl]    = currState.GetAxisValue(VRModuleRawAxis.IndexCurl);
                currAxisValue[(int)ControllerAxis.MiddleCurl]   = currState.GetAxisValue(VRModuleRawAxis.MiddleCurl);
                currAxisValue[(int)ControllerAxis.RingCurl]     = currState.GetAxisValue(VRModuleRawAxis.RingCurl);
                currAxisValue[(int)ControllerAxis.PinkyCurl]    = currState.GetAxisValue(VRModuleRawAxis.PinkyCurl);

                switch (currentInput2DType)
                {
                case VRModuleInput2DType.Unknown:
                case VRModuleInput2DType.TrackpadOnly:
                    currAxisValue[(int)ControllerAxis.PadX] = currState.GetAxisValue(VRModuleRawAxis.TouchpadX);
                    currAxisValue[(int)ControllerAxis.PadY] = currState.GetAxisValue(VRModuleRawAxis.TouchpadY);
                    if (!VIUSettings.individualTouchpadJoystickValue)
                    {
                        currAxisValue[(int)ControllerAxis.JoystickX] = currState.GetAxisValue(VRModuleRawAxis.TouchpadX);
                        currAxisValue[(int)ControllerAxis.JoystickY] = currState.GetAxisValue(VRModuleRawAxis.TouchpadY);
                    }
                    break;

                case VRModuleInput2DType.JoystickOnly:
                    currAxisValue[(int)ControllerAxis.JoystickX] = currState.GetAxisValue(VRModuleRawAxis.TouchpadX);
                    currAxisValue[(int)ControllerAxis.JoystickY] = currState.GetAxisValue(VRModuleRawAxis.TouchpadY);
                    if (!VIUSettings.individualTouchpadJoystickValue)
                    {
                        currAxisValue[(int)ControllerAxis.PadX] = currState.GetAxisValue(VRModuleRawAxis.TouchpadX);
                        currAxisValue[(int)ControllerAxis.PadY] = currState.GetAxisValue(VRModuleRawAxis.TouchpadY);
                    }
                    break;

                case VRModuleInput2DType.Both:
                    currAxisValue[(int)ControllerAxis.PadX]      = currState.GetAxisValue(VRModuleRawAxis.TouchpadX);
                    currAxisValue[(int)ControllerAxis.PadY]      = currState.GetAxisValue(VRModuleRawAxis.TouchpadY);
                    currAxisValue[(int)ControllerAxis.JoystickX] = currState.GetAxisValue(VRModuleRawAxis.JoystickX);
                    currAxisValue[(int)ControllerAxis.JoystickY] = currState.GetAxisValue(VRModuleRawAxis.JoystickY);
                    break;
                }

                // update d-pad
                var axis     = new Vector2(currAxisValue[(int)ControllerAxis.PadX], currAxisValue[(int)ControllerAxis.PadY]);
                var deadZone = VIUSettings.virtualDPadDeadZone;

                if (axis.sqrMagnitude >= deadZone * deadZone)
                {
                    var padPress = GetPress(ControllerButton.Pad);
                    var padTouch = GetPress(ControllerButton.PadTouch);

                    var right = Vector2.Angle(Vector2.right, axis) < 45f;
                    var up    = Vector2.Angle(Vector2.up, axis) < 45f;
                    var left  = Vector2.Angle(Vector2.left, axis) < 45f;
                    var down  = Vector2.Angle(Vector2.down, axis) < 45f;

                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.DPadRight, padPress && right);
                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.DPadUp, padPress && up);
                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.DPadLeft, padPress && left);
                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.DPadDown, padPress && down);
                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.DPadRightTouch, padTouch && right);
                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.DPadUpTouch, padTouch && up);
                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.DPadLeftTouch, padTouch && left);
                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.DPadDownTouch, padTouch && down);

                    var upperRight = axis.x > 0f && axis.y > 0f;
                    var upperLeft  = axis.x <0f && axis.y> 0f;
                    var lowerLeft  = axis.x < 0f && axis.y < 0f;
                    var lowerRight = axis.x > 0f && axis.y < 0f;

                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.DPadUpperRight, padPress && upperRight);
                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.DPadUpperLeft, padPress && upperLeft);
                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.DPadLowerLeft, padPress && lowerLeft);
                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.DPadLowerRight, padPress && lowerRight);
                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.DPadUpperRightTouch, padTouch && upperRight);
                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.DPadUpperLeftTouch, padTouch && upperLeft);
                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.DPadLowerLeftTouch, padTouch && lowerLeft);
                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.DPadLowerRightTouch, padTouch && lowerRight);
                }

                // update hair trigger
                var currTriggerValue = currAxisValue[(int)ControllerAxis.Trigger];

                EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.FullTrigger, currTriggerValue >= 0.99f);

                if (EnumUtils.GetFlag(prevButtonPressed, (int)ControllerButton.HairTrigger))
                {
                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.HairTrigger, currTriggerValue >= (hairTriggerLimit - hairDelta) && currTriggerValue > 0.0f);
                }
                else
                {
                    EnumUtils.SetFlag(ref currButtonPressed, (int)ControllerButton.HairTrigger, currTriggerValue > (hairTriggerLimit + hairDelta) || currTriggerValue >= 1.0f);
                }

                if (EnumUtils.GetFlag(currButtonPressed, (int)ControllerButton.HairTrigger))
                {
                    hairTriggerLimit = Mathf.Max(hairTriggerLimit, currTriggerValue);
                }
                else
                {
                    hairTriggerLimit = Mathf.Min(hairTriggerLimit, currTriggerValue);
                }

                // record pad down axis values
                if (GetPressDown(ControllerButton.Pad))
                {
                    padDownAxis = new Vector2(currAxisValue[(int)ControllerAxis.PadX], currAxisValue[(int)ControllerAxis.PadY]);
                }

                if (GetPressDown(ControllerButton.PadTouch))
                {
                    padTouchDownAxis = new Vector2(currAxisValue[(int)ControllerAxis.PadX], currAxisValue[(int)ControllerAxis.PadY]);
                }

                // record press down time and click count
                var timeNow = Time.unscaledTime;

                for (int button = 0; button < CONTROLLER_BUTTON_COUNT; ++button)
                {
                    if (GetPressDown((ControllerButton)button))
                    {
                        if (timeNow - lastPressDownTime[button] < clickInterval)
                        {
                            ++clickCount[button];
                        }
                        else
                        {
                            clickCount[button] = 1;
                        }

                        lastPressDownTime[button] = timeNow;
                    }
                }

                // invoke event listeners
                for (ControllerButton button = 0; button < (ControllerButton)CONTROLLER_BUTTON_COUNT; ++button)
                {
                    if (GetPress(button))
                    {
                        if (GetPressDown(button))
                        {
                            // PressDown event
                            TryInvokeListener(button, ButtonEventType.Down);
                            TryInvokeTypeListener(button, ButtonEventType.Down);
                        }

                        // Press event
                        TryInvokeListener(button, ButtonEventType.Press);
                        TryInvokeTypeListener(button, ButtonEventType.Press);
                    }
                    else if (GetPressUp(button))
                    {
                        // PressUp event
                        TryInvokeListener(button, ButtonEventType.Up);
                        TryInvokeTypeListener(button, ButtonEventType.Up);

                        if (timeNow - lastPressDownTime[(int)button] < clickInterval)
                        {
                            // Click event
                            TryInvokeListener(button, ButtonEventType.Click);
                            TryInvokeTypeListener(button, ButtonEventType.Click);
                        }
                    }
                }

                return(false);
            }