Exemplo n.º 1
0
        static float RotationSlider(int controlID, Vector3 cornerPos, float rotation, Vector3 pivot, Vector3 handleDir, Vector3 outwardsDir1, Vector3 outwardsDir2, float handleSize, HandlesExtra.CapFunction drawFunc, Vector2 snap)
        {
            EventType eventType = Event.current.GetTypeForControl(controlID);

            Vector3 b            = outwardsDir1 + outwardsDir2;
            Vector3 guiCornerPos = HandleUtility.WorldToGUIPoint(cornerPos);
            Vector3 b2           = ((Vector3)HandleUtility.WorldToGUIPoint(cornerPos + b) - guiCornerPos).normalized * 15f;

            cornerPos = HandlesExtra.GUIToWorld(guiCornerPos + b2);

            Vector3 newPosition = HandlesExtra.Slider2D(controlID, cornerPos, drawFunc);

            rotation = rotation - AngleAroundAxis(newPosition - pivot, cornerPos - pivot, handleDir);

            if (eventType == EventType.Layout)
            {
                HandleUtility.AddControl(controlID, HandleUtility.DistanceToCircle(cornerPos, HandleUtility.GetHandleSize(cornerPos + b) / 10f));
            }

            if (eventType == EventType.Repaint)
            {
                if ((HandleUtility.nearestControl == controlID && GUIUtility.hotControl == 0) || GUIUtility.hotControl == controlID)
                {
                    Rect cursorRect = new Rect(0, 0, 20f, 20f);
                    cursorRect.center = Event.current.mousePosition;
                    EditorGUIUtility.AddCursorRect(cursorRect, MouseCursor.RotateArrow, controlID);
                }
            }

            return(rotation);
        }
Exemplo n.º 2
0
        static Vector3 SideSlider(int controlID, Vector3 position, Vector3 sideVector, Vector3 direction, HandlesExtra.CapFunction drawFunc)
        {
            Vector3 vector = HandlesExtra.Slider2D(controlID, position, drawFunc);

            vector = position + Vector3.Project(vector - position, direction);

            return(vector);
        }
Exemplo n.º 3
0
        static Rect MoveHandlesGUI(Rect rect, ref Vector3 position, Quaternion rotation, bool anchorPivot = false)
        {
            int       controlID = GUIUtility.GetControlID("RectMoveHandles".GetHashCode(), FocusType.Passive);
            EventType eventType = Event.current.GetTypeForControl(controlID);

            if (eventType == EventType.MouseDown)
            {
                s_StartRect = rect;
            }

            EditorGUI.BeginChangeCheck();

            Vector3 newPosition = HandlesExtra.Slider2D(controlID, position, null);

            if (EditorGUI.EndChangeCheck())
            {
                if (anchorPivot)
                {
                    Vector2 delta = Quaternion.Inverse(rotation) * (newPosition - position);

                    rect.min = s_StartRect.min + delta;
                    rect.max = s_StartRect.max + delta;
                }
                else
                {
                    position = newPosition;
                }
            }

            if (eventType == EventType.Layout)
            {
                Vector2 mousePositionRectSpace = Vector2.zero;

                mousePositionRectSpace = Quaternion.Inverse(rotation) *
                                         (HandlesExtra.GUIToWorld(Event.current.mousePosition) - position);

                if (rect.Contains(mousePositionRectSpace, true))
                {
                    HandleUtility.AddControl(controlID, 0f);
                }
            }

            if (eventType == EventType.Repaint)
            {
                if ((!Event.current.alt &&
                     HandleUtility.nearestControl == controlID &&
                     GUIUtility.hotControl == 0) ||
                    GUIUtility.hotControl == controlID)
                {
                    Rect cursorRect = new Rect(0f, 0f, 20f, 20f);
                    cursorRect.center = Event.current.mousePosition;
                    EditorGUIUtility.AddCursorRect(cursorRect, MouseCursor.MoveArrow, controlID);
                }
            }

            return(rect);
        }
Exemplo n.º 4
0
        public static Rect Do(int controlID)
        {
            EventType eventType = Event.current.GetTypeForControl(controlID);

            if (eventType == EventType.MouseDown)
            {
                s_StartPosition        = HandlesExtra.GUIToWorld(Event.current.mousePosition);
                s_EndPosition          = s_StartPosition;
                s_currentRect.position = s_StartPosition;
                s_currentRect.size     = Vector2.zero;
            }

            if (eventType == EventType.Layout)
            {
                HandleUtility.AddDefaultControl(controlID);
            }

            if (eventType == EventType.Repaint)
            {
                if (GUIUtility.hotControl == controlID)
                {
                    RectHandles.RenderRect(s_currentRect, Vector3.zero, Quaternion.identity, new Color(0f, 1f, 1f, 1f),
                                           0.05f, 0.8f);
                }
            }

            if (Camera.current)
            {
#if UNITY_5_6_OR_NEWER
                s_EndPosition = Handles.Slider2D(controlID, s_EndPosition, Vector3.forward, Vector3.right, Vector3.up,
                                                 HandleUtility.GetHandleSize(s_EndPosition) / 4f, (id, pos, rot, size, evt) => { }, Vector2.zero);
#else
                s_EndPosition =
                    Handles.Slider2D(controlID, s_EndPosition, Vector3.forward, Vector3.right, Vector3.up, HandleUtility.GetHandleSize(s_EndPosition) / 4f, (id, pos, rot, size) => {}, Vector2.zero);
#endif
            }
            else
            {
                s_EndPosition = HandlesExtra.Slider2D(controlID, s_EndPosition, null);
            }

            s_currentRect.min = s_StartPosition;
            s_currentRect.max = s_EndPosition;

            return(s_currentRect);
        }
