Example #1
0
        public static float RotatedEdge2DHandle(float angle, Vector3 origin, float diameter, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize = 0.0f, UnitySceneExtensions.SceneHandles.CapFunction capFunction = null, Axes axes = Axes.None)
        {
            var id = GUIUtility.GetControlID(s_RotatedEdge2DHash, FocusType.Keyboard);

            return(RotatedEdge2DHandle(id, angle, origin, diameter, handleDir, slideDir1, slideDir2, handleSize, capFunction, axes));
        }
Example #2
0
        public static float RotatedEdge2DHandle(int id, float angle, Vector3 origin, float diameter, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize = 0.0f, UnitySceneExtensions.SceneHandles.CapFunction capFunction = null, Axes axes = Axes.None)
        {
            var from     = origin;
            var vector   = Quaternion.AngleAxis(angle, handleDir) * Vector3.forward;
            var to       = from + (vector * diameter);
            var position = from + (vector * (diameter * 0.5f));

            var evt = Event.current;

            switch (evt.GetTypeForControl(id))
            {
            case EventType.Layout:
            {
                UnityEditor.HandleUtility.AddControl(id, UnityEditor.HandleUtility.DistanceToLine(from, to) * 0.5f);
                break;
            }

            case EventType.Repaint:
            {
                SceneHandles.SetCursor(id, from, to);

                if (EditorGUIUtility.keyboardControl == id)
                {
                    UnityEditor.Handles.DrawAAPolyLine(3.0f, from, to);
                }
                else
                {
                    UnityEditor.Handles.DrawAAPolyLine(2.5f, from, to);
                }
                break;
            }
            }

            if (handleSize == 0.0f)
            {
                handleSize = UnityEditor.HandleUtility.GetHandleSize(position) * 0.05f;
            }


            if (evt.GetTypeForControl(id) == EventType.MouseDown &&
                GUIUtility.hotControl == 0 &&
                ((UnityEditor.HandleUtility.nearestControl == id && evt.button == 0) ||
                 (GUIUtility.keyboardControl == id && evt.button == 2)))
            {
                rotatedStartAngle  = angle;
                rotatedAngleOffset = 0.0f;
            }

            var newPosition = UnitySceneExtensions.SceneHandles.Slider2D.Do(id, to, position, Vector3.zero, handleDir, slideDir1, slideDir2, handleSize, capFunction, axes);

            if (GUIUtility.hotControl != id)
            {
                return(angle);
            }

            rotatedAngleOffset += Utilities.GeometryMath.SignedAngle(vector, (newPosition - origin).normalized, handleDir);


            // TODO: put somewhere else
            if (!Snapping.RotateSnappingActive)
            {
                return(rotatedStartAngle + rotatedAngleOffset);
            }

            var rotateSnap   = ChiselEditorSettings.RotateSnap;
            var newAngle     = rotatedStartAngle + rotatedAngleOffset;
            var snappedAngle = (int)Mathf.Round(newAngle / rotateSnap) * rotateSnap;

            return(snappedAngle);
        }
Example #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;
            }
        }
Example #4
0
        public static void PointDrawHandle(Rect dragArea, ref List <Vector3> points, out Matrix4x4 transformation, out ChiselModel modelBeneathCursor, UnitySceneExtensions.SceneHandles.CapFunction capFunction)
        {
            var id = GUIUtility.GetControlID(s_PointDrawingHash, FocusType.Keyboard);

            PointDrawing.Do(id, dragArea, ref points, out transformation, out modelBeneathCursor, capFunction);
        }
Example #5
0
        static ExtrusionState Do(int id, Rect dragArea, ref Vector3 position, Axis axis, float snappingSteps, UnitySceneExtensions.SceneHandles.CapFunction capFunction)
        {
            var transformation = UnityEditor.Handles.matrix;
            var state          = ExtrusionState.None;
            var evt            = Event.current;
            var type           = evt.GetTypeForControl(id);

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

            case EventType.ExecuteCommand:  { if (evt.commandName == PointDrawing.kSoftDeleteCommand)
                                              {
                                                  state = Cancel(evt); break;
                                              }
                                              break; }

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

            case EventType.KeyUp:                   { if (evt.keyCode == PointDrawing.kCancelKey)
                                                      {
                                                          state = Cancel(evt); break;
                                                      }
                                                      else
                                                      if (evt.keyCode == PointDrawing.kCommitKey)
                                                      {
                                                          state = Commit(evt); break;
                                                      }
                                                      break; }

            case EventType.MouseDrag:
            case EventType.MouseMove:
            {
                // If we can, make current control hot
                if (GUIUtility.hotControl == 0)
                {
                    TakeControl(id);
                    s_StartPosition        = transformation.MultiplyPoint(position);
                    s_StartMousePosition   = evt.mousePosition - evt.delta;
                    s_CurrentMousePosition = s_StartMousePosition;
                    s_Snapping1D.Initialize(evt.mousePosition,
                                            s_StartPosition,
                                            ((Vector3)transformation.GetColumn((int)axis)).normalized,
                                            snappingSteps, axis);
                }

                // If another control is hot, don't do anything
                if (GUIUtility.hotControl != id)
                {
                    break;
                }

                // necessary to get accurate mouse cursor position when wrapping around screen due to using EditorGUIUtility.SetWantsMouseJumping
                s_CurrentMousePosition += evt.delta;

                if (!s_Snapping1D.Move(s_CurrentMousePosition))
                {
                    break;
                }

                position = transformation.inverse.MultiplyPoint(SnappingUtility.Quantize(s_Snapping1D.WorldSnappedPosition));
                state    = ExtrusionState.Modified;
                break;
            }

            case EventType.Layout:
            {
                UnityEditor.HandleUtility.AddControl(id, 0.0f);
                break;
            }

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

                evt.Use();
                break;
            }

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

                state = Commit(evt);
                break;
            }
            }
            return(state);
        }
Example #6
0
 public static ExtrusionState Do(int id, Rect dragArea, ref Vector3 position, Axis axis, UnitySceneExtensions.SceneHandles.CapFunction capFunction, float?snappingSteps = null)
 {
     if (!snappingSteps.HasValue)
     {
         snappingSteps = Snapping.MoveSnappingSteps[(int)axis];
     }
     return(Do(id, dragArea, ref position, axis, snappingSteps.Value, capFunction));
 }
Example #7
0
        public static ExtrusionState DoHandle(Rect dragArea, ref Vector3 position, Axis axis, UnitySceneExtensions.SceneHandles.CapFunction capFunction = null, float?snappingSteps = null)
        {
            var id = GUIUtility.GetControlID(s_HeightSizingHash, FocusType.Keyboard);

            return(ExtrusionHandle.Do(id, dragArea, ref position, axis, capFunction, snappingSteps));
        }