Skip to content

Commit

Permalink
Mirroring Fix
Browse files Browse the repository at this point in the history
Fix #11
  • Loading branch information
mclemente committed Oct 2, 2021
1 parent 87a4fb9 commit fb2e159
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions scripts/logic.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { IndicatorMode, MODULE_ID } from "./settings.js";

function getDirection(token) {
const facingDirection = token.getFlag(MODULE_ID, "facingDirection") || game.settings.get(MODULE_ID, "facing-direction");
const directions = {
right: 0,
left: 180,
down: 270,
up: 90,
};
return directions[facingDirection];
}

function getFlipOrRotation(token) {
const tokenFlipOrRotate = token.getFlag(MODULE_ID, "flipOrRotate") || "global";
return tokenFlipOrRotate != "global" ? tokenFlipOrRotate : game.settings.get(MODULE_ID, "flip-or-rotate");
}

export function drawAboutFaceIndicator(wrapped, ...args) {
if (!canvas.scene.getFlag(MODULE_ID, "sceneEnabled")) {
wrapped(...args);
Expand All @@ -9,20 +25,9 @@ export function drawAboutFaceIndicator(wrapped, ...args) {
let dir = this.data.flags["about-face"]?.direction;
if (dir === undefined || dir === null) {
dir = 90;
const flipOrRotate = game.settings.get(MODULE_ID, "flip-or-rotate");
const flipOrRotate = getFlipOrRotation(this);
if (flipOrRotate !== "rotate") {
const facingDirection = game.settings.get(MODULE_ID, "facing-direction");
switch (facingDirection) {
case "right":
dir = 0;
break;
case "left":
dir = 180;
break;
case "down":
dir = 270;
break;
}
dir = getDirection(this);
}
}
const indicatorSize = [1, 1.5][game.settings.get(MODULE_ID, "sprite-type")];
Expand Down Expand Up @@ -99,13 +104,20 @@ export function onPreCreateToken(document, data, options, userId) {

export function onPreUpdateToken(token, updates) {
if (!canvas.scene.getFlag(MODULE_ID, "sceneEnabled")) return;
const tokenFlipOrRotate = token.getFlag(MODULE_ID, "flipOrRotate") || "global";
const flipOrRotate = tokenFlipOrRotate != "global" ? tokenFlipOrRotate : game.settings.get(MODULE_ID, "flip-or-rotate");
if (flipOrRotate == "rotate" && "rotation" in updates) {
const flipOrRotate = getFlipOrRotation(token);
if ("rotation" in updates) {
var dir = updates.rotation + 90;
//store the direction in the token data
if (!updates.flags) updates.flags = {};
updates.flags[MODULE_ID] = { direction: dir };
if (flipOrRotate != "rotate") {
if (flipOrRotate == "flip-h" && [180, 360].includes(dir)) {
updates.mirrorX = !token.data.mirrorX;
} else if (flipOrRotate == "flip-v" && [90, 270].includes(dir)) {
updates.mirrorY = !token.data.mirrorY;
}
return;
}
} else if ("x" in updates || "y" in updates) {
//get previews and new positions
const prevPos = { x: token.data.x, y: token.data.y };
Expand All @@ -117,11 +129,13 @@ export function onPreUpdateToken(token, updates) {
//store the direction in the token data
if (!updates.flags) updates.flags = {};
updates.flags[MODULE_ID] = { direction: dir };
if (flipOrRotate) {
if (flipOrRotate == "flip-h") diffY = 0;
else if (flipOrRotate == "flip-v") diffX = 0;
dir = (Math.atan2(diffY, diffX) * 180) / Math.PI;
if (!(diffY || diffX)) return;
if (flipOrRotate != "rotate") {
if (flipOrRotate == "flip-h" && diffX != 0) {
updates.mirrorX = !token.data.mirrorX;
} else if (flipOrRotate == "flip-v" && diffY != 0) {
updates.mirrorY = !token.data.mirrorY;
}
return;
}
} else return;
//update the rotation of the token
Expand Down

0 comments on commit fb2e159

Please sign in to comment.