Beispiel #1
0
        private static Quaternion RotationHandlesGUI(Rect rect, Vector3 pivot, Quaternion rotation)
        {
            Vector3 eulerAngles = rotation.eulerAngles;

            for (int i = 0; i <= 2; i += 2)
            {
                for (int j = 0; j <= 2; j += 2)
                {
                    Vector3 position   = GetRectPointInWorld(rect, pivot, rotation, i, j);
                    float   handleSize = 0.05f * HandleUtility.GetHandleSize(position);
                    int     controlID  = GUIUtility.GetControlID(s_RotationHandlesHash, FocusType.Passive);
                    if ((GUI.color.a > 0f) || (GUIUtility.hotControl == controlID))
                    {
                        EditorGUI.BeginChangeCheck();
                        Vector3 vector3 = (Vector3)((rotation * Vector3.right) * (i - 1));
                        Vector3 vector4 = (Vector3)((rotation * Vector3.up) * (j - 1));
                        float   num5    = RectHandles.RotationSlider(controlID, position, eulerAngles.z, pivot, (Vector3)(rotation * Vector3.forward), vector3, vector4, handleSize, null, Vector2.zero);
                        if (EditorGUI.EndChangeCheck())
                        {
                            if (Event.current.shift)
                            {
                                num5 = (Mathf.Round((num5 - eulerAngles.z) / 15f) * 15f) + eulerAngles.z;
                            }
                            eulerAngles.z = num5;
                            rotation      = Quaternion.Euler(eulerAngles);
                        }
                    }
                }
            }
            return(rotation);
        }
Beispiel #2
0
        internal static Vector3 SideSlider(int id, Vector3 position, Vector3 sideVector, Vector3 direction, float size, Handles.DrawCapFunction drawFunc, float snap, float bias)
        {
            Event   current     = Event.current;
            Vector3 normalized1 = Vector3.Cross(sideVector, direction).normalized;
            Vector3 vector3_1   = Handles.Slider2D(id, position, normalized1, direction, sideVector, 0.0f, drawFunc, Vector2.one * snap);
            Vector3 vector3_2   = position + Vector3.Project(vector3_1 - position, direction);

            switch (current.type)
            {
            case EventType.Repaint:
                if (HandleUtility.nearestControl == id && GUIUtility.hotControl == 0 || GUIUtility.hotControl == id)
                {
                    RectHandles.HandleDirectionalCursor(position, normalized1, direction);
                    break;
                }
                break;

            case EventType.Layout:
                Vector3 normalized2 = sideVector.normalized;
                HandleUtility.AddControl(id, HandleUtility.DistanceToLine(position + sideVector * 0.5f - normalized2 * size * 2f, position - sideVector * 0.5f + normalized2 * size * 2f) - bias);
                break;

            case EventType.MouseMove:
                RectHandles.DetectCursorChange(id);
                break;
            }
            return(vector3_2);
        }
Beispiel #3
0
        internal static Vector3 SideSlider(int id, Vector3 position, Vector3 sideVector, Vector3 direction, float size, Handles.DrawCapFunction drawFunc, float snap, float bias)
        {
            Event   current    = Event.current;
            Vector3 normalized = Vector3.Cross(sideVector, direction).normalized;
            Vector3 vector     = Handles.Slider2D(id, position, normalized, direction, sideVector, 0f, drawFunc, Vector2.one * snap);

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

            if (type != EventType.Repaint)
            {
                if (type != EventType.Layout)
                {
                    if (type == EventType.MouseMove)
                    {
                        RectHandles.DetectCursorChange(id);
                    }
                }
                else
                {
                    Vector3 normalized2 = sideVector.normalized;
                    HandleUtility.AddControl(id, HandleUtility.DistanceToLine(position + sideVector * 0.5f - normalized2 * size * 2f, position - sideVector * 0.5f + normalized2 * size * 2f) - bias);
                }
            }
            else if ((HandleUtility.nearestControl == id && GUIUtility.hotControl == 0) || GUIUtility.hotControl == id)
            {
                RectHandles.HandleDirectionalCursor(position, normalized, direction);
            }
            return(vector);
        }
        static Quaternion RotationHandlesGUI(Rect rect, Vector3 pivot, Quaternion rotation)
        {
            Vector3 euler = rotation.eulerAngles;

            // Loop through the 4 corner handles
            for (int xHandle = 0; xHandle <= 2; xHandle += 2)
            {
                for (int yHandle = 0; yHandle <= 2; yHandle += 2)
                {
                    Vector3 curPos = GetRectPointInWorld(rect, pivot, rotation, xHandle, yHandle);

                    float size = 0.05f * HandleUtility.GetHandleSize(curPos);
                    int   id   = GUIUtility.GetControlID(s_RotationHandlesHash, FocusType.Passive);
                    if (GUI.color.a > 0 || GUIUtility.hotControl == id)
                    {
                        EditorGUI.BeginChangeCheck();
                        Vector3 outwardsA = rotation * Vector3.right * (xHandle - 1);
                        Vector3 outwardsB = rotation * Vector3.up * (yHandle - 1);
                        float   angle     = RectHandles.RotationSlider(id, curPos, euler.z, pivot, rotation * Vector3.forward, outwardsA, outwardsB, size, null, Vector2.zero);
                        if (EditorGUI.EndChangeCheck())
                        {
                            if (Event.current.shift)
                            {
                                angle = Mathf.Round((angle - euler.z) / 15f) * 15f + euler.z;
                            }
                            euler.z  = angle;
                            rotation = Quaternion.Euler(euler);
                        }
                    }
                }
            }
            return(rotation);
        }
Beispiel #5
0
        private static Quaternion RotationHandlesGUI(Rect rect, Vector3 pivot, Quaternion rotation)
        {
            Vector3 eulerAngles = rotation.eulerAngles;
            int     xHandle     = 0;

            while (xHandle <= 2)
            {
                int yHandle = 0;
                while (yHandle <= 2)
                {
                    Vector3 rectPointInWorld = RectTool.GetRectPointInWorld(rect, pivot, rotation, xHandle, yHandle);
                    float   handleSize       = 0.05f * HandleUtility.GetHandleSize(rectPointInWorld);
                    int     controlId        = GUIUtility.GetControlID(RectTool.s_RotationHandlesHash, FocusType.Passive);
                    if ((double)GUI.color.a > 0.0 || GUIUtility.hotControl == controlId)
                    {
                        EditorGUI.BeginChangeCheck();
                        Vector3 outwardsDir1 = rotation * Vector3.right * (float)(xHandle - 1);
                        Vector3 outwardsDir2 = rotation * Vector3.up * (float)(yHandle - 1);
                        float   num          = RectHandles.RotationSlider(controlId, rectPointInWorld, eulerAngles.z, pivot, rotation * Vector3.forward, outwardsDir1, outwardsDir2, handleSize, (Handles.DrawCapFunction)null, Vector2.zero);
                        if (EditorGUI.EndChangeCheck())
                        {
                            if (Event.current.shift)
                            {
                                num = Mathf.Round((float)(((double)num - (double)eulerAngles.z) / 15.0)) * 15f + eulerAngles.z;
                            }
                            eulerAngles.z = num;
                            rotation      = Quaternion.Euler(eulerAngles);
                        }
                    }
                    yHandle += 2;
                }
                xHandle += 2;
            }
            return(rotation);
        }
Beispiel #6
0
        private static Quaternion RotationHandlesGUI(Rect rect, Vector3 pivot, Quaternion rotation)
        {
            Vector3 eulerAngles = rotation.eulerAngles;

            for (int i = 0; i <= 2; i += 2)
            {
                for (int j = 0; j <= 2; j += 2)
                {
                    Vector3 rectPointInWorld = RectTool.GetRectPointInWorld(rect, pivot, rotation, i, j);
                    float   handleSize       = 0.05f * HandleUtility.GetHandleSize(rectPointInWorld);
                    int     controlID        = GUIUtility.GetControlID(RectTool.s_RotationHandlesHash, FocusType.Passive);
                    if (GUI.color.a > 0f || GUIUtility.hotControl == controlID)
                    {
                        EditorGUI.BeginChangeCheck();
                        Vector3 outwardsDir  = rotation * Vector3.right * (float)(i - 1);
                        Vector3 outwardsDir2 = rotation * Vector3.up * (float)(j - 1);
                        float   num          = RectHandles.RotationSlider(controlID, rectPointInWorld, eulerAngles.z, pivot, rotation * Vector3.forward, outwardsDir, outwardsDir2, handleSize, null, Vector2.zero);
                        if (EditorGUI.EndChangeCheck())
                        {
                            if (Event.current.shift)
                            {
                                num = Mathf.Round((num - eulerAngles.z) / 15f) * 15f + eulerAngles.z;
                            }
                            eulerAngles.z = num;
                            rotation      = Quaternion.Euler(eulerAngles);
                        }
                    }
                }
            }
            return(rotation);
        }
Beispiel #7
0
 public static void PivotCap(int controlID, Vector3 position, Quaternion rotation, float size)
 {
     if (RectHandles.s_Styles == null)
     {
         RectHandles.s_Styles = new RectHandles.Styles();
     }
     RectHandles.DrawImageBasedCap(controlID, position, rotation, size, RectHandles.s_Styles.pivotdot, RectHandles.s_Styles.pivotdotactive);
 }
Beispiel #8
0
        public static void RenderRectWithShadow(bool active, params Vector3[] corners)
        {
            Vector3[] vector3Array = new Vector3[5] {
                corners[0], corners[1], corners[2], corners[3], corners[0]
            };
            Color color = Handles.color;

            Handles.color = new Color(1f, 1f, 1f, !active ? 0.5f : 1f);
            RectHandles.DrawPolyLineWithShadow(new Color(0.0f, 0.0f, 0.0f, !active ? 0.5f : 1f), new Vector2(1f, -1f), vector3Array);
            Handles.color = color;
        }
Beispiel #9
0
        private static void HandleDirectionalCursor(Vector3 handlePosition, Vector3 handlePlaneNormal, Vector3 direction)
        {
            Vector2 mousePosition = Event.current.mousePosition;
            Plane   plane         = new Plane(handlePlaneNormal, handlePosition);
            Vector3 worldPos;

            if (RectHandles.RaycastGUIPointToWorldHit(mousePosition, plane, out worldPos))
            {
                Vector2 direction2 = RectHandles.WorldToScreenSpaceDir(worldPos, direction);
                Rect    position   = new Rect(mousePosition.x - 100f, mousePosition.y - 100f, 200f, 200f);
                EditorGUIUtility.AddCursorRect(position, RectHandles.GetScaleCursor(direction2));
            }
        }
Beispiel #10
0
        private static void HandleDirectionalCursor(Vector3 handlePosition, Vector3 handlePlaneNormal, Vector3 direction)
        {
            Vector2 mousePosition = Event.current.mousePosition;
            Plane   plane         = new Plane(handlePlaneNormal, handlePosition);
            Vector3 hit;

            if (!RectHandles.RaycastGUIPointToWorldHit(mousePosition, plane, out hit))
            {
                return;
            }
            Vector2 screenSpaceDir = RectHandles.WorldToScreenSpaceDir(hit, direction);

            EditorGUIUtility.AddCursorRect(new Rect(mousePosition.x - 100f, mousePosition.y - 100f, 200f, 200f), RectHandles.GetScaleCursor(screenSpaceDir));
        }
Beispiel #11
0
        public static void RenderRectWithShadow(bool active, params Vector3[] corners)
        {
            Vector3[] points = new Vector3[]
            {
                corners[0],
                corners[1],
                corners[2],
                corners[3],
                corners[0]
            };
            Color color = Handles.color;

            Handles.color = new Color(1f, 1f, 1f, (!active) ? 0.5f : 1f);
            RectHandles.DrawPolyLineWithShadow(new Color(0f, 0f, 0f, (!active) ? 0.5f : 1f), new Vector2(1f, -1f), points);
            Handles.color = color;
        }
