-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_input_func.py
64 lines (55 loc) · 2.11 KB
/
get_input_func.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
64
import pygame as pg
from params import COLOR_ACTIVE,COLOR_INACTIVE,even_smaller_font as font
class InputBox:
def __init__(self, x, y, w, h, text=''):
self.rect = pg.Rect(x, y, w, h)
self.color = COLOR_INACTIVE
self.text = text
self.txt_surface = font.render(text, True, self.color)
self.active = False
self.cursor = ''
self.blink = 0
def handle_event(self, event):
if event.type == pg.MOUSEBUTTONDOWN:
# If the user clicked on the input_box rect.
if self.rect.collidepoint(event.pos):
# Toggle the active variable.
self.active = not self.active
else:
self.active = False
# Change the current color of the input box.
self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
# Check for shift key, backspace key and enter key
if event.type == pg.KEYDOWN:
if self.active:
if event.key == pg.K_RETURN:
self.active = False
elif event.key == pg.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
# Return the current text
return self.text
def update(self):
# Resize the box if the text is too long.
width = max(self.rect.w, self.txt_surface.get_width()+10)
self.rect.w = width
def draw(self, screen):
# Blit the text.
if self.active:
if self.blink >= 160 and self.blink <= 480:
self.cursor = '|'
self.blink +=1
elif self.blink == 640:
self.cursor = ''
self.blink = 0
else:
self.cursor = ''
self.blink +=1
else:
self.cursor = ''
self.blink = 0
self.txt_surface = font.render(self.text+self.cursor, True, self.color)
screen.blit(self.txt_surface, (self.rect.x+4, self.rect.y+3))
# Blit the rect.
pg.draw.rect(screen, self.color, self.rect, 1)