From b527f6e8422316cf198d8a1d519d5241c0f8177a Mon Sep 17 00:00:00 2001 From: kandashi Date: Sun, 17 Jan 2021 02:07:11 +0000 Subject: [PATCH] optional close --- module.json | 2 +- src/combatFocus.js | 106 +++++++++++++++++++++++++-------------------- 2 files changed, 60 insertions(+), 48 deletions(-) diff --git a/module.json b/module.json index 18a3ae1..39531cd 100644 --- a/module.json +++ b/module.json @@ -2,7 +2,7 @@ "name": "Combat-Focus", "title": "Combat Focus", "description": "Auto focus to current combatant", - "version": "0.0.21", + "version": "0.0.23", "authors": [ { "name": "Kandashi", diff --git a/src/combatFocus.js b/src/combatFocus.js index f6c74e2..0ba9a58 100644 --- a/src/combatFocus.js +++ b/src/combatFocus.js @@ -12,6 +12,15 @@ Hooks.on('init', () => { default: "0", config: true, }); + + game.settings.register("Combat-Focus", "close-old", { + name: 'Close old combatant actor sheet', + hint: 'Close all previous combatants sheet on turn change', + scope: 'world', + type: Boolean, + default: false, + config: true, + }); game.settings.register("Combat-Focus", "close-all", { name: 'Close all actor sheets', hint: 'Close all open actor sheet on turn change', @@ -20,61 +29,64 @@ Hooks.on('init', () => { default: false, config: true, }); -}) - -Hooks.on("updateCombat", async (combat, changed, options, userId) => { - + + }) + + Hooks.on("updateCombat", async (combat, changed, options, userId) => { + if (!("turn" in changed)) { - return; + return; } - + const firstGm = game.users.find((u) => u.isGM && u.active); if (firstGm && game.user === firstGm) { - if (game.combats.get(combat.id).data.combatants.length == 0) return; + if (game.combats.get(combat.id).data.combatants.length == 0) return; + + + const nextTurn = combat.turns[changed.turn]; + const previousTurn = combat.turns[changed.turn - 1 > -1 ? changed.turn - 1 : combat.turns.length - 1] + + let nextTokenId = null; + if (getProperty(nextTurn, "tokenId")) { + nextTokenId = nextTurn.tokenId; + } + else { + nextTokenId = getProperty(nextTurn, token._id); + } + + let currentToken = canvas.tokens.get(nextTokenId); + let previousToken = canvas.tokens.get(previousTurn.tokenId) + + if (!currentToken.actor) { + return; + } - - const nextTurn = combat.turns[changed.turn]; - const previousTurn = combat.turns[changed.turn - 1 > -1 ? changed.turn - 1 : combat.turns.length -1] - - let nextTokenId = null; - if (getProperty(nextTurn, "tokenId")) { - nextTokenId = nextTurn.tokenId; - } - else { - nextTokenId = getProperty(nextTurn, token._id); - } - - let currentToken = canvas.tokens.get(nextTokenId); - let previousToken = canvas.tokens.get(previousTurn.tokenId) - - if (!currentToken.actor) { - return; - } - const combatFocus = game.settings.get('Combat-Focus', 'combatFocus') const closeAll = game.settings.get('Combat-Focus', 'close-all') - if(combatFocus !== "0") { - await sleep(5) - await currentToken.control() - canvas.animatePan({x: currentToken.center.x, y: currentToken.center.y, duration: 250}); - let sheet = await currentToken.actor.sheet.render(true) - let rightPos = window.innerWidth - sheet.position.width - 310; - await sleep(5); - if(combatFocus === "1"){ - sheet.setPosition({ left: 107, top: 46}); - if(closeAll) await ui.windows.filter(i => i.actor).close(true) - else await previousToken.actor.sheet.close(true) - } - if(combatFocus === "2"){ - sheet.setPosition({ left: rightPos, top: 46}); - if(closeAll) await ui.windows.filter(i => i.actor).close(true) - else await previousToken.actor.sheet.close(true) - } + const closeOld = game.settings.get('Combat-Focus', 'close-old') + let currentWindows = Object.values(ui.windows) + if (combatFocus !== "0") { + await sleep(5) + await currentToken.control() + canvas.animatePan({ x: currentToken.center.x, y: currentToken.center.y, duration: 250 }); + let sheet = await currentToken.actor.sheet.render(true) + let rightPos = window.innerWidth - sheet.position.width - 310; + await sleep(5); + if (combatFocus === "1") { + sheet.setPosition({ left: 107, top: 46 }); + if (closeAll) for (let window of currentWindows) if (window.actor && window.actor.id !== currentToken.actor.id) window.close(true) + if(closeOld) await previousToken.actor.sheet.close(true) + } + if (combatFocus === "2") { + sheet.setPosition({ left: rightPos, top: 46 }); + if (closeAll) for (let window of currentWindows) if (window.actor && window.actor.id !== currentToken.actor.id) window.close(true) + if(closeOld) await previousToken.actor.sheet.close(true) + } - async function sleep(millis) { - return new Promise(r => setTimeout(r, millis)); - } + async function sleep(millis) { + return new Promise(r => setTimeout(r, millis)); + } } - } + } }); \ No newline at end of file