Skip to content

Commit

Permalink
Update for V12
Browse files Browse the repository at this point in the history
  • Loading branch information
dev7355608 committed May 9, 2024
1 parent 0d918db commit 5ec762c
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 92 deletions.
8 changes: 4 additions & 4 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
"email": "[email protected]"
}
],
"version": "2.1.0",
"version": "2.1.1",
"compatibility": {
"minimum": "10",
"verified": "11"
"verified": "12"
},
"minimumCoreVersion": "10",
"esmodules": [
Expand All @@ -28,8 +28,8 @@
},
"url": "https://github.com/dev7355608/advanced-drawing-tools",
"manifest": "https://raw.githubusercontent.com/dev7355608/advanced-drawing-tools/main/module.json",
"download": "https://github.com/dev7355608/advanced-drawing-tools/releases/download/v2.1.0/module.zip",
"changelog": "https://github.com/dev7355608/advanced-drawing-tools/releases/tag/v2.1.0",
"download": "https://github.com/dev7355608/advanced-drawing-tools/releases/download/v2.1.1/module.zip",
"changelog": "https://github.com/dev7355608/advanced-drawing-tools/releases/tag/v2.1.1",
"bugs": "https://github.com/dev7355608/advanced-drawing-tools/issues",
"readme": "https://raw.githubusercontent.com/dev7355608/advanced-drawing-tools/main/README.md",
"license": "https://raw.githubusercontent.com/dev7355608/advanced-drawing-tools/main/LICENSE"
Expand Down
14 changes: 8 additions & 6 deletions scripts/convert.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MODULE_NAME } from "./const.js";

Drawing.prototype._convertToPolygon = async function ({ confirm = false } = {}) {
if (this.document.shape.type === CONST.DRAWING_TYPES.POLYGON) {
if (this.document.shape.type === "p") {
return;
}

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

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

if (type === CONST.DRAWING_TYPES.RECTANGLE) {
if (type === "r") {
update.shape.points = [0, 0, width, 0, width, height, 0, height];
} else if (type === CONST.DRAWING_TYPES.ELLIPSE) {
} else if (type === "e") {
const rx = width / 2;
const ry = height / 2;

Expand Down Expand Up @@ -84,15 +84,17 @@ Drawing.prototype._convertToPolygon = async function ({ confirm = false } = {})
return;
}

this.document.shape.type = CONST.DRAWING_TYPES.POLYGON;
this.document.shape.type = "p";
update = this._rescaleDimensions(update, 0, 0);
update.shape.type = CONST.DRAWING_TYPES.POLYGON;
update.shape.type = "p";

if (this.document.fillType === CONST.DRAWING_FILL_TYPES.NONE) {
update.fillType = CONST.DRAWING_FILL_TYPES.SOLID;
update.fillAlpha = 0;
}

update.bezierFactor = 0;

await this.document.update(update);
});
};
155 changes: 100 additions & 55 deletions scripts/edit-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,36 @@ Hooks.on("refreshDrawing", drawing => {
});

