forked from Mawiszus/TOAD-GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI.py
968 lines (798 loc) · 41.1 KB
/
GUI.py
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
import re
from tkinter import *
from tkinter import ttk
from tkinter import filedialog as fd
from PIL import ImageTk, Image, ImageDraw
import os
import platform
import time
import threading
import queue
import torch
import math
import sys
import patching
from utils.mario_ai import MarioAI, MARIO_AI_PATH, AgentType
from utils.scrollable_image import ScrollableImage
from utils.tooltip import Tooltip
from utils.level_utils import read_level_from_file, one_hot_to_ascii_level, place_a_mario_token, ascii_to_one_hot_level
from utils.level_image_gen import LevelImageGen
from utils.toad_gan_utils import load_trained_pyramid, generate_sample, TOADGAN_obj
# Check if windows user
if platform.system() == "Windows":
import ctypes
# Set Taskbar Icon
my_appid = u'toad-gui.1.0'
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(my_appid)
# Make DPI aware - if size is too small or weird turn on/off
# ctypes.windll.shcore.SetProcessDpiAwareness(True)
# Validate Function for Entries
def on_validate(in_str, act_type):
if act_type == '1': # insertion
if not in_str.isdigit():
return False
return True
# Object holding important data about the current level
class LevelObject:
def __init__(self, ascii_level, oh_level, image, tokens, scales, noises):
self.ascii_level = ascii_level
self.oh_level = oh_level # one-hot encoded
self.image = image
self.tokens = tokens
self.scales = scales
self.noises = noises
# Main GUI code
def TOAD_GUI():
# Init Window
root = Tk(className=" TOAD-GUI")
# Thread Functions to keep GUI alive when playing/loading/generating
class ThreadedClient(threading.Thread):
def __init__(self, que, fcn):
threading.Thread.__init__(self)
self.que = que
self.fcn = fcn
def run(self):
time.sleep(0.01)
self.que.put(self.fcn())
def spawn_thread(que, fcn):
thread = ThreadedClient(que, fcn)
thread.start()
periodic_call(thread)
def threaded(fn):
# This decorator runs a function in a separate thread using ThreadedClient
def wrapper(*args, **kwargs):
spawn_thread(q, lambda: fn(*args, **kwargs))
return wrapper
def periodic_call(thread):
if thread.is_alive():
root.after(100, lambda: periodic_call(thread))
# Load Icons
toad_icon = ImageTk.PhotoImage(Image.open('icons/toad_icon.png'))
banner_icon = ImageTk.PhotoImage(Image.open('icons/banner.png'))
load_level_icon = ImageTk.PhotoImage(Image.open('icons/folder_level.png'))
load_generator_icon = ImageTk.PhotoImage(Image.open('icons/folder_toad.png'))
generate_level_icon = ImageTk.PhotoImage(Image.open('icons/gear_toad.png'))
play_level_icon = ImageTk.PhotoImage(Image.open('icons/play_button.png'))
save_level_icon = ImageTk.PhotoImage(Image.open('icons/save_button.png'))
blue_play_level_icon = ImageTk.PhotoImage(Image.open("icons/play_button_blue.png"))
apply_icon = ImageTk.PhotoImage(Image.open("icons/apply.png"))
root.iconphoto(False, toad_icon)
# Level to Image renderer
ImgGen = LevelImageGen(os.path.join(os.path.join(os.curdir, "utils"), "sprites"))
# Get images of tokens for edit context menu
full_token_list = torch.load("files/token_list.pth")
token_img_dict = {}
for token in full_token_list:
token_img_dict[token] = ImageTk.PhotoImage(ImgGen.render([token]))
# Placeholder image for the preview
placeholder = Image.new('RGB', (890, 256), (255, 255, 255))
draw = ImageDraw.Draw(placeholder)
draw.text((356, 128), "Level Preview will appear here.", (0, 0, 0))
levelimage = ImageTk.PhotoImage(placeholder)
# Define Variables
level_obj = LevelObject('-', None, levelimage, ['-'], None, None)
toadgan_obj = TOADGAN_obj(None, None, None, None, None, None)
level_l = IntVar()
level_h = IntVar()
load_string_gen = StringVar()
load_string_txt = StringVar()
error_msg = StringVar()
use_gen = BooleanVar()
is_loaded = BooleanVar()
q = queue.Queue()
# Set values
level_l.set(0)
level_h.set(0)
load_string_gen.set("Click the buttons to open a level or generator.")
load_string_txt.set(os.path.join(os.curdir, "levels"))
error_msg.set("No Errors")
use_gen.set(False)
is_loaded.set(False)
generator_name: str = ""
# ---------------------------------------- Define Callbacks ----------------------------------------
@threaded
def load_level():
fname = fd.askopenfilename(title='Load Level', initialdir=os.path.join(os.curdir, 'levels'),
filetypes=[("level .txt files", "*.txt")])
if len(fname) == 0:
return # loading was cancelled
try:
error_msg.set("Loading level...")
is_loaded.set(False)
use_gen.set(False)
if fname[-3:] == "txt":
load_string_gen.set('Path: ' + fname)
folder, lname = os.path.split(fname)
# Load level
lev, tok = read_level_from_file(folder, lname)
level_obj.oh_level = torch.Tensor(lev) # casting to Tensor to keep consistency with generated levels
level_obj.ascii_level = one_hot_to_ascii_level(lev, tok)
# Check if a Mario token exists - if not, we need to place one
m_exists = False
for line in level_obj.ascii_level:
if 'M' in line:
m_exists = True
if not m_exists:
level_obj.ascii_level = place_a_mario_token(level_obj.ascii_level)
level_obj.tokens = tok
img = ImageTk.PhotoImage(ImgGen.render(level_obj.ascii_level))
level_obj.image = img
level_obj.scales = None
level_obj.noises = None
level_l.set(lev.shape[-1])
level_h.set(lev.shape[-2])
is_loaded.set(True)
use_gen.set(False)
error_msg.set("Level loaded")
else:
error_msg.set("No level file selected.")
except Exception:
error_msg.set("No level file selected.")
return
@threaded
def load_generator():
fname = fd.askdirectory(title='Load Generator Directory', initialdir=os.path.join(os.curdir, 'generators'))
if len(fname) == 0:
return # loading was cancelled
try:
error_msg.set("Loading generator...")
use_gen.set(False)
is_loaded.set(False)
load_string_gen.set('Path: ' + fname)
folder = fname
nonlocal generator_name
generator_name = fname
# Load TOAD-GAN
loadgan, msg = load_trained_pyramid(folder)
toadgan_obj.Gs = loadgan.Gs
toadgan_obj.Zs = loadgan.Zs
toadgan_obj.reals = loadgan.reals
toadgan_obj.NoiseAmp = loadgan.NoiseAmp
toadgan_obj.token_list = loadgan.token_list
toadgan_obj.num_layers = loadgan.num_layers
level_l.set(toadgan_obj.reals[-1].shape[-1])
level_h.set(toadgan_obj.reals[-1].shape[-2])
error_msg.set(msg)
is_loaded.set(False)
use_gen.set(True)
except Exception:
error_msg.set("Could not load generator. Confirm that all needed .pth files are in the chosen folder.")
return
@threaded
def save_txt():
save_name = fd.asksaveasfile(title='Save level (.txt/.png)', initialdir=os.path.join(os.curdir, "levels"),
mode='w', defaultextension=".txt",
filetypes=[("txt files", ".txt"), ("png files", ".png")])
if save_name is None:
return # saving was cancelled
elif save_name.name[-3:] == "txt":
text2save = ''.join(level_obj.ascii_level)
save_name.write(text2save)
save_name.close()
elif save_name.name[-3:] == "png":
ImgGen.render(level_obj.ascii_level).save(save_name.name)
else:
error_msg.set("Could not save level with this extension. Supported: .txt, .png")
return
@threaded
def generate():
if toadgan_obj.Gs is None:
error_msg.set("Generator did not load correctly. Are all necessary files in the folder?")
else:
error_msg.set("Generating level...")
is_loaded.set(False)
# Get scaling from height and length
sc_h = level_h.get() / toadgan_obj.reals[-1].shape[-2]
sc_l = level_l.get() / toadgan_obj.reals[-1].shape[-1]
# Generate level
level, scales, noises = generate_sample(toadgan_obj.Gs, toadgan_obj.Zs, toadgan_obj.reals,
toadgan_obj.NoiseAmp, toadgan_obj.num_layers,
toadgan_obj.token_list,
scale_h=sc_l, scale_v=sc_h)
level_obj.oh_level = level.cpu()
level_obj.scales = scales
level_obj.noises = noises
level_obj.ascii_level = one_hot_to_ascii_level(level, toadgan_obj.token_list)
redraw_image()
is_loaded.set(True)
set_button_state(None, None, None)
print("Level generated!")
error_msg.set("Level generated!")
return
def redraw_image(edit_mode=False, rectangle=[(0, 0), (16, 16)], scale=0, broken_rectangles: list = [], path: list[tuple[int, int]] = []):
if is_loaded.get():
# Check if a Mario token exists - if not, we need to place one
m_exists = False
for line in level_obj.ascii_level:
if 'M' in line:
m_exists = True
if not m_exists:
level_obj.ascii_level = place_a_mario_token(level_obj.ascii_level)
if use_gen.get():
level_obj.tokens = toadgan_obj.token_list
# Render Image
pil_img = ImgGen.render(level_obj.ascii_level)
l_draw = ImageDraw.Draw(pil_img)
for rect in broken_rectangles:
x_range, y_range = rect
scaled_rect = [
(x_range[0] * 16, y_range[0] * 16),
(x_range[1] * 16, y_range[1] * 16)
]
l_draw.rectangle(scaled_rect, outline=(255, 0, 0), width=4)
l_draw.line(path, fill=(0, 255, 0), width=3)
if edit_mode:
# Add numbers to rows and cols
for y in range(level_obj.oh_level.shape[-2]):
l_draw.multiline_text((1, 4 + y * 16), str(y), (255, 255, 255),
stroke_width=-1, stroke_fill=(0, 0, 0))
for x in range(level_obj.oh_level.shape[-1]):
l_draw.multiline_text((6 + x * 16, 0), "".join(["%s\n" % c for c in str(x)]), (255, 255, 255),
stroke_width=-1, stroke_fill=(0, 0, 0), spacing=0,
align='right')
# Add bounding box rectangle
l_draw.rectangle(rectangle, outline=(255, 0, 0), width=2)
# Affected tokens rectangle
n_pads = len(level_obj.scales) - scale
if n_pads > 0: # if scale is chosen too big, this just does not render
padding_effect = 3 * n_pads
sc = level_obj.scales[scale].shape[-1] / level_obj.oh_level.shape[-1]
scaling_effect = math.ceil((1 / sc - 1) / 2) # affected tokens in every direction
aoe = (padding_effect + scaling_effect) * 16
affected_rect = [(rectangle[0][0] - aoe, rectangle[0][1] - aoe),
(rectangle[1][0] + aoe, rectangle[1][1] + aoe)]
l_draw.rectangle(affected_rect, outline=(255, 255, 0), width=2)
# Cast image
img = ImageTk.PhotoImage(pil_img)
level_obj.image = img
image_label.change_image(level_obj.image)
@threaded
def play_level(agent: AgentType, visuals: bool, speed: int, timeout: int):
error_msg.set("Playing level...")
is_loaded.set(False)
remember_use_gen = use_gen.get()
use_gen.set(False)
try:
with MarioAI() as mario:
result = mario.evaluate_level([line.rstrip() for line in level_obj.ascii_level], agent, visuals, speed, timeout)
perc = int(result.getCompletionPercentage() * 100)
error_msg.set("Level Played. Completion Percentage: %d%%" % perc)
except Exception as e:
error_msg.set(f"Error while playing level: {e}")
is_loaded.set(True)
use_gen.set(remember_use_gen)
is_loaded.set(True)
use_gen.set(remember_use_gen) # only set use_gen to True if it was previously
return
def load_original_level(level_id: str) -> list[str]:
"""
Loads the original level the given generator was trained on
:param generator_path: Path to the generator
:return: Original level the generator was trained on
"""
level_path: str = os.path.join(os.path.curdir, "levels", "originals", f"lvl_{level_id}.txt")
level: list[str]
with open(level_path, "r") as f:
level = [line.rstrip() for line in f.readlines()]
return level
def calculate_broken_range(
progress: float,
level_width: int,
level_height: int,
range_width: int = 5
) -> tuple[tuple[int, int], tuple[int, int]]:
"""
Calculates a range thats to be replaced by the patcher
:param progress: Progress the mario agent made in %
:param level_width: Total width of the level (blocks)
:param level_height: Total height of the level (blocks)
:param range_width: How wide the range is supposed to be in each direction
:return: Range
"""
block_progress = int(float(level_width) * progress)
return (
(max(block_progress - range_width, 0), min(block_progress + range_width, level_width - 2)),
(0, level_height)
)
@threaded
def fix_current_level():
error_msg.set(f"Trying to fix level using {dropdown_value.get()} ...")
patcher_human_play_button.state(['disabled'])
patcher_agent_play_button.state(['disabled'])
patcher_apply_button.state(['disabled'])
patcher_dropdown.state(['disabled'])
nonlocal generator_name
orig_level = load_original_level(
generator_name.split("_")[-1] if generator_name != "" else re.findall(r"\d{1}-\d{1}", load_string_gen.get())[0])
level = []
for line in level_obj.ascii_level:
level.append(line.rstrip())
level_width = len(level[0])
level_height = len(level)
broken_spots = []
with MarioAI() as mario:
result = mario.evaluate_level(level, AgentType.AstarDynamicPlanning, False, 4000, 30)
progress = result.getCompletionPercentage()
tries = 0
while progress < 0.99:
try:
b_range = calculate_broken_range(progress, level_width, level_height)
broken_spots.append(b_range)
level = patching.patchers[dropdown_value.get()].patch(
orig_level,
level,
b_range,
generator_name,
result,
)
result = mario.evaluate_level(level, AgentType.AstarDynamicPlanning, False, 4000, 30)
progress = result.getCompletionPercentage()
tries += 1
level_obj.ascii_level = [line + "\n" for line in level]
level_obj.ascii_level[-1] = level_obj.ascii_level[-1].rstrip()
redraw_image(broken_rectangles=broken_spots)
except Exception as e:
print(f"Patcher caused exception: {e}", file=sys.stderr)
patcher_human_play_button.state(['!disabled'])
patcher_agent_play_button.state(['!disabled'])
patcher_apply_button.state(['!disabled'])
patcher_dropdown.state(['!disabled'])
level_obj.ascii_level = [line + "\n" for line in level]
level_obj.ascii_level[-1] = level_obj.ascii_level[-1].rstrip()
path = result.getMarioPath()
path = [(position.getX(), position.getY() - 8) for position in path]
redraw_image(broken_rectangles=broken_spots, path=path)
error_msg.set(
f"Fixed level using {dropdown_value.get()} in {tries} attempts" if tries > 0 else "Level is playable")
# ---------------------------------------- Layout ----------------------------------------
settings = ttk.Frame(root, padding=(15, 15, 15, 15), width=1000, height=1000) # Main Frame
banner = ttk.Label(settings, image=banner_icon)
welcome_message = ttk.Label(settings, text="Welcome to TOAD-GUI, a Framework to view and playtest Mario levels "
"generated by TOAD-GAN.", width=95, anchor='center')
# Displays loaded path
fpath_label = ttk.Label(settings, textvariable=load_string_gen, width=100)
# Top Buttons
load_lev_button = ttk.Button(settings, compound='top', image=load_level_icon, width=35,
text='Open Level', command=load_level)
load_gen_button = ttk.Button(settings, compound='top', image=load_generator_icon, width=35,
text='Open Generator', command=load_generator)
save_button = ttk.Button(settings, compound='top', image=save_level_icon,
text='Save Level/Image', state='disabled', command=save_txt)
gen_button = ttk.Button(settings, compound='top', image=generate_level_icon,
text='Generate level', state='disabled', command=generate)
# Size Entries
size_frame = ttk.Frame(settings, padding=(1, 1, 1, 1))
h_label = ttk.Label(size_frame, text="Size:")
h_entry = ttk.Entry(size_frame, textvariable=level_h, validate="key", width=3, justify='right', state='disabled')
l_label = ttk.Label(size_frame, text="X")
l_entry = ttk.Entry(size_frame, textvariable=level_l, validate="key", width=3, justify='right', state='disabled')
vcmd_h = (h_entry.register(on_validate), '%P', '%d')
vcmd_l = (l_entry.register(on_validate), '%P', '%d')
h_entry.configure(validatecommand=vcmd_h)
l_entry.configure(validatecommand=vcmd_l)
# Callback to set buttons active/inactive depending on load state
def set_button_state(t1, t2, t3):
if use_gen.get():
gen_button.state(['!disabled'])
if not is_loaded.get():
save_button.state(['disabled'])
else:
save_button.state(['!disabled'])
h_entry.state(['!disabled'])
l_entry.state(['!disabled'])
emode_box.state(['!disabled'])
else:
gen_button.state(['disabled'])
if not is_loaded.get():
save_button.state(['disabled'])
else:
save_button.state(['!disabled'])
editmode.set(False)
h_entry.state(['disabled'])
l_entry.state(['disabled'])
emode_box.state(['disabled'])
return
use_gen.trace("w", callback=set_button_state)
dropdown_value = StringVar()
tab_control = ttk.Notebook(settings)
repair_tab = ttk.Frame(tab_control)
# Play and Controls frame
p_c_frame = ttk.Frame(tab_control)
play_button = ttk.Button(p_c_frame, compound='top', image=play_level_icon, text='Play level',
state='disabled', command=lambda: play_level(AgentType.Human, True, 30, 200))
tab_control.add(p_c_frame, text="Play / Edit")
tab_control.add(repair_tab, text="Repair")
patcher_explain_label = ttk.Label(repair_tab, text="Patching algorithm")
keys: list[str] = list(patching.patchers.keys())
dropdown_value.set(keys[0])
patcher_dropdown = ttk.OptionMenu(repair_tab, dropdown_value, keys[0], *keys)
patcher_dropdown.config(width=20)
repair_tab.columnconfigure(1, weight=1)
patcher_human_play_button = ttk.Button(repair_tab, text="Play", compound="top", image=play_level_icon,
command=lambda: play_level(AgentType.Human, True, 30, 200))
patcher_agent_play_button = ttk.Button(repair_tab, text="AI Agent", compound="top", image=blue_play_level_icon,
command=lambda: play_level(AgentType.AstarDynamicPlanning, True, 30, 200))
patcher_apply_button = ttk.Button(repair_tab, text="Apply", compound="top", image=apply_icon, command=fix_current_level)
patcher_explain_label.grid(column=0, row=0, sticky=(N, W), padx=(20, 0), pady=(25, 20))
patcher_dropdown.grid(column=0, row=1, sticky=(N, W), padx=(20, 10), pady=20)
patcher_apply_button.grid(column=1, row=0, rowspan=2, padx=15, pady=15, sticky=(N, S, W, E))
ttk.Separator(repair_tab, orient=VERTICAL).grid(column=2, row=0, rowspan=2, padx=5, pady=5, sticky=(N, S, E))
patcher_human_play_button.grid(column=3, row=0, rowspan=2, padx=10, pady=15, sticky=(N, W, S, E))
patcher_agent_play_button.grid(column=4, row=0, rowspan=2, padx=10, pady=15, sticky=(N, W, S, E))
tab_control.grid(column=0, row=7, sticky=(N, S, E, W), columnspan=3, padx=5, pady=5)
# Level Preview image
image_label = ScrollableImage(settings, image=levelimage, height=271)
# Token edit function
def change_token(tok, x, y):
level_obj.oh_level[0, :, y, x] = 0
level_obj.oh_level[0, level_obj.tokens.index(tok), y, x] = 1
level_obj.ascii_level = one_hot_to_ascii_level(level_obj.oh_level, level_obj.tokens)
toggle_editmode(None, None, None)
# Popup Menu for the token edit function
def popup_edit(event):
e_menu = Menu(root, tearoff=0)
# Get actual x,y position from click position
tok_x = int(event.widget.canvasx(event.x) / 16)
tok_y = int(event.widget.canvasy(event.y) / 16)
if is_loaded.get():
try:
for i, t in enumerate(level_obj.tokens):
if not (t == 'M' or t == 'F'): # Mario or Flag can not be placed, but are placed automatically
e_menu.add_command(image=token_img_dict[t], label=t, compound='left',
command=lambda tok=t: change_token(tok, tok_x, tok_y))
except TypeError:
e_menu.add_command(label="No changeable Level loaded")
else:
e_menu.add_command(label="No changeable Level loaded")
e_menu.tk_popup(event.x_root, event.y_root)
# Bind right-click to pop-up menu
if platform.system() == 'Darwin':
image_label.bind("<Button-2>", popup_edit) # MacOS has Button 2 for some reason
else:
image_label.bind("<Button-3>", popup_edit)
# Enable/Disable Play Button when loaded/unloaded
def set_play_state(t1, t2, t3):
if is_loaded.get():
play_button.state(['!disabled'])
image_label.change_image(level_obj.image)
else:
play_button.state(['disabled'])
toggle_editmode(t1, t2, t3)
return
is_loaded.trace("w", callback=set_play_state)
# Frame displaying the Game's controls
controls_frame = ttk.LabelFrame(p_c_frame, padding=(5, 5, 5, 5), text='Controls')
contr_a = ttk.Label(controls_frame, text=' a :')
contr_s = ttk.Label(controls_frame, text=' s :')
contr_l = ttk.Label(controls_frame, text='<- :')
contr_r = ttk.Label(controls_frame, text='-> :')
descr_a = ttk.Label(controls_frame, text='Sprint/Throw Fireball')
descr_s = ttk.Label(controls_frame, text='Jump')
descr_l = ttk.Label(controls_frame, text='Move left')
descr_r = ttk.Label(controls_frame, text='Move right')
error_label = ttk.Label(settings, textvariable=error_msg)
# ---------------------------------------- Grid Layout ----------------------------------------
# On root:
settings.grid(column=0, row=0, sticky=(N, S, E, W))
# On settings:
banner.grid(column=1, row=0, columnspan=2, sticky=(N, S))
welcome_message.grid(column=1, row=1, columnspan=2, sticky=(N, S), padx=5, pady=8)
load_lev_button.grid(column=2, row=3, sticky=(N, S, E, W), padx=5, pady=5)
load_gen_button.grid(column=1, row=3, sticky=(N, S, E, W), padx=5, pady=5)
gen_button.grid(column=1, row=4, sticky=(N, S, E, W), padx=5, pady=5)
save_button.grid(column=2, row=4, sticky=(N, S, E, W), padx=5, pady=5)
image_label.grid(column=0, row=6, columnspan=4, sticky=(N, E, W), padx=5, pady=8)
fpath_label.grid(column=0, row=99, columnspan=4, sticky=(S, E, W), padx=5, pady=5)
error_label.grid(column=0, row=100, columnspan=4, sticky=(S, E, W), padx=5, pady=1)
size_frame.grid(column=1, row=5, columnspan=1, sticky=(N, S), padx=5, pady=2)
# On size_frame:
h_label.grid(column=0, row=0, sticky=(N, S, E), padx=1, pady=0)
l_entry.grid(column=1, row=0, sticky=(N, S), padx=1, pady=0)
l_label.grid(column=2, row=0, sticky=(N, S), padx=1, pady=0)
h_entry.grid(column=3, row=0, sticky=(N, S), padx=1, pady=0)
# On p_c_frame:
play_button.grid(column=1, row=0, sticky=(N, S, E, W), padx=5, pady=5)
controls_frame.grid(column=2, row=0, sticky=(N, S, E, W), padx=5, pady=5)
# On controls_frame
contr_a.grid(column=0, row=0, sticky=(N, S, E), padx=1, pady=1)
contr_s.grid(column=0, row=1, sticky=(N, S, E), padx=1, pady=1)
contr_l.grid(column=0, row=2, sticky=(N, S, E), padx=1, pady=1)
contr_r.grid(column=0, row=3, sticky=(N, S, E), padx=1, pady=1)
descr_a.grid(column=1, row=0, sticky=(N, S, W), padx=1, pady=1)
descr_s.grid(column=1, row=1, sticky=(N, S, W), padx=1, pady=1)
descr_l.grid(column=1, row=2, sticky=(N, S, W), padx=1, pady=1)
descr_r.grid(column=1, row=3, sticky=(N, S, W), padx=1, pady=1)
# Column/Rowconfigure
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
settings.columnconfigure(0, weight=1)
settings.columnconfigure(1, weight=1)
settings.columnconfigure(2, weight=1)
settings.columnconfigure(3, weight=1)
settings.rowconfigure(0, weight=1)
settings.rowconfigure(1, weight=1)
settings.rowconfigure(2, weight=1)
settings.rowconfigure(3, weight=1)
settings.rowconfigure(4, weight=1)
settings.rowconfigure(5, weight=1)
settings.rowconfigure(6, weight=2)
settings.rowconfigure(7, weight=1)
settings.rowconfigure(99, weight=1)
settings.rowconfigure(100, weight=1)
p_c_frame.columnconfigure(1, weight=2)
p_c_frame.columnconfigure(2, weight=0)
p_c_frame.rowconfigure(0, weight=1)
controls_frame.columnconfigure(0, weight=1)
controls_frame.columnconfigure(1, weight=1)
controls_frame.rowconfigure(0, weight=1)
controls_frame.rowconfigure(1, weight=1)
controls_frame.rowconfigure(2, weight=1)
controls_frame.rowconfigure(3, weight=1)
# ---------------------------------------- Edit Mode ----------------------------------------
# Define Variables
editmode = BooleanVar()
bbox_x1 = IntVar()
bbox_x2 = IntVar()
bbox_y1 = IntVar()
bbox_y2 = IntVar()
edit_scale = IntVar()
scale_info = StringVar()
# Set values
editmode.set(False)
bbox_x1.set(0)
bbox_x2.set(16)
bbox_y1.set(0)
bbox_y2.set(16)
edit_scale.set(0)
scale_info.set("Scale 0 window: 8x8")
# Placeholder for the noise representation
noise_holder = Image.new('RGB', (8, 8), (255, 255, 255))
noiseimage = ImageTk.PhotoImage(noise_holder)
# ---------------------------------------- Edit Mode Widgets ----------------------------------------
# Set Edit mode Checkbox
emode_box = ttk.Checkbutton(settings, text="Edit mode", variable=editmode, state='disabled')
em_tooltip = Tooltip(emode_box,
text="Right click the image to change a token directly. \n"
"Edit mode allows for resampling parts of a generated level.",
wraplength=250, bg="white", enabled=True, waittime=100)
emode_box.grid(column=1, row=8, columnspan=2, sticky=(N, S, E, W), padx=5, pady=5)
settings.rowconfigure(8, weight=1)
# Edit mode frame
emode_frame = ttk.LabelFrame(p_c_frame, text="Edit mode controls", padding=(5, 5, 5, 5))
# Bounding Box frame
bbox_frame = ttk.LabelFrame(emode_frame, text="Bounding Box", padding=(5, 5, 5, 5))
# Bounding box Entries
x1_label = ttk.Label(bbox_frame, text="x1:", width=5, anchor="e")
x1_entry = ttk.Entry(bbox_frame, textvariable=bbox_y1, validate="key", width=3, justify='right')
x2_label = ttk.Label(bbox_frame, text="x2:")
x2_entry = ttk.Entry(bbox_frame, textvariable=bbox_y2, validate="key", width=3, justify='right')
y1_label = ttk.Label(bbox_frame, text=" y1:", width=5, anchor="e")
y1_entry = ttk.Entry(bbox_frame, textvariable=bbox_x1, validate="key", width=3, justify='right')
y2_label = ttk.Label(bbox_frame, text=" y2:")
y2_entry = ttk.Entry(bbox_frame, textvariable=bbox_x2, validate="key", width=3, justify='right')
bbox_tt_string = "Controls the red bounding box. The yellow box shows which tokens are also " \
"influenced by a change in this scale."
x1_tooltip = Tooltip(x1_label, text=bbox_tt_string, wraplength=250, bg="white", enabled=False)
x2_tooltip = Tooltip(x2_label, text=bbox_tt_string, wraplength=250, bg="white", enabled=False)
y1_tooltip = Tooltip(y1_label, text=bbox_tt_string, wraplength=250, bg="white", enabled=False)
y2_tooltip = Tooltip(y2_label, text=bbox_tt_string, wraplength=250, bg="white", enabled=False)
# Scale entry and
sc_frame = ttk.Frame(emode_frame, padding=(0, 5, 0, 5))
sc_label = ttk.Label(sc_frame, text="Scale:")
sc_entry = ttk.Entry(sc_frame, textvariable=edit_scale, validate="key", width=3, justify='right')
# Noise image and info label
sc_noise_image = ScrollableImage(emode_frame, image=noiseimage, height=50, width=300)
noise_tooltip = Tooltip(sc_noise_image,
text="This is a visualization of the noise map in the chosen scale. "
"Pixels to be resampled are marked red.",
wraplength=250, bg="white", enabled=False)
sc_info_label = ttk.Label(sc_frame, textvariable=scale_info)
sc_tooltip = Tooltip(sc_info_label,
text="Noise influence is a learned parameter that indicates "
"how much influence a change of the noise map will have on this scale.",
wraplength=250, bg="white", enabled=False)
# Entry Validation
vcmd_x1 = (x1_entry.register(on_validate), '%P', '%d')
vcmd_x2 = (x2_entry.register(on_validate), '%P', '%d')
vcmd_y1 = (y1_entry.register(on_validate), '%P', '%d')
vcmd_y2 = (y2_entry.register(on_validate), '%P', '%d')
vcmd_sc = (sc_entry.register(on_validate), '%P', '%d')
x1_entry.configure(validatecommand=vcmd_x1)
x2_entry.configure(validatecommand=vcmd_x2)
y1_entry.configure(validatecommand=vcmd_y1)
y2_entry.configure(validatecommand=vcmd_y2)
sc_entry.configure(validatecommand=vcmd_sc)
# TOAD-GAN resample function
@threaded
def re_sample():
if not level_obj.scales:
error_msg.set("Can't resample loaded level.") # should not be reachable
else:
is_loaded.set(False)
# Get scaled Bounding Box
sc = level_obj.scales[edit_scale.get()].shape[-1] / level_obj.oh_level.shape[-1]
scaled_bbox = (round(bbox_x1.get() * sc), round(bbox_x2.get() * sc),
round(bbox_y1.get() * sc), round(bbox_y2.get() * sc))
new_states = [level_obj.scales, level_obj.noises]
# Get x,y scales from height and length
sc_h = level_h.get() / toadgan_obj.reals[-1].shape[-2]
sc_l = level_l.get() / toadgan_obj.reals[-1].shape[-1]
# Regenerate level from noise (this will reset prior edits)
level, scales, noises = generate_sample(toadgan_obj.Gs, toadgan_obj.Zs, toadgan_obj.reals,
toadgan_obj.NoiseAmp, toadgan_obj.num_layers,
toadgan_obj.token_list, in_states=new_states,
gen_start_scale=edit_scale.get(), is_bboxed=True,
bbox=scaled_bbox, scale_v=sc_h, scale_h=sc_l)
level_obj.oh_level = level.cpu()
level_obj.scales = scales
level_obj.noises = noises
level_obj.ascii_level = one_hot_to_ascii_level(level, toadgan_obj.token_list)
redraw_image(scale=edit_scale.get())
is_loaded.set(True)
# Resample Button and info
resample_button = ttk.Button(emode_frame, text="Resample", state='disabled', command=re_sample)
sample_info = ttk.Label(emode_frame, text="Right click to edit Tokens directly.\n"
"Resampling will regenerate the level,\n"
"so prior Token edits will be lost.")
# Grid/Remove Edit mode widgets
def toggle_editmode(t1, t2, t3):
if editmode.get():
# Grid widgets
controls_frame.grid_configure(column=1, row=1) # move frame to make room
# On settings:
emode_frame.grid(column=0, row=0, rowspan=2, sticky=(E, W), padx=10)
# On emode_frame:
bbox_frame.grid(column=0, row=0, columnspan=1, sticky=(E, W), padx=5, pady=5)
resample_button.grid(column=0, row=5, columnspan=3, sticky=(N, S, W), padx=5, pady=5)
sample_info.grid(column=0, row=4, columnspan=3, sticky=(N, S), padx=5, pady=5)
sc_frame.grid(column=1, row=0, columnspan=5, sticky=(N, S, W), padx=5, pady=5)
sc_label.grid(column=0, row=0, columnspan=1, sticky=(N, S, W), padx=1, pady=5)
sc_entry.grid(column=1, row=0, columnspan=1, sticky=(N, S, W), padx=1, pady=5)
sc_info_label.grid(column=0, row=1, columnspan=2, sticky=(N, S, W), padx=1, pady=5)
sc_noise_image.grid(column=0, row=2, columnspan=2, sticky=(N, W), padx=1, pady=5)
# On bbox_frame:
x1_label.grid(column=0, row=0, sticky=(N, S, E), padx=1, pady=1)
x1_entry.grid(column=1, row=0, sticky=(N, S, W), padx=5, pady=1)
x2_label.grid(column=0, row=1, sticky=(N, S, E), padx=1, pady=1)
x2_entry.grid(column=1, row=1, sticky=(N, S, W), padx=5, pady=1)
y1_label.grid(column=2, row=0, sticky=(N, S, E), padx=1, pady=1)
y1_entry.grid(column=3, row=0, sticky=(N, S, W), padx=5, pady=1)
y2_label.grid(column=2, row=1, sticky=(N, S, E), padx=1, pady=1)
y2_entry.grid(column=3, row=1, sticky=(N, S, W), padx=5, pady=1)
# Column/Rowconfigure
emode_frame.columnconfigure(0, weight=0)
emode_frame.columnconfigure(1, weight=1)
emode_frame.rowconfigure(0, weight=1)
emode_frame.rowconfigure(1, weight=1)
emode_frame.rowconfigure(2, weight=1)
emode_frame.rowconfigure(3, weight=1)
emode_frame.rowconfigure(4, weight=1)
emode_frame.rowconfigure(5, weight=1)
bbox_frame.columnconfigure(0, weight=1)
bbox_frame.columnconfigure(1, weight=1)
bbox_frame.columnconfigure(2, weight=1)
bbox_frame.columnconfigure(3, weight=1)
bbox_frame.rowconfigure(0, weight=1)
bbox_frame.rowconfigure(1, weight=1)
sc_frame.columnconfigure(0, weight=1)
sc_frame.columnconfigure(1, weight=1)
sc_frame.rowconfigure(0, weight=1)
sc_frame.rowconfigure(1, weight=1)
sc_frame.rowconfigure(2, weight=1)
redraw_image(True, rectangle=[(bbox_y1.get() * 16, bbox_x1.get() * 16),
(bbox_y2.get() * 16, bbox_x2.get() * 16)], scale=edit_scale.get())
update_scale_info(t1, t2, t3)
else:
# Hide widgets
controls_frame.grid_configure(column=2, row=0) # move frame back
emode_frame.grid_forget()
redraw_image(False)
editmode.trace("w", callback=toggle_editmode)
# Update scale info label, noise image and resample button availability
def update_scale_info(t1, t2, t3):
try:
# Correct bbox values, TODO: BBox entries are not user friendly - lower value can't be set first
if bbox_y1.get() < 0:
bbox_y1.set(0)
if bbox_x1.get() < 0:
bbox_x1.set(0)
if bbox_y2.get() >= len(level_obj.ascii_level[-1]):
bbox_y2.set(len(level_obj.ascii_level[-1]))
if bbox_x2.get() >= len(level_obj.ascii_level):
bbox_x2.set(len(level_obj.ascii_level))
if bbox_y1.get() >= bbox_y2.get():
bbox_y1.set(bbox_y2.get() - 1)
if bbox_x1.get() >= bbox_x2.get():
bbox_x1.set(bbox_x2.get() - 1)
redraw_image(True, rectangle=[(bbox_y1.get() * 16, bbox_x1.get() * 16),
(bbox_y2.get() * 16, bbox_x2.get() * 16)], scale=edit_scale.get())
# Show Noise Amplitude, which indicates how much influence this noise change will have
scale_info.set("Noise influence: %.4f\n"
% (toadgan_obj.NoiseAmp[edit_scale.get()]))
sc_tooltip.enabled = True
# Calculate scaled bounding box
sc = level_obj.scales[edit_scale.get()].shape[-1] / level_obj.oh_level.shape[-1]
scaled_bbox = [(round(bbox_y1.get() * sc), round(bbox_x1.get() * sc)),
(round(bbox_y2.get() * sc), round(bbox_x2.get() * sc))]
# Get first tokens noise map on current scale as representation
noise_holder = Image.fromarray(level_obj.noises[edit_scale.get()][0, 0, 3:-3, 3:-3].cpu().numpy() * 255)
noise_holder = noise_holder.convert('RGB')
# Draw adjusted bounding box on noise map
if not (round(bbox_y1.get() * sc) == round(bbox_y2.get() * sc) or
round(bbox_x1.get() * sc) == round(bbox_x2.get() * sc)):
n_draw = ImageDraw.Draw(noise_holder, 'RGBA')
rectanglebox = scaled_bbox
rectanglebox[1] = (max(round(bbox_y1.get() * sc), round(bbox_y2.get() * sc) - 1),
max(round(bbox_x1.get() * sc), round(bbox_x2.get() * sc) - 1))
n_draw.rectangle(rectanglebox, fill=(255, 0, 0, 128))
# Make image bigger, so you can see it better
noise_holder = noise_holder.resize((noise_holder.size[0] * 2, noise_holder.size[1] * 2), Image.NEAREST)
# Set noise image
noiseimage = ImageTk.PhotoImage(noise_holder)
sc_noise_image.change_image(noiseimage)
resample_button.state(['!disabled']) # Allow resampling
noise_tooltip.enabled = True
x1_tooltip.enabled = True
x2_tooltip.enabled = True
y1_tooltip.enabled = True
y2_tooltip.enabled = True
except TclError:
scale_info.set("No scale")
resample_button.state(['disabled'])
sc_tooltip.enabled = False
noise_tooltip.enabled = True
x1_tooltip.enabled = True
x2_tooltip.enabled = True
y1_tooltip.enabled = True
y2_tooltip.enabled = True
except IndexError:
scale_info.set("Scale out of range")
resample_button.state(['disabled'])
sc_tooltip.enabled = False
noise_tooltip.enabled = True
x1_tooltip.enabled = True
x2_tooltip.enabled = True
y1_tooltip.enabled = True
y2_tooltip.enabled = True
except TypeError:
scale_info.set("No editable level loaded")
noise_holder = Image.new('RGB', (8, 8), (255, 255, 255))
noiseimage = ImageTk.PhotoImage(noise_holder)
sc_noise_image.change_image(noiseimage)
resample_button.state(['disabled'])
sc_tooltip.enabled = False
noise_tooltip.enabled = False
x1_tooltip.enabled = False
x2_tooltip.enabled = False
y1_tooltip.enabled = False
y2_tooltip.enabled = False
edit_scale.trace("w", callback=update_scale_info)
bbox_x1.trace("w", callback=update_scale_info)
bbox_x2.trace("w", callback=update_scale_info)
bbox_y1.trace("w", callback=update_scale_info)
bbox_y2.trace("w", callback=update_scale_info)
# Move scrollbar to middle (to have message on the test image appear centered)
root.update()
root.after(1, image_label.move_scrollbar_to_middle)
# Run Window
root.mainloop()