public void OnSceneGUI(SceneView sceneView, Rect dragArea)
        {
            Bounds    bounds;
            CSGModel  modelBeneathCursor;
            Matrix4x4 transformation;
            float     height;

            var flags = BoxExtrusionFlags.AlwaysFaceUp;

            switch (BoxExtrusionHandle.Do(dragArea, out bounds, out height, out modelBeneathCursor, out transformation, flags, Axis.Y))
            {
            case BoxExtrusionState.Create:
            {
                linearStairs = BrushMeshAssetFactory.Create <CSGLinearStairs>("Linear Stairs",
                                                                              BrushMeshAssetFactory.GetModelForNode(modelBeneathCursor),
                                                                              transformation);
                linearStairs.definition.Reset();
                linearStairs.Operation = forceOperation ?? CSGOperationType.Additive;
                linearStairs.Bounds    = bounds;
                linearStairs.UpdateGenerator();
                break;
            }

            case BoxExtrusionState.Modified:
            {
                linearStairs.Operation = forceOperation ??
                                         ((height < 0 && modelBeneathCursor) ?
                                          CSGOperationType.Subtractive :
                                          CSGOperationType.Additive);
                linearStairs.Bounds = bounds;
                break;
            }

            case BoxExtrusionState.Commit:
            {
                UnityEditor.Selection.activeGameObject = linearStairs.gameObject;
                CSGEditModeManager.EditMode            = CSGEditMode.ShapeEdit;
                Reset();
                break;
            }

            case BoxExtrusionState.Cancel:
            {
                Reset();
                Undo.RevertAllInCurrentGroup();
                EditorGUIUtility.ExitGUI();
                break;
            }

            case BoxExtrusionState.BoxMode:
            case BoxExtrusionState.SquareMode:      { CSGOutlineRenderer.VisualizationMode = VisualizationMode.SimpleOutline; break; }

            case BoxExtrusionState.HoverMode:       { CSGOutlineRenderer.VisualizationMode = VisualizationMode.Outline; break; }
            }

            HandleRendering.RenderBox(transformation, bounds);
        }
Beispiel #2
0
        public override void OnSceneGUI(SceneView sceneView, Rect dragArea)
        {
            var evt = Event.current;

            switch (evt.type)
            {
            case EventType.KeyDown:
            {
                if (evt.keyCode == ChiselKeyboardDefaults.kCancelKey)
                {
                    if (GUIUtility.hotControl == 0)
                    {
                        evt.Use();
                        break;
                    }
                }
                break;
            }

            case EventType.KeyUp:
            {
                if (evt.keyCode == ChiselKeyboardDefaults.kCancelKey)
                {
                    if (GUIUtility.hotControl == 0)
                    {
                        Selection.activeTransform = null;
                        evt.Use();
                        GUIUtility.ExitGUI();     // avoids a nullreference exception in sceneview
                        break;
                    }
                }
                break;
            }
            }

            var position = Tools.handlePosition;
            var rotation = Tools.handleRotation;

            EditorGUI.BeginChangeCheck();
            var handleIDs = new SceneHandles.PositionHandleIDs();

            SceneHandles.Initialize(ref handleIDs);

            Snapping.FindCustomSnappingPointsRayMethod = FindCustomSnappingRayPoints;
            Snapping.FindCustomSnappingPointsMethod    = FindCustomSnappingPoints;
            Snapping.CustomSnappedEvent = OnCustomSnappedEvent;
            var newPosition = SceneHandles.PositionHandle(ref handleIDs, position, rotation);

            Snapping.FindCustomSnappingPointsRayMethod = null;
            Snapping.FindCustomSnappingPointsMethod    = null;
            Snapping.CustomSnappedEvent = null;
            if (EditorGUI.EndChangeCheck())
            {
                var nodes = ChiselSelectionManager.SelectedNodes;
                if (nodes == null || nodes.Count == 0)
                {
                    return;
                }
                PIvotUtility.MovePivotTo(nodes, newPosition);
            }

            if (Event.current.type == EventType.Repaint)
            {
                var handleSize = UnityEditor.HandleUtility.GetHandleSize(position);
                HandleRendering.DrawCameraAlignedCircle(position, handleSize * 0.1f, Color.white, Color.black);

                if ((handleIDs.combinedState & (ControlState.Focused | ControlState.Active)) != ControlState.None)
                {
                    var prevColor     = Handles.color;
                    var selectedColor = UnityEditor.Handles.selectedColor;
                    selectedColor.a = 0.5f;
                    Handles.color   = selectedColor;

                    //if ((handleIDs.xAxisIndirectState & (ControlState.Focused | ControlState.Active)) != ControlState.None)
                    //    HandleRendering.DrawInfiniteLine(position, Axis.X);
                    //if ((handleIDs.yAxisIndirectState & (ControlState.Focused | ControlState.Active)) != ControlState.None)
                    //    HandleRendering.DrawInfiniteLine(position, Axis.Y);
                    //if ((handleIDs.zAxisIndirectState & (ControlState.Focused | ControlState.Active)) != ControlState.None)
                    //    HandleRendering.DrawInfiniteLine(position, Axis.Z);

                    DrawCustomSnappedEvent();

                    Handles.color = prevColor;
                }
            }
        }
