From b3afa47b9b89af1ca6e8441c629c0749bbec4fb1 Mon Sep 17 00:00:00 2001 From: mclemente Date: Mon, 17 Jan 2022 20:07:07 -0300 Subject: [PATCH] Sane Mirror Image Related to https://github.com/mclemente/about-face/issues/24 --- scripts/flipAngles.js | 154 ------------------------------------------ scripts/logic.js | 37 ++++------ 2 files changed, 15 insertions(+), 176 deletions(-) delete mode 100644 scripts/flipAngles.js diff --git a/scripts/flipAngles.js b/scripts/flipAngles.js deleted file mode 100644 index f549993..0000000 --- a/scripts/flipAngles.js +++ /dev/null @@ -1,154 +0,0 @@ -export default [ - {}, //gridless - { - //square - "flip-v": { - down: { - mirror: "mirrorY", - 0: true, - 180: false, - }, - up: { - mirror: "mirrorY", - 0: false, - 180: true, - }, - }, - "flip-h": { - right: { - mirror: "mirrorX", - 90: false, - 270: true, - }, - left: { - mirror: "mirrorX", - 90: true, - 270: false, - }, - }, - }, - { - //hex odd rows - "flip-v": { - down: { - mirror: "mirrorY", - 45: false, - 135: true, - 225: true, - 315: false, - }, - up: { - mirror: "mirrorY", - 45: true, - 135: false, - 225: false, - 315: true, - }, - }, - "flip-h": { - right: { - mirror: "mirrorX", - 90: true, - 270: false, - }, - left: { - mirror: "mirrorX", - 90: false, - 270: true, - }, - }, - }, - { - //hex even rows - "flip-v": { - down: { - mirror: "mirrorY", - 45: false, - 135: true, - 225: true, - 315: false, - }, - up: { - mirror: "mirrorY", - 45: true, - 135: false, - 225: false, - 315: true, - }, - }, - "flip-h": { - right: { - mirror: "mirrorX", - 90: true, - 270: false, - }, - left: { - mirror: "mirrorX", - 90: false, - 270: true, - }, - }, - }, - { - //hex odd cols - "flip-v": { - down: { - mirror: "mirrorY", - 0: false, - 180: true, - }, - up: { - mirror: "mirrorY", - 0: true, - 180: false, - }, - }, - "flip-h": { - right: { - mirror: "mirrorX", - 60: true, - 120: true, - 240: false, - 300: false, - }, - left: { - mirror: "mirrorX", - 60: false, - 120: false, - 240: true, - 300: true, - }, - }, - }, - { - // hex even cols - "flip-v": { - down: { - mirror: "mirrorY", - 0: false, - 180: true, - }, - up: { - mirror: "mirrorY", - 0: true, - 180: false, - }, - }, - "flip-h": { - right: { - mirror: "mirrorX", - 60: true, - 120: true, - 240: false, - 300: false, - }, - left: { - mirror: "mirrorX", - 60: false, - 120: false, - 240: true, - 300: true, - }, - }, - }, -]; diff --git a/scripts/logic.js b/scripts/logic.js index 756c220..9b04a81 100644 --- a/scripts/logic.js +++ b/scripts/logic.js @@ -1,5 +1,4 @@ import { IndicatorMode, MODULE_ID } from "./settings.js"; -import flipAngles from "./flipAngles.js"; let indicatorColor, indicatorDistance; const IndicatorDirections = { @@ -141,7 +140,7 @@ export function onPreUpdateToken(token, updates) { if (!updates.flags) updates.flags = {}; updates.flags[MODULE_ID] = { direction: dir }; if (flipOrRotate != "rotate") { - const [mirrorKey, mirrorVal] = getMirror(token, flipOrRotate, dir + 90); + const [mirrorKey, mirrorVal] = getMirror(token, { x: diffX, y: diffY }); if (mirrorKey) updates[mirrorKey] = mirrorVal; return; } @@ -175,28 +174,22 @@ function getFlipOrRotation(tokenDocument) { return tokenFlipOrRotate != "global" ? tokenFlipOrRotate : game.settings.get(MODULE_ID, "flip-or-rotate"); } -function getMirror(tokenDocument, flipOrRotate, dir) { - if (dir == null) dir = tokenDocument.getFlag(MODULE_ID, "direction") || 0; +function getMirror(tokenDocument, position) { const facingDirection = tokenDocument.getFlag(MODULE_ID, "facingDirection") || game.settings.get(MODULE_ID, "facing-direction"); - try { - var angles = flipAngles[canvas.grid.type][flipOrRotate][facingDirection]; - } catch (error) { - console.error(`About Face: failed to mirror token "${tokenDocument.name}" (ID: ${tokenDocument.id}). - tokenDocument.flags.about-face.facingDirection: ${tokenDocument.getFlag(MODULE_ID, "facingDirection")} - canvas.grid.type: ${canvas.grid.type} - flipOrRotate: ${flipOrRotate} - facingDirection: ${facingDirection} - angles: ${JSON.stringify(angles)} - ${error}`); - } finally { - if (angles && angles[dir] != null) { - const update = { - [angles.mirror]: angles[dir], - }; - return [angles.mirror, angles[dir]]; - } - return []; + if (facingDirection === "right") { + if (position.x < 0) return ["mirrorX", true]; + if (position.x > 0) return ["mirrorX", false]; + } else if (facingDirection === "left") { + if (position.x < 0) return ["mirrorX", false]; + if (position.x > 0) return ["mirrorX", true]; + } else if (facingDirection === "up") { + if (position.y < 0) return ["mirrorY", false]; + if (position.y > 0) return ["mirrorY", true]; + } else if (facingDirection === "down") { + if (position.y < 0) return ["mirrorY", true]; + if (position.y > 0) return ["mirrorY", false]; } + return []; } export function updateArrowColor(color) {