Ejemplo n.º 1
0
        /// <summary>
        /// Find the closest raycast hit in the list of RaycastResults that is also included in the LayerMask list.
        /// </summary>
        /// <param name="candidates">List of RaycastResults from a Unity UI raycast</param>
        /// <param name="layerMaskList">List of layers to support</param>
        /// <returns>RaycastResult if hit, or an empty RaycastResult if nothing was hit</returns>
        private RaycastResult FindClosestRaycastHitInLayermasks(List <RaycastResult> candidates, LayerMask[] layerMaskList)
        {
            int combinedLayerMask = 0;

            for (int i = 0; i < layerMaskList.Length; i++)
            {
                combinedLayerMask = combinedLayerMask | layerMaskList[i].value;
            }

            RaycastResult?minHit = null;

            for (var i = 0; i < candidates.Count; ++i)
            {
                if (candidates[i].gameObject == null || !IsLayerInLayerMask(candidates[i].gameObject.layer, combinedLayerMask))
                {
                    continue;
                }
                if (minHit == null || candidates[i].distance < minHit.Value.distance)
                {
                    minHit = candidates[i];
                }
            }

            return(minHit ?? new RaycastResult());
        }
        private void SetNearestRaycast(ref PointerEventData eventData, List <RaycastResult> resultAppendList)
        {
            RaycastResult?nearestRaycast = null;

            for (var index = 0; index < vrRaycastResults.Count; index++)
            {
                RaycastResult castResult = new RaycastResult();
                castResult.gameObject     = vrRaycastResults[index].graphic.gameObject;
                castResult.module         = this;
                castResult.distance       = vrRaycastResults[index].distance;
                castResult.screenPosition = vrRaycastResults[index].pointerPosition;
                castResult.worldPosition  = vrRaycastResults[index].position;
                castResult.index          = resultAppendList.Count;
                castResult.depth          = vrRaycastResults[index].graphic.depth;
                castResult.sortingLayer   = UICanvas.sortingLayerID;
                castResult.sortingOrder   = UICanvas.sortingOrder;
                if (!nearestRaycast.HasValue || castResult.distance < nearestRaycast.Value.distance)
                {
                    nearestRaycast = castResult;
                }
                resultAppendList.Add(castResult);
            }

            if (nearestRaycast.HasValue)
            {
                eventData.position              = nearestRaycast.Value.screenPosition;
                eventData.delta                 = eventData.position - lastPointerPosition;
                lastPointerPosition             = eventData.position;
                eventData.pointerCurrentRaycast = nearestRaycast.Value;
            }
        }
Ejemplo n.º 3
0
    private void FixedUpdate()
    {
        RaycastResult?result = terrainRaycast.LookingAt;

        lineRenderer.activated = result.HasValue;
        if (result.HasValue)
        {
            SetLocation(result.Value.Position);
        }
        else
        {
            return;
        }

        if (!isBreaking)
        {
            if (CrossPlatformInputManager.GetAxisRaw("Place") > 0)
            {
                Point3 p = result.Value.Position;
                terrainManager.ChangeCell(p.X, p.Y, p.Z, 0);
                StartCoroutine(Delay(0.1f));
            }
            else if (CrossPlatformInputManager.GetAxisRaw("Destroy") > 0)
            {
                Point3 p = result.Value.LastPosition;
                terrainManager.ChangeCell(p.X, p.Y, p.Z, placeBlockValue);
                //StartCoroutine (Delay (0.1f));
            }
        }
    }
        public override void Raycast(PointerEventData eventData, List <RaycastResult> resultAppendList)
        {
            float hitDistance;

            if (hitTestDistance > 0.01f)
            {
                hitDistance = hitTestDistance;
            }
            else
            {
                hitDistance = eventCamera.farClipPlane;
            }


            Debug.DrawRay(eventData.pointerCurrentRaycast.worldPosition, eventData.pointerCurrentRaycast.worldNormal, Color.green);

            Ray ray = new Ray(eventData.pointerCurrentRaycast.worldPosition, eventData.pointerCurrentRaycast.worldNormal);

            var hits = Physics.RaycastAll(ray, hitDistance, finalEventMask);

            if (hits.Length > 1)
            {
                System.Array.Sort(hits, (r1, r2) => r1.distance.CompareTo(r2.distance));
            }

            RaycastResult?closestResult = null;

            if (hits.Length != 0)
            {
                for (int b = 0, bmax = hits.Length; b < bmax; ++b)
                {
                    var result = new RaycastResult
                    {
                        gameObject    = hits[b].collider.gameObject,
                        module        = this,
                        distance      = hits[b].distance,
                        worldPosition = hits[b].point,
                        worldNormal   = hits[b].normal,
                        //    screenPosition = hits[b].pointerPosition,
                        index        = resultAppendList.Count,
                        sortingLayer = 0,
                        sortingOrder = 0
                    };
                    resultAppendList.Add(result);
                    if (b == 0)
                    {
                        closestResult = result;
                    }
                }

                if (closestResult.HasValue)
                {
                    eventData.pointerCurrentRaycast = closestResult.Value;
                    // eventData.position = closestResult.Value.ScreenPosition;
                    eventData.delta = eventData.position - m_lastPosition;
                    m_lastPosition  = eventData.position;
                }
            }
        }
