Пример #1
0
        public static Vector3 Do(int id, Vector3 position, Quaternion rotation, float size, Vector3 snap, Handles.DrawCapFunction capFunc)
        {
            Vector3   position2 = Handles.matrix.MultiplyPoint(position);
            Matrix4x4 matrix    = Handles.matrix;

            VertexSnapping.HandleKeyAndMouseMove(id);
            Event current = Event.current;

            switch (current.GetTypeForControl(id))
            {
            case EventType.MouseDown:
                if ((HandleUtility.nearestControl == id && current.button == 0) || (GUIUtility.keyboardControl == id && current.button == 2))
                {
                    GUIUtility.keyboardControl         = id;
                    GUIUtility.hotControl              = id;
                    FreeMove.s_CurrentMousePosition    = (FreeMove.s_StartMousePosition = current.mousePosition);
                    FreeMove.s_StartPosition           = position;
                    HandleUtility.ignoreRaySnapObjects = null;
                    current.Use();
                    EditorGUIUtility.SetWantsMouseJumping(1);
                }
                break;

            case EventType.MouseUp:
                if (GUIUtility.hotControl == id && (current.button == 0 || current.button == 2))
                {
                    GUIUtility.hotControl = 0;
                    HandleUtility.ignoreRaySnapObjects = null;
                    current.Use();
                    EditorGUIUtility.SetWantsMouseJumping(0);
                }
                break;

            case EventType.MouseDrag:
                if (GUIUtility.hotControl == id)
                {
                    bool flag = EditorGUI.actionKey && current.shift;
                    if (flag)
                    {
                        if (HandleUtility.ignoreRaySnapObjects == null)
                        {
                            Handles.SetupIgnoreRaySnapObjects();
                        }
                        object obj = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(current.mousePosition));
                        if (obj != null)
                        {
                            RaycastHit raycastHit = (RaycastHit)obj;
                            float      d          = 0f;
                            if (Tools.pivotMode == PivotMode.Center)
                            {
                                float num = HandleUtility.CalcRayPlaceOffset(HandleUtility.ignoreRaySnapObjects, raycastHit.normal);
                                if (num != float.PositiveInfinity)
                                {
                                    d = Vector3.Dot(position, raycastHit.normal) - num;
                                }
                            }
                            position = Handles.s_InverseMatrix.MultiplyPoint(raycastHit.point + raycastHit.normal * d);
                        }
                        else
                        {
                            flag = false;
                        }
                    }
                    if (!flag)
                    {
                        FreeMove.s_CurrentMousePosition += new Vector2(current.delta.x, -current.delta.y);
                        Vector3 vector = Camera.current.WorldToScreenPoint(Handles.s_Matrix.MultiplyPoint(FreeMove.s_StartPosition));
                        vector  += FreeMove.s_CurrentMousePosition - FreeMove.s_StartMousePosition;
                        position = Handles.s_InverseMatrix.MultiplyPoint(Camera.current.ScreenToWorldPoint(vector));
                        if (Camera.current.transform.forward == Vector3.forward || Camera.current.transform.forward == -Vector3.forward)
                        {
                            position.z = FreeMove.s_StartPosition.z;
                        }
                        if (Camera.current.transform.forward == Vector3.up || Camera.current.transform.forward == -Vector3.up)
                        {
                            position.y = FreeMove.s_StartPosition.y;
                        }
                        if (Camera.current.transform.forward == Vector3.right || Camera.current.transform.forward == -Vector3.right)
                        {
                            position.x = FreeMove.s_StartPosition.x;
                        }
                        if (Tools.vertexDragging)
                        {
                            if (HandleUtility.ignoreRaySnapObjects == null)
                            {
                                Handles.SetupIgnoreRaySnapObjects();
                            }
                            Vector3 v;
                            if (HandleUtility.FindNearestVertex(current.mousePosition, null, out v))
                            {
                                position = Handles.s_InverseMatrix.MultiplyPoint(v);
                            }
                        }
                        if (EditorGUI.actionKey && !current.shift)
                        {
                            Vector3 b = position - FreeMove.s_StartPosition;
                            b.x      = Handles.SnapValue(b.x, snap.x);
                            b.y      = Handles.SnapValue(b.y, snap.y);
                            b.z      = Handles.SnapValue(b.z, snap.z);
                            position = FreeMove.s_StartPosition + b;
                        }
                    }
                    GUI.changed = true;
                    current.Use();
                }
                break;

            case EventType.Repaint:
            {
                Color color = Color.white;
                if (id == GUIUtility.keyboardControl)
                {
                    color         = Handles.color;
                    Handles.color = Handles.selectedColor;
                }
                Handles.matrix = Matrix4x4.identity;
                capFunc(id, position2, Camera.current.transform.rotation, size);
                Handles.matrix = matrix;
                if (id == GUIUtility.keyboardControl)
                {
                    Handles.color = color;
                }
                break;
            }

            case EventType.Layout:
                Handles.matrix = Matrix4x4.identity;
                HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(position2, size * 1.2f));
                Handles.matrix = matrix;
                break;
            }
            return(position);
        }
