|
| 1 | +/* Wild-Shape Token Resizer |
| 2 | + * |
| 3 | + * A script to automatically resize a Rollable Table Token when a different |
| 4 | + * side is chosen. It does this by repurposing the “weight” attribute of the |
| 5 | + * Items in the Rollable Table. It was written with D&D Druid Wild Shape |
| 6 | + * tokens in mind, but would work for any square rollable table tokens from |
| 7 | + * which players will choose different sides. |
| 8 | + * |
| 9 | + * The script listens to token:change events, looks for a table with the |
| 10 | + * same name as the token, and updates the token size when the side changes, |
| 11 | + * so no other configuration should be required. |
| 12 | + * |
| 13 | + * By: Erik Ogan |
| 14 | + * Version: 0.0.2 |
| 15 | + */ |
| 16 | + |
| 17 | +var WildShapeResizer = |
| 18 | + WildShapeResizer || |
| 19 | + (() => { |
| 20 | + "use strict"; |
| 21 | + |
| 22 | + const version = "0.0.2"; |
| 23 | + const defaultGridSize = 70; |
| 24 | + |
| 25 | + const checkTokenSize = (token) => { |
| 26 | + const name = token.get("name"); |
| 27 | + if (!name) return; |
| 28 | + |
| 29 | + const tableItems = itemsForToken(token); |
| 30 | + if (!tableItems || tableItems.length < 1) return; |
| 31 | + |
| 32 | + const page = getObj("page", token.get("_pageid")); |
| 33 | + let gridSize = defaultGridSize; |
| 34 | + |
| 35 | + if (page) { |
| 36 | + gridSize = page.get("snapping_increment") * gridSize; |
| 37 | + } |
| 38 | + |
| 39 | + if (gridSize <= 0) { |
| 40 | + gridSize = defaultGridSize; |
| 41 | + } |
| 42 | + |
| 43 | + const side = tableItems[token.get("currentSide")]; |
| 44 | + if (!imageCompare(side.get("avatar"), token.get("imgsrc"))) { |
| 45 | + // Rollable Table sides are copied into the token when it is created. If you change the table |
| 46 | + log("WildShapeResizer ERROR: token image does not match table image"); |
| 47 | + sendChat( |
| 48 | + "WildShapeResizer", |
| 49 | + "/direct <strong>ERROR:</strong> token image does not match table image." + |
| 50 | + " This token likely needs to be recreated." |
| 51 | + ); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + const weight = side.get("weight"); |
| 56 | + const dimension = gridSize * weight; |
| 57 | + doResize(token, dimension); |
| 58 | + }; |
| 59 | + |
| 60 | + const doResize = (token, dimension) => { |
| 61 | + const name = token.get("name"); |
| 62 | + |
| 63 | + const currentW = token.get("width"); |
| 64 | + const currentH = token.get("height"); |
| 65 | + |
| 66 | + // TODO: get the locations of the other tokens on the board and try to keep from overlapping them |
| 67 | + const currentL = token.get("left"); |
| 68 | + const currentT = token.get("top"); |
| 69 | + |
| 70 | + if ( |
| 71 | + currentW && |
| 72 | + currentH && |
| 73 | + (currentW !== dimension || currentH !== dimension) |
| 74 | + ) { |
| 75 | + log(`WildShapeResizer: resizing ${name} to ${dimension}`); |
| 76 | + token.set("width", dimension); |
| 77 | + token.set("height", dimension); |
| 78 | + // TODO: Figure out why this is not working |
| 79 | + // Reset top & left so it does not center on the old size |
| 80 | + token.set("top", currentT); |
| 81 | + token.set("left", currentL); |
| 82 | + // Maybe with a timeout after we’ve finished our changes? |
| 83 | + setTimeout(() => { |
| 84 | + token.set("top", currentT); |
| 85 | + token.set("left", currentL); |
| 86 | + }, 10); |
| 87 | + } else { |
| 88 | + log(`WildShapeResizer: ${name} is already correctly sized.`); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + const itemsForToken = (token) => { |
| 93 | + const name = token.get("name"); |
| 94 | + if (!name) return undefined; |
| 95 | + |
| 96 | + const table = findObjs({ _type: "rollabletable", name: name })[0]; |
| 97 | + if (!table) return undefined; |
| 98 | + |
| 99 | + return findObjs({ |
| 100 | + _type: "tableitem", |
| 101 | + _rollabletableid: table.id, |
| 102 | + }); |
| 103 | + }; |
| 104 | + |
| 105 | + const imageCompare = (img1, img2) => { |
| 106 | + return imageNormalize(img1) === imageNormalize(img2); |
| 107 | + } |
| 108 | + |
| 109 | + const imageNormalize = (img) => { |
| 110 | + img = img.replace(/\?.*$/, ''); |
| 111 | + img = img.replace(/(.*)\/[^.]+/, '$1/max'); |
| 112 | + return img; |
| 113 | + } |
| 114 | + |
| 115 | + const registerHandlers = () => { |
| 116 | + on("change:token", checkTokenSize); |
| 117 | + }; |
| 118 | + |
| 119 | + const notifyStart = () => { |
| 120 | + log(`.oO WildShapeResizer ${version} Oo.`); |
| 121 | + }; |
| 122 | + |
| 123 | + return { |
| 124 | + notifyStart: notifyStart, |
| 125 | + registerHandlers: registerHandlers, |
| 126 | + }; |
| 127 | + })(); |
| 128 | + |
| 129 | +on("ready", () => { |
| 130 | + "use strict"; |
| 131 | + WildShapeResizer.notifyStart(); |
| 132 | + WildShapeResizer.registerHandlers(); |
| 133 | +}); |
0 commit comments