/// <summary> /// Inserts a point into the path, automatically determining insert index using Ferr2DT_Path.GetClosestSeg /// </summary> /// <returns>The index of the point that was just added.</returns> /// <param name="aPt">A 2D point to add to the path.</param> public int AddAutoPoint(Vector2 aPt) { if (path == null) { path = GetComponent <Ferr2D_Path>(); } int at = path.GetClosestSeg(aPt); return(AddPoint(aPt, at + 1 == path.pathVerts.Count ? -1 : at + 1)); }
private void DoShiftAdd (Ferr2D_Path path, GUIStyle iconStyle) { Vector3 snap = Event.current.control ? new Vector3(EditorPrefs.GetFloat("MoveSnapX"), EditorPrefs.GetFloat("MoveSnapY"), EditorPrefs.GetFloat("MoveSnapZ")) : Vector3.zero; Ferr2DT_PathTerrain terrain = path.gameObject.GetComponent<Ferr2DT_PathTerrain>(); Transform transform = path.transform; Transform camTransform = SceneView.lastActiveSceneView.camera.transform; Vector3 pos = transform.InverseTransformPoint( GetMousePos(Event.current.mousePosition, transform) ); bool hasDummy = path.pathVerts.Count <= 0; if (hasDummy) path.pathVerts.Add(Vector2.zero); int closestID = path.GetClosestSeg(pos); int secondID = closestID + 1 >= path.Count ? 0 : closestID + 1; float firstDist = Vector2.Distance(pos, path.pathVerts[closestID]); float secondDist = Vector2.Distance(pos, path.pathVerts[secondID]); Vector3 local = pos; if (Event.current.control && Ferr2DT_Menu.SnapMode == Ferr2DT_Menu.SnapType.SnapLocal ) local = SnapVector(local, snap); Vector3 global = transform.TransformPoint(pos); if (Event.current.control && Ferr2DT_Menu.SnapMode == Ferr2DT_Menu.SnapType.SnapGlobal) global = SnapVector(global, snap); Handles.color = Color.white; if (!(secondID == 0 && !path.closed && firstDist > secondDist)) { Handles.DrawLine( global, transform.TransformPoint(path.pathVerts[closestID])); } if (!(secondID == 0 && !path.closed && firstDist < secondDist)) { Handles.DrawLine( global, transform.TransformPoint(path.pathVerts[secondID])); } Handles.color = new Color(1, 1, 1, 1); Vector3 handlePos = transform.TransformPoint(pos); float scale = HandleScale(handlePos); if (Handles.Button(handlePos, camTransform.rotation, scale, scale, CapDotPlus)) { Vector3 finalPos = transform.InverseTransformPoint(global); if (secondID == 0) { if (firstDist < secondDist) { if (terrain) terrain.AddPoint(finalPos); else path.pathVerts.Add(finalPos); } else { if (terrain) terrain.AddPoint(finalPos, 0); else path.pathVerts.Insert(0, finalPos); } } else { if (terrain) terrain.AddPoint(finalPos, Mathf.Max(closestID, secondID)); else path.pathVerts.Insert(Mathf.Max(closestID, secondID), finalPos); } selectedPoints.Clear(); GUI.changed = true; } if (hasDummy) path.pathVerts.RemoveAt(0); }
private void DoShiftAdd(Ferr2D_Path path, GUIStyle iconStyle) { Vector3 snap = Event.current.control ? new Vector3(EditorPrefs.GetFloat("MoveSnapX"), EditorPrefs.GetFloat("MoveSnapY"), EditorPrefs.GetFloat("MoveSnapZ")) : Vector3.zero; Ferr2DT_PathTerrain terrain = path.gameObject.GetComponent <Ferr2DT_PathTerrain>(); Transform transform = path.transform; Transform camTransform = SceneView.lastActiveSceneView.camera.transform; Vector3 pos = transform.InverseTransformPoint(GetMousePos(Event.current.mousePosition, transform)); bool hasDummy = path.pathVerts.Count <= 0; if (hasDummy) { path.pathVerts.Add(Vector2.zero); } int closestID = path.GetClosestSeg(pos); int secondID = closestID + 1 >= path.Count ? 0 : closestID + 1; float firstDist = Vector2.Distance(pos, path.pathVerts[closestID]); float secondDist = Vector2.Distance(pos, path.pathVerts[secondID]); Vector3 local = pos; if (Event.current.control && Ferr2DT_Menu.SnapMode == Ferr2DT_Menu.SnapType.SnapLocal) { local = SnapVector(local, snap); } Vector3 global = transform.TransformPoint(pos); if (Event.current.control && Ferr2DT_Menu.SnapMode == Ferr2DT_Menu.SnapType.SnapGlobal) { global = SnapVector(global, snap); } Handles.color = Color.white; if (!(secondID == 0 && !path.closed && firstDist > secondDist)) { Handles.DrawLine(global, transform.TransformPoint(path.pathVerts[closestID])); } if (!(secondID == 0 && !path.closed && firstDist < secondDist)) { Handles.DrawLine(global, transform.TransformPoint(path.pathVerts[secondID])); } Handles.color = new Color(1, 1, 1, 1); Vector3 handlePos = transform.TransformPoint(pos); float scale = HandleScale(handlePos); if (Handles.Button(handlePos, camTransform.rotation, scale, scale, CapDotPlus)) { Vector3 finalPos = transform.InverseTransformPoint(global); if (secondID == 0) { if (firstDist < secondDist) { terrain.AddPoint(finalPos); } else { terrain.AddPoint(finalPos, 0); } } else { terrain.AddPoint(finalPos, Mathf.Max(closestID, secondID)); } selectedPoints.Clear(); EditorUtility.SetDirty(terrain); GUI.changed = true; } if (hasDummy) { path.pathVerts.RemoveAt(0); } }
private void DoShiftAdd(Ferr2D_Path path, GUIStyle iconStyle) { Ferr2DT_PathTerrain terrain = path.gameObject.GetComponent <Ferr2DT_PathTerrain>(); Quaternion inv = Quaternion.Inverse(path.transform.rotation); Vector2 pos = GetMousePos(Event.current.mousePosition, path.transform.position.z) - new Vector2(path.transform.position.x, path.transform.position.y); bool hasDummy = path.pathVerts.Count <= 0; if (hasDummy) { path.pathVerts.Add(Vector2.zero); } int closestID = path.GetClosestSeg(inv * new Vector2(pos.x / path.transform.localScale.x, pos.y / path.transform.localScale.y)); int secondID = closestID + 1 >= path.Count ? 0 : closestID + 1; float firstDist = Vector2.Distance(pos, path.pathVerts[closestID]); float secondDist = Vector2.Distance(pos, path.pathVerts[secondID]); Vector3 local = pos; if (Event.current.control && Ferr_Menu.SnapMode == Ferr2DT_SnapMode.SnapLocal) { local = SnapVector(local); } Vector3 global = path.transform.position + local; if (Event.current.control && Ferr_Menu.SnapMode == Ferr2DT_SnapMode.SnapGlobal) { global = SnapVector(global); } Handles.color = Color.white; if (!(secondID == 0 && !path.closed && firstDist > secondDist)) { Handles.DrawLine( global, path.transform.position + path.transform.rotation * Vector3.Scale(new Vector3(path.pathVerts[closestID].x, path.pathVerts[closestID].y, 0), path.transform.localScale)); } if (!(secondID == 0 && !path.closed && firstDist < secondDist)) { Handles.DrawLine( global, path.transform.position + path.transform.rotation * Vector3.Scale(new Vector3(path.pathVerts[secondID].x, path.pathVerts[secondID].y, 0), path.transform.localScale)); } Handles.color = new Color(1, 1, 1, 0); Vector3 handlePos = new Vector3(pos.x, pos.y, 0) + path.transform.position + offset; if (IsVisible(handlePos)) { if (Handles.Button(handlePos, SceneView.lastActiveSceneView.camera.transform.rotation, HandleScale(handlePos), HandleScale(handlePos), Handles.CircleCap)) { Vector3 finalPos = inv * (new Vector3(global.x / path.transform.localScale.x, global.y / path.transform.localScale.y, 0) - path.transform.position); if (secondID == 0) { if (firstDist < secondDist) { if (terrain) { terrain.AddPoint(finalPos); } else { path.pathVerts.Add(finalPos); } } else { if (terrain) { terrain.AddPoint(finalPos, 0); } else { path.pathVerts.Insert(0, finalPos); } } } else { if (terrain) { terrain.AddPoint(finalPos, Mathf.Max(closestID, secondID)); } else { path.pathVerts.Insert(Mathf.Max(closestID, secondID), finalPos); } } if (selectedPoints != null) { selectedPoints.Clear(); } GUI.changed = true; } SetScale(handlePos, texDotPlus, ref iconStyle); Handles.Label(handlePos, new GUIContent(texDotPlus), iconStyle); } if (hasDummy) { path.pathVerts.RemoveAt(0); } }
private void DoShiftAdd(Ferr2D_Path path, GUIStyle iconStyle) { Ferr2DT_PathTerrain terrain = path.gameObject.GetComponent<Ferr2DT_PathTerrain>(); Quaternion inv = Quaternion.Inverse(path.transform.rotation); Vector2 pos = GetMousePos(Event.current.mousePosition, path.transform.position.z) - new Vector2(path.transform.position.x, path.transform.position.y); bool hasDummy = path.pathVerts.Count <= 0; if (hasDummy) path.pathVerts.Add(Vector2.zero); int closestID = path.GetClosestSeg(inv * new Vector2(pos.x / path.transform.localScale.x, pos.y / path.transform.localScale.y)); int secondID = closestID + 1 >= path.Count ? 0 : closestID + 1; float firstDist = Vector2.Distance(pos, path.pathVerts[closestID]); float secondDist = Vector2.Distance(pos, path.pathVerts[secondID]); Vector3 local = pos; if (Event.current.control && Ferr_Menu.SnapMode == Ferr2DT_SnapMode.SnapLocal ) local = SnapVector(local ); Vector3 global = path.transform.position + local; if (Event.current.control && Ferr_Menu.SnapMode == Ferr2DT_SnapMode.SnapGlobal) global = SnapVector(global); Handles.color = Color.white; if (!(secondID == 0 && !path.closed && firstDist > secondDist)) { Handles.DrawLine( global, path.transform.position + path.transform.rotation * Vector3.Scale(new Vector3(path.pathVerts[closestID].x, path.pathVerts[closestID].y, 0), path.transform.localScale)); } if (!(secondID == 0 && !path.closed && firstDist < secondDist)) { Handles.DrawLine( global, path.transform.position + path.transform.rotation * Vector3.Scale(new Vector3(path.pathVerts[secondID].x, path.pathVerts[secondID].y, 0), path.transform.localScale)); } Handles.color = new Color(1, 1, 1, 0); Vector3 handlePos = new Vector3(pos.x, pos.y, 0) + path.transform.position + offset; if (IsVisible(handlePos)) { if (Handles.Button(handlePos, SceneView.lastActiveSceneView.camera.transform.rotation, HandleScale(handlePos), HandleScale(handlePos), Handles.CircleCap)) { Vector3 finalPos = inv * (new Vector3(global.x / path.transform.localScale.x, global.y / path.transform.localScale.y, 0) - path.transform.position); if (secondID == 0) { if (firstDist < secondDist) { if (terrain) terrain.AddPoint(finalPos); else path.pathVerts.Add(finalPos); } else { if (terrain) terrain.AddPoint(finalPos, 0); else path.pathVerts.Insert(0, finalPos); } } else { if (terrain) terrain.AddPoint(finalPos, Mathf.Max(closestID, secondID)); else path.pathVerts.Insert(Mathf.Max(closestID, secondID), finalPos); } selectedPoints.Clear(); GUI.changed = true; } SetScale(handlePos, texDotPlus, ref iconStyle); Handles.Label(handlePos, new GUIContent(texDotPlus), iconStyle); } if (hasDummy) path.pathVerts.RemoveAt(0); }
private void DoShiftAdd(Ferr2D_Path path, GUIStyle iconStyle) { #if FERR2D_TERRAIN Ferr2DT_PathTerrain terrain = path.gameObject.GetComponent<Ferr2DT_PathTerrain>(); #endif Quaternion inv = Quaternion.Inverse(path.transform.rotation); Vector2 pos = GetMousePos(Event.current.mousePosition, path.transform.position.z) - new Vector2(path.transform.position.x, path.transform.position.y); bool hasDummy = path.pathVerts.Count <= 0; if (hasDummy) path.pathVerts.Add(Vector2.zero); int closestID = path.GetClosestSeg(inv * new Vector2(pos.x / path.transform.localScale.x, pos.y / path.transform.localScale.y)); int secondID = closestID + 1 >= path.Count ? 0 : closestID + 1; float firstDist = Vector2.Distance(pos, path.pathVerts[closestID]); float secondDist = Vector2.Distance(pos, path.pathVerts[secondID]); Vector3 local = pos; if (Ferr_Menu.SnapMode == Ferr2DT_SnapMode.Local ) local = SnapVector(local ); Vector3 global = path.transform.position + local; if (Ferr_Menu.SnapMode == Ferr2DT_SnapMode.Global) global = SnapVector(global); Handles.color = Color.white; if (!(secondID == 0 && !path.closed && firstDist > secondDist)) { Handles.DrawLine( global, path.transform.position + path.transform.rotation * Vector3.Scale(new Vector3(path.pathVerts[closestID].x, path.pathVerts[closestID].y, 0), path.transform.localScale)); } if (!(secondID == 0 && !path.closed && firstDist < secondDist)) { Handles.DrawLine( global, path.transform.position + path.transform.rotation * Vector3.Scale(new Vector3(path.pathVerts[secondID].x, path.pathVerts[secondID].y, 0), path.transform.localScale)); } Handles.color = new Color(1, 1, 1, 0); Vector3 handlePos = new Vector3(pos.x, pos.y, 0) + path.transform.position + offset; if (Handles.Button(handlePos, Quaternion.identity, HandleScale(handlePos), HandleScale(handlePos), Handles.CircleCap)) { Vector3 finalPos = inv * (new Vector3(global.x / path.transform.localScale.x, global.y / path.transform.localScale.y, 0) - path.transform.position); if (secondID == 0) { if (firstDist < secondDist) { path.pathVerts.Add(finalPos); #if FERR2D_TERRAIN if (terrain) terrain.directionOverrides.Add(Ferr2DT_TerrainDirection.None); #endif } else { path.pathVerts.Insert(0, finalPos); #if FERR2D_TERRAIN if (terrain) terrain.directionOverrides.Insert(0, Ferr2DT_TerrainDirection.None); #endif } } else { path.pathVerts.Insert(Mathf.Max(closestID, secondID), finalPos); #if FERR2D_TERRAIN if (terrain) terrain.directionOverrides.Insert(Mathf.Max(closestID, secondID), Ferr2DT_TerrainDirection.None); #endif } GUI.changed = true; } if (SetScale(new Vector3(pos.x, pos.y, 0) + path.transform.position + offset, texDotPlus, ref iconStyle)) Handles.Label(new Vector3(pos.x, pos.y, 0) + path.transform.position + offset, new GUIContent(texDotPlus), iconStyle); if (hasDummy) path.pathVerts.RemoveAt(0); }
private void DoShiftAdd(Ferr2D_Path path, GUIStyle iconStyle) { #if FERR2D_TERRAIN Ferr2DT_PathTerrain terrain = path.gameObject.GetComponent <Ferr2DT_PathTerrain>(); #endif Vector2 pos = GetMousePos(Event.current.mousePosition, path.transform.position.z) - new Vector2(path.transform.position.x, path.transform.position.y); int closestID = path.GetClosestSeg(new Vector2(pos.x / path.transform.localScale.x, pos.y / path.transform.localScale.y)); int secondID = closestID + 1 >= path.Count ? 0 : closestID + 1; float firstDist = Vector2.Distance(pos, path.pathVerts[closestID]); float secondDist = Vector2.Distance(pos, path.pathVerts[secondID]); Handles.color = Color.white; if (!(secondID == 0 && !path.closed && firstDist > secondDist)) { Handles.DrawLine( path.transform.position + new Vector3(pos.x, pos.y, 0), path.transform.position + Vector3.Scale(new Vector3(path.pathVerts[closestID].x, path.pathVerts[closestID].y, 0), path.transform.localScale)); } if (!(secondID == 0 && !path.closed && firstDist < secondDist)) { Handles.DrawLine( path.transform.position + new Vector3(pos.x, pos.y, 0), path.transform.position + Vector3.Scale(new Vector3(path.pathVerts[secondID].x, path.pathVerts[secondID].y, 0), path.transform.localScale)); } Handles.color = new Color(1, 1, 1, 0); Vector3 handlePos = new Vector3(pos.x, pos.y, 0) + path.transform.position + offset; if (Handles.Button(handlePos, Quaternion.identity, HandleScale(handlePos), HandleScale(handlePos), Handles.CircleCap)) { if (secondID == 0) { if (firstDist < secondDist) { path.pathVerts.Add(new Vector2(pos.x / path.transform.localScale.x, pos.y / path.transform.localScale.y)); #if FERR2D_TERRAIN if (terrain) { terrain.directionOverrides.Add(Ferr2DT_TerrainDirection.None); } #endif } else { path.pathVerts.Insert(0, new Vector2(pos.x / path.transform.localScale.x, pos.y / path.transform.localScale.y)); #if FERR2D_TERRAIN if (terrain) { terrain.directionOverrides.Insert(0, Ferr2DT_TerrainDirection.None); } #endif } } else { path.pathVerts.Insert(Mathf.Max(closestID, secondID), new Vector2(pos.x / path.transform.localScale.x, pos.y / path.transform.localScale.y)); #if FERR2D_TERRAIN if (terrain) { terrain.directionOverrides.Insert(Mathf.Max(closestID, secondID), Ferr2DT_TerrainDirection.None); } #endif } GUI.changed = true; } if (SetScale(new Vector3(pos.x, pos.y, 0) + path.transform.position + offset, texDotPlus, ref iconStyle)) { Handles.Label(new Vector3(pos.x, pos.y, 0) + path.transform.position + offset, new GUIContent(texDotPlus), iconStyle); } }
private void DoShiftAdd(Ferr2D_Path path, GUIStyle iconStyle) { Vector2 pos = GetMousePos(Event.current.mousePosition, path.transform.position.z) - new Vector2(path.transform.position.x, path.transform.position.y); int closestID = path.GetClosestSeg(pos); int secondID = closestID + 1 >= path.Count ? 0 : closestID + 1; float firstDist = Vector2.Distance(pos, path.pathVerts[closestID]); float secondDist = Vector2.Distance(pos, path.pathVerts[secondID]); Handles.color = Color.white; if (!(secondID == 0 && !path.closed && firstDist > secondDist)) { Handles.DrawLine( path.transform.position + new Vector3(pos.x, pos.y, 0), path.transform.position + new Vector3(path.pathVerts[closestID].x, path.pathVerts[closestID].y, 0)); } if (!(secondID == 0 && !path.closed && firstDist < secondDist)) { Handles.DrawLine( path.transform.position + new Vector3(pos.x, pos.y, 0), path.transform.position + new Vector3(path.pathVerts[secondID].x, path.pathVerts[secondID].y, 0)); } Handles.color = new Color(1, 1, 1, 0); if (Handles.Button(new Vector3(pos.x, pos.y, 0) + path.transform.position + offset, Quaternion.identity, 0.4f, 0.4f, Handles.CircleCap)) { if (secondID == 0) { if (firstDist < secondDist) { path.pathVerts.Add(new Vector2(pos.x, pos.y)); } else { path.pathVerts.Insert(0, new Vector2(pos.x, pos.y)); } } else { path.pathVerts.Insert(Mathf.Max(closestID, secondID), new Vector2(pos.x, pos.y)); } GUI.changed = true; } if (SetScale(new Vector3(pos.x, pos.y, 0) + path.transform.position + offset, texDotPlus, ref iconStyle)) Handles.Label(new Vector3(pos.x, pos.y, 0) + path.transform.position + offset, new GUIContent(texDotPlus), iconStyle); }
private void DoShiftAdd(Ferr2D_Path path, GUIStyle iconStyle) { #if FERR2D_TERRAIN Ferr2DT_PathTerrain terrain = path.gameObject.GetComponent<Ferr2DT_PathTerrain>(); #endif Vector2 pos = GetMousePos(Event.current.mousePosition, path.transform.position.z) - new Vector2(path.transform.position.x, path.transform.position.y); int closestID = path.GetClosestSeg(new Vector2(pos.x / path.transform.localScale.x, pos.y / path.transform.localScale.y)); int secondID = closestID + 1 >= path.Count ? 0 : closestID + 1; float firstDist = Vector2.Distance(pos, path.pathVerts[closestID]); float secondDist = Vector2.Distance(pos, path.pathVerts[secondID]); Handles.color = Color.white; if (!(secondID == 0 && !path.closed && firstDist > secondDist)) { Handles.DrawLine( path.transform.position + new Vector3(pos.x, pos.y, 0), path.transform.position + Vector3.Scale(new Vector3(path.pathVerts[closestID].x, path.pathVerts[closestID].y, 0), path.transform.localScale)); } if (!(secondID == 0 && !path.closed && firstDist < secondDist)) { Handles.DrawLine( path.transform.position + new Vector3(pos.x, pos.y, 0), path.transform.position + Vector3.Scale(new Vector3(path.pathVerts[secondID].x, path.pathVerts[secondID].y, 0), path.transform.localScale)); } Handles.color = new Color(1, 1, 1, 0); Vector3 handlePos = new Vector3(pos.x, pos.y, 0) + path.transform.position + offset; if (Handles.Button(handlePos, Quaternion.identity, HandleScale(handlePos), HandleScale(handlePos), Handles.CircleCap)) { if (secondID == 0) { if (firstDist < secondDist) { path.pathVerts.Add(new Vector2(pos.x / path.transform.localScale.x, pos.y / path.transform.localScale.y)); #if FERR2D_TERRAIN if (terrain) terrain.directionOverrides.Add(Ferr2DT_TerrainDirection.None); #endif } else { path.pathVerts.Insert(0, new Vector2(pos.x / path.transform.localScale.x, pos.y / path.transform.localScale.y)); #if FERR2D_TERRAIN if (terrain) terrain.directionOverrides.Insert(0, Ferr2DT_TerrainDirection.None); #endif } } else { path.pathVerts.Insert(Mathf.Max(closestID, secondID), new Vector2(pos.x / path.transform.localScale.x, pos.y / path.transform.localScale.y)); #if FERR2D_TERRAIN if (terrain) terrain.directionOverrides.Insert(Mathf.Max(closestID, secondID), Ferr2DT_TerrainDirection.None); #endif } GUI.changed = true; } if (SetScale(new Vector3(pos.x, pos.y, 0) + path.transform.position + offset, texDotPlus, ref iconStyle)) Handles.Label(new Vector3(pos.x, pos.y, 0) + path.transform.position + offset, new GUIContent(texDotPlus), iconStyle); }