2020-10-17 10:45:59 +11:00
|
|
|
import React from "react";
|
2021-02-04 15:06:34 +11:00
|
|
|
import { Line, Group, RegularPolygon } from "react-konva";
|
2020-10-17 10:45:59 +11:00
|
|
|
|
2021-02-07 11:16:36 +11:00
|
|
|
import { getCellLocation } from "../helpers/grid";
|
2021-02-06 19:29:24 +11:00
|
|
|
import Vector2 from "../helpers/Vector2";
|
2020-10-17 10:45:59 +11:00
|
|
|
|
2021-02-06 13:32:38 +11:00
|
|
|
import { useGrid } from "../contexts/GridContext";
|
|
|
|
|
|
|
|
|
|
function Grid({ strokeWidth, stroke }) {
|
|
|
|
|
const {
|
|
|
|
|
grid,
|
|
|
|
|
gridStrokeWidth,
|
|
|
|
|
gridPixelSize,
|
|
|
|
|
gridOffset,
|
|
|
|
|
gridCellPixelSize,
|
|
|
|
|
} = useGrid();
|
|
|
|
|
|
2021-02-04 15:06:34 +11:00
|
|
|
if (!grid?.size.x || !grid?.size.y) {
|
2020-10-17 10:45:59 +11:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-06 19:29:24 +11:00
|
|
|
const negativeGridOffset = Vector2.multiply(gridOffset, -1);
|
2021-02-07 11:16:36 +11:00
|
|
|
const finalStrokeWidth = gridStrokeWidth * strokeWidth;
|
2021-02-06 19:29:24 +11:00
|
|
|
|
2021-02-04 15:06:34 +11:00
|
|
|
const shapes = [];
|
|
|
|
|
if (grid.type === "square") {
|
|
|
|
|
for (let x = 1; x < grid.size.x; x++) {
|
|
|
|
|
shapes.push(
|
|
|
|
|
<Line
|
|
|
|
|
key={`grid_x_${x}`}
|
2021-02-06 13:32:38 +11:00
|
|
|
points={[
|
|
|
|
|
x * gridCellPixelSize.width,
|
|
|
|
|
0,
|
|
|
|
|
x * gridCellPixelSize.width,
|
|
|
|
|
gridPixelSize.height,
|
|
|
|
|
]}
|
2021-02-04 15:06:34 +11:00
|
|
|
stroke={stroke}
|
2021-02-07 11:16:36 +11:00
|
|
|
strokeWidth={finalStrokeWidth}
|
2021-02-04 15:06:34 +11:00
|
|
|
opacity={0.5}
|
2021-02-06 19:29:24 +11:00
|
|
|
offset={negativeGridOffset}
|
2021-02-04 15:06:34 +11:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
for (let y = 1; y < grid.size.y; y++) {
|
|
|
|
|
shapes.push(
|
|
|
|
|
<Line
|
|
|
|
|
key={`grid_y_${y}`}
|
2021-02-06 13:32:38 +11:00
|
|
|
points={[
|
|
|
|
|
0,
|
|
|
|
|
y * gridCellPixelSize.height,
|
|
|
|
|
gridPixelSize.width,
|
|
|
|
|
y * gridCellPixelSize.height,
|
|
|
|
|
]}
|
2021-02-04 15:06:34 +11:00
|
|
|
stroke={stroke}
|
2021-02-07 11:16:36 +11:00
|
|
|
strokeWidth={finalStrokeWidth}
|
2021-02-04 15:06:34 +11:00
|
|
|
opacity={0.5}
|
2021-02-06 19:29:24 +11:00
|
|
|
offset={negativeGridOffset}
|
2021-02-04 15:06:34 +11:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else if (grid.type === "hexVertical" || grid.type === "hexHorizontal") {
|
2021-02-07 11:16:36 +11:00
|
|
|
// End at grid size + 1 to overshoot the bounds of the grid to ensure all lines are drawn
|
|
|
|
|
for (let x = 0; x < grid.size.x + 1; x++) {
|
|
|
|
|
for (let y = 0; y < grid.size.y + 1; y++) {
|
2021-02-06 13:32:38 +11:00
|
|
|
const cellLocation = getCellLocation(grid, x, y, gridCellPixelSize);
|
2021-02-04 15:06:34 +11:00
|
|
|
shapes.push(
|
|
|
|
|
<Group
|
|
|
|
|
key={`grid_${x}_${y}`}
|
|
|
|
|
x={cellLocation.x}
|
|
|
|
|
y={cellLocation.y}
|
2021-02-06 19:29:24 +11:00
|
|
|
offset={negativeGridOffset}
|
2021-02-04 15:06:34 +11:00
|
|
|
>
|
2021-02-07 11:16:36 +11:00
|
|
|
{/* Offset the hex tile to align to top left of grid */}
|
|
|
|
|
<Group offset={Vector2.multiply(gridCellPixelSize, -0.5)}>
|
|
|
|
|
<RegularPolygon
|
|
|
|
|
sides={6}
|
|
|
|
|
radius={gridCellPixelSize.radius}
|
|
|
|
|
stroke={stroke}
|
|
|
|
|
strokeWidth={finalStrokeWidth}
|
|
|
|
|
opacity={0.5}
|
|
|
|
|
rotation={grid.type === "hexVertical" ? 0 : 90}
|
|
|
|
|
/>
|
|
|
|
|
</Group>
|
2021-02-04 15:06:34 +11:00
|
|
|
</Group>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-17 10:45:59 +11:00
|
|
|
}
|
|
|
|
|
|
2021-02-07 11:16:36 +11:00
|
|
|
return (
|
|
|
|
|
<Group
|
|
|
|
|
// Clip grid to bounds to cover hex overshoot
|
|
|
|
|
clipFunc={(context) => {
|
|
|
|
|
context.rect(
|
|
|
|
|
gridOffset.x - finalStrokeWidth / 2,
|
|
|
|
|
gridOffset.y - finalStrokeWidth / 2,
|
|
|
|
|
gridPixelSize.width + finalStrokeWidth,
|
|
|
|
|
gridPixelSize.height + finalStrokeWidth
|
|
|
|
|
);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{shapes}
|
|
|
|
|
</Group>
|
|
|
|
|
);
|
2020-10-17 10:45:59 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Grid.defaultProps = {
|
|
|
|
|
strokeWidth: 0.1,
|
|
|
|
|
stroke: "white",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Grid;
|