All files / src/apps/tile-editor render-helpers.ts

0% Statements 0/147
0% Branches 0/1
0% Functions 0/1
0% Lines 0/147

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157                                                                                                                                                                                                                                                                                                                         
import { Graphics } from "pixi.js";
import { DRAW_CONFIG, LetterSegments } from "./draw-config";
import { Point } from "./types";
 
export function drawPolygonPath(
  g: Graphics,
  points: Point[],
  offset: Point,
  color: number,
  alpha: number,
  strokeColor: number,
  strokeWidth: number
): void {
  g.clear();
  if (points.length < 2) return;
  g.moveTo(points[0].x + offset.x, points[0].y + offset.y);
  for (let i = 1; i < points.length; i++) {
    g.lineTo(points[i].x + offset.x, points[i].y + offset.y);
  }
  if (alpha > 0) {
    g.fill({ color, alpha });
  }
  g.stroke({ color: strokeColor, width: strokeWidth });
}
 
export function drawDottedConnection(
  g: Graphics,
  start: Point,
  end: Point,
  color: number,
  width: number,
  scale: number
): void {
  const dx = end.x - start.x;
  const dy = end.y - start.y;
  const length = Math.hypot(dx, dy);
  if (length === 0) return;
  const dashLength = Math.max(width * DRAW_CONFIG.dottedDash.widthFactor, DRAW_CONFIG.dottedDash.minPx / scale);
  const gapLength = dashLength;
  const step = dashLength + gapLength;
  const ux = dx / length;
  const uy = dy / length;
  for (let dist = 0; dist < length; dist += step) {
    const segmentStart = dist;
    const segmentEnd = Math.min(dist + dashLength, length);
    g.moveTo(start.x + ux * segmentStart, start.y + uy * segmentStart);
    g.lineTo(start.x + ux * segmentEnd, start.y + uy * segmentEnd);
  }
  g.stroke({ color, width });
}
 
export function drawDashedPath(
  g: Graphics,
  points: Point[],
  offset: Point,
  primaryColor: number,
  secondaryColor: number,
  width: number,
  closePath: boolean,
  dashOffset: number,
  scale: number
): void {
  if (points.length < 2) return;
  const dashLength = Math.max(width * DRAW_CONFIG.dashedDash.widthFactor, DRAW_CONFIG.dashedDash.minPx / scale);
  const gapLength = dashLength;
  const pattern = dashLength + gapLength;
  const offsetNorm = ((dashOffset % pattern) + pattern) % pattern;
  let drawOn = offsetNorm < dashLength;
  let remaining = drawOn ? dashLength - offsetNorm : pattern - offsetNorm;
  const pathPoints = closePath ? [...points, points[0]] : points;
 
  for (let i = 0; i < pathPoints.length - 1; i++) {
    const start = { x: pathPoints[i].x + offset.x, y: pathPoints[i].y + offset.y };
    const end = { x: pathPoints[i + 1].x + offset.x, y: pathPoints[i + 1].y + offset.y };
    const dx = end.x - start.x;
    const dy = end.y - start.y;
    const segLen = Math.hypot(dx, dy);
    if (segLen === 0) continue;
    const ux = dx / segLen;
    const uy = dy / segLen;
    let segPos = 0;
    while (segPos < segLen) {
      const step = Math.min(remaining, segLen - segPos);
      if (drawOn && step > 0) {
        const from = segPos;
        const to = segPos + step;
        g.moveTo(start.x + ux * from, start.y + uy * from);
        g.lineTo(start.x + ux * to, start.y + uy * to);
      }
      segPos += step;
      remaining -= step;
      if (remaining <= DRAW_CONFIG.dashToggleEpsilon) {
        drawOn = !drawOn;
        remaining = drawOn ? dashLength : gapLength;
      }
    }
  }
  g.stroke({ color: primaryColor, width });
 
  const secondaryOffset = dashOffset + dashLength;
  const secondaryNorm = ((secondaryOffset % pattern) + pattern) % pattern;
  drawOn = secondaryNorm < dashLength;
  remaining = drawOn ? dashLength - secondaryNorm : pattern - secondaryNorm;
  for (let i = 0; i < pathPoints.length - 1; i++) {
    const start = { x: pathPoints[i].x + offset.x, y: pathPoints[i].y + offset.y };
    const end = { x: pathPoints[i + 1].x + offset.x, y: pathPoints[i + 1].y + offset.y };
    const dx = end.x - start.x;
    const dy = end.y - start.y;
    const segLen = Math.hypot(dx, dy);
    if (segLen === 0) continue;
    const ux = dx / segLen;
    const uy = dy / segLen;
    let segPos = 0;
    while (segPos < segLen) {
      const step = Math.min(remaining, segLen - segPos);
      if (drawOn && step > 0) {
        const from = segPos;
        const to = segPos + step;
        g.moveTo(start.x + ux * from, start.y + uy * from);
        g.lineTo(start.x + ux * to, start.y + uy * to);
      }
      segPos += step;
      remaining -= step;
      if (remaining <= DRAW_CONFIG.dashToggleEpsilon) {
        drawOn = !drawOn;
        remaining = drawOn ? dashLength : gapLength;
      }
    }
  }
  g.stroke({ color: secondaryColor, width });
}
 
export function drawLetter(
  g: Graphics,
  letter: string,
  size: number,
  color: number,
  width: number
): void {
  const segments = getLetterSegments(letter);
  if (!segments) return;
  g.clear();
  for (const [x1, y1, x2, y2] of segments) {
    g.moveTo(x1 * size, y1 * size);
    g.lineTo(x2 * size, y2 * size);
  }
  g.stroke({ color, width });
}
 
export function getLetterSegments(
  letter: string
): Array<[number, number, number, number]> | null {
  const l = letter.toUpperCase();
  const segments: Record<string, LetterSegments> = DRAW_CONFIG.letterGlyphs;
  return segments[l] ?? null;
}