Пример #2
0
        #pragma warning disable 618
        public static Vector3 Do(int id, Vector3 position, Quaternion rotation, float size, Vector3 snap, Handles.DrawCapFunction capFunc)
        #pragma warning restore 618
        {
            Vector3   worldPosition = Handles.matrix.MultiplyPoint(position);
            Matrix4x4 origMatrix    = Handles.matrix;

            VertexSnapping.HandleMouseMove(id);

            Event evt = Event.current;

            switch (evt.GetTypeForControl(id))
            {
            case EventType.Layout:
                // We only want the position to be affected by the Handles.matrix.
                Handles.matrix = Matrix4x4.identity;
                HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(worldPosition, size * 1.2f));
                Handles.matrix = origMatrix;
                break;

            case EventType.MouseDown:
                // am I closest to the thingy?
                if (HandleUtility.nearestControl == id && evt.button == 0)
                {
                    GUIUtility.hotControl              = id; // Grab mouse focus
                    s_CurrentMousePosition             = s_StartMousePosition = evt.mousePosition;
                    s_StartPosition                    = position;
                    HandleUtility.ignoreRaySnapObjects = null;
                    evt.Use();
                    EditorGUIUtility.SetWantsMouseJumping(1);
                }
                break;

            case EventType.MouseDrag:
                if (GUIUtility.hotControl == id)
                {
                    bool rayDrag = EditorGUI.actionKey && evt.shift;

                    if (rayDrag)
                    {
                        if (HandleUtility.ignoreRaySnapObjects == null)
                        {
                            Handles.SetupIgnoreRaySnapObjects();
                        }

                        object hit = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(evt.mousePosition));
                        if (hit != null)
                        {
                            RaycastHit rh     = (RaycastHit)hit;
                            float      offset = 0;
                            if (Tools.pivotMode == PivotMode.Center)
                            {
                                float geomOffset = HandleUtility.CalcRayPlaceOffset(HandleUtility.ignoreRaySnapObjects, rh.normal);
                                if (geomOffset != Mathf.Infinity)
                                {
                                    offset = Vector3.Dot(position, rh.normal) - geomOffset;
                                }
                            }
                            position = Handles.inverseMatrix.MultiplyPoint(rh.point + (rh.normal * offset));
                        }
                        else
                        {
                            rayDrag = false;
                        }
                    }

                    if (!rayDrag)
                    {
                        // normal drag
                        s_CurrentMousePosition += new Vector2(evt.delta.x, -evt.delta.y) * EditorGUIUtility.pixelsPerPoint;
                        Vector3 screenPos = Camera.current.WorldToScreenPoint(Handles.matrix.MultiplyPoint(s_StartPosition));
                        screenPos += (Vector3)(s_CurrentMousePosition - s_StartMousePosition);
                        position   = Handles.inverseMatrix.MultiplyPoint(Camera.current.ScreenToWorldPoint(screenPos));

                        // Due to floating point inaccuracies, the back-and-forth transformations used may sometimes introduce
                        // tiny unintended movement in wrong directions. People notice when using a straight top/left/right ortho camera.
                        // In that case, just restrain the movement to the plane.
                        if (Camera.current.transform.forward == Vector3.forward || Camera.current.transform.forward == -Vector3.forward)
                        {
                            position.z = s_StartPosition.z;
                        }
                        if (Camera.current.transform.forward == Vector3.up || Camera.current.transform.forward == -Vector3.up)
                        {
                            position.y = s_StartPosition.y;
                        }
                        if (Camera.current.transform.forward == Vector3.right || Camera.current.transform.forward == -Vector3.right)
                        {
                            position.x = s_StartPosition.x;
                        }

                        if (Tools.vertexDragging)
                        {
                            if (HandleUtility.ignoreRaySnapObjects == null)
                            {
                                Handles.SetupIgnoreRaySnapObjects();
                            }
                            Vector3 near;
                            if (HandleUtility.FindNearestVertex(evt.mousePosition, null, out near))
                            {
                                position = Handles.inverseMatrix.MultiplyPoint(near);
                            }
                        }

                        if (EditorGUI.actionKey && !evt.shift)
                        {
                            Vector3 delta = position - s_StartPosition;
                            delta.x  = Handles.SnapValue(delta.x, snap.x);
                            delta.y  = Handles.SnapValue(delta.y, snap.y);
                            delta.z  = Handles.SnapValue(delta.z, snap.z);
                            position = s_StartPosition + delta;
                        }
                    }
                    GUI.changed = true;
                    evt.Use();
                }
                break;

            case EventType.MouseUp:
                if (GUIUtility.hotControl == id && (evt.button == 0 || evt.button == 2))
                {
                    GUIUtility.hotControl = 0;
                    HandleUtility.ignoreRaySnapObjects = null;
                    evt.Use();
                    EditorGUIUtility.SetWantsMouseJumping(0);
                }
                break;

            case EventType.MouseMove:
                if (id == HandleUtility.nearestControl)
                {
                    HandleUtility.Repaint();
                }
                break;

            case EventType.Repaint:
                Color temp = Color.white;

                if (id == GUIUtility.hotControl)
                {
                    temp          = Handles.color;
                    Handles.color = Handles.selectedColor;
                }
                else if (id == HandleUtility.nearestControl && GUIUtility.hotControl == 0)
                {
                    temp          = Handles.color;
                    Handles.color = Handles.preselectionColor;
                }

                // We only want the position to be affected by the Handles.matrix.
                Handles.matrix = Matrix4x4.identity;
                capFunc(id, worldPosition, Camera.current.transform.rotation, size);
                Handles.matrix = origMatrix;

                if (id == GUIUtility.hotControl || id == HandleUtility.nearestControl && GUIUtility.hotControl == 0)
                {
                    Handles.color = temp;
                }
                break;
            }
            return(position);
        }