Beispiel #12
0
 public static void PivotHandleCap(int controlID, Vector3 position, Quaternion rotation, float size, EventType eventType)
 {
     if (RectHandles.s_Styles == null)
     {
         RectHandles.s_Styles = new RectHandles.Styles();
     }
     if (eventType != EventType.Layout)
     {
         if (eventType == EventType.Repaint)
         {
             RectHandles.DrawImageBasedCap(controlID, position, rotation, size, RectHandles.s_Styles.pivotdot, RectHandles.s_Styles.pivotdotactive);
         }
     }
     else
     {
         HandleUtility.AddControl(controlID, HandleUtility.DistanceToCircle(position, size * 0.5f));
     }
 }
Beispiel #13
0
        public static float RotationSlider(int id, Vector3 cornerPos, float rotation, Vector3 pivot, Vector3 handleDir, Vector3 outwardsDir1, Vector3 outwardsDir2, float handleSize, Handles.DrawCapFunction drawFunc, Vector2 snap)
        {
            Vector3 b      = outwardsDir1 + outwardsDir2;
            Vector2 vector = HandleUtility.WorldToGUIPoint(cornerPos);
            Vector2 b2     = (HandleUtility.WorldToGUIPoint(cornerPos + b) - vector).normalized * 15f;

            RectHandles.RaycastGUIPointToWorldHit(vector + b2, new Plane(handleDir, cornerPos), out cornerPos);
            Event   current = Event.current;
            Vector3 a       = Handles.Slider2D(id, cornerPos, handleDir, outwardsDir1, outwardsDir2, handleSize, drawFunc, Vector2.zero);

            if (current.type == EventType.MouseMove)
            {
                RectHandles.DetectCursorChange(id);
            }
            if (current.type == EventType.Repaint && ((HandleUtility.nearestControl == id && GUIUtility.hotControl == 0) || GUIUtility.hotControl == id))
            {
                Rect position = new Rect(current.mousePosition.x - 100f, current.mousePosition.y - 100f, 200f, 200f);
                EditorGUIUtility.AddCursorRect(position, MouseCursor.RotateArrow);
            }
            return(rotation - RectHandles.AngleAroundAxis(a - pivot, cornerPos - pivot, handleDir));
        }
Beispiel #14
0
        internal static Vector3 CornerSlider(int id, Vector3 cornerPos, Vector3 handleDir, Vector3 outwardsDir1, Vector3 outwardsDir2, float handleSize, Handles.DrawCapFunction drawFunc, Vector2 snap)
        {
            Event   current = Event.current;
            Vector3 vector3 = Handles.Slider2D(id, cornerPos, handleDir, outwardsDir1, outwardsDir2, handleSize, drawFunc, snap);

            switch (current.type)
            {
            case EventType.MouseMove:
                RectHandles.DetectCursorChange(id);
                break;

            case EventType.Repaint:
                if (HandleUtility.nearestControl == id && GUIUtility.hotControl == 0 || GUIUtility.hotControl == id)
                {
                    RectHandles.HandleDirectionalCursor(cornerPos, handleDir, outwardsDir1 + outwardsDir2);
                    break;
                }
                break;
            }
            return(vector3);
        }
Beispiel #15
0
        internal static Vector3 CornerSlider(int id, Vector3 cornerPos, Vector3 handleDir, Vector3 outwardsDir1, Vector3 outwardsDir2, float handleSize, Handles.DrawCapFunction drawFunc, Vector2 snap)
        {
            Event     current = Event.current;
            Vector3   result  = Handles.Slider2D(id, cornerPos, handleDir, outwardsDir1, outwardsDir2, handleSize, drawFunc, snap);
            EventType type    = current.type;

            if (type != EventType.MouseMove)
            {
                if (type == EventType.Repaint)
                {
                    if ((HandleUtility.nearestControl == id && GUIUtility.hotControl == 0) || GUIUtility.hotControl == id)
                    {
                        RectHandles.HandleDirectionalCursor(cornerPos, handleDir, outwardsDir1 + outwardsDir2);
                    }
                }
            }
            else
            {
                RectHandles.DetectCursorChange(id);
            }
            return(result);
        }
        protected override void ToolGUI(SceneView view, Vector3 handlePosition, bool isStatic)
        {
            Rect       rect         = Tools.handleRect;
            Quaternion rectRotation = Tools.handleRectRotation;

            // Draw rect
            Vector3[] verts = new Vector3[4];
            for (int i = 0; i < 4; i++)
            {
                Vector3 pos = GetLocalRectPoint(rect, i);
                verts[i] = rectRotation * pos + handlePosition;
            }
            RectHandles.RenderRectWithShadow(false, verts);

            // Handle fading
            Color oldColor = GUI.color;
            float faded    = 1;

            if (Camera.current)
            {
                Vector3 viewDir = Camera.current.orthographic ?
                                  Camera.current.transform.forward :
                                  (handlePosition + rectRotation * rect.center - Camera.current.transform.position);
                Vector3 rectRight   = rectRotation * Vector3.right * rect.width;
                Vector3 rectUp      = rectRotation * Vector3.up * rect.height;
                float   visibleSize = Mathf.Sqrt(Vector3.Cross(Vector3.ProjectOnPlane(rectRight, viewDir), Vector3.ProjectOnPlane(rectUp, viewDir)).magnitude);
                visibleSize /= HandleUtility.GetHandleSize(handlePosition);
                faded        = Mathf.Clamp01((visibleSize - kMinVisibleSize) / kMinVisibleSize * 2);
                Color fadedColor = oldColor;
                fadedColor.a *= faded;
                GUI.color     = fadedColor;
            }

            Vector3 oldPivot = Tools.cachedHandlePosition;

            // Pivot handle
            if (!Tools.vertexDragging)
            {
                RectTransform rectTransform      = Selection.activeTransform.GetComponent <RectTransform>();
                bool          groupPivot         = Selection.transforms.Length > 1;
                bool          rectTransformPivot = !groupPivot && Tools.pivotMode == PivotMode.Pivot && rectTransform != null;
                using (new EditorGUI.DisabledScope(!groupPivot && !rectTransformPivot))
                {
                    EditorGUI.BeginChangeCheck();
                    Vector3 newPivot = PivotHandleGUI(rect, oldPivot, rectRotation);
                    if (EditorGUI.EndChangeCheck() && !isStatic)
                    {
                        if (groupPivot)
                        {
                            Tools.localHandleOffset += Quaternion.Inverse(Tools.handleRotation) * (newPivot - oldPivot);
                        }
                        else if (rectTransformPivot)
                        {
                            Transform tr = Selection.activeTransform;
                            Undo.RecordObject(rectTransform, "Move Rectangle Pivot");
                            Transform space  = Tools.rectBlueprintMode && UnityEditorInternal.InternalEditorUtility.SupportsRectLayout(tr) ? tr.parent : tr;
                            Vector2   offset = space.InverseTransformVector(newPivot - oldPivot);
                            offset.x /= rectTransform.rect.width;
                            offset.y /= rectTransform.rect.height;
                            Vector2 pivot = rectTransform.pivot + offset;

                            RectTransformEditor.SetPivotSmart(rectTransform, pivot.x, 0, true, space != rectTransform.transform);
                            RectTransformEditor.SetPivotSmart(rectTransform, pivot.y, 1, true, space != rectTransform.transform);
                        }
                    }
                }
            }

            TransformManipulator.BeginManipulationHandling(true);
            if (!Tools.vertexDragging)
            {
                // Resize handles
                EditorGUI.BeginChangeCheck();
                Vector3 scalePivot = handlePosition;
                Vector3 scale      = ResizeHandlesGUI(rect, handlePosition, rectRotation, out scalePivot);
                if (EditorGUI.EndChangeCheck() && !isStatic)
                {
                    TransformManipulator.SetResizeDelta(scale, scalePivot, rectRotation);
                }

                bool enableRotation = true;
                if (Tools.rectBlueprintMode)
                {
                    foreach (Transform t in Selection.transforms)
                    {
                        if (t.GetComponent <RectTransform>() != null)
                        {
                            enableRotation = false;
                        }
                    }
                }

                if (enableRotation)
                {
                    // Rotation handles
                    EditorGUI.BeginChangeCheck();
                    Quaternion after = RotationHandlesGUI(rect, handlePosition, rectRotation);
                    if (EditorGUI.EndChangeCheck() && !isStatic)
                    {
                        Quaternion delta = Quaternion.Inverse(rectRotation) * after;
                        float      angle;
                        Vector3    axis;
                        delta.ToAngleAxis(out angle, out axis);
                        axis = rectRotation * axis;

                        Undo.RecordObjects(Selection.transforms, "Rotate");
                        foreach (Transform t in Selection.transforms)
                        {
                            t.RotateAround(handlePosition, axis, angle);

                            // sync euler hints after a rotate tool update tyo fake continuous rotation
                            t.SetLocalEulerHint(t.GetLocalEulerAngles(t.rotationOrder));

                            if (t.parent != null)
                            {
                                t.SendTransformChangedScale(); // force scale update, needed if transform has non-uniformly scaled parent.
                            }
                        }
                        Tools.handleRotation = Quaternion.AngleAxis(angle, axis) * Tools.handleRotation;
                    }
                }
            }
            TransformManipulator.EndManipulationHandling();

            TransformManipulator.BeginManipulationHandling(false);
            // Move handle
            EditorGUI.BeginChangeCheck();
            Vector3 newPos = MoveHandlesGUI(rect, handlePosition, rectRotation);

            if (EditorGUI.EndChangeCheck() && !isStatic)
            {
                if (GridSnapping.active)
                {
                    newPos = GridSnapping.Snap(newPos);
                }

                if (TransformManipulator.HandleHasMoved(newPos))
                {
                    TransformManipulator.SetPositionDelta(newPos, TransformManipulator.mouseDownHandlePosition);
                }
            }
            TransformManipulator.EndManipulationHandling();

            GUI.color = oldColor;
        }
