forked from STINKpython/CASTEL-DEFENSE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui_textbox.py
46 lines (37 loc) · 1.8 KB
/
gui_textbox.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
import pygame
from pygame.locals import *
from gui_widget import Widget
from configuraciones import *
class TextBox(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.writing_flag = False
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(self.writing_flag):
self.state = M_STATE_CLICK
else:
self.state = M_STATE_HOVER
for evento in lista_eventos:
if evento.type == pygame.MOUSEBUTTONDOWN :
self.writing_flag = self.slave_rect_collide.collidepoint(evento.pos)
if evento.type == pygame.KEYDOWN and self.writing_flag:
if evento.key == pygame.K_RETURN:
self.writing_flag = False
elif evento.key == pygame.K_BACKSPACE:
self._text = self._text[:-1]
else:
self._text += evento.unicode
self.render()