foundryvtt-reve-de-dragon/module/dialog-select-target.js

60 lines
1.6 KiB
JavaScript
Raw Normal View History

export class DialogSelectTarget extends Dialog {
static hasTargets() {
return (game.user.targets?.size ?? 0) > 0;
}
static async selectOneToken(onSelectTarget = target => { }) {
if (DialogSelectTarget.hasTargets()) {
const targets = game.user.targets.map(it => it);
switch (targets.size) {
case 0: return;
case 1:
onSelectTarget(targets[0]);
return;
default:
{
const tokens = targets.map(it => { return { id: it.id, name: it.document.name, img: it.document.texture.src ?? it.actor.img ?? 'icons/svg/mystery-man.svg' } })
const html = await renderTemplate("systems/foundryvtt-reve-de-dragon/templates/dialog-select-target.html", {
tokens: tokens
});
new DialogSelectTarget(html, onSelectTarget, targets).render(true);
}
}
}
}
constructor(html, onSelectTarget, targets) {
const options = {
classes: ["rdd-dialog-select-target"],
width: 'fit-content',
height: 'fit-content',
'max-height': 600,
'z-index': 99999
};
const conf = {
title: "Choisir une cible",
content: html,
buttons: {}
};
super(conf, options);
this.onSelectTarget = onSelectTarget;
this.targets = targets;
}
activateListeners(html) {
super.activateListeners(html);
html.find("li.select-target").click((event) => {
this.targetSelected($(event.currentTarget)?.data("token-id"));
});
}
targetSelected(tokenId) {
const target = this.targets.find(it => it.id == tokenId);
this.close();
if (target) {
this.onSelectTarget(target);
}
}
}