add spotlight request display on token option for GM

This commit is contained in:
CPTN Cosmo 2026-07-10 20:58:25 +02:00
parent 373b02a8fa
commit e020a4efd7
4 changed files with 149 additions and 23 deletions

View file

@ -9,6 +9,23 @@ Hooks.once("init", () => {
default: true
});
game.settings.register("cosmos-dh-tweaks", "spotlightRequestIndicator", {
name: "COSMOS_DH_TWEAKS.Settings.SpotlightRequestIndicator.Name",
hint: "COSMOS_DH_TWEAKS.Settings.SpotlightRequestIndicator.Hint",
scope: "world",
config: true,
type: Boolean,
default: true,
onChange: () => {
if (canvas.ready) {
for (const token of canvas.tokens.placeables) {
if (token.refresh) token.refresh();
else if (token.renderFlags) token.renderFlags.set({ refreshState: true });
}
}
}
});
game.settings.register("cosmos-dh-tweaks", "minimalCountdownUI", {
name: "COSMOS_DH_TWEAKS.Settings.MinimalCountdownUI.Name",
hint: "COSMOS_DH_TWEAKS.Settings.MinimalCountdownUI.Hint",
@ -97,7 +114,9 @@ Hooks.on("renderTokenHUD", (tokenHUD, html, tokenData) => {
// Players: only proceed if they own the token
if (!token.document.isOwner) return;
const combatant = token.combatant;
active = combatant?.system.spotlight?.requesting || false;
active = combatant
? (combatant.system.spotlight?.requesting || false)
: (token.actor?.getFlag("cosmos-dh-tweaks", "requestingSpotlight") || false);
}
const title = isGM
@ -154,24 +173,31 @@ Hooks.on("renderTokenHUD", (tokenHUD, html, tokenData) => {
token.renderFlags.set({ refreshTurnMarker: true });
}
}
// Clear spotlight request indicator if GM grants/takes spotlight
if (token.actor?.getFlag("cosmos-dh-tweaks", "requestingSpotlight")) {
await token.actor.setFlag("cosmos-dh-tweaks", "requestingSpotlight", false);
}
} else {
// Player requesting / cancelling spotlight
const combatant = token.combatant;
if (!combatant) {
ui.notifications.warn(game.i18n.localize("COSMOS_DH_TWEAKS.Spotlight.warnNoCombatant"));
return;
}
const isRequesting = combatant.system.spotlight.requesting;
const characters = game.combat.turns?.filter(x => !x.isNPC) ?? [];
const orderValues = characters.map(character => character.system.spotlight.requestOrderIndex || 0);
const maxRequestIndex = Math.max(...orderValues, 0);
if (combatant) {
const isRequesting = combatant.system.spotlight.requesting;
const characters = game.combat.turns?.filter(x => !x.isNPC) ?? [];
const orderValues = characters.map(character => character.system.spotlight.requestOrderIndex || 0);
const maxRequestIndex = Math.max(...orderValues, 0);
await combatant.update({
'system.spotlight': {
requesting: !isRequesting,
requestOrderIndex: !isRequesting ? maxRequestIndex + 1 : 0
}
});
await combatant.update({
'system.spotlight': {
requesting: !isRequesting,
requestOrderIndex: !isRequesting ? maxRequestIndex + 1 : 0
}
});
} else {
// Out of combat request
const isRequesting = !!token.actor?.getFlag("cosmos-dh-tweaks", "requestingSpotlight");
await token.actor?.setFlag("cosmos-dh-tweaks", "requestingSpotlight", !isRequesting);
}
}
// Rerender HUD to update icon state
@ -206,3 +232,96 @@ Hooks.on("updateSetting", (setting, change, options, userId) => {
}
}
});
// Trigger token refresh when Actor changes their requestingSpotlight flag
Hooks.on("updateActor", (actor, change, options, userId) => {
if (!game.ready) return;
const tokens = actor.getActiveTokens();
for (const token of tokens) {
if (token.refresh) token.refresh();
else if (token.renderFlags) token.renderFlags.set({ refreshState: true });
}
if (canvas.tokens.hud.rendered && canvas.tokens.hud.object?.actor?.id === actor.id) {
canvas.tokens.hud.render(true);
}
});
// Trigger token refresh when Token changes
Hooks.on("updateToken", (tokenDoc, change, options, userId) => {
if (!game.ready) return;
const token = tokenDoc.object;
if (token) {
if (token.refresh) token.refresh();
else if (token.renderFlags) token.renderFlags.set({ refreshState: true });
}
if (canvas.tokens.hud.rendered && canvas.tokens.hud.object?.id === tokenDoc.id) {
canvas.tokens.hud.render(true);
}
});
// Render the GM's spotlight request indicator on the token canvas
Hooks.on("refreshToken", (token, options) => {
if (!game.ready) return;
if (!game.user?.isGM) return;
if (!game.settings.get("cosmos-dh-tweaks", "tokenHudSpotlight")) return;
if (!game.settings.get("cosmos-dh-tweaks", "spotlightRequestIndicator")) {
if (token.cosmoSpotlightIndicator) {
token.cosmoSpotlightIndicator.visible = false;
}
return;
}
const inCombat = !!token.combatant;
const requestingInCombat = token.combatant?.system.spotlight?.requesting;
const requestingOutOfCombat = token.actor?.getFlag("cosmos-dh-tweaks", "requestingSpotlight");
const isRequesting = inCombat ? requestingInCombat : requestingOutOfCombat;
if (isRequesting) {
// Safe check to reconstruct if it was destroyed or detached
const isDestroyed = token.cosmoSpotlightIndicator && (token.cosmoSpotlightIndicator.destroyed || !token.children.includes(token.cosmoSpotlightIndicator));
if (isDestroyed) {
token.cosmoSpotlightIndicator = null;
}
if (!token.cosmoSpotlightIndicator) {
const container = new PIXI.Container();
container.name = "cosmoSpotlightIndicator";
// Draw a background circle (gold)
const bg = new PIXI.Graphics();
bg.beginFill(0xc5a45e, 0.9);
bg.lineStyle(1.5, 0xffffff, 1);
bg.drawCircle(0, 0, 11);
bg.endFill();
container.addChild(bg);
// Draw FontAwesome solid e05d hand sparkles icon
const style = new PIXI.TextStyle({
fontFamily: '"Font Awesome 6 Free"',
fontWeight: "900",
fontSize: 13,
fill: "#ffffff"
});
const text = new PIXI.Text("\ue05d", style);
text.anchor.set(0.5);
container.addChild(text);
token.cosmoSpotlightIndicator = token.addChild(container);
}
// Position the indicator at the top-right of the token container
const size = token.w || 50;
token.cosmoSpotlightIndicator.position.set(size - 10, 10);
token.cosmoSpotlightIndicator.visible = true;
} else {
if (token.cosmoSpotlightIndicator) {
token.cosmoSpotlightIndicator.visible = false;
}
}
});
Hooks.on("destroyToken", (token) => {
if (token.cosmoSpotlightIndicator) {
token.cosmoSpotlightIndicator = null;
}
});