Added fog toggle

This commit is contained in:
Mitchell McCaffrey 2020-04-29 20:40:34 +10:00
parent 9975f564fa
commit 5357b79e70
4 changed files with 69 additions and 35 deletions

View File

@ -99,6 +99,10 @@ function Map({
onFogDraw({ type: "remove", shapeIds: [shapeId], timestamp: Date.now() }); onFogDraw({ type: "remove", shapeIds: [shapeId], timestamp: Date.now() });
} }
function handleFogShapeEdit(shape) {
onFogDraw({ type: "edit", shapes: [shape], timestamp: Date.now() });
}
// Replay the draw actions and convert them to shapes for the map drawing // Replay the draw actions and convert them to shapes for the map drawing
useEffect(() => { useEffect(() => {
if (!mapState) { if (!mapState) {
@ -108,7 +112,7 @@ function Map({
let shapesById = {}; let shapesById = {};
for (let i = 0; i <= actionIndex; i++) { for (let i = 0; i <= actionIndex; i++) {
const action = actions[i]; const action = actions[i];
if (action.type === "add") { if (action.type === "add" || action.type === "edit") {
for (let shape of action.shapes) { for (let shape of action.shapes) {
shapesById[shape.id] = shape; shapesById[shape.id] = shape;
} }
@ -233,6 +237,7 @@ function Map({
shapes={fogShapes} shapes={fogShapes}
onShapeAdd={handleFogShapeAdd} onShapeAdd={handleFogShapeAdd}
onShapeRemove={handleFogShapeRemove} onShapeRemove={handleFogShapeRemove}
onShapeEdit={handleFogShapeEdit}
gridSize={gridSizeNormalized} gridSize={gridSizeNormalized}
/> />
); );

View File

@ -27,7 +27,7 @@ function MapDrawing({
const canvasRef = useRef(); const canvasRef = useRef();
const containerRef = useRef(); const containerRef = useRef();
const [isDrawing, setIsDrawing] = useState(false); const [isPointerDown, setIsPointerDown] = useState(false);
const [drawingShape, setDrawingShape] = useState(null); const [drawingShape, setDrawingShape] = useState(null);
const [pointerPosition, setPointerPosition] = useState({ x: -1, y: -1 }); const [pointerPosition, setPointerPosition] = useState({ x: -1, y: -1 });
@ -49,14 +49,14 @@ function MapDrawing({
return; return;
} }
if (event.touches && event.touches.length !== 1) { if (event.touches && event.touches.length !== 1) {
setIsDrawing(false); setIsPointerDown(false);
setDrawingShape(null); setDrawingShape(null);
return; return;
} }
const pointer = event.touches ? event.touches[0] : event; const pointer = event.touches ? event.touches[0] : event;
const position = getRelativePointerPosition(pointer, containerRef.current); const position = getRelativePointerPosition(pointer, containerRef.current);
setPointerPosition(position); setPointerPosition(position);
setIsDrawing(true); setIsPointerDown(true);
const brushPosition = getBrushPositionForTool( const brushPosition = getBrushPositionForTool(
position, position,
selectedTool, selectedTool,
@ -104,7 +104,7 @@ function MapDrawing({
); );
setPointerPosition(position); setPointerPosition(position);
} }
if (isDrawing) { if (isPointerDown) {
const position = getRelativePointerPosition( const position = getRelativePointerPosition(
pointer, pointer,
containerRef.current containerRef.current
@ -168,10 +168,10 @@ function MapDrawing({
onShapeAdd(drawingShape); onShapeAdd(drawingShape);
} }
if (selectedTool === "erase" && hoveredShapeRef.current && isDrawing) { if (selectedTool === "erase" && hoveredShapeRef.current && isPointerDown) {
onShapeRemove(hoveredShapeRef.current.id); onShapeRemove(hoveredShapeRef.current.id);
} }
setIsDrawing(false); setIsPointerDown(false);
setDrawingShape(null); setDrawingShape(null);
} }
@ -229,7 +229,7 @@ function MapDrawing({
width, width,
height, height,
pointerPosition, pointerPosition,
isDrawing, isPointerDown,
selectedTool, selectedTool,
drawingShape, drawingShape,
gridSize, gridSize,

View File

@ -12,6 +12,8 @@ import {
import MapInteractionContext from "../../contexts/MapInteractionContext"; import MapInteractionContext from "../../contexts/MapInteractionContext";
import diagonalPattern from "../../images/DiagonalPattern.png";
function MapFog({ function MapFog({
width, width,
height, height,
@ -20,12 +22,13 @@ function MapFog({
shapes, shapes,
onShapeAdd, onShapeAdd,
onShapeRemove, onShapeRemove,
onShapeEdit,
gridSize, gridSize,
}) { }) {
const canvasRef = useRef(); const canvasRef = useRef();
const containerRef = useRef(); const containerRef = useRef();
const [isDrawing, setIsDrawing] = useState(false); const [isPointerDown, setIsPointerDown] = useState(false);
const [drawingShape, setDrawingShape] = useState(null); const [drawingShape, setDrawingShape] = useState(null);
const [pointerPosition, setPointerPosition] = useState({ x: -1, y: -1 }); const [pointerPosition, setPointerPosition] = useState({ x: -1, y: -1 });
@ -45,14 +48,14 @@ function MapFog({
return; return;
} }
if (event.touches && event.touches.length !== 1) { if (event.touches && event.touches.length !== 1) {
setIsDrawing(false); setIsPointerDown(false);
setDrawingShape(null); setDrawingShape(null);
return; return;
} }
const pointer = event.touches ? event.touches[0] : event; const pointer = event.touches ? event.touches[0] : event;
const position = getRelativePointerPosition(pointer, containerRef.current); const position = getRelativePointerPosition(pointer, containerRef.current);
setPointerPosition(position); setPointerPosition(position);
setIsDrawing(true); setIsPointerDown(true);
const brushPosition = getBrushPositionForTool( const brushPosition = getBrushPositionForTool(
position, position,
"fog", "fog",
@ -68,6 +71,7 @@ function MapFog({
color: "black", color: "black",
blend: true, // Blend while drawing blend: true, // Blend while drawing
id: shortid.generate(), id: shortid.generate(),
visible: true,
}); });
} }
} }
@ -85,7 +89,7 @@ function MapFog({
if (shouldHover) { if (shouldHover) {
setPointerPosition(position); setPointerPosition(position);
} }
if (isDrawing) { if (isPointerDown) {
setPointerPosition(position); setPointerPosition(position);
const brushPosition = getBrushPositionForTool( const brushPosition = getBrushPositionForTool(
position, position,
@ -140,15 +144,18 @@ function MapFog({
} }
} }
if ( if (hoveredShapeRef.current && isPointerDown) {
toolSettings.type === "remove" && if (toolSettings.type === "remove") {
hoveredShapeRef.current &&
isDrawing
) {
onShapeRemove(hoveredShapeRef.current.id); onShapeRemove(hoveredShapeRef.current.id);
} else if (toolSettings.type === "toggle") {
onShapeEdit({
...hoveredShapeRef.current,
visible: !hoveredShapeRef.current.visible,
});
}
} }
setDrawingShape(null); setDrawingShape(null);
setIsDrawing(false); setIsPointerDown(false);
} }
// Add listeners for draw events on map to allow drawing past the bounds // Add listeners for draw events on map to allow drawing past the bounds
@ -176,6 +183,14 @@ function MapFog({
* Rendering * Rendering
*/ */
const hoveredShapeRef = useRef(null); const hoveredShapeRef = useRef(null);
const diagonalPatternRef = useRef();
useEffect(() => {
let image = new Image();
image.src = diagonalPattern;
diagonalPatternRef.current = image;
}, []);
useEffect(() => { useEffect(() => {
const canvas = canvasRef.current; const canvas = canvasRef.current;
if (canvas) { if (canvas) {
@ -183,23 +198,30 @@ function MapFog({
context.clearRect(0, 0, width, height); context.clearRect(0, 0, width, height);
let hoveredShape = null; let hoveredShape = null;
if (isEditing) {
const editPattern = context.createPattern(
diagonalPatternRef.current,
"repeat"
);
for (let shape of shapes) { for (let shape of shapes) {
if (shouldHover) { if (shouldHover) {
if (isShapeHovered(shape, context, pointerPosition, width, height)) { if (
isShapeHovered(shape, context, pointerPosition, width, height)
) {
hoveredShape = shape; hoveredShape = shape;
} }
} }
if (isEditing) {
drawShape( drawShape(
{ ...shape, blend: true }, {
...shape,
blend: true,
color: shape.visible ? "black" : editPattern,
},
context, context,
gridSize, gridSize,
width, width,
height height
); );
} else {
drawShape(shape, context, gridSize, width, height);
}
} }
if (drawingShape) { if (drawingShape) {
drawShape(drawingShape, context, gridSize, width, height); drawShape(drawingShape, context, gridSize, width, height);
@ -208,6 +230,14 @@ function MapFog({
const shape = { ...hoveredShape, color: "#BB99FF", blend: true }; const shape = { ...hoveredShape, color: "#BB99FF", blend: true };
drawShape(shape, context, gridSize, width, height); drawShape(shape, context, gridSize, width, height);
} }
} else {
// Not editing
for (let shape of shapes) {
if (shape.visible) {
drawShape(shape, context, gridSize, width, height);
}
}
}
hoveredShapeRef.current = hoveredShape; hoveredShapeRef.current = hoveredShape;
} }
}, [ }, [
@ -215,7 +245,6 @@ function MapFog({
width, width,
height, height,
pointerPosition, pointerPosition,
isDrawing,
isEditing, isEditing,
drawingShape, drawingShape,
gridSize, gridSize,

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB