示例#1
0
    public override void DrawSceneGUI()
    {
        if (springBody == null || multiEditing)
        {
            return;
        }

        //draw the hovered over spring
        if (drawIndex != -1)
        {
            JelloSpring spring = springBody.getSpring(drawIndex);
            Vector3     posA;
            Vector3     posB;

            posA = springBody.transform.TransformPoint(springBody.Shape.getVertex(spring.pointMassA));
            posB = springBody.transform.TransformPoint(springBody.Shape.getVertex(spring.pointMassB));

            Handles.color = Color.magenta;

            if (spring.lengthMultiplier != 1f)
            {
                float   dist = Vector2.Distance(posA, posB) * spring.lengthMultiplier;
                Vector3 mid  = (posA + posB) * 0.5f;
                posA = mid + (mid - posA).normalized * dist * 0.5f;
                posB = mid + (mid - posB).normalized * dist * 0.5f;
            }

            Handles.DrawLine(posA, posB);
        }

        //TODO make it remember the selected spring?
        //draw the currently selected spring
        if (editIndex != -1 && editIndex < springBody.SpringCount)
        {
            JelloSpring spring = springBody.getSpring(editIndex);
            Handles.color = Color.cyan;

            Vector3[] globalHandlePositions = new Vector3[2];
            for (int i = 0; i < handlePositions.Length; i++)
            {
                globalHandlePositions[i] = springBody.transform.TransformPoint(handlePositions[i]);
            }
            CalculateHandleSizes(handlePositions);

            bool mouseUp = false;

            if (Event.current.type == EventType.mouseUp)
            {
                mouseUp = true;
            }

            for (int i = 0; i < handlePositions.Length; i++)
            {
                handlePositions[i] = springBody.transform.InverseTransformPoint(Handles.FreeMoveHandle(springBody.transform.TransformPoint(handlePositions[i]), Quaternion.identity, handleSizes[i], Vector3.zero, Handles.CircleCap));
            }
            //handlePositions[1] = springBody.transform.InverseTransformPoint( Handles.FreeMoveHandle(springBody.transform.TransformPoint(handlePositions[1]), Quaternion.identity, HandleUtility.GetHandleSize(handlePositions[1]) * 0.15f, Vector3.zero, Handles.CircleCap));

            Handles.color = Color.magenta;
            Handles.DrawLine(springBody.transform.TransformPoint(handlePositions[0]), springBody.transform.TransformPoint(handlePositions[1]));

            if (mouseUp)
            {
                if ((Vector2)handlePositions[0] != springBody.Shape.getVertex(spring.pointMassA))
                {
                    Vector2[] points = new Vector2[springBody.Shape.VertexCount];
                    for (int i = 0; i < springBody.Shape.VertexCount; i++)
                    {
                        points[i] = springBody.Shape.getVertex(i);
                    }

                    spring.pointMassA = JelloShapeTools.FindClosestVertexOnShape(handlePositions[0], points);

                    handlePositions[0] = springBody.Shape.getVertex(spring.pointMassA);

                    spring.length = Vector2.Distance(springBody.Shape.getVertex(spring.pointMassA), springBody.Shape.getVertex(spring.pointMassB));

                    EditorUtility.SetDirty(springBody);
                }

                if ((Vector2)handlePositions[1] != springBody.Shape.getVertex(spring.pointMassB))
                {
                    Vector2[] points = new Vector2[springBody.Shape.VertexCount];
                    for (int i = 0; i < springBody.Shape.VertexCount; i++)
                    {
                        points[i] = springBody.Shape.getVertex(i);
                    }

                    spring.pointMassB = JelloShapeTools.FindClosestVertexOnShape(handlePositions[1], points);

                    handlePositions[1] = springBody.Shape.getVertex(spring.pointMassB);

                    spring.length = Vector2.Distance(springBody.Shape.getVertex(spring.pointMassA), springBody.Shape.getVertex(spring.pointMassB));

                    EditorUtility.SetDirty(springBody);
                }
            }

            Vector3 posA = springBody.transform.TransformPoint(springBody.Shape.getVertex(spring.pointMassA));
            Vector3 posB = springBody.transform.TransformPoint(springBody.Shape.getVertex(spring.pointMassB));

            if (spring.lengthMultiplier != 1f)
            {
                float   dist = Vector2.Distance(posA, posB) * spring.lengthMultiplier;
                Vector3 mid  = (posA + posB) * 0.5f;
                posA = mid + (mid - posA).normalized * dist * 0.5f;
                posB = mid + (mid - posB).normalized * dist * 0.5f;
            }

            Handles.color = Color.blue;

            Handles.DrawLine(posA, posB);
        }

        if (newSubComponentState != AddSubComponentState.inactive)
        {
            if (Event.current.isKey && Event.current.keyCode == KeyCode.Escape)
            {
                newSubComponentState = AddSubComponentState.inactive;
            }


            int controlID = GUIUtility.GetControlID(GetHashCode(), FocusType.Passive);

            if (Event.current.type == EventType.Layout)
            {
                HandleUtility.AddDefaultControl(controlID);
            }

            Handles.color = Color.red;

            Vector3 pos   = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition).origin;           //need where this ray intersects the zplane
            Plane   plane = new Plane(Vector3.forward, new Vector3(0, 0, springBody.transform.position.z));
            Ray     ray   = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
            float   dist  = 0f;
            plane.Raycast(ray, out dist);
            pos = ray.GetPoint(dist);
            Vector3 mousePosWorld = new Vector3(pos.x, pos.y, springBody.transform.position.z);

            if (newSubComponentState == AddSubComponentState.assignedFirst)
            {
                pos = springBody.transform.TransformPoint(springBody.Shape.getVertex(newSpring.pointMassA));
                Handles.CircleCap(3, pos, Quaternion.identity, HandleUtility.GetHandleSize(pos) * 0.15f);
                Handles.DrawLine(pos, mousePosWorld);

                Handles.color = Color.blue;
                Handles.CircleCap(3, mousePosWorld, Quaternion.identity, HandleUtility.GetHandleSize(mousePosWorld) * 0.15f);

                if (Event.current.type == EventType.MouseUp)
                {
                    Vector2[] points = new Vector2[springBody.Shape.VertexCount];
                    for (int i = 0; i < springBody.Shape.VertexCount; i++)
                    {
                        points[i] = springBody.Shape.getVertex(i);
                    }

                    newSpring.pointMassB = JelloShapeTools.FindClosestVertexOnShape(springBody.transform.InverseTransformPoint(mousePosWorld), points);
                    newSpring.length     = Vector2.Distance(springBody.Shape.getVertex(newSpring.pointMassA), springBody.Shape.getVertex(newSpring.pointMassB));
                    newSpring.stiffness  = springBody.DefaultCustomSpringStiffness;
                    newSpring.damping    = springBody.DefaultCustomSpringDamping;

                    editIndex = springBody.SpringCount;
                    springBody.addCustomSpring(newSpring);
                    newSubComponentState = AddSubComponentState.inactive;

                    handlePositions[0] = springBody.Shape.getVertex(newSpring.pointMassA);
                    handlePositions[1] = springBody.Shape.getVertex(newSpring.pointMassB);

                    eCustomSprings.isExpanded   = true;
                    eEdgeSprings.isExpanded     = false;
                    eInternalSprings.isExpanded = false;

                    EditorUtility.SetDirty(springBody);
                }
            }
            if (newSubComponentState == AddSubComponentState.initiated)
            {
                Handles.CircleCap(3, mousePosWorld, Quaternion.identity, HandleUtility.GetHandleSize(mousePosWorld) * 0.15f);

                if (Event.current.type == EventType.MouseUp)
                {
                    Vector2[] points = new Vector2[springBody.Shape.VertexCount];
                    for (int i = 0; i < springBody.Shape.VertexCount; i++)
                    {
                        points[i] = springBody.Shape.getVertex(i);
                    }

                    newSpring            = new JelloSpring();
                    newSpring.pointMassA = JelloShapeTools.FindClosestVertexOnShape(springBody.transform.InverseTransformPoint(mousePosWorld), points);
                    newSubComponentState = AddSubComponentState.assignedFirst;
                }
            }

            SceneView.RepaintAll();
        }

        if (newSubComponentState != AddSubComponentState.inactive || editIndex != -1)
        {
            Handles.color = new Color(0.5f, 0.5f, 0.5f, 0.5f);

            for (int i = springBody.EdgeSpringCount; i < springBody.SpringCount; i++)
            {
                if (editIndex == i)
                {
                    continue;
                }
                Handles.DrawLine(springBody.transform.TransformPoint(springBody.Shape.getVertex(springBody.getSpring(i).pointMassA)), springBody.transform.TransformPoint(springBody.Shape.getVertex(springBody.getSpring(i).pointMassB)));
            }

            mainEditor.DrawPointMasses(springBody, false);
        }
    }
	//TODO draw some info for the other body as well? like an outline?
	public void DrawjointSceneGUI()
	{
		mainEditor.DrawPointMasses(body, false);

		Vector3 pos;
		
		//hovered over joint
		if(drawIndex >= 0 && drawIndex < body.JointCount && body.GetJoint(drawIndex).bodyA != null)
		{
			JelloJoint joint = body.GetJoint(drawIndex);

			Vector3 posA = body.transform.TransformPoint(joint.GetAnchorPointA(true));
			posA.z = body.transform.position.z;

			Handles.color = Color.magenta;
			for(int i = 0; i < joint.affectedIndicesA.Length; i++)
			{
				Handles.DrawLine(posA, body.transform.TransformPoint(body.Shape.getVertex(joint.affectedIndicesA[i])));
				Handles.DotCap(3, 
				               body.transform.TransformPoint(body.Shape.getVertex(joint.affectedIndicesA[i])), 
				               Quaternion.identity, 
				               HandleUtility.GetHandleSize(body.transform.TransformPoint(body.Shape.getVertex(joint.affectedIndicesA[i]))) * 0.05f);
			}
			Handles.color = Color.blue;
			Handles.DotCap(3, posA, Quaternion.identity, HandleUtility.GetHandleSize(posA) * 0.075f);

			Vector3 posB = joint.GetAnchorPointB(true);



			if(joint.TransformB !=  null)
			{
				posB = joint.TransformB.TransformPoint(posB);
				
				if(joint.bodyB != null)
				{
					if(joint.affectedIndicesB != null)
					{


						Handles.color = Color.magenta;
						for(int i = 0; i < body.GetJoint(drawIndex).affectedIndicesB.Length; i++)
						{
							Handles.DrawLine(posB, joint.bodyB.transform.TransformPoint(joint.bodyB.Shape.getVertex(joint.affectedIndicesB[i])));
							Handles.DotCap(3, 
							               joint.bodyB.transform.TransformPoint(joint.bodyB.Shape.getVertex(joint.affectedIndicesB[i])), 
							               Quaternion.identity, 
							               HandleUtility.GetHandleSize(joint.bodyB.transform.TransformPoint(joint.bodyB.Shape.getVertex(joint.affectedIndicesB[i]))) * 0.05f);
						}
						Handles.color = Color.blue;
					}
				}
				
				
			}
			
			Handles.DotCap(3, posB, Quaternion.identity, HandleUtility.GetHandleSize(posB) * 0.075f);
			
			Handles.color = Color.red;
			Handles.DrawLine(posA, posB);
		}
		
		//selected joint
		if(editIndex >= 0 && editIndex < body.JointCount && body.GetJoint(editIndex).affectedIndicesA != null)
		{	
			JelloJoint joint = body.GetJoint(editIndex);


			int num = 2 + joint.affectedIndicesA.Length;
			if(joint.TransformB != null && joint.bodyB != null)
				num += joint.affectedIndicesB.Length;

			//first get handle sizes...
			//need global handle positions
			Vector3[] globalHandlePositions = new Vector3[num];

			globalHandlePositions[0] = body.transform.TransformPoint(handlePositions[0]);
			for(int i = 0; i < joint.affectedIndicesA.Length; i++)
				globalHandlePositions[i + 2] = body.transform.TransformPoint(handlePositions[i + 2]);
			
			
			if(joint.TransformB != null)
			{
				globalHandlePositions[1] = joint.TransformB.TransformPoint(handlePositions[1]);

				if(joint.bodyB != null)
				{
					for(int i = 0; i < joint.affectedIndicesB.Length; i++)
					{
						globalHandlePositions[i + 2 + joint.affectedIndicesA.Length] = joint.TransformB.TransformPoint(handlePositions[i + 2 + joint.affectedIndicesA.Length]);
					}
				}
			}
			else
			{
				globalHandlePositions[1] = joint.globalAnchorB;
			}

			CalculateHandleSizes(globalHandlePositions);
			
			bool mouseUp = false;
			
			if(Event.current.type == EventType.mouseUp)
				mouseUp = true;
			
			Handles.color = Color.cyan;
			
			EditorGUI.BeginChangeCheck();
			handlePositions[0] = body.transform.InverseTransformPoint( Handles.FreeMoveHandle(body.transform.TransformPoint(handlePositions[0]), Quaternion.identity, handleSizes[0], Vector3.zero, Handles.CircleCap));
			if(EditorGUI.EndChangeCheck())
			{
				Vector2[] affectedPoints = new Vector2[joint.affectedIndicesA.Length];
				for(int i = 0; i < affectedPoints.Length; i++)
					affectedPoints[i] = body.Shape.getVertex(joint.affectedIndicesA[i]);
				
				joint.RebuildAnchor
					(
						handlePositions[0], 
						true, 
						true,
						joint.affectedIndicesA,
						affectedPoints
						);
				SetEditIndex(editIndex);

				EditorUtility.SetDirty(body);

				SceneView.RepaintAll();
			}

			for(int i = 0; i < joint.affectedIndicesA.Length; i++)
			{
				Handles.color = Color.blue;
				handlePositions[i + 2] = body.transform.InverseTransformPoint(Handles.FreeMoveHandle(body.transform.TransformPoint(handlePositions[i + 2]), 
				                                                                                Quaternion.identity, 
				                                                                                handleSizes[i + 2],
				                                                                                Vector3.zero,  
				                                                                                Handles.CircleCap));
				
				if(mouseUp)
				{
					if((Vector2)handlePositions[i + 2] != body.Shape.getVertex(joint.affectedIndicesA[i]))
					{
						Vector2[] points = new Vector2[body.Shape.VertexCount];
						for(int s = 0; s < body.Shape.VertexCount; s++)
							points[s] = body.Shape.getVertex(s);
						
						int index = JelloShapeTools.FindClosestVertexOnShape(handlePositions[i + 2], points);
						bool indexInUse = false;
						
						for(int u = 0; u < joint.affectedIndicesA.Length; u++)
							if(index == joint.affectedIndicesA[u])
								indexInUse = true;
						
						if(!indexInUse)
						{
							joint.affectedIndicesA[i] = index;
							
							Vector2[] affectedVertices = new Vector2[joint.affectedIndicesA.Length];
							for(int v = 0; v < affectedVertices.Length; v++)
								affectedVertices[v] = body.Shape.getVertex(joint.affectedIndicesA[v]); 
							
							handlePositions[i + 2] = body.Shape.getVertex(index);
							
							
							joint.RebuildAnchor(joint.localAnchorA, true, true, null, affectedVertices);

							Vector2 newPosition = Vector2.zero;
							for(int v = 0; v < affectedVertices.Length; v++)
								newPosition += affectedVertices[v] * joint.scalarsA[v];

							handlePositions[0] = newPosition;

							EditorUtility.SetDirty(body);

							SceneView.RepaintAll();
						}
						else
						{
							handlePositions[i + 2] = body.Shape.getVertex(joint.affectedIndicesA[i]);
						}
					}
				}
				
				Handles.color = Color.black;
				Handles.DrawLine(body.transform.TransformPoint(handlePositions[0]), body.transform.TransformPoint( handlePositions[i + 2]));
			}
			
			//other object's GUI
			Handles.color = Color.magenta;
			
			if(joint.TransformB != null)
			{
				EditorGUI.BeginChangeCheck();
				handlePositions[1] = joint.TransformB.InverseTransformPoint( Handles.FreeMoveHandle(joint.TransformB.TransformPoint(handlePositions[1]), Quaternion.identity, handleSizes[1], Vector3.zero, Handles.CircleCap));
				if(EditorGUI.EndChangeCheck())
				{
					Vector2[] affectedPoints = new Vector2[0];
					
					if(joint.bodyB != null)
					{
						affectedPoints = new Vector2[joint.affectedIndicesB.Length];
						for(int i = 0; i < affectedPoints.Length; i++)
							affectedPoints[i] = joint.bodyB.Shape.getVertex(joint.affectedIndicesB[i]);
					}
					
					joint.RebuildAnchor
						(
							handlePositions[1], 
							false,
							true,
							joint.affectedIndicesB,
							affectedPoints
							);
					
					SetEditIndex(editIndex);

					EditorUtility.SetDirty(body);

					SceneView.RepaintAll();
				}

				if(joint.bodyB != null && joint.affectedIndicesB != null)
				{	
					int numAffectedA = joint.affectedIndicesA.Length;
					for(int i = 0; i < joint.affectedIndicesB.Length; i++)
					{
						int offsetIndex = i + numAffectedA + 2;
						
						Handles.color = Color.red;
						handlePositions[offsetIndex] = joint.TransformB.InverseTransformPoint(Handles.FreeMoveHandle(joint.TransformB.TransformPoint(handlePositions[offsetIndex]), 
						                                                                                                  Quaternion.identity, 
						                                                                                                  handleSizes[offsetIndex], 
						                                                                                                  Vector3.zero, 
						                                                                                                  Handles.CircleCap));

						if(mouseUp)
						{
							if((Vector2)handlePositions[offsetIndex] != joint.bodyB.Shape.getVertex(joint.affectedIndicesB[i]))
							{
								Vector2[] points = new Vector2[joint.bodyB.Shape.VertexCount];
								for(int s = 0; s < joint.bodyB.Shape.VertexCount; s++)
									points[s] = joint.bodyB.Shape.getVertex(s);
								
								int index = JelloShapeTools.FindClosestVertexOnShape(handlePositions[offsetIndex], points);
								bool indexInUse = false;
								
								for(int u = 0; u < joint.affectedIndicesB.Length; u++)
									if(index == joint.affectedIndicesB[u])
										indexInUse = true;
								
								
								if(!indexInUse)
								{
									joint.affectedIndicesB[i] = index;
									
									Vector2[] affectedVertices = new Vector2[joint.affectedIndicesB.Length];
									for(int v = 0; v < affectedVertices.Length; v++)
										affectedVertices[v] = joint.bodyB.Shape.getVertex(joint.affectedIndicesB[v]); 
									
									handlePositions[offsetIndex] = joint.bodyB.Shape.getVertex(index);
									

									joint.RebuildAnchor(joint.localAnchorB, false, true, null, affectedVertices);

									Vector2 newPosition = Vector2.zero;
									for(int v = 0; v < affectedVertices.Length; v++)
										newPosition += affectedVertices[v] * joint.scalarsB[v];
									handlePositions[1] = newPosition;

									EditorUtility.SetDirty(body);

									SceneView.RepaintAll();
								}
								else
								{
									handlePositions[offsetIndex] = joint.bodyB.Shape.getVertex(joint.affectedIndicesB[i]);
								}
							}
						}
						
						Handles.color = Color.grey;
						Handles.DrawLine(joint.bodyB.transform.TransformPoint(handlePositions[1]), joint.bodyB.transform.TransformPoint( handlePositions[offsetIndex]));
					}
				}
				
				Handles.color = Color.yellow;
				Handles.DrawLine(joint.TransformA.TransformPoint(handlePositions[0]), joint.TransformB.TransformPoint(handlePositions[1]));
			}
			else
			{
				//TODO this should be handlepositions[1]???
				EditorGUI.BeginChangeCheck();
				joint.globalAnchorB = Handles.FreeMoveHandle(joint.globalAnchorB, Quaternion.identity, handleSizes[1], Vector3.zero, Handles.CircleCap);
				if(EditorGUI.EndChangeCheck())
				{
					EditorUtility.SetDirty(body);
				}
				Handles.color = Color.yellow;
				Handles.DrawLine(joint.TransformA.TransformPoint(handlePositions[0]), joint.globalAnchorB);
			}
		}
		
		//add new joint logic
		if(newSubComponentState == AddSubComponentState.initiated)
		{
			int controlID = GUIUtility.GetControlID(GetHashCode(), FocusType.Passive);
			
			if(Event.current.type == EventType.Layout)
				HandleUtility.AddDefaultControl(controlID);
			
			pos = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition).origin; //need where this ray intersects the zplane
			Plane plane = new Plane(Vector3.forward, new Vector3(0, 0, body.transform.position.z));
			Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
			float dist = 0f;
			plane.Raycast(ray, out dist);
			pos = ray.GetPoint(dist);
			Vector3 mousePosWorld = new Vector3(pos.x, pos.y, body.transform.position.z);
			
			Handles.color = Color.blue;
			
			Handles.CircleCap(3, mousePosWorld, Quaternion.identity, HandleUtility.GetHandleSize(mousePosWorld) * 0.15f);
			
			if(Event.current.type == EventType.MouseUp)
			{
				body.AddJoint(new JelloJoint(body.transform, null, body.transform.InverseTransformPoint(mousePosWorld), Vector2.zero, true, true));
				newSubComponentState = AddSubComponentState.inactive;
				SetEditIndex(body.JointCount - 1);

				EditorUtility.SetDirty(body);
			}
			
			SceneView.RepaintAll();
		}
	}
	public void DrawAttachPointSceneGUI()
	{
		//TODO i have an error when reverting to prefab. If the handles are ind a different place, it sets dirty and messes up the seleced edit attach point.
		mainEditor.DrawPointMasses(body, false);

		Vector3 pos;
		
		//the attach point hovered over in the editor
		if(drawIndex >= 0 && drawIndex < body.AttachPointCount && body.GetAttachPoint(drawIndex).body != null)
		{
			pos = body.transform.TransformPoint(body.GetAttachPoint(drawIndex).point);

			Handles.color = Color.magenta;
			for(int i = 0; i < body.GetAttachPoint(drawIndex).affectedIndices.Length; i++)
			{
				Handles.DrawLine(pos, body.transform.TransformPoint(body.Shape.getVertex(body.GetAttachPoint(drawIndex).affectedIndices[i])));
				Handles.DotCap(3, 
				               body.transform.TransformPoint(body.Shape.getVertex(body.GetAttachPoint(drawIndex).affectedIndices[i])), 
				               Quaternion.identity, 
				               HandleUtility.GetHandleSize(body.transform.TransformPoint(body.Shape.getVertex(body.GetAttachPoint(drawIndex).affectedIndices[i]))) * 0.05f);
			}
			Handles.color = Color.blue;
			Handles.DotCap(3, pos, Quaternion.identity, HandleUtility.GetHandleSize(pos) * 0.075f);
		}
		
		//the attach point currently selected in the editor
		if(editIndex != -1 && editIndex < body.AttachPointCount && body.GetAttachPoint(editIndex).body != null)
		{	
			//handle sizes
			int num = body.GetAttachPoint(editIndex).affectedIndices.Length + 1;
			Vector3[] globalHandlePositions = new Vector3[num];
			globalHandlePositions[0] = body.transform.TransformPoint(handlePositions[0]);
			for(int i = 0; i < body.GetAttachPoint(editIndex).affectedIndices.Length; i++)
				globalHandlePositions[i + 1] = body.transform.TransformPoint(handlePositions[i + 1]);

			CalculateHandleSizes(globalHandlePositions);

			bool mouseUp = false;
			
			if(Event.current.type == EventType.mouseUp)
				mouseUp = true;
			
			Handles.color = Color.green;
			EditorGUI.BeginChangeCheck();
			handlePositions[0] = body.transform.InverseTransformPoint( Handles.FreeMoveHandle(body.transform.TransformPoint(handlePositions[0]), Quaternion.identity, handleSizes[0], Vector3.zero, Handles.CircleCap));
			if(EditorGUI.EndChangeCheck())
			{
				body.GetAttachPoint(editIndex).Rebuild(handlePositions[0], body, body.GetAttachPoint(editIndex).affectedIndices);
				SetEditIndex(editIndex);

				EditorUtility.SetDirty(body);
				//modified in scene
			}
		
			Handles.color = Color.white;
			Handles.DotCap(3, body.transform.TransformPoint(handlePositions[0]), Quaternion.identity, handleSizes[0] * 0.5f);
			//start at one because the point occupies the first position.
			//for(int i = 1; i < handlePositions.Length; i++)
			for(int i = 1; i < body.GetAttachPoint(editIndex).affectedIndices.Length + 1; i++)
			{
				Handles.color = Color.blue;
				handlePositions[i] = body.transform.InverseTransformPoint( Handles.FreeMoveHandle(body.transform.TransformPoint(handlePositions[i]), Quaternion.identity, handleSizes[i], Vector3.zero, Handles.CircleCap));
			
				if(mouseUp)
				{
					if((Vector2)handlePositions[i] != body.Shape.getVertex(body.GetAttachPoint(editIndex).affectedIndices[i - 1]))
					{
						Vector2[] points = new Vector2[body.Shape.VertexCount];
						for(int s = 0; s < body.Shape.VertexCount; s++)
							points[s] = body.Shape.getVertex(s);
						
						int index = JelloShapeTools.FindClosestVertexOnShape(handlePositions[i], points);

						bool occupied = false;
						for(int n = 0; n < body.GetAttachPoint(editIndex).affectedIndices.Length; n++)
							if(index == body.GetAttachPoint(editIndex).affectedIndices[n])
								occupied = true;

						if(!occupied)
						{
							body.GetAttachPoint(editIndex).affectedIndices[i - 1] = index;
							
							handlePositions[i] = body.Shape.getVertex(index);
							
							body.GetAttachPoint(editIndex).Rebuild(body.GetAttachPoint(editIndex).point, body, body.GetAttachPoint(editIndex).affectedIndices);

							handlePositions[0] = body.GetAttachPoint(editIndex).point;

							EditorUtility.SetDirty(body);
						}
						else
						{
							handlePositions[i] = body.Shape.getVertex(body.GetAttachPoint(editIndex).affectedIndices[i - 1]);
						}
					}
				}

				Handles.color = Color.black;
				Handles.DrawLine(body.transform.TransformPoint(handlePositions[0]), body.transform.TransformPoint( handlePositions[i]));
			}
		}
		
		//logic to add a new attach point
		if(newSubComponentState == AddSubComponentState.initiated)
		{
			int controlID = GUIUtility.GetControlID(GetHashCode(), FocusType.Passive);
			
			if(Event.current.type == EventType.Layout)
				HandleUtility.AddDefaultControl(controlID);
			
			pos = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition).origin; //need where this ray intersects the zplane
			Plane plane = new Plane(Vector3.forward, new Vector3(0, 0, body.transform.position.z));
			Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
			float dist = 0f;
			plane.Raycast(ray, out dist);
			pos = ray.GetPoint(dist);
			Vector3 mousePosWorld = new Vector3(pos.x, pos.y, body.transform.position.z);
			
			Handles.color = Color.blue;
			
			Handles.CircleCap(3, mousePosWorld, Quaternion.identity, HandleUtility.GetHandleSize(mousePosWorld) * 0.15f);
			
			if(Event.current.type == EventType.MouseUp)//TODO not working correctly...?
			{
				body.AddAttachPoint(new JelloAttachPoint(body.transform.InverseTransformPoint(mousePosWorld), body, true));
				newSubComponentState = AddSubComponentState.inactive;
				SetEditIndex(body.AttachPointCount - 1);

				EditorUtility.SetDirty(body);
			}
			
			SceneView.RepaintAll();
		}
	}