protected override void ImmediateRepaint() { var pointCount = points.Count; if (pointCount < 1) { return; } var lineColor = (deleteModifier) ? deleteSegmentColor : segmentColor; GraphViewStaticBridge.ApplyWireMaterial(); GL.Begin(GL.LINES); GL.Color(lineColor); for (int i = 1; i < pointCount; i++) { // Apply offset Vector2 start = points[i - 1] + parent.layout.position; Vector2 end = points[i] + parent.layout.position; DrawDottedLine(start, end, segmentSize); } GL.End(); }
private void DrawDottedLine(Vector3 p1, Vector3 p2, float segmentsLength, Color col) { GraphViewStaticBridge.ApplyWireMaterial(); GL.Begin(GL.LINES); GL.Color(col); float length = Vector3.Distance(p1, p2); // ignore z component int count = Mathf.CeilToInt(length / segmentsLength); for (int i = 0; i < count; i += 2) { GL.Vertex((Vector3.Lerp(p1, p2, i * segmentsLength / length))); GL.Vertex((Vector3.Lerp(p1, p2, (i + 1) * segmentsLength / length))); } GL.End(); }
protected override void ImmediateRepaint() { VisualElement target = parent; var graphView = target as GraphView; if (graphView == null) { throw new InvalidOperationException("GridBackground can only be added to a GraphView"); } m_Container = graphView.ContentViewContainer; Rect clientRect = graphView.layout; // Since we're always stretch to parent size, we will use (0,0) as (x,y) coordinates clientRect.x = 0; clientRect.y = 0; var containerScale = new Vector3(m_Container.transform.matrix.GetColumn(0).magnitude, m_Container.transform.matrix.GetColumn(1).magnitude, m_Container.transform.matrix.GetColumn(2).magnitude); var containerTranslation = m_Container.transform.matrix.GetColumn(3); var containerPosition = m_Container.layout; // background GraphViewStaticBridge.ApplyWireMaterial(); GL.Begin(GL.QUADS); GL.Color(gridBackgroundColor); GL.Vertex(new Vector3(clientRect.x, clientRect.y)); GL.Vertex(new Vector3(clientRect.xMax, clientRect.y)); GL.Vertex(new Vector3(clientRect.xMax, clientRect.yMax)); GL.Vertex(new Vector3(clientRect.x, clientRect.yMax)); GL.End(); // vertical lines Vector3 from = new Vector3(clientRect.x, clientRect.y, 0.0f); Vector3 to = new Vector3(clientRect.x, clientRect.height, 0.0f); var tx = Matrix4x4.TRS(containerTranslation, Quaternion.identity, Vector3.one); from = tx.MultiplyPoint(from); to = tx.MultiplyPoint(to); from.x += (containerPosition.x * containerScale.x); from.y += (containerPosition.y * containerScale.y); to.x += (containerPosition.x * containerScale.x); to.y += (containerPosition.y * containerScale.y); float thickGridLineX = from.x; float thickGridLineY = from.y; // Update from/to to start at beginning of clientRect from.x = (from.x % (Spacing * (containerScale.x)) - (Spacing * (containerScale.x))); to.x = from.x; from.y = clientRect.y; to.y = clientRect.y + clientRect.height; while (from.x < clientRect.width) { from.x += Spacing * containerScale.x; to.x += Spacing * containerScale.x; GL.Begin(GL.LINES); GL.Color(lineColor); GL.Vertex(Clip(clientRect, from)); GL.Vertex(Clip(clientRect, to)); GL.End(); } float thickLineSpacing = (Spacing * thickLines); from.x = to.x = (thickGridLineX % (thickLineSpacing * (containerScale.x)) - (thickLineSpacing * (containerScale.x))); while (from.x < clientRect.width + thickLineSpacing) { GL.Begin(GL.LINES); GL.Color(thickLineColor); GL.Vertex(Clip(clientRect, from)); GL.Vertex(Clip(clientRect, to)); GL.End(); from.x += (Spacing * containerScale.x * thickLines); to.x += (Spacing * containerScale.x * thickLines); } // horizontal lines from = new Vector3(clientRect.x, clientRect.y, 0.0f); to = new Vector3(clientRect.x + clientRect.width, clientRect.y, 0.0f); from.x += (containerPosition.x * containerScale.x); from.y += (containerPosition.y * containerScale.y); to.x += (containerPosition.x * containerScale.x); to.y += (containerPosition.y * containerScale.y); from = tx.MultiplyPoint(from); to = tx.MultiplyPoint(to); from.y = to.y = (from.y % (Spacing * (containerScale.y)) - (Spacing * (containerScale.y))); from.x = clientRect.x; to.x = clientRect.width; while (from.y < clientRect.height) { from.y += Spacing * containerScale.y; to.y += Spacing * containerScale.y; GL.Begin(GL.LINES); GL.Color(lineColor); GL.Vertex(Clip(clientRect, from)); GL.Vertex(Clip(clientRect, to)); GL.End(); } thickLineSpacing = Spacing * thickLines; from.y = to.y = (thickGridLineY % (thickLineSpacing * (containerScale.y)) - (thickLineSpacing * (containerScale.y))); while (from.y < clientRect.height + thickLineSpacing) { GL.Begin(GL.LINES); GL.Color(thickLineColor); GL.Vertex(Clip(clientRect, from)); GL.Vertex(Clip(clientRect, to)); GL.End(); from.y += Spacing * containerScale.y * thickLines; to.y += Spacing * containerScale.y * thickLines; } }