-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcycle-token-stack.js
139 lines (119 loc) · 3.42 KB
/
cycle-token-stack.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Cycle through a stack of tokens using the left-click or keyboard shortcut.
* Copyright (c) 2020 by John Sandberg, rights granted as described in LICENSE file
*/
/**
* Class for retaining current control and hover state and processing cycle requests.
*/
class CycleTokenStack {
constructor() {
this.tokenStack = [];
this.hovering = null;
this.cancelClick = false;
this.clicking = false;
this.isTooltipOK = false;
this.keyCycleForward = '[';
this.keyCycleBackward = ']';
this.showTokenList = "stacked";
this.minClickDelay = 300;
}
IsDeactivated(e) {
return (!this.isTooltipOK || !e || e.altKey || e.ctrlKey || e.metaKey ||
ui.controls.controls.find(n => n.name === "token").activeTool === "target");
}
BuildStack(token) {
this.tokenStack = [];
if (token) {
this.tokenStack = canvas.tokens.placeables.filter(t => (game.user.isGM || t.owner) &&
(t.x + t.w > token.x && t.y + t.h > token.y && t.x < token.x + token.w && t.y < token.y + token.h));
}
return token;
}
CycleSelected(token) {
if (!token || this.tokenStack.length < 2) return token;
let idx = this.tokenStack.findIndex(t => t.id === token.id);
idx = ((idx + 1) % this.tokenStack.length);
this.tokenStack[idx].control({ releaseOthers: true });
return this.tokenStack[idx];
}
UncycleSelected(token) {
if (!token || this.tokenStack.length < 2) return token;
this.tokenStack.forEach(t => {
if (t.id !== token.id)
t.control({ releaseOthers: true });
});
return token;
}
ReleaseHovered(token) {
this.BuildStack(token);
this.UncycleSelected(token);
}
RefreshStack(token) {
this.BuildStack(token);
this.CycleSelected(token);
}
OnKeyDown(e) {
if (this.IsDeactivated(e)) return;
if (this.hovering && e.key === this.keyCycleForward) {
if (this.hovering._controlled)
this.RefreshStack(this.hovering);
else {
this.hovering.control({ releaseOthers: true });
}
}
else if (this.hovering && e.key === this.keyCycleBackward)
this.ReleaseHovered(this.hovering);
}
OnMouseMove(e) {
_CycleTokenStack.cancelClick = true;
}
MouseDown(t, f) {
this.clicking = true;
this.cancelClick = false;
t.once('mousemove', this.OnMouseMove);
setTimeout(() => {
t.off('mousemove', this.OnMouseMove);
if (!this.cancelClick) {
if (f) this.RefreshStack(t);
}
this.clicking = false;
this.cancelClick = false;
}, this.minClickDelay);
}
OnMouseDown(e) {
const c = _CycleTokenStack;
const oe = e.data.originalEvent;
if (c.IsDeactivated(oe) || oe.shiftKey) return;
if (c.clicking) { c.cancelClick = true; return; }
c.MouseDown(this, true);
}
}
let _CycleTokenStack = new CycleTokenStack();
onkeydown = function (e) {
e = e || event;
_CycleTokenStack.OnKeyDown(e);
};
Hooks.on("controlToken", (token, controlled) => {
const c = _CycleTokenStack;
if (controlled) {
c.isTooltipOK = true;
token.on('mousedown', c.OnMouseDown);
}
else
token.off('mousedown', c.OnMouseDown);
});
Hooks.on("hoverToken", (token, hover) => {
const c = _CycleTokenStack;
if (hover) {
if (!c.clicking && !c.IsDeactivated(event)) {
c.hovering = token;
}
} else {
if (!c.clicking || c.cancelClick) {
c.hovering = null;
}
}
});
Hooks.on("deleteToken", (token) => {
_CycleTokenStack.hovering = null;
});