Exemple #1
0
        private void CheckInteract()
        {
            //Don't check for handle interactions if it is hidden or if the cursor is over the UI
            if (hidden || EventSystem.current.IsPointerOverGameObject(-1))
            {
                return;
            }

            //The axis or axes along which the tool handle is being dragged
            Axis plane;

            //Check if the tool handle was clicked
            draggingHandle = CheckHandleActivated(currentMousePosition, out plane);

            //If the tool handle wasn't clicked, don't start the interaction
            if (!draggingHandle)
            {
                return;
            }

            //Save the current transform of the tool handle
            handleOrigin.SetTRS(parentTransform);

            //Reset the axes being dragged
            drag.worldAxis = Vector3.zero;
            drag.localAxis = Vector3.zero;
            draggingAxes   = 0;

            //Set the relevant variables based on the drag plane
            SetDragData(plane);

            //Save the handle position and its distance to the camera
            if (Tool == Tool.Translate)
            {
                originalPosition = parentTransform.position;
                cameraDist       = (mainCamera.transform.position - parentTransform.position).magnitude;
            }
            //Save the current rotation, reset the total displacement and save the angle of the cursor click
            if (Tool == Tool.Rotate)
            {
                originalRotation = parentTransform.rotation;
                axisAngle        = 0f;
                clickTangent     = GetClickTangent();
            }
            //Reset the handle size and prime it for scaling
            else
            {
                prevScale      = Vector3.one;
                scale          = Vector3.one;
                currCursorDist = GetMouseHandleDist(currentMousePosition);
            }

            //Capture the cursor while dragging
            EditorManager.Instance.CaptureCursor();
            //Notify all listeners that the tool handle was activated
            OnHandleBegin?.Invoke();
        }
        void OnMouseDown()
        {
            scale = Vector3.one;

            drag.offset = Vector3.zero;

            axisAngle = 0f;

            DraggingHandle = CheckHandleActivated(Input.mousePosition, out Axis plane);

            mouseOrigin = Input.mousePosition;
            handleOrigin.SetTRS(Trs);

            drag.axis    = Vector3.zero;
            draggingAxes = 0;

            if (DraggingHandle)
            {
                Ray ray = Cam.ScreenPointToRay(Input.mousePosition);

                if ((plane & Axis.X) == Axis.X)
                {
                    draggingAxes++;
                    drag.axis = Trs.right;
                    drag.plane.SetNormalAndPosition(Trs.right.normalized, Trs.position);
                }

                if ((plane & Axis.Y) == Axis.Y)
                {
                    draggingAxes++;
                    if (draggingAxes > 1)
                    {
                        drag.plane.SetNormalAndPosition(Vector3.Cross(drag.axis, Trs.up).normalized, Trs.position);
                    }
                    else
                    {
                        drag.plane.SetNormalAndPosition(Trs.up.normalized, Trs.position);
                    }
                    drag.axis += Trs.up;
                }

                if ((plane & Axis.Z) == Axis.Z)
                {
                    draggingAxes++;
                    if (draggingAxes > 1)
                    {
                        drag.plane.SetNormalAndPosition(Vector3.Cross(drag.axis, Trs.forward).normalized, Trs.position);
                    }
                    else
                    {
                        drag.plane.SetNormalAndPosition(Trs.forward.normalized, Trs.position);
                    }
                    drag.axis += Trs.forward;
                }

                if (draggingAxes < 2)
                {
                    if (pb_HandleUtility.PointOnLine(new Ray(Trs.position, drag.axis), ray, out Vector3 a, out Vector3 b))
                    {
                        drag.offset = a - Trs.position;
                    }

                    if (drag.plane.Raycast(ray, out float hit))
                    {
                        drag.mouse = (ray.GetPoint(hit) - Trs.position).normalized;
                        drag.cross = Vector3.Cross(drag.axis, drag.mouse);
                    }
                }
                else
                {
                    if (drag.plane.Raycast(ray, out float hit))
                    {
                        drag.offset = ray.GetPoint(hit) - Trs.position;
                        drag.mouse  = (ray.GetPoint(hit) - Trs.position).normalized;
                        drag.cross  = Vector3.Cross(drag.axis, drag.mouse);
                    }
                }

                OnHandleBegin?.Invoke(GetTransform());
            }
        }