Beispiel #1
0
        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       = CSG_HandleUtility.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       = CSG_HandleUtility.GetHandleSize(worldPosition);
                float scaledHandleSize = handleSize * ToolConstants.handleScale;
                PaintUtility.SquareDotCap(id, worldPosition, rotation, scaledHandleSize);
            }

            Handles.matrix = origMatrix;
            Handles.color  = temp;
        }
        public bool UpdateRadius(TexGen surfaceTexGen, Vector3 currentSurfacePoint)
        {
            var handleSize     = CSG_HandleUtility.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 * GUIConstants.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 * GUIConstants.minRotateRadius);
            }
            else
            {
                rotateCurrentAngle = GeometryUtility.SignedAngle(this.RotateStartVector, vectorToCenter, this.RotateSurfaceNormal);
                var minSize        = handleSize * GUIConstants.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) - this.RotateOriginalAngle;

            return(this.HaveRotateStartAngle);
        }
Beispiel #3
0
        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       = CSG_HandleUtility.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       = CSG_HandleUtility.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;
        }
		void PaintCircle(SceneView sceneView, int id)
		{
            var camera      = sceneView.camera;
            var temp		= Handles.color;
			var origMatrix	= Handles.matrix;
					
			Handles.matrix = MathConstants.identityMatrix;
			var rotation = camera.transform.rotation;

			bool isValid;
			var realVertices = settings.GetVertices(buildPlane, worldPosition, gridTangent, gridBinormal, out 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], GUIConstants.oldLineScale, wireframeColor);
					PaintUtility.DrawDottedLine(realVertices[i - 1], realVertices[i], wireframeColor, 4.0f);
				}

				PaintUtility.DrawLine(realVertices[realVertices.Length - 1], realVertices[0], GUIConstants.oldLineScale, wireframeColor);
				PaintUtility.DrawDottedLine(realVertices[realVertices.Length - 1], realVertices[0], wireframeColor, 4.0f);
				
				if (realVertices.Length >= 3)
				{
					var color = ColorSettings.ShapeDrawingFill;
					PaintUtility.DrawPolygon(MathConstants.identityMatrix, realVertices, color);
				}

				PaintSquare(camera);
			}



			if (settings.vertices != null && settings.vertices.Length > 0)
			{
				Handles.color = ColorSettings.PointInnerStateColor[0];
				for (int i = 0; i < settings.vertices.Length; i++)
				{
					float handleSize = CSG_HandleUtility.GetHandleSize(settings.vertices[i]);
					float scaledHandleSize = handleSize * GUIConstants.handleScale;
					PaintUtility.SquareDotCap(id, settings.vertices[i], rotation, scaledHandleSize);
				}
				PaintRadiusMessage(camera);
			}
						
			Handles.color = ColorSettings.PointInnerStateColor[3];
			{
				float handleSize = CSG_HandleUtility.GetHandleSize(worldPosition);
				float scaledHandleSize = handleSize * GUIConstants.handleScale;
				PaintUtility.SquareDotCap(id, worldPosition, rotation, scaledHandleSize);
			}
			
			Handles.matrix = origMatrix;
			Handles.color = temp;
		}
		protected override void HandleEditShapeEvents(SceneView sceneView, Rect sceneRect)
		{
            var camera = sceneView.camera;
            if (!SceneDragToolManager.IsDraggingObjectInScene &&
				Event.current.type == EventType.Repaint)
			{			
				if (visualSnappedEdges != null)
					PaintUtility.DrawLines(visualSnappedEdges.ToArray(), GUIConstants.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)
				{
					if (visualSnappedBrush.compareTransformation != null &&
						visualSnappedBrush.ChildData != null &&
						visualSnappedBrush.ChildData.ModelTransform)
					{
						var brush_transformation	= visualSnappedBrush.compareTransformation.localToWorldMatrix;
						CSGRenderer.DrawOutlines(visualSnappedBrush.brushNodeID, brush_transformation, ColorSettings.SelectedOutlines, ColorSettings.SelectedOutlines, ColorSettings.SelectedOutlines, ColorSettings.SelectedOutlines);
					}						
				}
				
				var origMatrix	= Handles.matrix;
				Handles.matrix = MathConstants.identityMatrix;

				bool isValid;
				var realVertices		= settings.GetVertices(buildPlane, worldPosition, gridTangent, gridBinormal, out isValid);
				if (editMode == EditMode.EditShape)
					shapeIsValid = isValid;

				var wireframeColor		= ColorSettings.WireframeOutline;
				//var topWireframeColor	= ColorSettings.BoundsEdgeHover;

				if (!shapeIsValid || !isValid)
					wireframeColor = Color.red;

				if (realVertices.Length > 0)
				{
					if (realVertices.Length >= 3)
					{
						var color = ColorSettings.ShapeDrawingFill;
						PaintUtility.DrawPolygon(MathConstants.identityMatrix, realVertices, color);
					}

					for (int i = 1; i < realVertices.Length; i++)
					{
						PaintUtility.DrawLine(realVertices[i - 1], realVertices[i], GUIConstants.oldLineScale, wireframeColor);
						PaintUtility.DrawDottedLine(realVertices[i - 1], realVertices[i], wireframeColor, 4.0f);
					}

					PaintUtility.DrawLine(realVertices[realVertices.Length - 1], realVertices[0], GUIConstants.oldLineScale, wireframeColor);
					PaintUtility.DrawDottedLine(realVertices[realVertices.Length - 1], realVertices[0], wireframeColor, 4.0f);
					/*
					if (editMode == EditMode.ExtrudeShape)
					{
						var delta = (base.extrusionPoints[1].Vertex - base.extrusionPoints[0].Vertex);
						for (int i = 1; i < realVertices.Length; i++)
						{
							PaintUtility.DrawLine(realVertices[i - 1] + delta, realVertices[i] + delta, GUIConstants.oldLineScale, topWireframeColor);
							PaintUtility.DrawDottedLine(realVertices[i - 1] + delta, realVertices[i] + delta, topWireframeColor, 4.0f);
						}

						for (int i = 0; i < realVertices.Length; i++)
						{
							PaintUtility.DrawLine(realVertices[i] + delta, realVertices[i], GUIConstants.oldLineScale, wireframeColor);
							PaintUtility.DrawDottedLine(realVertices[i] + delta, realVertices[i], wireframeColor, 4.0f);
						}

						PaintUtility.DrawLine(realVertices[realVertices.Length - 1] + delta, realVertices[0] + delta, GUIConstants.oldLineScale, topWireframeColor);
						PaintUtility.DrawDottedLine(realVertices[realVertices.Length - 1] + delta, realVertices[0] + delta, topWireframeColor, 4.0f);
					}
					*/
					PaintSquare(camera);
					PaintRadiusMessage(camera);
				}
				
				Handles.matrix = origMatrix;
			}
			
			var shapeType = Event.current.GetTypeForControl(shapeId);
			HandleKeyboard(shapeType);

			for (int i = 1; i < settings.vertices.Length; i++)
			{
				var id = settings.vertexIDs[i];
				var point_type = Event.current.GetTypeForControl(id);
				switch (point_type)
				{
					case EventType.Repaint:
					{
						if (SceneDragToolManager.IsDraggingObjectInScene)
							break;

						bool isSelected = id == GUIUtility.keyboardControl;
						var temp		= Handles.color;
						var origMatrix	= Handles.matrix;
					
						Handles.matrix = MathConstants.identityMatrix;
						var rotation = camera.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 = CSG_HandleUtility.GetHandleSize(settings.vertices[i]);
						float scaledHandleSize = handleSize * GUIConstants.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 = CSG_HandleUtility.GetHandleSize(settings.vertices[i]);
						float scaledHandleSize = handleSize * GUIConstants.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);
							var alignedPlane	= new CSGPlane(RealtimeCSG.CSGGrid.CurrentWorkGridPlane.normal, settings.vertices[0]);
							var worldPosition	= buildPlane.Project(alignedPlane.RayIntersection(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(camera, worldPosition, buildPlane, ref base.visualSnappedEdges, out snappedOnBrush, generatedBrushes, ignoreAllBrushes: true);
							}
								
							base.visualSnappedGrid = RealtimeCSG.CSGGrid.FindAllGridEdgesThatTouchPoint(camera, worldPosition);

                            settings.vertices[i] = worldPosition;

							UpdateBaseShape(true);

							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 (SphereRadius == 0)
							{
								Cancel();
							}
							break;
						}
						break;
					}
				}
				
			}
		}
		protected override void HandleEditShapeEvents(SceneView sceneView, Rect sceneRect)
		{
            var camera = sceneView.camera;

			if (settings.vertices.Length < 2)
			{
				if (editMode == EditMode.ExtrudeShape ||
					editMode == EditMode.EditShape)
					editMode = EditMode.CreatePlane;
			}

			if (!SceneDragToolManager.IsDraggingObjectInScene &&
				Event.current.type == EventType.Repaint)
			{			
				if (visualSnappedEdges != null)
					PaintUtility.DrawLines(visualSnappedEdges.ToArray(), GUIConstants.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)
				{
					if (visualSnappedBrush.compareTransformation != null &&
						visualSnappedBrush.ChildData != null &&
						visualSnappedBrush.ChildData.ModelTransform)
					{
						var brush_transformation = visualSnappedBrush.compareTransformation.localToWorldMatrix;
						CSGRenderer.DrawOutlines(visualSnappedBrush.brushNodeID, brush_transformation, ColorSettings.SelectedOutlines, ColorSettings.SelectedOutlines, ColorSettings.SelectedOutlines, ColorSettings.SelectedOutlines);
					}						
				}

				var origMatrix = Handles.matrix;
				Handles.matrix = MathConstants.identityMatrix;
				PaintBounds(camera);
				Handles.matrix = origMatrix;
			}
			
			HandleHeightHandles(sceneView, 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 (SceneDragToolManager.IsDraggingObjectInScene)
							break;

						bool isSelected = id == GUIUtility.keyboardControl;
						var temp		= Handles.color;
						var origMatrix	= Handles.matrix;
					
						Handles.matrix = MathConstants.identityMatrix;
						var rotation = camera.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 = CSG_HandleUtility.GetHandleSize(settings.vertices[i]);
						float scaledHandleSize = handleSize * GUIConstants.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 = CSG_HandleUtility.GetHandleSize(settings.vertices[i]);
						float scaledHandleSize = handleSize * GUIConstants.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);
							RealtimeCSG.CSGGrid.SetForcedGrid(camera, buildPlane);
							var alignedPlane	= new CSGPlane(RealtimeCSG.CSGGrid.CurrentWorkGridPlane.normal, settings.vertices[0]);
							var worldPosition	= buildPlane.Project(alignedPlane.RayIntersection(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(camera, worldPosition, buildPlane, ref base.visualSnappedEdges, out snappedOnBrush, generatedBrushes);
							}
								
							base.visualSnappedGrid = RealtimeCSG.CSGGrid.FindAllGridEdgesThatTouchPoint(camera, worldPosition);

							settings.vertices[i] = worldPosition;
							
							CenterExtrusionPoints(buildPlane);
							UpdateBaseShape();

							if (editMode == EditMode.ExtrudeShape)
							{
								StartExtrudeMode(camera);
								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;
					}
				}
				
			}
		}
        protected void HandleHeightHandles(SceneView sceneView, Rect sceneRect, bool showHeightValue)
        {
            var camera = sceneView.camera;

            for (int p = 0; p < extrusionPoints.Length; p++)
            {
                var type = Event.current.GetTypeForControl(extrusionPoints[p].ID);
                switch (type)
                {
                case EventType.Repaint:
                {
                    if (SceneDragToolManager.IsDraggingObjectInScene)
                    {
                        break;
                    }

                    bool isSelected = extrusionPoints[p].ID == GUIUtility.keyboardControl;
                    var  temp       = Handles.color;
                    var  origMatrix = Handles.matrix;

                    Handles.matrix = MathConstants.identityMatrix;
                    var rotation = camera.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], GUIConstants.oldLineScale, topWireframeColor);
                            PaintUtility.DrawDottedLine(poly2dToWorldMatrix, outlineVertices[i - 1], outlineVertices[i], topWireframeColor, 4.0f);
                        }
                        PaintUtility.DrawLine(poly2dToWorldMatrix, outlineVertices[outlineVertices.Length - 1], outlineVertices[0], GUIConstants.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, GUIConstants.oldLineScale, wireframeColor);
                                PaintUtility.DrawDottedLine(from, to, wireframeColor, 4.0f);
                            }
                        }
                    }

                    var color = ColorSettings.PolygonInnerStateColor[(int)state];
                    if (!shapeIsValid)
                    {
                        color = Color.red;
                    }

                    var handleSize       = CSG_HandleUtility.GetHandleSize(extrusionPoints[p].Position);
                    var scaledHandleSize = handleSize * GUIConstants.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(camera, 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       = CSG_HandleUtility.GetHandleSize(extrusionPoints[p].Position);
                    float scaledHandleSize = handleSize * GUIConstants.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(camera))
                        {
                            Cancel();
                        }
                        else
                        {
                            UpdateBaseShape(registerUndo: false);
                            dragPositionStart = extrusionPoints[p].Position;
                            GrabHeightHandle(sceneView, 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(sceneView, 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(camera, worldPosition, new Ray(brushPosition, movePolygonDirection), ref visualSnappedEdges, out snappedOnBrush);
                            visualSnappedBrush = snappedOnBrush;
                        }

                        visualSnappedGrid = RealtimeCSG.CSGGrid.FindAllGridEdgesThatTouchPoint(camera, 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;
                    //forceDragSource = null;
                    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();
                        }

                        if (commitExtrusionAfterRelease)
                        {
                            var prevGeneratedBrushes = generatedBrushes;
                            Commit(camera);
                            // did we switch to edit mode?
                            if (EditModeManager.EditMode == ToolEditMode.Edit &&
                                prevGeneratedBrushes != null)
                            {
                                EditModeManager.UpdateTool();
                                EditModeManager.UpdateSelection(true);
                                var tool = EditModeManager.ActiveTool as EditModeMeshEdit;
                                if (tool)
                                {
                                    var brush        = prevGeneratedBrushes[0];
                                    var polygonCount = brush.ControlMesh.Polygons.Length;
                                    tool.SelectPolygon(brush, polygonCount - 2);                                             // select front most polygon
                                }
                            }
                        }
                        break;
                    }
                    break;
                }
                }
            }

            var shapeType = Event.current.GetTypeForControl(shapeId);

            HandleKeyboard(shapeType);
        }
