-
Notifications
You must be signed in to change notification settings - Fork 62
/
TokenMenu.js
4513 lines (3948 loc) · 165 KB
/
TokenMenu.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// deprecated. replaced by mytokens
tokendata={
folders:{},
};
// deprecated, but still needed for migrate_to_my_tokens() to work
function convert_path(path){
let pieces=path.split("/");
let current=tokendata;
for(let i=0;i<pieces.length;i++){
if(!current || pieces[i]=="")
continue;
current=current.folders[pieces[i]];
}
return current || {};
}
// deprecated, but still needed for migrate_to_my_tokens() to work
function persist_customtokens(){
console.warn("persist_customtokens no longer supported");
// delete tokendata.folders["AboveVTT BUILTIN"];
// localStorage.setItem("CustomTokens",JSON.stringify(tokendata));
// delete tokendata.folders["AboveVTT BUILTIN"];
}
function context_menu_flyout(id, hoverEvent, buildFunction) {
let contextMenu = $("#tokenOptionsPopup");
if (contextMenu.length === 0) {
console.warn("context_menu_flyout, but #tokenOptionsPopup could not be found");
return;
}
if (hoverEvent.type === "mouseenter") {
let flyout = $(`<div id='${id}' class='context-menu-flyout'></div>`);
$(`.context-menu-flyout`).remove(); // never duplicate
buildFunction(flyout);
$("#tokenOptionsContainer").append(flyout);
observe_hover_text(flyout);
let contextMenuCenter = (contextMenu.height() / 2);
let flyoutHeight = flyout.height();
let diff = (contextMenu.height() - flyoutHeight);
let flyoutTop = contextMenuCenter - (flyoutHeight / 2); // center alongside the contextmenu
if (diff > 0) {
// the flyout is smaller than the contextmenu. Make sure it's alongside the hovered row
// align to the top of the row. 14 is half the height of the button
let buttonPosition = $(hoverEvent.currentTarget).closest('.flyout-from-menu-item')[0].getBoundingClientRect().y - $("#tokenOptionsPopup")[0].getBoundingClientRect().y + 14
if(buttonPosition < contextMenuCenter) {
flyoutTop = buttonPosition - (flyoutHeight / 5)
}
else{
flyoutTop = buttonPosition - (flyoutHeight / 2)
}
}
flyout.css({
left: contextMenu.width(),
top: flyoutTop,
});
if ($(".context-menu-flyout")[0].getBoundingClientRect().top < 0) {
flyout.css("top", 0)
}
else if($(".context-menu-flyout")[0].getBoundingClientRect().bottom > window.innerHeight-15) {
flyout.css({
top: 'unset',
bottom: 0
});
}
}
}
function close_token_context_menu() {
$("#tokenOptionsClickCloseDiv").click();
}
/**
* Opens a sidebar modal with token configuration options
* @param tokenIds {Array<String>} an array of ids for the tokens being configured
*/
function token_context_menu_expanded(tokenIds, e) {
if (tokenIds === undefined || tokenIds.length === 0) {
console.warn(`token_context_menu_expanded was called without any token ids`);
return;
}
let tokens = tokenIds.map(id => window.TOKEN_OBJECTS[id]).filter(t => t !== undefined)
let door = (tokenIds.length == 1) ? $(`[data-id='${tokenIds}'].door-button`) : undefined;
if (tokens.length === 0 && door.length == 0) {
console.warn(`token_context_menu_expanded was called with ids: ${JSON.stringify(tokenIds)}, but no matching tokens could be found`);
return;
}
if(door?.length > 0 && !window.DM){
return;
}
$("#tokenOptionsPopup").remove();
let tokenOptionsClickCloseDiv = $("<div id='tokenOptionsClickCloseDiv'></div>");
tokenOptionsClickCloseDiv.off().on("click", function(){
$("#tokenOptionsPopup").remove();
$('.context-menu-list').trigger('contextmenu:hide')
tokenOptionsClickCloseDiv.remove();
$("#tokenOptionsContainer .sp-container").spectrum("destroy");
$("#tokenOptionsContainer .sp-container").remove();
$(`.context-menu-flyout`).remove();
});
let moveableTokenOptions = $("<div id='tokenOptionsPopup'></div>");
let body = $("<div id='tokenOptionsContainer'></div>");
moveableTokenOptions.append(body);
$('body').append(moveableTokenOptions);
$('body').append(tokenOptionsClickCloseDiv);
if(door?.length == 1){
if(window.DM) {
if(window.TOKEN_OBJECTS[tokenIds] == undefined){
let options = {
...default_options(),
left: `${parseFloat(door.css('--mid-x')) - 25}px`,
top: `${parseFloat(door.css('--mid-y')) - 25}px`,
id: tokenIds[0].replaceAll('.', ''),
vision:{
feet: 0,
color: `rgba(0, 0, 0, 0)`
},
imgsrc: `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=`,
type: 'door',
size: 50,
scaleCreated: window.CURRENT_SCENE_DATA.scale_factor
};
window.ScenesHandler.create_update_token(options)
}
let openButton = $(`<button class=" context-menu-icon-hidden door-open material-icons">Open/Close</button>`)
openButton.off().on("click", function(clickEvent){
let clickedItem = $(this);
let locked = door.hasClass('locked');
let secret = door.hasClass('secret');
const type = isDoor ? (secret ? (locked ? 5 : 4) : (locked ? 2 : 0)) : (secret ? (locked ? 7 : 6) : (locked ? 3 : 1))
let doors = window.DRAWINGS.filter(d => (d[1] == "wall" && doorColorsArray.includes(d[2]) && parseInt(d[3]) == x1 && parseInt(d[4]) == y1 && parseInt(d[5]) == x2 && parseInt(d[6]) == y2))
let opened = (/rgba.*0\.5\)/g).test(doors[0][2]) ? true : false;
isOpen = opened ? 'closed' : 'open';
door.toggleClass('open', !opened);
window.DRAWINGS = window.DRAWINGS.filter(d => d != doors[0]);
let data = ['line',
'wall',
doorColors[type][isOpen],
x1,
y1,
x2,
y2,
12,
doors[0][8],
doors[0][9]
];
window.DRAWINGS.push(data);
window.wallUndo.push({
undo: [data],
redo: [doors[0]]
})
redraw_light_walls();
redraw_light();
redraw_drawn_light();
sync_drawings();
if(window.TOKEN_OBJECTS[`${x1}${y1}${x2}${y2}${window.CURRENT_SCENE_DATA.id}`.replaceAll('.','')] != undefined){
window.TOKEN_OBJECTS[`${x1}${y1}${x2}${y2}${window.CURRENT_SCENE_DATA.id}`.replaceAll('.','')].place_sync_persist();
}
});
body.append(openButton);
let notesRow = $(`<div class="token-image-modal-footer-select-wrapper flyout-from-menu-item"><div class="token-image-modal-footer-title">Note</div></div>`);
notesRow.hover(function (hoverEvent) {
context_menu_flyout("notes-flyout", hoverEvent, function(flyout) {
flyout.append(build_notes_flyout_menu(tokenIds));
})
});
body.append(notesRow);
let lightRow = $(`<div class="token-image-modal-footer-select-wrapper flyout-from-menu-item"><div class="token-image-modal-footer-title">Token Vision/Light</div></div>`);
lightRow.hover(function (hoverEvent) {
context_menu_flyout("light-flyout", hoverEvent, function(flyout) {
flyout.append(build_token_light_inputs(tokenIds, true));
})
});
if(window.CURRENT_SCENE_DATA.disableSceneVision != true && window.DM){
body.append(lightRow);
}
let x1 = parseInt(door.attr('data-x1'));
let x2 = parseInt(door.attr('data-x2'));
let y1 = parseInt(door.attr('data-y1'));
let y2 = parseInt(door.attr('data-y2'));
let locked = door.hasClass('locked');
let secret = door.hasClass('secret');
let isDoor = door.children('.door').length>0;
let doors = window.DRAWINGS.filter(d => (d[1] == "wall" && doorColorsArray.includes(d[2]) && parseInt(d[3]) == x1 && parseInt(d[4]) == y1 && parseInt(d[5]) == x2 && parseInt(d[6]) == y2))
let color = doors[0][2];
let isOpen = (/rgba.*0\.5\)/g).test(color) ? 'open' : 'closed';
body.append($('<div class="token-image-modal-footer-title" style="margin-top:10px">Door Type</div>'));
let lockedButton = $(`<button class="${door.hasClass('locked') ? 'single-active active-condition' : 'none-active'} context-menu-icon-hidden door-lock material-icons">Locked</button>`)
lockedButton.off().on("click", function(clickEvent){
let clickedItem = $(this);
let locked = door.hasClass('locked');
let secret = door.hasClass('secret');
const type = isDoor ? (secret ? (!locked ? 5 : 4) : (!locked ? 2 : 0)) : (secret ? (!locked ? 7 : 6) : (!locked ? 3 : 1))
door.toggleClass('locked', !locked);
let doors = window.DRAWINGS.filter(d => (d[1] == "wall" && doorColorsArray.includes(d[2]) && parseInt(d[3]) == x1 && parseInt(d[4]) == y1 && parseInt(d[5]) == x2 && parseInt(d[6]) == y2))
window.DRAWINGS = window.DRAWINGS.filter(d => d != doors[0]);
let data = ['line',
'wall',
doorColors[type][isOpen],
x1,
y1,
x2,
y2,
12,
doors[0][8],
doors[0][9]
];
window.DRAWINGS.push(data);
window.wallUndo.push({
undo: [data],
redo: [doors[0]]
})
redraw_light_walls();
redraw_light();
sync_drawings();
clickedItem.removeClass("single-active all-active some-active active-condition");
clickedItem.addClass(`${!locked ? 'single-active active-condition' : ''}`);
});
body.append(lockedButton);
let secretButton = $(`<button class="${door.hasClass('secret') ? 'single-active active-condition' : 'none-active'} context-menu-icon-hidden door-secret material-icons">Secret</button>`)
secretButton.off().on("click", function(clickEvent){
let clickedItem = $(this);
let locked = door.hasClass('locked');
let secret = door.hasClass('secret');
const type = isDoor ? (!secret ? (locked ? 5 : 4) : (locked ? 2 : 0)) : (locked ? (!secret ? 7 : 3) : (!secret ? 6 : 1))
isOpen = locked ? 'closed' : isOpen;
door.toggleClass('secret', !secret);
let doors = window.DRAWINGS.filter(d => (d[1] == "wall" && doorColorsArray.includes(d[2]) && parseInt(d[3]) == x1 && parseInt(d[4]) == y1 && parseInt(d[5]) == x2 && parseInt(d[6]) == y2))
window.DRAWINGS = window.DRAWINGS.filter(d => d != doors[0]);
let data = ['line',
'wall',
doorColors[type][isOpen],
x1,
y1,
x2,
y2,
12,
doors[0][8],
doors[0][9]
];
window.DRAWINGS.push(data);
window.wallUndo.push({
undo: [data],
redo: [doors[0]]
})
redraw_light_walls();
redraw_light();
sync_drawings();
clickedItem.removeClass("single-active all-active some-active active-condition");
clickedItem.addClass(`${!secret ? 'single-active active-condition' : ''}`);
});
body.append(secretButton);
let hideButton = $(`<button class="${door.attr('data-hidden') == 'true' ? 'single-active active-condition' : 'none-active'} context-menu-icon-hidden door-hidden material-icons">Hide Icon-Show Walls to View</button>`)
hideButton.off().on("click", function(clickEvent){
let clickedItem = $(this);
let hidden = door.attr('data-hidden') == 'true';
let doors = window.DRAWINGS.filter(d => (d[1] == "wall" && doorColorsArray.includes(d[2]) && parseInt(d[3]) == x1 && parseInt(d[4]) == y1 && parseInt(d[5]) == x2 && parseInt(d[6]) == y2))
door.attr('data-hidden', !hidden);
window.DRAWINGS = window.DRAWINGS.filter(d => d != doors[0]);
let data = ['line',
'wall',
doors[0][2],
x1,
y1,
x2,
y2,
12,
doors[0][8],
!hidden
];
window.DRAWINGS.push(data);
window.wallUndo.push({
undo: [data],
redo: [doors[0]]
})
redraw_light_walls();
redraw_light();
sync_drawings();
clickedItem.removeClass("single-active all-active some-active active-condition");
clickedItem.addClass(`${!hidden ? 'single-active active-condition' : ''}`);
});
body.append(hideButton);
}
$("#tokenOptionsPopup").addClass("moveableWindow");
$("#tokenOptionsPopup").draggable({
addClasses: false,
scroll: false,
start: function () {
$("#resizeDragMon").append($('<div class="iframeResizeCover"></div>'));
$("#sheet").append($('<div class="iframeResizeCover"></div>'));
},
stop: function () {
$('.iframeResizeCover').remove();
}
});
if(e.touches?.length>0){
moveableTokenOptions.css("left", Math.max(e.touches[0].clientX - 230, 0) + 'px');
if($(moveableTokenOptions).height() + e.touches[0].clientY > window.innerHeight - 20) {
moveableTokenOptions.css("top", (window.innerHeight - $(moveableTokenOptions).height() - 20 + 'px'));
}
else {
moveableTokenOptions.css("top", e.touches[0].clientY - 10 + 'px');
}
$(moveableTokenOptions).toggleClass('touch', true);
}
else{
moveableTokenOptions.css("left", Math.max(e.clientX - 230, 0) + 'px');
if($(moveableTokenOptions).height() + e.clientY > window.innerHeight - 20) {
moveableTokenOptions.css("top", (window.innerHeight - $(moveableTokenOptions).height() - 20 + 'px'));
}
else {
moveableTokenOptions.css("top", e.clientY - 10 + 'px');
}
$(moveableTokenOptions).toggleClass('touch', false);
}
return;
}
let audioToken = (tokenIds.length == 1 && window.TOKEN_OBJECTS[tokenIds]?.options?.audioChannel) ? $(`[data-id='${tokenIds}']`) : undefined;
// Aoe tokens are treated differently from everything else so we need to check this more often
let isAoeList = tokens.map(t => t.isAoe());
let uniqueAoeList = [...new Set(isAoeList)];
const allTokensAreAoe = (uniqueAoeList.length === 1 && uniqueAoeList[0] === true);
const someTokensAreAoe = (uniqueAoeList.includes(true));
if(audioToken != undefined){
if(!window.DM)
return;
if (tokens.length > 1 || (tokens.length == 1 && tokens[0].options.groupId != undefined)) {
let addButtonInternals = `Group Tokens<span class="material-icons add-link"></span>`;
let removeButtonInternals = `Remove From Group<span class="material-icons link-off"></span>`;
let groupTokens = $(`<button class='${determine_grouped_classname(tokenIds)} context-menu-icon-grouped material-icons'></button>`);
if (groupTokens.hasClass('single-active')) {
// they are all in a group. Make it a remove button
groupTokens.addClass("remove-from-group");
groupTokens.html(removeButtonInternals);
} else {
// if any are not in the combat tracker, make it an add button.
groupTokens.addClass("add-to-group");
groupTokens.html(addButtonInternals);
}
groupTokens.off().on("click", function(clickEvent){
let clickedItem = $(this);
let groupAll = clickedItem.hasClass("some-active");
let group = uuid();
tokens.forEach(token => {
if (groupAll || clickedItem.hasClass('add-to-group')) {
token.options.groupId = group;
} else {
token.options.groupId = undefined;
}
token.place_sync_persist();
});
clickedItem.removeClass("single-active all-active some-active active-condition");
clickedItem.addClass(determine_grouped_classname(tokenIds));
});
body.append(groupTokens);
}
let toTopMenuButton = $("<button class='material-icons to-top'>Move to Top</button>");
let toBottomMenuButton = $("<button class='material-icons to-bottom'>Move to Bottom</button>")
body.append(toTopMenuButton);
body.append(toBottomMenuButton);
toTopMenuButton.off().on("click", function(tokenIds){
tokens.forEach(token => {
$(".token").each(function(){
let tokenId = $(this).attr('data-id');
let tokenzindexdiff = window.TOKEN_OBJECTS[tokenId].options.zindexdiff;
if (tokenzindexdiff >= window.TOKEN_OBJECTS[token.options.id].options.zindexdiff && tokenId != token.options.id) {
window.TOKEN_OBJECTS[token.options.id].options.zindexdiff = tokenzindexdiff + 1;
}
});
token.place_sync_persist();
});
});
toBottomMenuButton.off().on("click", function(tokenIds){
tokens.forEach(token => {
$(".token").each(function(){
let tokenId = $(this).attr('data-id');
let tokenzindexdiff = window.TOKEN_OBJECTS[tokenId].options.zindexdiff;
if (tokenzindexdiff <= window.TOKEN_OBJECTS[token.options.id].options.zindexdiff && tokenId != token.options.id) {
window.TOKEN_OBJECTS[token.options.id].options.zindexdiff = Math.max(tokenzindexdiff - 1, -5000);
}
});
token.place_sync_persist();
});
});
let lockSettings = token_setting_options().filter((d) => d.name == 'lockRestrictDrop')[0];
let selectedTokenSettings = tokens.map(t => t.options.lockRestrictDrop);
let uniqueSettings = [...new Set(selectedTokenSettings)];
let currentValue = null; // passing null will set the switch as unknown; undefined is the same as false
if (uniqueSettings.length === 1) {
currentValue = uniqueSettings[0];
}
let lockDropdown = build_dropdown_input(lockSettings, currentValue, function(name, newValue) {
tokens.forEach(token => {
token.options[name] = newValue;
token.place_sync_persist();
});
});
let lockTitle = lockDropdown.find('.token-image-modal-footer-title')
lockTitle.empty();
lockTitle.toggleClass('material-icons door-lock', true);
lockTitle.toggleClass('token-image-modal-footer-title', false);
body.append(lockDropdown);
let hideText = tokenIds.length > 1 ? "Hide Tokens" : "Hide Token"
let hiddenMenuButton = $(`<button class="${determine_hidden_classname(tokenIds)} context-menu-icon-hidden icon-invisible material-icons">${hideText}</button>`)
hiddenMenuButton.off().on("click", function(clickEvent){
let clickedItem = $(this);
let hideAll = clickedItem.hasClass("some-active");
tokens.forEach(token => {
if (hideAll || token.options.hidden !== true) {
token.hide();
} else {
token.show();
}
});
clickedItem.removeClass("single-active all-active some-active active-condition");
clickedItem.addClass(determine_hidden_classname(tokenIds));
});
body.append(hiddenMenuButton);
let attenuateButton = $(`<button class="${window.TOKEN_OBJECTS[tokenIds].options.audioChannel.attenuate ? 'single-active active-condition' : 'none-active'} context-menu-icon-hidden spatial-audio-off material-icons">Distance based volume</button>`)
attenuateButton.off().on("click", function(clickEvent){
let clickedItem = $(this);
window.TOKEN_OBJECTS[tokenIds].options.audioChannel.attenuate = !window.TOKEN_OBJECTS[tokenIds].options.audioChannel.attenuate;
let classes = window.TOKEN_OBJECTS[tokenIds].options.audioChannel.attenuate ? 'single-active active-condition context-menu-icon-hidden spatial-audio-off material-icons' : 'none-active context-menu-icon-hidden spatial-audio-off material-icons';
$(this).attr('class', `${classes}`)
window.TOKEN_OBJECTS[tokenIds].place_sync_persist();
});
body.append(attenuateButton);
let wallsBlockedButton = $(`<button class="${window.TOKEN_OBJECTS[tokenIds].options.audioChannel.wallsBlocked ? 'single-active active-condition' : 'none-active'} context-menu-icon-hidden select-to-speak material-icons">Blocked by Walls</button>`)
wallsBlockedButton.off().on("click", function(clickEvent){
let clickedItem = $(this);
window.TOKEN_OBJECTS[tokenIds].options.audioChannel.wallsBlocked = !window.TOKEN_OBJECTS[tokenIds].options.audioChannel.wallsBlocked;
let classes = window.TOKEN_OBJECTS[tokenIds].options.audioChannel.wallsBlocked ? 'single-active active-condition context-menu-icon-hidden select-to-speak material-icons' : 'none-active context-menu-icon-hidden select-to-speak material-icons';
$(this).attr('class', `${classes}`)
window.TOKEN_OBJECTS[tokenIds].place_sync_persist();
});
body.append(wallsBlockedButton);
let upsq = window.CURRENT_SCENE_DATA.upsq;
if (upsq === undefined || upsq.length === 0) {
upsq = "ft";
}
let audioRangeInput = $(`
<div class="token-image-modal-footer-select-wrapper" style="display:flex">
<div class="token-image-modal-footer-title">Range in ${upsq}</div>
<input type="number" min="${window.CURRENT_SCENE_DATA.fpsq / 2}" step="${window.CURRENT_SCENE_DATA.fpsq /2}"
name="data-token-size-custom" value=${window.TOKEN_OBJECTS[tokenIds].options.audioChannel.range} style="width: 3rem;">
</div>
`)
audioRangeInput.find('input').off().on("keyup focusout", function(clickEvent){
let clickedItem = $(this);
window.TOKEN_OBJECTS[tokenIds].options.audioChannel.range = $(this).val();
window.TOKEN_OBJECTS[tokenIds].place_sync_persist();
});
body.append(audioRangeInput);
if (tokens.length === 1) {
let notesRow = $(`<div class="token-image-modal-footer-select-wrapper flyout-from-menu-item"><div class="token-image-modal-footer-title">Token Note</div></div>`);
notesRow.hover(function (hoverEvent) {
context_menu_flyout("notes-flyout", hoverEvent, function(flyout) {
flyout.append(build_notes_flyout_menu(tokenIds));
})
});
body.append(notesRow);
}
let optionsRow = $(`<div class="token-image-modal-footer-select-wrapper flyout-from-menu-item"><div class="token-image-modal-footer-title">Token Options</div></div>`);
optionsRow.hover(function (hoverEvent) {
context_menu_flyout("options-flyout", hoverEvent, function(flyout) {
flyout.append(build_options_flyout_menu(tokenIds));
update_token_base_visibility(flyout);
});
});
body.append(optionsRow);
if(window.DM) {
body.append(`<hr style="opacity: 0.3" />`);
let deleteTokenMenuButton = $("<button class='deleteMenuButton icon-close-red material-icons'>Delete</button>")
body.append(deleteTokenMenuButton);
deleteTokenMenuButton.off().on("click", function(){
if(!$(e.target).hasClass("tokenselected")){
deselect_all_tokens();
}
tokens.forEach(token => {
token.selected = true;
});
delete_selected_tokens();
close_token_context_menu();
});
}
$("#tokenOptionsPopup").addClass("moveableWindow");
$("#tokenOptionsPopup").draggable({
addClasses: false,
scroll: false,
start: function () {
$("#resizeDragMon").append($('<div class="iframeResizeCover"></div>'));
$("#sheet").append($('<div class="iframeResizeCover"></div>'));
},
stop: function () {
$('.iframeResizeCover').remove();
}
});
if(e.touches?.length>0){
moveableTokenOptions.css("left", Math.max(e.touches[0].clientX - 230, 0) + 'px');
if($(moveableTokenOptions).height() + e.touches[0].clientY > window.innerHeight - 20) {
moveableTokenOptions.css("top", (window.innerHeight - $(moveableTokenOptions).height() - 20 + 'px'));
}
else {
moveableTokenOptions.css("top", e.touches[0].clientY - 10 + 'px');
}
$(moveableTokenOptions).toggleClass('touch', true);
}
else{
moveableTokenOptions.css("left", Math.max(e.clientX - 230, 0) + 'px');
if($(moveableTokenOptions).height() + e.clientY > window.innerHeight - 20) {
moveableTokenOptions.css("top", (window.innerHeight - $(moveableTokenOptions).height() - 20 + 'px'));
}
else {
moveableTokenOptions.css("top", e.clientY - 10 + 'px');
}
$(moveableTokenOptions).toggleClass('touch', false);
}
return;
}
// stat block / character sheet
if (tokens.length === 1) {
let token = tokens[0];
if (token.isPlayer() && !token.options.id.includes(window.PLAYER_ID)) {
let button = $(`<button>Open Character Sheet<span class="material-icons icon-view"></span></button>`);
button.on("click", function() {
open_player_sheet(token.options.id);
close_token_context_menu();
});
body.append(button);
}
else if(token.options.statBlock){
let button =$('<button>Open Monster Stat Block<span class="material-icons icon-view"></span></button>');
button.click(function(){
let customStatBlock = window.JOURNAL.notes[token.options.statBlock].text;
let pcURL = $(customStatBlock).find('.custom-pc-sheet.custom-stat').text();
if(pcURL){
open_player_sheet(pcURL);
}else{
load_monster_stat(undefined, token.options.id, customStatBlock)
}
close_token_context_menu();
});
if(token.options.player_owned || window.DM){
body.append(button);
}
}
else if (token.isMonster()) {
let button = $(`<button>Open Monster Stat Block<span class="material-icons icon-view"></span></button>`);
button.on("click", function() {
load_monster_stat(token.options.monster, token.options.id);
close_token_context_menu();
});
if(token.options.player_owned || window.DM){
body.append(button);
}
}
}
if (window.DM && !allTokensAreAoe) {
let addButtonInternals = `Add to Combat Tracker<span class="material-icons icon-person-add"></span>`;
let removeButtonInternals = `Remove From Combat Tracker<span class="material-icons icon-person-remove"></span>`;
let addGroupButtonInternals = `Add to Combat as Group<span class="material-symbols-outlined group_add"></span>`;
let removeGroupButtonInternals = `Remove Group from Combat<span class="material-symbols-outlined group_remove"></span>`;
let combatButton = $(`<button></button>`);
let groupCombatButton =$(`<button></button>`)
let inCombatStatuses = [...new Set(tokens.map(t => t.isInCombatTracker()))];
let inCombatGroupStatuses = [...new Set(tokens.map(t => t.options.combatGroup != undefined))];
if (inCombatStatuses.length === 1 && inCombatStatuses[0] === true) {
// they are all in the combat tracker. Make it a remove button
combatButton.addClass("remove-from-ct");
combatButton.html(removeButtonInternals);
} else {
// if any are not in the combat tracker, make it an add button.
combatButton.addClass("add-to-ct");
combatButton.html(addButtonInternals);
}
if (inCombatGroupStatuses.length === 1 && inCombatGroupStatuses[0] === true) {
// they are all in the combat tracker. Make it a remove button
groupCombatButton.addClass("remove-from-ct");
groupCombatButton.html(removeGroupButtonInternals);
} else {
// if any are not in the combat tracker, make it an add button.
groupCombatButton.addClass("add-to-ct");
groupCombatButton.html(addGroupButtonInternals);
}
let shiftClick = jQuery.Event("click");
shiftClick.shiftKey = true;
let ctrlClick = jQuery.Event("click");
ctrlClick.ctrlKey = true;
let roll_adv = $('<button title="Advantage to roll" id="adv" name="roll_mod" value="OFF" class="roll_mods_button icon-advantage markers-icon" />')
roll_adv.click(function(e){
e.stopPropagation();
$(this).parent().trigger(shiftClick);
});
let roll_disadv = $('<button title="Disadvantage to roll" id="disadv" name="roll_mod" value="OFF" class="roll_mods_button icon-disadvantage markers-icon" />')
roll_disadv.click(function(e){
e.stopPropagation();
$(this).parent().trigger(ctrlClick);
});
combatButton.append(roll_adv, roll_disadv);
combatButton.on("click", function(clickEvent) {
let clickedButton = $(clickEvent.currentTarget);
if (clickedButton.hasClass("remove-from-ct")) {
clickedButton.removeClass("remove-from-ct").addClass("add-to-ct");
clickedButton.html(addButtonInternals);
clickedButton.append(roll_adv.clone(true, true), roll_disadv.clone(true, true));
clickedButton.find('#disadv').click(function(e){
e.stopPropagation();
$(this).parent().trigger(ctrlClick);
});
clickedButton.find('#adv').click(function(e){
e.stopPropagation();
$(this).parent().trigger(shiftClick);
});
const reset_init = getCombatTrackersettings().remove_init;
tokens.forEach(t =>{
t.options.ct_show = undefined;
t.options.combatGroup = undefined;
if(reset_init == true)
t.options.init = undefined;
ct_remove_token(t, false);
t.update_and_sync();
});
} else {
clickedButton.removeClass("add-to-ct").addClass("remove-from-ct");
clickedButton.html(removeButtonInternals);
const reset_init = getCombatTrackersettings().remove_init;
tokens.forEach(t => {
t.options.combatGroup = undefined;
if(reset_init == true)
t.options.init = undefined;
ct_add_token(t, false, undefined, clickEvent.shiftKey, clickEvent.ctrlKey)
t.update_and_sync();
});
}
debounceCombatReorder();
});
groupCombatButton.on("click", function(clickEvent) {
let clickedButton = $(clickEvent.currentTarget);
if (clickedButton.hasClass("remove-from-ct")) {
combatButton.removeClass("remove-from-ct").addClass("add-to-ct");
combatButton.html(addButtonInternals);
clickedButton.removeClass("remove-from-ct").addClass("add-to-ct");
clickedButton.html(addGroupButtonInternals);
clickedButton.append(roll_adv.clone(true, true), roll_disadv.clone(true, true));
clickedButton.find('#disadv').click(function(e){
e.stopPropagation();
$(this).parent().trigger(ctrlClick);
});
clickedButton.find('#adv').click(function(e){
e.stopPropagation();
$(this).parent().trigger(shiftClick);
});
const reset_init = getCombatTrackersettings().remove_init;
tokens.forEach(t =>{
if(t.options.combatGroup && window.TOKEN_OBJECTS[t.options.combatGroup]){
window.TOKEN_OBJECTS[t.options.combatGroup].delete()
}
if(reset_init == true)
t.options.init = undefined;
t.options.combatGroup = undefined;
t.options.ct_show = undefined;
ct_remove_token(t, false);
t.update_and_sync();
});
} else {
clickedButton.removeClass("add-to-ct").addClass("remove-from-ct");
clickedButton.html(removeGroupButtonInternals);
combatButton.removeClass("add-to-ct").addClass("remove-from-ct");
combatButton.html(removeButtonInternals);
let group = uuid();
let allHidden = true;
const reset_init = getCombatTrackersettings().remove_init;
tokens.forEach(t => {
if(t.options.combatGroup && window.TOKEN_OBJECTS[t.options.combatGroup]){
window.TOKEN_OBJECTS[t.options.combatGroup].delete()
}
if(t.options.hidden !== true){
allHidden = false
}
if(reset_init == true)
t.options.init = undefined;
t.options.combatGroup = group;
ct_add_token(t, false, undefined, clickEvent.shiftKey, clickEvent.ctrlKey);
t.update_and_sync();
});
let t = new Token({
...tokens[0].options,
id: group,
combatGroupToken: group,
ct_show: !allHidden,
name: `${tokens[0].options.name} Group`
});
window.TOKEN_OBJECTS[group] = t;
if(window.all_token_objects[group] == undefined){
window.all_token_objects[group] = t;
}
t.sync = mydebounce(function(e) { // VA IN FUNZIONE SOLO SE IL TOKEN NON ESISTE GIA
window.MB.sendMessage('custom/myVTT/token', t.options);
}, 10);
t.place_sync_persist();
ct_add_token(window.TOKEN_OBJECTS[group], false, clickEvent.shiftKey, clickEvent.ctrlKey)
}
debounceCombatReorder();
});
body.append(combatButton);
if(tokens.length >1){
groupCombatButton.append(roll_adv.clone(true,true), roll_disadv.clone(true,true));
body.append(groupCombatButton);
}
}
if(window.DM){
let hideText = tokenIds.length > 1 ? "Hide Tokens" : "Hide Token"
let hiddenMenuButton = $(`<button class="${determine_hidden_classname(tokenIds)} context-menu-icon-hidden icon-invisible material-icons">${hideText}</button>`)
hiddenMenuButton.off().on("click", function(clickEvent){
let clickedItem = $(this);
let hideAll = clickedItem.hasClass("some-active");
tokens.forEach(token => {
if (hideAll || token.options.hidden !== true) {
token.hide();
} else {
token.show();
}
});
clickedItem.removeClass("single-active all-active some-active active-condition");
clickedItem.addClass(determine_hidden_classname(tokenIds));
});
body.append(hiddenMenuButton);
let lockSettings = token_setting_options().filter((d) => d.name == 'lockRestrictDrop')[0];
let selectedTokenSettings = tokens.map(t => t.options.lockRestrictDrop);
let uniqueSettings = [...new Set(selectedTokenSettings)];
let currentValue = null; // passing null will set the switch as unknown; undefined is the same as false
if (uniqueSettings.length === 1) {
currentValue = uniqueSettings[0];
}
let lockDropdown = build_dropdown_input(lockSettings, currentValue, function(name, newValue) {
tokens.forEach(token => {
token.options[name] = newValue;
token.place_sync_persist();
});
});
let lockTitle = lockDropdown.find('.token-image-modal-footer-title')
lockTitle.empty();
lockTitle.toggleClass('material-icons door-lock', true);
lockTitle.toggleClass('token-image-modal-footer-title', false);
body.append(lockDropdown);
}
if (tokens.length > 1 || (tokens.length == 1 && tokens[0].options.groupId != undefined)) {
let addButtonInternals = `Group Tokens<span class="material-icons add-link"></span>`;
let removeButtonInternals = `Remove From Group<span class="material-icons link-off"></span>`;
let groupTokens = $(`<button class='${determine_grouped_classname(tokenIds)} context-menu-icon-grouped material-icons'></button>`);
if (groupTokens.hasClass('single-active')) {
// they are all in a group. Make it a remove button
groupTokens.addClass("remove-from-group");
groupTokens.html(removeButtonInternals);
} else {
// if any are not in the combat tracker, make it an add button.
groupTokens.addClass("add-to-group");
groupTokens.html(addButtonInternals);
}
groupTokens.off().on("click", function(clickEvent){
let clickedItem = $(this);
let groupAll = clickedItem.hasClass("some-active");
let group = uuid();
tokens.forEach(token => {
if (groupAll || clickedItem.hasClass('add-to-group')) {
token.options.groupId = group;
} else {
token.options.groupId = undefined;
}
token.place_sync_persist();
});
clickedItem.removeClass("single-active all-active some-active active-condition");
clickedItem.addClass(determine_grouped_classname(tokenIds));
});
body.append(groupTokens);
}
// Start Quick Group Roll
if (window.DM) {
let quickRollMenu = $("<button class='material-icons open-menu'>Add/Remove from Quick Rolls</button>")
body.append(quickRollMenu);
quickRollMenu.on("click", function(clickEvent){
if(!childWindows['Quick Roll Menu'])
$("#qrm_dialog").show()
if ($('#quick_roll_area').length == 0){
close_token_context_menu()
open_quick_roll_menu(e)
}
tokens.forEach(token => {
$(token).each(function(){
if (window.TOKEN_OBJECTS[token.options.id].in_qrm == true) {
remove_from_quick_roll_menu(token)
}
else {
add_to_quick_roll_menu(token)
}
})
})
if(childWindows['Quick Roll Menu']){
qrm_update_popout();
}
})
}
// End Quick Group Roll
let toTopMenuButton = $("<button class='material-icons to-top'>Move to Top</button>");
let toBottomMenuButton = $("<button class='material-icons to-bottom'>Move to Bottom</button>")
let sendToGamelogButton = $("<button class='material-icons send-to'>Send To Gamelog</button>")
body.append(toTopMenuButton);
body.append(toBottomMenuButton);
body.append(sendToGamelogButton);
toTopMenuButton.off().on("click", function(tokenIds){
tokens.forEach(token => {
$(".token").each(function(){
let tokenId = $(this).attr('data-id');
let tokenzindexdiff = window.TOKEN_OBJECTS[tokenId].options.zindexdiff;
if (tokenzindexdiff >= window.TOKEN_OBJECTS[token.options.id].options.zindexdiff && tokenId != token.options.id) {
window.TOKEN_OBJECTS[token.options.id].options.zindexdiff = tokenzindexdiff + 1;
}
});
token.place_sync_persist();
});
});
toBottomMenuButton.off().on("click", function(tokenIds){
tokens.forEach(token => {
$(".token").each(function(){
let tokenId = $(this).attr('data-id');
let tokenzindexdiff = window.TOKEN_OBJECTS[tokenId].options.zindexdiff;
if (tokenzindexdiff <= window.TOKEN_OBJECTS[token.options.id].options.zindexdiff && tokenId != token.options.id) {
window.TOKEN_OBJECTS[token.options.id].options.zindexdiff = Math.max(tokenzindexdiff - 1, -5000);
}
});
token.place_sync_persist();
});
});
sendToGamelogButton.off().on("click", function(tokenIds){
tokens.forEach(async token => {
function setImageAttr(tokenImage, token){
let largeAvatar = cached_monster_items[token.options.monster].monsterData.largeAvatarUrl;
let avatar = cached_monster_items[token.options.monster].monsterData.avatarUrl;
let basicAvatar = cached_monster_items[token.options.monster].monsterData.basicAvatarUrl;
tokenImage.find('img').attr('src', largeAvatar);
tokenImage.find('img').attr('data-large-avatar-url', largeAvatar);
tokenImage.find('img').attr('data-avatar-url', largeAvatar);
tokenImage.find('img').attr('data-basic-avatar-url', largeAvatar);
tokenImage.find('img').attr('data-current-avatar-url', "largeAvatarUrl");
}
let tokenImage = $(`<div class="image" style="display: block; max-width:100%;"><${(token.options.videoToken == true || ['.mp4', '.webm','.m4v'].some(d => token.options.imgsrc.includes(d))) ? 'video disableremoteplayback muted' : 'img'} class='magnify' style='max-width:100%;' href='${token.options.imgsrc}' src='${token.options.imgsrc}'/> </div>`);
if(typeof token.options.monster == 'number' && token.options.itemType == 'monster' && token.options.alternativeImages == undefined){
if(cached_monster_items[token.options.monster] != undefined){
setImageAttr(tokenImage, token);