-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.py
63 lines (52 loc) · 2.25 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
#################################################################################
# #
# button.py #
# #
#################################################################################
# #
# Esse arquivo contem as definicoes da classe Button. #
# #
#################################################################################
import pygame
from colors import *
from pygame.locals import *
class Button:
background_color_hovered = white
background_color = gray
font_color = black
border_color = black
border_thickness = 1
# Inicializa a classe
#
# @in window: janela na qual este botao pertence
# @in rect: rect do objeto
# @in font_rect: rect do texto
# @in text: texto
# @in callback: funcao do clique
def __init__(self, window, rect, font_rect, text, callback):
self.window = window
self.rect = rect
self.font_rect = font_rect
self.text = text
self.callback = callback
self.font = pygame.font.Font(None, 50)
# Define qual e a cor de fundo com base na posicao do mouse.
#
# @in pos: posicao do mouse
# @out: A cor de fundo.
def getBackgroundColor(self, pos):
if self.rect.collidepoint(pos):
return self.background_color_hovered
else:
return self.background_color
# Executa a funcao associada ao botao, caso exista.
def onClick(self):
if self.callback:
self.callback()
# Pinta o botao
def paint(self):
pos = pygame.mouse.get_pos() # Obtem a posicao do mouse.
pos = [pos[0], pos[1]]
pygame.draw.rect(self.window, self.getBackgroundColor(pos), self.rect)
pygame.draw.rect(self.window, self.border_color, self.rect, self.border_thickness)
self.window.blit(self.font.render(self.text, 1, self.font_color), self.font_rect)