forked from MaterialFoundry/MaterialDeck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaterialDeck.js
389 lines (339 loc) · 12 KB
/
MaterialDeck.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import {registerSettings} from "./src/settings.js";
import {StreamDeck} from "./src/streamDeck.js";
import {TokenControl} from "./src/token.js";
import {Move} from "./src/move.js";
import {MacroControl} from "./src/macro.js";
import {CombatTracker} from "./src/combattracker.js";
import {PlaylistControl} from "./src/playlist.js";
import {SoundboardControl} from "./src/soundboard.js";
import {OtherControls} from "./src/othercontrols.js";
import {ExternalModules} from "./src/external.js";
import {SceneControl} from "./src/scene.js";
export var streamDeck;
export var tokenControl;
var move;
export var macroControl;
export var combatTracker;
export var playlistControl;
export var soundboard;
export var otherControls;
export var externalModules;
export var sceneControl;
export const moduleName = "MaterialDeck";
export var selectedTokenId;
let ready = false;
let activeSounds = [];
//CONFIG.debug.hooks = true;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Global variables
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
export var enableModule;
//Websocket variables
var ws; //Websocket variable
let wsOpen = false; //Bool for checking if websocket has ever been opened => changes the warning message if there's no connection
let wsInterval; //Interval timer to detect disconnections
let WSconnected = false;
/*
* Analyzes the message received
*
* @param {*} msg Message received
*/
async function analyzeWSmessage(msg){
if (enableModule == false) return;
const data = JSON.parse(msg);
//console.log("Received",data);
if (data.type == "connected" && data.data == "SD"){
console.log("streamdeck connected to server");
streamDeck.resetImageBuffer();
}
if (data == undefined || data.payload == undefined) return;
//console.log("Received",data);
const action = data.action;
const event = data.event;
const context = data.context;
const coordinates = data.payload.coordinates;
if (coordinates == undefined) coordinates = 0;
const settings = data.payload.settings;
if (data.data == 'init'){
}
if (event == 'willAppear' || event == 'didReceiveSettings'){
streamDeck.setScreen(action);
streamDeck.setContext(action,context,coordinates,settings);
if (action == 'token'){
tokenControl.active = true;
tokenControl.update(selectedTokenId);
}
else if (action == 'move')
move.update(settings,context);
else if (action == 'macro')
macroControl.update(settings,context);
else if (action == 'combattracker')
combatTracker.update(settings,context);
else if (action == 'playlist')
playlistControl.update(settings,context);
else if (action == 'soundboard')
soundboard.update(settings,context);
else if (action == 'other')
otherControls.update(settings,context);
else if (action == 'external')
externalModules.update(settings,context);
else if (action == 'scene')
sceneControl.update(settings,context);
}
else if (event == 'willDisappear'){
streamDeck.clearContext(action,coordinates);
}
else if (event == 'keyDown'){
if (action == 'token')
tokenControl.keyPress(settings);
else if (action == 'move')
move.keyPress(settings);
else if (action == 'macro')
macroControl.keyPress(settings);
else if (action == 'combattracker')
combatTracker.keyPress(settings,context);
else if (action == 'playlist')
playlistControl.keyPress(settings,context);
else if (action == 'soundboard')
soundboard.keyPressDown(settings);
else if (action == 'other')
otherControls.keyPress(settings,context);
else if (action == 'external')
externalModules.keyPress(settings,context);
else if (action == 'scene')
sceneControl.keyPress(settings);
}
else if (event == 'keyUp'){
if (action == 'soundboard'){
soundboard.keyPressUp(settings);
}
}
};
/**
* Start a new websocket
* Start a 10s interval, if no connection is made, run resetWS()
* If connection is made, set interval to 1.5s to check for disconnects
* If message is received, reset the interval, and send the message to analyzeWSmessage()
*/
function startWebsocket() {
const address = game.settings.get(moduleName,'address');
ws = new WebSocket('ws://'+address+'/');
ws.onmessage = function(msg){
//console.log(msg);
analyzeWSmessage(msg.data);
clearInterval(wsInterval);
wsInterval = setInterval(resetWS, 5000);
}
ws.onopen = function() {
WSconnected = true;
ui.notifications.info("Material Deck "+game.i18n.localize("MaterialDeck.Notifications.Connected") +": "+address);
wsOpen = true;
const msg = {
target: "server",
module: "MD"
}
ws.send(JSON.stringify(msg));
const msg2 = {
target: "SD",
type: "init"
}
ws.send(JSON.stringify(msg2));
clearInterval(wsInterval);
wsInterval = setInterval(resetWS, 5000);
}
clearInterval(wsInterval);
wsInterval = setInterval(resetWS, 10000);
}
/**
* Try to reset the websocket if a connection is lost
*/
function resetWS(){
if (wsOpen) ui.notifications.warn("Material Deck: "+game.i18n.localize("MaterialDeck.Notifications.Disconnected"));
else ui.notifications.warn("Material Deck: "+game.i18n.localize("MaterialDeck.Notifications.ConnectFail"));
WSconnected = false;
startWebsocket();
}
export function sendWS(txt){
if (WSconnected)
ws.send(txt);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Hooks
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Ready hook
* Attempt to open the websocket
*/
Hooks.once('ready', ()=>{
enableModule = (game.settings.get(moduleName,'Enable')) ? true : false;
game.socket.on(`module.MaterialDeck`, (payload) =>{
//console.log(payload);
if (payload.msgType != "playSound") return;
playTrack(payload.trackNr,payload.src,payload.play,payload.repeat,payload.volume);
});
for (let i=0; i<64; i++)
activeSounds[i] = false;
if (enableModule == false) return;
if (game.user.isGM == false) {
ready = true;
return;
}
startWebsocket();
soundboard = new SoundboardControl();
streamDeck = new StreamDeck();
tokenControl = new TokenControl();
move = new Move();
macroControl = new MacroControl();
combatTracker = new CombatTracker();
playlistControl = new PlaylistControl();
otherControls = new OtherControls();
externalModules = new ExternalModules();
sceneControl = new SceneControl();
let soundBoardSettings = game.settings.get(moduleName,'soundboardSettings');
let macroSettings = game.settings.get(moduleName, 'macroSettings');
let array = [];
for (let i=0; i<64; i++) array[i] = "";
let arrayVolume = [];
for (let i=0; i<64; i++) arrayVolume[i] = "50";
let arrayZero = [];
for (let i=0; i<64; i++) arrayZero[i] = "0";
if (macroSettings.color == undefined){
game.settings.set(moduleName,'macroSettings',{
macros: array,
color: arrayZero
});
}
if (soundBoardSettings.colorOff == undefined){
game.settings.set(moduleName,'soundboardSettings',{
playlist: "",
sounds: array,
colorOn: arrayZero,
colorOff: arrayZero,
mode: arrayZero,
toggle: arrayZero,
volume: arrayVolume
});
}
});
export function playTrack(soundNr,src,play,repeat,volume){
if (play){
volume *= game.settings.get("core", "globalInterfaceVolume");
let howl = new Howl({src, volume, loop: repeat, onend: (id)=>{
if (repeat == false){
activeSounds[soundNr] = false;
}
},
onstop: (id)=>{
activeSounds[soundNr] = false;
}});
howl.play();
activeSounds[soundNr] = howl;
}
else {
activeSounds[soundNr].stop();
activeSounds[soundNr] = false;
}
}
Hooks.on('updateToken',(scene,token)=>{
if (enableModule == false || ready == false) return;
let tokenId = token._id;
if (tokenId == selectedTokenId)
tokenControl.update(selectedTokenId);
});
Hooks.on('updateActor',(scene,actor)=>{
if (enableModule == false || ready == false) return;
let children = canvas.tokens.children[0].children;
for (let i=0; i<children.length; i++){
if (children[i].actor.id == actor._id){
let tokenId = children[i].id;
if (tokenId == selectedTokenId)
tokenControl.update(selectedTokenId);
}
}
});
Hooks.on('controlToken',(token,controlled)=>{
if (enableModule == false || ready == false) return;
if (controlled) {
selectedTokenId = token.data._id;
}
else {
selectedTokenId = undefined;
}
tokenControl.update(selectedTokenId);
});
Hooks.on('renderHotbar', (hotbar)=>{
if (enableModule == false || ready == false) return;
if (macroControl != undefined) macroControl.hotbar(hotbar.macros);
});
Hooks.on('renderCombatTracker',()=>{
if (enableModule == false || ready == false) return;
if (combatTracker != undefined) combatTracker.updateAll();
if (tokenControl != undefined) tokenControl.update(selectedTokenId);
});
Hooks.on('renderPlaylistDirectory', (playlistDirectory)=>{
if (enableModule == false || ready == false) return;
if (playlistControl != undefined) playlistControl.updateAll();
});
Hooks.on('closeplaylistConfigForm', (form)=>{
if (enableModule == false || ready == false) return;
if (form.template == "./modules/MaterialDeck/templates/playlistConfig.html")
playlistControl.updateAll();
});
Hooks.on('pauseGame',()=>{
if (enableModule == false || ready == false) return;
otherControls.updateAll();
});
Hooks.on('renderSidebarTab',()=>{
if (enableModule == false || ready == false) return;
if (otherControls != undefined) otherControls.updateAll();
if (sceneControl != undefined) sceneControl.updateAll();
});
Hooks.on('updateScene',()=>{
if (enableModule == false || ready == false) return;
sceneControl.updateAll();
externalModules.updateAll();
otherControls.updateAll();
});
Hooks.on('renderSceneControls',()=>{
if (enableModule == false || ready == false || otherControls == undefined) return;
otherControls.updateAll();
});
Hooks.on('targetToken',(user,token,targeted)=>{
if (enableModule == false || ready == false) return;
if (token.id == selectedTokenId) tokenControl.update(selectedTokenId);
});
Hooks.on('sidebarCollapse',()=>{
if (enableModule == false || ready == false) return;
otherControls.updateAll();
});
Hooks.on('renderCompendium',()=>{
if (enableModule == false || ready == false) return;
otherControls.updateAll();
});
Hooks.on('closeCompendium',()=>{
if (enableModule == false || ready == false) return;
otherControls.updateAll();
});
Hooks.on('renderJournalSheet',()=>{
if (enableModule == false || ready == false) return;
otherControls.updateAll();
});
Hooks.on('closeJournalSheet',()=>{
if (enableModule == false || ready == false) return;
otherControls.updateAll();
});
Hooks.on('gmScreenOpenClose',(html,isOpen)=>{
if (enableModule == false || ready == false) return;
externalModules.updateAll({gmScreen:isOpen});
});
Hooks.once('init', ()=>{
//CONFIG.debug.hooks = true;
registerSettings(); //in ./src/settings.js
});
Hooks.once('canvasReady',()=>{
ready = true;
});