From 924b3e248114ef2d1a30d5d029d063a329dfb2f8 Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Thu, 4 Feb 2021 09:59:03 +1100 Subject: [PATCH] Moved draw action helpers into new file to fix import errors with workers --- src/actions/Action.js | 2 ++ src/actions/CutShapeAction.js | 2 +- src/actions/SubtractShapeAction.js | 2 +- src/helpers/actions.js | 37 +++++++++++++++++++++++++++ src/helpers/drawing.js | 40 ------------------------------ 5 files changed, 41 insertions(+), 42 deletions(-) create mode 100644 src/helpers/actions.js diff --git a/src/actions/Action.js b/src/actions/Action.js index 3d52f4c..a147f2b 100644 --- a/src/actions/Action.js +++ b/src/actions/Action.js @@ -1,3 +1,5 @@ +// Load Diff for auto complete +// eslint-disable-next-line no-unused-vars import { Diff } from "deep-diff"; import { diff, revertChanges } from "../helpers/diff"; diff --git a/src/actions/CutShapeAction.js b/src/actions/CutShapeAction.js index d6ccf56..534b941 100644 --- a/src/actions/CutShapeAction.js +++ b/src/actions/CutShapeAction.js @@ -4,7 +4,7 @@ import Action from "./Action"; import { addPolygonDifferenceToShapes, addPolygonIntersectionToShapes, -} from "../helpers/drawing"; +} from "../helpers/actions"; class CutShapeAction extends Action { constructor(shapes) { diff --git a/src/actions/SubtractShapeAction.js b/src/actions/SubtractShapeAction.js index 6a1aece..712bbff 100644 --- a/src/actions/SubtractShapeAction.js +++ b/src/actions/SubtractShapeAction.js @@ -1,7 +1,7 @@ import polygonClipping from "polygon-clipping"; import Action from "./Action"; -import { addPolygonDifferenceToShapes } from "../helpers/drawing"; +import { addPolygonDifferenceToShapes } from "../helpers/actions"; class SubtractShapeAction extends Action { constructor(shapes) { diff --git a/src/helpers/actions.js b/src/helpers/actions.js new file mode 100644 index 0000000..8847937 --- /dev/null +++ b/src/helpers/actions.js @@ -0,0 +1,37 @@ +export function addPolygonDifferenceToShapes(shape, difference, shapes) { + for (let i = 0; i < difference.length; i++) { + let newId = `${shape.id}-dif-${i}`; + // Holes detected + let holes = []; + if (difference[i].length > 1) { + for (let j = 1; j < difference[i].length; j++) { + holes.push(difference[i][j].map(([x, y]) => ({ x, y }))); + } + } + + shapes[newId] = { + ...shape, + id: newId, + data: { + points: difference[i][0].map(([x, y]) => ({ x, y })), + holes, + }, + }; + } +} + +export function addPolygonIntersectionToShapes(shape, intersection, shapes) { + for (let i = 0; i < intersection.length; i++) { + let newId = `${shape.id}-int-${i}`; + shapes[newId] = { + ...shape, + id: newId, + data: { + points: intersection[i][0].map(([x, y]) => ({ x, y })), + holes: [], + }, + // Default intersection visibility to false + visible: false, + }; + } +} diff --git a/src/helpers/drawing.js b/src/helpers/drawing.js index 7d1301f..5babe00 100644 --- a/src/helpers/drawing.js +++ b/src/helpers/drawing.js @@ -10,7 +10,6 @@ const snappingThreshold = 1 / 5; export function getBrushPosition(map, mapStage, useGridSnappning, gridSize) { const mapImage = mapStage.findOne("#mapImage"); let position = getRelativePointerPositionNormalized(mapImage); - if (useGridSnappning) { // Snap to corners of grid // Subtract offset to transform into offset space then add it back transform back @@ -20,7 +19,6 @@ export function getBrushPosition(map, mapStage, useGridSnappning, gridSize) { offset ); const gridDistance = Vector2.length(Vector2.subtract(gridSnap, position)); - // Snap to center of grid // Subtract offset and half size to transform it into offset half space then transform it back const halfSize = Vector2.multiply(gridSize, 0.5); @@ -219,44 +217,6 @@ export function simplifyPoints(points, gridSize, scale) { ); } -export function addPolygonDifferenceToShapes(shape, difference, shapes) { - for (let i = 0; i < difference.length; i++) { - let newId = `${shape.id}-dif-${i}`; - // Holes detected - let holes = []; - if (difference[i].length > 1) { - for (let j = 1; j < difference[i].length; j++) { - holes.push(difference[i][j].map(([x, y]) => ({ x, y }))); - } - } - - shapes[newId] = { - ...shape, - id: newId, - data: { - points: difference[i][0].map(([x, y]) => ({ x, y })), - holes, - }, - }; - } -} - -export function addPolygonIntersectionToShapes(shape, intersection, shapes) { - for (let i = 0; i < intersection.length; i++) { - let newId = `${shape.id}-int-${i}`; - shapes[newId] = { - ...shape, - id: newId, - data: { - points: intersection[i][0].map(([x, y]) => ({ x, y })), - holes: [], - }, - // Default intersection visibility to false - visible: false, - }; - } -} - export function mergeShapes(shapes) { if (shapes.length === 0) { return shapes;