Пример #1
0
        public override void OnSceneGUI(SceneView sceneView, Rect dragArea)
        {
            var defaultID = GUIUtility.GetControlID(kSurfaceEditModeHash, FocusType.Passive, dragArea);

            HandleUtility.AddDefaultControl(defaultID);

            var selectionType = ChiselRectSelectionManager.GetCurrentSelectionType();
            var repaint       = ChiselUVToolCommon.SurfaceSelection(dragArea, selectionType);

            repaint = SurfaceRotateTool(selectionType, dragArea) || repaint;

            // Set cursor depending on selection type and/or active tool
            {
                MouseCursor cursor;
                switch (selectionType)
                {
                default: cursor = MouseCursor.RotateArrow; break;

                case SelectionType.Additive:    cursor = MouseCursor.ArrowPlus; break;

                case SelectionType.Subtractive: cursor = MouseCursor.ArrowMinus; break;
                }
                EditorGUIUtility.AddCursorRect(dragArea, cursor);
            }

            // Repaint the scene-views if we need to
            if (repaint &&
                // avoid infinite loop
                Event.current.type != EventType.Layout &&
                Event.current.type != EventType.Repaint)
            {
                SceneView.RepaintAll();
            }
        }
Пример #2
0
        private static bool SurfaceScaleTool(SelectionType selectionType, Rect dragArea)
        {
            var id = GUIUtility.GetControlID(kSurfaceScaleHash, FocusType.Keyboard, dragArea);

            if (!ChiselUVToolCommon.SurfaceToolBase(id, selectionType, dragArea))
            {
                return(false);
            }

            bool needRepaint = false;

            switch (Event.current.GetTypeForControl(id))
            {
            // TODO: support scaling texture using keyboard
            case EventType.Repaint:
            {
                // TODO: show scaling of uv
                break;
            }

            case EventType.MouseDrag:
            {
                if (!ChiselUVToolCommon.IsToolEnabled(id))
                {
                    break;
                }

                ChiselUVToolCommon.StartToolDragging();
                // TODO: implement
                break;
            }
            }
            return(needRepaint);
        }
Пример #3
0
        private static bool SurfaceMoveTool(SelectionType selectionType, Rect dragArea)
        {
            var id = GUIUtility.GetControlID(kSurfaceMoveHash, FocusType.Keyboard, dragArea);

            if (!ChiselUVToolCommon.SurfaceToolBase(id, selectionType, dragArea))
            {
                return(false);
            }

            bool needRepaint = false;

            switch (Event.current.GetTypeForControl(id))
            {
            // TODO: support moving texture using keyboard
            case EventType.Repaint:
            {
                if (!ChiselUVToolCommon.ToolIsDragging)
                {
                    break;
                }

                ChiselUVToolCommon.RenderIntersectionPoint();
                ChiselUVToolCommon.RenderSnapEvent();

                // TODO: show delta movement of uv
                break;
            }

            case EventType.MouseDrag:
            {
                if (!ChiselUVToolCommon.IsToolEnabled(id))
                {
                    break;
                }

                ChiselUVToolCommon.StartToolDragging();
                TranslateSurfacesInWorldSpace(-ChiselUVToolCommon.worldDragDeltaVector);     // TODO: figure out why this is reversed
                break;
            }
            }
            return(needRepaint);
        }
