-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvisualization.py
executable file
·192 lines (153 loc) · 6.05 KB
/
visualization.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
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GdkPixbuf, GLib
import pickle
import os, sys
import pygbx.stadium_blocks as bl
IMG_SIZE = 30
class TrackVisualizationWindow(Gtk.Window):
def __init__(self, current_idx):
Gtk.Window.__init__(self, title='Visualization')
self.timeout_ids = []
self.denied = []
self.rot_images = []
self.current_idx = current_idx
board = Gtk.Grid()
evbox = Gtk.EventBox()
evbox.add_events(256)
evbox.connect('button-press-event', self.on_button_press)
evbox.add(board)
self.images = [[None for x in range(32)] for y in range(32)]
self.overlays = [[None for x in range(32)] for y in range(32)]
for i in range(32):
for j in range(32):
overlay = Gtk.Overlay()
overlay.add_events(256)
img = Gtk.Image()
img.add_events(256)
img.set_pixel_size(IMG_SIZE)
img.set_property('icon-size', IMG_SIZE)
img.connect('button_press_event', self.on_button_press)
self.images[i][j] = img
overlay.add(img)
self.overlays[i][j] = overlay
board.attach(overlay, i, j, 1, 1)
sep = Gtk.Separator.new(Gtk.Orientation.VERTICAL)
accept_button = Gtk.Button.new_with_label('Accept')
accept_button.get_style_context ().add_class ('suggested-action')
accept_button.set_valign (Gtk.Align.START)
accept_button.connect('clicked', self.on_accept)
discard_button = Gtk.Button.new_with_label('Discard')
discard_button.get_style_context ().add_class ('destructive-action')
discard_button.connect('clicked', self.on_discard)
rebuild_button = Gtk.Button.new_with_label('Rebuild')
rebuild_button.connect('clicked', lambda w: self.build())
self.track_label = Gtk.Label('Track: ' + str(self.current_idx + 1) + ', ' + data[self.current_idx][0])
sidebar = Gtk.Box.new(Gtk.Orientation.VERTICAL, 12)
sidebar.set_valign (Gtk.Align.CENTER)
sidebar.set_vexpand(True)
sidebar.set_hexpand(True)
sidebar.set_margin_left(12)
sidebar.set_margin_right(12)
sidebar.set_margin_bottom(12)
sidebar.set_margin_top(12)
sidebar.add(self.track_label)
sidebar.add(accept_button)
sidebar.add(discard_button)
sidebar.add(rebuild_button)
grid = Gtk.Grid()
grid.attach(evbox, 0, 0, 1, 1)
grid.attach(sep, 1, 0, 1, 1)
grid.attach(sidebar, 2, 0, 1, 1)
self.add(grid)
self.connect('destroy', self.on_destroy)
def on_button_press(self, w, event):
x = int(event.x / IMG_SIZE)
z = int(event.y / IMG_SIZE)
self.images[x][z].set_opacity(0.3)
def on_accept(self, button):
self.current_idx += 1
if self.current_idx < len(data) - 1:
self.build()
self.track_label.set_label('Track: ' + str(self.current_idx + 1) + ', ' + data[self.current_idx][0])
def on_discard(self, button):
self.denied.append(data[self.current_idx][0])
self.current_idx += 1
if current_idx < len(data) - 1:
self.build()
self.track_label.set_label('Track: ' + str(self.current_idx + 1) + ', ' + data[self.current_idx][0])
def on_destroy(self, window):
print('Denied maps:')
print(self.denied)
Gtk.main_quit()
def add_block(self, block):
try:
name = bl.EDITOR_IDS[block[0]]
except KeyError:
return
x = block[1]
if x < 0:
x = -x
z = block[3]
if z < 0:
z = -z
if x > 31:
x = 31
if z > 31:
z = 31
if block[0] == bl.START_LINE_BLOCK:
print('Start block: {}'.format (block))
r = block[4]
rot_pix = GdkPixbuf.Pixbuf.new_from_file_at_size('blocks-images/arr.jpg', 12, 12)
if r == 1:
rot_pix = rot_pix.rotate_simple(GdkPixbuf.PixbufRotation.CLOCKWISE)
elif r == 2:
rot_pix = rot_pix.rotate_simple(GdkPixbuf.PixbufRotation.UPSIDEDOWN)
elif r == 3:
rot_pix = rot_pix.rotate_simple(GdkPixbuf.PixbufRotation.COUNTERCLOCKWISE)
rot_img = Gtk.Image.new_from_pixbuf(rot_pix)
rot_img.set_halign(Gtk.Align.END)
rot_img.set_valign(Gtk.Align.END)
self.rot_images.append(rot_img)
self.overlays[31 - x][31 - z].add_overlay(rot_img)
if os.path.isfile('blocks-images/{}.jpg'.format(name)):
pix = GdkPixbuf.Pixbuf.new_from_file_at_size('blocks-images/{}.jpg'.format(name), IMG_SIZE, IMG_SIZE)
self.images[31 - x][31 - z].set_from_pixbuf(pix)
else:
pix = GdkPixbuf.Pixbuf.new_from_file_at_size('blocks-images/empty.jpg', IMG_SIZE, IMG_SIZE)
self.images[31 - x][31 - z].set_from_pixbuf(pix)
self.show_all()
def build(self):
for tid in self.timeout_ids:
GLib.Source.remove(tid)
self.timeout_ids = []
for row in self.images:
for img in row:
img.set_from_pixbuf(None)
img.set_pixel_size(IMG_SIZE)
img.set_property('icon-size', IMG_SIZE)
for rot_img in self.rot_images:
rot_img.destroy()
track = data[self.current_idx][1]
print(track)
i = 1
for block in track:
self.timeout_ids.append(GLib.timeout_add(200 * i, self.add_block, block))
i += 1
print('Loading training data...')
current_idx = 0
if len(sys.argv) > 1:
if not sys.argv[1].isdigit():
load_fname = sys.argv[1]
track = pickle.load(open(load_fname, 'rb+'))
data = [('', track)]
else:
data = pickle.load(open('data/train_data.pkl', 'rb'))
current_idx = int(sys.argv[1])
else:
data = pickle.load(open('data/train_data.pkl', 'rb'))
current_idx = 0
win = TrackVisualizationWindow(current_idx)
win.build()
win.show_all()
Gtk.main()