-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketchpad12076847218103060181.autosave
1671 lines (1311 loc) · 50.6 KB
/
sketchpad12076847218103060181.autosave
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
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileSystemView;
// Size estimations for rendering:
//400% 16mb
//300% 9mb
//200% 4mb
//100% 1mb
//50% 0.25mb
//25% 0.0625mb
//
// Formula = width * height * 4 * scale * timeLength * framespersecond
//
public class Sketchpad extends Screen {
private String sketchiePath = "";
private TWEngine.PluginModule.Plugin plugin;
private FFmpegEngine ffmpeg;
private String code = "";
private AtomicBoolean compiling = new AtomicBoolean(false);
private AtomicBoolean successful = new AtomicBoolean(false);
private AtomicBoolean once = new AtomicBoolean(true);
private SpriteSystemPlaceholder sprites;
private SpriteSystemPlaceholder gui;
private PGraphics canvas;
private float canvasScale = 1.0;
private float canvasX = 0.0;
private float canvasY = 0.0;
private float canvasPaneScroll = 0.;
private float codePaneScroll = 0.;
private ArrayList<String> imagesInSketch = new ArrayList<String>(); // This is so that we can know what to remove when we exit this screen.
private ArrayList<PImage> loadedImages = new ArrayList<PImage>();
private JSONObject configJSON = null;
private AtomicBoolean loading = new AtomicBoolean(true);
private AtomicInteger processAfterLoadingIndex = new AtomicInteger(0);
float textAreaZoom = 22.0;
private boolean configMenu = false;
private boolean renderMenu = false;
private int canvasSmooth = 1;
private String renderFormat = "MPEG-4";
private float upscalePixels = 1.;
private boolean rendering = false;
private boolean converting = false;
private int timeBeforeStartingRender = 0;
private SwapchainCanvas[] swapchain; // In order to save concurrently, we use multiple scale canvases
private int swapchainIndex = 0;
private int renderFrameCount = 0;
private float renderFramerate = 0.;
private float musicVolume = 0.5;
private String[] musicFiles = new String[0];
private String selectedMusic = "";
private boolean playing = false;
private boolean loop = false;
private float time = 0.;
private float timeLength = 10.*60.;
// Canvas
private float beginDragX = 0.;
private float beginDragY = 0.;
private float prevCanvasX = 0.;
private float prevCanvasY = 0.;
private boolean isDragging = false;
// Selected pane
private int selectedPane = 0;
private int lastSelectedPane = 0; // Mostly just so I can use the space bar.
final static int CANVAS_PANE = 1;
final static int CODE_PANE = 2;
final static int TIMELINE_PANE = 3;
final static int SWAPCHAIN_SIZE = 20;
class SwapchainCanvas {
public AtomicBoolean ready = new AtomicBoolean(true);
public Thread savingThread;
public PGraphics shaderCanvas = null;
public PGraphics scaleCanvas = null;
public PImage saveImage = null;
}
private String[] defaultCode = {
"public void start() {",
" ",
"}",
"",
"public void run() {",
" g.background(120, 100, 140);",
" ",
"}"
};
public Sketchpad(TWEngine engine, String path) {
this(engine);
loadSketchieInSeperateThread(path);
}
public Sketchpad(TWEngine engine) {
super(engine);
myUpperBarWeight = 100.;
gui = new SpriteSystemPlaceholder(engine, engine.APPPATH+engine.PATH_SPRITES_ATTRIB+"gui/sketchpad/");
gui.interactable = false;
plugin = plugins.createPlugin();
createCanvas(1024, 1024, 1);
resetView();
canvasY = myUpperBarWeight;
input.keyboardMessage = "";
code = "";
// Load default code into keyboardMessage
for (String s : defaultCode) {
code += s+"\n";
}
ffmpeg = new FFmpegEngine();
lastSelectedPane = CODE_PANE;
//sound.streamMusic(engine.APPPATH+"engine/music/test.mp3");
}
////////////////////////////////////////////////////
// SETUP AND LOADING
private void createCanvas(int wi, int hi, int smooth) {
//console.log("CANVAS "+wi+" "+hi);
canvas = createGraphics(wi, hi, P2D);
if (smooth == 0) {
// Nearest neighbour (hey remember this ancient line of code?)
((PGraphicsOpenGL)canvas).textureSampling(2);
}
else {
canvas.smooth(smooth);
}
plugin.sketchioGraphics = canvas;
}
private void loadSketchieInSeperateThread(String path) {
loading.set(true);
processAfterLoadingIndex.set(0);
Thread t1 = new Thread(new Runnable() {
public void run() {
loadSketchie(path);
loading.set(false);
}
});
t1.start();
}
// NOTE: there isn't an equivalent "saveSketchie" method because we don't have
// to save the whole thing:
// - sprite data is saved automatically by the sprite class
// - images... well, I don't think they need to be saved.
private void saveScripts() {
// Not gonna bother putting a TODO but you know that the script isn't going to stick to
// a keyboard forever.
String[] strs = new String[1];
strs[0] = code;
app.saveStrings(sketchiePath+"scripts/main.pde", strs);
console.log("Saved.");
}
private void saveConfig() {
JSONObject json = new JSONObject();
json.setInt("canvas_width", canvas.width);
json.setInt("canvas_height", canvas.height);
json.setInt("smooth", canvasSmooth);
json.setFloat("time_length", timeLength);
json.setString("music_file", selectedMusic);
app.saveJSONObject(json, sketchiePath+"sketch_config.json");
}
// TODO: only loads one script
private String loadScript() {
String scriptPath = "";
String ccode = "";
if (file.exists(sketchiePath+"scripts")) scriptPath = sketchiePath+"scripts/";
if (file.exists(sketchiePath+"script")) scriptPath = sketchiePath+"script/";
// If scripts exist.
if (scriptPath.length() > 0) {
File[] scripts = (new File(scriptPath)).listFiles();
for (File f : scripts) {
String scriptAbsolutePath = f.getAbsolutePath();
if (file.getExt(scriptAbsolutePath).equals("pde")) {
String[] lines = app.loadStrings(scriptAbsolutePath);
ccode = "";
for (String s : lines) {
ccode += s+"\n";
}
// Big TODO here: we're just gonna load one script for now
// until I get things working.
break;
}
}
}
else {
// Script doesn't exist: return default code instead
for (String s : defaultCode) {
ccode += s+"\n";
}
}
println(" ---------------------- CODE: ----------------------");
println(ccode);
return ccode;
}
private void loadSketchie(String path) {
imagesInSketch.clear();
loadedImages.clear();
processAfterLoadingIndex.set(0);
// Undirectorify path
if (path.charAt(path.length()-1) == '/') {
path.substring(0, path.length()-1);
}
if (!file.getExt(path).equals(engine.SKETCHIO_EXTENSION) || !file.isDirectory(path)) {
console.warn("Not a valid sketchie file: "+path);
return;
}
// Re-directorify path
path = file.directorify(path);
sketchiePath = path;
//////////////////
// IMAGES
// Load images
String imgPath = "";
if (file.exists(path+"imgs")) imgPath = path+"imgs";
if (file.exists(path+"img")) imgPath = path+"img";
// Only if imgs folder exists
if (imgPath.length() > 0) {
// List out all the files, get each image.
File[] imgs = (new File(imgPath)).listFiles();
int numberImages = 0;
for (File f : imgs) {
if (f == null) continue;
String pathToSingularImage = f.getAbsolutePath().replaceAll("\\\\", "/");
String name = file.getIsolatedFilename(pathToSingularImage);
// Only load images
if (!file.isImage(pathToSingularImage)) {
continue;
}
// Actual loading (you'll want to run loadSketchie in a seperate thread);
PImage img = loadImage(pathToSingularImage);
// Error checking
if (img == null) {
console.warn("Error while loading image "+name);
continue;
}
if (img.width <= 0 && img.height <= 0) {
console.warn("Error while loading image "+name);
continue;
}
// To avoid race conditions, we need to put the images in a temp linked list
// Add to list so we know what to clear from memory once we're done.
imagesInSketch.add(name);
loadedImages.add(img);
numberImages++;
}
processAfterLoadingIndex.set(numberImages);
}
///////////////////
// SPRITES
// Next: load sprites. Not too hard.
String spritePath = "";
if (file.exists(path+"sprites")) spritePath = path+"sprites/";
if (file.exists(path+"sprite")) spritePath = path+"sprite/";
// If sprites exist.
if (spritePath.length() > 0) {
// Load our new sprite system, EZ.
sprites = new SpriteSystemPlaceholder(engine, spritePath);
sprites.interactable = true;
}
//////////////////
// SCRIPT
// And now: script
code = loadScript();
//////////////////
// MUSIC
// Load em into a list
if (file.exists(path+"music")) {
File[] files = (new File(path+"music")).listFiles();
musicFiles = new String[files.length];
for (int i = 0; i < files.length; i++) {
musicFiles[i] = files[i].getName();
}
}
//////////////////
// CONFIG
// Load sketch config
if (file.exists(path+"sketch_config.json")) {
configJSON = loadJSONObject(path+"sketch_config.json");
// Need to load the canvas from a seperate thread
// But while we're here, now's a good time to set the music file.
// and timelength cus why not.
timeLength = configJSON.getFloat("time_length", 10.0);
selectedMusic = configJSON.getString("music_file", "");
}
}
private void compileCode(String code) {
compiling.set(true);
Thread t1 = new Thread(new Runnable() {
public void run() {
successful.set(plugin.compile(code));
compiling.set(false);
once.set(true);
}
});
t1.start();
}
private void setMusic(String musicFileName) {
sound.stopMusic();
// Passing "" will stop any music.
if (musicFileName.length() == 0) return;
String path = sketchiePath+"music/"+musicFileName;
if (file.exists(path)) {
sound.streamMusic(path);
}
else {
console.warn(musicFileName+" music file not found.");
selectedMusic = "";
}
}
//////////////////////////////////////////////
// UTIL METHODS STUFF
// methods for use by the API
public float getTime() {
return time;
}
public float getDelta() {
// When we're rendering, all the file IO and expensive rendering operations will
// inevitably make the actual framerate WAY lower than what we're aiming for and therefore
if (rendering) return display.BASE_FRAMERATE/renderFramerate;
else return display.getDelta();
}
public String getPath() {
// Undirectorify path
if (sketchiePath.charAt(sketchiePath.length()-1) == '/') {
return sketchiePath.substring(0, sketchiePath.length()-1);
}
return sketchiePath;
}
public String getPathDirectorified() {
return sketchiePath;
}
////////////////////////////
private boolean menuShown() {
return configMenu || renderMenu || rendering;
}
private void resetView() {
canvasX = canvas.width*canvasScale*0.5;
canvasY = canvas.height*canvasScale*0.5;
canvasScale = 1.0;
// Only reset view if the mouse is in the canvas pane
if (input.mouseX() < middle()) {
canvasPaneScroll = -1000.;
}
else {
input.scrollOffset = -1000.;
}
}
// Creating this funciton because I think the width of
// canvas v the code editor will likely change later
// and i wanna maintain good code.
private boolean codeEditorShown = true;
private float middle() {
if (codeEditorShown())
return WIDTH/2;
else
return WIDTH;
}
private boolean codeEditorShown() {
return codeEditorShown || rendering;
}
// Ancient code copied from Timeway it aint my fault pls believe me.
private int countNewlines(String t) {
int count = 0;
for (int i = 0; i < t.length(); i++) {
if (t.charAt(i) == '\n') {
count++;
}
}
return count;
}
private float getTextHeight(String txt) {
float lineSpacing = 8;
return ((app.textAscent()+app.textDescent()+lineSpacing)*float(countNewlines(txt)+1));
}
private void togglePlay() {
if (playing) {
pause();
}
else {
play();
}
}
private void pause() {
if (playing) {
playing = false;
sound.pauseMusic();
}
}
private void play() {
if (!playing) {
playing = true;
sound.continueMusic();
}
}
////////////////////////////////////////////
// CANVAS & CODE EDITOR
private boolean inCanvasPane = false;
private void displayCanvas() {
if (input.altDown && input.shiftDown && input.keys[int('s')] == 2) {
input.backspace();
resetView();
}
boolean menuShown = configMenu || renderMenu;
if ((lastSelectedPane == CANVAS_PANE || !codeEditorShown()) && input.keyActionOnce("playPause") && !menuShown) {
togglePlay();
input.backspace(); // Don't want the unintended space.
}
// Difficulty: we have 2 scroll areas: canvas zoom, and code editor.
// if mouse is in canvas pane
boolean canvasPane = input.mouseX() < middle() && !menuShown();
if (canvasPane) {
if (!inCanvasPane) {
inCanvasPane = true;
// We need to switch to our scroll value for the zoom
codePaneScroll = input.scrollOffset; // Update code pane
input.scrollOffset = canvasPaneScroll;
}
input.processScroll(100., 2500.);
}
float scroll = input.scrollOffset;
if (!canvasPane) {
scroll = canvasPaneScroll;
}
// Scroll is negative
canvasScale = (2500.+scroll)/1000.;
if (canvasPane && sprites.selectedSprite == null && input.mouseY() < HEIGHT-myLowerBarWeight && input.mouseY() > myUpperBarWeight) {
if (input.primaryClick && !isDragging && selectedPane == 0) {
beginDragX = input.mouseX();
beginDragY = input.mouseY();
prevCanvasX = canvasX;
prevCanvasY = canvasY;
isDragging = true;
selectedPane = CANVAS_PANE;
lastSelectedPane = CANVAS_PANE;
}
}
if (isDragging && selectedPane == CANVAS_PANE) {
canvasX = prevCanvasX+(input.mouseX()-beginDragX);
canvasY = prevCanvasY+(input.mouseY()-beginDragY);
if (!input.primaryDown || sprites.selectedSprite != null) {
isDragging = false;
}
}
sprites.setMouseScale(canvasScale, canvasScale);
float xx = canvasX-canvas.width*canvasScale*0.5;
float yy = canvasY-canvas.height*canvasScale*0.5;
sprites.setMouseOffset(xx, yy);
app.image(canvas, xx, yy, canvas.width*canvasScale, canvas.height*canvasScale);
}
private void displayCodeEditor() {
// Update scroll for code pane
boolean inCodePane = input.mouseX() >= middle();
if (inCodePane) {
if (inCanvasPane) {
inCanvasPane = false;
// We need to switch to our scroll value for the code scroll
canvasPaneScroll = input.scrollOffset;
input.scrollOffset = codePaneScroll;
}
if (input.primaryClick) {
lastSelectedPane = CODE_PANE;
}
input.processScroll(0., max(getTextHeight(code)-(HEIGHT-myUpperBarWeight-myLowerBarWeight), 0));
}
// Positioning of the text variables
// Used to be a function in engine, moved it to here because complications with
// scroll, don't care.
float x = middle();
float y = myUpperBarWeight;
float wi = WIDTH-middle();
float hi = HEIGHT-myUpperBarWeight-myLowerBarWeight;
// TODO: I really wanna use our shaders to reduce shader-switching
// instead of processing's shaders.
app.resetShader();
// Draw background
app.fill(60);
app.noStroke();
app.rect(x, y, wi, hi);
if (rendering) {
// Use the same panel (code editor) for the rendering info.
}
// All of this is y'know... the actual code editor.
else {
// make sure code string is sync'd with keyboardmessage
// BIG TODO: This is not the safest solution. Please fix.
if (!menuShown() && !loading.get() && input.keyboardMessage.length() > 0) code = input.keyboardMessage;
// This should really be in the displayCanvas code but it's more convenient to have it here for now.
// Damn I'm really giving myself coding debt for adding shortcuts.
//boolean shortcuts = input.keyActionOnce("playPause");
//if (lastSelectedPane == CANVAS_PANE && shortcuts) {
// input.backspace(); // Don't want the unintended space.
//}
// ctrl+s save keystroke
// Really got to fix this input.keys flaw thing.
if (!input.altDown && input.ctrlDown && input.keys[int('s')] == 2) {
saveScripts();
}
// Zoom in/out keys
if (input.altDown && input.keys[int('=')] == 2) {
textAreaZoom += 2.;
input.backspace();
}
if (input.altDown && input.keys[int('-')] == 2) {
textAreaZoom -= 2.;
input.backspace();
}
// Scroll slightly when some y added to text.
if (input.enterOnce) {
if (getTextHeight(code) > (HEIGHT-myUpperBarWeight-myLowerBarWeight) && inCodePane) {
input.scrollOffset -= (textAreaZoom); // Literally just a random char
}
}
// Slight position offset
x += 5;
y += 5;
// Prepare font
app.fill(255);
app.textAlign(LEFT, TOP);
app.textFont(display.getFont("Source Code"), textAreaZoom);
app.textLeading(textAreaZoom);
// Scrolling (make sure to keep in account the whole mouse-in-left-or-right pane thing
// (god my code is so messy)
float scroll = input.scrollOffset;
if (!inCodePane) {
scroll = codePaneScroll;
}
// Display text
if (!menuShown()) {
app.text(input.keyboardMessageDisplay(code), x, y+scroll);
}
else {
app.text(code, x, y+scroll);
}
}
}
///////////////////////////////////////
// MENU
TextField widthField = new TextField("config-width", "Width: ");
TextField heightField = new TextField("config-height", "Height: ");
TextField timeLengthField = new TextField("config-timelength", "Video length: ");
TextField framerateField = new TextField("render-framerate", "Framerate: ");
private boolean smoothChangesMade = false;
public void displayMenu() {
if (menuShown()) {
// Bug fix to prevent sprite being selected as we click the menu.
if (!loading.get()) sprites.selectedSprite = null;
}
//////////////////
// CONFIG MENU
//////////////////
if (configMenu) {
// Background
gui.sprite("config-back-1", "black");
// Title
textSprite("config-menu-title", "--- Sketch config ---");
app.fill(255);
// Width field
widthField.display();
// Height field
heightField.display();
// Time length field
timeLengthField.display();
// Lil button next to timelength field to sync time to music
if (ui.button("config-syncmusictime", "music_time_128", "")) {
selectedField = null;
sound.playSound("select_any");
timeLengthField.value = str(sound.getCurrentMusicDuration());
//try {
// String musicPath = sketchiePath+"music/"+selectedMusic;
// if (selectedMusic.length() > 0 && file.exists(musicPath)) {
// Movie music = new Movie(app, musicPath);
// music.read();
// timeLengthField.value = str(music.duration());
// }
//}
//catch (RuntimeException e) {
// console.warn("Sound duration get failed. "+e.getMessage());
//}
}
// Anti-aliasing field
String smoothDisp = "Anti-aliasing: ";
switch (canvasSmooth) {
case 0:
smoothDisp += "None (pixelated)";
break;
case 1:
smoothDisp += "1x";
break;
case 2:
smoothDisp += "2x";
break;
case 4:
smoothDisp += "4x";
break;
case 8:
smoothDisp += "8x";
break;
}
if (textSprite("config-smooth", smoothDisp) && !ui.miniMenuShown()) {
String[] labels = new String[5];
Runnable[] actions = new Runnable[5];
labels[0] = "None (pixelated)";
actions[0] = new Runnable() {public void run() { canvasSmooth = 0; smoothChangesMade = true; }};
labels[1] = "1x anti-aliasing";
actions[1] = new Runnable() {public void run() { canvasSmooth = 1; smoothChangesMade = true; }};
labels[2] = "2x anti-aliasing";
actions[2] = new Runnable() {public void run() { canvasSmooth = 2; smoothChangesMade = true; }};
labels[3] = "4x anti-aliasing";
actions[3] = new Runnable() {public void run() { canvasSmooth = 4; smoothChangesMade = true; }};
labels[4] = "8x anti-aliasing";
actions[4] = new Runnable() {public void run() { canvasSmooth = 8; smoothChangesMade = true; }};
ui.createOptionsMenu(labels, actions);
}
String musicDisp = (musicFiles.length > 0 ? selectedMusic : "(no files available)");
if (selectedMusic.length() == 0) musicDisp = "(None)";
if (textSprite("config-music", "Music: "+musicDisp) && !ui.miniMenuShown()) {
if (musicFiles.length > 0) {
String[] labels = new String[musicFiles.length+1];
Runnable[] actions = new Runnable[musicFiles.length+1];
// None option
labels[0] = "(None)";
actions[0] = new Runnable() {public void run() { selectedMusic = ""; }};
for (int i = 0; i < musicFiles.length; i++) {
final int index = i;
labels[i+1] = musicFiles[i];
actions[i+1] = new Runnable() {
public void run() {
selectedMusic = musicFiles[index];
setMusic(selectedMusic);
}
};
}
ui.createOptionsMenu(labels, actions);
}
}
// Cross button
if (ui.button("config-cross-1", "cross", "")) {
sound.playSound("select_smaller");
input.keyboardMessage = code;
configMenu = false;
}
// Apply button
if (ui.button("config-ok", "tick_128", "Apply")) {
sound.playSound("select_any");
time = 0.;
try {
int wi = Integer.parseInt(widthField.value);
int hi = Integer.parseInt(heightField.value);
timeLength = Float.parseFloat(timeLengthField.value)*60.;
// Only recreate if changes have been made.
if (wi != (int)canvas.width ||
hi != (int)canvas.height ||
smoothChangesMade
) {
createCanvas(wi, hi, canvasSmooth);
}
}
catch (NumberFormatException e) {
console.log("Invalid inputs!");
return;
}
//setMusic(selectedMusic);
saveConfig();
// End
input.keyboardMessage = code;
configMenu = false;
}
}
/////////////////////
// RENDER MENU
/////////////////////
else if (renderMenu) {
// Background
gui.sprite("render-back-1", "black");
// Title
textSprite("render-menu-title", "--- Render ---");
// Framerate field
framerateField.display();
// That massive block below is indeed the
// compression field.
String compressionDisp = "Compression: "+renderFormat;
if (textSprite("render-compression", compressionDisp) && !ui.miniMenuShown()) {
String[] labels = new String[6];
Runnable[] actions = new Runnable[6];
labels[0] = "MPEG-4";
actions[0] = new Runnable() {public void run() { renderFormat = labels[0]; }};
labels[1] = "MPEG-4 (Lossless 4:2:0)";
actions[1] = new Runnable() {public void run() { renderFormat = labels[1]; }};
labels[2] = "MPEG-4 (Lossless (4:4:4)";
actions[2] = new Runnable() {public void run() { renderFormat = labels[2]; }};
labels[3] = "Apple ProRes 4444";
actions[3] = new Runnable() {public void run() { renderFormat = labels[3]; }};
labels[4] = "Animated GIF";
actions[4] = new Runnable() {public void run() { renderFormat = labels[4]; }};
labels[5] = "Animated GIF (Loop)";
actions[5] = new Runnable() {public void run() { renderFormat = labels[5]; }};
ui.createOptionsMenu(labels, actions);
}
// Pixel upscale field
String upscaleDisp = "Pixel upscale: "+int(upscalePixels*100.)+"% "+(upscalePixels == 1. ? "(None)" : "");
if (textSprite("render-upscale", upscaleDisp) && !ui.miniMenuShown()) {
String[] labels = new String[6];
Runnable[] actions = new Runnable[6];
labels[0] = "25%";
actions[0] = new Runnable() {public void run() { upscalePixels = 0.25; }};
labels[1] = "50%";
actions[1] = new Runnable() {public void run() { upscalePixels = 0.5; }};
labels[2] = "100% (None)";
actions[2] = new Runnable() {public void run() { upscalePixels = 1.; }};
labels[3] = "200%";
actions[3] = new Runnable() {public void run() { upscalePixels = 2.; }};
labels[4] = "300%";
actions[4] = new Runnable() {public void run() { upscalePixels = 3.; }};
labels[5] = "400%";
actions[5] = new Runnable() {public void run() { upscalePixels = 4.; }};
ui.createOptionsMenu(labels, actions);
}
// Start rendering button
if (ui.button("render-ok", "tick_128", "Start rendering")) {
sound.playSound("select_any");
try {
renderFramerate = Float.parseFloat(framerateField.value);
}
catch (NumberFormatException e) {
console.log("Invalid inputs!");
return;
}
beginRendering();
input.keyboardMessage = code;
renderMenu = false;
}
// Close menu button
if (ui.button("render-cross-1", "cross", "")) {
sound.playSound("select_smaller");
input.keyboardMessage = code;
renderMenu = false;
}
}
}
/////////////////////////////////////////////////
// TEXT FIELD CLASS
/////////////////////////////////////////////////
TextField selectedField = null;
class TextField {
private String spriteName = "";
public String value = "";
private String labelDisplay = "";
public TextField(String spriteName, String labelDisplay) {
this.spriteName = spriteName;
this.labelDisplay = labelDisplay;
}
public void display() {
String disp = gui.interactable ? "white" : "nothing";
if (selectedField == this) {
gui.sprite(spriteName, disp);
value = input.keyboardMessage;
}
else {
if (ui.button(spriteName, disp, "")) {
selectedField = this;
input.keyboardMessage = value;
input.cursorX = input.keyboardMessage.length();
}
}
float x = gui.getSprite(spriteName).getX();
float y = gui.getSprite(spriteName).getY();
app.textAlign(LEFT, TOP);
app.textSize(32);
if (selectedField == this) {
app.text(labelDisplay+input.keyboardMessageDisplay(value), x, y);
}
else {
app.text(labelDisplay+value, x, y);
}
}
}
public boolean textSprite(String name, String val) {
String disp = gui.interactable ? "white" : "nothing";
boolean clicked = ui.button(name, disp, "");
float x = gui.getSprite(name).getX();
float y = gui.getSprite(name).getY();
app.textAlign(LEFT, TOP);
app.textSize(32);
app.text(val, x, y);
return clicked;
}