Ejemplo n.º 5
0
        // Token: 0x06000232 RID: 562 RVA: 0x0001E1C0 File Offset: 0x0001C3C0
        protected virtual CusPointerInputModule.MouseState GetMousePointerEventData()
        {
            PointerEventData pointerEventData;
            bool             pointerData = this.GetPointerData(-1, out pointerEventData, true);

            pointerEventData.Reset();
            if (pointerData)
            {
                pointerEventData.position = Input.mousePosition;
            }
            Vector2 vector = Input.mousePosition;

            pointerEventData.delta       = vector - pointerEventData.position;
            pointerEventData.position    = vector;
            pointerEventData.scrollDelta = Input.mouseScrollDelta;
            pointerEventData.button      = PointerEventData.InputButton.Left;
            base.eventSystem.RaycastAll(pointerEventData, this.m_RaycastResultCache);
            RaycastResult pointerCurrentRaycast = this.FindFirstRaycast(this.m_RaycastResultCache, 0);

            pointerEventData.pointerCurrentRaycast = pointerCurrentRaycast;
            this.m_RaycastResultCache.Clear();
            PointerEventData pointerEventData2;

            this.GetPointerData(-2, out pointerEventData2, true);
            this.CopyFromTo(pointerEventData, pointerEventData2);
            pointerEventData2.button = PointerEventData.InputButton.Right;
            PointerEventData pointerEventData3;

            this.GetPointerData(-3, out pointerEventData3, true);
            this.CopyFromTo(pointerEventData, pointerEventData3);
            pointerEventData3.button = PointerEventData.InputButton.Middle;
            this.m_MouseState.SetButtonState(PointerEventData.InputButton.Left, CusPointerInputModule.StateForMouseButton(0), pointerEventData);
            this.m_MouseState.SetButtonState(PointerEventData.InputButton.Right, CusPointerInputModule.StateForMouseButton(1), pointerEventData2);
            this.m_MouseState.SetButtonState(PointerEventData.InputButton.Middle, CusPointerInputModule.StateForMouseButton(2), pointerEventData3);
            RaycastResult?maskRay = this.MaskRay;

            if (maskRay != null)
            {
                pointerData = this.GetPointerData(999, out pointerEventData, true);
                if (pointerData)
                {
                    pointerEventData.pointerCurrentRaycast = this.MaskRay.Value;
                }
                this.GetPointerData(-2, out pointerEventData2, true);
                this.CopyFromTo(pointerEventData, pointerEventData2);
                pointerEventData2.button = PointerEventData.InputButton.Right;
                this.GetPointerData(-3, out pointerEventData3, true);
                this.CopyFromTo(pointerEventData, pointerEventData3);
                pointerEventData3.button = PointerEventData.InputButton.Middle;
                this.m_MaskMouseState.SetButtonState(PointerEventData.InputButton.Left, CusPointerInputModule.StateForMouseButton(0), pointerEventData);
                this.m_MaskMouseState.SetButtonState(PointerEventData.InputButton.Right, CusPointerInputModule.StateForMouseButton(1), pointerEventData2);
                this.m_MaskMouseState.SetButtonState(PointerEventData.InputButton.Middle, CusPointerInputModule.StateForMouseButton(2), pointerEventData3);
            }
            return(this.m_MouseState);
        }
Ejemplo n.º 6
0
 void FixedUpdate()
 {
     LookingAt = AlaphaRaycast(Camera.main.transform.position, Camera.main.transform.forward);
     //Cube.SetActive(LookingAt.HasValue);
     //line.activated = LookingAt.HasValue;
     //if (LookingAt.HasValue)
     //{
     //line.SetLocation(LookingAt.Value.Position);
     //Cube.transform.position = LookingAt.Value.Position.ToVec3();
     //}
 }
Ejemplo n.º 7
0
        // Token: 0x0600022F RID: 559 RVA: 0x0001DF60 File Offset: 0x0001C160
        protected PointerEventData GetTouchPointerEventData(Touch input, out bool pressed, out bool released)
        {
            PointerEventData pointerEventData;
            bool             pointerData = this.GetPointerData(input.fingerId, out pointerEventData, true);

            pointerEventData.Reset();
            pressed  = (pointerData || input.phase == TouchPhase.Began);
            released = (input.phase == TouchPhase.Canceled || input.phase == TouchPhase.Ended);
            if (pointerData)
            {
                pointerEventData.position = input.position;
            }
            if (pressed)
            {
                pointerEventData.delta = Vector2.zero;
            }
            else
            {
                pointerEventData.delta = input.position - pointerEventData.position;
            }
            pointerEventData.position = input.position;
            pointerEventData.button   = PointerEventData.InputButton.Left;
            base.eventSystem.RaycastAll(pointerEventData, this.m_RaycastResultCache);
            RaycastResult pointerCurrentRaycast = this.FindFirstRaycast(this.m_RaycastResultCache, 0);

            pointerEventData.pointerCurrentRaycast = pointerCurrentRaycast;
            this.m_RaycastResultCache.Clear();
            RaycastResult?maskRay = this.MaskRay;

            if (maskRay != null && this.MaskEvent == null)
            {
                pointerData = this.GetPointerData(999, out this.MaskEvent, true);
                this.MaskEvent.Reset();
                pressed  = (pointerData || input.phase == TouchPhase.Began);
                released = (input.phase == TouchPhase.Canceled || input.phase == TouchPhase.Ended);
                if (pointerData)
                {
                    this.MaskEvent.position = input.position;
                }
                if (pressed)
                {
                    this.MaskEvent.delta = Vector2.zero;
                }
                else
                {
                    this.MaskEvent.delta = input.position - this.MaskEvent.position;
                }
                this.MaskEvent.position = input.position;
                this.MaskEvent.button   = PointerEventData.InputButton.Left;
                this.MaskEvent.pointerCurrentRaycast = this.MaskRay.Value;
            }
            return(pointerEventData);
        }
Ejemplo n.º 8
0
    private void Update()
    {
        if (dragging)
        {
            // Cast ray to see if there is a drop target behind
            var raycaster = GetComponentInParent <GvrPointerGraphicRaycaster>();
            var ped       = new PointerEventData(null);
            var results   = new List <RaycastResult>();
            raycaster.Raycast(ped, results);
            RaycastResult?dropTargetHit = null;
            foreach (var hitResult in results)
            {
                if (hitResult.gameObject.GetComponent <DropTarget>())
                {
                    dropTargetHit = hitResult;
                    break;
                }
            }
            print("Results: " + results.Count);
            if (dropTargetHit.HasValue)
            {
                print("Hit: " + dropTargetHit.Value.gameObject.name);
                // Move dragged object slightly in front of drop target and parallel to it
                var hit = dropTargetHit.Value;
                transform.position = hit.worldPosition + hit.gameObject.transform.forward * 0.07f;
                transform.rotation = hit.gameObject.transform.rotation;

                // Drop the object on the drop target on click
                if (GvrControllerInput.ClickButtonDown)
                {
                    // If button was clicked, attach object to the drop target
                    // and end dragging
                    EndDrag();
                    print("EndDrag");
                }
            }
            else
            {
                float distance = originalDistance - 0.1f; // draw a little closer
                transform.position = controllerTransform.position +
                                     originalDistance * controllerTransform.forward;
                var lookTarget = new Vector3(playerTransform.position.x, transform.position.y, playerTransform.position.z);
                transform.LookAt(lookTarget);
            }
        }
    }
Ejemplo n.º 9
0
        // Token: 0x06001759 RID: 5977 RVA: 0x0007CD38 File Offset: 0x0007AF38
        protected virtual void SetNearestRaycast(ref PointerEventData eventData, ref List <RaycastResult> resultAppendList, ref List <RaycastResult> raycastResults)
        {
            RaycastResult?raycastResult = null;

            for (int i = 0; i < raycastResults.Count; i++)
            {
                RaycastResult raycastResult2 = raycastResults[i];
                raycastResult2.index = (float)resultAppendList.Count;
                if (raycastResult == null || raycastResult2.distance < raycastResult.Value.distance)
                {
                    raycastResult = new RaycastResult?(raycastResult2);
                }
                resultAppendList.Add(raycastResult2);
            }
            if (raycastResult != null)
            {
                eventData.position              = raycastResult.Value.screenPosition;
                eventData.delta                 = eventData.position - this.lastKnownPosition;
                this.lastKnownPosition          = eventData.position;
                eventData.pointerCurrentRaycast = raycastResult.Value;
            }
        }
        //[Pure]
        /// <summary>
        /// Sets the nearest raycast result on the event
        /// </summary>
        /// <param name="eventData">pointer event data that will reference the nearest raycast</param>
        /// <param name="resultAppendList">List updated with results</param>
        /// <param name="raycastResults">Results to search through</param>
        protected virtual void SetNearestRaycast(ref PointerEventData eventData, ref List <RaycastResult> resultAppendList, ref List <RaycastResult> raycastResults)
        {
            RaycastResult?nearestRaycast = null;

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

            if (nearestRaycast.HasValue)
            {
                eventData.position              = nearestRaycast.Value.screenPosition;
                eventData.delta                 = eventData.position - _lastKnownRaycastHitPosition;
                _lastKnownRaycastHitPosition    = eventData.position;
                eventData.pointerCurrentRaycast = nearestRaycast.Value;
            }
        }
Ejemplo n.º 11
0
    void OnGUI()
    {
        if (showing)
        {
            int w = Screen.width, h = Screen.height;

            GUIStyle style = new GUIStyle();

            Rect rect = new Rect(0, 0, w, h * 9 / 100);
            style.alignment        = TextAnchor.UpperLeft;
            style.fontSize         = h * 3 / 100;
            style.normal.textColor = new Color(1.0f, 1.0f, 1.0f, 1.0f);
            float         msec = deltaTime * 1000.0f;
            float         fps  = 1.0f / deltaTime;
            string        text = string.Format("{0:0.0} ms ({1:0.} fps)\ncurrent chunk: {2}\n按ESC键暂停\n按P键截图", msec, fps, TerrainManager.CurrentChunk());
            RaycastResult?r    = GetComponent <TerrainRaycast>().LookingAt;
            if (r.HasValue)
            {
                //text += string.Format ("\nlooking at {0}", BlocksData.GetBlock(BlockTerrain.GetContent(r.Value.BlockValue)).Name);
                text += string.Format("\nlooking at {0}", BlocksData.Blocks[BlockTerrain.GetContent(r.Value.BlockValue)].ToString(r.Value.BlockValue));
            }
            GUI.Label(rect, text, style);
        }
    }
Ejemplo n.º 12
0
        // Token: 0x06000264 RID: 612 RVA: 0x0001EF38 File Offset: 0x0001D138
        private void ProcessMousePress(CusPointerInputModule.MouseButtonEventData data)
        {
            PointerEventData buttonData = data.buttonData;
            GameObject       gameObject = buttonData.pointerCurrentRaycast.gameObject;

            if (data.PressedThisFrame())
            {
                if (!UIHintMask.bPassThrough)
                {
                    RaycastResult?maskRay = this.MaskRay;
                    if (maskRay != null && gameObject != null && gameObject.name[0] != '~')
                    {
                        CusPointerInputModule.MouseButtonEventData eventData = this.m_MaskMouseState.GetButtonState(PointerEventData.InputButton.Left, 0).eventData;
                        PointerEventData buttonData2 = eventData.buttonData;
                        if (buttonData2.pointerCurrentRaycast.gameObject.activeInHierarchy)
                        {
                            return;
                        }
                    }
                }
                buttonData.eligibleForClick    = true;
                buttonData.delta               = Vector2.zero;
                buttonData.dragging            = false;
                buttonData.useDragThreshold    = true;
                buttonData.pressPosition       = buttonData.position;
                buttonData.pointerPressRaycast = buttonData.pointerCurrentRaycast;
                base.DeselectIfSelectionChanged(gameObject, buttonData);
                GameObject gameObject2 = ExecuteEvents.ExecuteHierarchy <IPointerDownHandler>(gameObject, buttonData, ExecuteEvents.pointerDownHandler);
                if (gameObject2 == null)
                {
                    gameObject2 = ExecuteEvents.GetEventHandler <IPointerClickHandler>(gameObject);
                }
                float unscaledTime = Time.unscaledTime;
                if (gameObject2 == buttonData.lastPress)
                {
                    float num = unscaledTime - buttonData.clickTime;
                    if (num < 0.3f)
                    {
                        buttonData.clickCount++;
                    }
                    else
                    {
                        buttonData.clickCount = 1;
                    }
                    buttonData.clickTime = unscaledTime;
                }
                else
                {
                    buttonData.clickCount = 1;
                }
                buttonData.pointerPress    = gameObject2;
                buttonData.rawPointerPress = gameObject;
                buttonData.clickTime       = unscaledTime;
                buttonData.pointerDrag     = ExecuteEvents.GetEventHandler <IDragHandler>(gameObject);
                if (buttonData.pointerDrag != null)
                {
                    ExecuteEvents.Execute <IInitializePotentialDragHandler>(buttonData.pointerDrag, buttonData, ExecuteEvents.initializePotentialDrag);
                }
            }
            if (data.ReleasedThisFrame())
            {
                RaycastResult?maskRay2 = this.MaskRay;
                if (maskRay2 != null)
                {
                    CusPointerInputModule.MouseButtonEventData eventData2 = this.m_MaskMouseState.GetButtonState(PointerEventData.InputButton.Left, 0).eventData;
                    PointerEventData buttonData3 = eventData2.buttonData;
                    if (buttonData3.pointerCurrentRaycast.gameObject.activeInHierarchy)
                    {
                        ExecuteEvents.Execute <IPointerClickHandler>(buttonData3.pointerCurrentRaycast.gameObject, buttonData3, ExecuteEvents.pointerClickHandler);
                        if (!UIHintMask.bPassThrough && gameObject != null && gameObject.name[0] != '~')
                        {
                            buttonData.eligibleForClick = false;
                            buttonData.pointerPress     = null;
                            buttonData.rawPointerPress  = null;
                            buttonData.dragging         = false;
                            buttonData.pointerDrag      = null;
                            if (gameObject != buttonData.pointerEnter)
                            {
                                base.HandlePointerExitAndEnter(buttonData, null);
                                base.HandlePointerExitAndEnter(buttonData, gameObject);
                            }
                            return;
                        }
                    }
                }
                ExecuteEvents.Execute <IPointerUpHandler>(buttonData.pointerPress, buttonData, ExecuteEvents.pointerUpHandler);
                GameObject eventHandler = ExecuteEvents.GetEventHandler <IPointerClickHandler>(gameObject);
                if (buttonData.pointerPress == eventHandler && buttonData.eligibleForClick)
                {
                    ExecuteEvents.Execute <IPointerClickHandler>(buttonData.pointerPress, buttonData, ExecuteEvents.pointerClickHandler);
                }
                else if (buttonData.pointerDrag != null)
                {
                    ExecuteEvents.ExecuteHierarchy <IDropHandler>(gameObject, buttonData, ExecuteEvents.dropHandler);
                }
                buttonData.eligibleForClick = false;
                buttonData.pointerPress     = null;
                buttonData.rawPointerPress  = null;
                if (buttonData.pointerDrag != null && buttonData.dragging)
                {
                    ExecuteEvents.Execute <IEndDragHandler>(buttonData.pointerDrag, buttonData, ExecuteEvents.endDragHandler);
                }
                buttonData.dragging    = false;
                buttonData.pointerDrag = null;
                if (gameObject != buttonData.pointerEnter)
                {
                    base.HandlePointerExitAndEnter(buttonData, null);
                    base.HandlePointerExitAndEnter(buttonData, gameObject);
                }
            }
        }
    // Update is called once per frame
    public override void SystemUpdate()
    {
        currentRaycastFromPointer   = RaycastFromPointer();
        currentSelectableUnderMouse = GetSelectableObjectUnderMouse();

        Vector2 viewportPos = mainCamera.ScreenToViewportPoint(Input.mousePosition);

        if (viewportPos.x < 0 || viewportPos.x > 1 || viewportPos.y < 0 || viewportPos.y > 1)
        {
            world2DPosition = null;
        }
        else
        {
            Vector3 screenPos = Input.mousePosition;
            float   maxY      = mainCamera.WorldToScreenPoint(editor.mouseYMaxLimit.position).y;

            // Calculate world2DPosition
            if (Input.mousePosition.y > maxY)
            {
                screenPos.y = maxY;
            }

            Ray ray       = mainCamera.ScreenPointToRay(screenPos);
            int layerMask = 1 << LayerMask.NameToLayer("Ignore Raycast");
            if (Physics.RaycastNonAlloc(ray, screenToPointHits, Mathf.Infinity, layerMask) > 0)
            {
                world2DPosition = screenToPointHits[0].point;
            }
            else
            {
                world2DPosition = null;
            }
        }

        if (cancel || (selectedGameObject && !selectedGameObject.activeSelf))
        {
            selectedGameObject = null;
            cancel             = false;
        }

        // OnSelectableMouseDown
        if ((Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)) && world2DPosition != null)
        {
            initMouseDragPos = (Vector2)world2DPosition;

            selectedGameObject = currentSelectableUnderMouse;

            if (selectedGameObject && selectedGameObject.activeSelf)
            {
                SelectableClick[] monos = selectedGameObject.GetComponents <SelectableClick>();

                foreach (SelectableClick mono in monos)
                {
                    mono.OnSelectableMouseDown();
                }
            }
        }

        if ((Input.GetMouseButton(0) || Input.GetMouseButton(1)) && world2DPosition != null && world2DPosition != initMouseDragPos)
        {
            dragging = true;
        }

        // OnSelectableMouseDrag
        if (dragging && selectedGameObject && selectedGameObject.activeSelf)
        {
            SelectableClick[] monos = selectedGameObject.GetComponents <SelectableClick>();
            foreach (SelectableClick mono in monos)
            {
                mono.OnSelectableMouseDrag();
            }
        }

        // OnSelectableMouseOver
        if (currentSelectableUnderMouse && currentSelectableUnderMouse.activeSelf)
        {
            SelectableClick[] mouseOver = currentSelectableUnderMouse.GetComponents <SelectableClick>();
            foreach (SelectableClick mono in mouseOver)
            {
                mono.OnSelectableMouseOver();
            }
        }

        // OnSelectableMouseUp
        if ((Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1)) && world2DPosition != null)
        {
            SendOnSelectableMouseUp();
        }

        if (cancel || (selectedGameObject && !selectedGameObject.activeSelf))
        {
            selectedGameObject = null;
            cancel             = false;
        }
    }
