Example #1
0
 protected virtual void AddActiveCollider(Collider collider)
 {
     if (touchedObject != null && touchedObjectColliders.Contains(collider))
     {
         VRTK_SharedMethods.AddListValue(touchedObjectActiveColliders, collider, true);
     }
 }
Example #2
0
        public virtual void OnControllerNearTouchInteractableObject(ObjectInteractEventArgs e)
        {
            VRTK_SharedMethods.AddListValue(nearTouchedObjects, e.target, true);

            if (ControllerNearTouchInteractableObject != null)
            {
                ControllerNearTouchInteractableObject(this, e);
            }
        }
Example #3
0
        //[Pure]
        protected virtual void Raycast(Canvas canvas, Camera eventCamera, PointerEventData eventData, Ray ray, ref List <RaycastResult> results)
        {
            float           hitDistance    = GetHitDistance(ray, VRTK_UIPointer.GetPointerLength(eventData.pointerId));
            IList <Graphic> canvasGraphics = GraphicRegistry.GetGraphicsForCanvas(canvas);

            for (int i = 0; i < canvasGraphics.Count; ++i)
            {
                Graphic graphic = canvasGraphics[i];

                if (graphic.depth == -1 || !graphic.raycastTarget)
                {
                    continue;
                }

                Transform graphicTransform = graphic.transform;
                Vector3   graphicForward   = graphicTransform.forward;
                float     distance         = Vector3.Dot(graphicForward, graphicTransform.position - ray.origin) / Vector3.Dot(graphicForward, ray.direction);

                if (distance < 0)
                {
                    continue;
                }

                //Prevents "flickering hover" on items near canvas center.
                if ((distance - UI_CONTROL_OFFSET) > hitDistance)
                {
                    continue;
                }

                Vector3 position        = ray.GetPoint(distance);
                Vector2 pointerPosition = eventCamera.WorldToScreenPoint(position);

                if (!RectTransformUtility.RectangleContainsScreenPoint(graphic.rectTransform, pointerPosition, eventCamera))
                {
                    continue;
                }

                if (graphic.Raycast(pointerPosition, eventCamera))
                {
                    RaycastResult result = new RaycastResult()
                    {
                        gameObject     = graphic.gameObject,
                        module         = this,
                        distance       = distance,
                        screenPosition = pointerPosition,
                        worldPosition  = position,
                        depth          = graphic.depth,
                        sortingLayer   = canvas.sortingLayerID,
                        sortingOrder   = canvas.sortingOrder,
                    };
                    VRTK_SharedMethods.AddListValue(results, result);
                }
            }

            results.Sort((g1, g2) => g2.depth.CompareTo(g1.depth));
        }
        /// <summary>
        /// The GetHoveringObjects method returns a List of valid GameObjects that are currently hovering (but not snapped) in the snap drop zone area.
        /// </summary>
        /// <returns>The List of valid GameObjects that are hovering (but not snapped) in the snap drop zone area.</returns>
        public virtual List <GameObject> GetHoveringObjects()
        {
            List <GameObject> returnList = new List <GameObject>();

            for (int i = 0; i < currentValidSnapInteractableObjects.Count; i++)
            {
                VRTK_SharedMethods.AddListValue(returnList, currentValidSnapInteractableObjects[i].gameObject);
            }
            return(returnList);
        }
Example #5
0
 protected virtual void StoreTouchedObjectColliders(Collider collider)
 {
     touchedObjectColliders.Clear();
     touchedObjectActiveColliders.Clear();
     Collider[] touchedObjectChildColliders = touchedObject.GetComponentsInChildren <Collider>();
     for (int i = 0; i < touchedObjectChildColliders.Length; i++)
     {
         VRTK_SharedMethods.AddListValue(touchedObjectColliders, touchedObjectChildColliders[i], true);
     }
     VRTK_SharedMethods.AddListValue(touchedObjectActiveColliders, collider, true);
 }
 protected virtual void AddCurrentValidSnapObject(VRTK_InteractableObject givenObject)
 {
     if (givenObject != null)
     {
         if (VRTK_SharedMethods.AddListValue(currentValidSnapInteractableObjects, givenObject, true))
         {
             givenObject.InteractableObjectGrabbed   += InteractableObjectGrabbed;
             givenObject.InteractableObjectUngrabbed += InteractableObjectUngrabbed;
         }
     }
 }
