All files / src/components/world/inspector/scenes/tilemap TilesetUnsetDefaultsOverlay.tsx

0% Statements 0/21
0% Branches 0/12
0% Functions 0/2
0% Lines 0/18

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                                                                                                   
import React, { useEffect, useRef } from "react";
import styled from "styled-components";
import { TILE_SIZE } from "consts";
 
const Canvas = styled.canvas`
  position: absolute;
  inset: 0;
  pointer-events: none;
  image-rendering: pixelated;
`;
 
interface TilesetUnsetDefaultsOverlayProps {
  width: number;
  height: number;
  values: number[];
  unsetValue: number;
}
 
const TilesetUnsetDefaultsOverlay = ({
  width,
  height,
  values,
  unsetValue,
}: TilesetUnsetDefaultsOverlayProps) => {
  const canvasRef = useRef<HTMLCanvasElement>(null);
 
  useEffect(() => {
    const ctx = canvasRef.current?.getContext("2d");
    if (!ctx) return;
    ctx.clearRect(0, 0, width * TILE_SIZE, height * TILE_SIZE);
    ctx.fillStyle = "rgba(128, 128, 128, 0.8)";
    for (let index = 0; index < width * height; index++) {
      if ((values[index] ?? unsetValue) !== unsetValue) continue;
      const x = (index % width) * TILE_SIZE;
      const y = Math.floor(index / width) * TILE_SIZE;
      ctx.fillRect(x, y, TILE_SIZE, TILE_SIZE);
    }
  }, [height, unsetValue, values, width]);
 
  return (
    <Canvas
      ref={canvasRef}
      width={width * TILE_SIZE}
      height={height * TILE_SIZE}
    />
  );
};
 
export default TilesetUnsetDefaultsOverlay;