-
Notifications
You must be signed in to change notification settings - Fork 2
/
textbox.py
50 lines (44 loc) · 1.79 KB
/
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
46
47
48
49
50
import pygame as pg
import colors as c
class TextBox:
def __init__(self, x: int, y: int, w: int, h: int=32, text: str=''):
self.FONT = pg.font.SysFont('consolas', 24)
self.rect = pg.Rect(x, y, w, h)
self.color = c.WHITE
self.text = text
self.txt_surface = self.FONT.render(text, True, self.color)
self.active = True
self.returned = ''
def handle_event(self, event: pg.event.EventType) -> None:
if event.type == pg.MOUSEBUTTONDOWN:
# If the user clicked on the input_box rect.
if self.rect.collidepoint(event.pos):
self.active = True
else:
pass
#self.active = False
if event.type == pg.KEYDOWN:
if self.active:
#self.returned = self.text
if event.key == pg.K_RETURN:
self.returned = self.text
self.text = ''
elif event.key == pg.K_BACKSPACE:
self.text = self.text[:-1]
elif event.key == pg.K_ESCAPE:
self.text = ''
else:
self.text += event.unicode
# Re-render the text.
self.txt_surface = self.FONT.render(self.text, True, self.color)
def update(self) -> None:
# Resize the box if the text is too long.
width = max(200, self.txt_surface.get_width()+10)
self.rect.w = width
def draw(self, screen: pg.display) -> None:
# Blit the text.
screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5))
# Blit the rect.
pg.draw.rect(screen, self.color, self.rect, 2)
def get_returned(self) -> str:
return self.returned