Пример #3
0
        public static Vector3 Do(int id, Vector3 position, Quaternion rotation, float size, Vector3 snap, Handles.CapFunction handleFunction)
        {
            Vector3   worldPosition = Handles.matrix.MultiplyPoint(position);
            Matrix4x4 origMatrix    = Handles.matrix;

            VertexSnapping.HandleMouseMove(id);

            Event evt = Event.current;

            switch (evt.GetTypeForControl(id))
            {
            case EventType.Layout:
                // We only want the position to be affected by the Handles.matrix.
                Handles.matrix = Matrix4x4.identity;
                handleFunction(id, worldPosition, Camera.current.transform.rotation, size, EventType.Layout);
                Handles.matrix = origMatrix;
                break;

            case EventType.MouseDown:
                // am I closest to the thingy?
                if (HandleUtility.nearestControl == id && evt.button == 0)
                {
                    GUIUtility.hotControl              = id; // Grab mouse focus
                    s_CurrentMousePosition             = s_StartMousePosition = evt.mousePosition;
                    s_StartPosition                    = position;
                    HandleUtility.ignoreRaySnapObjects = null;
                    evt.Use();
                    EditorGUIUtility.SetWantsMouseJumping(1);
                }
                break;

            case EventType.MouseDrag:
                if (GUIUtility.hotControl == id)
                {
                    bool rayDrag = EditorGUI.actionKey && evt.shift;

                    if (rayDrag)
                    {
                        if (HandleUtility.ignoreRaySnapObjects == null)
                        {
                            Handles.SetupIgnoreRaySnapObjects();
                        }

                        object hit = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(evt.mousePosition));
                        if (hit != null)
                        {
                            RaycastHit rh     = (RaycastHit)hit;
                            float      offset = 0;
                            if (Tools.pivotMode == PivotMode.Center)
                            {
                                float geomOffset = HandleUtility.CalcRayPlaceOffset(HandleUtility.ignoreRaySnapObjects, rh.normal);
                                if (geomOffset != Mathf.Infinity)
                                {
                                    offset = Vector3.Dot(position, rh.normal) - geomOffset;
                                }
                            }
                            position = Handles.inverseMatrix.MultiplyPoint(rh.point + (rh.normal * offset));
                        }
                        else
                        {
                            rayDrag = false;
                        }
                    }

                    if (!rayDrag)
                    {
                        // normal drag
                        s_CurrentMousePosition += new Vector2(evt.delta.x, -evt.delta.y) * EditorGUIUtility.pixelsPerPoint;
                        Vector3 screenPos = Camera.current.WorldToScreenPoint(Handles.matrix.MultiplyPoint(s_StartPosition));
                        screenPos += (Vector3)(s_CurrentMousePosition - s_StartMousePosition);
                        position   = Handles.inverseMatrix.MultiplyPoint(Camera.current.ScreenToWorldPoint(screenPos));

                        if (Tools.vertexDragging)
                        {
                            if (HandleUtility.ignoreRaySnapObjects == null)
                            {
                                Handles.SetupIgnoreRaySnapObjects();
                            }
                            Vector3 near;
                            if (HandleUtility.FindNearestVertex(evt.mousePosition, null, out near))
                            {
                                position = Handles.inverseMatrix.MultiplyPoint(near);
                            }
                        }

                        if (EditorGUI.actionKey && !evt.shift)
                        {
                            Vector3 delta = position - s_StartPosition;
                            delta.x  = Handles.SnapValue(delta.x, snap.x);
                            delta.y  = Handles.SnapValue(delta.y, snap.y);
                            delta.z  = Handles.SnapValue(delta.z, snap.z);
                            position = s_StartPosition + delta;
                        }
                    }
                    GUI.changed = true;
                    evt.Use();
                }
                break;

            case EventType.MouseUp:
                if (GUIUtility.hotControl == id && (evt.button == 0 || evt.button == 2))
                {
                    GUIUtility.hotControl = 0;
                    HandleUtility.ignoreRaySnapObjects = null;
                    evt.Use();
                    EditorGUIUtility.SetWantsMouseJumping(0);
                }
                break;

            case EventType.MouseMove:
                if (id == HandleUtility.nearestControl)
                {
                    HandleUtility.Repaint();
                }
                break;

            case EventType.Repaint:
                Color temp = Color.white;

                if (id == GUIUtility.hotControl)
                {
                    temp          = Handles.color;
                    Handles.color = Handles.selectedColor;
                }
                else if (id == HandleUtility.nearestControl && GUIUtility.hotControl == 0)
                {
                    temp          = Handles.color;
                    Handles.color = Handles.preselectionColor;
                }

                // We only want the position to be affected by the Handles.matrix.
                Handles.matrix = Matrix4x4.identity;
                handleFunction(id, worldPosition, Camera.current.transform.rotation, size, EventType.Repaint);
                Handles.matrix = origMatrix;

                if (id == GUIUtility.hotControl || id == HandleUtility.nearestControl && GUIUtility.hotControl == 0)
                {
                    Handles.color = temp;
                }
                break;
            }
            return(position);
        }
