fvtt-les-heritiers/modules/heritiers-hud.js

71 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-12-26 00:33:13 +01:00
/* -------------------------------------------- */
/* -------------------------------------------- */
export class HeritiersTokenHud {
static init() {
// Integration du TokenHUD
Hooks.on('renderTokenHUD', (app, html, data) => { HeritiersTokenHud.addTokenHudExtensions(app, html, data._id) });
}
/* -------------------------------------------- */
static async removeExtensionHud(app, html, tokenId) {
2022-12-26 10:13:05 +01:00
html.find('.control-icon.heritiers-adversite').remove()
2022-12-26 00:33:13 +01:00
}
/* -------------------------------------------- */
static async addExtensionHud(app, html, tokenId) {
let token = canvas.tokens.get(tokenId)
let actor = token.actor
app.hasExtension = true
const hudData = { actor: actor }
const controlIconActions = html.find('.control-icon[data-action=combat]');
// initiative
await HeritiersTokenHud._configureSubMenu(controlIconActions, 'systems/fvtt-les-heritiers/templates/hud-adversites.html', hudData,
(event) => {
let adversite = event.currentTarget.attributes['data-action-index'].value
let value = Number(event.currentTarget.attributes['data-action-value'].value)
hudData.actor.incDecAdversite( adversite, value)
} )
}
/* -------------------------------------------- */
static async addTokenHudExtensions(app, html, tokenId) {
const controlIconCombat = html.find('.control-icon[data-action=combat]')
if (controlIconCombat.length>0 ) {
HeritiersTokenHud.addExtensionHud(app, html, tokenId);
}
}
/* -------------------------------------------- */
static async _configureSubMenu(insertionPoint, template, hudData, onMenuItem) {
const hud = $(await renderTemplate(template, hudData))
2022-12-26 10:13:05 +01:00
const list = hud.find('div.heritiers-hud-list')
2022-12-26 00:33:13 +01:00
HeritiersTokenHud._toggleHudListActive(hud, list);
2022-12-26 10:13:05 +01:00
hud.find('img.heritiers-hud-togglebutton').click(event => HeritiersTokenHud._toggleHudListActive(hud, list));
list.find('.heritiers-hud-adversite').click(onMenuItem);
2022-12-26 00:33:13 +01:00
insertionPoint.after(hud);
}
/* -------------------------------------------- */
static _showControlWhen(control, condition) {
if (condition) {
control.show()
}
else {
control.hide()
}
}
/* -------------------------------------------- */
static _toggleHudListActive(hud, list) {
hud.toggleClass('active')
HeritiersTokenHud._showControlWhen(list, hud.hasClass('active'))
}
}