forked from STINKpython/CASTEL-DEFENSE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui_button.py
39 lines (30 loc) · 1.5 KB
/
gui_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
import pygame
from pygame.locals import *
from gui_widget import Widget
from configuraciones import *
class Button(Widget):
def __init__(self,master,x=0,y=0,w=200,h=50,color_background=GREEN,color_border=RED,image_background=None,text="Button",font="Arial",font_size=14,font_color=BLUE,on_click=None,on_click_param=None):
super().__init__(master,x,y,w,h,color_background,color_border,image_background,text,font,font_size,font_color)
self.on_click = on_click
self.on_click_param = on_click_param
self.state = M_STATE_NORMAL
self.render()
def render(self):
super().render()
if self.state == M_STATE_HOVER: # Se aclara la imagen
self.slave_surface.fill(M_BRIGHT_HOVER, special_flags=pygame.BLEND_RGB_ADD)
elif self.state == M_STATE_CLICK: # Se oscurece la imagen
self.slave_surface.fill(M_BRIGHT_CLICK, special_flags=pygame.BLEND_RGB_SUB)
def update(self,lista_eventos):
mousePos = pygame.mouse.get_pos()
self.state = M_STATE_NORMAL
if self.slave_rect_collide.collidepoint(mousePos):
if(pygame.mouse.get_pressed()[0]):
self.state = M_STATE_CLICK
else:
self.state = M_STATE_HOVER
for evento in lista_eventos:
if evento.type == pygame.MOUSEBUTTONDOWN:
if(self.slave_rect_collide.collidepoint(evento.pos)):
self.on_click(self.on_click_param)
self.render()