-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixelrealm_ui.pde
executable file
·2188 lines (1758 loc) · 67 KB
/
pixelrealm_ui.pde
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
// Wanna find a name of a realm template without searching through tons of folders?
// grep -ir "[realm name]" $(find ./ -type f -name "realmtemplate")
public class PixelRealmWithUI extends PixelRealm {
private final String TEMPLATE_METADATA_FILENAME = "realmtemplate.json";
private PImage IMG_BORDER_TILE;
private String musicInfo = "";
private String musicURL = "";
public boolean menuShown = false;
private boolean showPlayerPos = false;
private SpriteSystemPlaceholder gui = null;
private Menu menu = null;
private boolean touchControlsEnabled = false;
private String[] dm_welcome = {
"Welcome to "+engine.getAppName()+".",
"Your folders are your realms.",
"Your computer is your universe.",
"But first, let me show you the ropes.",
"This place you are in right now is actually your home folder.",
"This is a realm. Your files inside your home folder are in this realm.",
"Go have a look around, use WSAD to move and Q+E to look left/right."
};
private String[] dm_tutorial_1 = {
"Well done.",
"It's a bit blank for a realm though, isn't it?",
"Let's give it a proper look and feel.",
"Select a template."
};
private String[] dm_tutorial_2 = {
"Good choice.",
"But your files look a bit scattered and messy...",
"Let's learn how to organise things.",
"Press <tab>, then select \"Grabber\"."
};
// For when the player didn't choose a realm
private String[] dm_tutorial_2_alt = {
"You... didn't really choose anything, did you?",
"Doesn't matter. Let's move on.",
"Your files look a bit scattered and messy...",
"Let's learn how to organise things.",
"Press <tab>, then select \"Grabber\"."
};
// For when there's no realm templates
private String[] dm_tutorial_2_alt_2 = {
"Well done.",
"Your files look a bit scattered and messy...",
"Let's learn how to organise things.",
"Press <tab>, then select \"Grabber\"."
};
private String[] dm_tutorial_3 = {
"Now press the 'o' key to pick up some items."
};
private String[] dm_tutorial_4 = {
"Move it around, and place them down with 'p'."
};
private String[] dm_tutorial_end = {
"Well done. You have mastered the essentials.",
"Remember, all the items you see in your realms are files on your computer.",
"Portals resemble folders. Walk into them to go to enter the folder, or \"realm\".",
"You should be able to figure out the rest yourself, it's not too complicated.",
engine.getAppName()+" is an ongoing project. There may be bugs and missing features.",
"But there are many more things to come.",
"In the meantime, I hope you enjoy this demo."
};
//private String[] dm_hint_1 = {
// "Did you know you can create your own realm assets?",
// "Realms have 5 assets that make up its look and feel: sky, grass, trees, background music, and properties.",
// "These files can be found under the name .pixelrealm-... in your folders on your computer.",
// "If you are on MacOS or Linux, these files may be hidden.",
// "Try customising them, see what you can create.",
//};
// --- Constructors ---
public PixelRealmWithUI(TWEngine engine, String dir) {
super(engine, dir);
myUpperBarColor = color(130);
myLowerBarColor = color(130);
// Ugh. whatever.
IMG_BORDER_TILE = display.systemImages.get("menuborder");
touchControlsEnabled = settings.getBoolean("touch_controls");
// Obviously needed on phones, regardless of settings.
if (isAndroid()) {
touchControlsEnabled = true;
}
gui = new SpriteSystemPlaceholder(engine, engine.APPPATH+engine.PATH_SPRITES_ATTRIB()+"gui/pixelrealm/");
gui.suppressSpriteWarning = true;
gui.interactable = false;
ui.useSpriteSystem(gui);
// Indicates first time running, run the tutorial.
boolean statsExists = false;
statsExists = isAndroid() ? file.exists(getAndroidWriteableDir()+engine.STATS_FILE()) : file.exists(engine.APPPATH+engine.STATS_FILE());
if (!statsExists) {
this.requestTutorial();
}
}
private void beginInputPrompt(String text, Runnable r) {
engine.beginInputPrompt(text, r);
menu = new InputPromptMenu();
}
// --- UI classes ---
class Menu {
public void display() {
}
boolean cached = false;
float cache_backX = 0.;
float cache_backY = 0.;
float cache_backWi = 0.;
float cache_backHi = 0.;
int cache_tilesWi = 0;
int cache_tilesHi = 0;
String cache_backgroundName = "";
protected float getX() {
if (!cached || ui.getInUseSpriteSystem().interactable) return ui.getInUseSpriteSystem().getSprite(cache_backgroundName).getX();
return cache_backX;
}
private SpriteSystemPlaceholder gui() {
return ui.getInUseSpriteSystem();
}
protected float getY() {
if (!cached || gui().interactable) return gui().getSprite(cache_backgroundName).getY();
return cache_backY;
}
protected float getWidth() {
if (!cached || gui().interactable) return (float)gui().getSprite(cache_backgroundName).getWidth();
else return cache_backWi;
}
protected float getHeight() {
if (!cached || gui().interactable) return (float)gui().getSprite(cache_backgroundName).getHeight();
else return cache_backHi;
}
protected float getXmid() {
if (!cached || gui().interactable) return gui().getSprite(cache_backgroundName).getX()+(float)gui().getSprite(cache_backgroundName).getWidth()*0.5;
return cache_backX+cache_backWi*0.5;
}
protected float getYmid() {
if (!cached || gui().interactable) return gui().getSprite(cache_backgroundName).getY()+(float)gui().getSprite(cache_backgroundName).getHeight()*0.5;
return cache_backY+cache_backHi*0.5;
}
protected float getYbottom() {
if (!cached || gui().interactable) return gui.getSprite(cache_backgroundName).getY()+(float)gui().getSprite(cache_backgroundName).getHeight();
return cache_backY+cache_backHi;
}
protected void displayBackground(String backgroundName) {
gui().spriteVary(backgroundName, "black");
if (display.phoneMode) backgroundName += "-phone";
if (!cached || gui().interactable) {
cache_backgroundName = backgroundName;
cache_backX = gui().getSprite(backgroundName).getX();
cache_backY = gui().getSprite(backgroundName).getY();
int wi = gui().getSprite(backgroundName).getWidth();
int hi = gui().getSprite(backgroundName).getHeight();
cache_backWi = (float)wi;
cache_backHi = (float)hi;
cache_tilesWi = wi/IMG_BORDER_TILE.width;
cache_tilesHi = hi/IMG_BORDER_TILE.height;
cached = true;
}
display.recordRendererTime();
// Horizontal
float x = cache_backX;
float y = cache_backY;
float bottomOffset = float(cache_tilesHi*IMG_BORDER_TILE.height);
for (int ix = 0; ix < cache_tilesWi; ix++) {
image(IMG_BORDER_TILE, x, y);
image(IMG_BORDER_TILE, x, y+bottomOffset);
x += IMG_BORDER_TILE.width;
}
// Vertical
x = cache_backX;
y = cache_backY;
float sideOffset = float(cache_tilesWi*IMG_BORDER_TILE.width);
for (int iy = 0; iy < cache_tilesHi+1; iy++) {
image(IMG_BORDER_TILE, x, y);
image(IMG_BORDER_TILE, x+sideOffset, y);
y += IMG_BORDER_TILE.height;
}
display.recordLogicTime();
}
// Code called when menu is closed via (tab)
public void close() {
}
}
class TitleMenu extends Menu {
protected SpriteSystemPlaceholder.Sprite back;
protected String title;
protected String bgName;
public TitleMenu(String title, String backgroundName) {
super();
back = gui.getSprite(backgroundName);
this.title = title;
this.bgName = backgroundName;
}
public void setTitle(String t) {
this.title = t;
}
public void display() {
displayBackground(bgName);
fill(255);
textFont(engine.DEFAULT_FONT, 32);
textAlign(CENTER, TOP);
text(title, getXmid(), getY()+50.);
}
}
class DialogMenu extends TitleMenu {
private String[] dialog;
private int dialogIndex = 0;
protected Runnable runWhenDone = null;
private float appearTimer = 0;
private boolean enterToContinue = true;
private boolean playedSound = false;
private int time = 0;
public DialogMenu(String title, String backgroundName, String txt) {
super(title, backgroundName);
dialog = new String[1];
dialog[0] = txt;
}
public DialogMenu(String title, String backgroundName, String[] txt) {
super(title, backgroundName);
dialog = txt;
}
public DialogMenu(String title, String backgroundName, String[] txt, Runnable r) {
this(title, backgroundName, txt);
runWhenDone = r;
}
public DialogMenu(String title, String backgroundName, String txt, Runnable r) {
this(title, backgroundName, txt);
runWhenDone = r;
}
public void runWhenDone(Runnable r) {
runWhenDone = r;
}
public void setAppearTimer(int t) {
appearTimer = (float)t;
}
public void setEnterToContinue(boolean onoff) {
enterToContinue = onoff;
}
public void display() {
if (appearTimer > 0.) {
appearTimer -= display.getDelta();
movementPaused = false;
return;
}
time++;
if (!playedSound) {
sound.playSound("menu_prompt");
playedSound = true;
}
displayBackground(bgName);
// Below is enter to continue (ignored if enterToContinue is off);
fill(255);
textFont(engine.DEFAULT_FONT, 30);
textAlign(CENTER, CENTER);
text(dialog[dialogIndex], getX()+40, getY(), getWidth()-80, getHeight());
if (!enterToContinue) return;
textSize(18);
text("(Enter/return to continue)", getXmid(), getYbottom()-70);
boolean enterPressed = input.enterOnce;
if (touchControlsEnabled) {
enterPressed |= input.primaryOnce && time > 4;
}
if (enterPressed) {
sound.playSound("menu_select");
dialogIndex++;
time = 0;
if (dialogIndex >= dialog.length) {
menuShown = false;
menu = null;
if (runWhenDone != null)
runWhenDone.run();
}
}
}
}
class YesNoMenu extends DialogMenu {
protected Runnable runWhenDeclined = null;
public YesNoMenu(String title, String txt) {
super(title, "back-yesno", txt);
setEnterToContinue(false);
}
public YesNoMenu(String title, String txt, Runnable runYes) {
super(title, "back-yesno", txt, runYes);
setEnterToContinue(false);
}
public YesNoMenu(String title, String txt, Runnable runYes, Runnable runNo) {
super(title, "back-yesno", txt, runYes);
runWhenDeclined = runNo;
setEnterToContinue(false);
}
public void display() {
super.display();
if (ui.buttonVary("menu_yes", "tick_128", "Yes")) {
sound.playSound("menu_select");
menuShown = false;
menu = null;
if (runWhenDone != null)
runWhenDone.run();
}
if (ui.buttonVary("menu_no", "cross_128", "No")) {
sound.playSound("menu_select");
menuShown = false;
menu = null;
if (runWhenDeclined != null)
runWhenDeclined.run();
}
}
}
class MainMenu extends TitleMenu {
public MainMenu() {
super("--- M E N U ---", "back-mainmenu");
}
public void display() {
super.display();
// --- Creator menu ---
if (ui.buttonVary("creator_1", "new_entry_128", "Creator")) {
sound.playSound("menu_select");
menu = new CreatorMenu();
}
// --- Pocket menu ---
//if (ui.buttonVary("pocket_menu", "new_entry_128", "Pockets")) {
// sound.playSound("menu_select");
// menu = new PocketMenu();
//}
// Lighting menu
if (ui.buttonVary("lighting_button", "new_entry_128", "Lighting")) {
if (currRealm.versionCompatibility == 1) {
menu = new DialogMenu("Can't use morpher", "back-newrealm", "You can't customise lighting in older 1.x versions. Please upgrade realm via the terraformer.");
}
else {
sound.playSound("menu_select");
menu = new CustomiseLightingMenu();
}
}
// --- Command menu (for phone) ---
if (display.phoneMode) {
if (ui.buttonVary("command_button", "command_256", "Command")) {
engine.showCommandPrompt();
menu = null;
menuShown = false;
}
}
// --- Edit terrain menu ---
if (ui.buttonVary("terraform_menu", "new_entry_128", "Terraform")) {
sound.playSound("menu_select");
Runnable ryes = new Runnable() {
public void run() {
menu = new CustomiseTerrainMenu();
menuShown = true;
currRealm.terraformWarning = false;
}
};
Runnable rno = new Runnable() {
public void run() {
menuShown = true;
menu = new MainMenu();
}
};
if (currRealm.versionCompatibility == 1 || currRealm.versionCompatibility != 2) {
// Additional functionality for upgrading the realm.
ryes = new Runnable() {
public void run() {
if (!COMPATIBILITY_VERSION.equals("2.0") && !COMPATIBILITY_VERSION.equals("2.1")) {
menuShown = false;
menu = null;
console.bugWarn("Expecting COMPATIBILITY_VERSION "+COMPATIBILITY_VERSION+". Please remember to change this part of the code!");
return;
}
currRealm.version = COMPATIBILITY_VERSION;
currRealm.versionCompatibility = 2;
menu = new CustomiseTerrainMenu();
menuShown = true;
currRealm.terraformWarning = false;
}
};
menu = new YesNoMenu("Old version", "This realm uses version "+currRealm.version+" and needs to be upgraded to "+COMPATIBILITY_VERSION+" to be terraformed. Upgrade now and continue?", ryes, rno);
} else if (currRealm.terraformWarning) {
menu = new YesNoMenu("Warning", "Modifying the terrain generator will reset all terrain data in this realm.\nContinue?", ryes, rno);
} else {
menu = new CustomiseTerrainMenu();
menuShown = true;
currRealm.terraformWarning = false;
}
}
// -- Credits --
if (ui.buttonVary("credits", "credits_128", "Credits")) {
if (file.exists(engine.APPPATH+CreditsScreen.CREDITS_PATH)) {
requestScreen(new CreditsScreen(engine));
}
else {
console.warn("Credits file is missing.");
}
menu = null;
menuShown = false;
}
// --- Morpher tool ---
if (ui.buttonVary("morpher_1", "morpher_tool_128", "Morpher")) {
if (currRealm.versionCompatibility == 1) {
menu = new DialogMenu("Can't use morpher", "back-newrealm", "You can't use the morpher tool in the older 1.x versions. Please upgrade realm via the terraformer to use this tool.");
}
else {
currentTool = TOOL_MORPHER;
subTool = MORPHER_BULGE;
morpherBlockHeight = 0.;
morpherRadius = 150.;
globalHoldingObjectSlot = null;
currRealm.updateHoldingItem(globalHoldingObjectSlot);
menuShown = false;
sound.playSound("menu_select");
}
}
// --- Grabber tool ---
if (ui.buttonVary("grabber_1", "grabber_tool_128", "Grabber")) {
// Select the last item in the inventory if not already selected.
if (globalHoldingObject == null) {
if (pockets.tail != null) {
globalHoldingObjectSlot = pockets.tail;
}
}
currRealm.updateHoldingItem(globalHoldingObjectSlot);
currentTool = TOOL_GRABBER;
menuShown = false;
sound.playSound("menu_select");
}
// --- No tool ---
if (ui.buttonVary("notool_1", "notool_128", "No tool")) {
currentTool = TOOL_NORMAL;
globalHoldingObjectSlot = null;
currRealm.updateHoldingItem(globalHoldingObjectSlot);
menuShown = false;
sound.playSound("menu_select");
}
}
}
class FileOptionsMenu extends TitleMenu {
private String filename = "";
private PixelRealmState.FileObject probject = null;
public FileOptionsMenu(PixelRealmState.FileObject o) {
super("", "back-fileoptionsmenu");
this.probject = o;
this.filename = file.getFilename(probject.dir);
this.title = this.filename;
}
public void display() {
super.display();
if (ui.buttonVary("op-delete", "notool_128", "Delete")) {
sound.playSound("menu_select");
issueRefresherCommand(REFRESHER_PAUSE);
if (cassettePlaying.equals(this.filename)) {
sound.stopMusic();
sound.streamMusic(currRealm.musicPath);
cassettePlaying = "";
delay(100); // Don't care about the delay you won't notice a thing (probably)
}
if (file.recycle(probject.dir)) {
probject.destroy();
console.log(filename+" moved to recycle bin.");
}
else {
console.warn("Failed to recycle item. File might be in use.");
}
closeMenu();
}
if (ui.buttonVary("op-rename", "command_256", "Rename")) {
sound.playSound("menu_select");
Runnable r = new Runnable() {
public void run() {
if (input.keyboardMessage.length() == 0) {
return;
}
String newFilename = input.keyboardMessage;
String newPath = file.getDir(probject.dir)+"/"+newFilename;
if (file.exists(newPath)) {
//prompt("Can't rename file", newFilename+" already exists. Please choose a different name.");
Runnable rno = new Runnable() {
public void run() {
closeMenu();
}
};
Runnable ryes = new Runnable() {
public void run() {
issueRefresherCommand(REFRESHER_PAUSE);
String oldpath = probject.dir;
boolean successful = true;
// Rename existing item to temp name so we don't replace it
successful &= file.mv(newPath, newPath+"-tempname");
// Rename current item
if (successful) successful &= file.mv(oldpath, newPath);
// Rename existing item to old name
if (successful) successful &= file.mv(newPath+"-tempname", oldpath);
if (successful) {
// Need to refresh cus too lazy to get the right PRObjects.
currRealm.saveRealmJson();
currRealm.refreshFiles();
console.log("Swapped file names "+file.getFilename(oldpath)+" and "+file.getFilename(newPath));
}
else {
console.warn("Couldn't swap file names, maybe files in use?");
}
closeMenu();
}
};
menu = new YesNoMenu("Can't rename file", newFilename+" already exists. Want to swap the file names?", ryes, rno);
return;
}
issueRefresherCommand(REFRESHER_PAUSE);
if (file.mv(probject.dir, newPath)) {
probject.dir = newPath;
probject.filename = file.getFilename(newPath);
console.log("File renamed to "+file.getFilename(newPath));
}
else {
console.warn("Couldn't rename item. File might be in use.");
}
sound.playSound("menu_select");
closeMenu();
}
};
beginInputPrompt("Rename to:", r);
if (probject.filename.contains(".")) {
input.keyboardMessage = "."+file.getExt(probject.filename);
input.cursorX = 0;
}
//closeMenu();
}
if (ui.buttonVary("op-duplicate", "cuber_tool_128", "Duplicate")) {
sound.playSound("menu_select");
String ext = "";
if (probject.filename.contains(".")) ext = "."+file.getExt(probject.filename);
String dir = file.directorify(file.getDir(probject.dir));
String name = file.getIsolatedFilename(probject.filename);
String copyPath = dir+name+" - copy";
while (file.exists(copyPath+ext)) {
copyPath += " - copy";
}
copyPath += ext;
// TODO: Files may take a while to copy. Run this in a separate thread.
issueRefresherCommand(REFRESHER_PAUSE);
if (file.copy(probject.dir, copyPath)) {
console.log("Duplicated "+probject.filename+".");
currRealm.createPRObjectAndPickup(copyPath);
currentTool = TOOL_GRABBER;
}
else {
console.warn("Failed to duplicate file.");
}
console.log(copyPath);
closeMenu();
}
}
public void close() {
optionHighlightedItem = null;
}
}
class CreatorMenu extends Menu {
public CreatorMenu() {
}
public void display() {
displayBackground("back-creatormenu");
if (ui.buttonVary("newentry", "new_entry_128", "New entry")) {
sound.playSound("menu_select");
newEntry();
}
if (ui.buttonVary("newfolder", "new_folder_128", "New folder")) {
sound.playSound("menu_select");
newFolder();
}
if (ui.buttonVary("newshortcut", "create_shortcut_128", "New shortcut")) {
sound.playSound("menu_select");
issueRefresherCommand(REFRESHER_PAUSE);
((PixelRealmState.ShortcutPortal)currRealm.createPRObjectAndPickup(currRealm.createShortcut())).loadShortcut();
menuShown = false;
}
}
public void newFolder() {
sound.playSound("menu_select");
Runnable r = new Runnable() {
public void run() {
if (input.keyboardMessage.length() == 0) {
menuShown = false;
return;
}
String folderpath = currRealm.stateDirectory+input.keyboardMessage;
if (!file.exists(folderpath)) {
issueRefresherCommand(REFRESHER_PAUSE);
new File(folderpath).mkdirs();
stats.increase("folders_created", 1);
} else {
sound.playSound("nope");
console.log(input.keyboardMessage+" already exists!");
menuShown = false;
menu = null;
return;
}
currRealm.createPRObjectAndPickup(folderpath);
currentTool = TOOL_GRABBER;
menuShown = false;
sound.playSound("menu_select");
}
};
beginInputPrompt("Folder name:", r);
}
public void newEntry() {
sound.playSound("menu_select");
Runnable r = new Runnable() {
public void run() {
if (input.keyboardMessage.length() == 0) {
menuShown = false;
return;
}
String path = currRealm.stateDirectory+input.keyboardMessage+"."+engine.ENTRY_EXTENSION;
if (file.exists(path)) {
sound.playSound("nope");
console.log(input.keyboardMessage+" already exists!");
menuShown = false;
menu = null;
return;
}
// Create a new empty file so that we can hold it and place it down, editor will handle the rest.
try {
issueRefresherCommand(REFRESHER_PAUSE);
FileWriter emptyFile = new FileWriter(path);
emptyFile.write("");
emptyFile.close();
stats.increase("entries_created", 1);
}
catch (IOException e2) {
console.warn("Couldn't create entry, IO error!");
menuShown = false;
return;
}
currRealm.createPRObjectAndPickup(path);
currentTool = TOOL_GRABBER;
launchWhenPlaced = true;
menuShown = false;
sound.playSound("menu_select");
}
};
beginInputPrompt("Entry name:", r);
}
}
class PocketMenu extends Menu {
public void display() {
displayBackground("back-pocketmenu");
gui.spriteVary("pocket_line1", "white");
gui.spriteVary("pocket_line2", "white");
if (ui.buttonVary("pocket_back", "back_arrow_128", "")) {
sound.playSound("menu_select");
menu = new Menu();
}
}
}
class InputPromptMenu extends Menu {
public void display() {
displayBackground("back-inputprompt");
engine.displayInputPrompt();
}
}
class NewRealmMenu extends TitleMenu {
int tempIndex = -1;
int coolDown = 0;
// Slight delay before we show the menu, for a bug fix.
int tmr = 0;
ArrayList<String> templates = new ArrayList<String>();
String previewName = "";
public NewRealmMenu() {
super("Welcome to your new realm", "back-newrealm");
if (!file.exists(engine.APPPATH+engine.TEMPLATES_PATH)) {
console.warn("Templates folder not found.");
menuShown = false;
}
// Can't list our own files, gotta use the load_lists.txt
if (isAndroid()) {
String[] realms = loadStrings(engine.APPPATH+engine.TEMPLATES_PATH+"load_list.txt");
for (String path : realms) {
// Same as isDirectory() but check the string directly.
if (path.charAt(path.length()-1) == '/') {
templates.add(path);
}
}
}
else {
File realms = new File(engine.APPPATH+engine.TEMPLATES_PATH);
for (File f : realms.listFiles()) {
if (f.isDirectory()) {
templates.add(file.directorify(f.getAbsolutePath()));
}
}
}
}
private void preview(int index) {
String path = templates.get(index);
sound.playSound("menu_select");
changedTemplate = true;
coolDown = 5;
previewName = "";
// Load template information.
if (file.exists(path+TEMPLATE_METADATA_FILENAME)) {
try {
JSONObject json = app.loadJSONObject(path+TEMPLATE_METADATA_FILENAME);
previewName = json.getString("realm_name", "");
musicInfo = json.getString("music_name", "");
musicURL = json.getString("music_url", "");
}
catch (RuntimeException e) {
console.warn("There's a problem with this "+TEMPLATE_METADATA_FILENAME+"!");
}
}
// Need to clear terrain objects when we load a new realm otherwise we end up with wayyyy too much
// in our face.
currRealm.chunks.clear();
//tilesCache.clear();
currRealm.ordering = new LinkedList();
currRealm.legacy_autogenStuff = new HashSet<String>();
// Load terrain
currRealm.loadRealmTerrain(path);
// clearing ordering also clears our fileobjects.
// We need to re-add them back.
// And, while we're at it, let's put them level with the ground.
for (PixelRealmState.FileObject o : currRealm.files) {
currRealm.ordering.add(o);
o.surface();
}
//for (PixelRealmState.PRObject p : currRealm.ordering) {
// p.surface();
// if (p instanceof PixelRealmState.TerrainPRObject) {
// PixelRealmState.TerrainPRObject t = (PixelRealmState.TerrainPRObject)p;
// t.readjustSize();
// }
//}
currRealm.playerY = currRealm.onSurface(currRealm.playerX, currRealm.playerZ);
sound.stopMusic();
sound.streamMusic(currRealm.musicPath);
}
public void display() {
super.display();
// Text settings from title should still be applied here.
app.textSize(22);
app.text("Select a template", getXmid(), getY()+90);
app.textAlign(CENTER, CENTER);
app.textSize(30);
app.text(previewName, getXmid(), getYmid());
app.textSize(16);
//String left = str(settings.getKeybinding("inventorySelectLeft"));
//String right = str(settings.getKeybinding("inventorySelectRight"));
app.text("Navigate with < and > keys, press <enter/return> to confirm.", getXmid(), getYbottom()-40);
// Lil easter egg for the glitched realm
if (previewName.equals("YOUR FAVOURITE REALM")) {
app.noStroke();
app.fill(255);
int l = int(random(5, 30));
for (int i = 0; i < l; i++)
app.rect(random(getXmid()-200, getXmid()+200), random(getYmid()-20, getYmid()+20), random(10, 50), random(5, 20));
}
// Special condition to make sure we can't go to the last realm that isn't cached on our first playthrough
boolean allow = (sound.loadingMusic() && tempIndex > 0) || !sound.loadingMusic();
if (allow) {
if ((input.keyActionOnce("inventorySelectLeft")
|| ui.buttonVary("newrealm-prev", "back_arrow_128", ""))
&& coolDown == 0) {
tempIndex--;
if (tempIndex < 0) tempIndex = templates.size()-1;
preview(tempIndex);
}
}
if ((input.keyActionOnce("inventorySelectRight")
|| ui.buttonVary("newrealm-next", "forward_arrow_128", ""))
&& coolDown == 0) {
tempIndex++;
if (tempIndex > templates.size()-1) tempIndex = 0;
preview(tempIndex);
}
if (input.enterOnce || ui.buttonVary("newrealm-confirm", "tick_128", "")) {
sound.playSound("menu_select");
// User didn't select any realm.
if (tempIndex == -1) {
menuShown = false;
menu = null;
return;
}
// begin to copy the realm assets.
ArrayList<String> movefiles = new ArrayList<String>();
// get the realm files
String realmDir = file.directorify(templates.get(tempIndex));
File realmfile = new File(realmDir);
String dest = file.directorify(currRealm.stateDirectory);
if (isAndroid()) {
String[] files = loadStrings(realmDir+"load_list.txt");
for (String src : files) {
String name = file.getFilename(src);
// The realmtemplate file is an exception
if (name.equals(TEMPLATE_METADATA_FILENAME))
continue;
//if (file.exists(dest+name)) {
// conflict = true;
// break;
//}
movefiles.add(src);
}
}
else {
for (File f : realmfile.listFiles()) {
String src = f.getAbsolutePath().replaceAll("\\\\", "/");
String name = file.getFilename(src);
// The realmtemplate file is an exception
if (name.equals(TEMPLATE_METADATA_FILENAME) || name.equals("load_list.txt"))
continue;
//if (file.exists(dest+name)) {
// conflict = true;
// break;
//}
movefiles.add(src);
}
}
issueRefresherCommand(REFRESHER_PAUSE);
for (String src : movefiles) {
// Make it hidden
String filename = file.getFilename(src);
if (filename.charAt(0) != '.') filename = "."+filename;
if (!file.copy(src, dest+filename)) {
prompt("Copy error", "An error occured while copying realm template files. Maybe permissions are denied?");
// Return here so the menu stays open.
return;