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

116 lines
4.4 KiB
JavaScript
Raw Normal View History

2020-12-05 00:04:40 +01:00
/* -------------------------------------------- */
import { HtmlUtility } from "./html-utility.js";
import { Misc } from "./misc.js";
import { RdDCombatManager } from "./rdd-combat.js";
2020-12-05 00:04:40 +01:00
/* -------------------------------------------- */
export class RdDTokenHud {
2021-05-04 21:22:25 +02:00
static init() {
// Integration du TokenHUD
Hooks.on('renderTokenHUD', (app, html, token) => { RdDTokenHud.addTokenHudExtensions(app, html, token._id) });
}
2021-01-05 15:28:05 +01:00
/* -------------------------------------------- */
2021-05-04 21:22:25 +02:00
static async removeExtensionHud(app, html, tokenId) {
html.find('.control-icon.rdd-combat').remove();
html.find('.control-icon.rdd-initiative').remove();
2021-01-05 15:28:05 +01:00
}
2020-12-05 00:04:40 +01:00
/* -------------------------------------------- */
2021-05-04 21:22:25 +02:00
static async addExtensionHud(app, html, tokenId) {
2021-01-05 15:28:05 +01:00
2020-12-20 21:54:09 +01:00
let token = canvas.tokens.get(tokenId);
let actor = token.actor;
2022-06-12 09:46:58 +02:00
let combatant = game.combat.combatants.find(c => c.tokenId == tokenId);
if (! (combatant?.actor) ) {
ui.notifications.warn(`Le combatant ${token.name} n'est pas associé à un acteur, impossible de déterminer ses actions de combat!`)
return;
}
2021-01-05 15:28:05 +01:00
app.hasExtension = true;
let actionsCombat = RdDCombatManager.listActionsCombat(combatant);
2021-05-04 21:22:25 +02:00
const hudData = {
combatant: combatant,
actions: actionsCombat,
commandes: [
{ name: "Autre action", command: 'autre' },
{ name: 'Initiative +1', command: 'inc', value: 0.01 },
{ name: 'Initiative -1', command: 'dec', value: -0.01 }]
2021-05-04 21:22:25 +02:00
};
2021-05-04 21:22:25 +02:00
const controlIconCombat = html.find('.control-icon[data-action=combat]');
// initiative
await RdDTokenHud._configureSubMenu(controlIconCombat,
'systems/foundryvtt-reve-de-dragon/templates/hud-actor-init.html',
hudData,
(event) => {
let initCommand = event.currentTarget.attributes['data-command']?.value;
let combatantId = event.currentTarget.attributes['data-combatant-id']?.value;
if (initCommand) {
RdDTokenHud._initiativeCommand(initCommand, combatantId);
} else {
let index = event.currentTarget.attributes['data-action-index'].value;
let action = actionsCombat[index];
RdDCombatManager.rollInitiativeAction(combatantId, action);
}
2020-12-05 00:04:40 +01:00
});
2021-05-04 21:22:25 +02:00
const controlIconTarget = html.find('.control-icon[data-action=target]');
// combat
2021-05-04 21:22:25 +02:00
await RdDTokenHud._configureSubMenu(controlIconTarget, 'systems/foundryvtt-reve-de-dragon/templates/hud-actor-attaque.html', hudData,
(event) => {
const actionIndex = event.currentTarget.attributes['data-action-index']?.value;
const action = actionsCombat[actionIndex];
if (action.action == 'conjurer') {
actor.conjurerPossession(actor.getPossession(action.system.possessionid));
}
else {
actor.rollArme(action);
}
});
}
static _initiativeCommand(initCommand, combatantId) {
switch (initCommand) {
case 'inc': return RdDCombatManager.incDecInit(combatantId, 0.01);
case 'dec': return RdDCombatManager.incDecInit(combatantId, -0.01);
case 'autre': return RdDCombatManager.rollInitiativeAction(combatantId,
{ name: "Autre action", action: 'autre', system: { initOnly: true, competence: "Autre action" } });
}
}
2021-01-05 15:28:05 +01:00
/* -------------------------------------------- */
static async addTokenHudExtensions(app, html, tokenId) {
2021-05-04 21:22:25 +02:00
const controlIconCombat = html.find('.control-icon[data-action=combat]');
controlIconCombat.click(event => {
if (event.currentTarget.className.includes('active')) {
RdDTokenHud.removeExtensionHud(app, html, tokenId);
2021-01-05 15:28:05 +01:00
} else {
2021-05-04 21:22:25 +02:00
setTimeout(function () { RdDTokenHud.addExtensionHud(app, html, tokenId) }, 200);
2021-01-05 15:28:05 +01:00
}
2021-05-04 21:22:25 +02:00
});
2021-01-05 15:28:05 +01:00
2021-05-04 21:22:25 +02:00
if (controlIconCombat.length>0 && controlIconCombat[0].className.includes('active')) {
RdDTokenHud.addExtensionHud(app, html, tokenId);
2021-01-05 15:28:05 +01:00
}
}
/* -------------------------------------------- */
2020-12-20 21:54:09 +01:00
static async _configureSubMenu(insertionPoint, template, hudData, onMenuItem) {
const hud = $(await renderTemplate(template, hudData));
const list = hud.find('div.rdd-hud-list');
2021-05-04 21:22:25 +02:00
RdDTokenHud._toggleHudListActive(hud, list);
hud.find('img.rdd-hud-togglebutton').click(event => RdDTokenHud._toggleHudListActive(hud, list));
list.find('.rdd-hud-menu').click(onMenuItem);
insertionPoint.after(hud);
}
2021-05-04 21:22:25 +02:00
static _toggleHudListActive(hud, list) {
hud.toggleClass('active');
HtmlUtility.showControlWhen(list, hud.hasClass('active'));
2021-05-04 21:22:25 +02:00
}
2020-12-05 00:04:40 +01:00
}