Exemplo n.º 5
0
        static Rect PivotHandleGUI(Rect rect, ref Vector3 position, Quaternion rotation)
        {
            int       controlID = GUIUtility.GetControlID("RectPivotHandle".GetHashCode(), FocusType.Passive);
            EventType eventType = Event.current.GetTypeForControl(controlID);

            EditorGUI.BeginChangeCheck();

            Vector3 newPosition = HandlesExtra.Slider2D(controlID, position, HandlesExtra.PivotCap);

            if (EditorGUI.EndChangeCheck())
            {
                Vector3 pivotDelta      = (newPosition - position);
                Vector2 pivotDeltaLocal = Quaternion.Inverse(rotation) * pivotDelta;
                position       = newPosition;
                rect.position -= pivotDeltaLocal;
            }

            if (eventType == EventType.Layout)
            {
                float radius = HandleUtility.GetHandleSize(position) / 2f;

                if (Camera.current)
                {
                    radius = HandleUtility.GetHandleSize(position) / 10f;
                }

                HandleUtility.AddControl(controlID, HandleUtility.DistanceToCircle(position, radius));
            }

            if (eventType == EventType.Repaint)
            {
                if ((HandleUtility.nearestControl == controlID && GUIUtility.hotControl == 0) ||
                    GUIUtility.hotControl == controlID)
                {
                    Rect cursorRect = new Rect(0, 0, 20f, 20f);
                    cursorRect.center = Event.current.mousePosition;

                    EditorGUIUtility.AddCursorRect(cursorRect, MouseCursor.Arrow, controlID);
                }
            }

            return(rect);
        }
