-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.py
51 lines (42 loc) · 1.62 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
import pygame.font
class Button:
def __init__(self, display=None, top=0, left=0, w=0, h=0,
bgColor=(204, 255, 255), selectedBgColor=(0, 204, 204),
text='', font='arial', fontDimension=36,
textColor=(0, 0, 0), value=''):
self.display = display
self.bgColor = bgColor
self.selectedBgColor = selectedBgColor
self.text = text
self.font = font
self.width = w
self.height = h
self.selected = False
self.fontDimension = fontDimension
self.textColor = textColor
self.top = top
self.left = left
fontObj = pygame.font.SysFont(self.font, self.fontDimension)
self.renderedText = fontObj.render(self.text, True, self.textColor)
self.rectangle = pygame.Rect(left, top, w, h)
self.rectangleText = self.renderedText.get_rect(center=self.rectangle.center)
self.value = value
def select(self, selected):
self.selected = selected
self.draw()
def selectByCoord(self, coord):
if self.rectangle.collidepoint(coord):
self.select(True)
return True
return False
def updateRectangle(self):
self.rectangle.left = self.left
self.rectangle.top = self.top
self.rectangleText = self.renderedText.get_rect(center=self.rectangle.center)
def draw(self):
if self.selected:
bgColor = self.selectedBgColor
else:
bgColor = self.bgColor
pygame.draw.rect(self.display, bgColor, self.rectangle)
self.display.blit(self.renderedText, self.rectangleText)