Beispiel #17
0
        public override void ToolGUI(SceneView view, Vector3 handlePosition, bool isStatic)
        {
            Rect       handleRect         = Tools.handleRect;
            Quaternion handleRectRotation = Tools.handleRectRotation;

            Vector3[] vector3Array = new Vector3[4];
            for (int index = 0; index < 4; ++index)
            {
                Vector3 localRectPoint = (Vector3)RectTool.GetLocalRectPoint(handleRect, index);
                vector3Array[index] = handleRectRotation * localRectPoint + handlePosition;
            }
            RectHandles.RenderRectWithShadow(false, vector3Array);
            Color color1 = GUI.color;

            if ((bool)((Object)Camera.current))
            {
                Vector3 planeNormal = !Camera.current.orthographic ? handlePosition + handleRectRotation * (Vector3)handleRect.center - Camera.current.transform.position : Camera.current.transform.forward;
                Vector3 vector1     = handleRectRotation * Vector3.right * handleRect.width;
                Vector3 vector2     = handleRectRotation * Vector3.up * handleRect.height;
                float   num         = Mathf.Clamp01((float)(((double)(Mathf.Sqrt(Vector3.Cross(Vector3.ProjectOnPlane(vector1, planeNormal), Vector3.ProjectOnPlane(vector2, planeNormal)).magnitude) / HandleUtility.GetHandleSize(handlePosition)) - 0.200000002980232) / 0.200000002980232 * 2.0));
                Color   color2      = color1;
                color2.a *= num;
                GUI.color = color2;
            }
            Vector3 handlePosition1 = Tools.GetHandlePosition();

            if (!Tools.vertexDragging)
            {
                RectTransform component = Selection.activeTransform.GetComponent <RectTransform>();
                bool          flag1     = Selection.transforms.Length > 1;
                bool          flag2     = !flag1 && Tools.pivotMode == PivotMode.Pivot && (Object)component != (Object)null;
                EditorGUI.BeginDisabledGroup(!flag1 && !flag2);
                EditorGUI.BeginChangeCheck();
                Vector3 vector3 = RectTool.PivotHandleGUI(handleRect, handlePosition1, handleRectRotation);
                if (EditorGUI.EndChangeCheck() && !isStatic)
                {
                    if (flag1)
                    {
                        Tools.localHandleOffset += Quaternion.Inverse(Tools.handleRotation) * (vector3 - handlePosition1);
                    }
                    else if (flag2)
                    {
                        Transform activeTransform = Selection.activeTransform;
                        Undo.RecordObject((Object)component, "Move Rectangle Pivot");
                        Transform transform = !Tools.rectBlueprintMode || !InternalEditorUtility.SupportsRectLayout(activeTransform) ? activeTransform : activeTransform.parent;
                        Vector2   vector2_1 = (Vector2)transform.InverseTransformVector(vector3 - handlePosition1);
                        vector2_1.x /= component.rect.width;
                        vector2_1.y /= component.rect.height;
                        Vector2 vector2_2 = component.pivot + vector2_1;
                        RectTransformEditor.SetPivotSmart(component, vector2_2.x, 0, true, (Object)transform != (Object)component.transform);
                        RectTransformEditor.SetPivotSmart(component, vector2_2.y, 1, true, (Object)transform != (Object)component.transform);
                    }
                }
                EditorGUI.EndDisabledGroup();
            }
            TransformManipulator.BeginManipulationHandling(true);
            if (!Tools.vertexDragging)
            {
                EditorGUI.BeginChangeCheck();
                Vector3 scalePivot = handlePosition;
                Vector3 scaleDelta = RectTool.ResizeHandlesGUI(handleRect, handlePosition, handleRectRotation, out scalePivot);
                if (EditorGUI.EndChangeCheck() && !isStatic)
                {
                    TransformManipulator.SetResizeDelta(scaleDelta, scalePivot, handleRectRotation);
                }
                bool flag = true;
                if (Tools.rectBlueprintMode)
                {
                    foreach (Component transform in Selection.transforms)
                    {
                        if ((Object)transform.GetComponent <RectTransform>() != (Object)null)
                        {
                            flag = false;
                        }
                    }
                }
                if (flag)
                {
                    EditorGUI.BeginChangeCheck();
                    Quaternion quaternion = RectTool.RotationHandlesGUI(handleRect, handlePosition, handleRectRotation);
                    if (EditorGUI.EndChangeCheck() && !isStatic)
                    {
                        float   angle;
                        Vector3 axis1;
                        (Quaternion.Inverse(handleRectRotation) * quaternion).ToAngleAxis(out angle, out axis1);
                        Vector3 axis2 = handleRectRotation * axis1;
                        Undo.RecordObjects((Object[])Selection.transforms, "Rotate");
                        foreach (Transform transform in Selection.transforms)
                        {
                            transform.RotateAround(handlePosition, axis2, angle);
                            if ((Object)transform.parent != (Object)null)
                            {
                                transform.SendTransformChangedScale();
                            }
                        }
                        Tools.handleRotation = Quaternion.AngleAxis(angle, axis2) * Tools.handleRotation;
                    }
                }
            }
            int num1 = (int)TransformManipulator.EndManipulationHandling();

            TransformManipulator.BeginManipulationHandling(false);
            EditorGUI.BeginChangeCheck();
            Vector3 vector3_1 = RectTool.MoveHandlesGUI(handleRect, handlePosition, handleRectRotation);

            if (EditorGUI.EndChangeCheck() && !isStatic)
            {
                TransformManipulator.SetPositionDelta(vector3_1 - TransformManipulator.mouseDownHandlePosition);
            }
            int num2 = (int)TransformManipulator.EndManipulationHandling();

            GUI.color = color1;
        }
Beispiel #18
0
        private static Vector3 ResizeHandlesGUI(Rect rect, Vector3 pivot, Quaternion rotation, out Vector3 scalePivot)
        {
            if (Event.current.type == EventType.MouseDown)
            {
                s_StartRect = rect;
            }
            scalePivot = pivot;
            Vector3    one        = Vector3.one;
            Quaternion quaternion = Quaternion.Inverse(rotation);

            for (int i = 0; i <= 2; i++)
            {
                for (int j = 0; j <= 2; j++)
                {
                    if ((i != 1) || (j != 1))
                    {
                        Vector3 vector2   = GetRectPointInWorld(s_StartRect, pivot, rotation, i, j);
                        Vector3 position  = GetRectPointInWorld(rect, pivot, rotation, i, j);
                        float   size      = 0.05f * HandleUtility.GetHandleSize(position);
                        int     controlID = GUIUtility.GetControlID(s_ResizeHandlesHash, FocusType.Passive);
                        if ((GUI.color.a > 0f) || (GUIUtility.hotControl == controlID))
                        {
                            Vector3 vector4;
                            EditorGUI.BeginChangeCheck();
                            EventType typeBefore = Event.current.type;
                            if ((i == 1) || (j == 1))
                            {
                                Vector3 sideVector = (i != 1) ? ((Vector3)((rotation * Vector3.up) * rect.height)) : ((Vector3)((rotation * Vector3.right) * rect.width));
                                Vector3 direction  = (i != 1) ? ((Vector3)(rotation * Vector3.right)) : ((Vector3)(rotation * Vector3.up));
                                vector4 = RectHandles.SideSlider(controlID, position, sideVector, direction, size, null, 0f);
                            }
                            else
                            {
                                Vector3 vector7 = (Vector3)((rotation * Vector3.right) * (i - 1));
                                Vector3 vector8 = (Vector3)((rotation * Vector3.up) * (j - 1));
                                vector4 = RectHandles.CornerSlider(controlID, position, (Vector3)(rotation * Vector3.forward), vector7, vector8, size, new Handles.DrawCapFunction(RectHandles.RectScalingCap), Vector2.zero);
                            }
                            bool flag = ((Selection.transforms.Length == 1) && InternalEditorUtility.SupportsRectLayout(Selection.activeTransform)) && (Selection.activeTransform.parent.rotation == rotation);
                            if (flag)
                            {
                                Transform     activeTransform = Selection.activeTransform;
                                RectTransform component       = activeTransform.GetComponent <RectTransform>();
                                Transform     parent          = activeTransform.parent;
                                RectTransform parentRect      = parent.GetComponent <RectTransform>();
                                if ((typeBefore == EventType.MouseDown) && (Event.current.type != EventType.MouseDown))
                                {
                                    RectTransformSnapping.CalculateOffsetSnapValues(parent, activeTransform, parentRect, component, i, j);
                                }
                            }
                            if (EditorGUI.EndChangeCheck())
                            {
                                ManipulationToolUtility.SetMinDragDifferenceForPos(position);
                                if (flag)
                                {
                                    Transform     transform5   = Selection.activeTransform.parent;
                                    RectTransform transform6   = transform5.GetComponent <RectTransform>();
                                    Vector2       snapDistance = (Vector2)((Vector2.one * HandleUtility.GetHandleSize(vector4)) * 0.05f);
                                    Vector3       vector14     = (Vector3)(quaternion * transform5.TransformVector(Vector3.right));
                                    snapDistance.x /= vector14.x;
                                    Vector3 vector15 = (Vector3)(quaternion * transform5.TransformVector(Vector3.up));
                                    snapDistance.y /= vector15.y;
                                    Vector3 vector10 = transform5.InverseTransformPoint(vector4) - transform6.rect.min;
                                    Vector3 positionAfterSnapping = (Vector3)(RectTransformSnapping.SnapToGuides(vector10, snapDistance) + (Vector3.forward * vector10.z));
                                    ManipulationToolUtility.DisableMinDragDifferenceBasedOnSnapping(vector10, positionAfterSnapping);
                                    vector4 = transform5.TransformPoint(positionAfterSnapping + transform6.rect.min);
                                }
                                bool alt       = Event.current.alt;
                                bool actionKey = EditorGUI.actionKey;
                                bool flag4     = Event.current.shift && !actionKey;
                                if (!alt)
                                {
                                    scalePivot = GetRectPointInWorld(s_StartRect, pivot, rotation, 2 - i, 2 - j);
                                }
                                if (flag4)
                                {
                                    vector4 = Vector3.Project(vector4 - scalePivot, vector2 - scalePivot) + scalePivot;
                                }
                                Vector3 vector12 = (Vector3)(quaternion * (vector2 - scalePivot));
                                Vector3 vector13 = (Vector3)(quaternion * (vector4 - scalePivot));
                                if (i != 1)
                                {
                                    one.x = vector13.x / vector12.x;
                                }
                                if (j != 1)
                                {
                                    one.y = vector13.y / vector12.y;
                                }
                                if (flag4)
                                {
                                    float num5 = (i != 1) ? one.x : one.y;
                                    one = (Vector3)(Vector3.one * num5);
                                }
                                if (actionKey && (i == 1))
                                {
                                    if (Event.current.shift)
                                    {
                                        one.x = one.z = 1f / Mathf.Sqrt(Mathf.Max(one.y, 0.0001f));
                                    }
                                    else
                                    {
                                        one.x = 1f / Mathf.Max(one.y, 0.0001f);
                                    }
                                }
                                if (flag4)
                                {
                                    float num6 = (i != 1) ? one.x : one.y;
                                    one = (Vector3)(Vector3.one * num6);
                                }
                                if (actionKey && (i == 1))
                                {
                                    if (Event.current.shift)
                                    {
                                        one.x = one.z = 1f / Mathf.Sqrt(Mathf.Max(one.y, 0.0001f));
                                    }
                                    else
                                    {
                                        one.x = 1f / Mathf.Max(one.y, 0.0001f);
                                    }
                                }
                                if (actionKey && (j == 1))
                                {
                                    if (Event.current.shift)
                                    {
                                        one.y = one.z = 1f / Mathf.Sqrt(Mathf.Max(one.x, 0.0001f));
                                    }
                                    else
                                    {
                                        one.y = 1f / Mathf.Max(one.x, 0.0001f);
                                    }
                                }
                            }
                            switch (i)
                            {
                            case 0:
                                ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingLeft", typeBefore);
                                break;

                            case 2:
                                ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingRight", typeBefore);
                                break;
                            }
                            if (i != 1)
                            {
                                ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingWidth", typeBefore);
                            }
                            if (j == 0)
                            {
                                ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingBottom", typeBefore);
                            }
                            if (j == 2)
                            {
                                ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingTop", typeBefore);
                            }
                            if (j != 1)
                            {
                                ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingHeight", typeBefore);
                            }
                        }
                    }
                }
            }
            return(one);
        }
Beispiel #19
0
 internal static Vector3 SideSlider(int id, Vector3 position, Vector3 sideVector, Vector3 direction, float size, Handles.DrawCapFunction drawFunc, float snap)
 {
     return(RectHandles.SideSlider(id, position, sideVector, direction, size, drawFunc, snap, 0f));
 }
