-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbutton.py
More file actions
105 lines (96 loc) · 3.4 KB
/
button.py
File metadata and controls
105 lines (96 loc) · 3.4 KB
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
import pygame
from const import *
# https://github.com/Unlim8ted-Studio-Productions/Easy_pygame_UI-Maker/blob/main/button.py
class Button:
"""Button class for creating interactive buttons in the game."""
def __init__(
self,
text,
x,
y,
width,
height,
command,
additional_data: list = None,
color=WHITE,
):
"""
Initialize a button.
Args:
text (str): The text displayed on the button.
x (int): The x-coordinate of the button's top-left corner.
y (int): The y-coordinate of the button's top-left corner.
width (int): The width of the button.
height (int): The height of the button.
command (function): The function to be executed when the button is clicked.
aditional data (list): arguments the buttons command needs to run
"""
self.text = text
self.x = x
self.y = y
self.width = width
self.height = height
self.command = command
self.additional_data = additional_data
self.hovered = False
self.size = 36
self.active = False
self.bold = False
self.italics = False
self.underlined = False
self.font_name = "fonts/PixelifySans-Regular.ttf"
self.color = color
def draw(self, screen):
"""Draw the button on the screen."""
font = pygame.font.Font(self.font_name, self.size)
font.set_bold(self.bold)
font.set_italic(self.italics)
font.set_underline(self.underlined)
color = BUTTON_HOVER_COLOR if self.hovered else BUTTON_COLOR
pygame.draw.rect(screen, color, (self.x, self.y, self.width, self.height))
text = font.render(self.text, True, self.color)
screen.blit(
text,
(
self.x + self.width // 2 - text.get_width() // 2,
self.y + self.height // 2 - text.get_height() // 2,
),
)
def handle_event(self, event):
"""
Handle events related to the button.
Args:
event: The Pygame event to be processed.
"""
if event.type == pygame.MOUSEMOTION:
self.hovered = (
self.x < event.pos[0] < self.x + self.width
and self.y < event.pos[1] < self.y + self.height
)
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if self.hovered:
self.active = True
if self.additional_data != None:
self.command(*self.additional_data)
else:
self.command()
else:
self.active = False
def selected(self):
self.hovered = True
def change_text(self, event):
if event.type == pygame.MOUSEMOTION:
self.hovered = (
self.x < event.pos[0] < self.x + self.width
and self.y < event.pos[1] < self.y + self.height
)
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if self.hovered:
self.active = True
else:
self.active = False
if event.type == pygame.KEYDOWN and self.active:
if event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode