UnityEditor.RectTool.MoveHandlesGUI C# (CSharp) Method

MoveHandlesGUI() private static method

private static MoveHandlesGUI ( Rect rect, Vector3 pivot, Quaternion rotation ) : Vector3
rect UnityEngine.Rect
pivot Vector3
rotation UnityEngine.Quaternion
return Vector3
        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;
        }

Usage Example

示例#1
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;
        }
All Usage Examples Of UnityEditor.RectTool::MoveHandlesGUI