Ejemplo n.º 14
0
        // Token: 0x0600026F RID: 623 RVA: 0x0001F4AC File Offset: 0x0001D6AC
        private void ProcessTouchPress(PointerEventData pointerEvent, bool pressed, bool released)
        {
            GameObject gameObject = pointerEvent.pointerCurrentRaycast.gameObject;

            if (pressed)
            {
                if (!UIHintMask.bPassThrough)
                {
                    RaycastResult?maskRay = this.MaskRay;
                    if (maskRay != null && this.MaskEvent != null && gameObject != null && gameObject.name[0] != '~')
                    {
                        return;
                    }
                }
                pointerEvent.eligibleForClick    = true;
                pointerEvent.delta               = Vector2.zero;
                pointerEvent.dragging            = false;
                pointerEvent.useDragThreshold    = true;
                pointerEvent.pressPosition       = pointerEvent.position;
                pointerEvent.pointerPressRaycast = pointerEvent.pointerCurrentRaycast;
                base.DeselectIfSelectionChanged(gameObject, pointerEvent);
                if (pointerEvent.pointerEnter != gameObject)
                {
                    base.HandlePointerExitAndEnter(pointerEvent, gameObject);
                    pointerEvent.pointerEnter = gameObject;
                }
                GameObject gameObject2 = ExecuteEvents.ExecuteHierarchy <IPointerDownHandler>(gameObject, pointerEvent, ExecuteEvents.pointerDownHandler);
                if (gameObject2 == null)
                {
                    gameObject2 = ExecuteEvents.GetEventHandler <IPointerClickHandler>(gameObject);
                }
                float unscaledTime = Time.unscaledTime;
                if (gameObject2 == pointerEvent.lastPress)
                {
                    float num = unscaledTime - pointerEvent.clickTime;
                    if (num < 0.3f)
                    {
                        pointerEvent.clickCount++;
                    }
                    else
                    {
                        pointerEvent.clickCount = 1;
                    }
                    pointerEvent.clickTime = unscaledTime;
                }
                else
                {
                    pointerEvent.clickCount = 1;
                }
                pointerEvent.pointerPress    = gameObject2;
                pointerEvent.rawPointerPress = gameObject;
                pointerEvent.clickTime       = unscaledTime;
                pointerEvent.pointerDrag     = ExecuteEvents.GetEventHandler <IDragHandler>(gameObject);
                if (pointerEvent.pointerDrag != null)
                {
                    ExecuteEvents.Execute <IInitializePotentialDragHandler>(pointerEvent.pointerDrag, pointerEvent, ExecuteEvents.initializePotentialDrag);
                }
            }
            if (released)
            {
                RaycastResult?maskRay2 = this.MaskRay;
                if (maskRay2 != null)
                {
                    ExecuteEvents.Execute <IPointerClickHandler>(this.MaskEvent.pointerCurrentRaycast.gameObject, this.MaskEvent, ExecuteEvents.pointerClickHandler);
                    if (!UIHintMask.bPassThrough && gameObject != null && gameObject.name[0] != '~')
                    {
                        pointerEvent.eligibleForClick = false;
                        pointerEvent.pointerPress     = null;
                        pointerEvent.rawPointerPress  = null;
                        pointerEvent.dragging         = false;
                        pointerEvent.pointerDrag      = null;
                        pointerEvent.pointerEnter     = null;
                        return;
                    }
                }
                ExecuteEvents.Execute <IPointerUpHandler>(pointerEvent.pointerPress, pointerEvent, ExecuteEvents.pointerUpHandler);
                GameObject eventHandler = ExecuteEvents.GetEventHandler <IPointerClickHandler>(gameObject);
                if (pointerEvent.pointerPress == eventHandler && pointerEvent.eligibleForClick)
                {
                    ExecuteEvents.Execute <IPointerClickHandler>(pointerEvent.pointerPress, pointerEvent, ExecuteEvents.pointerClickHandler);
                }
                else if (pointerEvent.pointerDrag != null)
                {
                    ExecuteEvents.ExecuteHierarchy <IDropHandler>(gameObject, pointerEvent, ExecuteEvents.dropHandler);
                }
                pointerEvent.eligibleForClick = false;
                pointerEvent.pointerPress     = null;
                pointerEvent.rawPointerPress  = null;
                if (pointerEvent.pointerDrag != null && pointerEvent.dragging)
                {
                    ExecuteEvents.Execute <IEndDragHandler>(pointerEvent.pointerDrag, pointerEvent, ExecuteEvents.endDragHandler);
                }
                pointerEvent.dragging    = false;
                pointerEvent.pointerDrag = null;
                if (pointerEvent.pointerDrag != null)
                {
                    ExecuteEvents.Execute <IEndDragHandler>(pointerEvent.pointerDrag, pointerEvent, ExecuteEvents.endDragHandler);
                }
                pointerEvent.pointerDrag = null;
                ExecuteEvents.ExecuteHierarchy <IPointerExitHandler>(pointerEvent.pointerEnter, pointerEvent, ExecuteEvents.pointerExitHandler);
                pointerEvent.pointerEnter = null;
            }
        }
