Пример #1
0
        static void DoSizeHandles(ProBuilderShape proBuilderShape, bool updatePrefs)
        {
            int faceCount = s_Faces.Length;

            var evt = Event.current;

            var is2D = proBuilderShape.shape is Plane || proBuilderShape.shape is Sprite;

            for (int i = 0; i < faceCount; i++)
            {
                var face = faces[i];
                if (is2D && !face.IsValid)
                {
                    continue;
                }

                if (Event.current.type == EventType.Repaint)
                {
                    Color color = k_BoundsHandleColor;
                    color.a *= face.IsVisible ? 1f : 0.5f;
                    using (new Handles.DrawingScope(color))
                    {
                        int pointsCount = face.Points.Length;
                        for (int k = 0; k < pointsCount; k++)
                        {
                            Handles.DrawLine(face.Points[k], face.Points[(k + 1) % pointsCount]);
                        }
                    }
                }

                if (DoFaceSizeHandle(face, s_FaceControlIDs[i]))
                {
                    if (!s_SizeManipulationInit)
                    {
                        s_StartCenter          = proBuilderShape.transform.position + proBuilderShape.transform.TransformVector(proBuilderShape.shapeBox.center);
                        s_StartScale           = proBuilderShape.transform.lossyScale;
                        s_StartScaleInverse    = new Vector3(1f / Mathf.Abs(s_StartScale.x), 1f / Mathf.Abs(s_StartScale.y), 1f / Mathf.Abs(s_StartScale.z));
                        s_StartPositionLocal   = face.CenterPosition;
                        s_StartPositionGlobal  = proBuilderShape.transform.TransformPoint(Vector3.Scale(face.CenterPosition, s_StartScale));
                        s_StartSize            = proBuilderShape.size;
                        s_SizeManipulationInit = true;
                        s_Scaling = Vector3.Scale(face.Normal, Math.Sign(s_StartSize));
                    }

                    var targetSize = s_StartSize;
                    if (Math.IsCardinalAxis(proBuilderShape.transform.up) &&
                        EditorSnapSettings.gridSnapEnabled &&
                        !EditorSnapSettings.incrementalSnapActive &&
                        !evt.alt)
                    {
                        var faceDelta    = (s_SizeDelta * s_Faces[i].Normal);
                        var facePosition = s_StartPositionGlobal + faceDelta;
                        facePosition = ProBuilderSnapping.Snap(facePosition, EditorSnapping.activeMoveSnapValue);
                        targetSize  += Vector3.Scale((facePosition - s_StartPositionGlobal), s_Scaling);
                    }
                    else
                    {
                        //Should we expand on the 2 sides?
                        var modifier = evt.alt ? 2f : 1f;
                        var delta    = modifier * (s_SizeDelta * s_Faces[i].Normal);
                        delta.Scale(s_Scaling);
                        delta.Scale(s_StartScaleInverse);

                        targetSize += delta;
                        var snap = EditorSnapSettings.incrementalSnapActive
                            ? Vector3.Scale(EditorSnapping.activeMoveSnapValue, Math.Abs(face.Normal))
                            : Vector3.zero;

                        targetSize = ProBuilderSnapping.Snap(targetSize, snap);
                    }

                    var center = Vector3.zero;
                    if (!evt.alt)
                    {
                        center = Vector3.Scale((targetSize - s_StartSize) / 2f, s_Scaling);
                        center = Vector3.Scale(center, Math.Sign(s_StartScale));
                        center = proBuilderShape.transform.TransformVector(center);
                    }

                    ApplyProperties(proBuilderShape, s_StartCenter + center, targetSize);

                    if (updatePrefs)
                    {
                        DrawShapeTool.SaveShapeParams(proBuilderShape);
                    }
                }
            }
        }