Beispiel #8
0
        protected override void HandleEditShapeEvents(Rect sceneRect)
        {
            if (!SceneDragToolManager.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;

                /*
                 * bool isValid;
                 * var realVertices		= settings.GetVertices(buildPlane, worldPosition, gridTangent, gridBinormal, out isValid);
                 * if (editMode == EditMode.EditShape)
                 *      shapeIsValid = isValid;
                 *
                 * if (realVertices.Length > 0)*/
                {
                    PaintSquare();
                    PaintRadiusMessage();
                }

                Handles.matrix = origMatrix;
            }

            HandleHeightHandles(sceneRect, false);
            for (int i = 1; i < settings.vertices.Length; i++)
            {
                var id         = settings.vertexIDs[i];
                var point_type = Event.current.GetTypeForControl(id);
                switch (point_type)
                {
                case EventType.Repaint:
                {
                    if (SceneDragToolManager.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       = CSG_HandleUtility.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       = CSG_HandleUtility.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);
                        var alignedPlane  = new CSGPlane(RealtimeCSG.CSGGrid.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 = RealtimeCSG.CSGGrid.FindAllGridEdgesThatTouchPoint(worldPosition);

                        settings.vertices[i] = worldPosition;
                        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 (RadiusA == 0)
                        {
                            Cancel();
                        }
                        break;
                    }
                    break;
                }
                }
            }
        }