Exemplo n.º 6
0
        static Rect ResizeHandlesGUI(Rect rect, ref Vector3 position, Quaternion rotation, bool anchorPivot = false)
        {
            if (Event.current.type == EventType.MouseDown)
            {
                s_StartRect     = rect;
                s_CurrentRect   = rect;
                s_StartPosition = position;
                s_StartPivot    = GetNormalizedPivot(rect);
            }

            Vector3    scale           = Vector3.one;
            Quaternion inverseRotation = Quaternion.Inverse(rotation);

            for (int i = 0; i <= 2; i++)
            {
                for (int j = 0; j <= 2; j++)
                {
                    if (i != 1 || j != 1)
                    {
                        Vector3 startWorldPoint   = GetRectPointInWorld(s_StartRect, s_StartPosition, rotation, i, j);
                        Vector3 currentWorldPoint = GetRectPointInWorld(s_CurrentRect, s_StartPosition, rotation, i, j);
                        Vector3 rectWorldPoint    = GetRectPointInWorld(rect, position, rotation, i, j);

                        int controlID = GUIUtility.GetControlID("RectResizeHandles".GetHashCode(), FocusType.Passive);

                        EventType eventType = Event.current.GetTypeForControl(controlID);

                        if (GUI.color.a > 0f || GUIUtility.hotControl == controlID)
                        {
                            EditorGUI.BeginChangeCheck();
                            Vector3 newPosition = Vector3.zero;

                            MouseCursor cursor = MouseCursor.Arrow;

                            if (i == 1 || j == 1)
                            {
                                Vector3 sideVector = (i != 1) ? (rotation * Vector3.up * rect.height) : (rotation * Vector3.right * rect.width);
                                Vector3 direction  = (i != 1) ? (rotation * Vector3.right) : (rotation * Vector3.up);
                                newPosition = SideSlider(controlID, currentWorldPoint, sideVector, direction, null);

                                if (!Event.current.alt && eventType == EventType.Layout)
                                {
                                    Vector3 normalized2 = sideVector.normalized;

                                    Vector3 p1     = rectWorldPoint + sideVector * 0.5f;
                                    Vector3 p2     = rectWorldPoint - sideVector * 0.5f;
                                    Vector3 offset = -normalized2 *HandleUtility.GetHandleSize(p1) / 20f;

                                    Vector3 offset2 = normalized2 * HandleUtility.GetHandleSize(p2) / 20f;

                                    HandleUtility.AddControl(controlID, HandleUtility.DistanceToLine(p1 + offset, p2 + offset2));
                                }

                                cursor = GetScaleCursor(direction);
                            }
                            else
                            {
                                HandlesExtra.DotCap(controlID, rectWorldPoint, Quaternion.identity, 1f, eventType);

                                newPosition = HandlesExtra.Slider2D(controlID, currentWorldPoint, null);

                                if (!Event.current.alt && eventType == EventType.Layout)
                                {
                                    HandleUtility.AddControl(controlID, HandleUtility.DistanceToCircle(rectWorldPoint, HandleUtility.GetHandleSize(rectWorldPoint) / 20f));
                                }

                                Vector3 outwardsDir  = rotation * Vector3.right * (float)(i - 1);
                                Vector3 outwardsDir2 = rotation * Vector3.up * (float)(j - 1);

                                cursor = GetScaleCursor(outwardsDir + outwardsDir2);
                            }

                            if (eventType == EventType.Repaint)
                            {
                                if ((HandleUtility.nearestControl == controlID && GUIUtility.hotControl == 0) || GUIUtility.hotControl == controlID)
                                {
                                    Rect cursorRect = new Rect(0, 0, 20f, 20f);

                                    cursorRect.center = Event.current.mousePosition;

                                    EditorGUIUtility.AddCursorRect(cursorRect, cursor, controlID);
                                }
                            }

                            if (EditorGUI.EndChangeCheck())
                            {
                                Vector3 scalePivot      = Vector3.zero;
                                Vector2 scalePivotLocal = Vector2.one;

                                bool alt       = Event.current.alt;
                                bool actionKey = EditorGUI.actionKey;
                                bool shiftDown = Event.current.shift && !actionKey;
                                if (!alt)
                                {
                                    scalePivot      = GetRectPointInWorld(s_StartRect, s_StartPosition, rotation, 2 - i, 2 - j);
                                    scalePivotLocal = inverseRotation * (scalePivot - s_StartPosition);
                                }

                                Vector3 localRectPoint   = inverseRotation * (startWorldPoint - scalePivot);
                                Vector3 localNewPosition = inverseRotation * (newPosition - scalePivot);

                                if (i != 1)
                                {
                                    scale.x = localNewPosition.x / localRectPoint.x;
                                }
                                if (j != 1)
                                {
                                    scale.y = localNewPosition.y / localRectPoint.y;
                                }
                                if (shiftDown)
                                {
                                    float d = (i != 1) ? scale.x : scale.y;
                                    scale = Vector3.one * d;
                                }
                                if (actionKey && i == 1)
                                {
                                    if (Event.current.shift)
                                    {
                                        scale.x = (scale.z = 1f / Mathf.Sqrt(Mathf.Max(scale.y, 0.0001f)));
                                    }
                                    else
                                    {
                                        scale.x = 1f / Mathf.Max(scale.y, 0.0001f);
                                    }
                                }
                                if (shiftDown)
                                {
                                    float d2 = (i != 1) ? scale.x : scale.y;
                                    scale = Vector3.one * d2;
                                }
                                if (actionKey && i == 1)
                                {
                                    if (Event.current.shift)
                                    {
                                        scale.x = (scale.z = 1f / Mathf.Sqrt(Mathf.Max(scale.y, 0.0001f)));
                                    }
                                    else
                                    {
                                        scale.x = 1f / Mathf.Max(scale.y, 0.0001f);
                                    }
                                }
                                if (actionKey && j == 1)
                                {
                                    if (Event.current.shift)
                                    {
                                        scale.y = (scale.z = 1f / Mathf.Sqrt(Mathf.Max(scale.x, 0.0001f)));
                                    }
                                    else
                                    {
                                        scale.y = 1f / Mathf.Max(scale.x, 0.0001f);
                                    }
                                }

                                s_CurrentRect.min = Vector2.Scale(scale, s_StartRect.min - scalePivotLocal) + scalePivotLocal;
                                s_CurrentRect.max = Vector2.Scale(scale, s_StartRect.max - scalePivotLocal) + scalePivotLocal;

                                if (anchorPivot)
                                {
                                    rect.min = s_CurrentRect.min;
                                    rect.max = s_CurrentRect.max;
                                }
                                else
                                {
                                    rect.position = Vector2.Scale(scale, s_StartRect.position);
                                    rect.size     = Vector2.Scale(scale, s_StartRect.size);

                                    Vector2 newPivot = new Vector2(s_CurrentRect.xMin + (s_CurrentRect.xMax - s_CurrentRect.xMin) * s_StartPivot.x,
                                                                   s_CurrentRect.yMin + (s_CurrentRect.yMax - s_CurrentRect.yMin) * s_StartPivot.y);

                                    position = s_StartPosition + rotation * newPivot;
                                }
                            }
                        }
                    }
                }
            }
            return(rect);
        }