-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathp5.SimpleUI.js
967 lines (878 loc) · 28 KB
/
p5.SimpleUI.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
// p5.SimpleUI.js
/**
* p5.SimpleUI - A library for creating simple UI elements in p5.js
* @module SimpleUI
*/
(function (global) {
/**
* Represents a clickable button.
* @class
*/
class Button {
/**
* Create a button.
* @param {Object} p - The p5 instance.
* @param {number} x - The x-coordinate of the button.
* @param {number} y - The y-coordinate of the button.
* @param {number} w - The width of the button.
* @param {number} h - The height of the button.
* @param {string} label - The text label of the button.
*/
constructor(p, x, y, w, h, label) {
this.p = p;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.label = label;
this.onClick = null;
this.isHovered = false;
}
/**
* Display the button on the canvas.
*/
display() {
this.p.push();
this.p.stroke(0);
this.p.strokeWeight(0);
this.p.fill(this.isHovered ? 50 : 0);
this.p.cursor(this.isHovered ? this.p.HAND : this.p.ARROW);
this.p.rect(this.x, this.y, this.w, this.h, 5);
this.p.fill(255);
this.p.noStroke();
this.p.textAlign(this.p.CENTER, this.p.CENTER);
this.p.text(this.label, this.x + this.w / 2, this.y + this.h / 2);
this.p.pop();
}
/**
* Check if the mouse is over the button.
* @returns {boolean} True if the mouse is over the button, false otherwise.
*/
isMouseOver() {
return (
this.p.mouseX > this.x &&
this.p.mouseX < this.x + this.w &&
this.p.mouseY > this.y &&
this.p.mouseY < this.y + this.h
);
}
/**
* Handle click events on the button.
*/
handleClick() {
if (this.isMouseOver() && this.onClick) {
this.onClick();
}
}
/**
* Handle mouse movement events for hover effects.
*/
handleMouseMove() {
this.isHovered = this.isMouseOver();
}
/**
* Set the click event handler for the button.
* @param {Function} callback - The function to call when the button is clicked.
*/
setOnClick(callback) {
this.onClick = callback;
}
}
/**
* Represents a file picker button.
* @class
*/
class FilePicker {
/**
* Create a file picker.
* @param {Object} p - The p5 instance.
* @param {number} x - The x-coordinate of the file picker.
* @param {number} y - The y-coordinate of the file picker.
* @param {number} w - The width of the file picker.
* @param {number} h - The height of the file picker.
* @param {string} label - The text label of the file picker.
*/
constructor(p, x, y, w, h, label) {
this.p = p;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.label = label;
this.onSelect = null;
this.multiple = false;
this.fileInput = this.p.createFileInput(this.handleFile.bind(this), this.multiple);
this.fileInput.attribute('accept', 'image/*');
this.fileInput.hide();
this.button = this.p.createButton(this.label);
this.button.position(this.x, this.y);
this.button.size(this.w, this.h);
this.button.mousePressed(() => this.fileInput.elt.click());
this.button.style('background-color', '#000000');
this.button.style('color', '#FFFFFF');
this.button.style('border', 'none');
this.button.style('border-radius', '5px');
this.button.style('font-size', '14px');
this.button.style('cursor', 'pointer');
}
/**
* Handle file selection.
* @param {File|File[]} file - The selected file(s).
*/
handleFile(file) {
if (this.onSelect) {
if (Array.isArray(file)) {
Promise.all(file.map(f => this.loadImage(f))).then(loadedFiles => {
this.onSelect(loadedFiles);
});
} else {
this.loadImage(file).then(loadedFile => {
this.onSelect([loadedFile]);
});
}
}
}
/**
* Load an image file.
* @param {File} file - The file to load.
* @returns {Promise} A promise that resolves with the loaded image data.
*/
loadImage(file) {
return new Promise((resolve) => {
this.p.loadImage(file.data, img => {
resolve({
name: file.name,
type: file.type,
size: file.size,
data: img
});
});
});
}
/**
* Set the file selection event handler.
* @param {Function} callback - The function to call when files are selected.
*/
setOnSelect(callback) {
this.onSelect = callback;
}
/**
* Set whether multiple file selection is allowed.
* @param {boolean} enable - True to allow multiple file selection, false otherwise.
*/
setMultiple(enable) {
this.multiple = enable;
this.fileInput.attribute('multiple', enable);
}
/**
* Display the file picker. (Not needed as the button is already displayed by p5.js)
*/
display() {
// The button is already displayed by p5.js
}
}
/**
* Represents a slider input.
* @class
*/
class Slider {
/**
* Create a slider.
* @param {Object} p - The p5 instance.
* @param {number} x - The x-coordinate of the slider.
* @param {number} y - The y-coordinate of the slider.
* @param {number} length - The length of the slider.
* @param {number} min - The minimum value of the slider.
* @param {number} max - The maximum value of the slider.
* @param {number} defaultValue - The initial value of the slider.
* @param {boolean} [isInteger=false] - Whether the slider should only use integer values.
* @param {number} [step=null] - The step size for the slider values.
* @param {string} [label=""] - The label for the slider.
* @param {boolean} [isVertical=false] - Whether the slider should be vertical.
*/
constructor(p, x, y, length, min, max, defaultValue, isInteger = false, step = null, label = "", isVertical = false) {
this.p = p;
this.x = x;
this.y = y;
this.length = length;
this.isVertical = isVertical;
this.width = isVertical ? 20 : length;
this.height = isVertical ? length : 20;
this.min = min;
this.max = max;
this.isInteger = isInteger;
this.step = step || (isInteger ? 1 : (max - min) / 100);
this.value = this.isInteger ? Math.round(defaultValue) : defaultValue;
this.isDragging = false;
this.isHovering = false;
this.onChange = null;
this.handleSize = 20;
this.label = label;
this.labelWidth = 100;
}
/**
* Display the slider on the canvas.
*/
display() {
this.p.push();
// Display label
this.p.fill(0);
this.p.noStroke();
this.p.textAlign(this.isVertical ? this.p.CENTER : this.p.RIGHT, this.p.CENTER);
this.p.textSize(12);
if (this.isVertical) {
this.p.text(this.label, this.x + this.width / 2, this.y + this.height + 15);
} else {
this.p.text(this.label, this.x - 10, this.y + this.height / 2);
}
// Draw track
this.p.stroke(0);
this.p.strokeWeight(0);
this.p.fill(255);
this.p.rect(this.x, this.y, this.width, this.height);
// Draw active part of track
let handlePos = this.p.map(this.value, this.min, this.max, 0, this.length - this.handleSize);
this.p.noStroke();
this.p.fill(0);
if (this.isVertical) {
this.p.rect(this.x, this.y + this.height - handlePos - this.handleSize, this.width, handlePos + this.handleSize);
} else {
this.p.rect(this.x, this.y, handlePos + this.handleSize, this.height);
}
// Draw handle
this.p.stroke(0);
this.p.strokeWeight(0);
this.p.fill(this.isDragging ? 50 : (this.isHovering ? 50 : 0));
if (this.isVertical) {
this.p.rect(this.x, this.y + this.height - handlePos - this.handleSize, this.width, this.handleSize, 3);
} else {
this.p.rect(this.x + handlePos, this.y, this.handleSize, this.height, 3);
}
// Display value
this.p.noStroke();
this.p.fill(0);
this.p.textAlign(this.p.CENTER, this.p.BOTTOM);
this.p.textSize(12);
let displayValue = this.isInteger ? Math.round(this.value).toString() : this.value.toFixed(2);
if (this.isVertical) {
this.p.text(displayValue, this.x + this.width / 2, this.y + this.height - handlePos - this.handleSize - 5);
} else {
this.p.text(displayValue, this.x + handlePos + this.handleSize / 2, this.y - 5);
}
this.p.pop();
}
/**
* Handle dragging of the slider.
*/
handleDrag() {
if (this.isDragging) {
let mousePos = this.isVertical ? this.p.mouseY : this.p.mouseX;
let sliderStart = this.isVertical ? this.y : this.x;
let newValue = this.p.map(mousePos, sliderStart, sliderStart + this.length, this.isVertical ? this.max : this.min, this.isVertical ? this.min : this.max);
if (this.step) {
newValue = Math.round(newValue / this.step) * this.step;
}
this.value = this.isInteger ? Math.round(newValue) : newValue;
this.value = this.p.constrain(this.value, this.min, this.max);
if (this.onChange) {
this.onChange(this.value);
}
}
}
/**
* Handle mouse press on the slider.
*/
handleMousePressed() {
if (
this.p.mouseX > this.x &&
this.p.mouseX < this.x + this.width &&
this.p.mouseY > this.y &&
this.p.mouseY < this.y + this.height
) {
this.isDragging = true;
this.handleDrag();
}
}
/**
* Handle mouse release.
*/
handleMouseReleased() {
this.isDragging = false;
}
/**
* Handle mouse movement for hover effects.
*/
handleMouseMoved() {
this.isHovering = (
this.p.mouseX > this.x &&
this.p.mouseX < this.x + this.width &&
this.p.mouseY > this.y &&
this.p.mouseY < this.y + this.height
);
}
/**
* Get the current value of the slider.
* @returns {number} The current value of the slider.
*/
getValue() {
return this.value;
}
/**
* Set the value of the slider.
* @param {number} value - The new value for the slider.
*/
setValue(value) {
if (this.isInteger) {
value = Math.round(value);
} else if (this.step) {
value = Math.round(value / this.step) * this.step;
}
this.value = this.p.constrain(value, this.min, this.max);
if (this.onChange) {
this.onChange(this.value);
}
}
/**
* Set the change event handler for the slider.
* @param {Function} callback - The function to call when the slider value changes.
*/
setOnChange(callback) {
this.onChange = callback;
}
}
/**
* Represents a text area input.
* @class
*/
class TextArea {
/**
* Create a text area.
* @param {Object} p - The p5 instance.
* @param {number} x - The x-coordinate of the text area.
* @param {number} y - The y-coordinate of the text area.
* @param {number} w - The width of the text area.
* @param {number} h - The height of the text area.
* @param {string} [defaultText=''] - The initial text in the text area.
*/
constructor(p, x, y, w, h, defaultText = '') {
this.p = p;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.text = defaultText;
this.lines = [defaultText];
this.isFocused = false;
this.onInput = null;
this.cursorX = 0;
this.cursorY = 0;
this.scrollY = 0;
this.lineHeight = 20;
}
/**
* Display the text area on the canvas.
*/
display() {
this.p.push();
// Background
this.p.fill(this.isFocused ? 240 : 255);
// Border
this.p.stroke(this.isFocused ? this.p.color(0, 120, 255) : 0);
this.p.strokeWeight(this.isFocused ? 2 : 0);
this.p.rect(this.x, this.y, this.w, this.h);
// Create a graphics buffer for the text
let buffer = this.p.createGraphics(this.w, this.h);
buffer.background(255, 0);
buffer.fill(0);
buffer.textAlign(this.p.LEFT, this.p.TOP);
buffer.textSize(14);
for (let i = 0; i < this.lines.length; i++) {
buffer.text(this.lines[i], 5, 5 + i * this.lineHeight - this.scrollY);
}
// Display the buffer content within the text area
this.p.image(buffer, this.x, this.y, this.w, this.h);
// Cursor
if (this.isFocused && this.p.frameCount % 60 < 30) {
let cursorX = this.x + 5 + buffer.textWidth(this.lines[this.cursorY].substring(0, this.cursorX));
let cursorY = this.y + 5 + this.cursorY * this.lineHeight - this.scrollY;
this.p.stroke(0);
this.p.strokeWeight(1);
this.p.line(cursorX, cursorY, cursorX, cursorY + this.lineHeight);
}
this.p.pop();
}
/**
* Handle click events on the text area.
*/
handleClick() {
this.isFocused =
this.p.mouseX > this.x &&
this.p.mouseX < this.x + this.w &&
this.p.mouseY > this.y &&
this.p.mouseY < this.y + this.h;
if (this.isFocused) {
let y = Math.floor((this.p.mouseY - this.y + this.scrollY) / this.lineHeight);
this.cursorY = this.p.constrain(y, 0, this.lines.length - 1);
let x = this.p.mouseX - this.x - 5;
this.cursorX = 0;
let lineWidth = 0;
while (this.cursorX < this.lines[this.cursorY].length && lineWidth < x) {
lineWidth += this.p.textWidth(this.lines[this.cursorY][this.cursorX]);
this.cursorX++;
}
}
}
/**
* Handle key press events for the text area.
*/
handleKeyPress() {
if (!this.isFocused) return;
if (this.p.keyCode === this.p.ENTER) {
let rightPart = this.lines[this.cursorY].substring(this.cursorX);
this.lines[this.cursorY] = this.lines[this.cursorY].substring(0, this.cursorX);
this.lines.splice(this.cursorY + 1, 0, rightPart);
this.cursorY++;
this.cursorX = 0;
} else if (this.p.keyCode === this.p.BACKSPACE) {
if (this.cursorX > 0) {
this.lines[this.cursorY] = this.lines[this.cursorY].substring(0, this.cursorX - 1) + this.lines[this.cursorY].substring(this.cursorX);
this.cursorX--;
} else if (this.cursorY > 0) {
this.cursorX = this.lines[this.cursorY - 1].length;
this.lines[this.cursorY - 1] += this.lines[this.cursorY];
this.lines.splice(this.cursorY, 1);
this.cursorY--;
}
} else if (this.p.keyCode === this.p.LEFT_ARROW) {
if (this.cursorX > 0) {
this.cursorX--;
} else if (this.cursorY > 0) {
this.cursorY--;
this.cursorX = this.lines[this.cursorY].length;
}
} else if (this.p.keyCode === this.p.RIGHT_ARROW) {
if (this.cursorX < this.lines[this.cursorY].length) {
this.cursorX++;
} else if (this.cursorY < this.lines.length - 1) {
this.cursorY++;
this.cursorX = 0;
}
} else if (this.p.keyCode === this.p.UP_ARROW) {
if (this.cursorY > 0) {
this.cursorY--;
this.cursorX = Math.min(this.cursorX, this.lines[this.cursorY].length);
}
} else if (this.p.keyCode === this.p.DOWN_ARROW) {
if (this.cursorY < this.lines.length - 1) {
this.cursorY++;
this.cursorX = Math.min(this.cursorX, this.lines[this.cursorY].length);
}
} else if (this.p.key.length === 1) {
this.lines[this.cursorY] = this.lines[this.cursorY].substring(0, this.cursorX) + this.p.key + this.lines[this.cursorY].substring(this.cursorX);
this.cursorX++;
}
// Update scroll position
if (this.cursorY * this.lineHeight < this.scrollY) {
this.scrollY = this.cursorY * this.lineHeight;
} else if ((this.cursorY + 1) * this.lineHeight > this.scrollY + this.h) {
this.scrollY = (this.cursorY + 1) * this.lineHeight - this.h;
}
this.text = this.lines.join('\n');
if (this.onInput) {
this.onInput(this.text);
}
}
/**
* Get the current text in the text area.
* @returns {string} The current text in the text area.
*/
getText() {
return this.text;
}
/**
* Set the text in the text area.
* @param {string} text - The new text for the text area.
*/
setText(text) {
this.text = text;
this.lines = text.split('\n');
this.cursorX = this.lines[this.lines.length - 1].length;
this.cursorY = this.lines.length - 1;
}
/**
* Set the input event handler for the text area.
* @param {Function} callback - The function to call when the text area content changes.
*/
setOnInput(callback) {
this.onInput = callback;
}
}
/**
* Represents a radio button group.
* @class
*/
class RadioButton {
/**
* Create a radio button group.
* @param {Object} p - The p5 instance.
* @param {number} x - The x-coordinate of the radio button group.
* @param {number} y - The y-coordinate of the radio button group.
* @param {string[]} options - An array of options for the radio buttons.
*/
constructor(p, x, y, options) {
this.p = p;
this.x = x;
this.y = y;
this.options = options;
this.selected = 0;
this.onChange = null;
}
/**
* Display the radio button group on the canvas.
*/
display() {
this.p.push();
for (let i = 0; i < this.options.length; i++) {
this.p.stroke(0);
this.p.strokeWeight(0);
this.p.fill(255);
this.p.ellipse(this.x, this.y + i * 25, 20, 20);
if (i === this.selected) {
this.p.fill(0);
this.p.ellipse(this.x, this.y + i * 25, 10, 10);
}
this.p.fill(0);
this.p.noStroke();
this.p.textAlign(this.p.LEFT, this.p.CENTER);
this.p.text(this.options[i], this.x + 15, this.y + i * 25);
}
this.p.pop();
}
/**
* Handle click events on the radio button group.
*/
handleClick() {
for (let i = 0; i < this.options.length; i++) {
if (this.p.dist(this.p.mouseX, this.p.mouseY, this.x, this.y + i * 25) < 10) {
this.selected = i;
if (this.onChange) {
this.onChange(this.options[i]);
}
break;
}
}
}
/**
* Get the currently selected option.
* @returns {string} The currently selected option.
*/
getSelected() {
return this.options[this.selected];
}
/**
* Set the change event handler for the radio button group.
* @param {Function} callback - The function to call when the selection changes.
*/
setOnChange(callback) {
this.onChange = callback;
}
}
/**
* Represents a checkbox.
* @class
*/
class Checkbox {
/**
* Create a checkbox.
* @param {Object} p - The p5 instance.
* @param {number} x - The x-coordinate of the checkbox.
* @param {number} y - The y-coordinate of the checkbox.
* @param {string} label - The label for the checkbox.
*/
constructor(p, x, y, label) {
this.p = p;
this.x = x;
this.y = y;
this.label = label;
this.checked = false;
this.onChange = null;
}
/**
* Display the checkbox on the canvas.
*/
display() {
this.p.push();
this.p.stroke(0);
this.p.strokeWeight(0);
this.p.fill(255);
this.p.rect(this.x, this.y, 20, 20);
if (this.checked) {
this.p.fill(0);
this.p.rect(this.x + 4, this.y + 4, 12, 12);
}
this.p.fill(0);
this.p.noStroke();
this.p.textAlign(this.p.LEFT, this.p.CENTER);
this.p.text(this.label, this.x + 25, this.y + 10);
this.p.pop();
}
/**
* Handle click events on the checkbox.
*/
handleClick() {
if (
this.p.mouseX > this.x &&
this.p.mouseX < this.x + 20 &&
this.p.mouseY > this.y &&
this.p.mouseY < this.y + 20
) {
this.checked = !this.checked;
if (this.onChange) {
this.onChange(this.checked);
}
}
}
/**
* Get the current state of the checkbox.
* @returns {boolean} True if the checkbox is checked, false otherwise.
*/
isChecked() {
return this.checked;
}
/**
* Set the change event handler for the checkbox.
* @param {Function} callback - The function to call when the checkbox state changes.
*/
setOnChange(callback) {
this.onChange = callback;
}
}
/**
* Represents a color picker.
* @class
*/
class ColorPicker {
/**
* Create a color picker.
* @param {Object} p - The p5 instance.
* @param {number} x - The x-coordinate of the color picker.
* @param {number} y - The y-coordinate of the color picker.
* @param {number} w - The width of the color picker.
* @param {number} h - The height of the color picker.
* @param {string} [defaultColor='#000000'] - The initial color of the color picker.
*/
constructor(p, x, y, w, h, defaultColor = '#000000') {
this.p = p;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.color = defaultColor;
this.onColorChange = null;
this.colorInput = this.p.createInput(defaultColor, 'color');
this.colorInput.position(this.x, this.y);
this.colorInput.size(0, 0);
this.colorInput.style('visibility', 'hidden');
this.button = this.p.createButton(this.color);
this.button.position(this.x, this.y);
this.button.size(this.w, this.h);
this.button.mousePressed(() => this.colorInput.elt.click());
this.updateButtonStyle();
this.colorInput.input(() => {
this.color = this.colorInput.value();
this.updateButtonStyle();
if (this.onColorChange) {
this.onColorChange(this.color);
}
});
}
/**
* Update the style of the color picker button.
*/
updateButtonStyle() {
this.button.style('background-color', this.color);
this.button.html(this.color.toUpperCase());
let r = parseInt(this.color.slice(1, 3), 16);
let g = parseInt(this.color.slice(3, 5), 16);
let b = parseInt(this.color.slice(5, 7), 16);
let yiq = ((r * 299) + (g * 587) + (b * 114)) / 1000;
this.button.style('color', (yiq >= 128) ? '#000000' : '#FFFFFF');
this.button.style('border', 'none');
this.button.style('border-radius', '5px');
this.button.style('font-size', '14px');
this.button.style('cursor', 'pointer');
}
/**
* Set the color change event handler for the color picker.
* @param {Function} callback - The function to call when the color changes.
*/
setOnColorChange(callback) {
this.onColorChange = callback;
}
/**
* Get the current color of the color picker.
* @returns {string} The current color in hexadecimal format.
*/
getColor() {
return this.color;
}
/**
* Display the color picker. (Not needed as the button is already displayed by p5.js)
*/
display() {
// The button is already displayed by p5.js
}
/**
* Handle click events on the color picker.
*/
handleClick() {
if (
this.p.mouseX > this.x &&
this.p.mouseX < this.x + this.w &&
this.p.mouseY > this.y &&
this.p.mouseY < this.y + this.h
) {
this.colorInput.elt.click();
}
}
}
/**
* Represents an input box for single-line text input.
* @class
*/
class InputBox {
/**
* Create an input box.
* @param {Object} p - The p5 instance.
* @param {number} x - The x-coordinate of the input box.
* @param {number} y - The y-coordinate of the input box.
* @param {number} w - The width of the input box.
* @param {number} h - The height of the input box.
* @param {string} [defaultText=''] - The initial text in the input box.
*/
constructor(p, x, y, w, h, defaultText = '') {
this.p = p;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.text = defaultText;
this.isFocused = false;
this.onInput = null;
this.cursorVisible = true;
this.lastBlink = 0;
this.blinkInterval = 500;
}
/**
* Display the input box on the canvas.
*/
display() {
this.p.push();
this.p.fill(this.isFocused ? 240 : 255);
this.p.stroke(this.isFocused ? this.p.color(0, 120, 255) : 0);
this.p.strokeWeight(this.isFocused ? 2 : 0);
this.p.rect(this.x, this.y, this.w, this.h);
this.p.fill(0);
this.p.noStroke();
this.p.textAlign(this.p.LEFT, this.p.CENTER);
this.p.text(this.text, this.x + 5, this.y + this.h / 2);
if (this.isFocused) {
if (this.p.millis() - this.lastBlink > this.blinkInterval) {
this.cursorVisible = !this.cursorVisible;
this.lastBlink = this.p.millis();
}
if (this.cursorVisible) {
let textWidth = this.p.textWidth(this.text);
this.p.stroke(0);
this.p.line(this.x + textWidth + 5, this.y + 2, this.x + textWidth + 5, this.y + this.h - 2);
}
}
this.p.pop();
}
/**
* Handle click events on the input box.
*/
handleClick() {
this.isFocused =
this.p.mouseX > this.x &&
this.p.mouseX < this.x + this.w &&
this.p.mouseY > this.y &&
this.p.mouseY < this.y + this.h;
}
/**
* Handle key press events for the input box.
*/
handleKeyPress() {
if (!this.isFocused) return;
if (this.p.keyCode === this.p.BACKSPACE) {
if (this.text.length > 0) {
this.text = this.text.slice(0, -1);
}
} else if (this.p.keyCode === this.p.ENTER) {
this.isFocused = false;
} else if (this.p.key.length === 1) {
this.text += this.p.key;
}
if (this.onInput) {
this.onInput(this.text);
}
}
/**
* Get the current text in the input box.
* @returns {string} The current text in the input box.
*/
getText() {
return this.text;
}
/**
* Set the input event handler for the input box.
* @param {Function} callback - The function to call when the input box content changes.
*/
setOnInput(callback) {
this.onInput = callback;
}
}
/**
* The main SimpleUI object containing all UI element classes.
* @namespace
*/
const SimpleUI = {
Button,
Slider,
RadioButton,
Checkbox,
TextArea,
InputBox,
FilePicker,
ColorPicker,
/**
* Create a new SimpleUI instance.
* @param {Object} p - The p5 instance.
* @returns {Object} An object with methods to create UI elements.
*/
create: function(p) {
return {
Button: (x, y, w, h, label) => new Button(p, x, y, w, h, label),
Slider: (x, y, length, min, max, defaultValue, isInteger = false, step = null, label = "", isVertical = false) => {
return new Slider(p, x, y, length, min, max, defaultValue, isInteger, step, label, isVertical);
},
RadioButton: (x, y, options) => new RadioButton(p, x, y, options),
Checkbox: (x, y, label) => new Checkbox(p, x, y, label),
TextArea: (x, y, w, h, defaultText) => new TextArea(p, x, y, w, h, defaultText),
InputBox: (x, y, w, h, defaultText) => new InputBox(p, x, y, w, h, defaultText),
FilePicker: (x, y, w, h, label) => new FilePicker(p, x, y, w, h, label),
ColorPicker: (x, y, w, h, defaultColor) => new ColorPicker(p, x, y, w, h, defaultColor)
};
}
};
if (typeof p5 !== 'undefined') {
p5.prototype.createSimpleUI = function() {
return SimpleUI.create(this);
};
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = SimpleUI;
} else {
global.SimpleUI = SimpleUI;
}
})(this);