Пример #4
0
        private static bool SurfaceRotateTool(SelectionType selectionType, Rect dragArea)
        {
            var id = GUIUtility.GetControlID(kSurfaceRotateHash, FocusType.Keyboard, dragArea);

            if (!ChiselUVToolCommon.SurfaceToolBase(id, selectionType, dragArea))
            {
                return(false);
            }

            bool needRepaint = false;

            if (!ChiselUVToolCommon.IsToolEnabled(id))
            {
                needRepaint          = haveRotateStartAngle;
                haveRotateStartAngle = false;
                ChiselUVToolCommon.pointHasSnapped = false;
            }

            switch (Event.current.GetTypeForControl(id))
            {
            // TODO: support rotating texture using keyboard?
            case EventType.Repaint:
            {
                if (haveRotateStartAngle)
                {
                    var toWorldVector = ChiselUVToolCommon.worldDragDeltaVector;
                    var magnitude     = toWorldVector.magnitude;
                    toWorldVector /= magnitude;

                    // TODO: need a nicer visualization here, show delta rotation, angles etc.
                    Handles.DrawWireDisc(ChiselUVToolCommon.worldStartPosition, ChiselUVToolCommon.worldProjectionPlane.normal, magnitude);
                    if (haveRotateStartAngle)
                    {
                        var snappedToWorldVector = Quaternion.AngleAxis(rotateAngle, ChiselUVToolCommon.worldDragPlane.normal) * fromWorldVector;
                        Handles.DrawDottedLine(ChiselUVToolCommon.worldStartPosition, ChiselUVToolCommon.worldStartPosition + (fromWorldVector * magnitude), 4.0f);
                        Handles.DrawDottedLine(ChiselUVToolCommon.worldStartPosition, ChiselUVToolCommon.worldStartPosition + (snappedToWorldVector * magnitude), 4.0f);
                    }
                    else
                    {
                        Handles.DrawDottedLine(ChiselUVToolCommon.worldStartPosition, ChiselUVToolCommon.worldStartPosition + (toWorldVector * magnitude), 4.0f);
                    }
                }
                if (ChiselUVToolCommon.IsToolEnabled(id))
                {
                    if (haveRotateStartAngle &&
                        ChiselUVToolCommon.pointHasSnapped)
                    {
                        ChiselUVToolCommon.RenderIntersectionPoint();
                        ChiselUVToolCommon.RenderSnapEvent();
                    }
                }
                break;
            }

            case EventType.MouseDrag:
            {
                if (!ChiselUVToolCommon.IsToolEnabled(id))
                {
                    break;
                }

                if (ChiselUVToolCommon.StartToolDragging())
                {
                    haveRotateStartAngle = false;
                    ChiselUVToolCommon.pointHasSnapped = false;
                }

                var toWorldVector = ChiselUVToolCommon.worldDragDeltaVector;
                if (!haveRotateStartAngle)
                {
                    var handleSize     = HandleUtility.GetHandleSize(ChiselUVToolCommon.worldStartPosition);
                    var minDiameterSqr = handleSize * kMinRotateDiameter;
                    // Only start rotating when we've moved the cursor far away enough from the center of rotation
                    if (toWorldVector.sqrMagnitude > minDiameterSqr)
                    {
                        // Switch to rotation mode, we have a center and a start angle to compare with,
                        // from now on, when we move the mouse we change the rotation angle relative to this first angle.
                        haveRotateStartAngle = true;
                        ChiselUVToolCommon.pointHasSnapped = false;
                        fromWorldVector = toWorldVector.normalized;
                        rotateAngle     = 0;

                        // We override the snapping settings to only allow snapping against vertices & edges,
                        // we do this only after we have our starting vector, so that when we rotate we're not constantly
                        // snapping against the grid when we really just want to be able to snap against the current rotation step.
                        // On the other hand, we do want to be able to snap against vertices ..
                        ChiselUVToolCommon.toolSnapOverrides = SnapSettings.UVGeometryVertices | SnapSettings.UVGeometryEdges;
                    }
                }
                else
                {
                    // Get the angle between 'from' and 'to' on the plane we're dragging over
                    rotateAngle = MathExtensions.SignedAngle(fromWorldVector, toWorldVector.normalized, ChiselUVToolCommon.worldDragPlane.normal);

                    // If we snapped against something, ignore angle snapping
                    if (!ChiselUVToolCommon.pointHasSnapped)
                    {
                        rotateAngle = ChiselUVToolCommon.SnapAngle(rotateAngle);
                    }

                    RotateSurfacesInWorldSpace(ChiselUVToolCommon.worldStartPosition, ChiselUVToolCommon.worldDragPlane.normal, -rotateAngle);     // TODO: figure out why this is reversed
                }
                break;
            }
            }
            return(needRepaint);
        }
Пример #5
0
        private static bool SurfaceScaleTool(SelectionType selectionType, Rect dragArea)
        {
            var id = GUIUtility.GetControlID(kSurfaceScaleHash, FocusType.Keyboard, dragArea);

            if (!ChiselUVToolCommon.SurfaceToolBase(id, selectionType, dragArea))
            {
                return(false);
            }

            bool needRepaint = false;

            switch (Event.current.GetTypeForControl(id))
            {
            // TODO: support scaling texture using keyboard
            case EventType.Repaint:
            {
                if (haveScaleStartLength)
                {
                    var toWorldVector = ChiselUVToolCommon.worldDragDeltaVector;
                    var magnitude     = toWorldVector.magnitude;
                    toWorldVector /= magnitude;
                    if (haveScaleStartLength)
                    {
                        Handles.DrawDottedLine(ChiselUVToolCommon.worldStartPosition, ChiselUVToolCommon.worldStartPosition + (toWorldVector * compareDistance), 4.0f);
                        Handles.DrawDottedLine(ChiselUVToolCommon.worldStartPosition, ChiselUVToolCommon.worldStartPosition + (toWorldVector * magnitude), 4.0f);
                    }
                    else
                    {
                        Handles.DrawDottedLine(ChiselUVToolCommon.worldStartPosition, ChiselUVToolCommon.worldStartPosition + (toWorldVector * magnitude), 4.0f);
                    }
                }
                if (ChiselUVToolCommon.IsToolEnabled(id))
                {
                    if (haveScaleStartLength &&
                        ChiselUVToolCommon.pointHasSnapped)
                    {
                        ChiselUVToolCommon.RenderIntersectionPoint();
                        ChiselUVToolCommon.RenderSnapEvent();
                    }
                }
                break;
            }

            case EventType.MouseDrag:
            {
                if (!ChiselUVToolCommon.IsToolEnabled(id))
                {
                    break;
                }

                if (ChiselUVToolCommon.StartToolDragging())
                {
                    haveScaleStartLength = false;
                    ChiselUVToolCommon.pointHasSnapped = false;
                }

                var toWorldVector = ChiselUVToolCommon.worldDragDeltaVector;
                if (!haveScaleStartLength)
                {
                    var handleSize     = HandleUtility.GetHandleSize(ChiselUVToolCommon.worldStartPosition);
                    var minDiameterSqr = handleSize * kMinScaleDiameter;
                    // Only start scaling when we've moved the cursor far away enough from the center of scale
                    if (toWorldVector.sqrMagnitude > minDiameterSqr)
                    {
                        // Switch to scaling mode, we have a center and a start distance to compare with,
                        // from now on, when we move the mouse we change the scale relative to this first distance.
                        haveScaleStartLength = true;
                        ChiselUVToolCommon.pointHasSnapped = false;
                        compareDistance = toWorldVector.sqrMagnitude;
                    }
                }
                else
                {
                    // TODO: drag from one position to another -> texture should fit in between and tile accordingly, taking rotation into account
                    ScaleSurfacesInWorldSpace(ChiselUVToolCommon.worldStartPosition, ChiselUVToolCommon.worldDragPlane.normal, compareDistance / toWorldVector.sqrMagnitude);
                }
                break;
            }
            }
            return(needRepaint);
        }