-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_builder.html
More file actions
1441 lines (1268 loc) · 54.1 KB
/
Copy pathmatrix_builder.html
File metadata and controls
1441 lines (1268 loc) · 54.1 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Leds System Matrix Builder & Editor</title>
<!-- Modern Typography -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;600;700&family=JetBrains+Mono:wght@400;600&display=swap" rel="stylesheet">
<style>
:root {
--bg-gradient: linear-gradient(135deg, #0a0816 0%, #120e2b 100%);
--panel-bg: rgba(255, 255, 255, 0.03);
--panel-border: rgba(255, 255, 255, 0.08);
--text-primary: #e2e8f0;
--text-secondary: #94a3b8;
--accent: #8b5cf6;
--accent-glow: rgba(139, 92, 246, 0.4);
--accent-green: #10b981;
--accent-red: #ef4444;
--cell-bg-empty: rgba(255, 255, 255, 0.05);
--cell-border: rgba(255, 255, 255, 0.1);
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: 'Outfit', sans-serif;
background: var(--bg-gradient);
color: var(--text-primary);
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
padding: 2rem;
overflow-x: hidden;
}
header {
margin-bottom: 2rem;
text-align: center;
}
h1 {
font-size: 2.5rem;
font-weight: 700;
background: linear-gradient(90deg, #a78bfa, #8b5cf6, #3b82f6);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
margin-bottom: 0.5rem;
letter-spacing: -0.05em;
}
p.subtitle {
color: var(--text-secondary);
font-size: 1.1rem;
}
.container {
display: flex;
flex-direction: row;
gap: 2.5rem;
max-width: 1400px;
width: 100%;
justify-content: center;
align-items: flex-start;
}
/* Editor Section */
.editor-section {
background: var(--panel-bg);
border: 1px solid var(--panel-border);
border-radius: 24px;
padding: 2rem;
backdrop-filter: blur(20px);
box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
max-width: 750px;
}
/* Dimensions Settings */
.settings-bar {
display: flex;
gap: 1.5rem;
margin-bottom: 1.5rem;
background: rgba(0, 0, 0, 0.2);
padding: 1rem 1.5rem;
border-radius: 16px;
border: 1px solid rgba(255, 255, 255, 0.05);
width: 100%;
justify-content: center;
align-items: center;
}
.setting-group {
display: flex;
align-items: center;
gap: 0.75rem;
}
.setting-group label {
font-size: 0.85rem;
font-weight: 600;
color: var(--text-secondary);
text-transform: uppercase;
letter-spacing: 0.05em;
}
.setting-input {
width: 70px;
background: rgba(255, 255, 255, 0.05);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 8px;
padding: 0.4rem 0.6rem;
color: var(--text-primary);
font-family: 'JetBrains Mono', monospace;
font-weight: 600;
text-align: center;
outline: none;
transition: all 0.15s ease;
}
.setting-input:focus {
border-color: var(--accent);
box-shadow: 0 0 0 2px var(--accent-glow);
}
.toolbar {
display: flex;
gap: 0.75rem;
margin-bottom: 1.5rem;
width: 100%;
justify-content: center;
flex-wrap: wrap;
}
button {
font-family: 'Outfit', sans-serif;
font-weight: 600;
font-size: 0.9rem;
padding: 0.6rem 1.2rem;
border-radius: 12px;
border: 1px solid rgba(255, 255, 255, 0.1);
background: rgba(255, 255, 255, 0.05);
color: var(--text-primary);
cursor: pointer;
transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
display: flex;
align-items: center;
gap: 0.5rem;
}
button:hover {
background: rgba(255, 255, 255, 0.1);
border-color: rgba(255, 255, 255, 0.2);
transform: translateY(-1px);
}
button.primary {
background: var(--accent);
border-color: transparent;
box-shadow: 0 4px 14px var(--accent-glow);
}
button.primary:hover {
background: #7c3aed;
box-shadow: 0 6px 20px var(--accent-glow);
}
/* Grid */
.grid-container {
position: relative;
background: rgba(0, 0, 0, 0.2);
padding: 0.75rem;
border-radius: 16px;
border: 1px solid rgba(255, 255, 255, 0.05);
width: 100%;
max-width: 100%;
overflow: auto;
}
.matrix-grid {
display: grid;
gap: 3px;
justify-content: center;
margin: 0 auto;
}
.cell {
width: 32px;
height: 32px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 6px;
border: 1px solid var(--cell-border);
background: var(--cell-bg-empty);
transition: all 0.15s ease;
}
.cell input {
width: 100%;
height: 100%;
border: none;
background: transparent;
color: var(--text-primary);
font-family: 'JetBrains Mono', monospace;
font-size: 0.8rem;
font-weight: 600;
text-align: center;
outline: none;
pointer-events: none;
}
/* Hide HTML5 arrows */
.cell input::-webkit-outer-spin-button,
.cell input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
.cell input[type=number] {
-moz-appearance: textfield;
}
.cell:focus-within {
box-shadow: 0 0 0 2px var(--accent);
border-color: transparent;
}
.cell.filled {
box-shadow: inset 0 0 4px rgba(255, 255, 255, 0.3);
border-color: rgba(255, 255, 255, 0.2);
}
.cell.duplicate {
animation: pulse-red 1.5s infinite;
border-color: var(--accent-red) !important;
}
@keyframes pulse-red {
0% { box-shadow: 0 0 0 0px rgba(239, 68, 68, 0.7); }
70% { box-shadow: 0 0 0 6px rgba(239, 68, 68, 0); }
100% { box-shadow: 0 0 0 0px rgba(239, 68, 68, 0); }
}
/* Grid Headers */
.grid-header-cell {
width: 32px;
height: 32px;
display: flex;
align-items: center;
justify-content: center;
font-family: 'JetBrains Mono', monospace;
font-size: 0.7rem;
font-weight: 600;
color: var(--text-secondary);
user-select: none;
opacity: 0.6;
}
.grid-header-cell.corner {
border: none;
background: transparent;
}
.grid-header-cell.col-header {
border-bottom: 1px solid rgba(255, 255, 255, 0.08);
margin-bottom: 2px;
}
.grid-header-cell.row-header {
border-right: 1px solid rgba(255, 255, 255, 0.08);
justify-content: flex-end;
padding-right: 6px;
margin-right: 2px;
}
.cell.drag-over {
border-color: var(--accent) !important;
box-shadow: 0 0 8px var(--accent-glow);
transform: scale(1.05);
}
.cell.selected {
border: 2px dashed #f59e0b !important;
box-shadow: 0 0 8px rgba(245, 158, 11, 0.4);
}
/* Sidebar Output Section */
.output-section {
flex: 1;
max-width: 600px;
display: flex;
flex-direction: column;
gap: 1.5rem;
width: 100%;
}
.card {
background: var(--panel-bg);
border: 1px solid var(--panel-border);
border-radius: 20px;
padding: 1.5rem;
backdrop-filter: blur(20px);
}
.card h2 {
font-size: 1.25rem;
font-weight: 600;
margin-bottom: 1rem;
display: flex;
align-items: center;
justify-content: space-between;
color: #f1f5f9;
}
.code-block {
position: relative;
background: rgba(0, 0, 0, 0.4);
border-radius: 12px;
border: 1px solid rgba(255, 255, 255, 0.05);
padding: 1rem;
overflow-x: auto;
max-height: 250px;
}
.code-block.hex {
max-height: 120px;
}
code {
font-family: 'JetBrains Mono', monospace;
font-size: 0.8rem;
line-height: 1.4;
color: #cbd5e1;
white-space: pre;
}
.copy-btn {
background: rgba(255, 255, 255, 0.05);
padding: 0.4rem 0.8rem;
font-size: 0.8rem;
border-radius: 8px;
}
.copy-btn.success {
background: var(--accent-green);
color: #0f172a;
}
/* Info / validation details */
.validation-card {
background: rgba(255, 255, 255, 0.02);
}
.status-pill {
display: inline-flex;
align-items: center;
padding: 0.25rem 0.75rem;
border-radius: 9999px;
font-size: 0.8rem;
font-weight: 600;
}
.status-pill.valid {
background: rgba(16, 185, 129, 0.15);
color: #34d399;
}
.status-pill.error {
background: rgba(239, 68, 68, 0.15);
color: #f87171;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
margin-top: 1rem;
}
.stat-item {
background: rgba(0, 0, 0, 0.15);
padding: 0.75rem;
border-radius: 10px;
text-align: center;
border: 1px solid rgba(255, 255, 255, 0.02);
}
.stat-label {
font-size: 0.75rem;
color: var(--text-secondary);
margin-bottom: 0.25rem;
}
.stat-value {
font-size: 1.2rem;
font-weight: 700;
color: var(--accent);
}
/* Checklist Alert */
.missing-list {
margin-top: 1rem;
padding: 1rem;
background: rgba(239, 68, 68, 0.08);
border: 1px solid rgba(239, 68, 68, 0.15);
border-radius: 12px;
font-size: 0.85rem;
color: #fca5a5;
line-height: 1.5;
}
.success-banner {
margin-top: 1rem;
padding: 1rem;
background: rgba(16, 185, 129, 0.08);
border: 1px solid rgba(16, 185, 129, 0.15);
border-radius: 12px;
font-size: 0.9rem;
font-weight: 600;
color: #34d399;
text-align: center;
}
/* Tooltip style helper info */
.helper-text {
font-size: 0.8rem;
color: var(--text-secondary);
margin-top: 0.75rem;
text-align: center;
line-height: 1.4;
}
footer {
margin-top: 3rem;
color: var(--text-secondary);
font-size: 0.85rem;
}
</style>
</head>
<body>
<header>
<h1>Leds System Matrix Builder</h1>
<p class="subtitle">Design, validate, and vertically mirror your custom LED matrices</p>
</header>
<div class="container">
<!-- Editor panel -->
<div class="editor-section">
<!-- Dimensions Bar -->
<div class="settings-bar">
<div class="setting-group">
<label for="widthInput">Width</label>
<input type="number" id="widthInput" class="setting-input" value="18" min="1" max="50" onchange="resizeGrid()">
</div>
<div class="setting-group">
<label for="heightInput">Height</label>
<input type="number" id="heightInput" class="setting-input" value="14" min="1" max="50" onchange="resizeGrid()">
</div>
</div>
<div class="toolbar">
<button class="primary" onclick="mirrorVertical()" title="Mirror grid vertically">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><path d="M12 2v20M17 5H7M19 12H5M17 19H7"/></svg>
Mirror Vertical
</button>
<button id="btnSym" onclick="loadDefaultTemplate('symmetric')" title="Load 14x18 Symmetric matrix (unmirrored)">
Load Symmetric (14x18)
</button>
<button id="btnMirr" onclick="loadDefaultTemplate('mirrored')" title="Load 14x18 Symmetric mirrored matrix">
Load Mirrored (14x18)
</button>
<button onclick="clearGrid()" title="Reset all cells to -1">
Clear Grid
</button>
<button id="btnUndo" onclick="undo()" disabled style="opacity: 0.4; border-color: rgba(255, 255, 255, 0.1);" title="Undo last action (Ctrl+Z)">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><path d="M3 7v6h6M21 17a9 9 0 00-9-9 9 9 0 00-6 2.3L3 13"/></svg>
Undo
</button>
<button id="btnClearSel" onclick="clearSelection()" title="Deselect all selected cells" style="display: none; border-color: rgba(245, 158, 11, 0.2); background: rgba(245, 158, 11, 0.05); color: #f59e0b;">
Clear Selection
</button>
</div>
<div class="grid-container">
<div class="matrix-grid" id="matrixGrid">
<!-- Javascript will generate cells dynamically -->
</div>
</div>
<div class="helper-text">
<b>Ctrl + Click</b> to select multiple cells, drag selection to move. <b>Arrow Keys</b> to navigate.
</div>
</div>
<!-- Sidebar / Outputs -->
<div class="output-section">
<!-- Validation and Stats -->
<div class="card validation-card">
<h2>
Layout Validation
<span id="validationStatus" class="status-pill valid">All Valid</span>
</h2>
<div class="stats-grid">
<div class="stat-item">
<div class="stat-label">Mapped LEDs</div>
<div class="stat-value" id="statMapped">0 / 0</div>
</div>
<div class="stat-item">
<div class="stat-label">Max LED Index</div>
<div class="stat-value" id="statMaxIndex">-1</div>
</div>
<div class="stat-item">
<div class="stat-label">Grid Size</div>
<div class="stat-value" id="statGridDim">18 × 14</div>
</div>
</div>
<div id="validationWarning" style="color: var(--accent-red); font-size: 0.85rem; margin-top: 0.75rem; display: none;">
⚠️ Warning: Duplicate LED numbers found (highlighted in red).
</div>
<div id="missingContainer" class="missing-list" style="display: none;">
<b>Missing LED numbers:</b> <span id="missingIndices">None</span>
</div>
<div id="successBanner" class="success-banner" style="display: none;">
Checklist Complete! All LEDs 0 to <span id="successMax">0</span> are mapped exactly once.
</div>
</div>
<!-- Code Output 1: Python Tuple -->
<div class="card">
<h2>
Python Matrix Tuple (LED1_MATRIX)
<button class="copy-btn" onclick="copyCode('pythonCode', this)">Copy</button>
</h2>
<div class="code-block">
<code id="pythonCode">LED1_MATRIX = (...)</code>
</div>
</div>
<!-- Code Output 2: index_table hex string -->
<div class="card">
<h2>
index_table Hex String
<button class="copy-btn" onclick="copyCode('hexCode', this)">Copy</button>
</h2>
<div class="code-block hex">
<code id="hexCode">ffff...</code>
</div>
</div>
<!-- Import Card -->
<div class="card">
<h2>Import Matrix from Python Tuple</h2>
<div style="display: flex; flex-direction: column; gap: 0.75rem; margin-top: 0.5rem;">
<textarea id="importInput" placeholder="Paste your LED1_MATRIX = ((...)) tuple here..." style="width: 100%; height: 90px; background: rgba(0, 0, 0, 0.4); border: 1px solid rgba(255, 255, 255, 0.1); border-radius: 10px; padding: 0.6rem; color: #cbd5e1; font-family: 'JetBrains Mono', monospace; font-size: 0.75rem; resize: vertical; outline: none;"></textarea>
<div style="display: flex; align-items: center; justify-content: space-between; gap: 1rem;">
<button class="primary" onclick="importFromText()" style="padding: 0.5rem 1rem; font-size: 0.85rem;">Import Tuple</button>
<span id="importFeedback" style="font-size: 0.8rem; color: var(--accent-green); display: none;"></span>
</div>
</div>
</div>
</div>
</div>
<footer>
Leds System Exhibition Tool
</footer>
<script>
let gridWidth = 18;
let gridHeight = 14;
const gridCells = [];
const selectedCells = new Set();
const undoStack = [];
let focusStateBackup = null;
// 14x18 symmetric layouts in valid JS array syntax
const unmirroredTemplate = [
[-1, -1, -1, -1, -1, 9, -1, 10, 11, 12, -1, 13, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, 8, -1, -1, -1, -1, -1, -1, -1, 14, -1, -1, -1, -1, -1],
[-1, -1, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, 15, 16, -1, -1, -1],
[-1, 5, -1, 61, 62, 63, -1, -1, -1, -1, -1, 69, 70, 71, -1, 17, -1, -1],
[ 4, -1, -1, -1, -1, -1, 64, 65, 66, 67, 68, -1, -1, -1, -1, -1, 18, -1],
[ 3, -1, 60, 59, -1, -1, -1, -1, -1, -1, -1, -1, 50, 49, -1, -1, 19, -1],
[ 2, -1, -1, -1, 58, 57, -1, -1, -1, -1, 52, 51, -1, -1, -1, -1, 20, -1],
[-1, -1, -1, -1, -1, -1, 56, 55, 54, 53, -1, -1, -1, -1, -1, -1, -1, -1],
[ 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 21, -1],
[ 0, -1, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, -1, 22, -1],
[-1, 35, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 23, -1, -1],
[-1, -1, 34, 33, -1, -1, -1, -1, -1, -1, -1, -1, -1, 25, 24, -1, -1, -1],
[-1, -1, -1, -1, 32, -1, -1, -1, -1, -1, -1, -1, 26, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, 31, -1, 30, 29, 28, -1, 27, -1, -1, -1, -1, -1, -1]
];
const mirroredTemplate = [
[-1, -1, -1, -1, -1, 31, -1, 30, 29, 28, -1, 27, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, 32, -1, -1, -1, -1, -1, -1, -1, 26, -1, -1, -1, -1, -1],
[-1, -1, 34, 33, -1, -1, -1, -1, -1, -1, -1, -1, -1, 25, 24, -1, -1, -1],
[-1, 35, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 23, -1, -1],
[ 0, -1, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, -1, 22, -1],
[ 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 21, -1],
[-1, -1, -1, -1, -1, -1, 56, 55, 54, 53, -1, -1, -1, -1, -1, -1, -1, -1],
[ 2, -1, -1, -1, 58, 57, -1, -1, -1, -1, 52, 51, -1, -1, -1, -1, 20, -1],
[ 3, -1, 60, 59, -1, -1, -1, -1, -1, -1, -1, -1, 50, 49, -1, -1, 19, -1],
[ 4, -1, -1, -1, -1, -1, 64, 65, 66, 67, 68, -1, -1, -1, -1, -1, 18, -1],
[-1, 5, -1, 61, 62, 63, -1, -1, -1, -1, -1, 69, 70, 71, -1, 17, -1, -1],
[-1, -1, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, 15, 16, -1, -1, -1],
[-1, -1, -1, -1, 8, -1, -1, -1, -1, -1, -1, -1, 14, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, 9, -1, 10, 11, 12, -1, 13, -1, -1, -1, -1, -1, -1]
];
function buildGrid(width, height, retainValues = false) {
const gridContainer = document.getElementById('matrixGrid');
// Backup old values if needed
const backup = {};
if (retainValues) {
gridCells.forEach(input => {
const r = input.dataset.row;
const c = input.dataset.col;
backup[`${r}-${c}`] = input.value;
});
}
gridContainer.innerHTML = '';
gridContainer.style.gridTemplateColumns = `32px repeat(${width}, 1fr)`;
gridCells.length = 0;
// 1. Add Corner Header
const corner = document.createElement('div');
corner.className = 'grid-header-cell corner';
gridContainer.appendChild(corner);
// 2. Add Column Headers (0-based indices)
for (let c = 0; c < width; c++) {
const colHeader = document.createElement('div');
colHeader.className = 'grid-header-cell col-header';
colHeader.textContent = c;
gridContainer.appendChild(colHeader);
}
// 3. Add Rows (Row Header + Input Cells)
for (let r = 0; r < height; r++) {
const rowHeader = document.createElement('div');
rowHeader.className = 'grid-header-cell row-header';
rowHeader.textContent = r;
gridContainer.appendChild(rowHeader);
for (let c = 0; c < width; c++) {
const cellDiv = document.createElement('div');
cellDiv.className = 'cell';
const input = document.createElement('input');
input.type = 'number';
input.id = `cell-${r}-${c}`;
input.dataset.row = r;
input.dataset.col = c;
// Restore backup value or default to -1
const backupVal = backup[`${r}-${c}`];
input.value = (backupVal !== undefined) ? backupVal : -1;
// Set pointer-events none so cell wrapper captures drag events
input.style.pointerEvents = 'none';
// Append first so parentElement is set before styling
cellDiv.appendChild(input);
// Update styling on load so restored cells are immediately colored
updateCellStyling(input);
// Drag and Drop support
cellDiv.draggable = true;
let dragCounter = 0;
let shouldFocusOnMouseUp = false;
cellDiv.addEventListener('dragstart', (e) => {
shouldFocusOnMouseUp = false; // Cancel focus on drag start
// If the dragged cell is not in the selection, clear selection and select only this cell
if (!selectedCells.has(input.id)) {
clearSelection();
toggleCellSelection(input);
}
// Prepare multi-drag payload
const payload = {
originId: input.id,
cells: []
};
selectedCells.forEach(id => {
const selInput = document.getElementById(id);
if (selInput) {
payload.cells.push({
id: id,
r: parseInt(selInput.dataset.row),
c: parseInt(selInput.dataset.col),
val: selInput.value
});
}
});
e.dataTransfer.setData('application/json', JSON.stringify(payload));
e.dataTransfer.effectAllowed = 'move';
});
cellDiv.addEventListener('dragenter', (e) => {
dragCounter++;
cellDiv.classList.add('drag-over');
});
cellDiv.addEventListener('dragleave', (e) => {
dragCounter--;
if (dragCounter === 0) {
cellDiv.classList.remove('drag-over');
}
});
cellDiv.addEventListener('dragover', (e) => {
e.preventDefault();
e.dataTransfer.dropEffect = 'move';
});
cellDiv.addEventListener('drop', (e) => {
e.preventDefault();
dragCounter = 0;
cellDiv.classList.remove('drag-over');
try {
const rawData = e.dataTransfer.getData('application/json');
if (!rawData) return;
const payload = JSON.parse(rawData);
const originInput = document.getElementById(payload.originId);
if (!originInput) return;
const r_origin = parseInt(originInput.dataset.row);
const c_origin = parseInt(originInput.dataset.col);
const r_target = parseInt(input.dataset.row);
const c_target = parseInt(input.dataset.col);
const dr = r_target - r_origin;
const dc = c_target - c_origin;
// 1. Validate all target positions are in bounds
let allValid = true;
const moves = [];
payload.cells.forEach(c => {
const r_new = c.r + dr;
const c_new = c.c + dc;
if (r_new < 0 || r_new >= gridHeight || c_new < 0 || c_new >= gridWidth) {
allValid = false;
} else {
moves.push({
sourceId: c.id,
targetId: `cell-${r_new}-${c_new}`,
val: c.val
});
}
});
if (!allValid) {
return; // Silent abort if any cell goes out of bounds
}
// Push undo state before the move
pushState();
// 2. Perform the Move
// Clear old source cells first (if not being overwritten by a new value in the same move)
const targetIdsSet = new Set(moves.map(m => m.targetId));
payload.cells.forEach(c => {
if (!targetIdsSet.has(c.id)) {
const srcEl = document.getElementById(c.id);
if (srcEl) {
srcEl.value = -1;
updateCellStyling(srcEl);
}
}
});
// Write new values
moves.forEach(m => {
const tgtEl = document.getElementById(m.targetId);
if (tgtEl) {
tgtEl.value = m.val;
updateCellStyling(tgtEl);
}
});
clearSelection();
generateCode();
} catch (err) {
console.error("Drop failed: ", err);
}
});
// Disable dragging while focused so text selection works
input.addEventListener('focus', () => {
cellDiv.draggable = false;
input.style.pointerEvents = 'auto';
// Capture snapshot for undo stack before they edit
focusStateBackup = getGridStateSnapshot();
});
input.addEventListener('blur', () => {
cellDiv.draggable = true;
input.style.pointerEvents = 'none';
});
// Mouse interactions for selection / deferred drag-focus
cellDiv.addEventListener('mousedown', (e) => {
if (document.activeElement === input) return;
if (e.ctrlKey || e.metaKey) {
toggleCellSelection(input);
e.preventDefault();
} else {
if (selectedCells.has(input.id)) {
shouldFocusOnMouseUp = true;
} else {
clearSelection();
input.style.pointerEvents = 'auto';
input.focus();
input.select();
}
}
});
cellDiv.addEventListener('mouseup', (e) => {
if (shouldFocusOnMouseUp) {
clearSelection();
input.style.pointerEvents = 'auto';
input.focus();
input.select();
shouldFocusOnMouseUp = false;
}
});
// Event Listeners for input change
input.addEventListener('input', () => {
if (focusStateBackup) {
undoStack.push(focusStateBackup);
if (undoStack.length > 50) undoStack.shift();
focusStateBackup = null;
updateUndoButtonUI();
}
updateCellStyling(input);
generateCode();
});
input.addEventListener('keydown', handleKeyNavigation);
gridContainer.appendChild(cellDiv);
gridCells.push(input);
}
}
document.getElementById('statGridDim').textContent = `${width} × ${height}`;
generateCode();
}
// Triggered when width or height inputs change
function resizeGrid() {
const w = parseInt(document.getElementById('widthInput').value);
const h = parseInt(document.getElementById('heightInput').value);
if (isNaN(w) || w < 1) return;
if (isNaN(h) || h < 1) return;
pushState();
gridWidth = w;
gridHeight = h;
// Show/hide templates buttons if dimension is not 14x18
const btnSym = document.getElementById('btnSym');
const btnMirr = document.getElementById('btnMirr');
if (gridWidth === 18 && gridHeight === 14) {
btnSym.style.display = 'inline-flex';
btnMirr.style.display = 'inline-flex';
} else {
btnSym.style.display = 'none';
btnMirr.style.display = 'none';
}
buildGrid(gridWidth, gridHeight, true);
}
// Set colors and classes on cells based on input value
function updateCellStyling(input) {
const val = parseInt(input.value);
const parent = input.parentElement;
if (!parent) return;
if (isNaN(val) || val < 0) {
parent.style.background = '';
parent.classList.remove('filled');
} else {
parent.classList.add('filled');
// Dynamic hue mapping based on index value
const hue = (val * 360 / 72) % 360;
parent.style.background = `hsl(${hue}, 75%, 28%)`;
}
}
// Handle Arrow key navigation
function handleKeyNavigation(e) {
const row = parseInt(this.dataset.row);
const col = parseInt(this.dataset.col);
let nextInput = null;
switch(e.key) {
case 'ArrowUp':
nextInput = document.getElementById(`cell-${(row - 1 + gridHeight) % gridHeight}-${col}`);
break;
case 'ArrowDown':
nextInput = document.getElementById(`cell-${(row + 1) % gridHeight}-${col}`);
break;
case 'ArrowLeft':
nextInput = document.getElementById(`cell-${row}-${(col - 1 + gridWidth) % gridWidth}`);
break;
case 'ArrowRight':
nextInput = document.getElementById(`cell-${row}-${(col + 1) % gridWidth}`);
break;
case 'Backspace':
if (this.value === '') {
this.value = -1;
updateCellStyling(this);
generateCode();
e.preventDefault();
}
break;
}
if (nextInput) {
nextInput.focus();
nextInput.select();
e.preventDefault();
}
}
// Reset all grid inputs to -1
// Reset all grid inputs to -1
function clearGrid(shouldPush = true) {
if (shouldPush) pushState();
gridCells.forEach(input => {
input.value = -1;
updateCellStyling(input);
});
generateCode();
}
// Load pre-made templates
function loadDefaultTemplate(type) {
if (gridWidth !== 18 || gridHeight !== 14) {
alert('Dimensions must be 18x14 to load the default templates.');
return;
}
pushState();
clearGrid(false);
const template = type === 'symmetric' ? unmirroredTemplate : mirroredTemplate;
for (let r = 0; r < template.length; r++) {
for (let c = 0; c < template[r].length; c++) {
const val = template[r][c];
const input = document.getElementById(`cell-${r}-${c}`);
if (input) {
input.value = val;
updateCellStyling(input);
}
}
}
generateCode();
}
// Mirror the grid vertically in-place
function mirrorVertical() {
pushState();
const tempGrid = [];
for (let r = 0; r < gridHeight; r++) {
tempGrid[r] = [];
for (let c = 0; c < gridWidth; c++) {
const input = document.getElementById(`cell-${r}-${c}`);