-
Notifications
You must be signed in to change notification settings - Fork 0
/
paint.py
182 lines (146 loc) · 7.66 KB
/
paint.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
import pygame
import numpy as np
from buttons import TextButton, ColorButton
from drawing import Drawing
# TODO: clean up everything, make different lists for colorbuttons, modebuttons, brushsizebuttons and whatsoever
# TODO: make the buttons alright
pygame.init()
class PAINT3R:
def __init__(self, drawing_size=(300, 300), window_size=(800, 600)):
self.drawing_size = drawing_size
self.window_size = window_size
self.window = pygame.display.set_mode(window_size)
pygame.display.set_caption('GNU PAINT3R')
self.drawing = Drawing(window=self.window,
rect=(0,0),
size=self.drawing_size,
zoom_factor=2)
self.clock = pygame.time.Clock()
self.running = False
self.MODE_BUTTONS = [TextButton(self.window, (self.window_size[0] - 50, 75), (75, 75), 'Draw'),
TextButton(self.window, (self.window_size[0] - 50, 150), (75, 75), 'Erase'),
TextButton(self.window, (self.window_size[0] - 50, 225), (75, 75), 'Fill'),
TextButton(self.window, (self.window_size[0] - 50, 300), (75, 75), 'Replace'),
TextButton(self.window, (self.window_size[0] - 50, 375), (75, 75), 'Clear')
]
self.MODE_BUTTONS[0].selected = True
self.mode = 'd'
self.modes = ['d', 'e', 'f', 'r', 'c']
self.BRUSHSIZE_BUTTONS = [TextButton(self.window, (self.window_size[0] - 50, 425), (25, 25), '1'),
TextButton(self.window, (self.window_size[0] - 50, 450), (25, 25), '2'),
TextButton(self.window, (self.window_size[0] - 50, 475), (25, 25), '3'),
TextButton(self.window, (self.window_size[0] - 50, 500), (25, 25), '4'),
TextButton(self.window, (self.window_size[0] - 50, 525), (25, 25), '5')
]
self.BRUSHSIZE_BUTTONS[0].selected = True
self.brushsize = 1
self.colors = ((255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 255, 255), (0, 0, 0))
self.COLOR_BUTTONS = [ColorButton(window=self.window,
center=(self.window_size[0] - 170, 75),
size=(50, 50),
base_color=(225, 0, 0),
light_color=(255, 0, 0)
),
ColorButton(window=self.window,
center=(self.window_size[0] - 120, 75),
size=(50, 50),
base_color=(0, 225, 0),
light_color=(0, 255, 0)
),
ColorButton(window=self.window,
center=(self.window_size[0] - 120, 125),
size=(50, 50),
base_color=(0, 0, 225),
light_color=(0, 0, 255)
),
ColorButton(window=self.window,
center=(self.window_size[0] - 170, 125),
size=(50, 50),
base_color=(225, 225, 0),
light_color=(255, 255, 0)
),
ColorButton(window=self.window,
center=(self.window_size[0] - 120, 175),
size=(50, 50),
base_color=(225, 225, 225),
light_color=(255, 255, 255)
),
ColorButton(window=self.window,
center=(self.window_size[0] - 170, 175),
size=(50, 50),
base_color=(10, 10, 10),
light_color=(20, 20, 20)
),
]
self.COLOR_BUTTONS[0].selected = True
self.color = self.colors[0]
def draw(self, mouse_pos, mouse_click):
self.window.fill((0, 0, 0))
for button in self.MODE_BUTTONS:
button.draw(mouse_pos)
for button in self.MODE_BUTTONS:
if button.selected:
button.draw(mouse_pos)
for button in self.COLOR_BUTTONS:
button.draw(mouse_pos)
for button in self.COLOR_BUTTONS:
if button.selected:
button.draw(mouse_pos)
for button in self.BRUSHSIZE_BUTTONS:
button.draw(mouse_pos)
for button in self.BRUSHSIZE_BUTTONS:
if button.selected:
button.draw(mouse_pos)
self.drawing.draw(mouse_pos)
pygame.display.update()
@staticmethod
def get_events():
return pygame.event.get(), pygame.mouse.get_pos(), pygame.mouse.get_pressed()
def handle_events(self, events, mouse_pos, mouse_click):
for event in events:
if event.type == pygame.QUIT:
self.running = False
if self.drawing.handle_events(events, mouse_pos, mouse_click, self.mode, self.color, self.brushsize):
mode_ds = False
for idx in range(len(self.MODE_BUTTONS)):
self.MODE_BUTTONS[idx].handle_events(events, mouse_pos, mouse_click)
if self.MODE_BUTTONS[idx].directly_selected:
mode_ds = True
self.mode = self.modes[idx]
else:
mode_ds = False
for idx in range(len(self.MODE_BUTTONS)):
if self.modes[idx] != self.mode:
self.MODE_BUTTONS[idx].selected = False
if not mode_ds:
color_ds = False
for idx in range(len(self.COLOR_BUTTONS)):
self.COLOR_BUTTONS[idx].handle_events(events, mouse_pos, mouse_click)
if self.COLOR_BUTTONS[idx].directly_selected:
self.color = self.colors[idx]
color_ds = True
for idx in range(len(self.COLOR_BUTTONS)):
if self.colors[idx] != self.color:
self.COLOR_BUTTONS[idx].selected = False
if not color_ds:
for idx in range(len(self.BRUSHSIZE_BUTTONS)):
self.BRUSHSIZE_BUTTONS[idx].handle_events(events, mouse_pos, mouse_click)
if self.BRUSHSIZE_BUTTONS[idx].directly_selected:
self.brushsize = idx+1
for idx in range(len(self.BRUSHSIZE_BUTTONS)):
if idx+1 != self.brushsize:
self.BRUSHSIZE_BUTTONS[idx].selected = False
def main_loop(self):
self.running = True
while self.running:
# Sense everything
events, mouse_pos, mouse_click = self.get_events()
# Handle them
self.handle_events(events, mouse_pos, mouse_click)
# And now draw it
self.draw(mouse_pos, mouse_click)
#self.clock.tick(10)
pygame.quit()
if __name__ == '__main__':
painter = PAINT3R()
painter.main_loop()