Пример #4
0
        public static Vector3 Do(int id, Vector3 position, Quaternion rotation, float size, Vector3 snap, Handles.DrawCapFunction capFunc)
        {
            bool      flag;
            Vector3   vector = Handles.matrix.MultiplyPoint(position);
            Matrix4x4 matrix = Handles.matrix;

            VertexSnapping.HandleKeyAndMouseMove(id);
            Event current = Event.current;

            switch (current.GetTypeForControl(id))
            {
            case EventType.MouseDown:
                if (((HandleUtility.nearestControl == id) && (current.button == 0)) || ((GUIUtility.keyboardControl == id) && (current.button == 2)))
                {
                    int num3 = id;
                    GUIUtility.keyboardControl         = num3;
                    GUIUtility.hotControl              = num3;
                    s_CurrentMousePosition             = s_StartMousePosition = current.mousePosition;
                    s_StartPosition                    = position;
                    HandleUtility.ignoreRaySnapObjects = null;
                    current.Use();
                    EditorGUIUtility.SetWantsMouseJumping(1);
                }
                return(position);

            case EventType.MouseUp:
                if ((GUIUtility.hotControl == id) && ((current.button == 0) || (current.button == 2)))
                {
                    GUIUtility.hotControl = 0;
                    HandleUtility.ignoreRaySnapObjects = null;
                    current.Use();
                    EditorGUIUtility.SetWantsMouseJumping(0);
                }
                return(position);

            case EventType.MouseMove:
            case EventType.KeyDown:
            case EventType.KeyUp:
            case EventType.ScrollWheel:
                return(position);

            case EventType.MouseDrag:
                if (GUIUtility.hotControl != id)
                {
                    return(position);
                }
                flag = EditorGUI.actionKey && current.shift;
                if (flag)
                {
                    if (HandleUtility.ignoreRaySnapObjects == null)
                    {
                        Handles.SetupIgnoreRaySnapObjects();
                    }
                    object obj2 = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(current.mousePosition));
                    if (obj2 == null)
                    {
                        flag = false;
                        break;
                    }
                    RaycastHit hit = (RaycastHit)obj2;
                    float      num = 0f;
                    if (Tools.pivotMode == PivotMode.Center)
                    {
                        float num2 = HandleUtility.CalcRayPlaceOffset(HandleUtility.ignoreRaySnapObjects, hit.normal);
                        if (num2 != float.PositiveInfinity)
                        {
                            num = Vector3.Dot(position, hit.normal) - num2;
                        }
                    }
                    position = Handles.s_InverseMatrix.MultiplyPoint(hit.point + ((Vector3)(hit.normal * num)));
                }
                break;

            case EventType.Repaint:
            {
                Color white = Color.white;
                if (id == GUIUtility.keyboardControl)
                {
                    white         = Handles.color;
                    Handles.color = Handles.selectedColor;
                }
                Handles.matrix = Matrix4x4.identity;
                capFunc(id, vector, Camera.current.transform.rotation, size);
                Handles.matrix = matrix;
                if (id == GUIUtility.keyboardControl)
                {
                    Handles.color = white;
                }
                return(position);
            }

            case EventType.Layout:
                Handles.matrix = Matrix4x4.identity;
                HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(vector, size * 1.2f));
                Handles.matrix = matrix;
                return(position);

            default:
                return(position);
            }
            if (!flag)
            {
                s_CurrentMousePosition += new Vector2(current.delta.x, -current.delta.y);
                Vector3 vector2 = Camera.current.WorldToScreenPoint(Handles.s_Matrix.MultiplyPoint(s_StartPosition)) + (s_CurrentMousePosition - s_StartMousePosition);
                position = Handles.s_InverseMatrix.MultiplyPoint(Camera.current.ScreenToWorldPoint(vector2));
                if ((Camera.current.transform.forward == Vector3.forward) || (Camera.current.transform.forward == -Vector3.forward))
                {
                    position.z = s_StartPosition.z;
                }
                if ((Camera.current.transform.forward == Vector3.up) || (Camera.current.transform.forward == -Vector3.up))
                {
                    position.y = s_StartPosition.y;
                }
                if ((Camera.current.transform.forward == Vector3.right) || (Camera.current.transform.forward == -Vector3.right))
                {
                    position.x = s_StartPosition.x;
                }
                if (Tools.vertexDragging)
                {
                    Vector3 vector3;
                    if (HandleUtility.ignoreRaySnapObjects == null)
                    {
                        Handles.SetupIgnoreRaySnapObjects();
                    }
                    if (HandleUtility.FindNearestVertex(current.mousePosition, null, out vector3))
                    {
                        position = Handles.s_InverseMatrix.MultiplyPoint(vector3);
                    }
                }
                if (EditorGUI.actionKey && !current.shift)
                {
                    Vector3 vector4 = position - s_StartPosition;
                    vector4.x = Handles.SnapValue(vector4.x, snap.x);
                    vector4.y = Handles.SnapValue(vector4.y, snap.y);
                    vector4.z = Handles.SnapValue(vector4.z, snap.z);
                    position  = s_StartPosition + vector4;
                }
            }
            GUI.changed = true;
            current.Use();
            return(position);
        }