Beispiel #20
0
        public override void ToolGUI(SceneView view, Vector3 handlePosition, bool isStatic)
        {
            Rect       handleRect         = Tools.handleRect;
            Quaternion handleRectRotation = Tools.handleRectRotation;

            Vector3[] corners = new Vector3[4];
            for (int i = 0; i < 4; i++)
            {
                Vector3 localRectPoint = (Vector3)GetLocalRectPoint(handleRect, i);
                corners[i] = ((Vector3)(handleRectRotation * localRectPoint)) + handlePosition;
            }
            RectHandles.RenderRectWithShadow(false, corners);
            Color color = GUI.color;
            float num2  = 1f;

            if (Camera.current != null)
            {
                Vector3 planeNormal = !Camera.current.orthographic ? ((handlePosition + (handleRectRotation * handleRect.center)) - Camera.current.transform.position) : Camera.current.transform.forward;
                Vector3 vector      = (Vector3)((handleRectRotation * Vector3.right) * handleRect.width);
                Vector3 vector4     = (Vector3)((handleRectRotation * Vector3.up) * handleRect.height);
                float   num3        = Mathf.Sqrt(Vector3.Cross(Vector3.ProjectOnPlane(vector, planeNormal), Vector3.ProjectOnPlane(vector4, planeNormal)).magnitude) / HandleUtility.GetHandleSize(handlePosition);
                num2 = Mathf.Clamp01(((num3 - 0.2f) / 0.2f) * 2f);
                Color color2 = color;
                color2.a *= num2;
                GUI.color = color2;
            }
            Vector3 pivot = Tools.GetHandlePosition();

            if (!Tools.vertexDragging)
            {
                RectTransform component = Selection.activeTransform.GetComponent <RectTransform>();
                bool          flag      = Selection.transforms.Length > 1;
                bool          flag2     = (!flag && (Tools.pivotMode == PivotMode.Pivot)) && (component != null);
                EditorGUI.BeginDisabledGroup(!flag && !flag2);
                EditorGUI.BeginChangeCheck();
                Vector3 vector6 = PivotHandleGUI(handleRect, pivot, handleRectRotation);
                if (EditorGUI.EndChangeCheck() && !isStatic)
                {
                    if (flag)
                    {
                        Tools.localHandleOffset += Quaternion.Inverse(Tools.handleRotation) * (vector6 - pivot);
                    }
                    else if (flag2)
                    {
                        Transform activeTransform = Selection.activeTransform;
                        Undo.RecordObject(component, "Move Rectangle Pivot");
                        Transform transform3 = (!Tools.rectBlueprintMode || !InternalEditorUtility.SupportsRectLayout(activeTransform)) ? activeTransform : activeTransform.parent;
                        Vector2   vector7    = transform3.InverseTransformVector(vector6 - pivot);
                        vector7.x /= component.rect.width;
                        vector7.y /= component.rect.height;
                        Vector2 vector8 = component.pivot + vector7;
                        RectTransformEditor.SetPivotSmart(component, vector8.x, 0, true, transform3 != component.transform);
                        RectTransformEditor.SetPivotSmart(component, vector8.y, 1, true, transform3 != component.transform);
                    }
                }
                EditorGUI.EndDisabledGroup();
            }
            TransformManipulator.BeginManipulationHandling(true);
            if (!Tools.vertexDragging)
            {
                EditorGUI.BeginChangeCheck();
                Vector3 scalePivot = handlePosition;
                Vector3 scaleDelta = ResizeHandlesGUI(handleRect, handlePosition, handleRectRotation, out scalePivot);
                if (EditorGUI.EndChangeCheck() && !isStatic)
                {
                    TransformManipulator.SetResizeDelta(scaleDelta, scalePivot, handleRectRotation);
                }
                bool flag3 = true;
                if (Tools.rectBlueprintMode)
                {
                    foreach (Transform transform4 in Selection.transforms)
                    {
                        if (transform4.GetComponent <RectTransform>() != null)
                        {
                            flag3 = false;
                        }
                    }
                }
                if (flag3)
                {
                    EditorGUI.BeginChangeCheck();
                    Quaternion quaternion2 = RotationHandlesGUI(handleRect, handlePosition, handleRectRotation);
                    if (EditorGUI.EndChangeCheck() && !isStatic)
                    {
                        float   num5;
                        Vector3 vector11;
                        (Quaternion.Inverse(handleRectRotation) * quaternion2).ToAngleAxis(out num5, out vector11);
                        vector11 = (Vector3)(handleRectRotation * vector11);
                        Undo.RecordObjects(Selection.transforms, "Rotate");
                        foreach (Transform transform5 in Selection.transforms)
                        {
                            transform5.RotateAround(handlePosition, vector11, num5);
                            if (transform5.parent != null)
                            {
                                transform5.SendTransformChangedScale();
                            }
                        }
                        Tools.handleRotation = Quaternion.AngleAxis(num5, vector11) * Tools.handleRotation;
                    }
                }
            }
            TransformManipulator.EndManipulationHandling();
            TransformManipulator.BeginManipulationHandling(false);
            EditorGUI.BeginChangeCheck();
            Vector3 vector12 = MoveHandlesGUI(handleRect, handlePosition, handleRectRotation);

            if (EditorGUI.EndChangeCheck() && !isStatic)
            {
                Vector3 positionDelta = vector12 - TransformManipulator.mouseDownHandlePosition;
                TransformManipulator.SetPositionDelta(positionDelta);
            }
            TransformManipulator.EndManipulationHandling();
            GUI.color = color;
        }
Beispiel #21
0
        private static Vector3 MoveHandlesGUI(Rect rect, Vector3 pivot, Quaternion rotation)
        {
            bool    flag2;
            int     controlID = GUIUtility.GetControlID(s_MoveHandleHash, FocusType.Passive);
            Vector3 position  = pivot;
            float   radius    = HandleUtility.GetHandleSize(pivot) * 0.2f;
            float   num3      = 1f - GUI.color.a;

            Vector3[] worldPoints = new Vector3[] { (rotation * new Vector2(rect.x, rect.y)) + pivot, (rotation * new Vector2(rect.xMax, rect.y)) + pivot, (rotation * new Vector2(rect.xMax, rect.yMax)) + pivot, (rotation * new Vector2(rect.x, rect.yMax)) + pivot };
            VertexSnapping.HandleKeyAndMouseMove(controlID);
            bool      flag           = ((Selection.transforms.Length == 1) && InternalEditorUtility.SupportsRectLayout(Selection.activeTransform)) && (Selection.activeTransform.parent.rotation == rotation);
            Event     current        = Event.current;
            EventType typeForControl = current.GetTypeForControl(controlID);
            Plane     plane          = new Plane(worldPoints[0], worldPoints[1], worldPoints[2]);

            switch (typeForControl)
            {
            case EventType.MouseDown:
                flag2 = false;
                if (!Tools.vertexDragging)
                {
                    flag2 = (((current.button == 0) && (current.modifiers == EventModifiers.None)) && RectHandles.RaycastGUIPointToWorldHit(current.mousePosition, plane, out s_StartMouseWorldPos)) && ((SceneViewDistanceToRectangle(worldPoints, current.mousePosition) == 0f) || ((num3 > 0f) && (SceneViewDistanceToDisc(pivot, (Vector3)(rotation * Vector3.forward), radius, current.mousePosition) == 0f)));
                    break;
                }
                flag2 = true;
                break;

            case EventType.MouseUp:
                if (GUIUtility.hotControl == controlID)
                {
                    if (!s_Moving)
                    {
                        Selection.activeGameObject = SceneViewPicking.PickGameObject(current.mousePosition);
                    }
                    GUIUtility.hotControl = 0;
                    EditorGUIUtility.SetWantsMouseJumping(0);
                    HandleUtility.ignoreRaySnapObjects = null;
                    current.Use();
                }
                goto Label_0620;

            case EventType.MouseDrag:
                if (GUIUtility.hotControl == controlID)
                {
                    s_CurrentMousePos += current.delta;
                    if (!s_Moving)
                    {
                        Vector2 vector2 = s_CurrentMousePos - s_StartMousePos;
                        if (vector2.magnitude > 3f)
                        {
                            s_Moving = true;
                            RectHandles.RaycastGUIPointToWorldHit(s_CurrentMousePos, plane, out s_StartMouseWorldPos);
                        }
                    }
                    if (s_Moving)
                    {
                        if (!Tools.vertexDragging)
                        {
                            Vector3 vector4;
                            ManipulationToolUtility.SetMinDragDifferenceForPos(pivot);
                            if (RectHandles.RaycastGUIPointToWorldHit(s_CurrentMousePos, plane, out vector4))
                            {
                                Vector3 vector = vector4 - s_StartMouseWorldPos;
                                if (current.shift)
                                {
                                    vector = (Vector3)(Quaternion.Inverse(rotation) * vector);
                                    if (s_LockAxis == -1)
                                    {
                                        float introduced28 = Mathf.Abs(vector.x);
                                        s_LockAxis = (introduced28 <= Mathf.Abs(vector.y)) ? 1 : 0;
                                    }
                                    vector[1 - s_LockAxis] = 0f;
                                    vector = (Vector3)(rotation * vector);
                                }
                                else
                                {
                                    s_LockAxis = -1;
                                }
                                if (flag)
                                {
                                    Transform parent  = Selection.activeTransform.parent;
                                    Vector3   vector6 = s_StartRectPosition + parent.InverseTransformVector(vector);
                                    vector6.z = 0f;
                                    Quaternion quaternion   = Quaternion.Inverse(rotation);
                                    Vector2    snapDistance = (Vector2)((Vector2.one * HandleUtility.GetHandleSize(position)) * 0.05f);
                                    Vector3    vector8      = (Vector3)(quaternion * parent.TransformVector(Vector3.right));
                                    snapDistance.x /= vector8.x;
                                    Vector3 vector9 = (Vector3)(quaternion * parent.TransformVector(Vector3.up));
                                    snapDistance.y /= vector9.y;
                                    Vector3 positionAfterSnapping = (Vector3)RectTransformSnapping.SnapToGuides(vector6, snapDistance);
                                    ManipulationToolUtility.DisableMinDragDifferenceBasedOnSnapping(vector6, positionAfterSnapping);
                                    vector = parent.TransformVector(positionAfterSnapping - s_StartRectPosition);
                                }
                                position    = s_StartPosition + vector;
                                GUI.changed = true;
                            }
                        }
                        else
                        {
                            Vector3 vector3;
                            if (HandleUtility.ignoreRaySnapObjects == null)
                            {
                                Handles.SetupIgnoreRaySnapObjects();
                            }
                            if (HandleUtility.FindNearestVertex(s_CurrentMousePos, null, out vector3))
                            {
                                position    = vector3;
                                GUI.changed = true;
                            }
                            ManipulationToolUtility.minDragDifference = (Vector3)Vector2.zero;
                        }
                    }
                    current.Use();
                }
                goto Label_0620;

            case EventType.Repaint:
                if (!Tools.vertexDragging)
                {
                    Handles.color = Handles.secondaryColor * new Color(1f, 1f, 1f, 1.5f * num3);
                    Handles.CircleCap(controlID, pivot, rotation, radius);
                    Handles.color = Handles.secondaryColor * new Color(1f, 1f, 1f, 0.3f * num3);
                    Handles.DrawSolidDisc(pivot, (Vector3)(rotation * Vector3.forward), radius);
                }
                else
                {
                    RectHandles.RectScalingHandleCap(controlID, pivot, rotation, 1f, EventType.Repaint);
                }
                goto Label_0620;

            default:
                goto Label_0620;
            }
            if (flag2)
            {
                s_StartPosition = pivot;
                s_StartMousePos = s_CurrentMousePos = current.mousePosition;
                s_Moving        = false;
                s_LockAxis      = -1;
                int num4 = controlID;
                GUIUtility.keyboardControl = num4;
                GUIUtility.hotControl      = num4;
                EditorGUIUtility.SetWantsMouseJumping(1);
                HandleUtility.ignoreRaySnapObjects = null;
                current.Use();
                if (flag)
                {
                    Transform     activeTransform = Selection.activeTransform;
                    RectTransform component       = activeTransform.GetComponent <RectTransform>();
                    Transform     parentSpace     = activeTransform.parent;
                    RectTransform parentRect      = parentSpace.GetComponent <RectTransform>();
                    s_StartRectPosition = (Vector3)component.anchoredPosition;
                    RectTransformSnapping.CalculatePositionSnapValues(parentSpace, activeTransform, parentRect, component);
                }
            }
Label_0620:
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingPosX", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingLeft", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingRight", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingPosY", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingTop", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingBottom", typeForControl);
            return(position);
        }