Hooks.once("libWrapper.Ready", () => {
const getInteractionData = isNewerVersion(game.version, 11)
const getInteractionData = foundry.utils.isNewerVersion(game.version, 11)
? (event) => event.interactionData
: (event) => event.data;
const getOriginalData = isNewerVersion(game.version, 11)
const getOriginalData = foundry.utils.isNewerVersion(game.version, 11)
? (drawing, event) => event.interactionData.originalData
: (drawing, event) => drawing._original;
const setOriginalData = isNewerVersion(game.version, 11)
const setOriginalData = foundry.utils.isNewerVersion(game.version, 11)
? (drawing, event) => event.interactionData.originalData = drawing.document.toObject()
: (drawing, event) => drawing._original = drawing.document.toObject();
const setRestoreOriginalData = isNewerVersion(game.version, 11)
const setRestoreOriginalData = foundry.utils.isNewerVersion(game.version, 11)
? (drawing, event, value) => event.interactionData.restoreOriginalData = value
: (drawing, event, value) => { };
const refreshShape = isNewerVersion(game.version, 11)
? (drawing) => drawing.renderFlags.set({ refreshShape: true })
: (drawing) => drawing.refresh();

libWrapper.register(MODULE_ID, "Drawing.prototype.activateListeners", function (wrapped, ...args) {
wrapped(...args);

const pointerup = isNewerVersion(game.version, 11) ? "pointerup" : "mouseup";

this.frame.handle.off(pointerup).on(pointerup, this._onHandleMouseUp.bind(this));
}, libWrapper.WRAPPER);
const refreshSize = foundry.utils.isNewerVersion(game.version, 12)
? (drawing) => drawing.renderFlags.set({ refreshSize: true })
: foundry.utils.isNewerVersion(game.version, 11)
? (drawing) => drawing.renderFlags.set({ refreshShape: true })
: (drawing) => drawing.refresh();
const isDraggingHandle = foundry.utils.isNewerVersion(game.version, 12)
? (drawing, event) => event.interactionData.dragHandle
: (drawing, event) => drawing._dragHandle;

if (!foundry.utils.isNewerVersion(game.version, 12)) {
libWrapper.register(MODULE_ID, "Drawing.prototype.activateListeners", function (wrapped, ...args) {
wrapped(...args);

const pointerup = foundry.utils.isNewerVersion(game.version, 11) ? "pointerup" : "mouseup";

this.frame.handle.off(pointerup).on(pointerup, this._onHandleMouseUp.bind(this));
}, libWrapper.WRAPPER);
}

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

libWrapper.register(MODULE_ID, "Drawing.prototype._onHandleMouseDown", function (event) {
const handle = event.target;

if (handle instanceof PointHandle || handle instanceof EdgeHandle) {
if (!this.document.locked) {
this._dragHandle = true;
handle._hover = true;
handle.refresh();
this._editHandle = handle;
if (!foundry.utils.isNewerVersion(game.version, 12)) {
libWrapper.register(MODULE_ID, "Drawing.prototype._onHandleMouseDown", function (event) {
const handle = event.target;

if (handle instanceof PointHandle || handle instanceof EdgeHandle) {
if (!this.document.locked) {
this._dragHandle = true;
handle._hover = true;
handle.refresh();
this._editHandle = handle;
} else {
this._editHandle = null;
}
} else {
this._editHandle = null;
if (!this.document.locked) {
this._dragHandle = true;
}
}
} else {
if (!this.document.locked) {
this._dragHandle = true;
}
}
}, libWrapper.OVERRIDE);
}, libWrapper.OVERRIDE);
}

libWrapper.register(MODULE_ID, "Drawing.prototype._onDragLeftStart", function (wrapped, event) {
if (!this._dragHandle) {
if (!isDraggingHandle(this, event)) {
return wrapped(event);
}

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

if (update) {
this.document.updateSource(update);
refreshShape(this);
refreshSize(this);
}
}, libWrapper.MIXED);

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

if (!originalEvent.shiftKey) {
if (foundry.utils.isNewerVersion(game.version, 12)) {
destination = this.layer.getSnappedPoint(destination);
}
}

// Pan the canvas if the drag event approaches the edge
canvas._onDragCanvasPan(originalEvent);

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

try {
this.document.updateSource(update);
refreshShape(this);
refreshSize(this);
} catch (err) { }
}, libWrapper.OVERRIDE);

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

if (!originalEvent.shiftKey) {
destination = canvas.grid.getSnappedPosition(destination.x, destination.y, this.layer.gridPrecision);
if (foundry.utils.isNewerVersion(game.version, 12)) {
destination = this.layer.getSnappedPoint(destination);
} else {
destination = canvas.grid.getSnappedPosition(destination.x, destination.y, this.layer.gridPrecision);
}
}

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

if (foundry.utils.isNewerVersion(game.version, 12)) {
libWrapper.register(MODULE_ID, "Drawing.prototype._onClickLeft", function (wrapped, event) {
this._editHandle = null;

if (this._editHandles?.points.children.includes(event.target)
|| this._editHandles?.edges.children.includes(event.target)) {
event.interactionData.dragHandle = true;
event.stopPropagation();
this._editHandle = event.target;
return;
}

return wrapped(event);
}, libWrapper.MIXED);
}

libWrapper.register(MODULE_ID, "Drawing.prototype._onClickRight", function (wrapped, event) {
let handle = getInteractionData(event).handle;

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

if ((handle instanceof PointHandle || handle instanceof EdgeHandle) && handle._hover) {
event.stopPropagation();
this._dragHandle = false;
handle._hover = false;
handle.refresh();
Expand Down Expand Up @@ -252,12 +288,14 @@ Hooks.once("libWrapper.Ready", () => {
return wrapped(event);
}, libWrapper.MIXED);

Drawing.prototype._onHandleMouseUp = function (event) {
if (!getOriginalData(this, event)) {
this._dragHandle = false;
this._editHandle = null;
}
};
if (!foundry.utils.isNewerVersion(game.version, 12)) {
Drawing.prototype._onHandleMouseUp = function (event) {
if (!getOriginalData(this, event)) {
this._dragHandle = false;
this._editHandle = null;
}
};
}
});

Drawing.prototype._editMode = false;
Expand Down Expand Up @@ -287,7 +325,7 @@ Drawing.prototype._editHandles = null;
Drawing.prototype._refreshEditMode = function () {
const document = this.document;

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

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

const activateListeners = isNewerVersion(game.version, 11)
const activateListeners = foundry.utils.isNewerVersion(game.version, 12)
? (handle) => {
handle.off("pointerover").off("pointerout").off("pointerdown").off("pointerup")
handle.off("pointerover").off("pointerout")
.on("pointerover", this._onHandleHoverIn.bind(this))
.on("pointerout", this._onHandleHoverOut.bind(this))
.on("pointerdown", this._onHandleMouseDown.bind(this))
.on("pointerup", this._onHandleMouseUp.bind(this));
.on("pointerout", this._onHandleHoverOut.bind(this));
handle.eventMode = "static";
}
: (handle) => {
handle.off("mouseover").off("mouseout").off("mousedown").off("mouseup")
.on("mouseover", this._onHandleHoverIn.bind(this))
.on("mouseout", this._onHandleHoverOut.bind(this))
.on("mousedown", this._onHandleMouseDown.bind(this))
.on("mouseup", this._onHandleMouseUp.bind(this));
handle.interactive = true;
};
: foundry.utils.isNewerVersion(game.version, 11)
? (handle) => {
handle.off("pointerover").off("pointerout").off("pointerdown").off("pointerup")
.on("pointerover", this._onHandleHoverIn.bind(this))
.on("pointerout", this._onHandleHoverOut.bind(this))
.on("pointerdown", this._onHandleMouseDown.bind(this))
.on("pointerup", this._onHandleMouseUp.bind(this));
handle.eventMode = "static";
}
: (handle) => {
handle.off("mouseover").off("mouseout").off("mousedown").off("mouseup")
.on("mouseover", this._onHandleHoverIn.bind(this))
.on("mouseout", this._onHandleHoverOut.bind(this))
.on("mousedown", this._onHandleMouseDown.bind(this))
.on("mouseup", this._onHandleMouseUp.bind(this));
handle.interactive = true;
};

const points = document.shape.points;

Expand Down
4 changes: 2 additions & 2 deletions scripts/hud.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ Hooks.on("renderDrawingHUD", (hud, html) => {

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

if (drawing.document.shape.type === CONST.DRAWING_TYPES.POLYGON) {
if (drawing.document.shape.type === "p") {
drawing._toggleEditMode();
hud.render(true);
}
});

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

flipH.classList.add("control-icon");
Expand Down
28 changes: 15 additions & 13 deletions scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ import "./shape.js";
import "./text.js";

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

// Normal snapping precision
let size = canvas.dimensions.size;
if (size >= 128) return 16;
else if (size >= 64) return 8;
else if (size >= 32) return 4;
return 1;
}, libWrapper.OVERRIDE);
libWrapper.ignore_conflicts(MODULE_ID, "precise-drawing-tools", "DrawingsLayer.prototype.gridPrecision");
// Normal snapping precision
let size = canvas.dimensions.size;
if (size >= 128) return 16;
else if (size >= 64) return 8;
else if (size >= 32) return 4;
return 1;
}, libWrapper.OVERRIDE);
libWrapper.ignore_conflicts(MODULE_ID, "precise-drawing-tools", "DrawingsLayer.prototype.gridPrecision");
}

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

Hooks.once("init", () => {
if (isNewerVersion(game.version, 11)) {
if (foundry.utils.isNewerVersion(game.version, 11)) {
Hooks.on("updateDrawing", (document, changes) => {
if (!document.rendered) {
return;
Expand Down
3 changes: 2 additions & 1 deletion scripts/shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ Hooks.on("refreshDrawing", drawing => {
const document = drawing.document;

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

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

if (lineStyle && document.strokeWidth) {
const ls = document.getFlag(MODULE_ID, "lineStyle");
const isSmoothPolygon = document.shape.type === CONST.DRAWING_TYPES.POLYGON && document.bezierFactor > 0;
const isSmoothPolygon = document.shape.type === "p" && document.bezierFactor > 0;

Object.assign(lineStyle, {
cap: isSmoothPolygon ? PIXI.LINE_CAP.ROUND : PIXI.LINE_CAP.SQUARE,
Expand Down
Loading

0 comments on commit 5ec762c

Please sign in to comment.