Пример #5
0
        public static Vector3 Do(int id, Vector3 position, float size, Vector3 snap, Handles.CapFunction handleFunction)
        {
            Vector3   worldPosition = Handles.matrix.MultiplyPoint(position);
            Matrix4x4 origMatrix    = Handles.matrix;

            VertexSnapping.HandleMouseMove(id);

            Event evt = Event.current;

            switch (evt.GetTypeForControl(id))
            {
            case EventType.MouseMove:
            case EventType.Layout:
                // We only want the position to be affected by the Handles.matrix.
                Handles.matrix = Matrix4x4.identity;
                handleFunction(id, worldPosition, Camera.current.transform.rotation, size, EventType.Layout);
                Handles.matrix = origMatrix;
                break;

            case EventType.MouseDown:
                // am I closest to the thingy?
                if (HandleUtility.nearestControl == id && evt.button == 0)
                {
                    GUIUtility.hotControl              = id; // Grab mouse focus
                    s_CurrentMousePosition             = s_CurrentMousePositionScreen = s_StartMousePosition = evt.mousePosition;
                    s_StartPosition                    = position;
                    HandleUtility.ignoreRaySnapObjects = null;
                    evt.Use();
                    EditorGUIUtility.SetWantsMouseJumping(1);
                }
                break;

            case EventType.MouseDrag:
                if (GUIUtility.hotControl == id)
                {
                    bool rayDrag = EditorGUI.actionKey && evt.shift;

                    if (rayDrag)
                    {
                        if (HandleUtility.ignoreRaySnapObjects == null)
                        {
                            Handles.SetupIgnoreRaySnapObjects();
                        }

                        if (HandleUtility.PlaceObject(evt.mousePosition, out Vector3 point, out Vector3 normal))
                        {
                            float offset = 0;
                            if (Tools.pivotMode == PivotMode.Center)
                            {
                                float geomOffset = HandleUtility.CalcRayPlaceOffset(HandleUtility.ignoreRaySnapObjects, normal);
                                if (geomOffset != Mathf.Infinity)
                                {
                                    offset = Vector3.Dot(position, normal) - geomOffset;
                                }
                            }
                            position = Handles.inverseMatrix.MultiplyPoint(point + (normal * offset));
                        }
                        else
                        {
                            rayDrag = false;
                        }
                    }

                    if (!rayDrag)
                    {
                        // normal drag
                        Vector2 mouseDelta = evt.mousePosition - s_CurrentMousePositionScreen;
                        s_CurrentMousePositionScreen += mouseDelta;
                        s_CurrentMousePosition       += new Vector2(mouseDelta.x, -mouseDelta.y) * EditorGUIUtility.pixelsPerPoint;
                        Vector3 screenPos = Camera.current.WorldToScreenPoint(Handles.matrix.MultiplyPoint(s_StartPosition));
                        screenPos += (Vector3)(s_CurrentMousePosition - s_StartMousePosition);
                        position   = Handles.inverseMatrix.MultiplyPoint(Camera.current.ScreenToWorldPoint(screenPos));

                        if (Tools.vertexDragging)
                        {
                            if (HandleUtility.ignoreRaySnapObjects == null)
                            {
                                Handles.SetupIgnoreRaySnapObjects();
                            }
                            Vector3 near;
                            if (HandleUtility.FindNearestVertex(evt.mousePosition, null, out near))
                            {
                                position = Handles.inverseMatrix.MultiplyPoint(near);
                            }
                        }

                        if (EditorSnapSettings.incrementalSnapActive && !evt.shift)
                        {
                            Vector3 delta = position - s_StartPosition;
                            delta    = Handles.SnapValue(delta, snap);
                            position = s_StartPosition + delta;
                        }
                    }
                    GUI.changed = true;
                    evt.Use();
                }
                break;