-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.py
242 lines (232 loc) · 7.68 KB
/
button.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
import pygame
import support
import data
from consts import *
class Button:
def __init__(
self,
text_img,
center,
color,
hovercolor,
can_select=False,
selected=False,
fixed_size=None,
data=None,
draw_outline=True,
animate=True,
):
self.text_img = text_img
self.color = color
self.can_select = can_select
self.hover_color = hovercolor
self.data = data
self.draw_outline = draw_outline
self.animate = animate
self.selected = selected
self.hovered = False
self.clicked = False
self.offset = 0
inf = SCALE_RES(30)
if fixed_size is not None:
self.rect = (
pygame.Rect((0, 0), fixed_size).move_to(center=center).inflate(inf, inf)
)
else:
self.rect = self.text_img.get_rect(center=center).inflate(inf, inf)
def update(self) -> bool:
mpos = pygame.mouse.get_pos()
mouse = pygame.mouse.get_pressed()
self.offset += data.dt * BUTTON_SPEED
if self.offset >= BUTTON_SEGMENT * 2:
self.offset = 0
if self.rect.collidepoint(mpos):
if not self.hovered:
data.assets.play("button_hover")
self.hovered = True
if mouse[pygame.BUTTON_LEFT - 1]:
if not self.clicked:
self.clicked = True
else:
if self.clicked:
self.clicked = False
if self.rect.collidepoint(data.app.start_click):
if self.can_select:
self.selected = not self.selected
data.assets.play("button_click")
return True
else:
self.hovered = False
return False
def draw(self):
data.screen.blit(self.text_img, self.text_img.get_rect(center=self.rect.center))
if not self.draw_outline:
return
hover = self.hovered or self.selected
color = self.hover_color if hover else self.color
pygame.draw.circle(
data.screen,
color,
(self.rect.left + BUTTON_R, self.rect.top + BUTTON_R),
BUTTON_R,
BUTTON_W if hover else BUTTON_WS,
False,
True,
False,
False,
)
pygame.draw.circle(
data.screen,
color,
(self.rect.left + BUTTON_R, self.rect.bottom - BUTTON_R),
BUTTON_R,
BUTTON_W if hover else BUTTON_WS,
False,
False,
True,
False,
)
pygame.draw.circle(
data.screen,
color,
(self.rect.right - BUTTON_R, self.rect.top + BUTTON_R),
BUTTON_R,
BUTTON_W if hover else BUTTON_WS,
True,
False,
False,
False,
)
pygame.draw.circle(
data.screen,
color,
(self.rect.right - BUTTON_R, self.rect.bottom - BUTTON_R),
BUTTON_R,
BUTTON_W if hover else BUTTON_WS,
False,
False,
False,
True,
)
if not self.hovered and not self.selected:
pygame.draw.line(
data.screen,
self.color,
(self.rect.left + BUTTON_R, self.rect.top + BUTTON_WS / 2),
(self.rect.right - BUTTON_R, self.rect.top + BUTTON_WS / 2),
BUTTON_WS,
)
pygame.draw.line(
data.screen,
self.color,
(self.rect.left + BUTTON_R, self.rect.bottom - BUTTON_WS / 2),
(self.rect.right - BUTTON_R, self.rect.bottom - BUTTON_WS / 2),
BUTTON_WS,
)
pygame.draw.line(
data.screen,
self.color,
(self.rect.left + BUTTON_WS / 2, self.rect.top + BUTTON_R),
(self.rect.left + BUTTON_WS / 2, self.rect.bottom - BUTTON_R),
BUTTON_WS,
)
pygame.draw.line(
data.screen,
self.color,
(self.rect.right - BUTTON_WS / 2, self.rect.top + BUTTON_R),
(self.rect.right - BUTTON_WS / 2, self.rect.bottom - BUTTON_R),
BUTTON_WS,
)
return
if not self.animate:
return
x = self.rect.left + BUTTON_R + self.offset
ox = self.rect.right - BUTTON_R - self.offset
y_pos = [self.rect.top + BUTTON_W / 2, self.rect.bottom - BUTTON_W / 2]
on = True
if self.offset > BUTTON_SEGMENT:
pygame.draw.line(
data.screen,
self.hover_color,
(x - self.offset, y_pos[0]),
(x - self.offset + (self.offset - BUTTON_SEGMENT), y_pos[0]),
BUTTON_W,
)
pygame.draw.line(
data.screen,
self.hover_color,
(ox + self.offset - (self.offset - BUTTON_SEGMENT), y_pos[1]),
(ox + self.offset, y_pos[1]),
BUTTON_W,
)
while True:
if on:
pygame.draw.line(
data.screen,
self.hover_color,
(x, y_pos[0]),
(min(self.rect.right - BUTTON_R, x + BUTTON_SEGMENT), y_pos[0]),
BUTTON_W,
)
pygame.draw.line(
data.screen,
self.hover_color,
(
max(
self.rect.left + BUTTON_R,
ox - BUTTON_SEGMENT,
),
y_pos[1],
),
(ox, y_pos[1]),
BUTTON_W,
)
on = False
else:
on = True
x += BUTTON_SEGMENT
ox -= BUTTON_SEGMENT
if x >= self.rect.right - BUTTON_R:
break
y = self.rect.top + BUTTON_R + self.offset
oy = self.rect.bottom - BUTTON_R - self.offset
x_pos = [self.rect.left + BUTTON_W / 2, self.rect.right - BUTTON_W / 2]
on = True
if self.offset > BUTTON_SEGMENT:
pygame.draw.line(
data.screen,
self.hover_color,
(x_pos[1], y - self.offset),
(x_pos[1], y - self.offset + (self.offset - BUTTON_SEGMENT)),
BUTTON_W,
)
pygame.draw.line(
data.screen,
self.hover_color,
(x_pos[0], oy + self.offset - (self.offset - BUTTON_SEGMENT)),
(x_pos[0], oy + self.offset),
BUTTON_W,
)
while True:
if on:
pygame.draw.line(
data.screen,
self.hover_color,
(x_pos[1], y),
(x_pos[1], min(self.rect.bottom - BUTTON_R, y + BUTTON_SEGMENT)),
BUTTON_W,
)
pygame.draw.line(
data.screen,
self.hover_color,
(x_pos[0], max(self.rect.top + BUTTON_R, oy - BUTTON_SEGMENT)),
(x_pos[0], oy),
BUTTON_W,
)
on = False
else:
on = True
y += BUTTON_SEGMENT
oy -= BUTTON_SEGMENT
if y >= self.rect.bottom - BUTTON_R:
break