diff --git a/addons/sprite-clone-count/addon.json b/addons/sprite-clone-count/addon.json index 139602fb7c..edc95d2758 100644 --- a/addons/sprite-clone-count/addon.json +++ b/addons/sprite-clone-count/addon.json @@ -13,8 +13,8 @@ "matches": ["projects"] } ], - "versionAdded": "1.35.0", - "tags": ["projects", "featured"], + "versionAdded": "1.39.0", + "tags": ["editor", "codeEditor", "featured"], "enabledByDefault": false, "dynamicEnable": true, "dynamicDisable": true diff --git a/addons/sprite-clone-count/userscript.js b/addons/sprite-clone-count/userscript.js index efa1b9f3e6..32bfc91e3b 100644 --- a/addons/sprite-clone-count/userscript.js +++ b/addons/sprite-clone-count/userscript.js @@ -1,41 +1,47 @@ export default async function ({ addon, console }) { const vm = addon.tab.traps.vm; - const OGfunction = vm.runtime.changeCloneCounter; + const ogCloneCounter = vm.runtime.changeCloneCounter; - vm.runtime.changeCloneCounter = (e) => { - OGfunction.call(vm.runtime, e); - - setTimeout(() => { - let counts = {}; + function updateCounts() { + let counts = {}; - vm.runtime.targets - .filter((target) => !target.isOriginal) - .map((target) => target.sprite.name) - .forEach((target) => { - if (!counts[target]) counts[target] = 1; - else counts[target] += 1; - }); - - console.log(counts); + vm.runtime.targets + .filter((target) => !target.isOriginal) + .map((target) => target.sprite.name) + .forEach((target) => { + if (!counts[target]) counts[target] = 1; + else counts[target] += 1; + }); - const spriteNames = Array.from( - document - .querySelector("[class*=sprite-selector_items-wrapper]") - .querySelectorAll("[class*=sprite-selector-item_sprite-name]") - ); + const spriteNames = Array.from( + document + .querySelector("[class*=sprite-selector_items-wrapper]") + .querySelectorAll("[class*=sprite-selector-item_sprite-name]") + ); - console.log(spriteNames); + spriteNames.forEach((spriteName) => { + spriteName.querySelector(".clone-count")?.remove(); + if (counts[spriteName.innerText.split("\n")[0]] !== undefined) { + const count = document.createElement("div"); - spriteNames.forEach((spriteName) => { - if (counts[spriteName.innerText.split("\n")[0]]) { - spriteName.querySelector("div")?.remove(); + count.classList.add("clone-count"); + count.innerText = `(${counts[spriteName.innerText.split("\n")[0]]} clones)`; + spriteName.appendChild(count); + } + }); + } - const count = document.createElement("div"); + vm.runtime.changeCloneCounter = (e) => { + ogCloneCounter.call(vm.runtime, e); - count.innerText = `(${counts[spriteName.innerText.split("\n")[0]]} clones)`; - spriteName.appendChild(count); - } - }); + setTimeout(() => { + updateCounts(); }, 100); }; + + vm.addListener("targetsUpdate", () => { + setTimeout(() => { + updateCounts(); + }, 0); + }); }