-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathButton.py
198 lines (169 loc) · 6.11 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
import pygame
import sys
import random
import time
import math
import constants as c
class Button:
FONT = None
FONT_CACHE = None
FONT_CACHE_DISABLED = None
FONT_CACHE_HOVER = None
def __init__(self,
surf,
pos,
text="",
on_click=None,
hover_surf=None,
click_surf=None,
disabled_surf=None,
enabled=True,
grow_percent=5,
pulse=True,
on_click_args=None):
self.x, self.y = pos
self.width, self.height = surf.get_width(), surf.get_height()
self.text = text
self.surf = surf
self.on_click = on_click
self.hover_surf = hover_surf
self.click_surf = click_surf
self.disabled_surf = disabled_surf
self.enabled = enabled
self.clicked = False
self.grow_percent = grow_percent
self.scale = 1.0
self.target_scale = 1.0
self.pulse = pulse
if on_click_args==None:
on_click_args=()
self.on_click_args = on_click_args
if text and not Button.FONT:
Button.pre_init()
@staticmethod
def pre_init():
Button.FONT = pygame.font.Font("assets/fonts/Rudiment.ttf", 35)
Button.FONT_CACHE = {char: Button.FONT.render(char, 1, c.WHITE) for char in c.CHARS}
Button.FONT_CACHE_DISABLED = {char: Button.FONT.render(char, 1, (50, 50, 50)) for char in c.CHARS}
Button.FONT_CACHE_HOVER = {char: Button.FONT.render(char, 1, c.YELLOW) for char in c.CHARS}
def click(self):
if not self.enabled:
return
self.clicked = False
if type(self.on_click) is tuple or type(self.on_click) is list:
for item in self.on_click:
item(*self.on_click_args)
else:
self.on_click(*self.on_click_args)
def enable(self):
self.enabled = True
def disable(self):
self.enabled = False
def toggle(self):
self.enabled = not self.enabled
def is_hovered(self):
mpos = pygame.mouse.get_pos()
min_x = self.x - self.width/2
max_x = self.x + self.width/2
min_y = self.y - self.height/2
max_y = self.y + self.height/2
if min_x < mpos[0] < max_x and min_y < mpos[1] < max_y:
return True
return False
def get_surf(self):
surf = None
if not self.enabled and self.disabled_surf is not None:
surf = self.disabled_surf
elif self.clicked and self.enabled and self.click_surf is not None:
surf = self.click_surf
elif self.is_hovered() and self.enabled and self.hover_surf is not None:
surf = self.hover_surf
else:
surf = self.surf
if self.scale != 1.0:
width = int(surf.get_width() * self.scale)
height = int(surf.get_height() * self.scale)
surf = pygame.transform.scale(surf, (width, height))
return surf
def draw(self, surface, xoff=0, yoff=0):
surf = self.get_surf()
x = self.x - surf.get_width() * self.scale/2 + xoff
y = self.y - surf.get_height() * self.scale/2 + yoff
surface.blit(self.get_surf(), (x, y))
if not self.text:
return
if not self.enabled:
font_cache = self.FONT_CACHE_DISABLED
elif self.is_hovered():
font_cache = self.FONT_CACHE_HOVER
else:
font_cache = self.FONT_CACHE
chars = [font_cache[letter] for letter in self.text]
width = sum([char.get_width() for char in chars])
x = self.x - width//2 + xoff
y = self.y - chars[0].get_height()//2 + yoff
for char in chars:
surface.blit(char, (x, y))
x += char.get_width()
def update(self, dt, events):
for event in events:
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
if self.clicked and self.is_hovered():
self.click()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if self.is_hovered():
self.clicked = True
if self.clicked:
if not self.is_hovered():
self.clicked = False
if self.is_hovered() and self.enabled:
self.target_scale = 1.0 + self.grow_percent/100
elif self.pulse and self.enabled:
self.target_scale = 1.0 + 0.02*math.sin(time.time()*4)
else:
self.target_scale = 1.0
ds = self.target_scale - self.scale
speed = 25
if abs(ds) < 0.01:
self.scale = self.target_scale
ds = 0
if ds < 0:
self.scale = max(self.scale + ds * dt * 20, self.target_scale)
elif ds > 0:
self.scale = min(self.scale + ds * dt * 20, self.target_scale)
if __name__ == '__main__':
class Color:
def __init__(self):
self.val = (0, 0, 0)
def randomize(self):
self.val = (random.random()*255,
random.random()*255,
random.random()*255)
screen = pygame.display.set_mode((800, 600))
then = time.time()
button_surf = pygame.image.load("button.png")
button_hover = pygame.image.load("button_hover.png")
button_disabled = pygame.image.load("button_disabled.png")
button_clicked = pygame.image.load("button_clicked.png")
color = Color()
button = Button(button_surf,
(200, 200),
on_click=color.randomize,
hover_surf=button_hover,
click_surf=button_clicked,
disabled_surf=button_disabled)
while True:
now = time.time()
dt = now - then
then = now
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
button.update(dt, events)
screen.fill(color.val)
button.draw(screen)
pygame.display.flip()