Ejemplo n.º 15
0
        public override void Raycast(PointerEventData eventData, List <RaycastResult> resultAppendList)
        {
            if (m_canvas != null)
            {
                float hitDistance;
                if (hitTestDistance > 0.01f)
                {
                    hitDistance = hitTestDistance;
                }
                else
                {
                    hitDistance = eventCamera.farClipPlane;
                }

#if LOG_CONSOLE_VRGraphicRaycaster
                Debug.DrawRay(eventData.pointerCurrentRaycast.worldPosition, eventData.pointerCurrentRaycast.worldNormal, Color.red);
#endif
                Ray ray = new Ray(eventData.pointerCurrentRaycast.worldPosition, eventData.pointerCurrentRaycast.worldNormal);


                if (BlockingObjects != GraphicRaycaster.BlockingObjects.None)
                {
                    if ((BlockingObjects & GraphicRaycaster.BlockingObjects.ThreeD) == GraphicRaycaster.BlockingObjects.ThreeD)
                    {
                        RaycastHit hit;
                        if (Physics.Raycast(ray, out hit, hitDistance, m_BlockingMask))
                        {
                            hitDistance = Mathf.Min(hit.distance, hitDistance);
                        }
                    }

                    if ((BlockingObjects & GraphicRaycaster.BlockingObjects.TwoD) == GraphicRaycaster.BlockingObjects.TwoD)
                    {
                        RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, eventData.pointerCurrentRaycast.depth, m_BlockingMask);
                        if (hit.collider != null)
                        {
                            float currentHit = hit.fraction * eventData.pointerCurrentRaycast.depth;
                            if (currentHit < hitDistance)
                            {
                                hitDistance = currentHit;
                            }
                        }
                    }
                }

                m_RaycastResults.Clear();


                Raycast(m_canvas, ray, hitDistance, eventCamera, m_RaycastResults);

                RaycastResult?closestResult = null;

                for (var index = 0; index < m_RaycastResults.Count; index++)
                {
                    var go = m_RaycastResults[index].graphic.gameObject;

                    RaycastResult castResult = new RaycastResult
                    {
                        gameObject     = go,
                        module         = this,
                        distance       = m_RaycastResults[index].distance,
                        screenPosition = m_RaycastResults[index].pointerPosition,
                        worldPosition  = m_RaycastResults[index].position,
                        index          = resultAppendList.Count,
                        depth          = m_RaycastResults[index].graphic.depth,
                        sortingLayer   = m_canvas.sortingLayerID,
                        sortingOrder   = m_canvas.sortingOrder
                    };
                    resultAppendList.Add(castResult);
                    if (!closestResult.HasValue || castResult.distance < closestResult.Value.distance)
                    {
                        closestResult = castResult;
                    }
                }
                if (closestResult.HasValue)
                {
                    eventData.pointerCurrentRaycast = closestResult.Value;
                    eventData.position = closestResult.Value.screenPosition;
                    eventData.delta    = eventData.position - m_lastPosition;
                    m_lastPosition     = eventData.position;
                }
            }
        }
