-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.py
335 lines (248 loc) · 8.4 KB
/
canvas.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
import numpy as np
import sys
from collections import OrderedDict
from drawable import colors
from drawable.drawable import Text
from utils import ensureArray
from backends.pygame_backend import PygameBackend
from time import sleep
from ui import Menu
def panning(self, pos, rel):
if self.backend.isKeyPressed("space") and self.backend.isButtonPressed(1):
self.center += rel
def screenCap(self, key):
if key == 'p':
self.backend.saveFrame("screen.jpg")
def zooming(self, pos, button): # TODO
if button == 4:
amount = 5 if self.backend.isKeyPressed("left shift") else 1
if self.ratio - amount > 5:
self.ratio -= amount
elif button == 5:
amount = 5 if self.backend.isKeyPressed("left shift") else 1
self.ratio += amount
def printPoint(self, pos, button):
if button == 1:
print(pos, self.inverseTransform(pos))
def setTextToTime(self):
self.content = str(self.canvas.time / 1000)
def openMenuFunction(keybind):
def openMenu(self, key):
if key == keybind:
self.displayMenu()
return openMenu
class EventHandler:
def __init__(self, types):
self.handlers = {t: [] for t in types}
def add(self, *types):
for t in types:
self.handlers[t] = []
def on(self, type, function):
self.handlers[type].append(function)
def fire(self, canvas, type, *args, **kwargs):
for handler in self.handlers[type]:
handler(canvas, *args, **kwargs)
class Canvas:
def __init__(self, size, **kwargs):
self.objects = {}
self.nextObjectId = 0
self.animations = {}
self.nextAnimationId = 0
self.size = size
self.backgroundColor = kwargs.get("backgroundColor", colors.background_color)
self.defaultColor = kwargs.get("defaultColor", colors.default_color)
self.ratio = kwargs.get("ratio", 1.0)
self.center = ensureArray(kwargs.get("center", [self.size[0]/2, self.size[1]/2]))
self.backend = kwargs.get("backend", PygameBackend)(size, self, **kwargs)
self.time = kwargs.get("startTime", 0)
self.deltaTime = None
self.data = OrderedDict({})
self.debug = kwargs.get("debug", False)
self.handler = EventHandler(types=["tick"])
self.on("tick", type(self).onTick)
self.fps = self.backend.fps
self.frame_time = 1000 / self.fps
if self.backend.interactive: self.init_interactive(**kwargs)
if self.debug: self.init_debug(**kwargs)
def init_debug(self, **kwargs):
self.addObject(Text((0, 0), "", "monospace", color=colors.black, onBeforeDraw=setTextToTime,
bindings={"start": lambda: self.inverseTransform((10, self.size[1] - 20))}))
#
#
# EVENTS
#
#
def fire(self, type, *args, **kwargs):
self.handler.fire(self, type, *args, **kwargs)
def on(self, type, function):
self.handler.on(type, function)
def init_interactive(self, **kwargs):
self.handler.add("mouseDown", "mouseUp", "mouseMove", "keyPress")
self.menu = Menu(self)
self.menu.addOption("Quit", lambda canvas: sys.exit(), hotkey='q')
if kwargs.get("movement", True):
self.handler.on("mouseDown", zooming)
self.handler.on("mouseMove", panning)
if kwargs.get("screenCap", True):
self.handler.on("keyPress", screenCap)
if self.debug:
self.handler.on("mouseDown", printPoint)
self.handler.on("keyPress", openMenuFunction(kwargs.get("menu_key", 'e')))
def displayMenu(self):
print("\n" + '\n'.join([f"[{option.hotkey}] {option.name}" for option in self.menu.getOptions() if option.enabled]))
self.menu.choose(input('> ')).execute()
def onTick(self, time_passed):
pass
#
#
# GEOMETRY
#
#
@property
def min_x(self):
return -self.center[0] / self.ratio
@property
def max_x(self):
return (self.size[0] - self.center[0]) / self.ratio
@property
def min_y(self):
return -(self.size[1] - self.center[1]) / self.ratio
@property
def max_y(self):
return self.center[1] / self.ratio
@property
def transformation(self):
return self.backend.transformation @ [[self.ratio, 0],
[0, self.ratio]]
@property
def inverse_transformation(self):
return self.backend.inverse_transformation @ [[1.0/self.ratio, 0],
[0, 1.0/self.ratio]]
def transform(self, point):
return tuple([int(x) for x in (self.transformation @ point + self.center)])
def inverseTransform(self, point):
return self.inverse_transformation @ (point - self.center)
def inRange(self, point):
return (self.min_x <= point[0] <= self.max_x and self.min_y < point[1] < self.max_y)
#
#
# OBJECTS
#
#
def addObject(self, object):
object.attachTo(self)
return object.id
def getObjectId(self):
self.nextObjectId += 1
return f"sprite_{self.nextObjectId - 1}"
def getObjectColor(self):
return self.defaultColor # TODO
#
#
# ANIMATIONS
#
#
def addAnimation(self, animation):
animation.attachTo(self)
return animation.id
def getAnimationId(self):
self.nextAnimationId += 1
return f"animation_{self.nextAnimationId - 1}"
def pauseAnimations(self):
for id, animation in self.animations.items():
animation.pause()
def unpauseAnimations(self):
for id, animation in self.animations.items():
animation.unpause()
def toggleAnimations(self):
for id, animation in self.animations.items():
animation.togglePause()
#
#
# FRAMES
#
#
def newFrame(self):
self.backend.fill(self.backgroundColor)
def advance(self, time_passed=None): # milliseconds
if time_passed is None:
time_passed = self.frame_time
self.newFrame()
self.deltaTime = time_passed
self.time += self.deltaTime
self.update(time_passed)
def play(self, time): # seconds
n_frames = int(time * self.fps)
for i in range(1, n_frames + 1):
if self.debug:
print(f"Playing {int((i / n_frames) * 100)}%\r", end="")
self.advance()
def jump(self, time):
self.newFrame()
self.deltaTime = None
self.time = time
self.update()
def update(self, time_passed = None):
for id, obj in self.objects.items():
obj.beforeDraw()
obj.draw()
self.fire("tick", time_passed)
self.backend.update()
def loop(self, frequency=None): # frequency in Hz; Can't be stopped
if not self.backend.interactive:
raise NotImplemented
while 1:
if frequency is None:
frequency = self.backend.fps
self.advance(int(1000 / frequency)) # in ms
sleep(1 / frequency) # in seconds
#
#
# SAVING
#
#
def startRecording(self, filename, **kwargs):
self.backend.startRecording(filename, **kwargs)
def stopRecording(self):
self.backend.stopRecording()
def saveFrame(self, filename):
self.backend.saveFrame(filename)
#
#
# DRAWING
#
#
def putPixel(self, position, color):
self.backend.putPixel(self.transform(position), color)
def drawLine(self, start, end, color, **kwargs):
self.backend.drawLine(self.transform(start), self.transform(end), tuple(color), **kwargs)
def drawRectangle(self, start, width, height, color, **kwargs):
self.backend.drawRectangle(self.transform(start), int(width * self.ratio), int(height * self.ratio), tuple(color), **kwargs)
def drawCircle(self, center, radius, color, **kwargs):
self.backend.drawCircle(self.transform(center), int(radius * self.ratio), tuple(color), **kwargs)
def drawPolygon(self, points, color, **kwargs):
self.backend.drawPolygon([self.transform(p) for p in points], tuple(color), **kwargs)
def drawConvexPolygon(self, points, color, **kwargs):
self.backend.drawConvexPolygon([self.transform(p) for p in points], tuple(color), **kwargs)
def drawImage(self, point, image):
self.backend.drawImage(self.transform(point), image)
def drawArrow(self, start, end, color):
length = np.sqrt((start[0] - end[0])**2 + (start[1] - end[1])**2)
p1, p2 = arrow_points(start, end, min(length / 4, .20), np.pi/6)
p1, p2, start, end = map(self.transform, [p1, p2, start, end])
self.backend.drawLine(start, end, tuple(color), width=2)
self.backend.drawConvexPolygon([end, p1, p2], tuple(color))
def drawText(self, point, text, font, color, **kwargs):
self.backend.drawText(self.transform(point), text, font, tuple(color), **kwargs)
def arrow_points(start, end, length, target_angle):
if start[0] == end[0]:
line_angle = np.pi/2 if start[1] < end[1] else -np.pi/2
else:
line_angle = np.arctan((start[1] - end[1]) / (start[0] - end[0]))
arrow_angle_1 = line_angle - target_angle
arrow_angle_2 = np.pi / 2 - line_angle - target_angle
sign_x = 1 if start[0] > end[0] else -1
sign_y = sign_x
arrow_point_1 = (end[0] + sign_x * length * np.cos(arrow_angle_1), end[1] + sign_y * length * np.sin(arrow_angle_1))
arrow_point_2 = (end[0] + sign_x * length * np.sin(arrow_angle_2), end[1] + sign_y * length * np.cos(arrow_angle_2))
return (arrow_point_1, arrow_point_2)