191 lines
7.5 KiB
JavaScript
191 lines
7.5 KiB
JavaScript
/* -------------------------------------------- */
|
|
import { renderTemplate } from "./constants.js";
|
|
import { HtmlUtility } from "./html-utility.js";
|
|
import { Misc } from "./misc.js";
|
|
import { RdDCombatManager } from "./rdd-combat.js";
|
|
import { OptionsAvancees, ROLL_DIALOG_V2 } from "./settings/options-avancees.js";
|
|
import { Targets } from "./targets.js";
|
|
|
|
/* -------------------------------------------- */
|
|
export class RdDTokenHud {
|
|
|
|
static init() {
|
|
// Integration du TokenHUD
|
|
Hooks.on('renderTokenHUD', (app, html, token) => { RdDTokenHud.addTokenHudExtensions(app, html, token._id) });
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static async removeExtensionHud(app, html, tokenId) {
|
|
$(html).find('.control-icon.rdd-combat').remove();
|
|
$(html).find('.control-icon.rdd-initiative').remove();
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static async addExtensionHud(app, html, tokenId, isCombat) {
|
|
|
|
let token = canvas.tokens.get(tokenId);
|
|
let actor = token.actor;
|
|
app.hasExtension = true;
|
|
// soins
|
|
await RdDTokenHud.addExtensionHudSoins(html, actor);
|
|
|
|
if (isCombat) {
|
|
const combatant = game.combat.combatants.find(c => c.tokenId == tokenId)
|
|
const actor = RdDCombatManager.getActorCombatant(combatant, { warning: false })
|
|
if (actor) {
|
|
if (OptionsAvancees.isUsing(ROLL_DIALOG_V2)) {
|
|
await RdDTokenHud.addExtensionHudCombat(html, combatant, actor, token)
|
|
}
|
|
else {
|
|
const actions = RdDCombatManager.listActionsActorCombatant(actor)
|
|
// initiative
|
|
await RdDTokenHud.addExtensionHudInit(html, combatant, actions)
|
|
// combat
|
|
await RdDTokenHud.addExtensionHudAttaques(html, combatant, token, actions.filter(it => !it.initOnly))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static async addExtensionHudCombat(html, combatant, actor, token) {
|
|
const actionsActor = actor.listActionsCombat();
|
|
const ajustements = combatant?.initiative ?
|
|
[
|
|
{ label: 'Initiative +1', action: 'delta', value: 1 },
|
|
{ label: 'Initiative -1', action: 'delta', value: -1 }
|
|
] : []
|
|
const autres = [{ label: "Autre action", action: 'autre' }]
|
|
const actions = Misc.indexed(actionsActor.concat(ajustements).concat(autres))
|
|
|
|
const hudData = { combatant, token, actions };
|
|
const hud = $(await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/hud-actor-combat.hbs', hudData))
|
|
$(html).find('div.col.left').append(hud)
|
|
|
|
const list = hud.find('div.rdd-hud-list')
|
|
RdDTokenHud.setupHudToggle(hud, list)
|
|
|
|
const selectInitiative = list.find('select[name="initiative"]');
|
|
selectInitiative.change(event => {
|
|
const action = actions.find(it => it.index == event.currentTarget.value)
|
|
console.log('select initiative', combatant.id, action)
|
|
if (action) {
|
|
switch (action.action) {
|
|
case 'delta':
|
|
RdDCombatManager.incDecInit(combatant.id, action.value);
|
|
break
|
|
case 'autre':
|
|
RdDCombatManager.rollInitiativeAction(combatant.id,
|
|
{ label: "Autre action", action: 'autre', system: { initOnly: true, competence: "Autre action" } });
|
|
break
|
|
default:
|
|
RdDCombatManager.rollInitiativeAction(combatant.id, action)
|
|
}
|
|
selectInitiative.select("")
|
|
}
|
|
})
|
|
list.find('.rdd-attaque-v2').click(event => combatant.actor.rollAttaque(token))
|
|
}
|
|
|
|
static async addExtensionHudInit(html, combatant, actions) {
|
|
const hudData = {
|
|
combatant, actions,
|
|
commandes: [
|
|
{ label: "Autre action", command: 'autre' },
|
|
{ label: 'Initiative +1', command: 'delta', value: 1 },
|
|
{ label: 'Initiative -1', command: 'deltac', value: -1 }]
|
|
};
|
|
const controlIconCombat = $(html).find('.control-icon[data-action=combat]');
|
|
await RdDTokenHud._configureSubMenu(it => controlIconCombat.after(it),
|
|
'systems/foundryvtt-reve-de-dragon/templates/hud-actor-init.hbs',
|
|
hudData,
|
|
(event) => {
|
|
let initCommand = event.currentTarget.attributes['data-command']?.value
|
|
let initCommandValue = Number(event.currentTarget.attributes['data-command-value']?.value ?? 0)
|
|
let combatantId = event.currentTarget.attributes['data-combatant-id']?.value
|
|
if (initCommand) {
|
|
RdDCombatManager.applyInitiativeCommand(combatantId, initCommand, initCommandValue)
|
|
} else {
|
|
let index = event.currentTarget.attributes['data-action-index'].value
|
|
let action = hudData.actions[index]
|
|
RdDCombatManager.rollInitiativeAction(combatantId, action)
|
|
}
|
|
})
|
|
}
|
|
|
|
static async addExtensionHudAttaques(html, combatant, token, actions) {
|
|
|
|
const hudData = { combatant, token, actions, commandes: [] };
|
|
const divColLeft = $(html).find('div.col.left');
|
|
await RdDTokenHud._configureSubMenu(it => divColLeft.append(it),
|
|
'systems/foundryvtt-reve-de-dragon/templates/hud-actor-attaque.hbs',
|
|
hudData,
|
|
(event) => {
|
|
const actionIndex = event.currentTarget.attributes['data-action-index']?.value;
|
|
const action = hudData.actions[actionIndex];
|
|
const possession = action.action == 'possession' ? combatant.actor.getPossession(action.possessionid) : undefined;
|
|
if (possession) {
|
|
combatant.actor.conjurerPossession(possession);
|
|
}
|
|
else {
|
|
combatant.actor.rollArme(action.arme, action.main, token)
|
|
}
|
|
});
|
|
}
|
|
|
|
static async addExtensionHudSoins(html, sourceActor) {
|
|
const target = Targets.getTarget({ warn: false });
|
|
if (target?.actor) {
|
|
const hudSoins = { blessures: target.actor.blessuresASoigner() ?? [] };
|
|
if (hudSoins.blessures.length > 0) {
|
|
const controlIconTarget = $(html).find('.control-icon[data-action=combat]');
|
|
await RdDTokenHud._configureSubMenu(it => controlIconTarget.after(it),
|
|
'systems/foundryvtt-reve-de-dragon/templates/hud-actor-soins.hbs',
|
|
hudSoins,
|
|
(event) => {
|
|
const blessureId = event.currentTarget.attributes['data-blessure-id']?.value;
|
|
sourceActor.rollSoins(target.actor, blessureId)
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static async addTokenHudExtensions(app, html, tokenId) {
|
|
console.log(`Adding token HUD extensions for token ${tokenId}`);
|
|
const controlIconCombat = $(html).find('.control-icon[data-action=combat]');
|
|
if (controlIconCombat.length > 0) {
|
|
controlIconCombat.click(event => {
|
|
if (event.currentTarget.className.includes('active')) {
|
|
RdDTokenHud.removeExtensionHud(app, html, tokenId);
|
|
} else {
|
|
setTimeout(() => RdDTokenHud.addExtensionHud(app, html, tokenId), 200);
|
|
}
|
|
});
|
|
|
|
const isCombat = controlIconCombat[0].className.includes('active');
|
|
RdDTokenHud.addExtensionHud(app, html, tokenId, isCombat);
|
|
}
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static async _configureSubMenu(callInsertion, template, hudData, onMenuItem) {
|
|
const hud = $(await renderTemplate(template, hudData));
|
|
const list = hud.find('div.rdd-hud-list');
|
|
|
|
RdDTokenHud.setupHudToggle(hud, list)
|
|
|
|
list.find('.rdd-hud-menu').click(onMenuItem);
|
|
|
|
callInsertion(hud);
|
|
}
|
|
|
|
static setupHudToggle(hud, list) {
|
|
function toggleHudList(hud, list) {
|
|
hud.toggleClass('active')
|
|
HtmlUtility.showControlWhen(list, hud.hasClass('active'))
|
|
}
|
|
toggleHudList(hud, list)
|
|
$(hud).find('img.rdd-hud-togglebutton').click(event => toggleHudList(hud, list))
|
|
}
|
|
|
|
} |