Example #7
0
 protected virtual void ManageTouchers(GameObject interactingObject, bool add)
 {
     if (add)
     {
         VRTK_SharedMethods.AddListValue(touchers, interactingObject, true);
     }
     else
     {
         touchers.Remove(interactingObject);
     }
 }
 protected virtual void ObjectTouched(object sender, InteractableObjectEventArgs e)
 {
     DoShowMenu(CalculateAngle(e.interactingObject), sender);
     collidingObjects.Add(e.interactingObject);
     VRTK_SharedMethods.AddListValue(interactingObjects, e.interactingObject, true);
     if (addMenuCollider && menuCollider != null)
     {
         SetColliderState(true, e);
         if (delayedSetColliderEnabledRoutine != null)
         {
             StopCoroutine(delayedSetColliderEnabledRoutine);
         }
     }
 }
Example #9
0
        /// <summary>
        /// Set the control options and modify the trackables to match.
        /// </summary>
        /// <param name="givenControlOptions">The control options to set the current control options to.</param>
        public virtual void SetControlOptions(ControlOptions givenControlOptions)
        {
            controlOptions = givenControlOptions;
            trackedObjects.Clear();

            if (controllerLeftHand != null && controllerRightHand != null && (controlOptions == ControlOptions.HeadsetAndControllers || controlOptions == ControlOptions.ControllersOnly))
            {
                VRTK_SharedMethods.AddListValue(trackedObjects, VRTK_DeviceFinder.GetActualController(controllerLeftHand).transform, true);
                VRTK_SharedMethods.AddListValue(trackedObjects, VRTK_DeviceFinder.GetActualController(controllerRightHand).transform, true);
            }

            if (headset != null && (controlOptions == ControlOptions.HeadsetAndControllers || controlOptions == ControlOptions.HeadsetOnly))
            {
                VRTK_SharedMethods.AddListValue(trackedObjects, headset.transform, true);
            }
        }
Example #10
0
        protected virtual float CalculateListAverage()
        {
            float listAverage = 0;

            for (int i = 0; i < trackedObjects.Count; i++)
            {
                Transform trackedObj = trackedObjects[i];
                // Get the amount of Y movement that's occured since the last update.
                float previousYPosition = VRTK_SharedMethods.GetDictionaryValue(previousYPositions, trackedObj);
                float deltaYPostion     = Mathf.Abs(previousYPosition - trackedObj.transform.localPosition.y);

                // Convenience code.
                List <float> trackedObjList = VRTK_SharedMethods.GetDictionaryValue(movementList, trackedObj, new List <float>(), true);

                // Cap off the speed.
                if (deltaYPostion > sensitivity)
                {
                    VRTK_SharedMethods.AddListValue(trackedObjList, sensitivity);
                }
                else
                {
                    VRTK_SharedMethods.AddListValue(trackedObjList, deltaYPostion);
                }

                // Keep our tracking list at m_averagePeriod number of elements.
                if (trackedObjList.Count > averagePeriod)
                {
                    trackedObjList.RemoveAt(0);
                }

                // Average out the current tracker's list.
                float sum = 0;
                for (int j = 0; j < trackedObjList.Count; j++)
                {
                    float diffrences = trackedObjList[j];
                    sum += diffrences;
                }
                float avg = sum / averagePeriod;

                // Add the average to the the list average.
                listAverage += avg;
            }

            return(listAverage);
        }
Example #11
0
        //[Pure]
        protected virtual void SetNearestRaycast(ref PointerEventData eventData, ref List <RaycastResult> resultAppendList, ref List <RaycastResult> raycastResults)
        {
            RaycastResult?nearestRaycast = null;

            for (int index = 0; index < raycastResults.Count; index++)
            {
                RaycastResult castResult = raycastResults[index];
                castResult.index = resultAppendList.Count;
                if (!nearestRaycast.HasValue || castResult.distance < nearestRaycast.Value.distance)
                {
                    nearestRaycast = castResult;
                }
                VRTK_SharedMethods.AddListValue(resultAppendList, castResult);
            }

            if (nearestRaycast.HasValue)
            {
                eventData.position = nearestRaycast.Value.screenPosition;
                eventData.delta    = eventData.position - lastKnownPosition;
                lastKnownPosition  = eventData.position;
                eventData.pointerCurrentRaycast = nearestRaycast.Value;
            }
        }
