Ejemplo n.º 1
0
            public static Vector3[] Do(int id, Vector3[] points, Vector3 handleOrigin, Vector3 handleCursorOffset, Vector3 handleNormal, Vector3 slideDir1, Vector3 slideDir2, float handleSize, SceneHandles.CapFunction capFunction, Axes axes = Axes.None, bool selectLockingAxisOnClick = false, bool noSnapping = false, Vector3?snappingSteps = null)
            {
                var evt = Event.current;

                switch (evt.GetTypeForControl(id))
                {
                case EventType.MouseDown:
                {
                    if (Tools.current == Tool.View ||
                        Tools.current == Tool.None ||
                        evt.alt)
                    {
                        break;
                    }

                    if (GUIUtility.hotControl != 0)
                    {
                        break;
                    }

                    if ((UnityEditor.HandleUtility.nearestControl != id || evt.button != 0) &&
                        (GUIUtility.keyboardControl != id || evt.button != 2))
                    {
                        break;
                    }

                    GUIUtility.hotControl = GUIUtility.keyboardControl = id;
                    evt.Use();
                    EditorGUIUtility.SetWantsMouseJumping(1);

                    s_CurrentMousePosition = evt.mousePosition;
                    s_StartPoints          = points.ToArray();


                    var       localToWorldMatrix = UnityEditor.Handles.matrix;
                    var       center             = Grid.ActiveGrid.Center;
                    Matrix4x4 gridSpace          = Matrix4x4.identity;
                    gridSpace.SetColumn(0, localToWorldMatrix.MultiplyVector(slideDir1).normalized);
                    gridSpace.SetColumn(1, localToWorldMatrix.MultiplyVector(handleNormal).normalized);
                    gridSpace.SetColumn(2, localToWorldMatrix.MultiplyVector(slideDir2).normalized);
                    gridSpace.SetColumn(3, new Vector4(center.x, center.y, center.z, 1.0f));

                    var workGrid = new Grid(gridSpace, snappingSteps.HasValue ? snappingSteps.Value : Snapping.MoveSnappingSteps);

                    s_Snapping2D.Initialize(workGrid, s_CurrentMousePosition, handleOrigin, localToWorldMatrix);
                    s_Snapping2D.CalculateExtents(s_StartPoints);
                    s_MovedMouse = false;
                    break;
                }

                case EventType.MouseDrag:
                {
                    if (GUIUtility.hotControl != id)
                    {
                        break;
                    }

                    s_MovedMouse = true;

                    if (SceneHandles.disabled || Snapping.AreAxisLocked(axes))
                    {
                        break;
                    }

                    s_CurrentMousePosition += evt.delta;
                    evt.Use();

                    if (!s_Snapping2D.DragTo(s_CurrentMousePosition, noSnapping ? SnappingMode.Never: SnappingMode.Default))
                    {
                        break;
                    }

                    var handleInverseMatrix = UnityEditor.Handles.inverseMatrix;
                    var pointDelta          = handleInverseMatrix.MultiplyVector(s_Snapping2D.WorldSnappedDelta);

                    if (s_StartPoints != null)
                    {
                        points = new Vector3[points.Length];     // if we don't, it's hard to do Undo properly
                        for (int i = 0; i < points.Length; i++)
                        {
                            points[i] =
                                SnappingUtility.Quantize(s_StartPoints[i] + pointDelta);
                        }
                    }

                    //SceneView.RepaintAll();
                    GUI.changed = true;
                    break;
                }

                case EventType.MouseUp:
                {
                    if (GUIUtility.hotControl == id && (evt.button == 0 || evt.button == 2))
                    {
                        GUIUtility.hotControl      = 0;
                        GUIUtility.keyboardControl = 0;
                        s_StartPoints = null;
                        evt.Use();
                        EditorGUIUtility.SetWantsMouseJumping(0);
                        SceneView.RepaintAll();
                        if (!s_MovedMouse && selectLockingAxisOnClick)
                        {
                            Snapping.ActiveAxes = axes;
                        }
                    }
                    break;
                }

                case EventType.Layout:
                {
                    if (Tools.current == Tool.View ||
                        Tools.current == Tool.None ||
                        evt.alt)
                    {
                        break;
                    }

                    var position = handleOrigin + handleCursorOffset;
                    var rotation = Quaternion.LookRotation(handleNormal, slideDir1);

                    if (capFunction != null)
                    {
                        capFunction(id, position, rotation, handleSize, EventType.Layout);
                    }
                    else
                    {
                        UnityEditor.HandleUtility.AddControl(id, UnityEditor.HandleUtility.DistanceToCircle(position, handleSize * .5f));
                    }

                    int currentFocusControl = SceneHandleUtility.focusControl;
                    if ((currentFocusControl == id && s_PrevFocusControl != id) ||
                        (currentFocusControl != id && s_PrevFocusControl == id))
                    {
                        s_PrevFocusControl = currentFocusControl;
                        SceneView.RepaintAll();
                    }
                    break;
                }

                case EventType.Repaint:
                {
                    if (axes != Axes.None)
                    {
                        if (GUIUtility.hotControl == id)
                        {
                            var selectedColor = UnityEditor.Handles.selectedColor;
                            selectedColor.a = 0.5f;
                            using (new SceneHandles.DrawingScope(selectedColor))
                                HandleRendering.RenderSnapping3D(s_Snapping2D.WorldSlideGrid, s_Snapping2D.WorldSnappedExtents, s_Snapping2D.GridSnappedPosition, s_Snapping2D.SnapResult);
                        }
                    }

                    if (capFunction == null)
                    {
                        break;
                    }

                    var position = handleOrigin + handleCursorOffset;
                    var rotation = Quaternion.LookRotation(handleNormal, slideDir1);
                    var color    = SceneHandles.StateColor(SceneHandles.color, isSelected: (id == s_PrevFocusControl));

                    using (new SceneHandles.DrawingScope(color))
                    {
                        capFunction(id, position, rotation, handleSize, EventType.Repaint);
                    }
                    break;
                }
                }
                return(points);
            }
