Added snap to grid option to map settings

This commit is contained in:
Mitchell McCaffrey 2020-08-07 12:28:50 +10:00
parent 6b97614a60
commit 76cad29f2b
9 changed files with 52 additions and 55 deletions

View File

@ -284,6 +284,7 @@ function Map({
const mapDrawing = ( const mapDrawing = (
<MapDrawing <MapDrawing
map={map}
shapes={mapShapes} shapes={mapShapes}
onShapeAdd={handleMapShapeAdd} onShapeAdd={handleMapShapeAdd}
onShapesRemove={handleMapShapesRemove} onShapesRemove={handleMapShapesRemove}
@ -296,6 +297,7 @@ function Map({
const mapFog = ( const mapFog = (
<MapFog <MapFog
map={map}
shapes={fogShapes} shapes={fogShapes}
onShapeAdd={handleFogShapeAdd} onShapeAdd={handleFogShapeAdd}
onShapeSubtract={handleFogShapeSubtract} onShapeSubtract={handleFogShapeSubtract}
@ -315,6 +317,7 @@ function Map({
const mapMeasure = ( const mapMeasure = (
<MapMeasure <MapMeasure
map={map}
active={selectedToolId === "measure"} active={selectedToolId === "measure"}
gridSize={gridSizeNormalized} gridSize={gridSizeNormalized}
selectedToolSettings={toolSettings[selectedToolId]} selectedToolSettings={toolSettings[selectedToolId]}

View File

@ -18,6 +18,7 @@ import { getRelativePointerPositionNormalized } from "../../helpers/konva";
import colors from "../../helpers/colors"; import colors from "../../helpers/colors";
function MapDrawing({ function MapDrawing({
map,
shapes, shapes,
onShapeAdd, onShapeAdd,
onShapesRemove, onShapesRemove,
@ -52,6 +53,7 @@ function MapDrawing({
function getBrushPosition() { function getBrushPosition() {
const mapImage = mapStage.findOne("#mapImage"); const mapImage = mapStage.findOne("#mapImage");
return getBrushPositionForTool( return getBrushPositionForTool(
map,
getRelativePointerPositionNormalized(mapImage), getRelativePointerPositionNormalized(mapImage),
toolId, toolId,
toolSettings, toolSettings,
@ -153,23 +155,7 @@ function MapDrawing({
interactionEmitter.off("drag", handleBrushMove); interactionEmitter.off("drag", handleBrushMove);
interactionEmitter.off("dragEnd", handleBrushUp); interactionEmitter.off("dragEnd", handleBrushUp);
}; };
}, [ });
drawingShape,
erasingShapes,
gridSize,
isBrush,
isBrushDown,
active,
toolId,
isShape,
mapStageRef,
onShapeAdd,
onShapesRemove,
toolSettings,
shapes,
stageScale,
interactionEmitter,
]);
function handleShapeOver(shape, isDown) { function handleShapeOver(shape, isDown) {
if (shouldHover && isDown) { if (shouldHover && isDown) {

View File

@ -22,6 +22,7 @@ import {
} from "../../helpers/konva"; } from "../../helpers/konva";
function MapFog({ function MapFog({
map,
shapes, shapes,
onShapeAdd, onShapeAdd,
onShapeSubtract, onShapeSubtract,
@ -57,6 +58,7 @@ function MapFog({
function getBrushPosition() { function getBrushPosition() {
const mapImage = mapStage.findOne("#mapImage"); const mapImage = mapStage.findOne("#mapImage");
return getBrushPositionForTool( return getBrushPositionForTool(
map,
getRelativePointerPositionNormalized(mapImage), getRelativePointerPositionNormalized(mapImage),
toolId, toolId,
toolSettings, toolSettings,
@ -223,23 +225,7 @@ function MapFog({
mapStage.off("mousemove touchmove", handlePolygonMove); mapStage.off("mousemove touchmove", handlePolygonMove);
mapStage.off("click tap", handlePolygonClick); mapStage.off("click tap", handlePolygonClick);
}; };
}, [ });
mapStageRef,
active,
drawingShape,
editingShapes,
gridSize,
isBrushDown,
onShapeAdd,
onShapeSubtract,
onShapesEdit,
onShapesRemove,
toolId,
toolSettings,
shapes,
stageScale,
interactionEmitter,
]);
const finishDrawingPolygon = useCallback(() => { const finishDrawingPolygon = useCallback(() => {
const subtract = toolSettings.useFogSubtract; const subtract = toolSettings.useFogSubtract;

View File

@ -13,7 +13,7 @@ import {
import { getRelativePointerPositionNormalized } from "../../helpers/konva"; import { getRelativePointerPositionNormalized } from "../../helpers/konva";
import * as Vector2 from "../../helpers/vector2"; import * as Vector2 from "../../helpers/vector2";
function MapMeasure({ selectedToolSettings, active, gridSize }) { function MapMeasure({ map, selectedToolSettings, active, gridSize }) {
const { stageScale, mapWidth, mapHeight, interactionEmitter } = useContext( const { stageScale, mapWidth, mapHeight, interactionEmitter } = useContext(
MapInteractionContext MapInteractionContext
); );
@ -36,6 +36,7 @@ function MapMeasure({ selectedToolSettings, active, gridSize }) {
function getBrushPosition() { function getBrushPosition() {
const mapImage = mapStage.findOne("#mapImage"); const mapImage = mapStage.findOne("#mapImage");
return getBrushPositionForTool( return getBrushPositionForTool(
map,
getRelativePointerPositionNormalized(mapImage), getRelativePointerPositionNormalized(mapImage),
"drawing", "drawing",
{ type: "line" }, { type: "line" },
@ -87,15 +88,7 @@ function MapMeasure({ selectedToolSettings, active, gridSize }) {
interactionEmitter.off("drag", handleBrushMove); interactionEmitter.off("drag", handleBrushMove);
interactionEmitter.off("dragEnd", handleBrushUp); interactionEmitter.off("dragEnd", handleBrushUp);
}; };
}, [ });
drawingShapeData,
gridSize,
isBrushDown,
mapStageRef,
interactionEmitter,
active,
selectedToolSettings,
]);
function renderShape(shapeData) { function renderShape(shapeData) {
const linePoints = shapeData.points.reduce( const linePoints = shapeData.points.reduce(

View File

@ -103,7 +103,7 @@ function MapSettings({
<Flex <Flex
mt={2} mt={2}
mb={map.type === "default" ? 2 : 0} mb={map.type === "default" ? 2 : 0}
sx={{ alignItems: "center" }} sx={{ alignItems: "flex-end" }}
> >
<Box sx={{ width: "50%" }}> <Box sx={{ width: "50%" }}>
<Label>Grid Type</Label> <Label>Grid Type</Label>
@ -116,14 +116,28 @@ function MapSettings({
<option disabled>Hex (Coming Soon)</option> <option disabled>Hex (Coming Soon)</option>
</Select> </Select>
</Box> </Box>
<Label sx={{ width: "50%" }} ml={2}> <Flex sx={{ width: "50%", flexDirection: "column" }} ml={2}>
<Label>
<Checkbox <Checkbox
checked={!mapEmpty && map.showGrid} checked={!mapEmpty && map.showGrid}
disabled={mapEmpty || map.type === "default"} disabled={mapEmpty || map.type === "default"}
onChange={(e) => onSettingsChange("showGrid", e.target.checked)} onChange={(e) =>
onSettingsChange("showGrid", e.target.checked)
}
/> />
Show Grid Show Grid
</Label> </Label>
<Label>
<Checkbox
checked={!mapEmpty && map.snapToGrid}
disabled={mapEmpty || map.type === "default"}
onChange={(e) =>
onSettingsChange("snapToGrid", e.target.checked)
}
/>
Snap to Grid
</Label>
</Flex>
</Flex> </Flex>
{!mapEmpty && map.type !== "default" && ( {!mapEmpty && map.type !== "default" && (
<Flex my={2} sx={{ alignItems: "center" }}> <Flex my={2} sx={{ alignItems: "center" }}>

View File

@ -44,6 +44,7 @@ export function MapDataProvider({ children }) {
lastModified: Date.now() + i, lastModified: Date.now() + i,
gridType: "grid", gridType: "grid",
showGrid: false, showGrid: false,
snapToGrid: true,
}); });
// Add a state for the map if there isn't one already // Add a state for the map if there isn't one already
const state = await database.table("states").get(id); const state = await database.table("states").get(id);

View File

@ -144,6 +144,17 @@ function loadVersions(db) {
} }
}); });
}); });
// v1.5.0 - Added map snap to grid option
db.version(8)
.stores({})
.upgrade((tx) => {
return tx
.table("maps")
.toCollection()
.modify((map) => {
map.snapToGrid = true;
});
});
} }
// Get the dexie database used in DatabaseContext // Get the dexie database used in DatabaseContext

View File

@ -6,6 +6,7 @@ import { toDegrees, omit } from "./shared";
const snappingThreshold = 1 / 5; const snappingThreshold = 1 / 5;
export function getBrushPositionForTool( export function getBrushPositionForTool(
map,
brushPosition, brushPosition,
tool, tool,
toolSettings, toolSettings,
@ -14,12 +15,13 @@ export function getBrushPositionForTool(
) { ) {
let position = brushPosition; let position = brushPosition;
const useGridSnappning = const useGridSnappning =
(tool === "drawing" && map.snapToGrid &&
((tool === "drawing" &&
(toolSettings.type === "line" || (toolSettings.type === "line" ||
toolSettings.type === "rectangle" || toolSettings.type === "rectangle" ||
toolSettings.type === "circle" || toolSettings.type === "circle" ||
toolSettings.type === "triangle")) || toolSettings.type === "triangle")) ||
(tool === "fog" && toolSettings.type === "polygon"); (tool === "fog" && toolSettings.type === "polygon"));
if (useGridSnappning) { if (useGridSnappning) {
// Snap to corners of grid // Snap to corners of grid

View File

@ -22,6 +22,7 @@ const defaultMapProps = {
// TODO: add support for hex horizontal and hex vertical // TODO: add support for hex horizontal and hex vertical
gridType: "grid", gridType: "grid",
showGrid: false, showGrid: false,
snapToGrid: true,
quality: "original", quality: "original",
}; };