dh-token-spotlight/scripts/main.js

51 lines
1.9 KiB
JavaScript

Hooks.on('renderTokenHUD', (app, html, data) => {
const token = app.object;
if (!token?.combatant) return;
const viewedCombat = ui.combat?.viewed;
if (!viewedCombat) return;
// In Daggerheart, the combatant with the spotlight is the one whose turn it currently is.
const hasSpotlight = viewedCombat.combatant?.id === token.combatant.id;
// Determine icon and classes based on spotlight state
const iconClass = hasSpotlight
? "fa-solid fa-hand-sparkles dh-spotlight-glow"
: "fa-regular fa-hand-sparkles";
const buttonHtml = `
<div class="control-icon ${hasSpotlight ? 'active' : ''}" data-action="toggle-spotlight" title="Toggle Spotlight">
<i class="${iconClass}"></i>
</div>
`;
// Append to the right column of the Token HUD
const colRight = html.find('.col.right');
colRight.append(buttonHtml);
// Add click listener
html.find('[data-action="toggle-spotlight"]').click(async event => {
event.preventDefault();
event.stopPropagation();
if (ui.combat && typeof ui.combat.setCombatantSpotlight === "function") {
const btn = $(event.currentTarget);
const icon = btn.find('i');
const wasActive = btn.hasClass('active');
// Optimistic UI update
if (wasActive) {
btn.removeClass('active');
icon.removeClass('fa-solid dh-spotlight-glow').addClass('fa-regular');
} else {
btn.addClass('active');
icon.removeClass('fa-regular').addClass('fa-solid dh-spotlight-glow');
}
// Call the system's spotlight toggle function
await ui.combat.setCombatantSpotlight(token.combatant.id);
} else {
ui.notifications.warn("System does not support spotlight or combat is not initialized.");
}
});
});