Пример #2
0
        static bool DoOrientationHandle(FaceData face, ProBuilderShape proBuilderShape)
        {
            Event evt        = Event.current;
            bool  hasRotated = false;

            switch (evt.type)
            {
            case EventType.MouseDown:
                if (k_OrientationControlIDs.Contains(HandleUtility.nearestControl) && evt.button == 0)
                {
                    s_CurrentId           = HandleUtility.nearestControl;
                    GUIUtility.hotControl = s_CurrentId;
                    evt.Use();
                }
                break;

            case EventType.MouseUp:
                if (k_OrientationControlIDs.Contains(HandleUtility.nearestControl) && evt.button == 0)
                {
                    GUIUtility.hotControl = 0;
                    evt.Use();
                    if (s_CurrentId == HandleUtility.nearestControl)
                    {
                        //Execute rotation
                        Vector3 targetedNormal = Vector3.zero;
                        for (int i = 0; i < k_OrientationControlIDs.Length; i++)
                        {
                            if (k_OrientationControlIDs[i] == s_CurrentId)
                            {
                                targetedNormal = (s_ArrowsLines[i][1] - face.CenterPosition).normalized;
                                break;
                            }
                        }

                        var currentNormal = face.Normal;
                        currentNormal.Scale(Math.Sign(proBuilderShape.size));
                        targetedNormal.Scale(Math.Sign(proBuilderShape.size));
                        Vector3 rotationAxis = Vector3.Cross(currentNormal, targetedNormal);
                        var     angle        = Vector3.SignedAngle(currentNormal, targetedNormal, rotationAxis);
                        s_ShapeRotation = Quaternion.AngleAxis(angle, rotationAxis);
                        s_CurrentAngle  = (s_CurrentAngle + angle) % 360;

                        hasRotated = true;
                    }
                    s_CurrentId = -1;
                }
                break;

            case EventType.Layout:
                for (int i = 0; i < 4; i++)
                {
                    var   rectPos = 0.8f * s_ArrowsLines[i][1] + 0.2f * face.CenterPosition;
                    float dist    = HandleUtility.DistanceToRectangle(rectPos,
                                                                      Quaternion.LookRotation(face.Normal),
                                                                      HandleUtility.GetHandleSize(face.CenterPosition) * s_DefaultMidpointSquareSize / 2f);
                    HandleUtility.AddControl(k_OrientationControlIDs[i], dist);
                }
                break;

            case EventType.Repaint:
                if (s_CurrentArrowHovered != HandleUtility.nearestControl)
                {
                    s_CurrentAngle = 0f;
                }

                int pointsCount = face.Points.Length;
                s_CurrentArrowHovered = -1;
                for (int i = 0; i < pointsCount; i++)
                {
                    var rectHandleSize = HandleUtility.GetHandleSize(face.CenterPosition) * s_DefaultMidpointSquareSize;

                    var sideDirection  = (face.Points[(i + 1) % pointsCount] - face.Points[i]).normalized;
                    var arrowDirection = Vector3.Cross(face.Normal.normalized, sideDirection).normalized;

                    var topDirection = 2.5f * rectHandleSize * arrowDirection;
                    var top          = face.CenterPosition + topDirection;
                    var A            = topDirection.magnitude;
                    var a            = 0.33f * Mathf.Sqrt(2f * A * A);
                    var h            = 0.5f * Mathf.Sqrt(2f * a * a);
                    s_ArrowsLines[i][0] = top - (h * arrowDirection + h * sideDirection);
                    s_ArrowsLines[i][1] = top;
                    s_ArrowsLines[i][2] = top - (h * arrowDirection - h * sideDirection);

                    bool selected = HandleUtility.nearestControl == k_OrientationControlIDs[i];

                    Color color = selected
                               ? EditorHandleDrawing.edgeSelectedColor
                               : k_BoundsHandleColor;
                    color.a = 1.0f;

                    using (new Handles.DrawingScope(color))
                    {
                        Handles.DrawAAPolyLine(5f, s_ArrowsLines[i]);
                        if (selected)
                        {
                            EditorGUIUtility.AddCursorRect(new Rect(0, 0, Screen.width, Screen.height), MouseCursor.RotateArrow);
                            s_CurrentArrowHovered = HandleUtility.nearestControl;
                            Handles.DrawAAPolyLine(3f,
                                                   new Vector3[]
                            {
                                Vector3.Scale(proBuilderShape.rotation * Vector3.up, proBuilderShape.size / 2f),
                                Vector3.zero,
                                Vector3.Scale(proBuilderShape.rotation * Vector3.forward, proBuilderShape.size / 2f)
                            });
                        }
                    }
                }
                break;

            case EventType.MouseDrag:
                if (k_OrientationControlIDs.Contains(s_CurrentId) && HandleUtility.nearestControl != s_CurrentId)
                {
                    GUIUtility.hotControl = 0;
                    s_CurrentId           = -1;
                }
                break;
            }
            return(hasRotated);
        }
        static void DoSizeHandles(ProBuilderShape proBuilderShape, bool updatePrefs)
        {
            int faceCount = s_Faces.Length;

            var evt = Event.current;

            var is2D = proBuilderShape.shape is Plane || proBuilderShape.shape is Sprite;

            for (int i = 0; i < faceCount; i++)
            {
                var face = faces[i];
                if (is2D && !face.IsValid)
                {
                    continue;
                }

                if (Event.current.type == EventType.Repaint)
                {
                    Color color = k_BoundsHandleColor;
                    color.a *= face.IsVisible ? 1f : 0.5f;
                    using (new Handles.DrawingScope(color))
                    {
                        int pointsCount = face.Points.Length;
                        for (int k = 0; k < pointsCount; k++)
                        {
                            Handles.DrawLine(face.Points[k], face.Points[(k + 1) % pointsCount]);
                        }
                    }
                }

                if (DoFaceSizeHandle(face))
                {
                    float modifier = 1f;
                    if (evt.alt)
                    {
                        modifier = 2f;
                    }

                    if (!s_SizeManipulationInit)
                    {
                        s_StartCenter          = proBuilderShape.transform.position + proBuilderShape.transform.TransformVector(proBuilderShape.shapeBox.center);
                        s_StartPosition        = face.CenterPosition;
                        s_StartSize            = proBuilderShape.size;
                        s_SizeManipulationInit = true;

                        s_Scaling = Vector3.Scale(face.Normal, Math.Sign(s_StartSize));
                    }

                    var targetDelta = modifier * (s_TargetSize - s_StartPosition);
                    targetDelta.Scale(s_Scaling);

                    var targetSize = s_StartSize + targetDelta;

                    var snap = Math.IsCardinalAxis(proBuilderShape.transform.up) && EditorSnapSettings.gridSnapEnabled ?
                               EditorSnapping.activeMoveSnapValue :
                               Vector3.zero;
                    targetSize = ProBuilderSnapping.Snap(targetSize, snap);

                    var center = Vector3.zero;
                    if (!evt.alt)
                    {
                        center = Vector3.Scale((targetSize - s_StartSize) / 2f, s_Scaling);
                        center = Vector3.Scale(center, Math.Sign(proBuilderShape.transform.lossyScale));
                        center = proBuilderShape.transform.TransformVector(center);
                    }

                    ApplyProperties(proBuilderShape, s_StartCenter + center, targetSize);

                    if (updatePrefs)
                    {
                        DrawShapeTool.SaveShapeParams(proBuilderShape);
                    }
                }
            }
        }