2020-04-29 18:21:44 +10:00
|
|
|
import React, { useRef, useEffect, useState, useContext } from "react";
|
2020-04-19 13:33:31 +10:00
|
|
|
import shortid from "shortid";
|
2020-04-18 18:11:21 +10:00
|
|
|
|
2020-04-28 17:04:31 +10:00
|
|
|
import { compare as comparePoints } from "../../helpers/vector2";
|
2020-04-27 21:39:21 +10:00
|
|
|
import {
|
|
|
|
|
getBrushPositionForTool,
|
|
|
|
|
getDefaultShapeData,
|
|
|
|
|
getUpdatedShapeData,
|
2020-04-28 11:04:54 +10:00
|
|
|
isShapeHovered,
|
|
|
|
|
drawShape,
|
2020-04-28 17:04:31 +10:00
|
|
|
simplifyPoints,
|
2020-04-28 22:05:47 +10:00
|
|
|
getRelativePointerPosition,
|
2020-04-27 21:39:21 +10:00
|
|
|
} from "../../helpers/drawing";
|
2020-04-20 11:56:56 +10:00
|
|
|
|
2020-04-29 18:21:44 +10:00
|
|
|
import MapInteractionContext from "../../contexts/MapInteractionContext";
|
|
|
|
|
|
2020-04-19 13:33:31 +10:00
|
|
|
function MapDrawing({
|
|
|
|
|
width,
|
|
|
|
|
height,
|
|
|
|
|
selectedTool,
|
2020-04-27 17:29:46 +10:00
|
|
|
toolSettings,
|
2020-04-19 13:33:31 +10:00
|
|
|
shapes,
|
|
|
|
|
onShapeAdd,
|
|
|
|
|
onShapeRemove,
|
2020-04-20 15:17:56 +10:00
|
|
|
gridSize,
|
2020-04-19 13:33:31 +10:00
|
|
|
}) {
|
2020-04-18 18:54:13 +10:00
|
|
|
const canvasRef = useRef();
|
2020-04-18 18:11:21 +10:00
|
|
|
const containerRef = useRef();
|
|
|
|
|
|
2020-04-19 17:39:26 +10:00
|
|
|
const [isDrawing, setIsDrawing] = useState(false);
|
2020-04-27 21:39:21 +10:00
|
|
|
const [drawingShape, setDrawingShape] = useState(null);
|
2020-04-19 17:39:26 +10:00
|
|
|
const [pointerPosition, setPointerPosition] = useState({ x: -1, y: -1 });
|
|
|
|
|
|
2020-04-28 22:05:47 +10:00
|
|
|
const shouldHover = selectedTool === "erase";
|
|
|
|
|
const isEditing =
|
|
|
|
|
selectedTool === "brush" ||
|
|
|
|
|
selectedTool === "shape" ||
|
|
|
|
|
selectedTool === "erase";
|
2020-04-28 17:04:31 +10:00
|
|
|
|
2020-04-29 18:21:44 +10:00
|
|
|
const { scaleRef } = useContext(MapInteractionContext);
|
|
|
|
|
|
2020-04-19 17:39:26 +10:00
|
|
|
// Reset pointer position when tool changes
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setPointerPosition({ x: -1, y: -1 });
|
|
|
|
|
}, [selectedTool]);
|
|
|
|
|
|
|
|
|
|
function handleStart(event) {
|
2020-04-28 22:05:47 +10:00
|
|
|
if (!isEditing) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-04-19 17:39:26 +10:00
|
|
|
if (event.touches && event.touches.length !== 1) {
|
|
|
|
|
setIsDrawing(false);
|
2020-04-27 21:39:21 +10:00
|
|
|
setDrawingShape(null);
|
2020-04-19 17:39:26 +10:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const pointer = event.touches ? event.touches[0] : event;
|
2020-04-28 22:05:47 +10:00
|
|
|
const position = getRelativePointerPosition(pointer, containerRef.current);
|
2020-04-19 17:39:26 +10:00
|
|
|
setPointerPosition(position);
|
|
|
|
|
setIsDrawing(true);
|
2020-04-27 21:39:21 +10:00
|
|
|
const brushPosition = getBrushPositionForTool(
|
|
|
|
|
position,
|
2020-04-28 11:31:01 +10:00
|
|
|
selectedTool,
|
2020-04-28 17:04:31 +10:00
|
|
|
toolSettings,
|
2020-04-27 21:39:21 +10:00
|
|
|
gridSize,
|
|
|
|
|
shapes
|
|
|
|
|
);
|
|
|
|
|
const commonShapeData = {
|
2020-04-28 22:05:47 +10:00
|
|
|
color: toolSettings && toolSettings.color,
|
|
|
|
|
blend: toolSettings && toolSettings.useBlending,
|
2020-04-27 21:39:21 +10:00
|
|
|
id: shortid.generate(),
|
|
|
|
|
};
|
2020-04-19 00:24:06 +10:00
|
|
|
if (selectedTool === "brush") {
|
2020-04-27 21:39:21 +10:00
|
|
|
setDrawingShape({
|
|
|
|
|
type: "path",
|
|
|
|
|
pathType: toolSettings.type,
|
|
|
|
|
data: { points: [brushPosition] },
|
|
|
|
|
strokeWidth: toolSettings.type === "stroke" ? 1 : 0,
|
|
|
|
|
...commonShapeData,
|
|
|
|
|
});
|
|
|
|
|
} else if (selectedTool === "shape") {
|
|
|
|
|
setDrawingShape({
|
|
|
|
|
type: "shape",
|
|
|
|
|
shapeType: toolSettings.type,
|
|
|
|
|
data: getDefaultShapeData(toolSettings.type, brushPosition),
|
|
|
|
|
strokeWidth: 0,
|
|
|
|
|
...commonShapeData,
|
|
|
|
|
});
|
2020-04-19 00:24:06 +10:00
|
|
|
}
|
2020-04-18 18:11:21 +10:00
|
|
|
}
|
|
|
|
|
|
2020-04-19 17:39:26 +10:00
|
|
|
function handleMove(event) {
|
2020-04-28 22:05:47 +10:00
|
|
|
if (!isEditing) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-04-19 17:39:26 +10:00
|
|
|
if (event.touches && event.touches.length !== 1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const pointer = event.touches ? event.touches[0] : event;
|
2020-04-28 17:04:31 +10:00
|
|
|
// Set pointer position every frame for erase tool and fog
|
|
|
|
|
if (shouldHover) {
|
2020-04-28 22:05:47 +10:00
|
|
|
const position = getRelativePointerPosition(
|
|
|
|
|
pointer,
|
|
|
|
|
containerRef.current
|
|
|
|
|
);
|
2020-04-19 17:39:26 +10:00
|
|
|
setPointerPosition(position);
|
2020-04-19 00:24:06 +10:00
|
|
|
}
|
2020-04-27 21:39:21 +10:00
|
|
|
if (isDrawing) {
|
2020-04-28 22:05:47 +10:00
|
|
|
const position = getRelativePointerPosition(
|
|
|
|
|
pointer,
|
|
|
|
|
containerRef.current
|
|
|
|
|
);
|
2020-04-19 17:39:26 +10:00
|
|
|
setPointerPosition(position);
|
2020-04-27 21:39:21 +10:00
|
|
|
const brushPosition = getBrushPositionForTool(
|
|
|
|
|
position,
|
2020-04-28 11:31:01 +10:00
|
|
|
selectedTool,
|
2020-04-28 17:04:31 +10:00
|
|
|
toolSettings,
|
2020-04-27 21:39:21 +10:00
|
|
|
gridSize,
|
|
|
|
|
shapes
|
|
|
|
|
);
|
|
|
|
|
if (selectedTool === "brush") {
|
|
|
|
|
setDrawingShape((prevShape) => {
|
|
|
|
|
const prevPoints = prevShape.data.points;
|
2020-04-28 17:04:31 +10:00
|
|
|
if (
|
|
|
|
|
comparePoints(
|
|
|
|
|
prevPoints[prevPoints.length - 1],
|
|
|
|
|
brushPosition,
|
|
|
|
|
0.001
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
return prevShape;
|
2020-04-27 21:39:21 +10:00
|
|
|
}
|
2020-04-28 17:04:31 +10:00
|
|
|
const simplified = simplifyPoints(
|
2020-04-27 21:39:21 +10:00
|
|
|
[...prevPoints, brushPosition],
|
2020-04-29 18:21:44 +10:00
|
|
|
gridSize,
|
|
|
|
|
scaleRef.current
|
2020-04-27 21:39:21 +10:00
|
|
|
);
|
|
|
|
|
return {
|
|
|
|
|
...prevShape,
|
|
|
|
|
data: { points: simplified },
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
} else if (selectedTool === "shape") {
|
|
|
|
|
setDrawingShape((prevShape) => ({
|
|
|
|
|
...prevShape,
|
|
|
|
|
data: getUpdatedShapeData(
|
|
|
|
|
prevShape.shapeType,
|
|
|
|
|
prevShape.data,
|
2020-04-29 11:04:33 +10:00
|
|
|
brushPosition,
|
|
|
|
|
gridSize
|
2020-04-27 21:39:21 +10:00
|
|
|
),
|
|
|
|
|
}));
|
|
|
|
|
}
|
2020-04-18 18:11:21 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 17:39:26 +10:00
|
|
|
function handleStop(event) {
|
2020-04-28 22:05:47 +10:00
|
|
|
if (!isEditing) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-04-19 17:39:26 +10:00
|
|
|
if (event.touches && event.touches.length !== 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-04-29 09:25:56 +10:00
|
|
|
if (selectedTool === "brush" && drawingShape) {
|
2020-04-27 21:39:21 +10:00
|
|
|
if (drawingShape.data.points.length > 1) {
|
|
|
|
|
onShapeAdd(drawingShape);
|
2020-04-19 17:39:26 +10:00
|
|
|
}
|
2020-04-29 09:25:56 +10:00
|
|
|
} else if (selectedTool === "shape" && drawingShape) {
|
2020-04-27 21:39:21 +10:00
|
|
|
onShapeAdd(drawingShape);
|
2020-04-19 13:33:31 +10:00
|
|
|
}
|
2020-04-27 21:39:21 +10:00
|
|
|
|
2020-04-29 09:25:56 +10:00
|
|
|
if (selectedTool === "erase" && hoveredShapeRef.current && isDrawing) {
|
2020-04-19 13:33:31 +10:00
|
|
|
onShapeRemove(hoveredShapeRef.current.id);
|
2020-04-19 00:24:06 +10:00
|
|
|
}
|
2020-04-29 09:25:56 +10:00
|
|
|
setIsDrawing(false);
|
|
|
|
|
setDrawingShape(null);
|
2020-04-18 18:11:21 +10:00
|
|
|
}
|
|
|
|
|
|
2020-04-27 17:40:36 +10:00
|
|
|
// Add listeners for draw events on map to allow drawing past the bounds
|
|
|
|
|
// of the container
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const map = document.querySelector(".map");
|
|
|
|
|
map.addEventListener("mousedown", handleStart);
|
|
|
|
|
map.addEventListener("mousemove", handleMove);
|
|
|
|
|
map.addEventListener("mouseup", handleStop);
|
|
|
|
|
map.addEventListener("touchstart", handleStart);
|
|
|
|
|
map.addEventListener("touchmove", handleMove);
|
|
|
|
|
map.addEventListener("touchend", handleStop);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
map.removeEventListener("mousedown", handleStart);
|
|
|
|
|
map.removeEventListener("mousemove", handleMove);
|
|
|
|
|
map.removeEventListener("mouseup", handleStop);
|
|
|
|
|
map.removeEventListener("touchstart", handleStart);
|
|
|
|
|
map.removeEventListener("touchmove", handleMove);
|
|
|
|
|
map.removeEventListener("touchend", handleStop);
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2020-04-28 10:14:45 +10:00
|
|
|
/**
|
|
|
|
|
* Rendering
|
|
|
|
|
*/
|
2020-04-19 13:33:31 +10:00
|
|
|
const hoveredShapeRef = useRef(null);
|
2020-04-18 18:54:13 +10:00
|
|
|
useEffect(() => {
|
|
|
|
|
const canvas = canvasRef.current;
|
|
|
|
|
if (canvas) {
|
|
|
|
|
const context = canvas.getContext("2d");
|
|
|
|
|
|
|
|
|
|
context.clearRect(0, 0, width, height);
|
2020-04-19 13:33:31 +10:00
|
|
|
let hoveredShape = null;
|
|
|
|
|
for (let shape of shapes) {
|
2020-04-28 17:04:31 +10:00
|
|
|
if (shouldHover) {
|
2020-04-28 11:04:54 +10:00
|
|
|
if (isShapeHovered(shape, context, pointerPosition, width, height)) {
|
2020-04-19 13:33:31 +10:00
|
|
|
hoveredShape = shape;
|
2020-04-19 00:24:06 +10:00
|
|
|
}
|
|
|
|
|
}
|
2020-04-28 22:05:47 +10:00
|
|
|
drawShape(shape, context, gridSize, width, height);
|
2020-04-19 00:24:06 +10:00
|
|
|
}
|
2020-04-27 21:39:21 +10:00
|
|
|
if (drawingShape) {
|
2020-04-28 11:04:54 +10:00
|
|
|
drawShape(drawingShape, context, gridSize, width, height);
|
2020-04-19 13:33:31 +10:00
|
|
|
}
|
|
|
|
|
if (hoveredShape) {
|
2020-04-28 11:04:54 +10:00
|
|
|
const shape = { ...hoveredShape, color: "#BB99FF", blend: true };
|
|
|
|
|
drawShape(shape, context, gridSize, width, height);
|
2020-04-18 18:54:13 +10:00
|
|
|
}
|
2020-04-19 13:33:31 +10:00
|
|
|
hoveredShapeRef.current = hoveredShape;
|
2020-04-18 18:54:13 +10:00
|
|
|
}
|
2020-04-19 13:33:31 +10:00
|
|
|
}, [
|
|
|
|
|
shapes,
|
|
|
|
|
width,
|
|
|
|
|
height,
|
2020-04-19 17:39:26 +10:00
|
|
|
pointerPosition,
|
|
|
|
|
isDrawing,
|
2020-04-19 13:33:31 +10:00
|
|
|
selectedTool,
|
2020-04-27 21:39:21 +10:00
|
|
|
drawingShape,
|
|
|
|
|
gridSize,
|
2020-04-28 22:05:47 +10:00
|
|
|
shouldHover,
|
2020-04-19 13:33:31 +10:00
|
|
|
]);
|
2020-04-18 18:54:13 +10:00
|
|
|
|
2020-04-18 18:11:21 +10:00
|
|
|
return (
|
|
|
|
|
<div
|
2020-04-28 22:05:47 +10:00
|
|
|
style={{
|
|
|
|
|
position: "absolute",
|
|
|
|
|
top: 0,
|
|
|
|
|
left: 0,
|
|
|
|
|
right: 0,
|
|
|
|
|
bottom: 0,
|
|
|
|
|
pointerEvents: "none",
|
|
|
|
|
}}
|
2020-04-18 18:11:21 +10:00
|
|
|
ref={containerRef}
|
2020-04-18 18:54:13 +10:00
|
|
|
>
|
|
|
|
|
<canvas
|
|
|
|
|
ref={canvasRef}
|
|
|
|
|
width={width}
|
|
|
|
|
height={height}
|
|
|
|
|
style={{ width: "100%", height: "100%" }}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2020-04-18 18:11:21 +10:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MapDrawing;
|