forked from PermutaTriangle/tilingsgui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyglet_main.py
525 lines (459 loc) · 18.7 KB
/
pyglet_main.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
import math, pyglet, sys
from permuta import Perm
from permuta.misc import DIR_NONE, DIR_NORTH, DIR_SOUTH, DIR_WEST, DIR_EAST
from tilings import Tiling, Obstruction, Requirement, GriddedPerm
from tilescopethree.strategies.inferral_strategies.row_and_column_separation import row_and_column_separation as real_row_and_col_sep
from tilescopethree.strategies.inferral_strategies.obstruction_transitivity import obstruction_transitivity as real_obs_trans
from tilescopethree.strategies.inferral_strategies.subobstruction_inferral import empty_cell_inferral as real_empty_cell_inferral
from tilescopethree.strategies.equivalence_strategies.point_placements import place_point_of_requirement
from tilescopethree.strategies.equivalence_strategies.partial_point_placements import partial_place_point_of_requirement
MIN_WIDTH = 300
MIN_HEIGHT = 300
INITIAL_WIDTH = 600
INITIAL_HEIGHT = 600
SHADING = True
PRETTY_POINTS = True
SHOW_CROSSING = True
SHOW_LOCALIZED = True
HIGHLIGHT_TOUCHING_CELL = False
MOUSE_POS = (0,0)
window = pyglet.window.Window(height=INITIAL_HEIGHT,
width=INITIAL_WIDTH,
resizable=True)
window.set_minimum_size(MIN_WIDTH, MIN_HEIGHT)
def clamp(x, mnx, mxx):
return min(mxx, max(x, mnx))
def fill_background(color):
cols = [x/255.0 for x in color]
cols.append(1)
pyglet.gl.glClearColor(*cols)
window.clear()
def draw_circle(pos, r, color=[0,0,0]):
N = 30
x,y = pos
verts = [x, y]
cols = color*(N+2)
for i in range(N+1):
ang = 2*math.pi*i/N
verts.append(x+math.cos(ang)*r)
verts.append(y+math.sin(ang)*r)
pyglet.graphics.draw(N+2,
pyglet.gl.GL_TRIANGLE_FAN,
('v2f', verts),
('c3B', cols))
def draw_line_segment(p1, p2, color=[0,0,0]):
N = 2
x1,y1 = p1
x2,y2 = p2
verts = [x1, y1, x2, y2]
cols = color*N
pyglet.graphics.draw(N,
pyglet.gl.GL_LINE_STRIP,
('v2f', verts),
('c3B', cols))
def draw_segment_array(locs, color=[0,0,0]):
verts = []
N = len(locs)
cols = color*N
for i in range(N):
verts.append(locs[i][0])
verts.append(locs[i][1])
pyglet.graphics.draw(N,
pyglet.gl.GL_LINE_STRIP,
('v2f', verts),
('c3B', cols))
def draw_filled_rectangle(loc, sz, color=[0,0,0]):
x,y = loc
w,h = sz
N = 4
verts = [x, y, x, y+h, x+w, y, x+w, y+h]
cols = color * N
pyglet.graphics.draw(N,
pyglet.gl.GL_TRIANGLE_STRIP,
('v2f', verts),
('c3B', cols))
def gridded_perm_initial_locations(gp, gridsz, cellsz):
colcount = [0]*gridsz[0]
rowcount = [0]*gridsz[1]
col = [[] for i in range(gridsz[0])]
row = [[] for i in range(gridsz[1])]
locs = []
for ind in range(len(gp)):
cx,cy = gp.pos[ind]
colcount[cx]+=1
rowcount[cy]+=1
col[cx].append(ind)
row[cy].append(gp.patt[ind])
for r in row:
r.sort()
for ind in range(len(gp)):
val = gp.patt[ind]
cx,cy = gp.pos[ind]
locx = cx*cellsz[0] + cellsz[0]*(col[cx].index(ind)+1)//(colcount[cx]+1)
locy = cy*cellsz[1] + cellsz[1]*(row[cy].index(val)+1)//(rowcount[cy]+1)
locs.append((locx, locy))
return locs
def draw_gridded_perm(locs, radius, color, lines=False):
for i in range(len(locs)):
draw_circle(locs[i], radius, color)
if lines and len(locs) > 1:
draw_segment_array(locs, color)
def distsq(v, w):
return (w[0]-v[0])**2 + (w[1]-v[1])**2
class TilingDrawing(object):
def __init__(self, tiling, x, y, w, h):
self.tiling = tiling
self.x = x
self.y = y
self.w = w
self.h = h
tw, th = self.tiling.dimensions
self.obstruction_locs = [gridded_perm_initial_locations(gp, (tw, th), (self.w/tw, self.h/th))
for gp in self.tiling.obstructions]
self.requirement_locs = [[gridded_perm_initial_locations(gp, (tw, th), (self.w/tw, self.h/th))
for gp in reqlist]
for reqlist in self.tiling.requirements]
def set_size(self, width, height):
for obs in self.obstruction_locs:
for j in range(len(obs)):
xratio = obs[j][0]/self.w
yratio = obs[j][1]/self.h
obs[j] = (xratio*width, yratio*height)
for reqlist in self.requirement_locs:
for req in reqlist:
for j in range(len(req)):
xratio = req[j][0]/self.w
yratio = req[j][1]/self.h
req[j] = (xratio*width, yratio*height)
self.w = width
self.h = height
def cell_to_rect(self, c):
cx, cy = c
tw, th = self.tiling.dimensions
cw, ch = self.w/tw, self.h/th
return ((self.x+cx*cw, self.y+cy*ch), (cw, ch))
def draw_shaded_cells(self):
SHADING_COLOR = (127,127,127)
for c in self.tiling.empty_cells:
draw_filled_rectangle(*self.cell_to_rect(c), SHADING_COLOR)
def draw_point_cells(self):
POINT_COLOR = (0,0,0)
RAD = 10
for c in self.tiling.point_cells:
pos,sz = self.cell_to_rect(c)
x,y = pos
w,h = sz
draw_circle((x+w/2, y+h/2), RAD, POINT_COLOR)
def draw(self):
GRID_COLOR = (0,0,0)
OBS_COLOR = (255,0,0)
REQ_COLOR = (0,0,255)
HIGHLIGHTED_COLOR = (0,0,0)
RAD = 5
THICK = 3
tw, th = self.tiling.dimensions
hover_cell = self.get_cell(MOUSE_POS)
hover_index = None
#hover_index = self.get_point_req_index(pygame.mouse.get_pos())
if SHADING:
self.draw_shaded_cells()
if PRETTY_POINTS:
self.draw_point_cells()
for i in range(tw):
x = self.x + self.w*i/tw
draw_line_segment((x, self.y+self.h), (x, self.y), GRID_COLOR)
for i in range(th):
y = self.y + self.h*i/th
draw_line_segment((self.x, y), (self.x+self.w, y), GRID_COLOR)
for i,loc in enumerate(self.obstruction_locs):
if SHADING and self.tiling.obstructions[i].is_point_obstr():
continue
if PRETTY_POINTS and all(p in self.tiling.point_cells for p in self.tiling.obstructions[i].pos):
continue
col = OBS_COLOR
if HIGHLIGHT_TOUCHING_CELL and any(p == hover_cell for p in self.tiling.obstructions[i].pos):
col = HIGHLIGHTED_COLOR
localized = self.tiling.obstructions[i].is_localized()
if (localized and SHOW_LOCALIZED) or (not localized and SHOW_CROSSING):
draw_gridded_perm(loc, RAD, col, True)
for i,reqlist in enumerate(self.requirement_locs):
if PRETTY_POINTS and any(p in self.tiling.point_cells for req in self.tiling.requirements[i]
for p in req.pos):
continue
col = HIGHLIGHTED_COLOR if hover_index != None and i == hover_index[0]else REQ_COLOR
for j,loc in enumerate(reqlist):
localized = self.tiling.requirements[i][j].is_localized()
if (localized and SHOW_LOCALIZED) or (not localized and SHOW_CROSSING):
draw_gridded_perm(loc, RAD, col, True)
def get_cell(self, mpos):
tw,th = self.tiling.dimensions
cw,ch = self.w/tw, self.h/th
mx,my = mpos[0]-self.x, mpos[1]-self.y
cx,cy = int(mx/cw),int(my/ch)
return (cx, cy)
def get_point_obs_index(self, mpos):
mx,my = mpos[0]-self.x, mpos[1]-self.y
for j,loc in enumerate(self.obstruction_locs):
for k,v in enumerate(loc):
if distsq((mx,my), v) <= 100:
return (j,k)
def get_point_req_index(self, mpos):
mx,my = mpos[0]-self.x, mpos[1]-self.y
for i,reqlist in enumerate(self.requirement_locs):
for j,loc in enumerate(reqlist):
for k,v in enumerate(loc):
if distsq((mx,my), v) <= 100:
return (i,j,k)
def cell_insertion(t, x, y, button, modifiers):
cx,cy = t.get_cell((x,y))
if button == pyglet.window.mouse.LEFT:
return t.tiling.add_single_cell_requirement(Perm((0,)), (cx, cy))
elif button == pyglet.window.mouse.RIGHT:
return t.tiling.add_single_cell_obstruction(Perm((0,)), (cx, cy))
def cell_insertion_12(t, x, y, button, modifiers):
cx,cy = t.get_cell((x,y))
if button == pyglet.window.mouse.LEFT:
return t.tiling.add_single_cell_requirement(Perm((0, 1)), (cx, cy))
elif button == pyglet.window.mouse.RIGHT:
return t.tiling.add_single_cell_obstruction(Perm((0, 1)), (cx, cy))
def place_point(t, x, y, button, modifiers, force_dir=DIR_NONE):
if button == pyglet.window.mouse.LEFT:
ind = t.get_point_req_index((x,y))
if ind != None:
return place_point_of_requirement(t.tiling, ind[0], ind[2], force_dir)
def partial_place_point(t, x, y, button, modifiers, force_dir=DIR_NONE):
if button == pyglet.window.mouse.LEFT:
ind = t.get_point_req_index((x,y))
if ind != None:
return partial_place_point_of_requirement(t.tiling, ind[0], ind[2], force_dir)
def factor(t, x, y, button, modifiers):
if button == pyglet.window.mouse.LEFT:
facs,maps = t.tiling.find_factors(regions=True)
c = t.get_cell((x,y))
for i in range(len(facs)):
if c in maps[i]:
return facs[i]
if button == pyglet.window.mouse.RIGHT:
facs,maps = t.tiling.find_factors(regions=True)
cell = t.get_cell((x,y))
component = set() # Set of cells belonging to the selected component
for i in range(len(facs)):
if cell in maps[i]:
component.update(maps[i].keys())
# Filter out obstructions intersecting the component
obs = filter(lambda x: set(x.pos) & component == set(), t.tiling.obstructions)
# And the same for requirements
reqs = []
for req in t.tiling.requirements:
stripped_r = tuple(filter(lambda x: set(x.pos) & component == set(), req))
if len(stripped_r) > 0:
reqs.append(stripped_r)
return Tiling(tuple(obs), tuple(reqs))
def place_point_south(t, x, y, button, modifiers):
return place_point(t, x, y, button, modifiers, DIR_SOUTH)
def place_point_north(t, x, y, button, modifiers):
return place_point(t, x, y, button, modifiers, DIR_NORTH)
def place_point_west(t, x, y, button, modifiers):
return place_point(t, x, y, button, modifiers, DIR_WEST)
def place_point_east(t, x, y, button, modifiers):
return place_point(t, x, y, button, modifiers, DIR_EAST)
def partial_place_point_south(t, x, y, button, modifiers):
return partial_place_point(t, x, y, button, modifiers, DIR_SOUTH)
def partial_place_point_north(t, x, y, button, modifiers):
return partial_place_point(t, x, y, button, modifiers, DIR_NORTH)
def partial_place_point_west(t, x, y, button, modifiers):
return partial_place_point(t, x, y, button, modifiers, DIR_WEST)
def partial_place_point_east(t, x, y, button, modifiers):
return partial_place_point(t, x, y, button, modifiers, DIR_EAST)
def row_and_col_separation(t, x, y, button, modifiers):
if button == pyglet.window.mouse.LEFT:
x = real_row_and_col_sep(t.tiling)
if x:
return x.comb_classes[0]
def obstruction_transitivity(t, x, y, button, modifiers):
if button == pyglet.window.mouse.LEFT:
x = real_obs_trans(t.tiling)
if x:
return x.comb_classes[0]
def empty_cell_inferral(t, x, y, button, modifiers):
if button == pyglet.window.mouse.LEFT:
x = real_empty_cell_inferral(t.tiling)
if x and x.comb_classes[0] != t.tiling:
return x.comb_classes[0]
tiling_drawing = None
strats = [cell_insertion,
cell_insertion_12,
place_point_north,
place_point_south,
place_point_west,
place_point_east,
partial_place_point_north,
partial_place_point_south,
partial_place_point_west,
partial_place_point_east,
factor,
row_and_col_separation,
obstruction_transitivity]
cur_strat = 0
stack = []
fwd_stack = []
selected_point = None
point_move_bounds = None
move_type = "none"
window.set_caption("Tilings GUI - strat: {}".format(strats[cur_strat].__name__))
@window.event
def on_mouse_press(x, y, button, modifiers):
global tiling_drawing, selected_point, point_move_bounds, move_type
if not (modifiers & pyglet.window.key.MOD_SHIFT
or modifiers & pyglet.window.key.MOD_CTRL):
try:
new_tiling = strats[cur_strat](tiling_drawing, x, y, button, modifiers)
if new_tiling != None:
stack.append(tiling_drawing)
tiling_drawing = TilingDrawing(new_tiling, 0, 0, *window.get_size())
fwd_stack.clear()
except Exception as e:
raise e
elif button == pyglet.window.mouse.LEFT and (modifiers & pyglet.window.key.MOD_SHIFT
or modifiers & pyglet.window.key.MOD_CTRL):
if modifiers & pyglet.window.key.MOD_SHIFT:
move_type = "point"
elif modifiers & pyglet.window.key.MOD_CTRL:
move_type = "gp"
selected_point = tiling_drawing.get_point_obs_index((x, y))
if selected_point != None:
i,j = selected_point
gploc = tiling_drawing.obstruction_locs[i]
gp = tiling_drawing.tiling.obstructions[i]
else:
selected_point = tiling_drawing.get_point_req_index((x, y))
if selected_point != None:
a,i,j = selected_point
gploc = tiling_drawing.requirement_locs[a][i]
gp = tiling_drawing.tiling.requirements[a][i]
if selected_point != None:
v = gp.patt[j]
cell = gp.pos[j]
loc, sz = tiling_drawing.cell_to_rect(cell)
MIN_SPACE = 10 # SPACING
mnx, mny = loc[0] + MIN_SPACE, loc[1] + MIN_SPACE
mxx, mxy = loc[0]+sz[0] - MIN_SPACE, loc[1]+sz[1] - MIN_SPACE
for k in range(len(gp)):
if k == j-1:
mnx = max(mnx, gploc[k][0] + MIN_SPACE)
if k == j+1:
mxx = min(mxx, gploc[k][0] - MIN_SPACE)
if gp.patt[k] == v-1:
mny = max(mny, gploc[k][1] + MIN_SPACE)
if gp.patt[k] == v+1:
mxy = min(mxy, gploc[k][1] - MIN_SPACE)
point_move_bounds = (mnx, mxx, mny, mxy)
@window.event
def on_mouse_motion(x, y, dx, dy):
global MOUSE_POS
MOUSE_POS = (x, y)
@window.event
def on_mouse_release(x, y, button, modifiers):
global selected_point, point_move_bounds, move_type
if button == pyglet.window.mouse.LEFT:
selected_point = None
point_move_bounds = None
move_type = "none"
@window.event
def on_mouse_drag(x, y, dx, dy, button, modifiers):
global MOUSE_POS
MOUSE_POS = (x, y)
if selected_point == None:
return
elif len(selected_point) == 2:
i,j = selected_point
mnx, mxx, mny, mxy = point_move_bounds
if move_type == "point":
tiling_drawing.obstruction_locs[i][j] = (clamp(x, mnx, mxx), clamp(y, mny, mxy))
else:
for k in range(len(tiling_drawing.obstruction_locs[i])):
ox, oy = tiling_drawing.obstruction_locs[i][k]
ox += dx
oy += dy
tiling_drawing.obstruction_locs[i][k] = (ox, oy)
elif len(selected_point) == 3:
i,j,k = selected_point
mnx, mxx, mny, mxy = point_move_bounds
tiling_drawing.requirement_locs[i][j][k] = (clamp(x, mnx, mxx), clamp(y, mny, mxy))
@window.event
def on_key_press(symbol, modifiers):
global cur_strat, tiling_drawing
global SHOW_LOCALIZED, SHOW_CROSSING
global PRETTY_POINTS, SHADING
global HIGHLIGHT_TOUCHING_CELL
# PREVIOUS STRATEGY
if symbol == pyglet.window.key.LEFT:
cur_strat -= 1
cur_strat %= len(strats)
window.set_caption("Tilings GUI - strat: {}".format(strats[cur_strat].__name__))
# NEXT STRATEGY
if symbol == pyglet.window.key.RIGHT:
cur_strat += 1
cur_strat %= len(strats)
window.set_caption("Tilings GUI - strat: {}".format(strats[cur_strat].__name__))
# PRINT TILING REPR
if symbol == pyglet.window.key.R and modifiers & pyglet.window.key.MOD_CTRL:
print(repr(tiling_drawing.tiling))
# PRINT TILING STRING
if symbol == pyglet.window.key.S and modifiers & pyglet.window.key.MOD_CTRL:
try:
print(str(tiling_drawing.tiling))
except UnicodeEncodeError:
print("Unicode error, make sure your terminal/font support unicode then try running the command:")
print("export PYTHONIOENCODING=UTF-8")
# UNDO
if symbol == pyglet.window.key.BACKSPACE:
if len(stack) >= 1:
fwd_stack.append(tiling_drawing)
tiling_drawing = stack.pop()
(w, h) = window.get_size()
tiling_drawing.set_size(w, h)
# REDO
if symbol == pyglet.window.key.R:
if len(fwd_stack) >= 1:
stack.append(tiling_drawing)
tiling_drawing = fwd_stack.pop()
(w, h) = window.get_size()
tiling_drawing.set_size(w, h)
# TOGGLE LOCALIZED
if symbol == pyglet.window.key.L:
SHOW_LOCALIZED = not SHOW_LOCALIZED
# TOGGLE CROSSING
if symbol == pyglet.window.key.O:
SHOW_CROSSING = not SHOW_CROSSING
# TOGGLE PRETTY POINTS
if symbol == pyglet.window.key.P:
PRETTY_POINTS = not PRETTY_POINTS
# TOGGLE SHADING
if symbol == pyglet.window.key.S:
SHADING = not SHADING
# TOGGLE HIGHLIGHT TOUCHING CELL
if symbol == pyglet.window.key.C:
HIGHLIGHT_TOUCHING_CELL = not HIGHLIGHT_TOUCHING_CELL
@window.event
def on_resize(width, height):
tiling_drawing.set_size(width, height)
def draw():
if any(len(obs) == 0 for obs in tiling_drawing.tiling.obstructions):
fill_background([127,127,127])
else:
fill_background([255,255,255])
tiling_drawing.draw()
def update(time):
pass
def main():
global tiling_drawing
start_tiling = Tiling.from_string(sys.argv[1])
tiling_drawing = TilingDrawing(start_tiling, 0, 0, *window.get_size())
@window.event
def on_draw():
draw()
pyglet.clock.schedule_interval(update, 1/120.0)
pyglet.app.run()
if __name__ == "__main__":
main()