-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathdrawing_tool.py
164 lines (130 loc) · 4.15 KB
/
drawing_tool.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
# Imports
import sys
import pygame
import ctypes
# Increas Dots Per inch so it looks sharper
ctypes.windll.shcore.SetProcessDpiAwareness(True)
# Pygame Configuration
pygame.init()
fps = 300
fpsClock = pygame.time.Clock()
width, height = 640, 480
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
font = pygame.font.SysFont('Arial', 20)
# Variables
# Our Buttons will append themself to this list
objects = []
# Initial color
drawColor = [0, 0, 0]
# Initial brush size
brushSize = 30
brushSizeSteps = 3
# Drawing Area Size
canvasSize = [800, 800]
# Button Class
class Button():
def __init__(self, x, y, width, height, buttonText='Button', onclickFunction=None, onePress=False):
self.x = x
self.y = y
self.width = width
self.height = height
self.onclickFunction = onclickFunction
self.onePress = onePress
self.fillColors = {
'normal': '#ffffff',
'hover': '#666666',
'pressed': '#333333',
}
self.buttonSurface = pygame.Surface((self.width, self.height))
self.buttonRect = pygame.Rect(self.x, self.y, self.width, self.height)
self.buttonSurf = font.render(buttonText, True, (20, 20, 20))
self.alreadyPressed = False
objects.append(self)
def process(self):
mousePos = pygame.mouse.get_pos()
self.buttonSurface.fill(self.fillColors['normal'])
if self.buttonRect.collidepoint(mousePos):
self.buttonSurface.fill(self.fillColors['hover'])
if pygame.mouse.get_pressed(num_buttons=3)[0]:
self.buttonSurface.fill(self.fillColors['pressed'])
if self.onePress:
self.onclickFunction()
elif not self.alreadyPressed:
self.onclickFunction()
self.alreadyPressed = True
else:
self.alreadyPressed = False
self.buttonSurface.blit(self.buttonSurf, [
self.buttonRect.width/2 - self.buttonSurf.get_rect().width/2,
self.buttonRect.height/2 - self.buttonSurf.get_rect().height/2
])
screen.blit(self.buttonSurface, self.buttonRect)
# Handler Functions
# Changing the Color
def changeColor(color):
global drawColor
drawColor = color
# Changing the Brush Size
def changebrushSize(dir):
global brushSize
if dir == 'greater':
brushSize += brushSizeSteps
else:
brushSize -= brushSizeSteps
# Save the surface to the Disk
def save():
pygame.image.save(canvas, "canvas.png")
# Button Variables.
buttonWidth = 120
buttonHeight = 35
# Buttons and their respective functions.
buttons = [
['Black', lambda: changeColor([0, 0, 0])],
['White', lambda: changeColor([255, 255, 255])],
['Blue', lambda: changeColor([0, 0, 255])],
['Green', lambda: changeColor([0, 255, 0])],
['Brush Larger', lambda: changebrushSize('greater')],
['Brush Smaller', lambda: changebrushSize('smaller')],
['Save', save],
]
# Making the buttons
for index, buttonName in enumerate(buttons):
Button(index * (buttonWidth + 10) + 10, 10, buttonWidth,
buttonHeight, buttonName[0], buttonName[1])
# Canvas
canvas = pygame.Surface(canvasSize)
canvas.fill((255, 255, 255))
# Game loop.
while True:
screen.fill((30, 30, 30))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Drawing the Buttons
for object in objects:
object.process()
# Draw the Canvas at the center of the screen
x, y = screen.get_size()
screen.blit(canvas, [x/2 - canvasSize[0]/2, y/2 - canvasSize[1]/2])
# Drawing with the mouse
if pygame.mouse.get_pressed()[0]:
mx, my = pygame.mouse.get_pos()
# Calculate Position on the Canvas
dx = mx - x/2 + canvasSize[0]/2
dy = my - y/2 + canvasSize[1]/2
pygame.draw.circle(
canvas,
drawColor,
[dx, dy],
brushSize,
)
# Reference Dot
pygame.draw.circle(
screen,
drawColor,
[100, 100],
brushSize,
)
pygame.display.flip()
fpsClock.tick(fps)