-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathButton.py
158 lines (130 loc) · 5.5 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
#Assets from: https://www.megupets.com/
import pygame
vec = pygame.math.Vector2
# import pygame module in this program
# activate the pygame library .
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
# define the RGB value
# for white colour
white = (255, 255, 255)
# assigning values to X and Y variable
X = 400
Y = 400
# create the display surface object
# of specific dimension..e(X, Y).
display_surface = pygame.display.set_mode((X, Y ))
# set the pygame window name
pygame.display.set_caption('Image')
# create a surface object, image is drawn on it.
# titleBG = GIFImage("graphicAssets/BgTitle2.gif")
#image = pygame.image.load(r'graphicAssets/BgTitle.gif')
class Button:
def __init__(self, surface, x, y, width, height, function = 0, color = (255, 255, 255),
hover_color = (255, 255, 255), border= True, border_width = 2, border_colour = (0, 0, 0), txt= False,
text ='', font_name = 'arial', text_size = 20, text_color = (0, 0, 0), check_bold= False):
#information of the button
self.x = x
self.y = y
self.pos = vec(x, y)
self.width = width
self.height = height
#draw on the image
self.surface = surface
self.image = pygame.Surface((width, height))
self.fill = self.image.fill(color)
self.rect = self.image.get_rect()
#call this function when button is clicked
self.function = function
#background color of the button
self.color = color
#change color if button is hovered
self.mouse_hover_color = hover_color
#check if border is needed for the button
self.border = border
#information of the border
self.border_width = border_width
self.border_colour = border_colour
#text to put inside the button (give font, text, size, color, and whether bold text is wanted)
self.txt = txt
self.text = text
self.font_name = font_name
self.text_size = text_size
self.text_color = text_color
self.check_bold = check_bold
#initialize mouse hover as false
#call function by Button.mouse_hovering(arguments)
def mouse_hovering(self, pos):
#check if mouse is in the x axis of the button
if pos[0] > self.pos[0] and pos[0]<self.pos[0] + self.width:
#check if mouse is in the y axis of the button
if pos[1] > self.pos[1] and pos[1] < self.pos[1] + self.height:
return True
return False
#call function by Button.update(arguments)
def update (self, pos):
#call function to check if mouse is hovered and update the position
if self.mouse_hovering(pos):
self.hovered = True
else:
self.hovered = False
#call function by Button.draw(arguments)
def draw (self):
pygame.draw.rect(self.image, self.mouse_hover_color, (self.border_width, self.border_width, (self.width-(self.border_width*2), self.height-(self.border_width*2))
#if we want border for button
if self.border:
print("hello")
self.fill(self.border.color)
if self.hovered:
print("hello")
#if it is hovered, draw the rectangle with other mouse hover color otherwise with original color
pygame.draw.rect(self.image, self.mouse_hover_color, (self.border_width, self.border_width, (self.width-(self.border_width*2), self.height-(self.border_width*2))
else:
print("hello")
pygame.draw.rect(self.image, self.color, (self.border_width, self.border_width, self.width-(self.border_width*2), self.height-(self.border_width*2)))
#if we do not want border for button
else:
print("hello")
self.fill(self.color)
#if we want text on button
if self.txt:
self.show_text()
#draw image on top of the surface
#overlap the surface and can present the image from a loaded library at the given position
self.surface.blit(self.image, self.pos)
#call function by Button.show_text(arguments)
def show_text(self):
font = pygame.font.SysFont(self.font_name, self.text_size, bold = self.check_bold)
text = font.render(self.text, False, self.text_color)
size = text.get_size()
pos = vec(self.width//2-(size[0]//2), self.height//2-(size[1]//2))
#draw text on top of the surface
self.surface.blit(text, pos)
#call function by clicking the function (arguments)
def click(self):
if self.function != 0:
self.function()
# infinite loop
while True :
# completely fill the surface object
# with white colour
display_surface.fill(white)
# titleBG.render(display_surface, (0, 0))
pygame.display.update()
# copying the image surface object
# to the display surface object at
# (0, 0) coordinate.
# display_surface.blit(image, (0, 0))
# iterate over the list of Event objects
# that was returned by pygame.event.get() method.
for event in pygame.event.get() :
# if event object type is QUIT
# then quitting the pygame
# and program both.
if event.type == pygame.QUIT :
# deactivates the pygame library
pygame.quit()
# quit the program.
quit()
# Draws the surface object to the screen.