-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrawing.py
More file actions
293 lines (242 loc) · 12.6 KB
/
drawing.py
File metadata and controls
293 lines (242 loc) · 12.6 KB
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
import pygame
import numpy as np
class Drawing:
def __init__(self,
window,
rect,
size=(300, 300),
default_color=(255, 255, 255),
zoom_factor=4,
autocompletion=True
):
self.window = window
self.rect = rect
self.size = size
self.default_color = default_color
self.zoom_factor = zoom_factor
self.autocompletion = autocompletion
if self.autocompletion:
self.MODES = {'d': self.drawing_draw_autocomplete, 'e': self.drawing_erase, 'f': self.drawing_fill,
'r': self.drawing_replace, 'c': self.drawing_clear}
else:
self.MODES = {'d': self.drawing_draw, 'e': self.drawing_erase, 'f': self.drawing_fill,
'r': self.drawing_replace, 'c': self.drawing_clear}
self.rect = (rect[0], rect[1], self.size[0] * self.zoom_factor, self.size[1] * self.zoom_factor)
self.drawing = np.full((self.size[0], self.size[1], 3), -1)
self.shown_drawing = self.drawing
self.shown_drawing[self.shown_drawing == -1] = 255
self.last_point = False
def handle_events(self,
events,
mouse_pos,
mouse_click,
mode,
color,
brushsize):
if mouse_click[0]:
if self.rect[0] < mouse_pos[0] < self.rect[0] + self.rect[2]:
mouse_pos = self.calculate_relative_pos(mouse_pos)
self.MODES[mode](mouse_pos, color, brushsize)
if mode != 'd':
self.last_point = False
return False
elif mode == 'c':
self.MODES['c'](mouse_pos, color, brushsize)
elif mode == 'd':
self.last_point = False
return True
def calculate_relative_pos(self, mouse_pos):
return (mouse_pos[0] - self.rect[0]) // self.zoom_factor, (mouse_pos[1] - self.rect[1]) // self.zoom_factor
def drawing_draw_brush_size(self, pos, brushsize, color):
self.drawing[pos] = color
for i in range(-brushsize + 1, brushsize - 1):
for j in range(-brushsize + 1, brushsize - 1):
self.drawing[
max(0, min(pos[0] + i, self.size[0] - 1)), max(0, min(pos[1] + j, self.size[1] - 1))] = color
def drawing_draw_autocomplete(self, mouse_pos, color, brushsize):
if self.last_point:
pos = (mouse_pos[0] - self.last_point[0], mouse_pos[1] - self.last_point[1])
pre = (1 if abs(pos[0]) == pos[0] else -1, 1 if abs(pos[1]) == pos[1] else -1)
if pos[0] == 0 and pos[1] != 0:
for i in range(0, pos[1], pre[1]):
self.drawing_draw_brush_size((self.last_point[0], self.last_point[1] + i), brushsize, color)
elif pos[1] == 0 and pos[0] != 0:
for i in range(0, pos[0], pre[0]):
self.drawing_draw_brush_size((self.last_point[0] + i, self.last_point[1]), brushsize, color)
elif pos[1] != 0 and pos[0] != 0:
if abs(pos[1]) > abs(pos[0]):
ratio = pos[1] / pos[0]
for i in range(0, pos[1], pre[1]):
self.drawing_draw_brush_size(
(int(self.last_point[0] + (i // ratio)), int(self.last_point[1] + i)), brushsize, color)
else:
ratio = pos[0] / pos[1]
for i in range(0, pos[0], pre[0]):
self.drawing_draw_brush_size(
(int(self.last_point[0] + i), int(self.last_point[1] + i // ratio)), brushsize, color)
self.drawing_draw_brush_size(mouse_pos, brushsize, color)
self.last_point = mouse_pos
def drawing_draw(self, mouse_pos, color, brushsize):
self.drawing_draw_brush_size(mouse_pos, brushsize, color)
def drawing_erase(self, mouse_pos, color, brushsize):
if self.autocompletion:
self.drawing_draw_autocomplete(mouse_pos, -1, brushsize)
else:
self.drawing_draw_brush_size(mouse_pos, brushsize, color)
def drawing_fill(self, mouse_pos, color, brushsize):
f_color = self.drawing[mouse_pos].copy()
self.drawing[mouse_pos] = color
up_list = []
down_list = []
left_list = []
right_list = []
dones = []
done = False
while not done:
smth = False
up_old_list = up_list.copy()
down_old_list = down_list.copy()
left_old_list = left_list.copy()
right_old_list = right_list.copy()
if len(right_list) == len(left_list) == len(down_list) == len(up_list) == 0:
if np.array_equal(self.drawing[mouse_pos[0], mouse_pos[1] - 1], f_color):
up_list.append((mouse_pos[0], mouse_pos[1] - 1))
smth = True
self.drawing[mouse_pos[0], mouse_pos[1] - 1] = color
if np.array_equal(self.drawing[mouse_pos[0], mouse_pos[1] + 1], f_color):
smth = True
down_list.append((mouse_pos[0], mouse_pos[1] + 1))
self.drawing[mouse_pos[0], mouse_pos[1] + 1] = color
if np.array_equal(self.drawing[mouse_pos[0] - 1, mouse_pos[1]], f_color):
smth = True
left_list.append((mouse_pos[0] - 1, mouse_pos[1]))
self.drawing[mouse_pos[0] - 1, mouse_pos[1]] = color
if np.array_equal(self.drawing[mouse_pos[0] + 1, mouse_pos[1]], f_color):
smth = True
right_list.append((mouse_pos[0] + 1, mouse_pos[1]))
self.drawing[mouse_pos[0] + 1, mouse_pos[1]] = color
second_up_list = []
second_down_list = []
second_right_list = []
second_left_list = []
for idx, i in enumerate(up_list):
if not i in dones and not (i[0] + 1 >= self.size[0] or i[1] + 1 >= self.size[1] or i[0] == 0 or i[1] == 0):
dones.append(i)
second_up_list.append(i)
for idx, i in enumerate(down_list):
if not i in dones and not (i[0] + 1 >= self.size[0] or i[1] + 1 >= self.size[1] or i[0] == 0 or i[1] == 0):
dones.append(i)
second_down_list.append(i)
for idx, i in enumerate(right_list):
if not i in dones and not (i[0] + 1 >= self.size[0] or i[1] + 1 >= self.size[1] or i[0] == 0 or i[1] == 0):
dones.append(i)
second_right_list.append(i)
for idx, i in enumerate(left_list):
if not i in dones and not (i[0] + 1 >= self.size[0] or i[1] + 1 >= self.size[1] or i[0] == 0 or i[1] == 0):
dones.append(i)
second_left_list.append(i)
up_list = second_up_list
down_list = second_down_list
right_list = second_right_list
left_list = second_left_list
if len(right_list) == len(left_list) == len(down_list) == len(up_list) == 0:
done = True
return
else:
up_list = []
down_list = []
left_list = []
right_list = []
for i in up_old_list:
if i[1] - 1 > -1:
if np.array_equal(self.drawing[i[0], i[1] - 1], f_color):
up_list.append((i[0], i[1] - 1))
self.drawing[i[0], i[1] - 1] = color
if i[0] - 1 > -1:
if np.array_equal(self.drawing[i[0] - 1, i[1]], f_color):
left_list.append((i[0] - 1, i[1]))
self.drawing[i[0] - 1, i[1]] = color
if i[0] + 1 < self.size[0]:
if np.array_equal(self.drawing[i[0] + 1, i[1]], f_color):
right_list.append((i[0] + 1, i[1]))
self.drawing[i[0] + 1, i[1]] = color
for i in down_old_list:
if i[1] + 1 < self.size[1]:
if np.array_equal(self.drawing[i[0], i[1] + 1], f_color):
down_list.append((i[0], i[1] + 1))
self.drawing[i[0], i[1] + 1] = color
if i[0] - 1 > -1:
if np.array_equal(self.drawing[i[0] - 1, i[1]], f_color):
left_list.append((i[0] - 1, i[1]))
self.drawing[i[0] - 1, i[1]] = color
if i[0] + 1 < self.size[0]:
if np.array_equal(self.drawing[i[0] + 1, i[1]], f_color):
right_list.append((i[0] + 1, i[1]))
self.drawing[i[0] + 1, i[1]] = color
for i in left_old_list:
if i[1] - 1 > -1:
if np.array_equal(self.drawing[i[0], i[1] - 1], f_color):
up_list.append((i[0], i[1] - 1))
self.drawing[i[0], i[1] - 1] = color
if i[1] + 1 < self.size[1]:
if np.array_equal(self.drawing[i[0], i[1] + 1], f_color):
down_list.append((i[0], i[1] + 1))
self.drawing[i[0], i[1] + 1] = color
if i[0] - 1 > -1:
if np.array_equal(self.drawing[i[0] - 1, i[1]], f_color):
left_list.append((i[0] - 1, i[1]))
self.drawing[i[0] - 1, i[1]] = color
for i in right_old_list:
if i[1] - 1 > -1:
if np.array_equal(self.drawing[i[0], i[1] - 1], f_color):
up_list.append((i[0], i[1] - 1))
self.drawing[i[0], i[1] - 1] = color
if i[1] + 1 < self.size[1]:
if np.array_equal(self.drawing[i[0], i[1] + 1], f_color):
down_list.append((i[0], i[1] + 1))
self.drawing[i[0], i[1] + 1] = color
if i[0] + 1 < self.size[0]:
if np.array_equal(self.drawing[i[0] + 1, i[1]], f_color):
right_list.append((i[0] + 1, i[1]))
self.drawing[i[0] + 1, i[1]] = color
second_up_list = []
second_down_list = []
second_right_list = []
second_left_list = []
for idx, i in enumerate(up_list):
if not i in dones:
dones.append(i)
second_up_list.append(i)
for idx, i in enumerate(down_list):
if not i in dones:
dones.append(i)
second_down_list.append(i)
for idx, i in enumerate(right_list):
if not i in dones:
dones.append(i)
second_right_list.append(i)
for idx, i in enumerate(left_list):
if not i in dones:
dones.append(i)
second_left_list.append(i)
up_list = second_up_list.copy()
down_list = second_down_list.copy()
right_list = second_right_list.copy()
left_list = second_left_list.copy()
if len(right_list) == len(left_list) == len(down_list) == len(up_list) == 0:
done = True
return
def drawing_replace(self, mouse_pos, color, brushsize):
r_color = self.drawing[mouse_pos].copy()
for i in range(self.size[0]):
for j in range(self.size[1]):
if np.array_equal(self.drawing[i, j], r_color):
self.drawing[i, j] = color
def drawing_clear(self, mouse_pos, color, brushsize):
self.drawing = np.full((self.size[0], self.size[1], 3), -1)
def draw(self, mouse_pos):
self.shown_drawing = self.drawing
self.shown_drawing[self.shown_drawing == -1] = 255
self.window.blit(pygame.transform.scale(pygame.surfarray.make_surface(self.shown_drawing),
((self.size[0] * self.zoom_factor), (self.size[1] * self.zoom_factor))),
self.rect[:2])