foundryvtt-reve-de-dragon/module/rdd-token-hud.js

70 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-12-05 00:04:40 +01:00
/* -------------------------------------------- */
import { HtmlUtility } from "./html-utility.js";
2020-12-05 00:04:40 +01:00
import { RdDUtility } from "./rdd-utility.js";
/* -------------------------------------------- */
export class RdDTokenHud {
2020-12-05 00:04:40 +01:00
/* -------------------------------------------- */
2020-12-20 21:54:09 +01:00
static async addTokenHudExtensions(app, html, tokenId) {
let token = canvas.tokens.get(tokenId);
let actor = token.actor;
let combatant = game.combat.data.combatants.find(c => c.tokenId == token.data._id);
// Affichage seulement si le token est un combat et a un actor
if (actor === undefined) return;
if (!token.inCombat) return;
if (!combatant) return;
2020-12-21 23:41:17 +01:00
let armesList = RdDTokenHud.buildListeActionsCombat(combatant) ;
const hudData = { combatant: combatant, armes: armesList };
// initiative
2020-12-20 21:54:09 +01:00
await RdDTokenHud._configureSubMenu(html.find('.control-icon.combat'), 'systems/foundryvtt-reve-de-dragon/templates/hud-actor-init.html', hudData,
(event) => {
2020-12-05 00:37:10 +01:00
let armeIndex = event.currentTarget.attributes['data-arme-id'].value;
let combatantId = event.currentTarget.attributes['data-combatant-id'].value;
2020-12-21 23:10:18 +01:00
let arme = armesList[armeIndex];
RdDUtility.rollInitiativeCompetence(combatantId, arme);
2020-12-05 00:04:40 +01:00
});
// combat
2020-12-20 21:54:09 +01:00
await RdDTokenHud._configureSubMenu(html.find('.control-icon.target'), 'systems/foundryvtt-reve-de-dragon/templates/hud-actor-attaque.html', hudData,
(event) => {
let armeIndex = event.currentTarget.attributes['data-arme-id'].value;
let arme = armesList[armeIndex];
actor.rollArme(arme.data.competence, arme.name);
});
}
/* -------------------------------------------- */
static buildListeActionsCombat(combatant) {
let armesList = RdDUtility.buildArmeList(combatant);
for (let armeIndex = 0; armeIndex < armesList.length; armeIndex++) {
armesList[armeIndex] = duplicate(armesList[armeIndex]);
armesList[armeIndex].index = armeIndex;
2020-12-05 00:04:40 +01:00
}
return armesList;
2020-12-05 00:04:40 +01:00
}
/* -------------------------------------------- */
2020-12-20 21:54:09 +01:00
static async _configureSubMenu(insertionPoint, template, hudData, onMenuItem) {
const hud = $(await renderTemplate(template, hudData));
const imgHud = hud.find('img.rdd-hud-togglebutton');
const list = hud.find('div.rdd-hud-list');
2020-12-21 23:41:17 +01:00
hud.toggleClass('active');
HtmlUtility._showControlWhen(list, hud.hasClass('active'));
imgHud.click(event => {
hud.toggleClass('active');
HtmlUtility._showControlWhen(list, hud.hasClass('active'));
});
list.find('.rdd-hud-menu').click(onMenuItem);
insertionPoint.after(hud);
}
2020-12-05 00:04:40 +01:00
}