public static bool IsInRange(Vector2Int left, Vector2Int right, int range) { Rect2 rangeRect = new Rect2(left.ToVec2(), new Vector2(1, 1)); rangeRect = rangeRect.Grow(range); return(rangeRect.HasPoint(right.ToVec2())); }
public override void _Draw() { // ---- Draws Grid ----- for (int i = 0; i < 5; i++) { float t = i / 4f; Vector2 origin = new Vector2( Mathf.Lerp(MARGIN, RectSize.x - MARGIN, t), 0 ); Vector2 to = origin + RectSize * Vector2.Down; DrawLine(origin, to, new Color(1f, 1f, 1f, .25f)); DrawString(defaultFont, to, t.ToString(), new Color(1f, 1f, 1f, .25f)); } for (int i = 0; i < 3; i++) { float t = i / 2f; Vector2 origin = new Vector2( 0, Mathf.Lerp(MARGIN, RectSize.y - MARGIN, t) ); Vector2 to = origin + RectSize * Vector2.Right; DrawLine(origin, to, new Color(1f, 1f, 1f, .25f)); DrawString(defaultFont, origin + Vector2.Right * MARGIN, (1f - t).ToString(), new Color(1f, 1f, 1f, .25f)); } // --------------------- // ---- Draws Curve ---- Vector2[] linePoints = new Vector2[CURVE_RESOLUTION]; for (int i = 0; i < CURVE_RESOLUTION; i++) { float t = i / (float)(CURVE_RESOLUTION - 1); float x = Mathf.Lerp(MARGIN, RectSize.x - MARGIN, t); float y = Mathf.Lerp(MARGIN, RectSize.y - MARGIN, 1f - curve.Interpolate(t)); linePoints[i] = new Vector2(x, y); } DrawPolyline(linePoints, new Color(1f, 1f, 1f, .5f)); // --------------------- // --- Draws Handles --- int pointCount = curve.GetPointCount(); for (int i = 0; i < pointCount; i++) { float t = i / (float)(pointCount - 1); Rect2 r = new Rect2(); Vector2 handlePos = GetHandlePos(i); if (i == selectedIdx) { if (i > 0) { Vector2 tanPos = GetHandleLeftTangent(i); r.Position = tanPos - Vector2.One * (TANGENT_HANDLE_SIZE / 2f); r.Size = Vector2.One * TANGENT_HANDLE_SIZE; DrawLine(handlePos, tanPos, Colors.SkyBlue); DrawRect(r, Colors.SkyBlue); } if (i < pointCount - 1) { Vector2 tanPos = GetHandleRightTangent(i); r.Position = tanPos - Vector2.One * (TANGENT_HANDLE_SIZE / 2f); r.Size = Vector2.One * TANGENT_HANDLE_SIZE; DrawLine(handlePos, tanPos, Colors.SkyBlue); DrawRect(r, Colors.SkyBlue); } } r.Position = handlePos - Vector2.One * (HANDLE_SIZE / 2f); r.Size = Vector2.One * HANDLE_SIZE; if (i == selectedIdx) { DrawRect(r, Colors.SkyBlue); } else { DrawRect(r, Colors.White); } if (i == hoverIdx) { r = r.Grow(3); DrawRect(r, Colors.White, false); } } // --------------------- }