Beispiel #22
0
        private static Vector3 MoveHandlesGUI(Rect rect, Vector3 pivot, Quaternion rotation)
        {
            int     controlId = GUIUtility.GetControlID(RectTool.s_MoveHandleHash, FocusType.Passive);
            Vector3 position  = pivot;
            float   num1      = HandleUtility.GetHandleSize(pivot) * 0.2f;
            float   num2      = 1f - GUI.color.a;

            Vector3[] worldPoints = new Vector3[4] {
                rotation *(Vector3) new Vector2(rect.x, rect.y) + pivot, rotation *(Vector3) new Vector2(rect.xMax, rect.y) + pivot, rotation *(Vector3) new Vector2(rect.xMax, rect.yMax) + pivot, rotation *(Vector3) new Vector2(rect.x, rect.yMax) + pivot
            };
            VertexSnapping.HandleKeyAndMouseMove(controlId);
            bool      flag           = Selection.transforms.Length == 1 && InternalEditorUtility.SupportsRectLayout(Selection.activeTransform) && Selection.activeTransform.parent.rotation == rotation;
            Event     current        = Event.current;
            EventType typeForControl = current.GetTypeForControl(controlId);
            Plane     plane          = new Plane(worldPoints[0], worldPoints[1], worldPoints[2]);

            switch (typeForControl)
            {
            case EventType.MouseDown:
                if (Tools.vertexDragging || current.button == 0 && current.modifiers == EventModifiers.None && RectHandles.RaycastGUIPointToWorldHit(current.mousePosition, plane, out RectTool.s_StartMouseWorldPos) && ((double)RectTool.SceneViewDistanceToRectangle(worldPoints, current.mousePosition) == 0.0 || (double)num2 > 0.0 && (double)RectTool.SceneViewDistanceToDisc(pivot, rotation * Vector3.forward, num1, current.mousePosition) == 0.0))
                {
                    RectTool.s_StartPosition = pivot;
                    RectTool.s_StartMousePos = RectTool.s_CurrentMousePos = current.mousePosition;
                    RectTool.s_Moving        = false;
                    RectTool.s_LockAxis      = -1;
                    int num3 = controlId;
                    GUIUtility.keyboardControl = num3;
                    GUIUtility.hotControl      = num3;
                    EditorGUIUtility.SetWantsMouseJumping(1);
                    HandleUtility.ignoreRaySnapObjects = (Transform[])null;
                    current.Use();
                    if (flag)
                    {
                        Transform     activeTransform = Selection.activeTransform;
                        RectTransform component1      = activeTransform.GetComponent <RectTransform>();
                        Transform     parent          = activeTransform.parent;
                        RectTransform component2      = parent.GetComponent <RectTransform>();
                        RectTool.s_StartRectPosition = (Vector3)component1.anchoredPosition;
                        RectTransformSnapping.CalculatePositionSnapValues(parent, activeTransform, component2, component1);
                        break;
                    }
                    break;
                }
                break;

            case EventType.MouseUp:
                if (GUIUtility.hotControl == controlId)
                {
                    if (!RectTool.s_Moving)
                    {
                        Selection.activeGameObject = SceneViewPicking.PickGameObject(current.mousePosition);
                    }
                    GUIUtility.hotControl = 0;
                    EditorGUIUtility.SetWantsMouseJumping(0);
                    HandleUtility.ignoreRaySnapObjects = (Transform[])null;
                    current.Use();
                    break;
                }
                break;

            case EventType.MouseDrag:
                if (GUIUtility.hotControl == controlId)
                {
                    RectTool.s_CurrentMousePos += current.delta;
                    if (!RectTool.s_Moving && (double)(RectTool.s_CurrentMousePos - RectTool.s_StartMousePos).magnitude > 3.0)
                    {
                        RectTool.s_Moving = true;
                        RectHandles.RaycastGUIPointToWorldHit(RectTool.s_CurrentMousePos, plane, out RectTool.s_StartMouseWorldPos);
                    }
                    if (RectTool.s_Moving)
                    {
                        if (Tools.vertexDragging)
                        {
                            if (HandleUtility.ignoreRaySnapObjects == null)
                            {
                                Handles.SetupIgnoreRaySnapObjects();
                            }
                            Vector3 vertex;
                            if (HandleUtility.FindNearestVertex(RectTool.s_CurrentMousePos, (Transform[])null, out vertex))
                            {
                                position    = vertex;
                                GUI.changed = true;
                            }
                            ManipulationToolUtility.minDragDifference = (Vector3)Vector2.zero;
                        }
                        else
                        {
                            ManipulationToolUtility.SetMinDragDifferenceForPos(pivot);
                            Vector3 hit;
                            if (RectHandles.RaycastGUIPointToWorldHit(RectTool.s_CurrentMousePos, plane, out hit))
                            {
                                Vector3 vector = hit - RectTool.s_StartMouseWorldPos;
                                if (current.shift)
                                {
                                    vector = Quaternion.Inverse(rotation) * vector;
                                    if (RectTool.s_LockAxis == -1)
                                    {
                                        RectTool.s_LockAxis = (double)Mathf.Abs(vector.x) <= (double)Mathf.Abs(vector.y) ? 1 : 0;
                                    }
                                    vector[1 - RectTool.s_LockAxis] = 0.0f;
                                    vector = rotation * vector;
                                }
                                else
                                {
                                    RectTool.s_LockAxis = -1;
                                }
                                if (flag)
                                {
                                    Transform parent = Selection.activeTransform.parent;
                                    Vector3   positionBeforeSnapping = RectTool.s_StartRectPosition + parent.InverseTransformVector(vector);
                                    positionBeforeSnapping.z = 0.0f;
                                    Quaternion quaternion   = Quaternion.Inverse(rotation);
                                    Vector2    snapDistance = Vector2.one * HandleUtility.GetHandleSize(position) * 0.05f;
                                    snapDistance.x /= (quaternion * parent.TransformVector(Vector3.right)).x;
                                    snapDistance.y /= (quaternion * parent.TransformVector(Vector3.up)).y;
                                    Vector3 guides = (Vector3)RectTransformSnapping.SnapToGuides((Vector2)positionBeforeSnapping, snapDistance);
                                    ManipulationToolUtility.DisableMinDragDifferenceBasedOnSnapping(positionBeforeSnapping, guides);
                                    vector = parent.TransformVector(guides - RectTool.s_StartRectPosition);
                                }
                                position    = RectTool.s_StartPosition + vector;
                                GUI.changed = true;
                            }
                        }
                    }
                    current.Use();
                    break;
                }
                break;

            case EventType.Repaint:
                if (Tools.vertexDragging)
                {
                    RectHandles.RectScalingCap(controlId, pivot, rotation, 1f);
                    break;
                }
                Handles.color = Handles.secondaryColor * new Color(1f, 1f, 1f, 1.5f * num2);
                Handles.CircleCap(controlId, pivot, rotation, num1);
                Handles.color = Handles.secondaryColor * new Color(1f, 1f, 1f, 0.3f * num2);
                Handles.DrawSolidDisc(pivot, rotation * Vector3.forward, num1);
                break;
            }
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingPosX", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingLeft", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingRight", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingPosY", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingTop", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingBottom", typeForControl);
            return(position);
        }
        static Vector3 ResizeHandlesGUI(Rect rect, Vector3 pivot, Quaternion rotation, out Vector3 scalePivot)
        {
            if (Event.current.type == EventType.MouseDown)
            {
                s_StartRect = rect;
            }

            scalePivot = pivot;
            Vector3 scale = Vector3.one;

            Quaternion inverseRotation = Quaternion.Inverse(rotation);

            // Loop through the 8 handles (sides and corners) using a nested loop.
            // (The loop covers 9 combinations, but the center position is ignored.)
            for (int xHandle = 0; xHandle <= 2; xHandle++)
            {
                for (int yHandle = 0; yHandle <= 2; yHandle++)
                {
                    // Ignore center
                    if (xHandle == 1 && yHandle == 1)
                    {
                        continue;
                    }

                    Vector3 origPos = GetRectPointInWorld(s_StartRect, pivot, rotation, xHandle, yHandle);
                    Vector3 curPos  = GetRectPointInWorld(rect, pivot, rotation, xHandle, yHandle);

                    float size = 0.05f * HandleUtility.GetHandleSize(curPos);
                    int   id   = GUIUtility.GetControlID(s_ResizeHandlesHash, FocusType.Passive);
                    if (GUI.color.a > 0 || GUIUtility.hotControl == id)
                    {
                        EditorGUI.BeginChangeCheck();
                        Vector3 newPos;

                        EventType typeBefore = Event.current.type;

                        if (xHandle == 1 || yHandle == 1)
                        {
                            // Side resizer (1D)
                            Vector3 sideDir  = (xHandle == 1 ? rotation * Vector3.right * rect.width : rotation * Vector3.up * rect.height);
                            Vector3 slideDir = (xHandle == 1 ? rotation * Vector3.up : rotation * Vector3.right);
                            newPos = RectHandles.SideSlider(id, curPos, sideDir, slideDir, size, null, EditorSnapSettings.move);
                        }
                        else
                        {
                            // Corner handle (2D)
                            Vector3 outwardsA = rotation * Vector3.right * (xHandle - 1);
                            Vector3 outwardsB = rotation * Vector3.up * (yHandle - 1);
                            newPos = RectHandles.CornerSlider(id, curPos, rotation * Vector3.forward, outwardsA, outwardsB, size, RectHandles.RectScalingHandleCap, EditorSnapSettings.move);
                        }

                        // Calculate snapping values if applicable
                        bool supportsRectSnapping = Selection.transforms.Length == 1 &&
                                                    UnityEditorInternal.InternalEditorUtility.SupportsRectLayout(Selection.activeTransform) &&
                                                    Selection.activeTransform.parent.rotation == rotation;
                        if (supportsRectSnapping)
                        {
                            Transform     transform           = Selection.activeTransform;
                            RectTransform rectTransform       = transform.GetComponent <RectTransform>();
                            Transform     transformParent     = transform.parent;
                            RectTransform rectTransformParent = transformParent.GetComponent <RectTransform>();
                            if (typeBefore == EventType.MouseDown && Event.current.type != EventType.MouseDown)
                            {
                                RectTransformSnapping.CalculateOffsetSnapValues(transformParent, transform, rectTransformParent, rectTransform, xHandle, yHandle);
                            }
                        }

                        if (EditorGUI.EndChangeCheck())
                        {
                            // Resize handles require more fine grained rounding of values than other tools.
                            // With other tools, the slight rounding is not notizable as long as it's just sub-pixel,
                            // because the manipulated object is being moved at the same time.
                            // However, with resize handles, when dragging one edge or corner,
                            // the opposite is standing still, and even slight rounding can cause shaking/vibration.
                            // At a fraction of the normal rounding, the shaking is very unlikely to happen though.
                            ManipulationToolUtility.SetMinDragDifferenceForPos(curPos, 0.1f);

                            if (supportsRectSnapping)
                            {
                                Transform     transformParent = Selection.activeTransform.parent;
                                RectTransform rectParent      = transformParent.GetComponent <RectTransform>();

                                Vector2 snapSize = Vector2.one * HandleUtility.GetHandleSize(newPos) * RectTransformSnapping.kSnapThreshold;
                                snapSize.x /= (inverseRotation * transformParent.TransformVector(Vector3.right)).x;
                                snapSize.y /= (inverseRotation * transformParent.TransformVector(Vector3.up)).y;

                                Vector3 newPosInParent        = transformParent.InverseTransformPoint(newPos) - (Vector3)rectParent.rect.min;
                                Vector3 newPosInParentSnapped = (Vector3)RectTransformSnapping.SnapToGuides(newPosInParent, snapSize) + Vector3.forward * newPosInParent.z;
                                ManipulationToolUtility.DisableMinDragDifferenceBasedOnSnapping(newPosInParent, newPosInParentSnapped);
                                newPos = transformParent.TransformPoint(newPosInParentSnapped + (Vector3)rectParent.rect.min);
                            }

                            bool scaleFromPivot = Event.current.alt;
                            bool uniformScaling = Event.current.shift;

                            if (!scaleFromPivot)
                            {
                                scalePivot = GetRectPointInWorld(s_StartRect, pivot, rotation, 2 - xHandle, 2 - yHandle);
                            }

                            if (uniformScaling)
                            {
                                newPos = Vector3.Project(newPos - scalePivot, origPos - scalePivot) + scalePivot;
                            }

                            Vector3 sizeBefore = inverseRotation * (origPos - scalePivot);
                            Vector3 sizeAfter  = inverseRotation * (newPos - scalePivot);
                            if (xHandle != 1)
                            {
                                scale.x = sizeAfter.x / sizeBefore.x;
                            }
                            if (yHandle != 1)
                            {
                                scale.y = sizeAfter.y / sizeBefore.y;
                            }

                            if (uniformScaling)
                            {
                                float refScale = (xHandle == 1 ? scale.y : scale.x);
                                scale = Vector3.one * refScale;
                            }

                            if (uniformScaling)
                            {
                                float refScale = (xHandle == 1 ? scale.y : scale.x);
                                scale = Vector3.one * refScale;
                            }
                        }

                        if (xHandle == 0)
                        {
                            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp(kChangingLeft, typeBefore);
                        }
                        if (xHandle == 2)
                        {
                            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp(kChangingRight, typeBefore);
                        }
                        if (xHandle != 1)
                        {
                            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp(kChangingWidth, typeBefore);
                        }
                        if (yHandle == 0)
                        {
                            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp(kChangingBottom, typeBefore);
                        }
                        if (yHandle == 2)
                        {
                            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp(kChangingTop, typeBefore);
                        }
                        if (yHandle != 1)
                        {
                            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp(kChangingHeight, typeBefore);
                        }
                    }
                }
            }

            return(scale);
        }