Beispiel #3
0
        public static void Do(int id, Rect dragArea, ref List <Vector3> points, out Matrix4x4 transformation, out ChiselModel modelBeneathCursor, UnitySceneExtensions.SceneHandles.CapFunction capFunction)
        {
            modelBeneathCursor = null;
            var evt  = Event.current;
            var type = evt.GetTypeForControl(id);

            switch (type)
            {
            case EventType.ValidateCommand: { if (evt.commandName == kSoftDeleteCommand)
                                              {
                                                  evt.Use(); break;
                                              }
                                              break; }

            case EventType.ExecuteCommand:  { if (evt.commandName == kSoftDeleteCommand)
                                              {
                                                  Cancel(evt, ref points); break;
                                              }
                                              break; }

            case EventType.KeyDown:                 { if (evt.keyCode == kCancelKey ||
                                                          evt.keyCode == kCommitKey)
                                                      {
                                                          evt.Use(); break;
                                                      }
                                                      break; }

            case EventType.KeyUp:                   { if (evt.keyCode == kCancelKey)
                                                      {
                                                          Cancel(evt, ref points); break;
                                                      }
                                                      else
                                                      if (evt.keyCode == kCommitKey)
                                                      {
                                                          Commit(evt, dragArea, ref points); break;
                                                      }
                                                      break; }

            case EventType.Layout:
            {
                if (SceneHandles.InCameraOrbitMode)
                {
                    break;
                }

                if (s_StartIntersection == null)
                {
                    break;
                }

                // We set the id at the maximum handle distance so that other things, such as the axis gizmo,
                // will block the click to create a point. If we don't we wouldn't be able to use the axis gizmo.
                UnityEditor.HandleUtility.AddControl(id, kMaxHandleDistance);
                break;
            }

            case EventType.Repaint:
            {
                if (s_StartIntersection == null)
                {
                    break;
                }

                if (points.Count == 0)
                {
                    break;
                }

                if (SceneHandleUtility.focusControl != id)
                {
                    break;
                }

                using (new UnityEditor.Handles.DrawingScope(Matrix4x4.identity))
                {
                    var orientation = s_StartIntersection.orientation;
                    if (capFunction != null)
                    {
                        using (new UnityEditor.Handles.DrawingScope(s_Transform))
                        {
                            for (int i = 0; i < points.Count; i++)
                            {
                                capFunction(id, points[i], orientation, UnityEditor.HandleUtility.GetHandleSize(points[i]) * kPointScale, type);
                            }
                        }
                    }

                    var selectedColor = UnityEditor.Handles.selectedColor;
                    selectedColor.a = 0.5f;
                    using (new UnityEditor.Handles.DrawingScope(selectedColor))
                    {
                        HandleRendering.RenderSnapping3D(s_Snapping2D.WorldSlideGrid, s_Snapping2D.WorldSnappedExtents, s_Snapping2D.GridSnappedPosition, s_Snapping2D.SnapResult, true);
                    }
                }
                break;
            }

            case EventType.MouseMove:
            {
                if (GUIUtility.hotControl != 0 &&
                    GUIUtility.hotControl != id)
                {
                    break;
                }

                UpdatePoints(points, GetPointAtPosition(evt.mousePosition, dragArea));
                SceneView.RepaintAll();
                break;
            }

            case EventType.MouseDrag:
            {
                if (GUIUtility.hotControl != id)
                {
                    break;
                }

                UpdatePoints(points, GetPointAtPosition(evt.mousePosition, dragArea));
                GUI.changed = true;
                evt.Use();
                break;
            }

            case EventType.MouseDown:
            {
                if (SceneHandles.InCameraOrbitMode)
                {
                    break;
                }

                if (GUIUtility.hotControl != 0)
                {
                    break;
                }

                if ((UnityEditor.HandleUtility.nearestControl != id || evt.button != 0) &&
                    (GUIUtility.keyboardControl != id || evt.button != 2))
                {
                    break;
                }

                if (s_StartIntersection == null)
                {
                    break;
                }

                s_CurrentPointIndex++;
                GUIUtility.hotControl = GUIUtility.keyboardControl = id;
                EditorGUIUtility.SetWantsMouseJumping(1);
                evt.Use();
                break;
            }

            case EventType.MouseUp:
            {
                if (GUIUtility.hotControl != id || (evt.button != 0 && evt.button != 2))
                {
                    break;
                }

                GUIUtility.hotControl      = 0;
                GUIUtility.keyboardControl = 0;
                evt.Use();
                EditorGUIUtility.SetWantsMouseJumping(0);

                // reset the starting position
                UpdatePoints(points, GetPointAtPosition(evt.mousePosition, dragArea));
                break;
            }
            }
            if (s_StartIntersection != null)
            {
                modelBeneathCursor = s_StartIntersection.model;
                transformation     = s_Transform;
            }
            else
            {
                transformation = Matrix4x4.identity;
            }
        }
        public void OnSceneGUI(SceneView sceneView, Rect dragArea)
        {
            Bounds    bounds;
            CSGModel  modelBeneathCursor;
            Matrix4x4 transformation;
            float     height;

            var flags = (generateFromCenterY  ? BoxExtrusionFlags.GenerateFromCenterY  : BoxExtrusionFlags.None) |
                        (isSymmetrical        ? BoxExtrusionFlags.IsSymmetricalXZ      : BoxExtrusionFlags.None) |
                        (generateFromCenterXZ ? BoxExtrusionFlags.GenerateFromCenterXZ : BoxExtrusionFlags.None);

            switch (BoxExtrusionHandle.Do(dragArea, out bounds, out height, out modelBeneathCursor, out transformation, flags, Axis.Y))
            {
            case BoxExtrusionState.Create:
            {
                sphere = BrushMeshAssetFactory.Create <CSGSphere>("Sphere",
                                                                  BrushMeshAssetFactory.GetModelForNode(modelBeneathCursor),
                                                                  transformation);

                sphere.definition.Reset();
                sphere.Operation          = forceOperation ?? CSGOperationType.Additive;
                sphere.VerticalSegments   = verticalSegments;
                sphere.HorizontalSegments = horizontalSegments;
                sphere.GenerateFromCenter = generateFromCenterY;
                sphere.DiameterXYZ        = bounds.size;
                sphere.UpdateGenerator();
                break;
            }

            case BoxExtrusionState.Modified:
            {
                sphere.Operation = forceOperation ??
                                   ((height < 0 && modelBeneathCursor) ?
                                    CSGOperationType.Subtractive :
                                    CSGOperationType.Additive);
                sphere.DiameterXYZ = bounds.size;
                break;
            }

            case BoxExtrusionState.Commit:
            {
                UnityEditor.Selection.activeGameObject = sphere.gameObject;
                CSGEditModeManager.EditMode            = CSGEditMode.ShapeEdit;
                Reset();
                break;
            }

            case BoxExtrusionState.Cancel:
            {
                Reset();
                Undo.RevertAllInCurrentGroup();
                EditorGUIUtility.ExitGUI();
                break;
            }

            case BoxExtrusionState.BoxMode:
            case BoxExtrusionState.SquareMode: { CSGOutlineRenderer.VisualizationMode = VisualizationMode.SimpleOutline; break; }

            case BoxExtrusionState.HoverMode: { CSGOutlineRenderer.VisualizationMode = VisualizationMode.Outline; break; }
            }

            // TODO: Make a RenderSphere method
            HandleRendering.RenderCylinder(transformation, bounds, horizontalSegments);
        }
        public void OnSceneGUI(SceneView sceneView, Rect dragArea)
        {
            Curve2D   shape;
            CSGModel  modelBeneathCursor;
            Matrix4x4 transformation;
            float     height;

            // TODO: handle snapping against own points
            // TODO: handle ability to 'commit' last point
            switch (ShapeExtrusionHandle.Do(dragArea, out shape, out height, out modelBeneathCursor, out transformation, Axis.Y))
            {
            case ShapeExtrusionState.Create:
            {
                var center2D = shape.Center;
                var center3D = new Vector3(center2D.x, 0, center2D.y);
                extrudedShape = BrushMeshAssetFactory.Create <CSGExtrudedShape>("Extruded Shape",
                                                                                BrushMeshAssetFactory.GetModelForNode(modelBeneathCursor),
                                                                                transformation * Matrix4x4.TRS(center3D, Quaternion.identity, Vector3.one));
                shape.Center = Vector2.zero;

                extrudedShape.Operation = forceOperation ?? CSGOperationType.Additive;
                extrudedShape.Shape     = new Curve2D(shape);
                extrudedShape.Path      = new Path(new[] {
                        new PathPoint(Vector3.zero),
                        new PathPoint(new Vector3(0, 1, 0))
                    });
                extrudedShape.UpdateGenerator();
                break;
            }

            case ShapeExtrusionState.Modified:
            {
                extrudedShape.Operation = forceOperation ??
                                          ((height < 0 && modelBeneathCursor) ?
                                           CSGOperationType.Subtractive :
                                           CSGOperationType.Additive);
                extrudedShape.Path.segments[1].position = new Vector3(0, height, 0);
                extrudedShape.UpdateGenerator();
                break;
            }

            case ShapeExtrusionState.Commit:
            {
                UnityEditor.Selection.activeGameObject = extrudedShape.gameObject;
                Reset();
                CSGEditModeManager.EditMode = CSGEditMode.ShapeEdit;
                break;
            }

            case ShapeExtrusionState.Cancel:
            {
                Reset();
                Undo.RevertAllInCurrentGroup();
                EditorGUIUtility.ExitGUI();
                break;
            }

            case ShapeExtrusionState.ExtrusionMode:
            case ShapeExtrusionState.ShapeMode:             { CSGOutlineRenderer.VisualizationMode = VisualizationMode.SimpleOutline; break; }

            case ShapeExtrusionState.HoverMode:             { CSGOutlineRenderer.VisualizationMode = VisualizationMode.Outline; break; }
            }

            HandleRendering.RenderShape(transformation, shape, height);
        }
        public void OnSceneGUI(SceneView sceneView, Rect dragArea)
        {
            Bounds    bounds;
            CSGModel  modelBeneathCursor;
            Matrix4x4 transformation;
            float     height;

            var flags = (isSymmetrical ? BoxExtrusionFlags.IsSymmetricalXZ : BoxExtrusionFlags.None) |
                        (generateFromCenterXZ ? BoxExtrusionFlags.GenerateFromCenterXZ : BoxExtrusionFlags.None);

            switch (BoxExtrusionHandle.Do(dragArea, out bounds, out height, out modelBeneathCursor, out transformation, flags, Axis.Y))
            {
            case BoxExtrusionState.Create:
            {
                capsule = BrushMeshAssetFactory.Create <CSGCapsule>("Capsule",
                                                                    BrushMeshAssetFactory.GetModelForNode(modelBeneathCursor),
                                                                    transformation);
                capsule.definition.Reset();
                capsule.Operation      = forceOperation ?? CSGOperationType.Additive;
                capsule.Sides          = sides;
                capsule.TopSegments    = topSegments;
                capsule.BottomSegments = bottomSegments;
                capsule.TopHeight      = topHeight;
                capsule.BottomHeight   = bottomHeight;
                capsule.DiameterX      = bounds.size[(int)Axis.X];
                capsule.Height         = height;
                capsule.DiameterZ      = bounds.size[(int)Axis.Z];
                capsule.UpdateGenerator();
                break;
            }

            case BoxExtrusionState.Modified:
            {
                capsule.Operation = forceOperation ??
                                    ((height < 0 && modelBeneathCursor) ?
                                     CSGOperationType.Subtractive :
                                     CSGOperationType.Additive);
                capsule.TopHeight    = topHeight;
                capsule.BottomHeight = bottomHeight;
                capsule.DiameterX    = bounds.size[(int)Axis.X];
                capsule.Height       = height;
                capsule.DiameterZ    = bounds.size[(int)Axis.Z];
                break;
            }

            case BoxExtrusionState.Commit:
            {
                UnityEditor.Selection.activeGameObject = capsule.gameObject;
                CSGEditModeManager.EditMode            = CSGEditMode.ShapeEdit;
                Reset();
                break;
            }

            case BoxExtrusionState.Cancel:
            {
                Reset();
                Undo.RevertAllInCurrentGroup();
                EditorGUIUtility.ExitGUI();
                break;
            }

            case BoxExtrusionState.BoxMode:
            case BoxExtrusionState.SquareMode: { CSGOutlineRenderer.VisualizationMode = VisualizationMode.SimpleOutline; break; }

            case BoxExtrusionState.HoverMode: { CSGOutlineRenderer.VisualizationMode = VisualizationMode.Outline; break; }
            }

            // TODO: render capsule here
            HandleRendering.RenderCylinder(transformation, bounds, (capsule) ? capsule.Sides : sides);
        }
