Skip to content

Commit 5ec762c

Browse files
committed
Update for V12
1 parent 0d918db commit 5ec762c

File tree

7 files changed

+155
-92
lines changed

7 files changed

+155
-92
lines changed

module.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
"email": "dev7355608@gmail.com"
1010
}
1111
],
12-
"version": "2.1.0",
12+
"version": "2.1.1",
1313
"compatibility": {
1414
"minimum": "10",
15-
"verified": "11"
15+
"verified": "12"
1616
},
1717
"minimumCoreVersion": "10",
1818
"esmodules": [
@@ -28,8 +28,8 @@
2828
},
2929
"url": "https://github.com/dev7355608/advanced-drawing-tools",
3030
"manifest": "https://raw.githubusercontent.com/dev7355608/advanced-drawing-tools/main/module.json",
31-
"download": "https://github.com/dev7355608/advanced-drawing-tools/releases/download/v2.1.0/module.zip",
32-
"changelog": "https://github.com/dev7355608/advanced-drawing-tools/releases/tag/v2.1.0",
31+
"download": "https://github.com/dev7355608/advanced-drawing-tools/releases/download/v2.1.1/module.zip",
32+
"changelog": "https://github.com/dev7355608/advanced-drawing-tools/releases/tag/v2.1.1",
3333
"bugs": "https://github.com/dev7355608/advanced-drawing-tools/issues",
3434
"readme": "https://raw.githubusercontent.com/dev7355608/advanced-drawing-tools/main/README.md",
3535
"license": "https://raw.githubusercontent.com/dev7355608/advanced-drawing-tools/main/LICENSE"

scripts/convert.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { MODULE_NAME } from "./const.js";
22

33
Drawing.prototype._convertToPolygon = async function ({ confirm = false } = {}) {
4-
if (this.document.shape.type === CONST.DRAWING_TYPES.POLYGON) {
4+
if (this.document.shape.type === "p") {
55
return;
66
}
77

@@ -17,11 +17,11 @@ Drawing.prototype._convertToPolygon = async function ({ confirm = false } = {})
1717
this.document.reset();
1818

1919
const { x, y, shape: { width, height, type } } = this.document;
20-
let update = { x, y, shape: { width, height, type: CONST.DRAWING_TYPES.POLYGON } };
20+
let update = { x, y, shape: { width, height, type: "p" } };
2121

22-
if (type === CONST.DRAWING_TYPES.RECTANGLE) {
22+
if (type === "r") {
2323
update.shape.points = [0, 0, width, 0, width, height, 0, height];
24-
} else if (type === CONST.DRAWING_TYPES.ELLIPSE) {
24+
} else if (type === "e") {
2525
const rx = width / 2;
2626
const ry = height / 2;
2727

@@ -84,15 +84,17 @@ Drawing.prototype._convertToPolygon = async function ({ confirm = false } = {})
8484
return;
8585
}
8686

87-
this.document.shape.type = CONST.DRAWING_TYPES.POLYGON;
87+
this.document.shape.type = "p";
8888
update = this._rescaleDimensions(update, 0, 0);
89-
update.shape.type = CONST.DRAWING_TYPES.POLYGON;
89+
update.shape.type = "p";
9090

9191
if (this.document.fillType === CONST.DRAWING_FILL_TYPES.NONE) {
9292
update.fillType = CONST.DRAWING_FILL_TYPES.SOLID;
9393
update.fillAlpha = 0;
9494
}
9595

96+
update.bezierFactor = 0;
97+
9698
await this.document.update(update);
9799
});
98100
};

scripts/edit-mode.js

+100-55
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,36 @@ Hooks.on("refreshDrawing", drawing => {
55
});
66