Ejemplo n.º 2
0
            internal static Vector3[] Do(int id, Axis axis, Vector3[] points, Vector3 handleOrigin, Vector3 handleDirection, Vector3 slideDirection, float snappingStep = 0, float handleSize = 0, SceneHandles.CapFunction capFunction = null, bool selectLockingAxisOnClick = false)
            {
                if (snappingStep == 0)
                {
                    snappingStep = Snapping.MoveSnappingSteps[(int)axis];
                }
                if (handleSize == 0)
                {
                    handleSize = UnityEditor.HandleUtility.GetHandleSize(handleOrigin) * 0.05f;
                }

                if (handleDirection.sqrMagnitude == 0)
                {
                    return(points);
                }

                var evt  = Event.current;
                var type = evt.GetTypeForControl(id);

                switch (type)
                {
                case EventType.MouseDown:
                {
                    if (SceneHandles.InCameraOrbitMode)
                    {
                        break;
                    }

                    if (GUIUtility.hotControl != 0)
                    {
                        break;
                    }

                    if ((UnityEditor.HandleUtility.nearestControl != id || evt.button != 0) &&
                        (GUIUtility.keyboardControl != id || evt.button != 2))
                    {
                        break;
                    }

                    GUIUtility.hotControl = GUIUtility.keyboardControl = id;
                    evt.Use();
                    EditorGUIUtility.SetWantsMouseJumping(1);

                    s_CurrentMousePosition = evt.mousePosition;
                    s_StartPoints          = points.ToArray();
                    var handleMatrix = SceneHandles.matrix;

                    s_Snapping1D.Initialize(s_CurrentMousePosition,
                                            handleMatrix.MultiplyPoint(handleOrigin),
                                            handleMatrix.MultiplyVector(slideDirection),
                                            snappingStep, axis);
                    s_Snapping1D.CalculateExtents(SceneHandles.inverseMatrix, s_StartPoints);
                    s_MovedMouse = false;
                    break;
                }

                case EventType.MouseDrag:
                {
                    if (GUIUtility.hotControl != id)
                    {
                        break;
                    }

                    s_MovedMouse = true;

                    if (SceneHandles.disabled || Snapping.IsAxisLocked(axis))
                    {
                        break;
                    }

                    s_CurrentMousePosition += evt.delta;
                    evt.Use();

                    if (!s_Snapping1D.Move(s_CurrentMousePosition))
                    {
                        break;
                    }

                    var handleInverseMatrix = SceneHandles.inverseMatrix;
                    var pointDelta          = handleInverseMatrix.MultiplyVector(s_Snapping1D.WorldSnappedOffset);

                    if (s_StartPoints != null)
                    {
                        points = new Vector3[points.Length];     // if we don't, it's hard to do Undo properly
                        for (int i = 0; i < points.Length; i++)
                        {
                            points[i] = SnappingUtility.Quantize(s_StartPoints[i] + pointDelta);
                        }
                    }
                    //SceneView.RepaintAll();
                    GUI.changed = true;
                    break;
                }

                case EventType.MouseUp:
                {
                    if (GUIUtility.hotControl == id && (evt.button == 0 || evt.button == 2))
                    {
                        GUIUtility.hotControl      = 0;
                        GUIUtility.keyboardControl = 0;
                        //Grid.currentGrid = s_PrevGrid;
                        s_StartPoints = null;
                        evt.Use();
                        EditorGUIUtility.SetWantsMouseJumping(0);
                        if (!s_MovedMouse && selectLockingAxisOnClick)
                        {
                            switch (axis)
                            {
                            case Axis.X: { Snapping.ActiveAxes = Axes.X; break; }

                            case Axis.Y: { Snapping.ActiveAxes = Axes.Y; break; }

                            case Axis.Z: { Snapping.ActiveAxes = Axes.Z; break; }
                            }
                        }
                        SceneView.RepaintAll();
                    }
                    break;
                }

#if UNITY_2020_1_OR_NEWER
                case EventType.MouseMove:
                {
                    if (SceneHandles.InCameraOrbitMode)
                    {
                        break;
                    }

                    var position = handleOrigin;
                    var rotation = Quaternion.LookRotation(handleDirection);

                    if (handleSize > 0)
                    {
                        if (capFunction != null)
                        {
                            capFunction(id, position, rotation, handleSize, type);
                        }
                    }

                    int currentFocusControl = SceneHandleUtility.focusControl;
                    if ((currentFocusControl == id && s_PrevFocusControl != id) ||
                        (currentFocusControl != id && s_PrevFocusControl == id))
                    {
                        s_PrevFocusControl = currentFocusControl;
                        SceneView.RepaintAll();
                    }
                    break;
                }
#endif
                case EventType.Layout:
                {
                    if (SceneHandles.InCameraOrbitMode)
                    {
                        break;
                    }

                    var position = handleOrigin;
                    var rotation = Quaternion.LookRotation(handleDirection);

                    if (handleSize > 0)
                    {
                        if (capFunction != null)
                        {
                            capFunction(id, position, rotation, handleSize, type);
                        }
                        else
                        {
                            UnityEditor.HandleUtility.AddControl(id, UnityEditor.HandleUtility.DistanceToCircle(position, handleSize * .2f));
                        }
                    }

                    int currentFocusControl = SceneHandleUtility.focusControl;
                    if ((currentFocusControl == id && s_PrevFocusControl != id) ||
                        (currentFocusControl != id && s_PrevFocusControl == id))
                    {
                        s_PrevFocusControl = currentFocusControl;
                        SceneView.RepaintAll();
                    }
                    break;
                }

                case EventType.Repaint:
                {
                    if (axis != Axis.None)
                    {
                        if (GUIUtility.hotControl == id)
                        {
                            var selectedColor = SceneHandles.StateColor(SceneHandles.MultiplyTransparency(SceneHandles.selectedColor, 0.5f));
                            using (new SceneHandles.DrawingScope(selectedColor))
                                HandleRendering.RenderSnapping1D(s_Snapping1D.Min, s_Snapping1D.Max, s_Snapping1D.WorldSnappedPosition, s_Snapping1D.SlideDirection, s_Snapping1D.SnapResult, axis);
                        }
                    }

                    if (capFunction == null)
                    {
                        break;
                    }

                    var position = handleOrigin;
                    var rotation = Quaternion.LookRotation(handleDirection);
                    var color    = SceneHandles.StateColor(SceneHandles.color, isSelected: (id == s_PrevFocusControl));

                    using (new SceneHandles.DrawingScope(color))
                    {
                        capFunction(id, position, rotation, handleSize, EventType.Repaint);
                    }
                    break;
                }
                }
                return(points);
            }