Beispiel #7
0
        public void OnSceneGUI(SceneView sceneView, Rect dragArea)
        {
            Bounds    bounds;
            CSGModel  modelBeneathCursor;
            Matrix4x4 transformation;
            float     height;

            var flags = BoxExtrusionFlags.AlwaysFaceUp |
                        BoxExtrusionFlags.IsSymmetricalXZ |
                        (generateFromCenterXZ ? BoxExtrusionFlags.GenerateFromCenterXZ : BoxExtrusionFlags.None);

            switch (BoxExtrusionHandle.Do(dragArea, out bounds, out height, out modelBeneathCursor, out transformation, flags, Axis.Y, snappingSteps: stepHeight))
            {
            case BoxExtrusionState.Create:
            {
                spiralStairs = BrushMeshAssetFactory.Create <CSGSpiralStairs>("Spiral Stairs",
                                                                              BrushMeshAssetFactory.GetModelForNode(modelBeneathCursor),
                                                                              transformation);
                spiralStairs.definition.Reset();
                spiralStairs.Operation     = forceOperation ?? CSGOperationType.Additive;
                spiralStairs.StepHeight    = stepHeight;
                spiralStairs.Height        = height;
                spiralStairs.OuterDiameter = bounds.size[(int)Axis.X];
                spiralStairs.OuterSegments = outerSegments;
                spiralStairs.UpdateGenerator();
                break;
            }

            case BoxExtrusionState.Modified:
            {
                spiralStairs.Operation = forceOperation ??
                                         ((height < 0 && modelBeneathCursor) ?
                                          CSGOperationType.Subtractive :
                                          CSGOperationType.Additive);
                spiralStairs.Height        = bounds.size.y;
                spiralStairs.OuterDiameter = bounds.size[(int)Axis.X];
                break;
            }

            case BoxExtrusionState.Commit:
            {
                UnityEditor.Selection.activeGameObject = spiralStairs.gameObject;
                CSGEditModeManager.EditMode            = CSGEditMode.ShapeEdit;
                Reset();
                break;
            }

            case BoxExtrusionState.Cancel:
            {
                Reset();
                Undo.RevertAllInCurrentGroup();
                EditorGUIUtility.ExitGUI();
                break;
            }

            case BoxExtrusionState.BoxMode:
            case BoxExtrusionState.SquareMode:      { CSGOutlineRenderer.VisualizationMode = VisualizationMode.SimpleOutline; break; }

            case BoxExtrusionState.HoverMode:       { CSGOutlineRenderer.VisualizationMode = VisualizationMode.Outline; break; }
            }

            HandleRendering.RenderCylinder(transformation, bounds, (spiralStairs) ? spiralStairs.OuterSegments : outerSegments);
        }