Beispiel #24
0
        public override void ToolGUI(SceneView view, Vector3 handlePosition, bool isStatic)
        {
            Rect       handleRect         = Tools.handleRect;
            Quaternion handleRectRotation = Tools.handleRectRotation;

            Vector3[] array = new Vector3[4];
            for (int i = 0; i < 4; i++)
            {
                Vector3 point = RectTool.GetLocalRectPoint(handleRect, i);
                array[i] = handleRectRotation * point + handlePosition;
            }
            RectHandles.RenderRectWithShadow(false, array);
            Color color = GUI.color;

            if (Camera.current)
            {
                Vector3 planeNormal = (!Camera.current.orthographic) ? (handlePosition + handleRectRotation * handleRect.center - Camera.current.transform.position) : Camera.current.transform.forward;
                Vector3 vector      = handleRectRotation * Vector3.right * handleRect.width;
                Vector3 vector2     = handleRectRotation * Vector3.up * handleRect.height;
                float   num         = Mathf.Sqrt(Vector3.Cross(Vector3.ProjectOnPlane(vector, planeNormal), Vector3.ProjectOnPlane(vector2, planeNormal)).magnitude);
                num /= HandleUtility.GetHandleSize(handlePosition);
                float num2   = Mathf.Clamp01((num - 0.2f) / 0.2f * 2f);
                Color color2 = color;
                color2.a *= num2;
                GUI.color = color2;
            }
            Vector3 handlePosition2 = Tools.GetHandlePosition();

            if (!Tools.vertexDragging)
            {
                RectTransform component = Selection.activeTransform.GetComponent <RectTransform>();
                bool          flag      = Selection.transforms.Length > 1;
                bool          flag2     = !flag && Tools.pivotMode == PivotMode.Pivot && component != null;
                using (new EditorGUI.DisabledScope(!flag && !flag2))
                {
                    EditorGUI.BeginChangeCheck();
                    Vector3 a = RectTool.PivotHandleGUI(handleRect, handlePosition2, handleRectRotation);
                    if (EditorGUI.EndChangeCheck() && !isStatic)
                    {
                        if (flag)
                        {
                            Tools.localHandleOffset += Quaternion.Inverse(Tools.handleRotation) * (a - handlePosition2);
                        }
                        else if (flag2)
                        {
                            Transform activeTransform = Selection.activeTransform;
                            Undo.RecordObject(component, "Move Rectangle Pivot");
                            Transform transform = (!Tools.rectBlueprintMode || !InternalEditorUtility.SupportsRectLayout(activeTransform)) ? activeTransform : activeTransform.parent;
                            Vector2   b         = transform.InverseTransformVector(a - handlePosition2);
                            b.x /= component.rect.width;
                            b.y /= component.rect.height;
                            Vector2 vector3 = component.pivot + b;
                            RectTransformEditor.SetPivotSmart(component, vector3.x, 0, true, transform != component.transform);
                            RectTransformEditor.SetPivotSmart(component, vector3.y, 1, true, transform != component.transform);
                        }
                    }
                }
            }
            TransformManipulator.BeginManipulationHandling(true);
            if (!Tools.vertexDragging)
            {
                EditorGUI.BeginChangeCheck();
                Vector3 pivotPosition = handlePosition;
                Vector3 scaleDelta    = RectTool.ResizeHandlesGUI(handleRect, handlePosition, handleRectRotation, out pivotPosition);
                if (EditorGUI.EndChangeCheck() && !isStatic)
                {
                    TransformManipulator.SetResizeDelta(scaleDelta, pivotPosition, handleRectRotation);
                }
                bool flag3 = true;
                if (Tools.rectBlueprintMode)
                {
                    Transform[] transforms = Selection.transforms;
                    for (int j = 0; j < transforms.Length; j++)
                    {
                        Transform transform2 = transforms[j];
                        if (transform2.GetComponent <RectTransform>() != null)
                        {
                            flag3 = false;
                        }
                    }
                }
                if (flag3)
                {
                    EditorGUI.BeginChangeCheck();
                    Quaternion rhs = RectTool.RotationHandlesGUI(handleRect, handlePosition, handleRectRotation);
                    if (EditorGUI.EndChangeCheck() && !isStatic)
                    {
                        float   angle;
                        Vector3 vector4;
                        (Quaternion.Inverse(handleRectRotation) * rhs).ToAngleAxis(out angle, out vector4);
                        vector4 = handleRectRotation * vector4;
                        Undo.RecordObjects(Selection.transforms, "Rotate");
                        Transform[] transforms2 = Selection.transforms;
                        for (int k = 0; k < transforms2.Length; k++)
                        {
                            Transform transform3 = transforms2[k];
                            transform3.RotateAround(handlePosition, vector4, angle);
                            transform3.SetLocalEulerHint(transform3.GetLocalEulerAngles(transform3.rotationOrder));
                            if (transform3.parent != null)
                            {
                                transform3.SendTransformChangedScale();
                            }
                        }
                        Tools.handleRotation = Quaternion.AngleAxis(angle, vector4) * Tools.handleRotation;
                    }
                }
            }
            TransformManipulator.EndManipulationHandling();
            TransformManipulator.BeginManipulationHandling(false);
            EditorGUI.BeginChangeCheck();
            Vector3 vector5 = RectTool.MoveHandlesGUI(handleRect, handlePosition, handleRectRotation);

            if (EditorGUI.EndChangeCheck() && !isStatic)
            {
                if (GridSnapping.active)
                {
                    vector5 = GridSnapping.Snap(vector5);
                }
                if (TransformManipulator.HandleHasMoved(vector5))
                {
                    TransformManipulator.SetPositionDelta(vector5, TransformManipulator.mouseDownHandlePosition);
                }
            }
            TransformManipulator.EndManipulationHandling();
            GUI.color = color;
        }
Beispiel #25
0
        private static Vector3 ResizeHandlesGUI(Rect rect, Vector3 pivot, Quaternion rotation, out Vector3 scalePivot)
        {
            if (Event.current.type == EventType.MouseDown)
            {
                RectTool.s_StartRect = rect;
            }
            scalePivot = pivot;
            Vector3    result    = Vector3.one;
            Quaternion rotation2 = Quaternion.Inverse(rotation);

            for (int i = 0; i <= 2; i++)
            {
                for (int j = 0; j <= 2; j++)
                {
                    if (i != 1 || j != 1)
                    {
                        Vector3 rectPointInWorld  = RectTool.GetRectPointInWorld(RectTool.s_StartRect, pivot, rotation, i, j);
                        Vector3 rectPointInWorld2 = RectTool.GetRectPointInWorld(rect, pivot, rotation, i, j);
                        float   num       = 0.05f * HandleUtility.GetHandleSize(rectPointInWorld2);
                        int     controlID = GUIUtility.GetControlID(RectTool.s_ResizeHandlesHash, FocusType.Passive);
                        if (GUI.color.a > 0f || GUIUtility.hotControl == controlID)
                        {
                            EditorGUI.BeginChangeCheck();
                            EventType type = Event.current.type;
                            Vector3   vector;
                            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);
                                vector = RectHandles.SideSlider(controlID, rectPointInWorld2, sideVector, direction, num, null, 0f);
                            }
                            else
                            {
                                Vector3 vector2   = rotation * Vector3.right * (float)(i - 1);
                                Vector3 vector3   = rotation * Vector3.up * (float)(j - 1);
                                int     arg_1AB_0 = controlID;
                                Vector3 arg_1AB_1 = rectPointInWorld2;
                                Vector3 arg_1AB_2 = rotation * Vector3.forward;
                                Vector3 arg_1AB_3 = vector2;
                                Vector3 arg_1AB_4 = vector3;
                                float   arg_1AB_5 = num;
                                if (RectTool.< > f__mg$cache0 == null)
                                {
                                    RectTool.< > f__mg$cache0 = new Handles.CapFunction(RectHandles.RectScalingHandleCap);
                                }
                                vector = RectHandles.CornerSlider(arg_1AB_0, arg_1AB_1, arg_1AB_2, arg_1AB_3, arg_1AB_4, arg_1AB_5, RectTool.< > f__mg$cache0, Vector2.zero);
                            }
                            bool flag = Selection.transforms.Length == 1 && InternalEditorUtility.SupportsRectLayout(Selection.activeTransform) && Selection.activeTransform.parent.rotation == rotation;
                            if (flag)
                            {
                                Transform     activeTransform = Selection.activeTransform;
                                RectTransform component       = activeTransform.GetComponent <RectTransform>();
                                Transform     parent          = activeTransform.parent;
                                RectTransform component2      = parent.GetComponent <RectTransform>();
                                if (type == EventType.MouseDown && Event.current.type != EventType.MouseDown)
                                {
                                    RectTransformSnapping.CalculateOffsetSnapValues(parent, activeTransform, component2, component, i, j);
                                }
                            }
                            if (EditorGUI.EndChangeCheck())
                            {
                                ManipulationToolUtility.SetMinDragDifferenceForPos(rectPointInWorld2, 0.1f);
                                if (flag)
                                {
                                    Transform     parent2      = Selection.activeTransform.parent;
                                    RectTransform component3   = parent2.GetComponent <RectTransform>();
                                    Vector2       snapDistance = Vector2.one * HandleUtility.GetHandleSize(vector) * 0.05f;
                                    snapDistance.x /= (rotation2 * parent2.TransformVector(Vector3.right)).x;
                                    snapDistance.y /= (rotation2 * parent2.TransformVector(Vector3.up)).y;
                                    Vector3 vector4 = parent2.InverseTransformPoint(vector) - component3.rect.min;
                                    Vector3 vector5 = RectTransformSnapping.SnapToGuides(vector4, snapDistance) + Vector3.forward * vector4.z;
                                    ManipulationToolUtility.DisableMinDragDifferenceBasedOnSnapping(vector4, vector5);
                                    vector = parent2.TransformPoint(vector5 + component3.rect.min);
                                }
                                bool alt       = Event.current.alt;
                                bool actionKey = EditorGUI.actionKey;
                                bool flag2     = Event.current.shift && !actionKey;
                                if (!alt)
                                {
                                    scalePivot = RectTool.GetRectPointInWorld(RectTool.s_StartRect, pivot, rotation, 2 - i, 2 - j);
                                }
                                if (flag2)
                                {
                                    vector = Vector3.Project(vector - scalePivot, rectPointInWorld - scalePivot) + scalePivot;
                                }
                                Vector3 vector6 = rotation2 * (rectPointInWorld - scalePivot);
                                Vector3 vector7 = rotation2 * (vector - scalePivot);
                                if (i != 1)
                                {
                                    result.x = vector7.x / vector6.x;
                                }
                                if (j != 1)
                                {
                                    result.y = vector7.y / vector6.y;
                                }
                                if (flag2)
                                {
                                    float d = (i != 1) ? result.x : result.y;
                                    result = Vector3.one * d;
                                }
                                if (actionKey && i == 1)
                                {
                                    if (Event.current.shift)
                                    {
                                        result.x = (result.z = 1f / Mathf.Sqrt(Mathf.Max(result.y, 0.0001f)));
                                    }
                                    else
                                    {
                                        result.x = 1f / Mathf.Max(result.y, 0.0001f);
                                    }
                                }
                                if (flag2)
                                {
                                    float d2 = (i != 1) ? result.x : result.y;
                                    result = Vector3.one * d2;
                                }
                                if (actionKey && i == 1)
                                {
                                    if (Event.current.shift)
                                    {
                                        result.x = (result.z = 1f / Mathf.Sqrt(Mathf.Max(result.y, 0.0001f)));
                                    }
                                    else
                                    {
                                        result.x = 1f / Mathf.Max(result.y, 0.0001f);
                                    }
                                }
                                if (actionKey && j == 1)
                                {
                                    if (Event.current.shift)
                                    {
                                        result.y = (result.z = 1f / Mathf.Sqrt(Mathf.Max(result.x, 0.0001f)));
                                    }
                                    else
                                    {
                                        result.y = 1f / Mathf.Max(result.x, 0.0001f);
                                    }
                                }
                            }
                            if (i == 0)
                            {
                                ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingLeft", type);
                            }
                            if (i == 2)
                            {
                                ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingRight", type);
                            }
                            if (i != 1)
                            {
                                ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingWidth", type);
                            }
                            if (j == 0)
                            {
                                ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingBottom", type);
                            }
                            if (j == 2)
                            {
                                ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingTop", type);
                            }
                            if (j != 1)
                            {
                                ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingHeight", type);
                            }
                        }
                    }
                }
            }
            return(result);
        }
