bol/module/system/bol-action-hud.js

96 lines
3.5 KiB
JavaScript
Raw Permalink Normal View History

2022-03-21 23:21:05 +01:00
/* -------------------------------------------- */
import { BoLRoll } from "../controllers/bol-rolls.js";
2023-06-23 08:37:50 +02:00
import { BoLUtility } from "../system/bol-utility.js";
2022-03-21 23:21:05 +01:00
/* -------------------------------------------- */
export class BoLTokenHud {
static init() {
// Integration du TokenHUD
Hooks.on('renderTokenHUD', (app, html, data) => { BoLTokenHud.addTokenHudExtensions(app, html, data._id) });
}
/* -------------------------------------------- */
static async removeExtensionHud(app, html, tokenId) {
html.find('.control-icon.bol-roll').remove()
html.find('.control-icon.bol-action').remove()
}
/* -------------------------------------------- */
static async addExtensionHud(app, html, tokenId) {
let token = canvas.tokens.get(tokenId)
let actor = token.actor
app.hasExtension = true
const hudData = { actor: actor, actionsList: actor.buildListeActions(), rollsList: actor.buildRollList() }
const controlIconActions = html.find('.control-icon[data-action=combat]');
// initiative
await BoLTokenHud._configureSubMenu(controlIconActions, 'systems/bol/templates/token/hud-actor-actions.hbs', hudData,
(event) => {
let actionIndex = Number(event.currentTarget.attributes['data-action-index'].value)
let action = hudData.actionsList[actionIndex]
2023-06-23 08:37:50 +02:00
const actionItem = actor.items.get(action._id)
if (actionItem.system.subtype == "weapon") {
BoLRoll.weaponCheckWithWeapon(hudData.actor, duplicate(actionItem))
} else if (actionItem.system.subtype == "fightoption") {
let chatData = duplicate(actionItem)
if (actionItem.actor) {
chatData.actor = { id: actionItem.actor._id };
}
BoLUtility.postItem(chatData);
}
})
2022-03-21 23:21:05 +01:00
const controlIconTarget = html.find('.control-icon[data-action=target]');
// att+apt+career
await BoLTokenHud._configureSubMenu(controlIconTarget, 'systems/bol/templates/token/hud-actor-rolls.hbs', hudData,
(event) => {
let rollIndex = Number(event.currentTarget.attributes['data-roll-index'].value)
let roll = hudData.rollsList[rollIndex]
2023-06-23 08:37:50 +02:00
if (roll.type == "aptitude") {
BoLRoll.aptitudeCheck(actor, roll.key)
} else if (roll.type == "attribute") {
BoLRoll.attributeCheck(actor, roll.key)
2022-03-21 23:21:05 +01:00
}
})
}
/* -------------------------------------------- */
static async addTokenHudExtensions(app, html, tokenId) {
2023-06-23 08:37:50 +02:00
const controlIconCombat = html.find('.control-icon[data-action=combat]')
if (controlIconCombat.length > 0) {
2022-03-21 23:21:05 +01:00
BoLTokenHud.addExtensionHud(app, html, tokenId);
}
}
/* -------------------------------------------- */
static async _configureSubMenu(insertionPoint, template, hudData, onMenuItem) {
const hud = $(await renderTemplate(template, hudData))
const list = hud.find('div.bol-hud-list')
2023-06-23 08:37:50 +02:00
2022-03-21 23:21:05 +01:00
BoLTokenHud._toggleHudListActive(hud, list);
2023-06-23 08:37:50 +02:00
2022-03-21 23:21:05 +01:00
hud.find('img.bol-hud-togglebutton').click(event => BoLTokenHud._toggleHudListActive(hud, list));
list.find('.bol-hud-menu').click(onMenuItem);
insertionPoint.after(hud);
}
/* -------------------------------------------- */
static _showControlWhen(control, condition) {
if (condition) {
control.show()
}
else {
control.hide()
}
}
/* -------------------------------------------- */
static _toggleHudListActive(hud, list) {
hud.toggleClass('active')
BoLTokenHud._showControlWhen(list, hud.hasClass('active'))
}
}