-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
83 lines (77 loc) · 2.12 KB
/
init.js
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
76
77
78
79
80
81
82
83
Hooks.on('canvasReady', (canvas) => {
canvas.tokens.placeables.forEach((token) => {
token.effects.addListener('childAdded', (child) => {
fixEffectScale(token, child);
});
updateEffectScales(token);
});
});
Hooks.on('createToken', (parent, tokenData) => {
setTimeout(() => {
const token = canvas.tokens.get(tokenData._id);
if (token) {
token.effects.addListener('childAdded', (child) => {
fixEffectScale(token, child);
});
updateEffectScales(token);
}
}, 1000);
});
Hooks.on('updateToken', (parent, tokenData) => {
setTimeout(() => {
const token = canvas.tokens.get(tokenData._id);
updateEffectScales(token);
}, 1000);
});
let pendingUpdates = {};
function fixEffectScale(token, child) {
clearTimeout(pendingUpdates[token.id]);
pendingUpdates[token.id] = setTimeout(() => {
updateEffectScales(token);
}, 10);
}
function countEffects(token) {
if(!token) {
return 0;
}
const tokenEffects = token.data.effects;
const actorEffects = token.actor && token.actor.temporaryEffects || [];
let numEffects = tokenEffects.length;
actorEffects.forEach(actorEffect => {
if ( !actorEffect.getFlag("core", "overlay") ) {
numEffects++;
}
});
return numEffects;
}
function updateEffectScales(token) {
const numEffects = countEffects(token);
if (numEffects > 0 && token.effects.children.length > 0) {
const w = Math.floor(token.w / 3) - 1;
const bg = token.effects.children[0];
bg.clear();
bg.beginFill(0x000000, 0.6).lineStyle(1.0, 0x000000);
token.effects.children.forEach((effectIcon, i) => {
if (i === 0) {
// BG
} else if (i <= numEffects) {
// Effect icon
const ei = i - 1;
const x = (ei % 3) * w;
const y = Math.floor(ei / 3) * w;
effectIcon.width = effectIcon.height = w;
effectIcon.position.x = x;
effectIcon.position.y = y;
bg.drawRoundedRect(
effectIcon.position.x,
effectIcon.position.y,
effectIcon.width,
effectIcon.width,
2
);
} else {
// Overlay icon
}
});
}
}