public bool UpdateRadius(TexGen surfaceTexGen, Vector3 currentSurfacePoint, bool toggleSnapping) { var handleSize = GUIStyleUtility.GetHandleSize(this.RotateCenterPoint); var vectorToCenter = currentSurfacePoint - this.RotateCenterPoint; this.RotateRadius = vectorToCenter.magnitude; vectorToCenter.Normalize(); var rotateCurrentAngle = 0.0f; if (!this.HaveRotateStartAngle) { if (this.RotateRadius > handleSize * ToolConstants.minRotateRadius) { this.HaveRotateStartAngle = true; this.RotateOriginalAngle = surfaceTexGen.RotationAngle; this.RotateStartVector = vectorToCenter; this.RotateCurrentStartAngle = GeometryUtility.SignedAngle(this.RotateSurfaceTangent, vectorToCenter, this.RotateSurfaceNormal); } this.RotateRadius = Math.Max(this.RotateRadius, handleSize * ToolConstants.minRotateRadius); } else { rotateCurrentAngle = GeometryUtility.SignedAngle(this.RotateStartVector, vectorToCenter, this.RotateSurfaceNormal); var minSize = handleSize * ToolConstants.minRotateRadius * 2; var radiusStepSize = minSize; this.RotateRadius = (Mathf.CeilToInt(((this.RotateRadius - minSize) / radiusStepSize) - 0.5f) * radiusStepSize) + minSize; } // snap texture coordinates in world/local space this.RotateCurrentSnappedAngle = GridUtility.SnappedAngle(this.RotateOriginalAngle + rotateCurrentAngle, toggleSnapping) - this.RotateOriginalAngle; return(this.HaveRotateStartAngle); }
void PaintShape(int id) { var temp = Handles.color; var origMatrix = Handles.matrix; Handles.matrix = MathConstants.identityMatrix; var rotation = Camera.current.transform.rotation; bool isValid; var realVertices = settings.GetVertices(buildPlane, worldPosition, gridTangent, gridBinormal, out isValid); if (editMode == EditMode.EditShape) { shapeIsValid = isValid; } if (realVertices != null && realVertices.Length >= 3) { var wireframeColor = ColorSettings.WireframeOutline; if (!shapeIsValid || !isValid) { wireframeColor = Color.red; } for (int i = 1; i < realVertices.Length; i++) { PaintUtility.DrawLine(realVertices[i - 1], realVertices[i], ToolConstants.oldLineScale, wireframeColor); PaintUtility.DrawDottedLine(realVertices[i - 1], realVertices[i], wireframeColor, 4.0f); } PaintUtility.DrawLine(realVertices[realVertices.Length - 1], realVertices[0], ToolConstants.oldLineScale, wireframeColor); PaintUtility.DrawDottedLine(realVertices[realVertices.Length - 1], realVertices[0], wireframeColor, 4.0f); //var color = ColorSettings.ShapeDrawingFill; //PaintUtility.DrawPolygon(MathConstants.identityMatrix, realVertices, color); } if (settings.vertices != null && settings.vertices.Length > 0) { Handles.color = ColorSettings.PointInnerStateColor[0]; for (int i = 0; i < settings.vertices.Length; i++) { float handleSize = GUIStyleUtility.GetHandleSize(settings.vertices[i]); float scaledHandleSize = handleSize * ToolConstants.handleScale; PaintUtility.SquareDotCap(id, settings.vertices[i], rotation, scaledHandleSize); } PaintSquare(); PaintBounds(); } Handles.color = ColorSettings.PointInnerStateColor[3]; { float handleSize = GUIStyleUtility.GetHandleSize(worldPosition); float scaledHandleSize = handleSize * ToolConstants.handleScale; PaintUtility.SquareDotCap(id, worldPosition, rotation, scaledHandleSize); } Handles.matrix = origMatrix; Handles.color = temp; }
void PaintShape(int id) { var rotation = Camera.current.transform.rotation; var temp = Handles.color; var origMatrix = Handles.matrix; { Handles.matrix = MathConstants.identityMatrix; Handles.color = ColorSettings.PointInnerStateColor[3]; { float handleSize = GUIStyleUtility.GetHandleSize(worldPosition); float scaledHandleSize = handleSize * ToolConstants.handleScale; PaintUtility.SquareDotCap(id, worldPosition, rotation, scaledHandleSize); } if (settings.vertices != null && settings.vertices.Length > 0) { Handles.color = ColorSettings.PointInnerStateColor[0]; for (int i = 0; i < settings.vertices.Length; i++) { float handleSize = GUIStyleUtility.GetHandleSize(settings.vertices[i]); float scaledHandleSize = handleSize * ToolConstants.handleScale; PaintUtility.SquareDotCap(id, settings.vertices[i], rotation, scaledHandleSize); } PaintSquare(); PaintBounds(); } } Handles.matrix = origMatrix; Handles.color = temp; }
protected void HandleHeightHandles(Rect sceneRect, bool showHeightValue) { for (int p = 0; p < extrusionPoints.Length; p++) { var type = Event.current.GetTypeForControl(extrusionPoints[p].ID); switch (type) { case EventType.Repaint: { if (SceneTools.IsDraggingObjectInScene) { break; } bool isSelected = extrusionPoints[p].ID == GUIUtility.keyboardControl; var temp = Handles.color; var origMatrix = Handles.matrix; Handles.matrix = MathConstants.identityMatrix; var rotation = Camera.current.transform.rotation; var state = SelectState.None; if (isSelected) { state |= SelectState.Selected; state |= SelectState.Hovering; } else if (HandleUtility.nearestControl == extrusionPoints[p].ID) { state |= SelectState.Hovering; } if (polygons != null && outlineVertices.Length >= 3) { var wireframeColor = ColorSettings.WireframeOutline; var topWireframeColor = ColorSettings.WireframeOutline; if (!shapeIsValid) { wireframeColor = Color.red; } var surfaceColor = ColorSettings.ShapeDrawingFill; if (GUIUtility.hotControl == extrusionPoints[p].ID) { topWireframeColor = ColorSettings.BoundsEdgeHover; surfaceColor = ColorSettings.PolygonInnerStateColor[(int)(SelectState.Selected | SelectState.Hovering)]; surfaceColor.a *= 0.5f; } else if (GUIUtility.hotControl == 0 && HandleUtility.nearestControl == extrusionPoints[p].ID) { topWireframeColor = ColorSettings.BoundsEdgeHover; surfaceColor = ColorSettings.PolygonInnerStateColor[(int)(SelectState.Selected)]; surfaceColor.a *= 0.5f; } var poly2dToWorldMatrix = Matrix4x4.TRS(extrusionPoints[p].Position, Quaternion.FromToRotation(MathConstants.upVector3, buildPlane.normal), MathConstants.oneVector3); for (int i = 0; i < polygons.Length; i++) { PaintUtility.DrawPolygon(poly2dToWorldMatrix, polygons[i].Vertices, surfaceColor); } poly2dToWorldMatrix = Matrix4x4.TRS(extrusionPoints[p].Position, Quaternion.identity, MathConstants.oneVector3); for (int i = 1; i < outlineVertices.Length; i++) { PaintUtility.DrawLine(poly2dToWorldMatrix, outlineVertices[i - 1], outlineVertices[i], ToolConstants.oldLineScale, topWireframeColor); PaintUtility.DrawDottedLine(poly2dToWorldMatrix, outlineVertices[i - 1], outlineVertices[i], topWireframeColor, 4.0f); } PaintUtility.DrawLine(poly2dToWorldMatrix, outlineVertices[outlineVertices.Length - 1], outlineVertices[0], ToolConstants.oldLineScale, topWireframeColor); PaintUtility.DrawDottedLine(poly2dToWorldMatrix, outlineVertices[outlineVertices.Length - 1], outlineVertices[0], topWireframeColor, 4.0f); if (p > 0) { var prevOffset = extrusionPoints[p - 1].Position; var prevPoly2dToWorldMatrix = Matrix4x4.TRS(prevOffset, Quaternion.identity, MathConstants.oneVector3); for (int i = 0; i < outlineVertices.Length; i++) { var from = prevPoly2dToWorldMatrix.MultiplyPoint(outlineVertices[i]); var to = poly2dToWorldMatrix.MultiplyPoint(outlineVertices[i]); PaintUtility.DrawLine(from, to, ToolConstants.oldLineScale, wireframeColor); PaintUtility.DrawDottedLine(from, to, wireframeColor, 4.0f); } } } var color = ColorSettings.PolygonInnerStateColor[(int)state]; if (!shapeIsValid) { color = Color.red; } var handleSize = GUIStyleUtility.GetHandleSize(extrusionPoints[p].Position); var scaledHandleSize = handleSize * ToolConstants.handleScale; if (p > 0) { PaintUtility.DrawDottedLine(extrusionPoints[p - 1].Position, extrusionPoints[p].Position, color, 4.0f); } Handles.color = color; PaintUtility.SquareDotCap(extrusionPoints[p].ID, extrusionPoints[p].Position, rotation, scaledHandleSize); var direction = haveForcedDirection ? forcedDirection : buildPlane.normal; var distance = new CSGPlane(direction, extrusionPoints[p].Position).Distance(extrusionPoints[1 - p].Position); if (distance <= MathConstants.DistanceEpsilon) { PaintUtility.DrawArrowCap(extrusionPoints[p].Position, direction, HandleUtility.GetHandleSize(extrusionPoints[p].Position)); } if (distance > -MathConstants.DistanceEpsilon) { PaintUtility.DrawArrowCap(extrusionPoints[p].Position, -direction, HandleUtility.GetHandleSize(extrusionPoints[p].Position)); } Handles.matrix = origMatrix; Handles.color = temp; if (p > 0 && showHeightValue) { var length = GetSegmentLength(extrusionPoints[p].Position, extrusionPoints[p - 1].Position, direction); PaintHeightMessage(extrusionPoints[p - 1].Position, extrusionPoints[p].Position, gridTangent, length); } break; } case EventType.Layout: { if ((Tools.viewTool != ViewTool.None && Tools.viewTool != ViewTool.Pan)) { break; } var poly2dToWorldMatrix = Matrix4x4.TRS(extrusionPoints[p].Position, Quaternion.FromToRotation(MathConstants.upVector3, buildPlane.normal), MathConstants.oneVector3); var forceOverHandle = IsMouseOverShapePolygons(poly2dToWorldMatrix); HandleUtility.AddControl(extrusionPoints[p].ID, forceOverHandle ? 3.0f : float.PositiveInfinity); var origMatrix = Handles.matrix; Handles.matrix = MathConstants.identityMatrix; float handleSize = GUIStyleUtility.GetHandleSize(extrusionPoints[p].Position); float scaledHandleSize = handleSize * ToolConstants.handleScale * handle_extension; float distanceToCircle = HandleUtility.DistanceToCircle(extrusionPoints[p].Position, scaledHandleSize); HandleUtility.AddControl(extrusionPoints[p].ID, distanceToCircle); var direction = haveForcedDirection ? forcedDirection : buildPlane.normal; var distance = new CSGPlane(direction, extrusionPoints[p].Position).Distance(extrusionPoints[1 - p].Position); if (distance <= MathConstants.DistanceEpsilon) { PaintUtility.AddArrowCapControl(extrusionPoints[p].ID, extrusionPoints[p].Position, direction, HandleUtility.GetHandleSize(extrusionPoints[p].Position)); } if (distance > -MathConstants.DistanceEpsilon) { PaintUtility.AddArrowCapControl(extrusionPoints[p].ID, extrusionPoints[p].Position, -direction, HandleUtility.GetHandleSize(extrusionPoints[p].Position)); } if (generatedGameObjects != null && generatedGameObjects.Length > 0) { for (int g = generatedGameObjects.Length - 1; g >= 0; g--) { if (generatedGameObjects[g]) { continue; } ArrayUtility.RemoveAt(ref generatedGameObjects, g); } if (generatedGameObjects == null || generatedGameObjects.Length == 0) { Cancel(); } } Handles.matrix = origMatrix; break; } case EventType.MouseDown: { if (!sceneRect.Contains(Event.current.mousePosition)) { break; } if ((Tools.viewTool != ViewTool.None && Tools.viewTool != ViewTool.Pan) || Event.current.modifiers != EventModifiers.None) { break; } if (GUIUtility.hotControl == 0 && HandleUtility.nearestControl == extrusionPoints[p].ID && Event.current.button == 0) { if (editMode != EditMode.ExtrudeShape && !StartExtrudeMode()) { Cancel(); } else { UpdateBaseShape(registerUndo: false); dragPositionStart = extrusionPoints[p].Position; GrabHeightHandle(p); BeginExtrusion(); Event.current.Use(); } } break; } case EventType.MouseDrag: case EventType.MouseMove: { if (Tools.viewTool != ViewTool.None && Tools.viewTool != ViewTool.Pan) { break; } if (GUIUtility.hotControl == extrusionPoints[p].ID) // && Event.current.button == 0) { Undo.RecordObject(this, "Extrude shape"); heightPosition += Event.current.delta; Vector3 worldPosition = GetHeightHandlePosition(extrusionPoints[p].Position) - heightHandleOffset; if (float.IsInfinity(worldPosition.x) || float.IsNaN(worldPosition.x) || float.IsInfinity(worldPosition.y) || float.IsNaN(worldPosition.y) || float.IsInfinity(worldPosition.z) || float.IsNaN(worldPosition.z)) { worldPosition = extrusionPoints[p].Position; } ResetVisuals(); if (raySnapFunction != null) { CSGBrush snappedOnBrush = null; worldPosition = raySnapFunction(worldPosition, new Ray(brushPosition, movePolygonDirection), ref visualSnappedEdges, out snappedOnBrush); visualSnappedBrush = snappedOnBrush; } visualSnappedGrid = RealtimeCSG.Grid.FindAllGridEdgesThatTouchPoint(worldPosition); extrusionPoints[p].Position = GeometryUtility.ProjectPointOnInfiniteLine(worldPosition, brushPosition, movePolygonDirection); if (p == 0) { MoveShape(extrusionPoints[0].Position - dragPositionStart); UpdateBaseShape(); } UpdateBrushPosition(); UpdateExtrudedShape(); GUI.changed = true; Event.current.Use(); break; } break; } case EventType.MouseUp: { forceDragHandle = false; if (GUIUtility.hotControl == extrusionPoints[p].ID && Event.current.button == 0 && (Tools.viewTool == ViewTool.None || Tools.viewTool == ViewTool.Pan)) { EndExtrusion(); if (firstClick) { firstClick = false; break; } GUIUtility.hotControl = 0; GUIUtility.keyboardControl = 0; EditorGUIUtility.editingTextField = false; Event.current.Use(); ResetVisuals(); CleanupGrid(); if (!HaveExtrusion) { RevertToEditVertices(); } break; } break; } } } var shapeType = Event.current.GetTypeForControl(shapeId); HandleKeyboard(shapeType); }
protected override void HandleEditShapeEvents(Rect sceneRect) { if (settings.vertices.Length < 2) { if (editMode == EditMode.ExtrudeShape || editMode == EditMode.EditShape) { editMode = EditMode.CreatePlane; } } if (!SceneTools.IsDraggingObjectInScene && Event.current.type == EventType.Repaint) { if (visualSnappedEdges != null) { PaintUtility.DrawLines(visualSnappedEdges.ToArray(), ToolConstants.oldThickLineScale, ColorSettings.SnappedEdges); } if (visualSnappedGrid != null) { var _origMatrix = Handles.matrix; Handles.matrix = MathConstants.identityMatrix; PaintUtility.DrawDottedLines(visualSnappedGrid.ToArray(), ColorSettings.SnappedEdges); Handles.matrix = _origMatrix; } if (visualSnappedBrush != null) { var brush_cache = InternalCSGModelManager.GetBrushCache(visualSnappedBrush); if (brush_cache != null && brush_cache.compareTransformation != null && brush_cache.childData != null && brush_cache.childData.ModelTransform != null && brush_cache.childData.ModelTransform) { var brush_translation = brush_cache.compareTransformation.modelLocalPosition + brush_cache.childData.ModelTransform.position; CSGRenderer.DrawOutlines(visualSnappedBrush.brushID, brush_translation, ColorSettings.SelectedOutlines, ColorSettings.SelectedOutlines, ColorSettings.SelectedOutlines, ColorSettings.SelectedOutlines); } } var origMatrix = Handles.matrix; Handles.matrix = MathConstants.identityMatrix; PaintBounds(); Handles.matrix = origMatrix; } HandleHeightHandles(sceneRect, false); for (int i = 0; i < settings.vertices.Length; i++) { var id = settings.vertexIDs[i]; var point_type = Event.current.GetTypeForControl(id); switch (point_type) { case EventType.Repaint: { if (SceneTools.IsDraggingObjectInScene) { break; } bool isSelected = id == GUIUtility.keyboardControl; var temp = Handles.color; var origMatrix = Handles.matrix; Handles.matrix = MathConstants.identityMatrix; var rotation = Camera.current.transform.rotation; if (isSelected) { Handles.color = ColorSettings.PointInnerStateColor[3]; } else if (HandleUtility.nearestControl == id) { Handles.color = ColorSettings.PointInnerStateColor[1]; } else { Handles.color = ColorSettings.PointInnerStateColor[0]; } float handleSize = GUIStyleUtility.GetHandleSize(settings.vertices[i]); float scaledHandleSize = handleSize * ToolConstants.handleScale; PaintUtility.SquareDotCap(id, settings.vertices[i], rotation, scaledHandleSize); Handles.matrix = origMatrix; Handles.color = temp; break; } case EventType.layout: { if ((Tools.viewTool != ViewTool.None && Tools.viewTool != ViewTool.Pan)) { break; } var origMatrix = Handles.matrix; Handles.matrix = MathConstants.identityMatrix; float handleSize = GUIStyleUtility.GetHandleSize(settings.vertices[i]); float scaledHandleSize = handleSize * ToolConstants.handleScale; HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(settings.vertices[i], scaledHandleSize)); Handles.matrix = origMatrix; break; } case EventType.ValidateCommand: case EventType.keyDown: { if (GUIUtility.hotControl == id) { if (Keys.CancelActionKey.IsKeyPressed()) { Event.current.Use(); break; } } break; } case EventType.keyUp: { if (GUIUtility.hotControl == id) { if (Keys.CancelActionKey.IsKeyPressed()) { GUIUtility.hotControl = 0; GUIUtility.keyboardControl = 0; EditorGUIUtility.editingTextField = false; Event.current.Use(); break; } } break; } case EventType.MouseDown: { if (!sceneRect.Contains(Event.current.mousePosition)) { break; } if (Tools.viewTool != ViewTool.None && Tools.viewTool != ViewTool.Pan) { break; } if (GUIUtility.hotControl == 0 && HandleUtility.nearestControl == id && Event.current.button == 0) { GUIUtility.hotControl = id; GUIUtility.keyboardControl = id; EditorGUIUtility.editingTextField = false; EditorGUIUtility.SetWantsMouseJumping(1); Event.current.Use(); break; } break; } case EventType.MouseDrag: { if (Tools.viewTool != ViewTool.None && Tools.viewTool != ViewTool.Pan) { break; } if (GUIUtility.hotControl == id && Event.current.button == 0) { Undo.RecordObject(this, "Modify shape"); var mouseRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); Grid.SetForcedGrid(buildPlane); var alignedPlane = new CSGPlane(Grid.CurrentWorkGridPlane.normal, settings.vertices[0]); var worldPosition = buildPlane.Project(alignedPlane.Intersection(mouseRay)); if (float.IsInfinity(worldPosition.x) || float.IsNaN(worldPosition.x) || float.IsInfinity(worldPosition.y) || float.IsNaN(worldPosition.y) || float.IsInfinity(worldPosition.z) || float.IsNaN(worldPosition.z)) { worldPosition = settings.vertices[i]; } ResetVisuals(); if (snapFunction != null) { CSGBrush snappedOnBrush; worldPosition = snapFunction(worldPosition, buildPlane, ref base.visualSnappedEdges, out snappedOnBrush, generatedBrushes); } base.visualSnappedGrid = Grid.FindAllGridEdgesThatTouchPoint(worldPosition); settings.vertices[i] = worldPosition; CenterExtrusionPoints(buildPlane); UpdateBaseShape(); if (editMode == EditMode.ExtrudeShape) { StartExtrudeMode(); } UpdateBaseShape(); UpdateExtrudedShape(); GUI.changed = true; Event.current.Use(); break; } break; } case EventType.MouseUp: { if (GUIUtility.hotControl != id) { break; } if (Tools.viewTool != ViewTool.None && Tools.viewTool != ViewTool.Pan) { break; } if (Event.current.button == 0) { GUIUtility.hotControl = 0; GUIUtility.keyboardControl = 0; EditorGUIUtility.editingTextField = false; EditorGUIUtility.SetWantsMouseJumping(0); Event.current.Use(); ResetVisuals(); //if (Length == 0 || Width == 0) //{ // Cancel(); //} break; } break; } } } }