Example #12
0
 /// <summary>
 /// The AddButton method is used to add a new button to the menu.
 /// </summary>
 /// <param name="newButton">The button to add.</param>
 public void AddButton(RadialMenuButton newButton)
 {
     VRTK_SharedMethods.AddListValue(buttons, newButton, true);
     RegenerateButtons();
 }
Example #13
0
        /// <summary>
        /// The RegenerateButtons method creates all the button arcs and populates them with desired icons.
        /// </summary>
        public void RegenerateButtons()
        {
            RemoveAllButtons();
            for (int i = 0; i < buttons.Count; i++)
            {
                // Initial placement/instantiation
                GameObject newButton = Instantiate(buttonPrefab);
                newButton.transform.SetParent(transform);
                newButton.transform.localScale = Vector3.one;
                newButton.GetComponent <RectTransform>().offsetMax = Vector2.zero;
                newButton.GetComponent <RectTransform>().offsetMin = Vector2.zero;

                //Setup button arc
                UICircle circle = newButton.GetComponent <UICircle>();
                if (buttonThickness == 1f)
                {
                    circle.fill = true;
                }
                else
                {
                    circle.thickness = (int)(buttonThickness * (GetComponent <RectTransform>().rect.width / 2f));
                }
                int fillPerc = (int)(100f / buttons.Count);
                circle.fillPercent = fillPerc;
                circle.color       = buttonColor;

                //Final placement/rotation
                float angle = ((360f / buttons.Count) * i) + offsetRotation;
                newButton.transform.localEulerAngles = new Vector3(0, 0, angle);
                newButton.layer = 4; //UI Layer
                newButton.transform.localPosition = Vector3.zero;
                if (circle.fillPercent < 55)
                {
                    float   angleRad    = (angle * Mathf.PI) / 180f;
                    Vector2 angleVector = new Vector2(-Mathf.Cos(angleRad), -Mathf.Sin(angleRad));
                    newButton.transform.localPosition += (Vector3)angleVector * offsetDistance;
                }

                //Place and populate Button Icon
                GameObject buttonIcon = newButton.GetComponentInChildren <RadialButtonIcon>().gameObject;
                if (buttons[i].ButtonIcon == null)
                {
                    buttonIcon.SetActive(false);
                }
                else
                {
                    buttonIcon.GetComponent <Image>().sprite = buttons[i].ButtonIcon;
                    buttonIcon.transform.localPosition       = new Vector2(-1 * ((newButton.GetComponent <RectTransform>().rect.width / 2f) - (circle.thickness / 2f)), 0);
                    //Min icon size from thickness and arc
                    float scale1            = Mathf.Abs(circle.thickness);
                    float absButtonIconXPos = Mathf.Abs(buttonIcon.transform.localPosition.x);
                    float bAngle            = (359f * circle.fillPercent * 0.01f * Mathf.PI) / 180f;
                    float scale2            = (absButtonIconXPos * 2f * Mathf.Sin(bAngle / 2f));
                    if (circle.fillPercent > 24) //Scale calc doesn't work for > 90 degrees
                    {
                        scale2 = float.MaxValue;
                    }

                    float iconScale = Mathf.Min(scale1, scale2) - iconMargin;
                    buttonIcon.GetComponent <RectTransform>().sizeDelta = new Vector2(iconScale, iconScale);
                    //Rotate icons all vertically if desired
                    if (!rotateIcons)
                    {
                        buttonIcon.transform.eulerAngles = GetComponentInParent <Canvas>().transform.eulerAngles;
                    }
                }
                VRTK_SharedMethods.AddListValue(menuButtons, newButton, true);
            }
        }
Example #14
0
 protected virtual void OnTriggerEnter(Collider collider)
 {
     StartNearTouch(collider);
     VRTK_SharedMethods.AddListValue(nearTouchedObjects, collider.gameObject, true);
 }