-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtoken-hud.mjs
75 lines (60 loc) · 2.75 KB
/
token-hud.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
export default (TokenHUD) => class extends TokenHUD {
/** @override */
async _renderInner(data) {
const html = await super._renderInner(data);
if (!this.document || !this.document.sight.enabled || !this.document.isOwner) {
return html;
}
const visionModes = [];
for (const [visionId, detectionId] of [
["blindsight", "blindsight"],
["darkvision", "basicSight"],
["devilsSight", "devilsSight"],
["truesight", "seeAll"],
]) {
if (this.document.detectionModes.some((mode) => mode.id === detectionId && mode.enabled && mode.range > 0)) {
visionModes.push(CONFIG.Canvas.visionModes[visionId]);
}
}
if (visionModes.length <= 1) {
return html;
}
visionModes.sort((a, b) => game.i18n.localize(a.label).localeCompare(game.i18n.localize(b.label), game.i18n.lang));
html[0].querySelector(`.control-icon[data-action="target"]`)
.insertAdjacentHTML("beforebegin", `
<div class="control-icon" data-action="vision-mode">
<i class="fas fa-eye"></i>
</div>
`);
const visionControl = html[0].querySelector(`.control-icon[data-action="vision-mode"]`);
visionControl.addEventListener("click", (event) => {
event.preventDefault();
const button = event.currentTarget;
button.classList.toggle("active");
button.querySelector(`.vision-5e.vision-modes`).classList.toggle("active");
});
visionControl.insertAdjacentHTML("beforeend", `
<div class="vision-5e vision-modes">
${visionModes.map((mode) => `
<div class="vision-5e vision-mode ${mode.id === this.document.sight.visionMode ? "active" : ""} flexrow" data-vision-mode="${mode.id}">
<span class="vision-5e vision-mode-label">${game.i18n.localize(mode.label)}</span>
</div>`).join("")}
</div>
`);
const visionModesList = visionControl.querySelector(`.vision-5e.vision-modes`);
["click", "contextmenu", "mouseenter", "mouseleave"].forEach(
(eventType) => visionModesList.addEventListener(eventType, (event) => {
event.preventDefault();
event.stopPropagation();
}),
);
visionModesList.querySelectorAll(`.vision-5e.vision-mode`).forEach(
(element) => element.addEventListener("click", (event) => {
event.preventDefault();
this.document.updateVisionMode(event.currentTarget.dataset.visionMode);
this.clear();
}),
);
return html;
}
};