-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.py
67 lines (53 loc) · 2.46 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
import pygame
import config
class Button:
def __init__(self, x, y, width, height, color, text='', font_size=32, font_color=(0, 0, 0), action=lambda: None):
self.rect = pygame.Rect(x, y, width, height)
self.color = color
self.text = text
self.font = pygame.font.Font(None, font_size)
self.font_color = font_color
self.action = action
def draw(self, surface):
pygame.draw.rect(surface, self.color, self.rect)
text_surf = self.font.render(self.text, True, self.font_color)
text_rect = text_surf.get_rect(center=self.rect.center)
surface.blit(text_surf, text_rect)
def is_clicked(self, mouse_pos):
if self.rect.collidepoint(mouse_pos):
return True
return False
def click(self, surface):
self.action()
class Toggle(Button):
def __init__(self, x, y, width, height, color, text='', font_size=32, font_color=(0, 0, 0), start_state=False, action=lambda: None):
super().__init__(x, y, width, height, color, text, font_size, font_color, action)
self.state = start_state
def draw(self, surface):
# Draw border polygon around button
width = self.rect.width + 10
height = self.rect.height + 10
pygame.draw.polygon(surface, (0 if self.state else 255, 255 if self.state else 0, 0), [
(self.rect.center[0] - width / 2, self.rect.center[1] - height / 2),
(self.rect.center[0] + width / 2, self.rect.center[1] - height / 2),
(self.rect.center[0] + width / 2, self.rect.center[1] + height / 2),
(self.rect.center[0] - width / 2, self.rect.center[1] + height / 2)
])
super().draw(surface)
def toggle(self, surface):
self.state = not self.state
self.draw(surface)
def click(self, surface):
self.toggle(surface)
super().click(surface)
class OptionList(Button):
def __init__(self, x, y, width, height, color, text='', font_size=32, font_color=(0, 0, 0), states=["Default"], start_state=0, action=lambda: None):
super().__init__(x, y, width, height, color, text, font_size, font_color, action)
self.states = states
self.current_state = start_state
self.text = self.states[self.current_state]
def click(self, surface):
self.current_state = (self.current_state + 1) % len(self.states)
self.text = self.states[self.current_state]
self.draw(surface)
super().click(surface)