Skip to content

Commit 8693a05

Browse files
authored
Fix polygon_drawing_to_walls.js for V11
Changed the macro's logic to account for the changes of API that happened in from V8-9 to V11
1 parent 003e6fd commit 8693a05

File tree

1 file changed

+41
-46
lines changed

1 file changed

+41
-46
lines changed

misc/polygon_drawing_to_walls.js

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,44 @@
33
* @Author: cole#9640
44
*/
55

6-
function toRadians(degrees)
7-
{
8-
var pi = Math.PI;
9-
return degrees * (pi/180);
10-
}
11-
12-
13-
let drawings = canvas.drawings.controlled;
14-
15-
drawings = drawings.filter(drawing => {
16-
if (!drawing.isPolygon) {
17-
ui.notifications.warn(`Drawing "${drawing.data._id}" is not a polygon, skipping`);
18-
return false;
19-
}
20-
return true;
21-
});
22-
23-
if (drawings.length) {
24-
const newWalls = drawings.flatMap((drawing) => {
25-
const { x, y, width, height } = drawing.data;
26-
const xCenterOffset = width/2;
27-
const yCenterOffset = height/2;
28-
29-
const θ = toRadians(drawing.data.rotation);
30-
const cosθ = Math.cos(θ);
31-
const sinθ = Math.sin(θ);
32-
33-
const points = drawing.data.points.map((point) => {
34-
const offsetX = point[0] - xCenterOffset;
35-
const offsetY = point[1] - yCenterOffset;
36-
const rotatedX = (offsetX * cosθ - offsetY * sinθ);
37-
const rotatedY = (offsetY * cosθ + offsetX * sinθ);
38-
return [rotatedX + x + xCenterOffset, rotatedY + y + yCenterOffset];
39-
});
40-
41-
return points.slice(0, points.length - 1)
42-
.map((point, i) => {
43-
return { c: point.concat(points[i + 1]) };
44-
});
45-
});
46-
47-
canvas.scene.createEmbeddedDocuments("Wall", newWalls);
48-
canvas.walls.activate();
49-
} else {
50-
ui.notifications.error("No polygon drawings selected!");
51-
}
6+
let drawings = canvas.drawings.controlled;
7+
8+
drawings = drawings.filter(drawing => {
9+
if (!drawing.isPolygon) {
10+
ui.notifications.warn(`Drawing "${drawing.document._id}" is not a polygon, skipping`);
11+
return false;
12+
}
13+
return true;
14+
});
15+
16+
if (drawings.length) {
17+
const newWalls = drawings.flatMap((drawing) => {
18+
const { x, y } = drawing.document;
19+
const { width, height } = drawing.document.shape;
20+
const xCenterOffset = width / 2;
21+
const yCenterOffset = height / 2;
22+
23+
const o = Math.toRadians(drawing.document.rotation);
24+
const coso = Math.cos(o);
25+
const sino = Math.sin(o);
26+
27+
pts = drawing.document.shape.points;
28+
points = [];
29+
for (i = 0; i < pts.length - 1; i += 2) {
30+
const offsetX = pts[i] - xCenterOffset;
31+
const offsetY = pts[i + 1] - yCenterOffset;
32+
const rotatedX = (offsetX * coso - offsetY * sino);
33+
const rotatedY = (offsetY * coso + offsetX * sino);
34+
points.push([rotatedX + x + xCenterOffset, rotatedY + y + yCenterOffset]);
35+
}
36+
return points.slice(0, points.length - 1)
37+
.map((point, i) => {
38+
return { c: point.concat(points[i + 1]) };
39+
});
40+
});
41+
42+
canvas.scene.createEmbeddedDocuments("Wall", newWalls).then(as => { canvas.walls.activate(); });
43+
44+
} else {
45+
ui.notifications.error("No polygon drawings selected!");
46+
}

0 commit comments

Comments
 (0)