77
Hooks.once("libWrapper.Ready", () => {
8-
const getInteractionData = isNewerVersion(game.version, 11)
8+
const getInteractionData = foundry.utils.isNewerVersion(game.version, 11)
99
? (event) => event.interactionData
1010
: (event) => event.data;
11-
const getOriginalData = isNewerVersion(game.version, 11)
11+
const getOriginalData = foundry.utils.isNewerVersion(game.version, 11)
1212
? (drawing, event) => event.interactionData.originalData
1313
: (drawing, event) => drawing._original;
14-
const setOriginalData = isNewerVersion(game.version, 11)
14+
const setOriginalData = foundry.utils.isNewerVersion(game.version, 11)
1515
? (drawing, event) => event.interactionData.originalData = drawing.document.toObject()
1616
: (drawing, event) => drawing._original = drawing.document.toObject();
17-
const setRestoreOriginalData = isNewerVersion(game.version, 11)
17+
const setRestoreOriginalData = foundry.utils.isNewerVersion(game.version, 11)
1818
? (drawing, event, value) => event.interactionData.restoreOriginalData = value
1919
: (drawing, event, value) => { };
20-
const refreshShape = isNewerVersion(game.version, 11)
21-
? (drawing) => drawing.renderFlags.set({ refreshShape: true })
22-
: (drawing) => drawing.refresh();
23-
24-
libWrapper.register(MODULE_ID, "Drawing.prototype.activateListeners", function (wrapped, ...args) {
25-
wrapped(...args);
26-
27-
const pointerup = isNewerVersion(game.version, 11) ? "pointerup" : "mouseup";
28-
29-
this.frame.handle.off(pointerup).on(pointerup, this._onHandleMouseUp.bind(this));
30-
}, libWrapper.WRAPPER);
20+
const refreshSize = foundry.utils.isNewerVersion(game.version, 12)
21+
? (drawing) => drawing.renderFlags.set({ refreshSize: true })
22+
: foundry.utils.isNewerVersion(game.version, 11)
23+
? (drawing) => drawing.renderFlags.set({ refreshShape: true })
24+
: (drawing) => drawing.refresh();
25+
const isDraggingHandle = foundry.utils.isNewerVersion(game.version, 12)
26+
? (drawing, event) => event.interactionData.dragHandle
27+
: (drawing, event) => drawing._dragHandle;
28+
29+
if (!foundry.utils.isNewerVersion(game.version, 12)) {
30+
libWrapper.register(MODULE_ID, "Drawing.prototype.activateListeners", function (wrapped, ...args) {
31+
wrapped(...args);
32+
33+
const pointerup = foundry.utils.isNewerVersion(game.version, 11) ? "pointerup" : "mouseup";
34+
35+
this.frame.handle.off(pointerup).on(pointerup, this._onHandleMouseUp.bind(this));
36+
}, libWrapper.WRAPPER);
37+
}
3138

3239
libWrapper.register(MODULE_ID, "Drawing.prototype._onHandleHoverIn", function (event) {
3340
if (this._dragHandle) {
@@ -60,27 +67,29 @@ Hooks.once("libWrapper.Ready", () => {
6067
}
6168
}, libWrapper.OVERRIDE);
6269

63-
libWrapper.register(MODULE_ID, "Drawing.prototype._onHandleMouseDown", function (event) {
64-
const handle = event.target;
65-
66-
if (handle instanceof PointHandle || handle instanceof EdgeHandle) {
67-
if (!this.document.locked) {
68-
this._dragHandle = true;
69-
handle._hover = true;
70-
handle.refresh();
71-
this._editHandle = handle;
70+
if (!foundry.utils.isNewerVersion(game.version, 12)) {
71+
libWrapper.register(MODULE_ID, "Drawing.prototype._onHandleMouseDown", function (event) {
72+
const handle = event.target;
73+
74+
if (handle instanceof PointHandle || handle instanceof EdgeHandle) {
75+
if (!this.document.locked) {
76+
this._dragHandle = true;
77+
handle._hover = true;
78+
handle.refresh();
79+
this._editHandle = handle;
80+
} else {
81+
this._editHandle = null;
82+
}
7283
} else {
73-
this._editHandle = null;
84+
if (!this.document.locked) {
85+
this._dragHandle = true;
86+
}
7487
}
75-
} else {
76-
if (!this.document.locked) {
77-
this._dragHandle = true;
78-
}
79-
}
80-
}, libWrapper.OVERRIDE);
88+
}, libWrapper.OVERRIDE);
89+
}
8190

8291
libWrapper.register(MODULE_ID, "Drawing.prototype._onDragLeftStart", function (wrapped, event) {
83-
if (!this._dragHandle) {
92+
if (!isDraggingHandle(this, event)) {
8493
return wrapped(event);
8594
}
8695

@@ -112,7 +121,7 @@ Hooks.once("libWrapper.Ready", () => {
112121

113122
if (update) {
114123
this.document.updateSource(update);
115-
refreshShape(this);
124+
refreshSize(this);
116125
}
117126
}, libWrapper.MIXED);
118127

@@ -126,6 +135,12 @@ Hooks.once("libWrapper.Ready", () => {
126135
const originalEvent = event.data.originalEvent;
127136
let update;
128137

138+
if (!originalEvent.shiftKey) {
139+
if (foundry.utils.isNewerVersion(game.version, 12)) {
140+
destination = this.layer.getSnappedPoint(destination);
141+
}
142+
}
143+
129144
// Pan the canvas if the drag event approaches the edge
130145
canvas._onDragCanvasPan(originalEvent);
131146

@@ -157,7 +172,7 @@ Hooks.once("libWrapper.Ready", () => {
157172

158173
try {
159174
this.document.updateSource(update);
160-
refreshShape(this);
175+
refreshSize(this);
161176
} catch (err) { }
162177
}, libWrapper.OVERRIDE);
163178

@@ -174,7 +189,11 @@ Hooks.once("libWrapper.Ready", () => {
174189
setRestoreOriginalData(this, event, false);
175190

176191
if (!originalEvent.shiftKey) {
177-
destination = canvas.grid.getSnappedPosition(destination.x, destination.y, this.layer.gridPrecision);
192+
if (foundry.utils.isNewerVersion(game.version, 12)) {
193+
destination = this.layer.getSnappedPoint(destination);
194+
} else {
195+
destination = canvas.grid.getSnappedPosition(destination.x, destination.y, this.layer.gridPrecision);
196+
}
178197
}
179198

180199
if (handle instanceof PointHandle || handle instanceof EdgeHandle) {
@@ -213,6 +232,22 @@ Hooks.once("libWrapper.Ready", () => {
213232
return this.document.update(update, { diff: false });
214233
}, libWrapper.OVERRIDE);
215234

235+
if (foundry.utils.isNewerVersion(game.version, 12)) {
236+
libWrapper.register(MODULE_ID, "Drawing.prototype._onClickLeft", function (wrapped, event) {
237+
this._editHandle = null;
238+
239+
if (this._editHandles?.points.children.includes(event.target)
240+
|| this._editHandles?.edges.children.includes(event.target)) {
241+
event.interactionData.dragHandle = true;
242+
event.stopPropagation();
243+
this._editHandle = event.target;
244+
return;
245+
}
246+
247+
return wrapped(event);
248+
}, libWrapper.MIXED);
249+
}
250+
216251
libWrapper.register(MODULE_ID, "Drawing.prototype._onClickRight", function (wrapped, event) {
217252
let handle = getInteractionData(event).handle;
218253

@@ -221,6 +256,7 @@ Hooks.once("libWrapper.Ready", () => {
221256
}
222257

223258
if ((handle instanceof PointHandle || handle instanceof EdgeHandle) && handle._hover) {
259+
event.stopPropagation();
224260
this._dragHandle = false;
225261
handle._hover = false;
226262
handle.refresh();
@@ -252,12 +288,14 @@ Hooks.once("libWrapper.Ready", () => {
252288
return wrapped(event);
253289
}, libWrapper.MIXED);
254290

255-
Drawing.prototype._onHandleMouseUp = function (event) {
256-
if (!getOriginalData(this, event)) {
257-
this._dragHandle = false;
258-
this._editHandle = null;
259-
}
260-
};
291+
if (!foundry.utils.isNewerVersion(game.version, 12)) {
292+
Drawing.prototype._onHandleMouseUp = function (event) {
293+
if (!getOriginalData(this, event)) {
294+
this._dragHandle = false;
295+
this._editHandle = null;
296+
}
297+
};
298+
}
261299
});
262300

263301
Drawing.prototype._editMode = false;
@@ -287,7 +325,7 @@ Drawing.prototype._editHandles = null;
287325
Drawing.prototype._refreshEditMode = function () {
288326
const document = this.document;
289327

290-
if (this._editMode && this.layer.active && !document._source.locked && document.shape.type === CONST.DRAWING_TYPES.POLYGON) {
328+
if (this._editMode && this.layer.active && !document._source.locked && document.shape.type === "p") {
291329
let editHandles = this._editHandles;
292330

293331
if (!editHandles || editHandles.destroyed) {
@@ -296,23 +334,30 @@ Drawing.prototype._refreshEditMode = function () {
296334
editHandles.points = editHandles.addChild(new PIXI.Container());
297335
}
298336

299-
const activateListeners = isNewerVersion(game.version, 11)
337+
const activateListeners = foundry.utils.isNewerVersion(game.version, 12)
300338
? (handle) => {
301-
handle.off("pointerover").off("pointerout").off("pointerdown").off("pointerup")
339+
handle.off("pointerover").off("pointerout")
302340
.on("pointerover", this._onHandleHoverIn.bind(this))
303-
.on("pointerout", this._onHandleHoverOut.bind(this))
304-
.on("pointerdown", this._onHandleMouseDown.bind(this))
305-
.on("pointerup", this._onHandleMouseUp.bind(this));
341+
.on("pointerout", this._onHandleHoverOut.bind(this));
306342
handle.eventMode = "static";
307343
}
308-
: (handle) => {
309-
handle.off("mouseover").off("mouseout").off("mousedown").off("mouseup")
310-
.on("mouseover", this._onHandleHoverIn.bind(this))
311-
.on("mouseout", this._onHandleHoverOut.bind(this))
312-
.on("mousedown", this._onHandleMouseDown.bind(this))
313-
.on("mouseup", this._onHandleMouseUp.bind(this));
314-
handle.interactive = true;
315-
};
344+
: foundry.utils.isNewerVersion(game.version, 11)
345+
? (handle) => {
346+
handle.off("pointerover").off("pointerout").off("pointerdown").off("pointerup")
347+
.on("pointerover", this._onHandleHoverIn.bind(this))
348+
.on("pointerout", this._onHandleHoverOut.bind(this))
349+
.on("pointerdown", this._onHandleMouseDown.bind(this))
350+
.on("pointerup", this._onHandleMouseUp.bind(this));
351+
handle.eventMode = "static";
352+
}
353+
: (handle) => {
354+
handle.off("mouseover").off("mouseout").off("mousedown").off("mouseup")
355+
.on("mouseover", this._onHandleHoverIn.bind(this))
356+
.on("mouseout", this._onHandleHoverOut.bind(this))
357+
.on("mousedown", this._onHandleMouseDown.bind(this))
358+
.on("mouseup", this._onHandleMouseUp.bind(this));
359+
handle.interactive = true;
360+
};
316361

317362
const points = document.shape.points;
318363

scripts/hud.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ Hooks.on("renderDrawingHUD", (hud, html) => {
2525

2626
await drawing._convertToPolygon({ confirm: true });
2727

28-
if (drawing.document.shape.type === CONST.DRAWING_TYPES.POLYGON) {
28+
if (drawing.document.shape.type === "p") {
2929
drawing._toggleEditMode();
3030
hud.render(true);
3131
}
3232
});
3333

34-
if (hud.object.document.shape.type === CONST.DRAWING_TYPES.POLYGON) {
34+
if (hud.object.document.shape.type === "p") {
3535
const flipH = document.createElement("div");
3636

3737
flipH.classList.add("control-icon");

scripts/index.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@ import "./shape.js";
1111
import "./text.js";
1212

1313
Hooks.once("libWrapper.Ready", () => {
14-
libWrapper.register(MODULE_ID, "DrawingsLayer.prototype.gridPrecision", function () {
15-
// Force snapping to grid vertices
16-
if (this._forceSnap) return canvas.grid.type <= CONST.GRID_TYPES.SQUARE ? 2 : 5;
14+
if (!foundry.utils.isNewerVersion(game.version, 12)) {
15+
libWrapper.register(MODULE_ID, "DrawingsLayer.prototype.gridPrecision", function () {
16+
// Force snapping to grid vertices
17+
if (this._forceSnap) return canvas.grid.type <= CONST.GRID_TYPES.SQUARE ? 2 : 5;
1718

18-
// Normal snapping precision
19-
let size = canvas.dimensions.size;
20-
if (size >= 128) return 16;
21-
else if (size >= 64) return 8;
22-
else if (size >= 32) return 4;
23-
return 1;
24-
}, libWrapper.OVERRIDE);
25-
libWrapper.ignore_conflicts(MODULE_ID, "precise-drawing-tools", "DrawingsLayer.prototype.gridPrecision");
19+
// Normal snapping precision
20+
let size = canvas.dimensions.size;
21+
if (size >= 128) return 16;
22+
else if (size >= 64) return 8;
23+
else if (size >= 32) return 4;
24+
return 1;
25+
}, libWrapper.OVERRIDE);
26+
libWrapper.ignore_conflicts(MODULE_ID, "precise-drawing-tools", "DrawingsLayer.prototype.gridPrecision");
27+
}
2628

27-
if (!isNewerVersion(game.version, 11)) {
29+
if (!foundry.utils.isNewerVersion(game.version, 11)) {
2830
libWrapper.register(MODULE_ID, "Drawing.prototype._rescaleDimensions", function (original, dx, dy) {
2931
let { points, width, height } = original.shape;
3032
width += dx;
@@ -71,7 +73,7 @@ Hooks.on("preUpdateDrawing", (document, data) => {
7173
});
7274

7375
Hooks.once("init", () => {
74-
if (isNewerVersion(game.version, 11)) {
76+
if (foundry.utils.isNewerVersion(game.version, 11)) {
7577
Hooks.on("updateDrawing", (document, changes) => {
7678
if (!document.rendered) {
7779
return;

scripts/shape.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ Hooks.on("refreshDrawing", drawing => {
99
const document = drawing.document;
1010

1111
if (document.getFlag(MODULE_ID, "invisible") && !(drawing.layer.active && (game.user.isGM || game.user === document.author))) {
12+
drawing.visible = false;
1213
drawing.shape.visible = false;
1314
}
1415

1516
const { lineStyle, fillStyle } = drawing.shape.geometry?.graphicsData?.[0] ?? {};
1617

1718
if (lineStyle && document.strokeWidth) {
1819
const ls = document.getFlag(MODULE_ID, "lineStyle");
19-
const isSmoothPolygon = document.shape.type === CONST.DRAWING_TYPES.POLYGON && document.bezierFactor > 0;
20+
const isSmoothPolygon = document.shape.type === "p" && document.bezierFactor > 0;
2021

2122
Object.assign(lineStyle, {
2223
cap: isSmoothPolygon ? PIXI.LINE_CAP.ROUND : PIXI.LINE_CAP.SQUARE,

0 commit comments

Comments
 (0)