forked from typeou/karasubonk
-
Notifications
You must be signed in to change notification settings - Fork 5
/
renderer.js
2799 lines (2375 loc) · 125 KB
/
renderer.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
const { ipcRenderer } = require("electron");
const fs = require("fs");
const version = 1.10;
// ------
// Status
// ------
var status = 0;
const statusTitle = [
"准备就绪!",
"等待连接直播间...",
"正在连接网页源...",
"校准中 (1/2)",
"校准中 (2/2)",
"正在连接VTube Studio...",
"校准",
"正在激活弹幕事件...",
"错误:端口被占用",
"警告:版本不匹配",
"警告:版本不匹配"
];
const statusDesc = [
"",
"<p>点击下方的“连接”按钮连接B站直播间。</p>",
`<p>如果这条提示没有消失,请在OBS中刷新浏览器源。</p><p>提示:请在OBS中添加该路径下的本地文件为浏览器源(点击文本进行复制): </p><p><input readonly id="bonkerInput"></p><p>(直播姬用户请直接粘贴在浏览器源的URL一栏)</p>`,
"<p>请将VTS模型的头部移动至OBS浏览器源的标志上。</p><p>提示:您有可能因为将VTS源置于浏览器源上方导致无法看到标志。</p><p>单击 <mark>下一步</mark> 进行下一步校准</p>",
"<p>请将VTS模型的头部移动至OBS浏览器源的标志上。</p><p>提示:您有可能因为将VTS源置于浏览器源上方导致无法看到标志。</p><p>单击 <mark>完成</mark> 完成校准</p>",
["<p>如果这条提示没有消失,请在OBS中刷新浏览器源。</p><p>如果刷新了浏览器源后仍然无效,请确认VTS中的API已经开启,并且端口为<mark>", "</mark>。</p>"],
"<p>这项设置用于校准扔出物体的目标方位。</p><p>请点击 \"开始校准\" 进行校准。</p>",
"",
["<p>端口:<mark>", "</mark> 被占用。</p><p>您可以尝试在 <mark>设置 -> 高级设置</mark> 中修改浏览器源的端口。</p><p>端口应在 1024 至 65535 的范围内。</p>"],
"<p>KBonk 与浏览器源的版本不一致。</p><p>请确认OBS或直播姬中使用的浏览器源文件路径如下(点击文本进行复制):</p><p><input readonly id='bonkerInput'></p><p>如果确认路径无误,请确认OBS中的源已经刷新。</p>",
"<p>浏览器源没有返回版本号。</p><p>KBonk与浏览器源可能版本不一致。</p><p>请确认OBS或直播姬中使用的浏览器源文件路径如下(点击文本进行复制):</p><p><input readonly id='bonkerInput'></p><p>如果确认路径无误,请确认OBS中的源已经刷新。</p>"
];
// consolelog
ipcRenderer.on("consoleLog", (_, ...args) => {
console.log(...args);
})
// 连接房间状态改变
ipcRenderer.on("connectStatus", (_, status) => {
setRoomInputStatus(status);
})
ipcRenderer.on("connected", () => {
document.querySelector("#logout").innerText = "断开连接";
});
// bilibili 连接失败
ipcRenderer.on("biliConnectFailed", () => {
showToast("B站直播间连接失败,请重试", ToastType.error);
})
// 房间号留空报错
ipcRenderer.on("roomidEmptyError", () => {
showToast("请输入正确的房间号", ToastType.error);
setRoomInputStatus(RoomInputStatus.disconnected);
})
// 获取表情回调
ipcRenderer.on("gettingExpression", () => {
setExpressionDetailStatus(expressionDetailStatus.loading);
})
var userDataPath = null;
ipcRenderer.on("userDataPath", (event, message) => {
userDataPath = message;
})
ipcRenderer.send("getUserDataPath");
// 设置app底部元素的disable
function setRoomidFormDisabled({ input, button }) {
document.querySelector("#roomid").disabled = input;
document.querySelector("#logout").disabled = button;
document.querySelector("#logout").classList[button ? "add" : "remove"]("disabled");
}
// 连接浏览器源步骤时,路径文本框的复制功能
async function selectAndCopyBonkerPath() {
this.select();
await navigator.clipboard.writeText(this.value);
showToast("已成功复制路径", ToastType.success);
}
// 仅允许输入数字
document.querySelector("#roomid").oninput = function () {
this.value = this.value.replace(/\D/g, '');
}
// 点击连接房间
document.querySelector("#logout").addEventListener("click", async () => {
let roomid = document.querySelector("#roomid").value;
ipcRenderer.send("connect", roomid);
});
document.querySelector("#githubLink a").addEventListener("click", () => { ipcRenderer.send("link"); });
document.querySelector("#originalItchLink").addEventListener("click", () => {
ipcRenderer.send("originalItchLink");
})
document.querySelector("#originalAuthorLink").addEventListener("click", () => {
ipcRenderer.send("originalAuthorLink");
})
document.querySelector("#creditGithubLink").addEventListener("click", () => {
ipcRenderer.send("creditGithubLink");
})
document.querySelector("#bilibiliLink").addEventListener("click", () => {
ipcRenderer.send("bilibiliLink");
})
// 房间连接状态
const RoomInputStatus = {
disconnected: 0,
connecting: 1,
connected: 2
}
// 房间连接部分的状设置
function setRoomInputStatus(status) {
switch (status) {
case RoomInputStatus.disconnected: {
document.querySelector("#logout").innerText = "连接";
setRoomidFormDisabled({ button: undefined, input: undefined });
break;
}
case RoomInputStatus.connecting: {
document.querySelector("#logout").innerText = "连接";
setRoomidFormDisabled({ button: true, input: true });
break;
}
case RoomInputStatus.connected: {
document.querySelector("#logout").innerText = "断开连接";
setRoomidFormDisabled({ button: undefined, input: true });
break;
}
}
}
ipcRenderer.on("status", (event, message) => { setStatus(event, message); });
async function setStatus(_, message) {
if (status == message) return;
if (status == 2 || status == 9 || status == 10) {
document.querySelector("#bonkerInput").removeEventListener("click", selectAndCopyBonkerPath);
}
status = message;
document.querySelector("#status").innerHTML = statusTitle[status];
document.querySelector("#headerStatusInner").innerHTML = statusTitle[status] + (status != 0 ? " (单击查看详情)" : "");
if (status == 0) {
document.querySelector("#headerStatus").classList.remove("errorText");
document.querySelector("#headerStatus").classList.remove("workingText");
document.querySelector("#headerStatus").classList.add("readyText");
}
else if (status == 8 || status == 9 || status == 10) {
document.querySelector("#headerStatus").classList.add("errorText");
document.querySelector("#headerStatus").classList.remove("workingText");
document.querySelector("#headerStatus").classList.remove("readyText");
}
else {
document.querySelector("#headerStatus").classList.remove("errorText");
document.querySelector("#headerStatus").classList.add("workingText");
document.querySelector("#headerStatus").classList.remove("readyText");
}
if (status == 5)
document.querySelector("#statusDesc").innerHTML = statusDesc[status][0] + await getData("portVTubeStudio") + statusDesc[status][1];
else if (status == 8)
document.querySelector("#statusDesc").innerHTML = statusDesc[status][0] + await getData("portThrower") + statusDesc[status][1];
else
document.querySelector("#statusDesc").innerHTML = statusDesc[status];
if (status == 2 || status == 9 || status == 10) {
let bonkerPathInput = document.querySelector("#bonkerInput");
bonkerPathInput.value = __dirname + '\\bonker.html';
bonkerPathInput.addEventListener("click", selectAndCopyBonkerPath);
}
if (status == 3 || status == 4 || status == 6) {
if (status == 6)
document.querySelector("#nextCalibrate").querySelector(".innerTopButton").innerText = "开始校准";
else if (status == 3)
document.querySelector("#nextCalibrate").querySelector(".innerTopButton").innerText = "下一步";
else if (status == 4)
document.querySelector("#nextCalibrate").querySelector(".innerTopButton").innerText = "完成";
document.querySelector("#calibrateButtons").classList.remove("hidden");
}
else
document.querySelector("#calibrateButtons").classList.add("hidden");
// 表情设置判断
if (status == 0 || status == 3 || status == 4 || status == 6) {
document.querySelector("#hitExpressionDetail .loadingMask").classList.remove("vtsDisconnect");
} else {
document.querySelector("#hitExpressionDetail .loadingMask").classList.add("vtsDisconnect");
}
}
// ---------
// Libraries
// ---------
// Adding a new image to the list
document.querySelector("#newImage").addEventListener("click", () => { document.querySelector("#loadImage").click(); });
document.querySelector("#loadImage").addEventListener("change", loadImage);
// 替换图片回调
document.querySelector("#replaceImage").addEventListener("change", replaceImage);
async function loadImage() {
var throws = await getData("throws");
var files = document.querySelector("#loadImage").files;
for (var i = 0; i < files.length; i++) {
// Grab the image that was just loaded
var imageFile = files[i];
// If the folder for objects doesn't exist for some reason, make it
if (!fs.existsSync(userDataPath + "/throws/"))
fs.mkdirSync(userDataPath + "/throws/");
// Ensure that we're not overwriting any existing files with the same name
// If a file already exists, add an interating number to the end until it"s a unique filename
var append = "";
if (imageFile.path != userDataPath + "\\throws\\" + imageFile.name)
while (fs.existsSync(userDataPath + "/throws/" + imageFile.name.substr(0, imageFile.name.lastIndexOf(".")) + append + imageFile.name.substr(imageFile.name.lastIndexOf("."))))
append = append == "" ? 2 : (append + 1);
var filename = imageFile.name.substr(0, imageFile.name.lastIndexOf(".")) + append + imageFile.name.substr(imageFile.name.lastIndexOf("."));
// Make a copy of the file into the local folder
fs.copyFileSync(imageFile.path, userDataPath + "/throws/" + filename);
// Add the new image, update the data, and refresh the images page
throws.unshift({
"enabled": true,
"location": "throws/" + filename,
"weight": 1.0,
"scale": 1.0,
"sound": null,
"volume": 1.0,
"customs": []
});
}
setData("throws", throws);
openImages();
copyFilesToDirectory();
// Reset the image upload
document.querySelector("#loadImage").value = null;
}
// 替换图片回调
async function replaceImage() {
var throws = await getData("throws");
var files = document.querySelector("#replaceImage").files;
var imageFile = files[0];
var location = throws[currentImageIndex].location;
// 删除原图片
fs.unlinkSync(userDataPath + "/" + location);
// 将新图片复制并重命名
var filename = location.substr(location.lastIndexOf("/") + 1, location.lastIndexOf(".") - (location.lastIndexOf("/") + 1)) + imageFile.name.substr(imageFile.name.lastIndexOf("."));
fs.copyFileSync(imageFile.path, userDataPath + "/throws/" + filename);
throws[currentImageIndex].location = "throws/" + filename;
setData("throws", throws);
openImages();
copyFilesToDirectory();
// Reset the image replace
document.querySelector("#replaceImage").value = null;
}
document.querySelector("#imageTable").querySelector(".selectAll input").addEventListener("change", async () => {
document.querySelector("#imageTable").querySelectorAll(".imageEnabled").forEach((element) => {
element.checked = document.querySelector("#imageTable").querySelector(".selectAll input").checked;
});
var throws = await getData("throws");
for (var i = 0; i < throws.length; i++)
throws[i].enabled = document.querySelector("#imageTable").querySelector(".selectAll input").checked;
setData("throws", throws);
});
async function openImages() {
var throws = await getData("throws");
document.querySelector("#imageTable").querySelectorAll(".imageRow").forEach((element) => { element.remove(); });
var allEnabled = true;
for (var i = 0; i < throws.length; i++) {
if (!throws[i].enabled) {
allEnabled = false;
break;
}
}
document.querySelector("#imageTable").querySelector(".selectAll input").checked = allEnabled;
if (throws == null)
setData("throws", []);
else {
throws.forEach((_, index) => {
if (fs.existsSync(userDataPath + "/" + throws[index].location)) {
var row = document.querySelector("#imageRow").cloneNode(true);
row.removeAttribute("id");
row.classList.add("imageRow");
row.removeAttribute("hidden");
document.querySelector("#imageTable").appendChild(row);
row.querySelector(".imageLabel").innerText = throws[index].location.substr(throws[index].location.lastIndexOf('/') + 1);
row.querySelector(".imageImage").src = userDataPath + "/" + throws[index].location;
row.querySelector(".imageEnabled").checked = throws[index].enabled;
row.querySelector(".imageEnabled").addEventListener("change", () => {
throws[index].enabled = row.querySelector(".imageEnabled").checked;
setData("throws", throws);
var allEnabled = true;
for (var i = 0; i < throws.length; i++) {
if (!throws[i].enabled) {
allEnabled = false;
break;
}
}
document.querySelector("#imageTable").querySelector(".selectAll input").checked = allEnabled;
});
row.querySelector(".imageDetails").addEventListener("click", () => {
currentImageIndex = index;
openImageDetails();
showPanel("imageDetails", true);
});
row.querySelector(".imageReplace").addEventListener("click", () => {
currentImageIndex = index;
document.querySelector("#replaceImage").click();
})
row.querySelector(".imageRemove").addEventListener("click", () => {
// 删除后删除文件
fs.unlinkSync(userDataPath + "/" + throws[index].location);
fs.unlinkSync(__dirname + "/" + throws[index].location);
throws.splice(index, 1);
setData("throws", throws);
openImages();
});
}
else {
throws.splice(index, 1);
setData("throws", throws);
}
});
}
}
async function loadImageCustom(customName) {
var throws = await getData("throws");
var files = document.querySelector("#loadImageCustom").files;
for (var i = 0; i < files.length; i++) {
// Grab the image that was just loaded
var imageFile = files[i];
// If the folder for objects doesn't exist for some reason, make it
if (!fs.existsSync(userDataPath + "/throws/"))
fs.mkdirSync(userDataPath + "/throws/");
// Ensure that we're not overwriting any existing files with the same name
// If a file already exists, add an interating number to the end until it"s a unique filename
var append = "";
if (imageFile.path != userDataPath + "\\throws\\" + imageFile.name)
while (fs.existsSync(userDataPath + "/throws/" + imageFile.name.substr(0, imageFile.name.lastIndexOf(".")) + append + imageFile.name.substr(imageFile.name.lastIndexOf("."))))
append = append == "" ? 2 : (append + 1);
var filename = imageFile.name.substr(0, imageFile.name.lastIndexOf(".")) + append + imageFile.name.substr(imageFile.name.lastIndexOf("."));
// Make a copy of the file into the local folder
fs.copyFileSync(imageFile.path, userDataPath + "/throws/" + filename);
// Add the new image, update the data, and refresh the images page
throws.unshift({
"enabled": false,
"location": "throws/" + filename,
"weight": 1.0,
"scale": 1.0,
"sound": null,
"volume": 1.0,
"customs": [customName]
});
}
setData("throws", throws);
openImagesCustom(customName);
copyFilesToDirectory();
// Reset the image upload
document.querySelector("#loadImageCustom").value = null;
}
async function openImagesCustom(customName) {
// Refresh table to remove old event listeners
var oldTable = document.querySelector("#imageTableCustom");
var newTable = oldTable.cloneNode(true);
oldTable.after(newTable);
oldTable.remove();
document.querySelector("#newImageCustom").addEventListener("click", () => { document.querySelector("#loadImageCustom").click(); });
document.querySelector("#loadImageCustom").addEventListener("change", () => { loadImageCustom(customName); });
var throws = await getData("throws");
var allEnabled = true;
for (var i = 0; i < throws.length; i++) {
if (!throws[i].customs.includes(customName)) {
allEnabled = false;
break;
}
}
document.querySelector("#imageTableCustom").querySelector(".selectAll input").checked = allEnabled;
document.querySelector("#imageTableCustom").querySelector(".selectAll input").addEventListener("change", () => {
document.querySelector("#imageTableCustom").querySelectorAll(".imageEnabled").forEach((element) => {
element.checked = document.querySelector("#imageTableCustom").querySelector(".selectAll input").checked;
});
for (var i = 0; i < throws.length; i++) {
if (document.querySelector("#imageTableCustom").querySelector(".selectAll input").checked && !throws[i].customs.includes(customName))
throws[i].customs.push(customName);
else if (!document.querySelector("#imageTableCustom").querySelector(".selectAll input").checked && throws[i].customs.includes(customName))
throws[i].customs.splice(throws[i].customs.indexOf(customName), 1);
}
setData("throws", throws);
});
document.querySelector("#imageTableCustom").querySelectorAll(".imageRow").forEach((element) => { element.remove(); });
if (throws == null)
setData("throws", []);
else {
throws.forEach((_, index) => {
if (fs.existsSync(userDataPath + "/" + throws[index].location)) {
var row = document.querySelector("#imageRowCustom").cloneNode(true);
row.removeAttribute("id");
row.classList.add("imageRow");
row.removeAttribute("hidden");
document.querySelector("#imageTableCustom").appendChild(row);
row.querySelector(".imageLabel").innerText = throws[index].location.substr(throws[index].location.lastIndexOf('/') + 1);
row.querySelector(".imageImage").src = userDataPath + "/" + throws[index].location;
row.querySelector(".imageEnabled").checked = throws[index].customs.includes(customName);
row.querySelector(".imageEnabled").addEventListener("change", () => {
if (!row.querySelector(".imageEnabled").checked && throws[index].customs.includes(customName))
throws[index].customs.splice(throws[index].customs.indexOf(customName), 1);
else if (row.querySelector(".imageEnabled").checked && !throws[index].customs.includes(customName))
throws[index].customs.push(customName);
setData("throws", throws);
var allEnabled = true;
for (var i = 0; i < throws.length; i++) {
if (!throws[i].customs.includes(customName)) {
allEnabled = false;
break;
}
}
document.querySelector("#imageTableCustom").querySelector(".selectAll input").checked = allEnabled;
});
}
else {
throws.splice(index, 1);
setData("throws", throws);
}
});
}
}
async function loadSoundCustom(customName) {
var impacts = await getData("impacts");
var files = document.querySelector("#loadSoundCustom").files;
for (var i = 0; i < files.length; i++) {
var soundFile = files[i];
if (!fs.existsSync(userDataPath + "/impacts/"))
fs.mkdirSync(userDataPath + "/impacts/");
var append = "";
if (soundFile.path != userDataPath + "\\impacts\\" + soundFile.name)
while (fs.existsSync(userDataPath + "/impacts/" + soundFile.name.substr(0, soundFile.name.lastIndexOf(".")) + append + soundFile.name.substr(soundFile.name.lastIndexOf("."))))
append = append == "" ? 2 : (append + 1);
var filename = soundFile.name.substr(0, soundFile.name.lastIndexOf(".")) + append + soundFile.name.substr(soundFile.name.lastIndexOf("."));
fs.copyFileSync(soundFile.path, userDataPath + "/impacts/" + filename);
impacts.unshift({
"location": "impacts/" + filename,
"volume": 1.0,
"enabled": false,
"customs": [customName]
});
}
setData("impacts", impacts);
openSoundsCustom(customName);
copyFilesToDirectory();
document.querySelector("#loadSoundCustom").value = null;
}
async function openSoundsCustom(customName) {
// Refresh table to remove old event listeners
var oldTable = document.querySelector("#soundTableCustom");
var newTable = oldTable.cloneNode(true);
oldTable.after(newTable);
oldTable.remove();
document.querySelector("#newSoundCustom").addEventListener("click", () => { document.querySelector("#loadSoundCustom").click(); });
document.querySelector("#loadSoundCustom").addEventListener("change", () => { loadSoundCustom(customName); });
var impacts = await getData("impacts");
var allEnabled = true;
for (var i = 0; i < impacts.length; i++) {
if (!impacts[i].customs.includes(customName)) {
allEnabled = false;
break;
}
}
document.querySelector("#soundTableCustom").querySelector(".selectAll input").checked = allEnabled;
document.querySelector("#soundTableCustom").querySelector(".selectAll input").addEventListener("change", () => {
document.querySelector("#soundTableCustom").querySelectorAll(".imageEnabled").forEach((element) => {
element.checked = document.querySelector("#soundTableCustom").querySelector(".selectAll input").checked;
});
for (var i = 0; i < impacts.length; i++) {
if (document.querySelector("#soundTableCustom").querySelector(".selectAll input").checked && !impacts[i].customs.includes(customName))
impacts[i].customs.push(customName);
else if (!document.querySelector("#soundTableCustom").querySelector(".selectAll input").checked && impacts[i].customs.includes(customName))
impacts[i].customs.splice(impacts[i].customs.indexOf(customName), 1);
}
setData("impacts", impacts);
});
document.querySelector("#soundTableCustom").querySelectorAll(".soundRow").forEach((element) => { element.remove(); });
if (impacts == null)
setData("impacts", []);
else {
impacts.forEach((_, index) => {
if (fs.existsSync(userDataPath + "/" + impacts[index].location)) {
var row = document.querySelector("#soundRowCustom").cloneNode(true);
row.removeAttribute("id");
row.classList.add("soundRow");
row.removeAttribute("hidden");
row.querySelector(".imageLabel").innerText = impacts[index].location.substr(impacts[index].location.lastIndexOf('/') + 1);
document.querySelector("#soundTableCustom").appendChild(row);
row.querySelector(".imageEnabled").checked = impacts[index].customs.includes(customName);
row.querySelector(".imageEnabled").addEventListener("change", () => {
if (!row.querySelector(".imageEnabled").checked && impacts[index].customs.includes(customName))
impacts[index].customs.splice(impacts[index].customs.indexOf(customName), 1);
else if (row.querySelector(".imageEnabled").checked && !impacts[index].customs.includes(customName))
impacts[index].customs.push(customName);
setData("impacts", impacts);
for (var i = 0; i < impacts.length; i++) {
if (!impacts[i].customs.includes(customName)) {
allEnabled = false;
break;
}
}
document.querySelector("#soundTableCustom").querySelector(".selectAll input").checked = allEnabled;
});
}
else {
impacts.splice(index, 1);
setData("impacts", impacts);
}
});
}
}
async function loadImpactDecal(customName) {
var customBonks = await getData("customBonks");
var files = document.querySelector("#loadImpactDecal").files;
for (var i = 0; i < files.length; i++) {
var imageFile = files[i];
if (!fs.existsSync(userDataPath + "/decals/"))
fs.mkdirSync(userDataPath + "/decals/");
var append = "";
if (imageFile.path != userDataPath + "\\decals\\" + imageFile.name)
while (fs.existsSync(userDataPath + "/decals/" + imageFile.name.substr(0, imageFile.name.lastIndexOf(".")) + append + imageFile.name.substr(imageFile.name.lastIndexOf("."))))
append = append == "" ? 2 : (append + 1);
var filename = imageFile.name.substr(0, imageFile.name.lastIndexOf(".")) + append + imageFile.name.substr(imageFile.name.lastIndexOf("."));
fs.copyFileSync(imageFile.path, userDataPath + "/decals/" + filename);
customBonks[customName].impactDecals.unshift({
"location": "decals/" + filename,
"duration": 0.25,
"scale": 1,
"enabled": true
});
}
setData("customBonks", customBonks);
openImpactDecals(customName);
copyFilesToDirectory();
document.querySelector("#loadImpactDecal").value = null;
}
async function openImpactDecals(customName) {
// Refresh table to remove old event listeners
var oldTable = document.querySelector("#impactDecalsTable");
var newTable = oldTable.cloneNode(true);
oldTable.after(newTable);
oldTable.remove();
document.querySelector("#newImpactDecal").addEventListener("click", () => { document.querySelector("#loadImpactDecal").click(); });
document.querySelector("#loadImpactDecal").addEventListener("change", () => { loadImpactDecal(customName) });
var customBonks = await getData("customBonks");
var allEnabled = true;
for (var i = 0; i < customBonks[customName].impactDecals.length; i++) {
if (!customBonks[customName].impactDecals[i].enabled) {
allEnabled = false;
break;
}
}
document.querySelector("#impactDecalsTable").querySelector(".selectAll input").checked = allEnabled;
document.querySelector("#impactDecalsTable").querySelector(".selectAll input").addEventListener("change", async () => {
document.querySelector("#impactDecalsTable").querySelectorAll(".imageEnabled").forEach((element) => {
element.checked = document.querySelector("#impactDecalsTable").querySelector(".selectAll input").checked;
});
for (var i = 0; i < customBonks[customName].impactDecals.length; i++)
customBonks[customName].impactDecals[i].enabled = document.querySelector("#impactDecalsTable").querySelector(".selectAll input").checked;
setData("customBonks", customBonks);
});
document.querySelector("#impactDecalsTable").querySelectorAll(".imageRow").forEach((element) => { element.remove(); });
customBonks[customName].impactDecals.forEach((_, index) => {
if (fs.existsSync(userDataPath + "/" + customBonks[customName].impactDecals[index].location)) {
var row = document.querySelector("#impactDecalRow").cloneNode(true);
row.removeAttribute("id");
row.classList.add("imageRow");
row.removeAttribute("hidden");
row.querySelector(".imageLabel").innerText = customBonks[customName].impactDecals[index].location.substr(customBonks[customName].impactDecals[index].location.lastIndexOf('/') + 1);
document.querySelector("#impactDecalsTable").appendChild(row);
row.querySelector(".imageImage").src = userDataPath + "/" + customBonks[customName].impactDecals[index].location;
row.querySelector(".imageRemove").addEventListener("click", () => {
customBonks[customName].impactDecals.splice(index, 1);
setData("customBonks", customBonks);
openImpactDecals(customName);
});
row.querySelector(".imageEnabled").checked = customBonks[customName].impactDecals[index].enabled;
row.querySelector(".imageEnabled").addEventListener("change", () => {
customBonks[customName].impactDecals[index].enabled = row.querySelector(".imageEnabled").checked;
setData("customBonks", customBonks);
var allEnabled = true;
for (var i = 0; i < customBonks[customName].impactDecals.length; i++) {
if (!customBonks[customName].impactDecals[i].enabled) {
allEnabled = false;
break;
}
}
document.querySelector("#impactDecalsTable").querySelector(".selectAll input").checked = allEnabled;
});
row.querySelector(".decalDuration").value = customBonks[customName].impactDecals[index].duration;
row.querySelector(".decalDuration").addEventListener("change", () => {
clampValue(row.querySelector(".decalDuration").value, 0, null);
customBonks[customName].impactDecals[index].duration = parseFloat(row.querySelector(".decalDuration").value);
setData("customBonks", customBonks);
});
row.querySelector(".decalScale").value = customBonks[customName].impactDecals[index].scale;
row.querySelector(".decalScale").addEventListener("change", () => {
clampValue(row.querySelector(".decalScale"), 0, null);
customBonks[customName].impactDecals[index].scale = parseFloat(row.querySelector(".decalScale").value);
setData("customBonks", customBonks);
});
}
else {
customBonks[customName].impactDecals.splice(index, 1);
setData("customBonks", customBonks);
}
});
}
async function loadWindupSound(customName) {
var customBonks = await getData("customBonks");
var files = document.querySelector("#loadWindupSound").files;
for (var i = 0; i < files.length; i++) {
var soundFile = files[i];
if (!fs.existsSync(userDataPath + "/windups/"))
fs.mkdirSync(userDataPath + "/windups/");
var append = "";
if (soundFile.path != userDataPath + "\\windups\\" + soundFile.name)
while (fs.existsSync(userDataPath + "/windups/" + soundFile.name.substr(0, soundFile.name.lastIndexOf(".")) + append + soundFile.name.substr(soundFile.name.lastIndexOf("."))))
append = append == "" ? 2 : (append + 1);
var filename = soundFile.name.substr(0, soundFile.name.lastIndexOf(".")) + append + soundFile.name.substr(soundFile.name.lastIndexOf("."));
fs.copyFileSync(soundFile.path, userDataPath + "/windups/" + filename);
customBonks[customName].windupSounds.unshift({
"location": "windups/" + filename,
"volume": 1.0,
"enabled": true
});
}
setData("customBonks", customBonks);
openWindupSounds(customName);
copyFilesToDirectory();
document.querySelector("#loadWindupSound").value = null;
}
async function openWindupSounds(customName) {
// Refresh table to remove old event listeners
var oldTable = document.querySelector("#windupSoundTable");
var newTable = oldTable.cloneNode(true);
oldTable.after(newTable);
oldTable.remove();
document.querySelector("#newWindupSound").addEventListener("click", () => { document.querySelector("#loadWindupSound").click(); });
document.querySelector("#loadWindupSound").addEventListener("change", () => { loadWindupSound(customName) });
var customBonks = await getData("customBonks");
var allEnabled = true;
for (var i = 0; i < customBonks[customName].windupSounds.length; i++) {
if (!customBonks[customName].windupSounds[i].enabled) {
allEnabled = false;
break;
}
}
document.querySelector("#windupSoundTable").querySelector(".selectAll input").checked = allEnabled;
document.querySelector("#windupSoundTable").querySelector(".selectAll input").addEventListener("change", async () => {
document.querySelector("#windupSoundTable").querySelectorAll(".imageEnabled").forEach((element) => {
element.checked = document.querySelector("#windupSoundTable").querySelector(".selectAll input").checked;
});
for (var i = 0; i < customBonks[customName].windupSounds.length; i++)
customBonks[customName].windupSounds[i].enabled = document.querySelector("#windupSoundTable").querySelector(".selectAll input").checked;
setData("customBonks", customBonks);
});
document.querySelector("#windupSoundTable").querySelectorAll(".soundRow").forEach((element) => { element.remove(); });
customBonks[customName].windupSounds.forEach((_, index) => {
if (fs.existsSync(userDataPath + "/" + customBonks[customName].windupSounds[index].location)) {
var row = document.querySelector("#windupSoundRow").cloneNode(true);
row.removeAttribute("id");
row.classList.add("soundRow");
row.removeAttribute("hidden");
row.querySelector(".imageLabel").innerText = customBonks[customName].windupSounds[index].location.substr(customBonks[customName].windupSounds[index].location.lastIndexOf('/') + 1);
document.querySelector("#windupSoundTable").appendChild(row);
row.querySelector(".imageRemove").addEventListener("click", () => {
customBonks[customName].windupSounds.splice(index, 1);
setData("customBonks", customBonks);
openWindupSounds(customName);
});
row.querySelector(".imageEnabled").checked = customBonks[customName].windupSounds[index].enabled;
row.querySelector(".imageEnabled").addEventListener("change", () => {
customBonks[customName].windupSounds[index].enabled = row.querySelector(".imageEnabled").checked;
setData("customBonks", customBonks);
var allEnabled = true;
for (var i = 0; i < customBonks[customName].windupSounds.length; i++) {
if (!customBonks[customName].windupSounds[i].enabled) {
allEnabled = false;
break;
}
}
document.querySelector("#windupSoundTable").querySelector(".selectAll input").checked = allEnabled;
});
row.querySelector(".soundVolume").value = customBonks[customName].windupSounds[index].volume;
row.querySelector(".soundVolume").addEventListener("change", () => {
clampValue(row.querySelector(".soundVolume"), 0, 1);
customBonks[customName].windupSounds[index].volume = parseFloat(row.querySelector(".soundVolume").value);
setData("customBonks", customBonks);
});
}
else {
customBonks[customName].windupSounds.splice(index, 1);
setData("customBonks", customBonks);
}
});
}
document.querySelector("#guard3").querySelector(".guardImageScale").addEventListener("change", async () => {
var guardThrows = await getData("guardThrows");
guardThrows.guard3.scale = parseFloat(document.querySelector("#guard3").querySelector(".guardImageScale").value);
setData("guardThrows", guardThrows);
});
document.querySelector("#guardImageAdd3").addEventListener("click", () => { document.querySelector("#loadGuardImage3").click(); });
document.querySelector("#loadGuardImage3").addEventListener("change", () => { loadGuardImage("3") });
document.querySelector("#guard2").querySelector(".guardImageScale").addEventListener("change", async () => {
var guardThrows = await getData("guardThrows");
guardThrows.guard2.scale = parseFloat(document.querySelector("#guard2").querySelector(".guardImageScale").value);
setData("guardThrows", guardThrows);
});
document.querySelector("#guardImageAdd2").addEventListener("click", () => { document.querySelector("#loadGuardImage2").click(); });
document.querySelector("#loadGuardImage2").addEventListener("change", () => { loadGuardImage("2") });
document.querySelector("#guard1").querySelector(".guardImageScale").addEventListener("change", async () => {
var guardThrows = await getData("guardThrows");
guardThrows.guard1.scale = parseFloat(document.querySelector("#guard1").querySelector(".guardImageScale").value);
setData("guardThrows", guardThrows);
});
document.querySelector("#guardImageAdd1").addEventListener("click", () => { document.querySelector("#loadGuardImage1").click(); });
document.querySelector("#loadGuardImage1").addEventListener("change", () => { loadGuardImage("1") });
async function loadGuardImage(key) {
var guardThrows = await getData("guardThrows");
var files = document.querySelector("#loadGuardImage" + key).files;
// Grab the image that was just loaded
var imageFile = files[0];
// If the folder for objects doesn't exist for some reason, make it
if (!fs.existsSync(userDataPath + "/throws/"))
fs.mkdirSync(userDataPath + "/throws/");
// Ensure that we're not overwriting any existing files with the same name
// If a file already exists, add an interating number to the end until it"s a unique filename
var append = "";
if (imageFile.path != userDataPath + "\\throws\\" + imageFile.name)
while (fs.existsSync(userDataPath + "/throws/" + imageFile.name.substr(0, imageFile.name.lastIndexOf(".")) + append + imageFile.name.substr(imageFile.name.lastIndexOf("."))))
append = append == "" ? 2 : (append + 1);
var filename = imageFile.name.substr(0, imageFile.name.lastIndexOf(".")) + append + imageFile.name.substr(imageFile.name.lastIndexOf("."));
// Make a copy of the file into the local folder
fs.copyFileSync(imageFile.path, userDataPath + "/throws/" + filename);
// Add the new image, update the data, and refresh the images page
guardThrows[`guard${key}`].location = "throws/" + filename;
setData("guardThrows", guardThrows);
openGuardImages();
copyFilesToDirectory();
// Reset the image upload
document.querySelector("#loadGuardImage" + key).value = null;
}
async function openGuardImages() {
var guardThrows = await getData("guardThrows");
if (guardThrows == null) {
guardThrows = defaultData.guardThrows;
setData("guardThrows", guardThrows);
}
document.querySelector("#guard3").querySelector(".imageImage").src = userDataPath + "/" + guardThrows.guard3.location;
document.querySelector("#guard3").querySelector(".guardImageScale").value = guardThrows.guard3.scale;
document.querySelector("#guard2").querySelector(".imageImage").src = userDataPath + "/" + guardThrows.guard2.location;
document.querySelector("#guard2").querySelector(".guardImageScale").value = guardThrows.guard2.scale;
document.querySelector("#guard1").querySelector(".imageImage").src = userDataPath + "/" + guardThrows.guard1.location;
document.querySelector("#guard1").querySelector(".guardImageScale").value = guardThrows.guard1.scale;
}
// 电池瓜子操作
document.querySelector("#coinBattery").querySelector(".coinImageScale").addEventListener("change", async () => {
let coinThrows = await getData("coinThrows");
coinThrows.battery.scale = parseFloat(document.querySelector("#coinBattery").querySelector(".coinImageScale").value);
setData("coinThrows", coinThrows);
});
document.querySelector("#coinBatteryAdd").addEventListener("click", () => {
document.querySelector("#loadCoinImagebattery").click();
});
document.querySelector("#loadCoinImagebattery").addEventListener("change", () => {
loadCoinImage("battery");
})
document.querySelector("#coinSilver").querySelector(".coinImageScale").addEventListener("change", async () => {
let coinThrows = await getData("coinThrows");
coinThrows.silver.scale = parseFloat(document.querySelector("#coinSilver").querySelector(".coinImageScale").value);
setData("coinThrows", coinThrows);
});
document.querySelector("#coinSilverAdd").addEventListener("click", () => {
document.querySelector("#loadCoinImagesilver").click();
});
document.querySelector("#loadCoinImagesilver").addEventListener("change", () => {
loadCoinImage("silver");
})
// 加载电池瓜子图片
async function loadCoinImage(key) {
var coinThrows = await getData("coinThrows");
var files = document.querySelector("#loadCoinImage" + key).files;
// Grab the image that was just loaded
var imageFile = files[0];
// If the folder for objects doesn't exist for some reason, make it
if (!fs.existsSync(userDataPath + "/throws/"))
fs.mkdirSync(userDataPath + "/throws/");
// Ensure that we're not overwriting any existing files with the same name
// If a file already exists, add an interating number to the end until it"s a unique filename
var append = "";
if (imageFile.path != userDataPath + "\\throws\\" + imageFile.name)
while (fs.existsSync(userDataPath + "/throws/" + imageFile.name.substr(0, imageFile.name.lastIndexOf(".")) + append + imageFile.name.substr(imageFile.name.lastIndexOf("."))))
append = append == "" ? 2 : (append + 1);
var filename = imageFile.name.substr(0, imageFile.name.lastIndexOf(".")) + append + imageFile.name.substr(imageFile.name.lastIndexOf("."));
// Make a copy of the file into the local folder
fs.copyFileSync(imageFile.path, userDataPath + "/throws/" + filename);
// Add the new image, update the data, and refresh the images page
coinThrows[key].location = "throws/" + filename;
setData("coinThrows", coinThrows);
openCoinImages();
copyFilesToDirectory();
// Reset the image upload
document.querySelector("#loadCoinImage" + key).value = null;
}
async function openCoinImages() {
var coinThrows = await getData("coinThrows");
if (coinThrows == null) {
coinThrows = defaultData.coinThrows;
setData("coinThrows", coinThrows);
}
document.querySelector("#coinBattery").querySelector(".imageImage").src = userDataPath + "/" + coinThrows.battery.location;
document.querySelector("#coinBattery").querySelector(".coinImageScale").value = coinThrows.battery.scale;
document.querySelector("#coinSilver").querySelector(".imageImage").src = userDataPath + "/" + coinThrows.silver.location;
document.querySelector("#coinSilver").querySelector(".coinImageScale").value = coinThrows.silver.scale;
}
document.querySelector("#loadImageSound").addEventListener("change", loadImageSound);
async function loadImageSound() {
// Grab the image that was just loaded
var soundFile = document.querySelector("#loadImageSound").files[0];
// If the folder for objects doesn"t exist for some reason, make it
if (!fs.existsSync(userDataPath + "/impacts/"))
fs.mkdirSync(userDataPath + "/impacts/");
// Ensure that we're not overwriting any existing files with the same name
// If a file already exists, add an interating number to the end until it"s a unique filename
var append = "";
if (soundFile.path != userDataPath + "\\impacts\\" + soundFile.name)
while (fs.existsSync(userDataPath + "/impacts/" + soundFile.name.substr(0, soundFile.name.lastIndexOf(".")) + append + soundFile.name.substr(soundFile.name.lastIndexOf("."))))
append = append == "" ? 2 : (append + 1);
var filename = soundFile.name.substr(0, soundFile.name.lastIndexOf(".")) + append + soundFile.name.substr(soundFile.name.lastIndexOf("."));
// Make a copy of the file into the local folder
fs.copyFileSync(soundFile.path, userDataPath + "/impacts/" + filename);
// Get the existing images, add the new image, update the data, and refresh the images page
var throws = await getData("throws");
throws[currentImageIndex].sound = "impacts/" + filename;
setData("throws", throws);
// Reset the image upload
document.querySelector("#loadImageSound").value = null;
openImageDetails(currentImageIndex);
copyFilesToDirectory();
}
var currentImageIndex = -1;
async function openImageDetails() {
var throws = await getData("throws");
// Refresh nodes to remove old listeners
var oldButton = document.querySelector("#testImage");
var newButton = document.querySelector("#testImage").cloneNode(true);
oldButton.after(newButton);
oldButton.remove();
var oldTable = document.querySelector("#testImage");
var newTable = oldTable.cloneNode(true);
oldTable.after(newTable);
oldTable.remove();
document.querySelector("#testImage").addEventListener("click", () => testItem(currentImageIndex));
const details = document.querySelector("#imageDetails");
details.querySelector(".imageLabel").innerText = throws[currentImageIndex].location.substr(throws[currentImageIndex].location.lastIndexOf('/') + 1);
details.querySelector(".imageImage").src = userDataPath + "/" + throws[currentImageIndex].location;
details.querySelector(".imageImage").style.transform = "scale(" + throws[currentImageIndex].scale + ")";
details.querySelector(".imageWeight").value = throws[currentImageIndex].weight;
details.querySelector(".imageScale").value = throws[currentImageIndex].scale;
if (throws[currentImageIndex].sound != null) {
details.querySelector(".imageSoundName").value = throws[currentImageIndex].sound.substr(8);
details.querySelector(".imageSoundRemove").removeAttribute("disabled");
}