Ejemplo n.º 1
0
        public override void Listener()
        {
            if (Input.GetMouseButtonUp(0) && buttonLocked != null)
            {
                buttonLocked.onUp?.Invoke();

                // invoke onClick event when
                // pointer was not moved or
                // when flag allows read click after pointer and was still on this same object
                if (!moved ||
                    (acceptClickAfterMove && (followedButton3D == buttonLocked)))
                {
                    buttonLocked.onClick?.Invoke();
                }
                if (dragMoved)
                {
                    buttonLocked.onEndDrag?.Invoke();
                    dragMoved = false;
                }
                buttonLocked = null;
            }

            // when object was clicked (onDown) and also was moved then assume dragging
            if (buttonLocked != null && moved)
            {
                float movedDistance = Mathf.Abs(Vector3.Distance(lastInputPosition, Input.mousePosition));
                if (movedDistance > moveTrashHold)
                {
                    buttonLocked.onDrag?.Invoke();
                    dragMoved = true;
                }
            }

            if (EventSystem.current != null && EventSystem.current.IsPointerOverGameObject())
            {
                return;
            }

            Ray        ray = button3DCamera.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
            {
                Button3D button = hit.transform.GetComponent <Button3D>();
                if (button != null)
                {
                    if (followedButton3D != button)
                    {
                        followedButton3D?.onExit?.Invoke();
                        followedButton3D = button;
                        followedButton3D?.onEnter?.Invoke();
                    }

                    if (Input.GetMouseButtonDown(0))
                    {
                        moved = false;
                        firstInputPosition = Input.mousePosition;
                        button.onDown?.Invoke();
                        buttonLocked = button;
                    }

                    // button is pressed on Button3D, check if cursor is moved and cancel possibility to onClick (when flag: acceptClickAfterMove is false)
                    // start read dragging
                    if (Input.GetMouseButton(0))
                    {
                        float movedDistance = Mathf.Abs(Vector3.Distance(firstInputPosition, Input.mousePosition));
                        if (!moved && movedDistance > moveTrashHold)
                        {
                            moved = true;
                            button.onBeginDrag?.Invoke();
                        }
                    }
                }
                else // exit
                {
                    followedButton3D?.onExit?.Invoke();
                    followedButton3D = null;
                }
            }
            else
            {
                followedButton3D?.onExit?.Invoke();
                followedButton3D = null;
            }

            lastInputPosition = Input.mousePosition;
        }
        public override void Listener()
        {
            if (Input.GetMouseButtonUp(0) && buttonLocked != null)
            {
                buttonLocked.onUp?.Invoke();
                if (!moved)
                {
                    buttonLocked.onClick?.Invoke();
                }
                buttonLocked = null;
            }

            if (EventSystem.current != null && EventSystem.current.IsPointerOverGameObject())
            {
                return;
            }

            Ray        ray = button3DCamera.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
            {
                Button3D button = hit.transform.GetComponent <Button3D>();
                if (button != null)
                {
                    if (followedButton3D != button)
                    {
                        followedButton3D?.onExit?.Invoke();
                        followedButton3D = button;
                        followedButton3D?.onEnter?.Invoke();
                    }

                    if (Input.GetMouseButtonDown(0))
                    {
                        moved = false;
                        firstInputPosition = Input.mousePosition;
                        button.onDown?.Invoke();
                        buttonLocked = button;
                    }

                    // button is pressed on Button3D, check if cursor is moved and cancel possibility to onClick if necessary
                    if (Input.GetMouseButton(0))
                    {
                        float movedDistance = Mathf.Abs(Vector3.Distance(firstInputPosition, Input.mousePosition));
                        if (!moved && movedDistance > moveTrashHold)
                        {
                            moved = true;
                        }
                    }
                }
                else // exit
                {
                    followedButton3D?.onExit?.Invoke();
                    followedButton3D = null;
                }
            }
            else
            {
                followedButton3D?.onExit?.Invoke();
                followedButton3D = null;
            }
        }