2using System.Collections.Generic;
15 private Dictionary<string, LineObject> lines;
18 [SerializeField]
private RectTransform canvasRect;
19 [SerializeField]
private LineObject actualLine;
24 if (!canvasRect) canvasRect =
canvas.GetComponent<RectTransform>();
30 lines =
new Dictionary<string, LineObject>();
33 internal void UpdateLines()
35 foreach (var o
in lines)
51 var allAO = FindObjectsOfType<ActionObject>();
52 var orderedList = allAO.OrderBy(ao =>
Vector3.Distance(lr.transform.position, ao.transform.position));
53 foreach (var ao
in orderedList)
68 var allSides = ao.GetSides();
69 var intersections =
new List<Vector2>();
71 for (var i = 0; i < allSides.Count - 1; i++)
74 var y = allSides[i + 1];
76 if (Intersects(lr.transform.position, (Vector2)lr.transform.position + lr.Points[lr.Points.Length - 1], x, y, out var point))
79 intersections.Add(point);
86 if (Intersects(lr.transform.position, (Vector2)lr.transform.position + lr.Points[lr.Points.Length - 1], x1, y1, out var point1))
89 intersections.Add(point1);
92 if (intersections.Count == 2)
95 intersections = intersections.OrderBy(i =>
Vector2.Distance(lr.transform.position, i)).ToList();
96 var corner1 = ClosestPoint(intersections[0], allSides);
97 var corner2 = ClosestPoint(intersections[1], allSides);
98 intersections.Insert(1, corner1);
101 if (corner1.x != corner2.x && corner1.y != corner2.y)
103 intersections.Insert(index,
new Vector2(corner1.x, corner2.y));
107 intersections.Insert(index, corner2);
109 var currentLine = lr.Points.ToList();
110 var offset = -(
Vector2)lr.transform.position;
112 currentLine.Insert(currentLine.Count - 1, corner1 + offset);
115 currentLine.Insert(currentLine.Count - 1,
new Vector2(corner1.x, corner2.y) + offset);
117 currentLine.Insert(currentLine.Count - 1, corner2 + offset);
121 lr.Points = currentLine.ToArray();
124 if (intersections.Count == 1)
126 var currentLine = lr.Points.ToList();
127 var offset = -(
Vector2)lr.transform.position;
129 if (
Vector2.Distance(intersections[0] + offset, currentLine[0]) < 10 ||
Vector2.Distance(intersections[0] + offset, currentLine.Last()) < 10)
134 var corner2 = ClosestPoint(intersections[0], allSides);
136 var checkpoint =
Vector2.MoveTowards(currentLine[0] + offset, corner2, 10);
137 var corner1 = ClosestPoint(checkpoint, allSides);
141 currentLine.Insert(currentLine.Count - 1, corner2 + offset);
143 lr.Points = currentLine.ToArray();
155 private Vector2 ClosestPoint(Vector2 point, List<Vector2> points)
157 var bestDist = 9999f;
159 foreach (var p
in points)
161 var dist =
Vector2.Distance(point, p);
175 lo.id = Guid.NewGuid().ToString();
179 lo.lineRenderer.gameObject.name = lo.id;
183 lines.Add(lo.id, lo);
188 static bool Intersects(Vector2 a1, Vector2 a2, Vector2 b1, Vector2 b2, out Vector2 intersection)
190 intersection = Vector2.zero;
194 float bDotDPerp = b.x * d.y - b.y * d.x;
197 if (bDotDPerp == 0.0f)
201 float t = (c.x * d.y - c.y * d.x) / bDotDPerp;
205 float u = (c.x * b.y - c.y * b.x) / bDotDPerp;
209 intersection = a1 + t * b;
222 lines.Remove(lineObj.
id);
227 return new Vector2(-canvasRect.rect.width, canvasRect.rect.height);
232 return new Vector2(canvasRect.position.x, canvasRect.position.y);
241 item.Value.lineRenderer.rectTransform.position = item.Value.startRect.transform.position;
242 if (item.Value.lineRenderer.transform.parent.transform.lossyScale != Vector3.zero)
244 item.Value.lineRenderer.rectTransform.localScale =
245 Divide(1, item.Value.lineRenderer.transform.parent.transform.lossyScale);
246 item.Value.lineRenderer.LineThickness =
247 LineWidth * item.Value.lineRenderer.transform.parent.transform.lossyScale.x;
250 item.Value.lineRenderer.Points[0] = Vector2.zero;
251 item.Value.lineRenderer.Points[1] =
252 item.Value.endRect.transform.position - item.Value.startRect.transform.position;
253 item.Value.lineRenderer.SetAllDirty();
255 (item.Value.lineRenderer.gameObject.transform as RectTransform).SetWidth((Mathf.Abs(item.Value.startRect.position.x - item.Value.endRect.position.x) +
LineWidth) * 2);
256 (item.Value.lineRenderer.gameObject.transform as RectTransform).SetHeight((Mathf.Abs(item.Value.startRect.position.y - item.Value.endRect.position.y) +
LineWidth) * 2);
258 UpdateLine(item.Value);
262 public void StartConnection(RectTransform startRect,
string outputID,
bool blue =
false)
300 actualLine.
endRect.transform.position - actualLine.
startRect.transform.position;
303 actualLine.
endID = inputID;
318 var temp =
new Vector2(actualLine.
startRect.transform.position.x, actualLine.
startRect.transform.position.y);
331 return lines.Where(l =>
336 public Vector3
Divide(Vector3 x, Vector3 y)
338 return new Vector3(x[0] / y[0], x[1] / y[1], x[2] / y[2]);
341 public Vector3
Divide(
float x, Vector3 y)
343 return new Vector3(x / y[0], x / y[1], x / y[2]);
346 public Vector3
Divide(Vector3 x,
float y)
348 return new Vector3(x[0] / y, x[1] / y, x[2] / y);
List< string > GetInputs()
List< string > GetOutputs()
ActionObject GetActionById(string id)
static Controller Instance
static LineDrawer Instance
List< KeyValuePair< string, LineObject > > GetLinesByActionId(string id)
Vector3 Divide(Vector3 x, Vector3 y)
Vector3 Divide(Vector3 x, float y)
Vector2 GetCanvasOffset()
void DragConnection(Vector2 position)
LineObject EndConnection(RectTransform endRect, string inputID, bool weldParent=false)
Vector3 Divide(float x, Vector3 y)
void StartConnection(RectTransform startRect, string outputID, bool blue=false)
void RemoveLine(string lineId)
void RemoveLine(LineObject lineObj)
UILineRenderer lineRenderer
Vector2[] Points
Points to be drawn in the line.
Credit Erdener Gonenc - @PixelEnvision.