From 5ec762c3ecdf36a846966e78f4a2e0b0449c294f Mon Sep 17 00:00:00 2001 From: dev7355608 Date: Thu, 9 May 2024 19:56:16 +0200 Subject: [PATCH] Update for V12 --- module.json | 8 +-- scripts/convert.js | 14 ++-- scripts/edit-mode.js | 155 ++++++++++++++++++++++++++++--------------- scripts/hud.js | 4 +- scripts/index.js | 28 ++++---- scripts/shape.js | 3 +- scripts/text.js | 35 +++++++--- 7 files changed, 155 insertions(+), 92 deletions(-) diff --git a/module.json b/module.json index c93f243..7bba7ec 100644 --- a/module.json +++ b/module.json @@ -9,10 +9,10 @@ "email": "dev7355608@gmail.com" } ], - "version": "2.1.0", + "version": "2.1.1", "compatibility": { "minimum": "10", - "verified": "11" + "verified": "12" }, "minimumCoreVersion": "10", "esmodules": [ @@ -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" diff --git a/scripts/convert.js b/scripts/convert.js index ac91682..f5890bd 100644 --- a/scripts/convert.js +++ b/scripts/convert.js @@ -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; } @@ -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; @@ -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); }); }; diff --git a/scripts/edit-mode.js b/scripts/edit-mode.js index aeb23cc..da1330b 100644 --- a/scripts/edit-mode.js +++ b/scripts/edit-mode.js @@ -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) { @@ -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); } @@ -112,7 +121,7 @@ Hooks.once("libWrapper.Ready", () => { if (update) { this.document.updateSource(update); - refreshShape(this); + refreshSize(this); } }, libWrapper.MIXED); @@ -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); @@ -157,7 +172,7 @@ Hooks.once("libWrapper.Ready", () => { try { this.document.updateSource(update); - refreshShape(this); + refreshSize(this); } catch (err) { } }, libWrapper.OVERRIDE); @@ -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) { @@ -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; @@ -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(); @@ -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; @@ -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) { @@ -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; diff --git a/scripts/hud.js b/scripts/hud.js index c202557..ad1ec6e 100644 --- a/scripts/hud.js +++ b/scripts/hud.js @@ -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"); diff --git a/scripts/index.js b/scripts/index.js index ddf525c..ebf91e2 100644 --- a/scripts/index.js +++ b/scripts/index.js @@ -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; @@ -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; diff --git a/scripts/shape.js b/scripts/shape.js index 1031611..9702dca 100644 --- a/scripts/shape.js +++ b/scripts/shape.js @@ -9,6 +9,7 @@ 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; } @@ -16,7 +17,7 @@ Hooks.on("refreshDrawing", drawing => { 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, diff --git a/scripts/text.js b/scripts/text.js index f9b7234..94b8f64 100644 --- a/scripts/text.js +++ b/scripts/text.js @@ -3,7 +3,7 @@ import { WarpedText } from "./warped-text.js"; import { calculateValue } from "./utils.js"; Hooks.once("libWrapper.Ready", () => { - if (isNewerVersion(game.version, 11)) { + if (foundry.utils.isNewerVersion(game.version, 11)) { return; } @@ -113,17 +113,30 @@ Hooks.on("refreshDrawing", drawing => { wordWrapWidth: calculateValue(ts?.wordWrapWidth, document.shape.width) ?? document.shape.width }); - const offset = Math.min(Math.max(document.shape.width - text.width, 0) / 2, document.strokeWidth + document.fontSize / 2); - - if (ts?.align === "left" || ts?.align === "justify") { - text.pivot.set(Math.max(text.width, document.shape.width) / 2 - offset, text.height / 2); - } else if (ts?.align === "right") { - text.pivot.set(text.width - Math.max(text.width, document.shape.width) / 2 + offset, text.height / 2); + if (foundry.utils.isNewerVersion(game.version, 12)) { + if (ts?.align === "left" || ts?.align === "justify") { + text.position.set(0, document.shape.height / 2); + text.anchor.set(0, 0.5); + } else if (ts?.align === "right") { + text.anchor.set(1, 0.5); + text.position.set(document.shape.width, document.shape.height / 2); + } else { + text.anchor.set(0.5, 0.5); + text.position.set(document.shape.width / 2, document.shape.height / 2); + } } else { - text.pivot.set(text.width / 2, text.height / 2); - } + const offset = Math.min(Math.max(document.shape.width - text.width, 0) / 2, document.strokeWidth + document.fontSize / 2); + + if (ts?.align === "left" || ts?.align === "justify") { + text.pivot.set(Math.max(text.width, document.shape.width) / 2 - offset, text.height / 2); + } else if (ts?.align === "right") { + text.pivot.set(text.width - Math.max(text.width, document.shape.width) / 2 + offset, text.height / 2); + } else { + text.pivot.set(text.width / 2, text.height / 2); + } - text.position.set(document.shape.width / 2, document.shape.height / 2); + text.position.set(document.shape.width / 2, document.shape.height / 2); + } const arc = Math.clamped(ts?.arc ? ts.arc / 180 * Math.PI : 0, -2 * Math.PI, +2 * Math.PI); @@ -133,7 +146,7 @@ Hooks.on("refreshDrawing", drawing => { } if (!drawing._warpedText) { - drawing._warpedText = drawing.addChildAt(new WarpedText(drawing.text), drawing.getChildIndex(drawing.text) + 1); + drawing._warpedText = drawing.addChildAt(new WarpedText(drawing.text), drawing.text.parent.getChildIndex(drawing.text) + 1); } drawing._warpedText.alpha = document.hidden ? Math.min(0.5, document.textAlpha ?? 1) : (document.textAlpha ?? 1);