Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
957 lines (807 loc) · 33.2 KB
/
Copy pathmain.js
File metadata and controls
957 lines (807 loc) · 33.2 KB
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
// balls code
/** @type {Array<PlistAndImageComboDeal>} */
let loadedStuff = []
let loading = 0
/** @type {PlistAndImageComboDeal} */
let currentCombo = null
/** @type {PlistDict} */
let currentDict = null
let currentDictLocked = false
let currentAnimationName = ""
function getSelectedVersion() { return Number(document.querySelector("#version-select").value) ?? Version.V22081 }
function getSelectedSheet() { return document.querySelector("#sheet-select").value }
function getSelectedResolution() { return Number(document.querySelector("#quality-select").value) ?? Resolution.uhd }
function getPath(name, resolution, version, type, ignoreResolution) {
let versionString
let typeString
let resolutionString
if (resolution == Resolution.hd) resolutionString = "-hd"
else if (resolution == Resolution.sd) resolutionString = ""
else if (resolution == Resolution.uhd) resolutionString = "-uhd"
if (version == Version.V2113) versionString = "2.113"
else if (version == Version.V2204) versionString = "2.204"
else if (version == Version.V22073) versionString = "2.2073"
else if (version == Version.V22074) versionString = "2.2074"
else if (version == Version.V2208) versionString = "2.208"
else if (version == Version.V22081) versionString = "2.2081"
else if (version == Version.V2209) versionString = "2.209"
else if (version == Version.VLitePinkMoreGames) versionString = "1.2litepinkmoregames"
else if (version == Version.VGDWeb1) versionString = "gd-web-1"
else if (version == Version.VGeode4100) versionString = "geode-4.10.0"
else if (version == Version.VGeode520) versionString = "geode-5.2.0"
else if (version == Version.VGeode553) versionString = "geode-5.5.3"
if (type == FileType.Image) typeString = "png"
else if (type == FileType.Plist) typeString = "plist"
if (ignoreResolution) {
return `./assets/${versionString}/${name}.${typeString}`
} else {
return `./assets/${versionString}/${name}${resolutionString}.${typeString}`
}
}
/**
* @param {string} name
* @param {number} resolution
* @param {number} version
* @returns {Promise<PlistAndImageComboDeal>}
*/
async function getImageAndPlist(name, resolution, version) {
return new Promise(async resolve => {
// check if it's loaded
let combo = loadedStuff.find(value => (value.name == name && value.resolution == resolution && value.version == version))
if (combo) {
// yes!
console.log("%s at %s (%s) is loaded!", name, resolution, version)
resolve(combo)
} else {
// no :(
console.log("need to load %s at %s (%s)", name, resolution, version)
let isWebSheet = name == "GJ_WebSheet"
let imagePath = getPath(name, resolution, version, FileType.Image, isWebSheet)
let plistPath = getPath(name, resolution, version, FileType.Plist, isWebSheet)
// parse the plist
let plistResponse = await fetch(plistPath)
let plistString = await plistResponse.text()
let plistDOM = new DOMParser().parseFromString(plistString, "text/xml")
let plistBody = plistDOM.querySelector("dict").querySelector("dict")
// only integer tag is the format, naive approach but works
let format = plistDOM.querySelector("integer").innerHTML
if (isWebSheet) format = -1
let plist = []
for (let i = 0; i < plistBody.children.length; i += 2) {
plist.push(new PlistDict(plistBody.children[i].innerHTML, plistBody.children[i + 1], format))
}
// load the image
loading++
let image = new Image()
image.src = imagePath
image.addEventListener("load", async () => {
loading--
loadedStuff.push(new PlistAndImageComboDeal(name, resolution, version, image, plist))
resolve(await getImageAndPlist(name, resolution, version))
})
}
})
}
async function autoGetImageAndPlist() {
return getImageAndPlist(getSelectedSheet(), getSelectedResolution(), getSelectedVersion())
}
function populateSheetSelect() {
let sheetSelect = document.querySelector("#sheet-select")
let prevSelected = sheetSelect.value
sheetSelect.innerText = ""
let sheets = []
let noUhd = false
let noResolution = false
let isLegacy = false
let notes = ""
let defaultSheet = ""
let version = getSelectedVersion()
if (
version == Version.V2204
|| version == Version.V22073
|| version == Version.V22074
|| version == Version.V2208
|| version == Version.V22081
|| version == Version.V2209
) {
sheets = [
"DungeonSheet",
"FireSheet_01",
"GauntletSheet",
"GJ_GameSheet",
"GJ_GameSheet02",
"GJ_GameSheet03",
"GJ_GameSheet04",
"GJ_GameSheetEditor",
"GJ_GameSheetGlow",
"GJ_LaunchSheet",
"GJ_ParticleSheet",
"GJ_PathSheet",
"GJ_ShopSheet",
"GJ_ShopSheet01",
"GJ_ShopSheet02",
"GJ_ShopSheet03",
"PixelSheet_01",
"SecretSheet",
"TowerSheet",
"TreasureRoomSheet",
"WorldSheet",
]
defaultSheet = "GJ_GameSheet03"
} else if (version == Version.V2113) {
sheets = [
"DungeonSheet",
"FireSheet_01",
"GauntletSheet",
"GJ_GameSheet",
"GJ_GameSheet02",
"GJ_GameSheet03",
"GJ_GameSheet04",
"GJ_GameSheetGlow",
"GJ_LaunchSheet",
"GJ_ShopSheet",
"SecretSheet",
"WorldSheet",
]
defaultSheet = "GJ_GameSheet03"
} else if (version == Version.VLitePinkMoreGames) {
sheets = [
"GJ_GameSheet",
"GJ_LaunchSheet"
]
noUhd = true
isLegacy = true
notes = `A GD Lite version that was only released for 14 days - it had a pink More Games button that did nothing when pressed, see it in the middle of GJ_GameSheet! See <a href="https://twitter.com/Misabr0penguin/status/1623083029554950145" target="_blank">this twitter post.</a>`
defaultSheet = "GJ_GameSheet"
} else if (
version == Version.VGeode4100
|| version == Version.VGeode520
|| version == Version.VGeode553
) {
sheets = [
"APISheet",
"BlankSheet",
"EventSheet",
"LogoSheet"
]
defaultSheet = "APISheet"
} else if (version == Version.VGDWeb1) {
sheets = [ "GJ_WebSheet" ]
noResolution = true
isLegacy = true
notes = `<a href="https://geometrydash.com/">https://geometrydash.com/</a> (converted from json to plist)<br/>note: this is regarded as version 2.0 of texturepacker (same as 1.2), but the format differs slightly!`
defaultSheet = "GJ_WebSheet"
}
if (version == Version.V2209) {
sheets.push("GJ_FontSheet");
sheets.sort();
}
if (version == Version.V22073) {
notes = "Pretty sure this is identical to 2.2074 but it was on my laptop so might as well archive it."
}
if (version == Version.VGeode553) {
notes = "no way Sapphire SDK"
}
for (let sheet of sheets) {
let element = document.createElement("option")
element.innerText = sheet
element.value = sheet
sheetSelect.appendChild(element)
}
if (sheets.includes(prevSelected))
sheetSelect.value = prevSelected
let versionNotesElement = document.querySelector("#version-notes")
versionNotesElement.innerHTML = notes
// remove or add uhd for old sheets
let definitionSelector = document.querySelector("#quality-select")
if (noResolution) {
definitionSelector.disabled = true // any truthy
} else {
definitionSelector.disabled = false
}
if (definitionSelector.children.length == 2 && noUhd == false) {
let uhdOption = document.createElement("option")
uhdOption.innerText = "UHD"
uhdOption.value = Resolution.uhd
uhdOption.selected = true // any truthy value
definitionSelector.appendChild(uhdOption)
} else if (definitionSelector.children.length == 3 && noUhd) {
let uhdOption = Array.from(definitionSelector.querySelectorAll("option")).find(el => el.value == Resolution.uhd)
definitionSelector.removeChild(uhdOption)
let hdOption = Array.from(definitionSelector.querySelectorAll("option")).find(el => el.value == Resolution.hd)
hdOption.selected = true // any truthy
}
// remove or add size for legacy sheets
let size = document.querySelector("#size")
if (isLegacy) {
size.classList.add("legacy-doesnt-exist")
} else {
size.classList.remove("legacy-doesnt-exist")
}
// default
document.querySelector("#sheet-select").value = defaultSheet
}
function updateCanvasSize() {
let canvas = document.querySelector("#canvas")
let canvasWrap = document.querySelector("#canvas-wrapper")
let rect = canvasWrap.getBoundingClientRect()
// thanks prevter
let dpr = Math.min(window.devicePixelRatio || 1, 2);
canvas.width = rect.width * dpr
canvas.height = rect.height * dpr
updatePreview()
}
async function updateCurrentCombo() {
currentDict = null
currentCombo = await autoGetImageAndPlist()
}
function draw() {
/** @type HTMLCanvasElement */
let canvas = document.querySelector("#canvas")
let ctx = canvas.getContext("2d")
ctx.clearRect(0, 0, canvas.width, canvas.height)
// this also handles showing #loader
if (loading > 0) {
document.querySelector("#loader").style.display = "block"
} else {
document.querySelector("#loader").style.display = "none"
}
// everything below here requires a combo to be selected
if (currentCombo == null) {
requestAnimationFrame(draw)
return
}
if (canvas.width / canvas.height < currentCombo.image.width / currentCombo.image.height)
ctx.drawImage(currentCombo.image, 0, 0, canvas.width, (canvas.width / currentCombo.image.width) * currentCombo.image.height)
else
ctx.drawImage(currentCombo.image, 0, 0, (canvas.height / currentCombo.image.height) * currentCombo.image.width, canvas.height)
// draw a box where the cursor is selecting a sprite
if (currentDict != null) {
let scale
if (canvas.width / canvas.height < currentCombo.image.width / currentCombo.image.height)
scale = canvas.width / currentCombo.image.width
else
scale = canvas.height / currentCombo.image.height
let width, height
if (currentDict.textureRotated) {
width = currentDict.textureRect.h * scale
height = currentDict.textureRect.w * scale
} else {
width = currentDict.textureRect.w * scale
height = currentDict.textureRect.h * scale
}
ctx.strokeStyle = "red"
ctx.lineWidth = 2
ctx.strokeRect(currentDict.textureRect.x * scale, currentDict.textureRect.y * scale, width, height)
if (currentDictLocked) {
// dont tell anyone i borrowed the idea from colon
if (currentSearchResults.length > 0) {
// dim all sprites that don't match the search
ctx.beginPath()
ctx.rect(0, 0, canvas.width, canvas.height)
for (let matchingDict of currentSearchResults) {
let w, h
if (matchingDict.textureRotated) {
w = matchingDict.textureRect.h * scale
h = matchingDict.textureRect.w * scale
} else {
w = matchingDict.textureRect.w * scale
h = matchingDict.textureRect.h * scale
}
ctx.rect(matchingDict.textureRect.x * scale, matchingDict.textureRect.y * scale, w, h)
}
ctx.fillStyle = "#00000056"
ctx.fill("evenodd")
} else {
ctx.beginPath()
ctx.rect(0, 0, canvas.width, canvas.height)
ctx.rect(currentDict.textureRect.x * scale, currentDict.textureRect.y * scale, width, height)
ctx.fillStyle = "#00000056"
ctx.fill("evenodd")
}
}
}
// draw all the textureRects lol (debug)
// let textureScale = canvas.width / currentCombo.image.width
// for (let dict of currentCombo.plist) {
// let rect = dict.textureRect
// ctx.strokeRect = "red"
// ctx.lineWidth = 2
// if (dict.textureRotated) ctx.strokeRect(rect.x * textureScale, rect.y * textureScale, rect.h * textureScale, rect.w * textureScale)
// else ctx.strokeRect(rect.x * textureScale, rect.y * textureScale, rect.w * textureScale, rect.h * textureScale)
// }
requestAnimationFrame(draw)
}
// runs on mousemove
/**
* @param {MouseEvent} event
* @returns {boolean} whether the mouse is hovering something
*/
function tickCursor(event) {
if (currentCombo == null) return false
if (currentDictLocked) return false
let rect = document.querySelector("#canvas").getBoundingClientRect()
let mx = event.pageX - rect.left
let my = event.pageY - rect.top
// thanks prevter
let dpr = Math.min(window.devicePixelRatio || 1, 2);
mx *= dpr
my *= dpr
let scale
if (canvas.width / canvas.height < currentCombo.image.width / currentCombo.image.height)
scale = canvas.width / currentCombo.image.width
else
scale = canvas.height / currentCombo.image.height
// instead of multiplying everything by scale
// i can divide the mouse cursor by scale :bigbrain:
mx /= scale
my /= scale
for (let dict of currentCombo.plist) {
if (
// unrotated
(!dict.textureRotated && dict.textureRect.x < mx && dict.textureRect.y < my && dict.textureRect.x + dict.textureRect.w > mx && dict.textureRect.y + dict.textureRect.h > my)
||
// rotated
(dict.textureRotated && dict.textureRect.x < mx && dict.textureRect.y < my && dict.textureRect.x + dict.textureRect.h > mx && dict.textureRect.y + dict.textureRect.w > my)
) {
currentDict = dict
// fill out info
updateInfoAndPreview()
return true
}
}
}
function updateInfoAndPreview() {
document.querySelector("#name").innerHTML = currentDict.key
document.querySelector("#offset").innerHTML = currentDict.spriteOffset
document.querySelector("#size").innerHTML = currentDict.spriteSize.toString(true)
document.querySelector("#ssize").innerHTML = currentDict.spriteSourceSize.toString(true)
document.querySelector("#rect").innerHTML = currentDict.textureRect
document.querySelector("#rot").innerHTML = (currentDict.textureRotated ? "True" : "False")
updatePreview()
}
function updatePreview(useAltCanvas = false) {
/** @type {HTMLCanvasElement} */
let canvas
if (useAltCanvas) {
canvas = document.querySelector("#preview2")
canvas.style.display = "block"
} else {
canvas = document.querySelector("#preview")
document.querySelector("#preview2").style.display = "none"
}
let ctx = canvas.getContext("2d")
if (currentDict == null) {
return
}
let sourceWidth, sourceHeight
if (currentDict.textureRotated) {
sourceWidth = currentDict.textureRect.h
sourceHeight = currentDict.textureRect.w
} else {
sourceWidth = currentDict.textureRect.w
sourceHeight = currentDict.textureRect.h
}
canvas.width = sourceWidth
canvas.height = sourceHeight
ctx.drawImage(
currentCombo.image,
currentDict.textureRect.x,
currentDict.textureRect.y,
sourceWidth, sourceHeight,
0, 0,
canvas.width, canvas.height
)
if (currentDict.textureRotated) {
// translateX and sourceWidth used here which seems counter-intuitive but
// it's been rotated so you need to use the other way around if that makes
// sense
// also this is a SHIT way to do it but the gdimagebrowser codebase is already
// so fucking bad at this point it's built on top of itself too many times :sob:
// not even following 80 column with these comments
canvas.style.transform = `rotate(-90deg)`
} else {
canvas.style.transform = ""
}
let wrap = document.querySelector("#preview-wrapper")
let max = wrap.getBoundingClientRect()
let cur = canvas.getBoundingClientRect()
// taken from object nodes LOL
let maxRatio = max.width / max.height
let curRatio = cur.width / cur.height
let scale = 1
if (maxRatio > curRatio) {
// adjust for height
let maxSize = max.height
let minSize = max.height * 0.5
if (cur.height > maxSize) {
scale = maxSize / cur.height
} else if (cur.height < minSize) {
scale = minSize / cur.height
}
} else {
// adjust for width
let maxSize = max.width
let minSize = max.width * 0.5
if (cur.width > maxSize) {
scale = maxSize / cur.width
} else if (cur.width < minSize) {
scale = minSize / cur.width
}
}
canvas.style.transform += ` scale(${scale})`
document.querySelector("#preview-scale").innerText = `(${scale.toFixed(3)}x scale)`
if (currentDict.textureRotated) {
canvas.style.transform += ` translateX(-${sourceWidth}px)`
}
if (document.querySelector("#include-offset").checked) {
if (currentDict.textureRotated) {
canvas.style.transform += ` translate(${currentDict.spriteOffset.x}px, ${currentDict.spriteOffset.y}px)`
} else {
canvas.style.transform += ` translate(${currentDict.spriteOffset.y}px, ${currentDict.spriteOffset.x}px)`
}
}
if (!useAltCanvas
&& document.querySelector("#find-portal").checked
&& currentDict.key.startsWith("portal_")
&& !currentDict.key.includes("extra")) {
let otherName
if (currentDict.key.includes("front")) otherName = currentDict.key.replace("front", "back")
else otherName = currentDict.key.replace("back", "front")
let other = searchCurrentCombo(otherName, true)
if (other.result) {
let prevDict = currentDict
currentDict = other.result
updatePreview(true)
currentDict = prevDict
} else {
console.log("portal other side not found!")
}
}
}
let animationSpeed = Number(document.querySelector("#animate-speed").value)
function checkShouldAnimate(first) {
if (currentDict == null) return
if (!currentDictLocked) return
if (!document.querySelector("#animate").checked) return
// see if any other sprites exist that are the in the same animation
let regex = new RegExp(/^(.+?)_(\d\d\d)\.png$/)
let match = currentDict.key.match(regex)
let name = match[1]
let index = Number(match[2])
if (first) {
// skip this iter, dont immediately overwrite sprite, set currentAnimationName
currentAnimationName = name
setTimeout(() => {
checkShouldAnimate(false)
}, animationSpeed)
return
}
if (name != currentAnimationName) return
let potentialResetSprite = null
let found = false
for (let dict of currentCombo.plist) {
let match = dict.key.match(regex)
if (match == null) continue
if (match[1] == name) {
// ah ha, found another
let newIndex = Number(match[2])
if (newIndex == index + 1) {
// this is the next one
currentDict = dict
found = true
break
}
if (newIndex == 1) {
// this could potentially be the next one to wrap around to
potentialResetSprite = dict
}
}
}
if (!found && potentialResetSprite != null) {
// nothing found but we have a reset sprite
currentDict = potentialResetSprite
found = true
}
if (found) {
// update shit
updateInfoAndPreview()
}
setTimeout(() => {
checkShouldAnimate(false)
}, animationSpeed)
}
/**
* @returns {OffscreenCanvas}
*/
function unrotatePreviewCanvas() {
/** @type {HTMLCanvasElement} */
let canvas = document.querySelector("#preview")
let rotated = canvas.style.transform.includes("rotate")
if (rotated) {
let offscreen = new OffscreenCanvas(canvas.height, canvas.width)
let ctx = offscreen.getContext("2d")
ctx.translate(0, canvas.width)
ctx.rotate(-90 * Math.PI / 180)
ctx.drawImage(canvas, 0, 0)
return offscreen
} else {
let offscreen = new OffscreenCanvas(canvas.width, canvas.height)
let ctx = offscreen.getContext("2d")
ctx.drawImage(canvas, 0, 0)
return offscreen
}
}
// runs on contextmenu
function downloadPart(event) {
event.preventDefault()
// blobbage
unrotatePreviewCanvas().convertToBlob().then(blob => {
// create link to download
const link = document.createElement("a")
link.href = URL.createObjectURL(blob)
link.download = currentDict.key.replace(".png", "") + (getSelectedResolution() == Resolution.hd ? "-hd" : (getSelectedResolution() == Resolution.uhd ? "-uhd" : "-sd")) + ".png"
// then click
link.click()
})
}
// runs on dblclick
function copyPart(event) {
event.preventDefault()
// blobbage
unrotatePreviewCanvas().convertToBlob().then(blob => {
navigator.clipboard.write([new ClipboardItem({'image/png' : blob})]);
})
}
let currentSearchTerm = ""
let currentSearchIndex = 0
let currentSearchResults = []
let currentSearchSelectedIndex = 0
function searchCurrentCombo(name, exact = false) {
if (!currentCombo) return {
result: undefined,
results: 0,
current: 0
}
if (name == currentSearchTerm) {
currentSearchIndex++
} else {
currentSearchIndex = 0
}
currentSearchTerm = name
let results
if (exact) results = currentCombo.plist.filter(dict => dict.key == name)
else results = currentCombo.plist.filter(dict => dict.key.toLowerCase().includes(name.toLowerCase()))
return {
result: results[currentSearchIndex % results.length],
results: results.length,
current: currentSearchIndex % results.length
}
}
function populateSelectionFromURL() {
let hash = new URL(window.location.href).hash
if (hash == "") return
let sheet = null
let resolution = null
let version = null
let split = hash.split(/(?=[#@\-!])/)
console.log(split)
for (let part of split) {
let term = part.charAt(0)
part = part.substring(1)
if (term == "#") {
// sheet
sheet = part
continue
}
if (term == "-") {
// resolution
resolution = eval(`Resolution.${part}`) ?? Resolution.uhd
continue
}
if (term == "@") {
// version
version = eval(`Version.V${part.replace(/\./g, "")}`) ?? Version.V22081
continue
}
if (term == "!") {
// selected dict
console.log(part.replace("_", "/"))
currentDict = searchCurrentCombo(part.replace("+", "/").replaceAll(",", "-")).result
console.log(currentDict)
if (currentDict) currentDictLocked = true
continue
}
}
if (version != null) document.querySelector("#version-select").value = version
if (sheet != null) document.querySelector("#sheet-select").value = sheet
if (resolution != null) document.querySelector("#quality-select").value = resolution
}
// -----------------------------------------------------------------------------
// allow top level await
(async () => {
document.querySelector("#version-select").addEventListener("change", () => {populateSheetSelect(); updateCurrentCombo()})
document.querySelector("#sheet-select").addEventListener("change", () => {updateCurrentCombo()})
document.querySelector("#quality-select").addEventListener("change", () => {updateCurrentCombo()})
document.querySelector("#canvas").addEventListener("mousemove", tickCursor)
if (window.innerWidth > 700) { // consistent with the css (normally id just use user agent)
document.querySelector("#canvas").addEventListener("dblclick", downloadPart)
document.querySelector("#canvas").addEventListener("contextmenu", copyPart)
}
document.querySelector("#canvas").addEventListener("click", event => {
// returns whether the cursor is hovering over something
let ret = tickCursor(event)
if (ret) {
currentDictLocked = !currentDictLocked
checkShouldAnimate(true)
}
// if not hovering over anything, unlock
else currentDictLocked = false
})
document.querySelector("#animate").addEventListener("change", () => {
checkShouldAnimate(false)
})
document.querySelector("#animate-speed").addEventListener("input", () => {
animationSpeed = Number(document.querySelector("#animate-speed").value)
})
document.querySelector("#copy").addEventListener("click", () => {
navigator.clipboard.writeText(currentDict.key)
})
document.querySelector("#copy-url").addEventListener("click", () => {
let resolutionString
if (currentCombo.resolution == Resolution.hd) resolutionString = "-hd"
else if (currentCombo.resolution == Resolution.sd) resolutionString = "-sd"
else if (currentCombo.resolution == Resolution.uhd) resolutionString = "-uhd"
if (currentCombo.name == "GJ_WebSheet") resolutionString = ""
let versionString
if (currentCombo.version == Version.V2113) versionString = "2.113"
else if (currentCombo.version == Version.V2204) versionString = "2.204"
else if (currentCombo.version == Version.V22073) versionString = "2.2073"
else if (currentCombo.version == Version.V22074) versionString = "2.2074"
else if (currentCombo.version == Version.V2208) versionString = "2.208"
else if (currentCombo.version == Version.V22081) versionString = "2.2081"
else if (currentCombo.version == Version.V2209) versionString = "2.209"
else if (currentCombo.version == Version.VLitePinkMoreGames) versionString = "LitePinkMoreGames"
else if (currentCombo.version == Version.VGeode4100) versionString = "Geode4.10.0"
else if (currentCombo.version == Version.VGeode520) versionString = "Geode5.2.0"
else if (currentCombo.version == Version.VGeode553) versionString = "Geode5.5.3"
else if (currentCombo.version == Version.VGDWeb1) versionString = "GDWeb1"
let urlSafeKey = currentDict ? `!${currentDict.key.replace("/", "+").replaceAll("-", ",")}` : ""
navigator.clipboard.writeText(
`${window.location.protocol}//${window.location.host}${window.location.pathname}#${currentCombo.name}${resolutionString}@${versionString}${urlSafeKey}`
)
})
document.querySelector("#copy-image").addEventListener("click", () => {
copyPart({ preventDefault: () => {} })
})
document.querySelector("#download").addEventListener("click", () => {
downloadPart({ preventDefault: () => {} })
})
window.addEventListener("resize", updateCanvasSize)
updateCanvasSize()
let searchBar = document.querySelector("#search-bar")
let searchDropdown = document.querySelector("#search-dropdown")
// handle arrow key navigation in search results
searchBar.addEventListener("keydown", event => {
if (event.code === "ArrowDown" || event.code === "ArrowUp") {
if (currentSearchResults.length === 0) return
event.preventDefault()
if (event.code === "ArrowDown") {
currentSearchSelectedIndex = (currentSearchSelectedIndex + 1) % currentSearchResults.length
} else {
currentSearchSelectedIndex = (currentSearchSelectedIndex - 1 + currentSearchResults.length) % currentSearchResults.length
}
currentDict = currentSearchResults[currentSearchSelectedIndex]
currentDictLocked = true
updateInfoAndPreview()
document.querySelectorAll("#search-dropdown li").forEach((item, i) => {
item.classList.toggle("selected", i === currentSearchSelectedIndex)
})
// scroll selected item into view
document.querySelectorAll("#search-dropdown li")[currentSearchSelectedIndex].scrollIntoView({ block: "nearest" })
}
})
// use keyup instead of input to capture enter key as well
searchBar.addEventListener("keyup", event => {
let searchTerm = searchBar.value
// unrelated key pressed
if (!event.code.startsWith("Key") && event.code != "Enter" && event.code != "Backspace") {
return
}
if (searchTerm == "") {
currentDictLocked = false
currentSearchResults = []
currentSearchSelectedIndex = 0
searchDropdown.style.display = "none"
searchDropdown.innerHTML = ""
document.querySelector("#search-info").innerText = "Type to search..."
return
}
// get all matching results
currentSearchResults = currentCombo.plist.filter(dict => dict.key.toLowerCase().includes(searchTerm.toLowerCase()))
if (currentSearchResults.length === 0) {
currentSearchSelectedIndex = 0
document.querySelector("#search-info").innerText = `No results!`
searchDropdown.style.display = "none"
searchDropdown.innerHTML = ""
return
}
// populate dropdown
searchDropdown.innerHTML = ""
currentSearchSelectedIndex = 0
currentSearchResults.forEach((dict, index) => {
let li = document.createElement("li")
li.textContent = dict.key
li.dataset.index = index
li.addEventListener("mouseover", (e) => {
currentSearchSelectedIndex = index
currentDict = dict
currentDictLocked = true
updateInfoAndPreview()
document.querySelectorAll("#search-dropdown li").forEach((item, i) => {
item.classList.toggle("selected", i === index)
})
})
li.addEventListener("click", (e) => {
e.preventDefault()
e.stopPropagation()
currentSearchSelectedIndex = index
currentDict = dict
currentDictLocked = true
updateInfoAndPreview()
document.querySelectorAll("#search-dropdown li").forEach((item, i) => {
item.classList.toggle("selected", i === index)
})
})
searchDropdown.appendChild(li)
})
// select first result by default
currentDict = currentSearchResults[0]
currentDictLocked = true
updateInfoAndPreview()
searchDropdown.children[0].classList.add("selected")
searchDropdown.style.display = "block"
document.querySelector("#search-info").innerText = `${currentSearchResults.length} result${currentSearchResults.length !== 1 ? "s" : ""}`
})
window.addEventListener("keydown", event => {
if (event.code == "KeyF" && (event.ctrlKey || event.metaKey)) {
event.preventDefault()
searchBar.value = ""
currentSearchResults = []
currentSearchSelectedIndex = 0
searchDropdown.style.display = "none"
searchDropdown.innerHTML = ""
document.querySelector("#search-info").innerText = "Type to search..."
searchBar.dataset.state = "active"
searchBar.focus()
}
if (event.code == "Escape") {
searchBar.value = ""
currentSearchResults = []
currentSearchSelectedIndex = 0
searchDropdown.style.display = "none"
searchDropdown.innerHTML = ""
searchBar.dataset.state = ""
searchBar.blur()
}
})
// hide search when clicking outside
window.addEventListener("click", event => {
if (event.target !== searchBar && event.target !== searchDropdown && !searchDropdown.contains(event.target)) {
searchBar.value = ""
currentSearchResults = []
currentSearchSelectedIndex = 0
searchDropdown.style.display = "none"
searchDropdown.innerHTML = ""
searchBar.dataset.state = ""
searchBar.blur()
}
})
populateSelectionFromURL() // version + resolution before inital select populate
populateSheetSelect()
populateSelectionFromURL() // sheet after populating options
await updateCurrentCombo() // currentCombo after being set
populateSelectionFromURL() // currentDict after combo set
if (currentDict) updateInfoAndPreview()
draw()
})()