-
Notifications
You must be signed in to change notification settings - Fork 5
/
sprite_display.py
164 lines (137 loc) · 5.69 KB
/
sprite_display.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
import cairo
from display import (CellContext, HorizEdgeContext, PointContext,
VertEdgeContext, get_surface, show_surface, transform_x,
transform_y)
from sprite import south, east, west, str_to_dir
class FramedMixin(object):
@property
def gx(self):
return super(FramedMixin, self).gx + self.offset_gx
@property
def gy(self):
return super(FramedMixin, self).gy + self.offset_gy
class FramedCellContext(FramedMixin, CellContext):
def __init__(self, surface, cell, model, scale, offset_gx, offset_gy, t):
CellContext.__init__(self, surface, cell, model, scale)
self.offset_gx = offset_gx
self.offset_gy = offset_gy
self.t = t
class FramedHorizEdgeContext(FramedMixin, HorizEdgeContext):
def __init__(self, surface, edge, model, scale, offset_gx, offset_gy, t):
HorizEdgeContext.__init__(self, surface, edge, model, scale)
self.offset_gx = offset_gx
self.offset_gy = offset_gy
self.t = t
class FramedVertEdgeContext(FramedMixin, VertEdgeContext):
def __init__(self, surface, edge, model, scale, offset_gx, offset_gy, t):
VertEdgeContext.__init__(self, surface, edge, model, scale)
self.offset_gx = offset_gx
self.offset_gy = offset_gy
self.t = t
class FramedPointContext(FramedMixin, PointContext):
def __init__(self, surface, point, model, scale, offset_gx, offset_gy, t):
PointContext.__init__(self, surface, point, model, scale)
self.offset_gx = offset_gx
self.offset_gy = offset_gy
self.t = t
class SpriteContext(object):
def __init__(self, surface, sprite, model, scale, offset_gx, offset_gy, t):
self.surface = surface
self.ctx = cairo.Context(surface)
self.sprite = sprite
self.model = model
self.scale = scale
self.offset_gx = offset_gx
self.offset_gy = offset_gy
self.t = t
def gx(self, t=None):
if t is None:
t = self.t
return self.model.eval(self.sprite.x(t)).as_long()
def gy(self, t=None):
if t is None:
t = self.t
return self.model.eval(self.sprite.y(t)).as_long()
def dir(self, t=None):
if t is None:
t = self.t
return str_to_dir[str(self.model.eval(self.sprite.dir(t)))]
def x(self, t):
return transform_x(self.gx(t) + self.offset_gx + 0.5, self.scale)
def y(self, t):
return transform_y(self.gy(t) + self.offset_gy + 0.5, self.scale)
def draw_rotated(self, path, t=None, width=1, color=(0, 0, 0, 1)):
r, g, b, a = color
self.ctx.set_source_rgba(b, g, r, a)
self.ctx.set_line_width(width)
self.ctx.set_line_join(cairo.LINE_JOIN_BEVEL)
d = self.dir(t)
dx = self.gx(t) + self.offset_gx
dy = self.gy(t) + self.offset_gy
first = True
for x, y in path:
if d == south:
x, y = 1 - x, 1 - y
elif d == east:
x, y = 1 - y, x
elif d == west:
x, y = y, 1 - x
x += dx
y += dy
if first:
self.ctx.move_to(transform_x(x, self.scale),
transform_y(y, self.scale))
first = False
else:
self.ctx.line_to(transform_x(x, self.scale),
transform_y(y, self.scale))
self.ctx.stroke()
def path(self, from_t, to_t, width=1, color=(0, 0, 0, 1)):
r, g, b, a = color
self.ctx.set_source_rgba(b, g, r, a)
self.ctx.set_line_width(width)
self.ctx.set_line_join(cairo.LINE_JOIN_BEVEL)
self.ctx.move_to(self.x(from_t), self.y(from_t))
for t in range(from_t + 1, to_t):
self.ctx.line_to(self.x(t), self.y(t))
self.ctx.stroke()
def draw_grid_frames_and_sprites(grid, model, scale, frame_rows, sprites,
cell_fn=None, horiz_fn=None, vert_fn=None,
point_fn=None, sprite_fn=None):
frame_w = max(map(len, frame_rows))
frame_h = len(frame_rows)
canvas_w = ((grid.width + 1) * frame_w) * scale
canvas_h = ((grid.height + 1) * frame_h) * scale
surface = get_surface(canvas_w, canvas_h)
i = 0
for y0, row in enumerate(frame_rows):
for x0, t in enumerate(row):
x = x0 * (grid.width + 1)
y = y0 * (grid.height + 1)
if cell_fn:
for cell in grid.cells:
cell_ctx = FramedCellContext(surface, cell, model, scale,
x, y, t)
cell_fn(cell_ctx, i)
if horiz_fn:
for horiz in grid.horizs:
horiz_ctx = FramedHorizEdgeContext(surface, horiz, model,
scale, x, y, t)
horiz_fn(horiz_ctx, i)
if vert_fn:
for vert in grid.verts:
vert_ctx = FramedVertEdgeContext(surface, vert, model,
scale, x, y, t)
vert_fn(vert_ctx, i)
if point_fn:
for point in grid.points:
point_ctx = FramedPointContext(surface, point, model,
scale, x, y, t)
point_fn(point_ctx, i)
if sprite_fn:
for sprite in sprites:
sprite_ctx = SpriteContext(surface, sprite, model, scale,
x, y, t)
sprite_fn(sprite_ctx, i)
i += 1
show_surface(surface)