Ejemplo n.º 16
0
        public override void Raycast(PointerEventData eventData, List <RaycastResult> resultAppendList)
        {
            float hitDistance = eventCamera.farClipPlane;

#if LOG_CONSOLE_VRGraphicRaycaster
            Debug.DrawRay(eventData.pointerCurrentRaycast.worldPosition, eventData.pointerCurrentRaycast.worldNormal, Color.red);
#endif
            Ray ray = new Ray(eventData.pointerCurrentRaycast.worldPosition, eventData.pointerCurrentRaycast.worldNormal);

            // Phisics hits (to block the ray)
            {
                // 3D hits
                {
                    RaycastHit hit;
                    if (Physics.Raycast(ray, out hit, eventData.pointerCurrentRaycast.depth, m_BlockingMask))
                    {
                        hitDistance = hit.distance;
                    }
                }
                // 2D hits
                {
                    RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, eventData.pointerCurrentRaycast.depth, m_BlockingMask);

                    if (hit.collider != null)
                    {
                        float currentHit = hit.fraction * eventData.pointerCurrentRaycast.depth;
                        if (currentHit < hitDistance)
                        {
                            hitDistance = currentHit;
                        }
                    }
                }
            }

            m_RaycastResults.Clear();

            Raycast(m_canvas, ray, hitDistance, eventCamera, m_RaycastResults);
#if LOG_CONSOLE_VRGraphicRaycaster
            //StringBuilder cast = new StringBuilder();
            //for (int i = 0; i < m_RaycastResults.Count; ++i)
            //{
            //    cast.Append(m_RaycastResults[i]);
            //    cast.Append("\n");
            //}
            //if (cast.Length > 0)
            //    Log(cast.ToString());
#endif

            RaycastResult?closestResult = null;
            eventData.hovered.Clear();
            for (var index = 0; index < m_RaycastResults.Count; index++)
            {
                var go = m_RaycastResults[index].graphic.gameObject;

                RaycastResult castResult = new RaycastResult
                {
                    gameObject     = go,
                    module         = this,
                    distance       = m_RaycastResults[index].distance,
                    screenPosition = m_RaycastResults[index].pointerPosition,
                    worldPosition  = m_RaycastResults[index].position,
                    index          = resultAppendList.Count,
                    depth          = m_RaycastResults[index].graphic.depth,
                    sortingLayer   = m_canvas.sortingLayerID,
                    sortingOrder   = m_canvas.sortingOrder
                };
                resultAppendList.Add(castResult);
                if (!closestResult.HasValue || castResult.distance < closestResult.Value.distance)
                {
                    closestResult = castResult;
                }
                eventData.hovered.Add(castResult.gameObject);
            }
            if (closestResult.HasValue)
            {
                eventData.pointerCurrentRaycast = closestResult.Value;
                eventData.position = closestResult.Value.screenPosition;
                eventData.delta    = eventData.position - m_lastPosition;
                m_lastPosition     = eventData.position;
            }
        }