Beispiel #26
0
        private static Vector3 MoveHandlesGUI(Rect rect, Vector3 pivot, Quaternion rotation)
        {
            int     controlID = GUIUtility.GetControlID(RectTool.s_MoveHandleHash, FocusType.Passive);
            Vector3 vector    = pivot;
            float   num       = HandleUtility.GetHandleSize(pivot) * 0.2f;
            float   num2      = 1f - GUI.color.a;

            Vector3[] array = new Vector3[]
            {
                rotation *new Vector2(rect.x, rect.y) + pivot,
                rotation *new Vector2(rect.xMax, rect.y) + pivot,
                rotation *new Vector2(rect.xMax, rect.yMax) + pivot,
                rotation *new Vector2(rect.x, rect.yMax) + pivot
            };
            VertexSnapping.HandleKeyAndMouseMove(controlID);
            bool      flag           = Selection.transforms.Length == 1 && InternalEditorUtility.SupportsRectLayout(Selection.activeTransform) && Selection.activeTransform.parent.rotation == rotation;
            Event     current        = Event.current;
            EventType typeForControl = current.GetTypeForControl(controlID);
            Plane     plane          = new Plane(array[0], array[1], array[2]);

            switch (typeForControl)
            {
            case EventType.MouseDown:
            {
                bool flag2 = Tools.vertexDragging || (current.button == 0 && current.modifiers == EventModifiers.None && RectHandles.RaycastGUIPointToWorldHit(current.mousePosition, plane, out RectTool.s_StartMouseWorldPos) && (RectTool.SceneViewDistanceToRectangle(array, current.mousePosition) == 0f || (num2 > 0f && RectTool.SceneViewDistanceToDisc(pivot, rotation * Vector3.forward, num, current.mousePosition) == 0f)));
                if (flag2)
                {
                    RectTool.s_StartPosition = pivot;
                    RectTool.s_StartMousePos = (RectTool.s_CurrentMousePos = current.mousePosition);
                    RectTool.s_Moving        = false;
                    RectTool.s_LockAxis      = -1;
                    int num3 = controlID;
                    GUIUtility.keyboardControl = num3;
                    GUIUtility.hotControl      = num3;
                    EditorGUIUtility.SetWantsMouseJumping(1);
                    HandleUtility.ignoreRaySnapObjects = null;
                    current.Use();
                    if (flag)
                    {
                        Transform     activeTransform = Selection.activeTransform;
                        RectTransform component       = activeTransform.GetComponent <RectTransform>();
                        Transform     parent          = activeTransform.parent;
                        RectTransform component2      = parent.GetComponent <RectTransform>();
                        RectTool.s_StartRectPosition = component.anchoredPosition;
                        RectTransformSnapping.CalculatePositionSnapValues(parent, activeTransform, component2, component);
                    }
                }
                break;
            }

            case EventType.MouseUp:
                if (GUIUtility.hotControl == controlID)
                {
                    if (!RectTool.s_Moving)
                    {
                        Selection.activeGameObject = SceneViewPicking.PickGameObject(current.mousePosition);
                    }
                    GUIUtility.hotControl = 0;
                    EditorGUIUtility.SetWantsMouseJumping(0);
                    HandleUtility.ignoreRaySnapObjects = null;
                    current.Use();
                }
                break;

            case EventType.MouseDrag:
                if (GUIUtility.hotControl == controlID)
                {
                    RectTool.s_CurrentMousePos += current.delta;
                    if (!RectTool.s_Moving && (RectTool.s_CurrentMousePos - RectTool.s_StartMousePos).magnitude > 3f)
                    {
                        RectTool.s_Moving = true;
                        RectHandles.RaycastGUIPointToWorldHit(RectTool.s_CurrentMousePos, plane, out RectTool.s_StartMouseWorldPos);
                    }
                    if (RectTool.s_Moving)
                    {
                        if (Tools.vertexDragging)
                        {
                            if (HandleUtility.ignoreRaySnapObjects == null)
                            {
                                Handles.SetupIgnoreRaySnapObjects();
                            }
                            Vector3 vector2;
                            if (HandleUtility.FindNearestVertex(RectTool.s_CurrentMousePos, null, out vector2))
                            {
                                vector      = vector2;
                                GUI.changed = true;
                            }
                            ManipulationToolUtility.minDragDifference = Vector2.zero;
                        }
                        else
                        {
                            ManipulationToolUtility.SetMinDragDifferenceForPos(pivot);
                            Vector3 a;
                            if (RectHandles.RaycastGUIPointToWorldHit(RectTool.s_CurrentMousePos, plane, out a))
                            {
                                Vector3 vector3 = a - RectTool.s_StartMouseWorldPos;
                                if (current.shift)
                                {
                                    vector3 = Quaternion.Inverse(rotation) * vector3;
                                    if (RectTool.s_LockAxis == -1)
                                    {
                                        RectTool.s_LockAxis = ((Mathf.Abs(vector3.x) <= Mathf.Abs(vector3.y)) ? 1 : 0);
                                    }
                                    vector3[1 - RectTool.s_LockAxis] = 0f;
                                    vector3 = rotation * vector3;
                                }
                                else
                                {
                                    RectTool.s_LockAxis = -1;
                                }
                                if (flag)
                                {
                                    Transform parent2 = Selection.activeTransform.parent;
                                    Vector3   vector4 = RectTool.s_StartRectPosition + parent2.InverseTransformVector(vector3);
                                    vector4.z = 0f;
                                    Quaternion rotation2    = Quaternion.Inverse(rotation);
                                    Vector2    snapDistance = Vector2.one * HandleUtility.GetHandleSize(vector) * 0.05f;
                                    snapDistance.x /= (rotation2 * parent2.TransformVector(Vector3.right)).x;
                                    snapDistance.y /= (rotation2 * parent2.TransformVector(Vector3.up)).y;
                                    Vector3 vector5 = RectTransformSnapping.SnapToGuides(vector4, snapDistance);
                                    ManipulationToolUtility.DisableMinDragDifferenceBasedOnSnapping(vector4, vector5);
                                    vector3 = parent2.TransformVector(vector5 - RectTool.s_StartRectPosition);
                                }
                                vector      = RectTool.s_StartPosition + vector3;
                                GUI.changed = true;
                            }
                        }
                    }
                    current.Use();
                }
                break;

            case EventType.Repaint:
                if (Tools.vertexDragging)
                {
                    RectHandles.RectScalingHandleCap(controlID, pivot, rotation, 1f, EventType.Repaint);
                }
                else
                {
                    Handles.color = Handles.secondaryColor * new Color(1f, 1f, 1f, 1.5f * num2);
                    Handles.CircleHandleCap(controlID, pivot, rotation, num, EventType.Repaint);
                    Handles.color = Handles.secondaryColor * new Color(1f, 1f, 1f, 0.3f * num2);
                    Handles.DrawSolidDisc(pivot, rotation * Vector3.forward, num);
                }
                break;
            }
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingPosX", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingLeft", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingRight", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingPosY", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingTop", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingBottom", typeForControl);
            return(vector);
        }
        static Vector3 MoveHandlesGUI(Rect rect, Vector3 pivot, Quaternion rotation)
        {
            int id = GUIUtility.GetControlID(s_MoveHandleHash, FocusType.Passive);

            Vector3 newPos      = pivot;
            float   discSize    = HandleUtility.GetHandleSize(pivot) * 0.2f;
            float   discOpacity = (1 - GUI.color.a);

            Vector3[] corners = new Vector3[4];
            corners[0] = rotation * new Vector2(rect.x, rect.y) + pivot;
            corners[1] = rotation * new Vector2(rect.xMax, rect.y) + pivot;
            corners[2] = rotation * new Vector2(rect.xMax, rect.yMax) + pivot;
            corners[3] = rotation * new Vector2(rect.x, rect.yMax) + pivot;

            VertexSnapping.HandleMouseMove(id);

            bool supportsRectSnapping = Selection.transforms.Length == 1 &&
                                        UnityEditorInternal.InternalEditorUtility.SupportsRectLayout(Selection.activeTransform) &&
                                        Selection.activeTransform.parent.rotation == rotation;

            Event     evt       = Event.current;
            EventType eventType = evt.GetTypeForControl(id);
            Plane     guiPlane  = new Plane(corners[0], corners[1], corners[2]);

            switch (eventType)
            {
            case EventType.MouseDown:
            {
                bool acceptClick = false;

                if (Tools.vertexDragging)
                {
                    acceptClick = true;
                }
                else
                {
                    acceptClick =
                        evt.button == 0 &&
                        evt.modifiers == 0 &&
                        RectHandles.RaycastGUIPointToWorldHit(evt.mousePosition, guiPlane, out s_StartMouseWorldPos) &&
                        (
                            SceneViewDistanceToRectangle(corners, evt.mousePosition) == 0f ||
                            (discOpacity > 0 && SceneViewDistanceToDisc(pivot, rotation * Vector3.forward, discSize, evt.mousePosition) == 0f)
                        );
                }

                if (acceptClick)
                {
                    s_StartPosition       = pivot;
                    s_StartMousePos       = s_CurrentMousePos = evt.mousePosition;
                    s_Moving              = false;
                    s_LockAxis            = -1;
                    GUIUtility.hotControl = GUIUtility.keyboardControl = id;
                    EditorGUIUtility.SetWantsMouseJumping(1);
                    HandleUtility.ignoreRaySnapObjects = null;
                    evt.Use();

                    // Calculate snapping values if applicable
                    if (supportsRectSnapping)
                    {
                        Transform     transform           = Selection.activeTransform;
                        RectTransform rectTransform       = transform.GetComponent <RectTransform>();
                        Transform     transformParent     = transform.parent;
                        RectTransform rectTransformParent = transformParent.GetComponent <RectTransform>();

                        s_StartRectPosition = rectTransform.anchoredPosition;

                        RectTransformSnapping.CalculatePositionSnapValues(transformParent, transform, rectTransformParent, rectTransform);
                    }
                }
                break;
            }

            case EventType.MouseDrag:
            {
                if (GUIUtility.hotControl == id)
                {
                    s_CurrentMousePos += evt.delta;
                    if (!s_Moving && (s_CurrentMousePos - s_StartMousePos).magnitude > 3f)
                    {
                        s_Moving = true;
                        // Re-raycast to get start mouse pos when effective dragging starts.
                        // This prevents a sudden unsnap when the dragging is enabled.
                        RectHandles.RaycastGUIPointToWorldHit(s_CurrentMousePos, guiPlane, out s_StartMouseWorldPos);
                    }
                    if (s_Moving)
                    {
                        if (Tools.vertexDragging)
                        {
                            if (HandleUtility.ignoreRaySnapObjects == null)
                            {
                                Handles.SetupIgnoreRaySnapObjects();
                            }
                            Vector3 near;
                            if (HandleUtility.FindNearestVertex(s_CurrentMousePos, null, out near))
                            {
                                // Snap position based on found near vertex
                                newPos      = near;
                                GUI.changed = true;
                            }
                            ManipulationToolUtility.minDragDifference = Vector2.zero;
                        }
                        else
                        {
                            ManipulationToolUtility.SetMinDragDifferenceForPos(pivot);
                            Vector3 pos;
                            if (RectHandles.RaycastGUIPointToWorldHit(s_CurrentMousePos, guiPlane, out pos))
                            {
                                Vector3 offset = pos - s_StartMouseWorldPos;

                                // Snap to axis
                                if (evt.shift)
                                {
                                    // Get offset in rect handles space
                                    offset = Quaternion.Inverse(rotation) * offset;
                                    // Determine lock axis if not already set
                                    if (s_LockAxis == -1)
                                    {
                                        s_LockAxis = Mathf.Abs(offset.x) > Mathf.Abs(offset.y) ? 0 : 1;
                                    }
                                    // Cancel mocement on other axis
                                    offset[1 - s_LockAxis] = 0;
                                    // Put offset back in world space
                                    offset = rotation * offset;
                                }
                                else
                                {
                                    s_LockAxis = -1;
                                }

                                if (supportsRectSnapping)
                                {
                                    Transform transformParent = Selection.activeTransform.parent;
                                    Vector3   rectPosition    = s_StartRectPosition + transformParent.InverseTransformVector(offset);
                                    rectPosition.z = 0;

                                    Quaternion inverseRotation = Quaternion.Inverse(rotation);
                                    Vector2    snapSize        = Vector2.one * HandleUtility.GetHandleSize(newPos) * RectTransformSnapping.kSnapThreshold;
                                    snapSize.x /= (inverseRotation * transformParent.TransformVector(Vector3.right)).x;
                                    snapSize.y /= (inverseRotation * transformParent.TransformVector(Vector3.up)).y;

                                    Vector3 newRectPosition = RectTransformSnapping.SnapToGuides(rectPosition, snapSize);
                                    ManipulationToolUtility.DisableMinDragDifferenceBasedOnSnapping(rectPosition, newRectPosition);
                                    offset = transformParent.TransformVector(newRectPosition - s_StartRectPosition);
                                }

                                newPos = s_StartPosition + offset;

                                GUI.changed = true;
                            }
                        }
                    }
                    evt.Use();
                }
                break;
            }

            case EventType.MouseUp:
            {
                if (GUIUtility.hotControl == id)
                {
                    if (!s_Moving)
                    {
                        Selection.activeGameObject = SceneViewPicking.PickGameObject(evt.mousePosition);
                    }
                    GUIUtility.hotControl = 0;
                    EditorGUIUtility.SetWantsMouseJumping(0);
                    HandleUtility.ignoreRaySnapObjects = null;
                    evt.Use();
                }
                break;
            }

            case EventType.Repaint:
            {
                if (Tools.vertexDragging)
                {
                    RectHandles.RectScalingHandleCap(id, pivot, rotation, 1, EventType.Repaint);
                }
                else
                {
                    Handles.color = Handles.secondaryColor * new Color(1, 1, 1, 1.5f * discOpacity);
                    Handles.CircleHandleCap(id, pivot, rotation, discSize, EventType.Repaint);
                    Handles.color = Handles.secondaryColor * new Color(1, 1, 1, 0.3f * discOpacity);
                    Handles.DrawSolidDisc(pivot, rotation * Vector3.forward, discSize);
                }
                break;
            }
            }

            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp(kChangingPosX, eventType);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp(kChangingLeft, eventType);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp(kChangingRight, eventType);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp(kChangingPosY, eventType);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp(kChangingTop, eventType);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp(kChangingBottom, eventType);

            return(newPos);
        }
