All files / src/components/world SceneSlopePreview.tsx

0% Statements 0/74
0% Branches 0/32
0% Functions 0/6
0% Lines 0/71

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                                                                                                                                                                                                                                                                                                         
import React, { useCallback, useEffect, useRef, useState } from "react";
import { SlopePreview } from "store/features/editor/editorState";
import styled from "styled-components";
 
const TILE_SIZE = 8;
 
const Canvas = styled.canvas`
  position: absolute;
  top: 0;
  left: 0;
`;
 
interface SceneSlopePreviewProps {
  width: number;
  height: number;
  slopePreview: SlopePreview;
}
 
const SceneSlopePreview = ({
  width,
  height,
  slopePreview,
}: SceneSlopePreviewProps) => {
  const canvas = useRef<HTMLCanvasElement>(null);
  const { startX, startY, endX, endY, slopeIncline } = slopePreview;
  const [offset, setOffset] = useState(slopePreview.offset);
 
  const onKeyDown = useCallback((e: KeyboardEvent) => {
    Iif (!(e.target instanceof HTMLElement)) return;
    Iif (e.target.nodeName !== "BODY") {
      return;
    }
    Iif (e.shiftKey) {
      setOffset(true);
    }
  }, []);
 
  const onKeyUp = useCallback((e: KeyboardEvent) => {
    Iif (!(e.target instanceof HTMLElement)) return;
    Iif (e.target.nodeName !== "BODY") {
      return;
    }
    Iif (!e.shiftKey) {
      setOffset(false);
    }
  }, []);
 
  // Keyboard handlers
  useEffect(() => {
    window.addEventListener("keydown", onKeyDown);
    window.addEventListener("keyup", onKeyUp);
    return () => {
      window.removeEventListener("keydown", onKeyDown);
      window.removeEventListener("keyup", onKeyUp);
    };
  }, [onKeyDown, onKeyUp]);
 
  useEffect(() => {
    Iif (canvas.current) {
      // eslint-disable-next-line no-self-assign
      canvas.current.width = canvas.current.width; // Clear canvas
      const ctx = canvas.current.getContext("2d");
 
      Iif (!ctx) return;
 
      ctx.strokeStyle = "rgba(0,0,255,0.6)";
 
      let lineStartX = 0;
      let lineStartY = 0;
      let lineEndX = 0;
      let lineEndY = 0;
 
      const diffX = endX - startX;
      const diffY = endY - startY;
      const signX = Math.sign(diffX);
      const signY = Math.sign(diffY);
 
      if (endX < startX) {
        lineStartX = (startX + 1) * TILE_SIZE;
        lineEndX = (endX + 1) * TILE_SIZE;
      } else {
        lineStartX = startX * TILE_SIZE;
        lineEndX = endX * TILE_SIZE;
      }
 
      if (endY < startY) {
        lineStartY = (startY + 1) * TILE_SIZE;
        lineEndY = (endY + 1) * TILE_SIZE;
      } else {
        lineStartY = startY * TILE_SIZE;
        lineEndY = endY * TILE_SIZE;
      }
 
      Iif (slopeIncline === "shallow" && offset) {
        lineStartY += signY * 0.5 * TILE_SIZE;
        lineEndY += signY * 0.5 * TILE_SIZE;
      }
      Iif (slopeIncline === "steep" && offset) {
        lineStartX += signX * 0.5 * TILE_SIZE;
        lineEndX += signX * 0.5 * TILE_SIZE;
      }
 
      Iif (
        lineStartY === lineEndY &&
        // Drag left to right for top collision
        // Drag right to left for bottom collision
        // Shift to toggle
        ((lineStartX > lineEndX && !offset) ||
          (lineStartX <= lineEndX && offset))
      ) {
        // Bottom collision
        ctx.strokeStyle = "rgba(255,250,40,0.6)";
        lineStartY += TILE_SIZE - 1;
        lineEndY += TILE_SIZE - 1;
      }
      if (
        lineStartX === lineEndX &&
        // Drag top to bottom for left collision
        // Drag bottom to top for right collision
        // Shift to toggle
        ((lineStartY > lineEndY && !offset) ||
          (lineStartY <= lineEndY && offset))
      ) {
        // Right collision
        ctx.strokeStyle = "rgba(40,250,250,0.6)";
        lineStartX += TILE_SIZE - 1;
        lineEndX += TILE_SIZE - 1;
      } else Iif (lineStartX === lineEndX) {
        // Left collision
        ctx.strokeStyle = "rgba(250,40,250,0.6)";
      }
 
      ctx.moveTo(lineStartX, lineStartY);
      ctx.lineTo(lineEndX, lineEndY);
      ctx.stroke();
    }
  }, [endX, endY, height, offset, slopeIncline, startX, startY, width]);
 
  return (
    <Canvas
      ref={canvas}
      width={width * TILE_SIZE}
      height={height * TILE_SIZE}
    />
  );
};
 
export default SceneSlopePreview;