Ejemplo n.º 17
0
        private RaycastResult?TryGetPreferredHit(IList <RaycastResult> semiOrderedHits, IList <LayerMask> prioritizedLayerMasks)
        {
            Debug.Assert(semiOrderedHits != null);
            Debug.Assert(prioritizedLayerMasks != null);

            RaycastResult?preferred = null;
            int           preferredLayerMaskIndex = int.MaxValue;

            for (int iHit = 0; iHit < semiOrderedHits.Count; iHit++)
            {
                RaycastResult hit = semiOrderedHits[iHit];
                int           hitLayerMaskIndex = GetLayerMaskIndex(hit.gameObject, prioritizedLayerMasks);


                // First, prefer by order in priority layer masks list:

                if ((preferred == null) || (hitLayerMaskIndex < preferredLayerMaskIndex))
                {
                    preferred = hit;
                    preferredLayerMaskIndex = hitLayerMaskIndex;
                    continue;
                }
                else if (hitLayerMaskIndex > preferredLayerMaskIndex)
                {
                    continue;
                }
                Debug.Assert(hitLayerMaskIndex == preferredLayerMaskIndex);


                // Then by biggest sorting layer:

                if (hit.sortingLayer > preferred.Value.sortingLayer)
                {
                    preferred = hit;
                    preferredLayerMaskIndex = hitLayerMaskIndex;
                    continue;
                }
                else if (hit.sortingLayer < preferred.Value.sortingLayer)
                {
                    continue;
                }
                Debug.Assert(hit.sortingLayer == preferred.Value.sortingLayer);


                // Then by biggest order in layer:

                if (hit.sortingOrder > preferred.Value.sortingOrder)
                {
                    preferred = hit;
                    preferredLayerMaskIndex = hitLayerMaskIndex;
                    continue;
                }
                else if (hit.sortingOrder < preferred.Value.sortingOrder)
                {
                    continue;
                }
                Debug.Assert(hit.sortingOrder == preferred.Value.sortingOrder);


                // Then by smallest distance:

                if (hit.distance < preferred.Value.distance)
                {
                    preferred = hit;
                    preferredLayerMaskIndex = hitLayerMaskIndex;
                    continue;
                }
                else if (hit.distance > preferred.Value.distance)
                {
                    continue;
                }
                Debug.Assert(hit.distance == preferred.Value.distance);


                // Then by order in hits list, which seems to break the tie correctly for UI layered flat on
                // the same canvas. By virtue of letting the loop continue here without updating preferred,
                // we break the tie with the order in hits list.
            }

            return(preferred);
        }
        /*--------------------------------------------------------------------------------------------*/
        public void Update()
        {
            Controllers.TryExpireControllers();

            if (CursorDataProvider == null)
            {
                Debug.LogError("Reference to " + typeof(HoverCursorDataProvider).Name +
                               " must be set.", this);
                return;
            }

            ICursorData   cursor  = GetCursorData();
            RaycastResult?raycast = cursor.BestRaycastResult;
            Transform     tx      = gameObject.transform;

            if (FollowCursorActive)
            {
                foreach (GameObject go in ObjectsToActivate)
                {
                    go.SetActive(cursor.IsActive);
                }
            }

            if (FollowCursorPosition)
            {
                Controllers.Set(SettingsControllerMap.TransformPosition, this, 0);
                tx.position = (raycast == null ? cursor.WorldPosition : raycast.Value.WorldPosition);
            }

            if (FollowCursorRotation)
            {
                Controllers.Set(SettingsControllerMap.TransformRotation, this, 0);

                if (raycast == null)
                {
                    tx.rotation = cursor.WorldRotation;
                }
                else
                {
                    RaycastResult rc      = raycast.Value;
                    Vector3       perpDir = (cursor.RaycastLocalDirection == Vector3.up ?
                                             Vector3.right : Vector3.up);    //TODO: does this work in all cases?
                    Vector3    castUpPos          = rc.WorldPosition + rc.WorldRotation * perpDir;
                    Vector3    cursorUpPos        = rc.WorldPosition + cursor.WorldRotation * perpDir;
                    float      upToPlaneDist      = rc.WorldPlane.GetDistanceToPoint(cursorUpPos);
                    Vector3    cursorUpOnPlanePos = cursorUpPos - rc.WorldPlane.normal * upToPlaneDist;
                    Quaternion invCastRot         = Quaternion.Inverse(rc.WorldRotation);
                    Vector3    fromLocalVec       = invCastRot * (castUpPos - rc.WorldPosition);
                    Vector3    toLocalVec         = invCastRot * (cursorUpOnPlanePos - rc.WorldPosition);
                    Quaternion applyRot           = Quaternion.FromToRotation(fromLocalVec, toLocalVec);
                    //Debug.DrawLine(rc.WorldPosition, castUpPos, Color.red);
                    //Debug.DrawLine(rc.WorldPosition, cursorUpOnPlanePos, Color.blue);

                    tx.rotation = rc.WorldRotation * applyRot;
                }
            }

            if (ScaleUsingCursorSize)
            {
                Controllers.Set(SettingsControllerMap.TransformLocalScale, this, 0);
                tx.localScale = Vector3.one * (cursor.Size * CursorSizeMultiplier);
            }
        }