/
sandbox.py
339 lines (272 loc) · 8.74 KB
/
sandbox.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
"""
A small framework for experimentation and visualization. Draws some inspiration
from JavaFX.
"""
from pyglet.window import Window, key
import pyglet
from pyglet.gl import glColor4f, glLineWidth, glBegin, glEnd, glHint
from pyglet.gl import glVertex2f, glClearColor, glEnable, glBlendFunc
from pyglet.gl import glRectf
from pyglet.gl import GL_BLEND, GL_TRIANGLE_FAN, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP
from pyglet.gl import GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA
from pyglet.gl import GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA
from pyglet.gl import GL_POINT_SMOOTH, GL_POINT_SMOOTH_HINT
from pyglet.gl import GL_LINE_SMOOTH, GL_LINE_SMOOTH_HINT
from pyglet.gl import GL_POLYGON_SMOOTH, GL_POLYGON_SMOOTH_HINT, GL_NICEST
from pyglet.gl import GL_POINTS
from math import pi, cos, sin, sqrt
import os
class Point:
def __init__(self, *args):
self.x = 0
self.y = 0
self.set(*args)
def set(self, *args):
if len(args) == 0:
self.x = 0
self.y = 0
elif len(args) == 1 and type(args[0]) == Point:
self.x = args[0].x
self.y = args[0].y
elif len(args) == 2:
self.x = args[0]
self.y = args[1]
def distance(self, other):
dx = self.x - other.x
dy = self.y - other.y
return sqrt(dx * dx + dy * dy)
def __repr__(self):
return "<Point {} {}>".format(float(self.x), float(self.y))
class Box:
def __init__(self, *args):
if len(args) == 4:
self._pos = Point(args[0], args[1])
self._width = args[2]
self._height = args[3]
elif len(args) == 1 and isinstance(args[0], Box):
self._pos = Point(args[0]._pos)
self._width = args[0]._width
self._height = args[0]._height
@property
def left(self):
return self._pos.x - self._width // 2
@property
def right(self):
return self._pos.x + self._width // 2
@property
def top(self):
return self._pos.y - self._height // 2
@property
def bottom(self):
return self._pos.y + self._height // 2
@property
def width(self):
return self._width
@property
def height(self):
return self._height
class GraphicsContext:
def __init__(self):
self._stroke_color = (1, 1, 1, 1)
self._fill_color = (1, 1, 1, 1)
self._line_width = 1
glEnable(GL_POINT_SMOOTH)
glEnable(GL_LINE_SMOOTH)
glEnable(GL_POLYGON_SMOOTH)
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST)
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST)
def _color(self, *args):
c = list(*args)
if len(c) == 3:
c.append(1)
return c
def set_fill(self, *args):
self._fill_color = self._color(args)
def set_stroke(self, *args):
self._stroke_color = self._color(args)
def set_line_width(self, width):
self._line_width = width
def _circle(self, point, r, color, filled):
c = 2 * r * pi
iterations = int(c / 2)
s = sin(2 * pi / iterations)
c = cos(2 * pi / iterations)
dx, dy = r, 0
glColor4f(*color)
if filled:
glBegin(GL_TRIANGLE_FAN)
glVertex2f(point.x, point.y)
else:
glLineWidth(self._line_width)
glBegin(GL_LINE_LOOP)
glVertex2f(point.x + dx, point.y + dy)
for i in range(int(iterations) + 1):
glVertex2f(point.x + dx, point.y + dy)
dx, dy = (dx * c - dy * s), (dy * c + dx * s)
glEnd()
def stroke_circle(self, point, r):
self._circle(point, r, self._stroke_color, False)
def fill_circle(self, point, r):
self._circle(point, r, self._fill_color, True)
def _decode_xywh(self, *args):
if len(args) == 4:
x = args[0]
y = args[1]
w = args[2]
h = args[3]
elif len(args) == 2 and isinstance(args[0], Point) and isinstance(args[1], Point):
x = args[0].x
y = args[0].y
w = args[1].x
h = args[1].y
else:
raise ValueError()
return x, y, w, h
def fill_rect(self, *args):
x, y, w, h = self._decode_xywh(*args)
glColor4f(*self._fill_color)
glRectf(x, y, x + w, y + h)
def stroke_rect(self, *args):
x, y, w, h = self._decode_xywh(*args)
glColor4f(*self._stroke_color)
glBegin(GL_LINE_LOOP)
glVertex2f(x, y)
glVertex2f(x + w, y)
glVertex2f(x + w, y + h)
glVertex2f(x, y + h)
glEnd()
def lines(self, *points):
glColor4f(*self._stroke_color)
glLineWidth(self._line_width)
glBegin(GL_LINE_STRIP)
for point in points:
glVertex2f(point.x, point.y)
glEnd()
def line_loop(self, *points):
if len(points) <= 2:
raise ValueError()
glColor4f(*self._stroke_color)
glLineWidth(self._line_width)
glBegin(GL_LINE_LOOP)
for point in points:
glVertex2f(point.x, point.y)
glEnd()
def point(self, *args):
if len(args) == 2:
x, y = args[0], args[1]
if len(args) == 1 and type(args[0]) == Point:
x, y = args.x, args.y
glColor4f(*self._stroke_color)
glBegin(GL_POINTS)
glVertex2f(x, y)
glEnd()
class Mouse:
def __init__(self):
self._buttons = [False] * 8
self._x = 0
self._y = 0
self._dx = 0
self._dy = 0
@property
def left_button(self):
return self._buttons[1]
@property
def middle_button(self):
return self._buttons[2]
@property
def right_button(self):
return self._buttons[4]
@property
def x(self):
return self._x
@property
def y(self):
return self._y
@property
def pos(self):
return Point(self._x, self._y)
class Application(Window):
def __init__(self):
self._width = 800
self._height = 600
super().__init__(width=self._width,
height=self._height,
resizable=False,
vsync=False)
self._mouse = Mouse()
self._debug = False
try:
if os.environ['DEBUG'] in ('1', 'TRUE', 'True'):
self.push_handlers(pyglet.window.event.WindowEventLogger())
self._debug = True
except KeyError:
pass
glClearColor(0.0, 0.05, 0.2, 1.0)
glEnable(GL_BLEND)
glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA)
self._gc = GraphicsContext()
pyglet.clock.schedule_interval(self._main_loop, 1 / 60)
self.set_mouse_visible(False)
@property
def debug(self):
return self._debug
def _main_loop(self, dt):
self.do_update(dt)
def get_width(self):
return self._width
def get_height(self):
return self._height
def do_init(self):
pass
def do_update(self, dt):
pass
def do_draw(self, gc):
pass
def do_mouse_motion(self, point, dx, dy):
pass
def do_mouse_press(self, point, button, modifiers):
pass
def do_mouse_drag(self, point, dx, dy, buttons, modifiers):
pass
def do_mouse_release(self, point, button, modifiers):
pass
def do_key_press(self, symbol, modifiers):
pass
def do_key_release(self, symbol, modifiers):
pass
def on_draw(self):
self.do_draw(self._gc)
@property
def mouse(self):
return self._mouse
def on_mouse_motion(self, x, y, dx, dy):
self._mouse._x = x
self._mouse._y = y
self._mouse._dx = dx
self._mouse._dy = dy
self.do_mouse_motion(Point(x, y), dx, dy)
def on_mouse_press(self, x, y, button, modifiers):
self._mouse._buttons[button] = True
self.do_mouse_press(Point(x, y), button, modifiers)
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
self._mouse._x = x
self._mouse._y = y
self._mouse._dx = dx
self._mouse._dy = dy
self.do_mouse_drag(Point(x, y), dx, dy, buttons, modifiers)
def on_mouse_release(self, x, y, button, modifiers):
self._mouse._buttons[button] = False
self.do_mouse_release(Point(x, y), button, modifiers)
def on_key_press(self, symbol, modifiers):
if symbol == key.ESCAPE:
self.stop()
else:
self.do_key_press(symbol, modifiers)
def on_key_release(self, symbol, modifiers):
self.do_key_release(symbol, modifiers)
def stop(self):
pyglet.app.exit()
def start(self):
self.do_init()
pyglet.app.run()