Beispiel #28
0
        private static Vector3 ResizeHandlesGUI(Rect rect, Vector3 pivot, Quaternion rotation, out Vector3 scalePivot)
        {
            if (Event.current.type == EventType.MouseDown)
            {
                RectTool.s_StartRect = rect;
            }
            scalePivot = pivot;
            Vector3    vector3_1  = Vector3.one;
            Quaternion quaternion = Quaternion.Inverse(rotation);

            for (int xHandle = 0; xHandle <= 2; ++xHandle)
            {
                for (int yHandle = 0; yHandle <= 2; ++yHandle)
                {
                    if (xHandle != 1 || yHandle != 1)
                    {
                        Vector3 rectPointInWorld1 = RectTool.GetRectPointInWorld(RectTool.s_StartRect, pivot, rotation, xHandle, yHandle);
                        Vector3 rectPointInWorld2 = RectTool.GetRectPointInWorld(rect, pivot, rotation, xHandle, yHandle);
                        float   num       = 0.05f * HandleUtility.GetHandleSize(rectPointInWorld2);
                        int     controlId = GUIUtility.GetControlID(RectTool.s_ResizeHandlesHash, FocusType.Passive);
                        if ((double)GUI.color.a > 0.0 || GUIUtility.hotControl == controlId)
                        {
                            EditorGUI.BeginChangeCheck();
                            EventType type = Event.current.type;
                            Vector3   position;
                            if (xHandle == 1 || yHandle == 1)
                            {
                                Vector3 sideVector = xHandle != 1 ? rotation * Vector3.up * rect.height : rotation * Vector3.right * rect.width;
                                Vector3 direction  = xHandle != 1 ? rotation * Vector3.right : rotation * Vector3.up;
                                position = RectHandles.SideSlider(controlId, rectPointInWorld2, sideVector, direction, num, (Handles.DrawCapFunction)null, 0.0f);
                            }
                            else
                            {
                                Vector3 outwardsDir1 = rotation * Vector3.right * (float)(xHandle - 1);
                                Vector3 outwardsDir2 = rotation * Vector3.up * (float)(yHandle - 1);
                                position = RectHandles.CornerSlider(controlId, rectPointInWorld2, rotation * Vector3.forward, outwardsDir1, outwardsDir2, num, new Handles.DrawCapFunction(RectHandles.RectScalingCap), Vector2.zero);
                            }
                            bool flag1 = Selection.transforms.Length == 1 && InternalEditorUtility.SupportsRectLayout(Selection.activeTransform) && Selection.activeTransform.parent.rotation == rotation;
                            if (flag1)
                            {
                                Transform     activeTransform = Selection.activeTransform;
                                RectTransform component1      = activeTransform.GetComponent <RectTransform>();
                                Transform     parent          = activeTransform.parent;
                                RectTransform component2      = parent.GetComponent <RectTransform>();
                                if (type == EventType.MouseDown && Event.current.type != EventType.MouseDown)
                                {
                                    RectTransformSnapping.CalculateOffsetSnapValues(parent, activeTransform, component2, component1, xHandle, yHandle);
                                }
                            }
                            if (EditorGUI.EndChangeCheck())
                            {
                                ManipulationToolUtility.SetMinDragDifferenceForPos(rectPointInWorld2);
                                if (flag1)
                                {
                                    Transform     parent       = Selection.activeTransform.parent;
                                    RectTransform component    = parent.GetComponent <RectTransform>();
                                    Vector2       snapDistance = Vector2.one * HandleUtility.GetHandleSize(position) * 0.05f;
                                    snapDistance.x /= (quaternion * parent.TransformVector(Vector3.right)).x;
                                    snapDistance.y /= (quaternion * parent.TransformVector(Vector3.up)).y;
                                    Vector3 positionBeforeSnapping = parent.InverseTransformPoint(position) - (Vector3)component.rect.min;
                                    Vector3 positionAfterSnapping  = (Vector3)RectTransformSnapping.SnapToGuides((Vector2)positionBeforeSnapping, snapDistance) + Vector3.forward * positionBeforeSnapping.z;
                                    ManipulationToolUtility.DisableMinDragDifferenceBasedOnSnapping(positionBeforeSnapping, positionAfterSnapping);
                                    position = parent.TransformPoint(positionAfterSnapping + (Vector3)component.rect.min);
                                }
                                bool alt       = Event.current.alt;
                                bool actionKey = EditorGUI.actionKey;
                                bool flag2     = Event.current.shift && !actionKey;
                                if (!alt)
                                {
                                    scalePivot = RectTool.GetRectPointInWorld(RectTool.s_StartRect, pivot, rotation, 2 - xHandle, 2 - yHandle);
                                }
                                if (flag2)
                                {
                                    position = Vector3.Project(position - scalePivot, rectPointInWorld1 - scalePivot) + scalePivot;
                                }
                                Vector3 vector3_2 = quaternion * (rectPointInWorld1 - scalePivot);
                                Vector3 vector3_3 = quaternion * (position - scalePivot);
                                if (xHandle != 1)
                                {
                                    vector3_1.x = vector3_3.x / vector3_2.x;
                                }
                                if (yHandle != 1)
                                {
                                    vector3_1.y = vector3_3.y / vector3_2.y;
                                }
                                if (flag2)
                                {
                                    vector3_1 = Vector3.one * (xHandle != 1 ? vector3_1.x : vector3_1.y);
                                }
                                if (actionKey && xHandle == 1)
                                {
                                    vector3_1.x = !Event.current.shift ? 1f / Mathf.Max(vector3_1.y, 0.0001f) : (vector3_1.z = 1f / Mathf.Sqrt(Mathf.Max(vector3_1.y, 0.0001f)));
                                }
                                if (flag2)
                                {
                                    vector3_1 = Vector3.one * (xHandle != 1 ? vector3_1.x : vector3_1.y);
                                }
                                if (actionKey && xHandle == 1)
                                {
                                    vector3_1.x = !Event.current.shift ? 1f / Mathf.Max(vector3_1.y, 0.0001f) : (vector3_1.z = 1f / Mathf.Sqrt(Mathf.Max(vector3_1.y, 0.0001f)));
                                }
                                if (actionKey && yHandle == 1)
                                {
                                    vector3_1.y = !Event.current.shift ? 1f / Mathf.Max(vector3_1.x, 0.0001f) : (vector3_1.z = 1f / Mathf.Sqrt(Mathf.Max(vector3_1.x, 0.0001f)));
                                }
                            }
                            if (xHandle == 0)
                            {
                                ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingLeft", type);
                            }
                            if (xHandle == 2)
                            {
                                ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingRight", type);
                            }
                            if (xHandle != 1)
                            {
                                ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingWidth", type);
                            }
                            if (yHandle == 0)
                            {
                                ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingBottom", type);
                            }
                            if (yHandle == 2)
                            {
                                ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingTop", type);
                            }
                            if (yHandle != 1)
                            {
                                ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingHeight", type);
                            }
                        }
                    }
                }
            }
            return(vector3_1);
        }