-
Notifications
You must be signed in to change notification settings - Fork 1
/
pushTokenBack.js
89 lines (57 loc) · 1.99 KB
/
pushTokenBack.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
// pushTokenBack
// (c) 2021 David Zvekic
// permission granted to use and distribute as per LICENSE file
let pushTokenBack = new Object();
pushTokenBack.hoverToken= new Object();
pushTokenBack.hoverToken.hoveredTarget=0;
pushTokenBack.hotkey='';
Hooks.on('init', () => {
game.settings.register('pushTokenBack', 'hotkey', {
name: game.i18n.localize("PUSHTOKENBACK.SelectHotKey"),
hint: game.i18n.localize("PUSHTOKENBACK.SelectHotKeyHelp"),
scope: 'client',
config: true,
type: String,
default: "Z",
onChange: value => { pushTokenBack.hotkey = value // value is the new value of the setting
console.log(value)
}
});
pushTokenBack.hotkey=game.settings.get('pushTokenBack', 'hotkey');
});
pushTokenBack.pushTokenBackListener = function(event){
if ( event.isComposing ) return;
if (event.key==pushTokenBack.hotkey && !event.repeat) pushToBack();
};
pushTokenBack.hoverToken.hook= Hooks.on('hoverToken',(token,hoverON)=>{
if (hoverON) {
pushTokenBack.hoverToken.hoveredTarget=token;
window.addEventListener('keydown', pushTokenBack.pushTokenBackListener );
}
else {
window.removeEventListener('keydown', pushTokenBack.pushTokenBackListener );
delete pushTokenBack.hoverToken.hoveredTarget;
}
});
function pushToBack()
{ if (typeof pushTokenBack == "undefined") {
console.warn("Module pushTokenBack is not installed.");
return;
}
if ( pushTokenBack?.hoverToken?.hoveredTarget instanceof Token) {
const token = pushTokenBack.hoverToken.hoveredTarget;
let position = 0;
var foundchild;
for (const x of canvas.tokens.children[0].children) {
if (x==token) break;
position++;
}
if (position<canvas.tokens.children[0].children.length){
// found the child, push it to element 0
canvas.tokens.children[0].children.splice(position,1);
canvas.tokens.children[0].children.unshift(token);
}
} else {
console.warn('pushtoback - invalid target', pushTokenBack.hoverToken.hoveredTarget);
}
};