-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharMatrix.js
1792 lines (1578 loc) · 54.1 KB
/
arMatrix.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
// To do list:
// - improve performance at higher resolutions by drawing the pixel grid using WEBGL (https://npm.io/package/pixel-grid, https://stackoverflow.com/questions/31892414/draw-each-pixel-separately-webgl, https://pixijs.com/)
// - highlight corresponding color in color palete when hovering on pixel, and inverse
// - stroke width control, replace cursor by square of corresponding size and pos when hovering on canvas
// - layers
// - "pixel perfect" drawing tool (smooth curves)
// - striaght line tool, toggleable tools selection panel on lower right, use line tool logic to fill gaps when free drawing
// - cut tool (keep part of matrix)
// - fill tool (change all neighboring pixels of same value at once)
// - rect tool, circle tool
// - select -> copy/cut -> move tool
// - hue shifting, somehow
// - turn buttons & palette into objects of their respective classes, better for referencing each button (instead of using label)
// - add GUI scaling controls (linearly move uipx inside a fixed range)
// - optimize cursor hover checks
// Global variables
// Inner logic
p5.disableFriendlyErrors = false; // Simple performance optimization
document.addEventListener("keydown", (e) => e.ctrlKey && e.preventDefault()); // Prevent default ctrl + key functionality
document.addEventListener("contextmenu", (e) => e.preventDefault()); // Prevent context menu popup on right click
let verbose = false; // If true, prints m whenever a change is made to it
let a; // Placeholder for arrays
let m = []; // Matrix
let mw = 16; // Matrix width
let mh = 16; // Matrix height
let dv = 0; // Default matrix value
let mEmpty = true; // Matrix contains only dv
let mod = false; // m was modified during this frame
let mHist = []; // Matrix history
let cm = 0; // Current matrix, index for mh
let mCopy; // Placeholder for deep-copy of m
let pxChange = false; // m was modified on previous frames, while holding LMB
let button;
let valCol;
let val; // General iteration/indexing purposes
let i; // General iteration/indexing purposes
let x; // General matrix element iteration purposes
let y; // General matrix element iteration purposes
let col; // General cPalette element iteration purposes
let isSelected; // General selected element purposes
// General visuals
let xpx1;
let xpx2;
let ypx1;
let ypx2; // Pixel coordinates of corner of matrix element
let w; // Sketch width (set in setup)
let h; // Sketch height (set in setup)
let pxd = 1; // Pixel density
let pxwh = 20; // True pixels of width and height of each square at the default zoom level
let zoom = 1.0; // Amount of zoom, default 1
let minZ = 1/pxwh; // Lower bound of allowed zoom range
let maxZ = 5; // Upper bound of allowed zoom range
let zoomInKb; // Holding + key
let zoomOutKb; // Holding - key
let zoomInW; // Mouse wheel scrolling up
let zoomOutW; // Mouse wheel scrolling down
let s = pxwh*zoom; // Actual size (width and height) of each square
let mwpx = s*mw; // Width of matrix in pixels
let mhpx = s*mh; // Height of matrix in pixels
let hRef; // Horizontal reference pixel
let vRef; // Vertical reference pixel
let hPan = 0; // Horizontal camera pan
let vPan = 0; // Vertical camera pan
let bgc = 35; // Background color
let tca = [255, 69]; // Text color and alpha (constant)
let lineca = [120, 255]; // Gridline color (constant) and alpha (based on zoom, dynamic)
let linew = 1; // Gridline width (based on zoom, dynamic)
let totColors = 32; // Palette size
let colorsPerRow = 16; // Colors per palette row
let cPaletteRows; // Number of rows on color palette
let row; // Palette row index
let rC; // Random color
let nNeg = 0; // Number of negative values, must be at least 0 and at most totColors - 1 (constrained in setup)
let cPaletteFixed = [[-1, [222, 82, 82], [222, 82, 82], 0, 0, false],
[ 0, [bgc, bgc, bgc], [bgc, bgc, bgc], 0, 0, false],
]; // Index based value-color array, [0:value, 1:cPick, 2:tempPick, 3:buttonx, 4:buttony, 5:cursorOnTop] (value and position data set in setup)
let nFixedColors = cPaletteFixed.length; // Number of preselected colors
let cPalette = []; // Holds all color palette data
// Interactivity & related visuals
let uipx = 40; // UI button length
let uibcpx = uipx/10; // UI curved border amount
let uisdpx = uipx/10; // UI shadow X, Y displacement
let uibc = 80; // UI button color
let uisc = bgc/2; // UI shadow color
let uihc = 200; // UI highlight color
let uihcoff = uihc*3/4; // UI highlight color (tool disabled, uihcoff < uihc)
let hcFillValue; // Placeholder variable for either uihc or uihcoff, accordingly
let uipscl = 0.6; // Scale of palette buttons in relation to the rest of the buttons
let uipxp = uipx*uipscl; // Palette button length
let undoredo = true; // Undo or Redo was used during the previous frame, init value must be true
let clickButtonArray = [[0, 0, undo, undoButton, null, 'undo', 'ctrl + Z', false],
[0, 0, redo, redoButton, null, 'redo', 'ctrl + shift + Z', false],
[0, 0, reCenter, reCenterButton, null, 'recenter', 'R', false],
[0, 0, upScale, upScaleButton, 2, 'upscale', '', false],
[0, 0, null, loadButton, null, 'load', '', false], // Special case, uses hidden DOM element
[0, 0, saveFile, saveButton, null, 'save', 'ctrl + S', false],
[0, 0, openSaveMenu, savePNGButton, null, 'save image', 'ctrl + shift + S', false], // Triggers save interface, not saving itself
]; // Holds all relevant clickable button data in the form [[0:xPos, 1:yPos, 2:function, 3:drawFunction, 4:functionArgs, 5:label, 6:kbShortcutLabel, 7:cursorOnTop] for each button]. Take care if modifying labels, as they are used to reference buttons in the code. Mostly set in setup.
let bx;
let by;
let on;
let bdraw; // Placeholders for button information (x, y, cursorOnTop, func)
let nClickButtons = clickButtonArray.length; // Number of clickable buttons
let ctrl = false; // Ctrl key is being held down
let shift = false; // Shift key is being held down
let cPick; // Color picked from color wheel
let cHover; // Color under pointer from color wheel
let cPicking = false; // Currently picking a color using the color wheel
let cPickedHolding = false; // cPicking just turned false because of mouse press, currently holding down mouse button
let clickedOnColor = false; // On mouse click, some color from the palette was clicked
let cPickingIndex = null; // Index (cPalette) of color being currently picked
let cSelectIndex; // Index (cPalette) of currently selected color (set in setup, defaults to 1 if possible)
let vMod = false; // Currently modifying luminosity value on color wheel
let v = 0.8; // Luminosity value
let pxIndex; // Index of current pixel while hovering over color wheel
let cWheel; // Holds the actual wheel image, precomputed in setup
let cWx; // Color wheel x pos (set in setup)
let cWy; // Color wheel y pos (set in setup)
let cWd = uipx*4; // Color wheel diameter
let cWr = cWd/2; // Color Wheel radius
let onGUI; // Mouse pointer currently close to GUI elements (palette, tools, etc.)
let cPalettew;
let cPaletteh;
let clickButtonsw;
let clickButtonsh;
let cWheelw;
let cWheelh; // Size (width and height) of GUI element (set in setup)
let onPalette;
let onClickButtons;
let onWheel; // Mouse pointer currently close to a particular GUI element section
let numkeyType; // 48 or 96, depending on which numeric keys are being used
let mouseIndex; // Holds output from mousePosToMatrixIndex()
let wheelDelta; // temporary holder of last mouse wheel scroll value
let onHelp = false; // Mouse pointer currently close of help GUI element (set in mouseOnGUI())
let onHelpButton = false; // Mouse pointer currently on top of help button
let helping = true; // Currently showing help overlay
let helped = false; // Was showing help overlay on previous frame
let helpbx; // Help button x pos (set in setup)
let helpby; // Help button y pos (set in setup)
// cWheel shader
let radius;
let ww;
let wh;
let cx;
let cy;
let rx;
let ry;
let cH;
let cS; // cWheel properties
// Save image menu
let saveMenuing = false; // Currently viewing save menu
let saveMenuOpened = false; // Save menu opened on current frame
let saveMenuGUIw;
let saveMenuGUIh; // Width and height of the entire GUI element and surroundings (set in setup)
let saveMenuGUIx;
let saveMenuGUIy; // Center xpos and ypos of GUI element
let onSaveMenu; // Mouse pointer currently close to GUI element
let menupxw;
let menupxh; // Save menu window xpos, ypos, width and height (set in setup)
let menuDeltah; // Save menu y displacement from center (set in setup)
let stillSaveMenuing; // Didn't exit menu on current frame
let uimbc = bgc*1.5; // UI menu background color
let savePxScale = 1;
let saveCrop = false;
let saveTrans = false; // Function parameters for savePNG
let tempScale; // Holds str version of savePxScale
let biphasicToggle = false; // Alternates between true and false, half of the time each, every biphasicPeriod frames
let biphasicPeriod = 30;
let backToMinScale = true; // Deleted all digits or scrolled down, reached 1
let onScaleUp = false;
let onScaleDown = false;
let onCrop = false;
let onTrans = false;
let onExit = false;
let onMenuSaveButton = false; // Cursor on top of UI element
let maxScale = 30; // Max px scale value, min is always 1
let toggleSize;
let saveMenuButtonx;
let scaley;
let cropy;
let transy;
let scaleUpy;
let scaleDowny;
let scaleH; // Button positions and sizes, toggleSize and saveMenuButtonx shared by scale, crop and trans (set in setup)
let exitx;
let exity; // Exit button xpos and ypos (set in setup), uses toggleSize for side length
let menuSaveButtonx;
let menuSaveButtony; // Save button xpos and ypos (set in setup), uses uipx for side length
// Easter eggs
let keyLogger = '';
let sussybaka = [[[0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,3,1,3,3,3,3,3,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,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492753623189,184.50144927536232,193.20434782608697],[142,221,255],[85,133,255],[199.75174225362704,76.18455582661338,152.11140616501473],[150.9005284952083,20.874223348205735,244.10854020288355],[252.90944461808735,248.2162770367919,225.00816605069883],[167.59287209452427,146.74722210276468,167.81022708209156],[191.4328575542337,159.60288878374573,64.09053259033512],[96.63436767175575,134.1654187485623,107.55441029458666],[24.361707160055634,221.6874326000852,152.42795527909195],[205.30397126994592,181.58552956827495,110.90117980146695],[250.98773588034192,175.31819738325808,74.33822147084932],[44.71887038984821,55.439289249471756,179.36463053711773],[45.386596419687045,102.18607720019261,249.13554663242257],[8.407719482557011,16.56075421805331,152.43616867410256],[164.7284188078402,104.21128359788725,127.51348102030248],[102.10951328395062,160.17409588188372,108.58007582028239],[68.36862073712024,167.19804773513488,218.6453798175691],[11.200218241128354,90.42274612501669,77.0100692311605],[231.1161267773046,129.66020283489306,121.2347336499869],[215.49095731273385,8.891660611813714,123.49638665600297],[235.08568379463958,82.49073395936321,247.67271661428973],[32.028940378949954,39.24550739038877,30.435019806815212],[81.87121947931898,223.44696465782462,82.72482204712192],[70.84689523809735,156.4294860130571,19.114021365667224],[105.56208307156683,226.42116214003835,62.80687762613628],[41.42686260752605,116.3422036577738,106.0687652033917],[109.74388104616881,75.70244560894172,166.98087281221163]]];
function setup() {
// Initialize canvas
w = constrain(windowWidth, 666, windowWidth);
h = constrain(windowHeight, 500, windowHeight);
createCanvas(w, h);
// Misc
smooth();
imageMode(CENTER);
ellipseMode(CENTER);
rectMode(CENTER);
hRef = (w-mwpx)/2;
vRef = (h-mhpx)/2;
// Precompute cWheel
cWheelShader();
// Initialize m
initMatrix();
// Initialize GUI
initGUI();
// Initilize history
mHist.push([deepCopy2D(m), getCurrentPalette()]);
}
function draw() {
background(bgc);
// Update zoom and pan
updateZoom(minZ, maxZ);
updatePan();
// Check if mouse on GUI elements
mouseOnGUI();
// Draw matrix
drawGrid();
// Drawing tools
freeDraw();
// GUI display and interaction (except for mouseClicked, mouseReleased, mouseWheel and keyPressed)
checkCursorHover();
drawClickButtons();
drawColorPalette();
mouseHeldInteractions();
drawColorWheel();
updateHoverColor();
showHelp();
checkIfEmpty();
saveMenu();
// History
mModHandler();
// Debug
// GUIdebug();
}
function drawGrid() {
lineca[1] = 255 * map(zoom, minZ, 1, 0, 1);
linew = constrain(map(zoom, minZ, 1, 0, 1), 0, 1);
for (let y = 0; y < mh; y++) {
for (let x = 0; x < mw; x++) {
xpx1 = hRef + x*s;
ypx1 = vRef + y*s;
xpx2 = xpx1 + s;
ypx2 = ypx1 + s;
stroke(lineca);
strokeWeight(linew);
fill(cPalette[m[y][x] + nNeg][2]);
rect(xpx1, ypx1, s, s);
}
}
}
function initMatrix() {
for (let y = 0; y < mh; y++) {
m[y] = [];
for (let x = 0; x < mw; x++) {
m[y][x] = dv;
}
}
}
function initGUI() {
i = 1;
for (let button of clickButtonArray) {
button[0] = w-uipx/1.25;
button[1] = (i*2-1)*uipx/1.25;
i++;
}
nNeg = constrain(nNeg, 0, totColors - 1);
cSelectIndex = constrain(nNeg+1, nNeg, totColors-1);
for (let i = -nNeg; i < totColors - nNeg; i++) {
if (i < -1 || i > nFixedColors - 2) {
rC = [random(255), random(255), random(255)];
cPalette.push([i, [...rC], [...rC], 0, 0]);
} else {
cPalette.push([i, [...cPaletteFixed[i+1][1]], [...cPaletteFixed[i+1][2]], 0, 0, false]);
}
}
i = 1;
for (let valCol of cPalette) {
row = floor((i-1)/colorsPerRow);
valCol[3] = (uipx/1.25-uipx/2+uipx/2*uipscl)+(i*2-2 - row*2*colorsPerRow)*uipx/1.25*uipscl;
valCol[4] = (uipx/1.25-uipx/2+uipx/2*uipscl)+(row*2)*uipx/1.25*uipscl;
i++;
}
cPaletteRows = floor(totColors/colorsPerRow);
cPalettew = (uipx/1.25-uipx/2)+(colorsPerRow)*2*(uipx/1.25)*uipscl - (uipx/1.25-uipx/2)*uipscl;
cPaletteh = (uipx/1.25-uipx/2)+(cPaletteRows)*2*(uipx/1.25)*uipscl;
clickButtonsw = 2*uipx/1.25;
clickButtonsh = nClickButtons*2*uipx/1.25;
cWheelw = cWd/8 + cWd*1.15;
cWheelh = cWd*1.4;
cWx = cWd/1.725 + uipx/5;
cWy = cWd/1.725 + cPaletteh;
helpbx = uipx/1.25-uipx/2+uipx/2*0.7;
helpby = h - uipx/1.25+uipx/2-uipx/2*0.7;
// Load file button functionality (Special case)
load = createFileInput(loadFile);
load.elt.style = "opacity: 0";
load.size(uipx, uipx);
i = 1;
for (let button of clickButtonArray) {
if (button[5] == 'load') {
load.position(w-uipx/1.25-uipx/2, (i*2-1)*uipx/1.25-uipx/2);
} i++;
}
initSaveMenu();
}
function mModHandler() {
if (frameCount == 1 || mod) {
if (!undoredo) { // If undo or redo, the problem gets adressed in undo() or redo()
cm++;
mHist = mHist.slice(0, cm);
mHist.push([deepCopy2D(m), getCurrentPalette()]);
}
mod = false;
undoredo = false;
pxChange = false;
if (verbose) {
console.log(JSON.stringify([m, getCurrentPalette()]));
}
}
}
function updateZoom(min, max) {
zoomInKb = !saveMenuing && keyIsPressed && (key === '+');
zoomOutKb = !saveMenuing && keyIsPressed && (key === '-');
if (zoomInKb) {
zoom *= 1.04;
} else if (zoomOutKb) {
zoom *= 0.96;
}
if (mouseIsPressed) {
if (mouseButton === CENTER) {
zoom -= 2*zoom*(mouseY-pmouseY)/h;
}
}
if (wheelDelta && !saveMenuing) {
zoomInW = wheelDelta < 0;
zoomOutW = wheelDelta > 0;
if (zoomInW) {
zoom *= 1.15;
} else if (zoomOutW) {
zoom *= 0.85;
}
wheelDelta = 0;
zoomInW = false
zoomOutW = false
}
if (zoomInKb || zoomOutKb || zoomInW || zoomOutW){
helping = false;
}
zoom = constrain(zoom, min, max);
s = pxwh*zoom;
mwpx = s*mw;
mhpx = s*mh;
}
function updatePan() {
let p = pxwh/2;
let r = !saveMenuing && (keyIsDown(RIGHT_ARROW) || (keyIsDown(68) && !(ctrl || shift)));
let l = !saveMenuing && (keyIsDown(LEFT_ARROW) || (keyIsDown(65) && !(ctrl || shift)));
let u = !saveMenuing && (keyIsDown(UP_ARROW) || (keyIsDown(87) && !(ctrl || shift)));
let d = !saveMenuing && (keyIsDown(DOWN_ARROW) || (keyIsDown(83) && !(ctrl || shift)));
if (r) {
hPan -= p/zoom;
} if (l) {
hPan += p/zoom;
} if (u) {
vPan += p/zoom;
} if (d) {
vPan -= p/zoom;
}
if (mouseIsPressed) {
if (mouseButton === RIGHT) {
hPan += (mouseX-pmouseX)/zoom;
vPan += (mouseY-pmouseY)/zoom;
}
}
if (r || l || u || d){
helping = false;
}
hRef = (w-mwpx+s)/2 + hPan*zoom;
vRef = (h-mhpx+s)/2 + vPan*zoom;
}
function reCenter() {
hPan = 0;
vPan = 0;
zoom = 1.0;
}
function upScale(n) {
let nmw = n*mw;
let nmh = n*mh;
let nm = [];
for (let y = 0; y < nmh; y++) {
nm[y] = [];
for (let x = 0; x < nmw; x++) {
nm[y][x] = dv;
}
}
for (let y = 0; y < nmh; y+=n) {
for (let x = 0; x < nmw; x+=n) {
for (let ny = y; ny < y+n; ny++) {
for (let nx = x; nx < x+n; nx++) {
nm[ny][nx] = m[ceil(y/n)][ceil(x/n)];
}
}
}
}
m = nm;
mw = nmw;
mh = nmh;
mod = true;
}
function reShapeMatrix(nmw, nmh) {
// m is reshaped from bottom-left to up-right
let nm = [];
let delta = nmh - mh;
for (let y = 0; y < nmh; y++) {
nm[y] = [];
for (let x = 0; x < nmw; x++) {
if (y-delta+1 > 0 && x < mw) {
nm[y][x] = m[y-delta][x];
} else {
nm[y][x] = dv;
}
}
}
m = nm;
mw = nmw;
mh = nmh;
mod = true;
}
function undo() {
if (cm > 0) {
cm--;
nm = mHist[cm];
m = deepCopy2D(nm[0]);
mw = m[0].length;
mh = m.length;
p = deepCopy2D(nm[1]);
setPalette(p);
mod = true;
undoredo = true;
}
}
function redo() {
if (cm < mHist.length-1) {
cm++;
nm = mHist[cm];
m = deepCopy2D(nm[0]);
mw = m[0].length;
mh = m.length;
p = deepCopy2D(nm[1]);
setPalette(p);
mod = true;
undoredo = true;
}
}
function saveFile(content) {
let writer = createWriter('arMatrixSavefile.arm');
writer.write(JSON.stringify([m, getCurrentPalette()]));
writer.close();
}
function baseButton(x, y, color) {
rectMode(CENTER);
noStroke();
fill(color);
rect(x, y, uipx, uipx, uibcpx);
noFill();
strokeWeight(1);
stroke(uihc, 100);
rect(x, y, uipx-2, uipx-2, uibcpx);
noStroke();
}
function reCenterButton(x, y) {
if (hPan || vPan || zoom != 1.0) {
hcFillValue = uihc;
} else{
hcFillValue = uihcoff;
}
fill(uisc);
noStroke();
rect(x + uisdpx, y + uisdpx, uipx, uipx, uibcpx);
baseButton(x, y, uibc);
fill(hcFillValue);
ellipse(x, y, uipx*2.25/3);
fill(uibc);
ellipse(x, y, uipx*1.9/3);
fill(hcFillValue);
ellipse(x, y, uipx*1/5);
}
function upScaleButton(x, y) {
fill(uisc);
noStroke();
rect(x + uisdpx, y + uisdpx, uipx, uipx, uibcpx);
baseButton(x, y, uibc);
noFill();
strokeWeight(uipx/20);
stroke(uihc, 150);
rect(x-uipx*1/6, y-uipx*1/6, uipx*1/3);
stroke(uihc);
rect(x, y, uipx*2/3);
}
function undoButton(x, y) {
if (cm == 0) {
hcFillValue = uihcoff;
} else{
hcFillValue = uihc;
}
fill(uisc);
noStroke();
rect(x + uisdpx, y + uisdpx, uipx, uipx, uibcpx);
baseButton(x, y, uibc);
fill(hcFillValue);
ellipse(x, y, uipx*1.8/3);
fill(uibc);
ellipse(x, y, uipx*1.4/3);
rect(x-uipx/4.5, y-uipx/4.5, uipx/2.25);
fill(hcFillValue);
triangle(x, y-0.85*uipx/2.25, x, y-0.35*uipx/2.25, x-uipx/6, y-0.6*uipx/2.25);
}
function redoButton(x, y) {
if (cm == mHist.length-1) {
hcFillValue = uihcoff;
} else{
hcFillValue = uihc;
}
fill(uisc);
noStroke();
rect(x + uisdpx, y + uisdpx, uipx, uipx, uibcpx);
baseButton(x, y, uibc);
fill(hcFillValue);
ellipse(x, y, uipx*1.8/3);
fill(uibc);
ellipse(x, y, uipx*1.4/3);
rect(x+uipx/4.5, y-uipx/4.5, uipx/2.25);
fill(hcFillValue);
triangle(x, y-0.85*uipx/2.25, x, y-0.35*uipx/2.25, x+uipx/6, y-0.6*uipx/2.25);
}
function loadButton(x, y) {
fill(uisc);
noStroke();
rect(x + uisdpx, y + uisdpx, uipx, uipx, uibcpx);
baseButton(x, y, uibc);
noFill();
strokeWeight(uipx/20);
stroke(uihc);
rect(x, y, uipx*2/3);
fill(uibc);
noStroke();
rect(x, y-uipx/7, uipx*2.5/3, uipx*1.75/3);
fill(uihc);
// triangle(x, y+uipx/10, x-uipx/10, y+uipx/4, x+uipx/10, y+uipx/4);
stroke(uihc);
line(x-uipx/5, y+uipx/20-uipx/20, x+uipx/5, y+uipx/20-uipx/20);
line(x-uipx/5, y-uipx/10-uipx/20, x+uipx/5, y-uipx/10-uipx/20);
line(x-uipx/5, y-uipx/4-uipx/20, x+uipx/5, y-uipx/4-uipx/20);
}
function saveButton(x, y) {
fill(uisc);
noStroke();
rect(x + uisdpx, y + uisdpx, uipx, uipx, uibcpx);
baseButton(x, y, uibc);
noFill();
strokeWeight(uipx/20);
stroke(uihc);
rect(x, y, uipx*2/3);
fill(uibc);
noStroke();
rect(x, y-uipx/7, uipx*2.5/3, uipx*1.75/3);
fill(uihc);
stroke(uihc);
line(x-uipx/5, y+uipx/4.5, x+uipx/5, y+uipx/4.5);
noFill();
triangle(x, y, x-uipx/5, y-uipx/4, x+uipx/5, y-uipx/4);
}
function savePNGButton(x, y) {
fill(uisc);
noStroke();
rect(x + uisdpx, y + uisdpx, uipx, uipx, uibcpx);
baseButton(x, y, uibc);
noFill();
strokeWeight(uipx/20);
stroke(uihc);
rect(x, y, uipx*2/3);
fill(uihc);
noStroke();
triangle(x-uipx/4,y,x-uipx/4, y+uipx/4, x+uipx/7.5, y+uipx/4)
triangle(x+uipx/4,y-uipx/6,x-uipx/20, y+uipx/4, x+uipx/4, y+uipx/4)
circle(x-uipx/20, y-uipx/10, uipx/7)
}
function helpButton(x, y) {
fill(uisc);
noStroke();
rect(x + uisdpx, y + uisdpx, uipx*0.7, uipx*0.7, uibcpx);
rectMode(CENTER);
fill(uibc);
rect(x, y, uipx*0.7, uipx*0.7, uibcpx);
fill(uihc);
textAlign(CENTER, CENTER);
textSize(uipx*0.7/1.25);
stroke(uisc, 150);
strokeWeight(uipx/10);
textFont('Georgia');
text('i', x, y+uipx/20);
noFill();
strokeWeight(1+1*(onHelpButton && !helping));
stroke(uihc, 100+155*(onHelpButton && !helping));
rect(x, y, uipx*0.7-2, uipx*0.7-2, uibcpx);
}
function drawClickButtons() {
for (let button of clickButtonArray) {
bx = button[0];
by = button[1];
bdraw = button[3];
bdraw(bx, by);
if (button[7] && !helping && (button[5] != 'recenter' || (button[5] == 'recenter' && (hPan || vPan || zoom != 1.0))) && ((button[5] != 'undo' && button[5] != 'redo') || (button[5] == 'undo' && cm > 0) || (button[5] == 'redo' && cm != mHist.length-1))) {
noFill();
stroke(uihc);
strokeWeight(2);
rect(bx, by, uipx-2, uipx-2, uibcpx);
}
}
helpButton(helpbx, helpby);
}
function drawColorPalette() {
for (let valCol of cPalette) {
val = valCol[0];
col = valCol[2];
bx = valCol[3];
by = valCol[4];
on = valCol[5];
isSelected = cSelectIndex == val + nNeg;
rectMode(CENTER);
if (isSelected) {
stroke(bgc);
strokeWeight(uipxp/15);
fill(uihc);
rect(bx, by, uipxp*1.3, uipxp*1.3, uipxp/6);
rect(bx, by+uipxp*1.4/1.65, uipxp*1.15/1.8, uipxp/6, uipxp/15);
} else {
noStroke();
fill(uisc);
rect(bx + uisdpx, by + uisdpx, uipxp, uipxp, uipscl*uibcpx);
if (!helping && on) {
stroke(bgc);
strokeWeight(uipxp/15);
fill(uihc);
rect(bx, by, uipxp*1.3, uipxp*1.3, uipxp/6);
}
}
noStroke();
fill(col);
rect(bx, by, uipxp, uipxp, uipxp/10);
if (col[0] < 130 && col[1] < 130 && col[2] < 140) {
fill(255, 50);
} else {
fill(0, 50);
}
rect(bx, by, uipxp, uipxp, uipxp/10);
fill(col);
rect(bx, by, uipxp/1.2, uipxp/1.2, uipxp/10);
}
}
function showHelp() {
if (helping) {
background(bgc, 100);
textFont('helvetica');
textAlign(CENTER, CENTER);
fill(uihc-50);
stroke(uibc-50);
strokeWeight(3);
// Draw
rect(w/2, h/2, uipx*5/3, uipx*8/3, uipx);
// fill(uihc+10);
fill(cPalette[cSelectIndex][2]);
rect(w/2-uipx*12.5/30, h/2-uipx*2/3, uipx*5/6, uipx*4/3, 5);
noFill();
stroke(uihc);
strokeWeight(uipx*1.5/30);
rect(w/2-uipx*12.5/30, h/2-uipx*2/3, uipx*2/3, uipx*35/30, uipx/15);
rect(w/2, h/2-uipx*11/30, uipx*12/30, uipx*22/30, uipx*7/30);
stroke(uibc-50);
strokeWeight(uipx/10);
fill(uihc-50);
rect(w/2+uipx*12.5/30, h/2-uipx*2/3, uipx*5/6, uipx*4/3, uipx/6);
rect(w/2, h/2-uipx*1/3, uipx*7/30, uipx*2/3, uipx*7/30);
// Pan
rect(w/2-uipx*100/30, h/2-uipx*55/30, uipx*17/30, uipx*17/30, uipx*1/6);
rect(w/2-uipx*(100-17)/30, h/2-uipx*55/30, uipx*17/30, uipx*17/30, uipx*1/6);
rect(w/2-uipx*(100+17)/30, h/2-uipx*55/30, uipx*17/30, uipx*17/30, uipx*1/6);
rect(w/2-uipx*10/3, h/2-uipx*72/30, uipx*17/30, uipx*17/30, uipx*1/6);
fill(uisc);
triangle(w/2-uipx*(100+0.25)/30, h/2-uipx*55/30, w/2-uipx*(100-0.25)/30, h/2-uipx*55/30, w/2-uipx*10/3, h/2-uipx*(55-0.35)/30);
triangle(w/2-uipx*(100+0.25)/30, h/2-uipx*72/30, w/2-uipx*(100-0.25)/30, h/2-uipx*72/30, w/2-uipx*10/3, h/2-uipx*(72+0.35)/30);
triangle(w/2-uipx*(100-17)/30, h/2-uipx*(55+0.25)/30, w/2-uipx*(100-17)/30, h/2-uipx*(55-0.25)/30, w/2-uipx*(100-17-0.35)/30, h/2-uipx*55/30);
triangle(w/2-uipx*(100+17)/30, h/2-uipx*(55+0.25)/30, w/2-uipx*(100+17)/30, h/2-uipx*(55-0.25)/30, w/2-uipx*(100+17+0.35)/30, h/2-uipx*55/30);
fill(uihc-50);
rect(w/2-uipx*10/3, h/2, uipx*5/3, uipx*8/3, uipx);
rect(w/2-uipx*(100+12.5)/30, h/2-uipx*2/3, uipx*5/6, uipx*4/3, uipx*1/6);
fill(uihc);
// fill(cPalette[cSelectIndex][2]);
rect(w/2-uipx*(100-12.5)/30, h/2-uipx*2/3, uipx*5/6, uipx*4/3, uipx*1/6);
fill(uihc-50);
rect(w/2-uipx*10/3, h/2-uipx*1/3, uipx*7/30, uipx*2/3, uipx*7/30);
// Zoom
rect(w/2+uipx*(100-14)/30, h/2-uipx*57/30, uipx*22/30, uipx*22/30, uipx*1/6);
rect(w/2+uipx*(100+14)/30, h/2-uipx*57/30, uipx*22/30, uipx*22/30, uipx*1/6);
fill(uisc);
noStroke();
textSize(uipx*2/3);
textStyle(BOLD);
text('+', w/2+uipx*(100-14)/30, h/2-uipx*57/30);
text('-', w/2+uipx*(100+14)/30, h/2-uipx*57/30);
stroke(uibc-50);
fill(uihc-50);
rect(w/2+uipx*10/3, h/2, uipx*5/3, uipx*8/3, uipx);
rect(w/2+uipx*(100-12.5)/30, h/2-uipx*2/3, uipx*5/6, uipx*4/3, uipx*1/6);
rect(w/2+uipx*(100+12.5)/30, h/2-uipx*2/3, uipx*5/6, uipx*4/3, uipx*1/6);
fill(uihc);
// fill(cPalette[cSelectIndex][2]);
rect(w/2+uipx*10/3, h/2-uipx*1/3, uipx*7/30, uipx*2/3, uipx*7/30);
fill(uihc-50);
textStyle(BOLDITALIC);
textSize(uipx*5/6);
fill(uihc);
text('pan', w/2-uipx*10/3, h/2+uipx*2);
text('draw', w/2, h/2+uipx*2);
text('zoom', w/2+uipx*10/3, h/2+uipx*2);
textSize(uipx/1.5);
textAlign(LEFT);
text('color palette', helpbx-uipx*0.35, cPaletteh+10);
textSize(uipx/2);
textStyle(ITALIC);
fill(uihc-50);
text('click on a color once to select it, and a second time to modify it', helpbx-uipx*0.35, cPaletteh+10+uipx/1.5);
text('is the background color', helpbx-uipx*0.35+(1.75*uipx/2.5), cPaletteh+10+uipx*1.25);
textAlign(CENTER, CENTER);
text('github.com/TomoBossi/ArMatrix', w/2, h-15);
textAlign(RIGHT);
textStyle(NORMAL);
textSize(uipx/2.9);
fill(uihc-30);
for (let i = 0; i < clickButtonArray.length; i++) {
text(clickButtonArray[i][6], w-2*uipx/1.25, clickButtonArray[i][1]+uipx/1.75);
}
textStyle(BOLDITALIC);
textSize(uipx/2);
fill(uihc);
for (let i = 0; i < clickButtonArray.length; i++) {
text(clickButtonArray[i][5], w-2*uipx/1.25, clickButtonArray[i][1]);
}
textStyle(NORMAL);
textAlign(CENTER, CENTER);
i = 0;
while (i < cPalette.length && val != dv) {
val = cPalette[i][0];
bx = cPalette[i][3];
by = cPalette[i][4];
if (val == dv) {
stroke(0, 110);
strokeWeight(3);
fill(255, 200);
textAlign(CENTER, CENTER);
textFont('sans-serif');
textSize(uipxp/1.75);
text('BG', bx, by);
textAlign(LEFT);
stroke(uibc-50);
textSize(uipx/2.5);
text('BG', helpbx-uipx*0.35, cPaletteh+10+uipx*1.25);
noFill();
stroke(uihc);
strokeWeight(2);
rect(bx, by, uipxp, uipxp, uibcpx);
}
i++;
}
}
}
function checkCursorHover() {
for (let button of clickButtonArray) {
bx = button[0];
by = button[1];
button[7] = mouseX > bx - uipx/2 && mouseX < bx + uipx/2 && mouseY > by - uipx/2 && mouseY < by + uipx/2;
}
for (let valCol of cPalette) {
bx = valCol[3];
by = valCol[4];
valCol[5] = mouseX > bx - uipxp/2 && mouseX < bx + uipxp/2 && mouseY > by - uipxp/2 && mouseY < by + uipxp/2;
}
onHelpButton = mouseX > helpbx - uipx/2*0.7 && mouseX < helpbx + uipx/2*0.7 && mouseY > helpby - uipx/2*0.7 && mouseY < helpby + uipx/2*0.7;
}
function mouseClicked() {
// Clickable Buttons (upper right)
for (let button of clickButtonArray) {
let bx = button[0];
let by = button[1];
let on = button[7];
let func = button[2];
let args = button[4];
if (func && on) {
if (args) {
func(args);
} else {
func();
}
}
}
// Help button
if (helping) {
helping = false;
helped = true;
}
if (!helped && onHelpButton) {
helping = true;
} helped = false;
// Colors
// Palette
clickedOnColor = false;
for (let valCol of cPalette) {
bx = valCol[3];
by = valCol[4];
on = valCol[5];
i = valCol[0] + nNeg;
if (on) {
clickedOnColor = true;
if (i == cSelectIndex) {
if (!cPicking) {
if (!cPickedHolding) {
cPickingIndex = i;
cPicking = true;
} else {
cPickingIndex = null;
cPicking = false;
cPickedHolding = false;
}
} else {
cPickingIndex = null;
cPicking = false;
}
} else {
cPickedHolding = false;
cPickingIndex = null;
cPicking = false;
cSelectIndex = i;
}
}
} if (!clickedOnColor) {
cPickedHolding = false;
}
// Wheel
if (cPicking) {
ww = cWd;
wh = cWd;
rx = mouseX - cWx -1;
ry = mouseY - cWy -1;
cS = (sqrt(sq(rx) + sq(ry)) / (0.95*cWr));
if (cS <= 1.05) {
cH = ((atan2(rx, ry) / PI) + 1.0) / 2;
cPick = HSVtoRGB(cH, cS, v);
cPalette[cPickingIndex][1] = [...cPick];
cPickingIndex = null;
cPicking = false;
mod = true;
}
}
// Save menu
if (saveMenuing) {
if (!onSaveMenu && !saveMenuOpened) {
closeSaveMenu();
}
saveMenuOpened = false;
if (onScaleUp) {
savePxScale++;
backToMinScale = false;
} else if (onScaleDown) {
if (savePxScale == 1) {
backToMinScale = true;
}
savePxScale--;
}
if (savePxScale > maxScale || savePxScale < 1) {
savePxScale = constrain(savePxScale, 1, maxScale);
}
if (onCrop && !isEmpty(m)) {
saveCrop = !saveCrop;
}