-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextBox.py
More file actions
56 lines (46 loc) · 1.83 KB
/
TextBox.py
File metadata and controls
56 lines (46 loc) · 1.83 KB
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
import pygame
import time
import Constants as C
class TextBox(object):
def __init__(self, color, x, y, width, height, max):
self.color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.active = False
self.user_text = ""
self.max = 15
def draw(self, surface):
box_rect = (self.x, self.y, self.width, self.height)
text_font = C.FONT
box_text = text_font.render(self.user_text, True, C.BLACK)
text_pos = (self.x + 5, (self.y + self.height / 2) - box_text.get_height() / 2)
pygame.draw.rect(surface, self.color, box_rect)
surface.blit(box_text, text_pos)
if self.active:
if time.time() % 1 > 0.5:
text_rect = box_text.get_rect(topleft=text_pos)
cursor = pygame.Rect(text_rect.topright, (2, box_text.get_height()))
pygame.draw.rect(surface, C.BLACK, cursor)
def update_text(self, event):
if self.active:
if event.type == pygame.KEYDOWN:
#Backspace
if event.key == pygame.K_BACKSPACE:
self.user_text = self.user_text[:-1]
else:
if len(self.user_text) < self.max:
self.user_text += event.unicode
def check_pressed(self, mouse_pos, event):
if event.type == pygame.MOUSEBUTTONUP:
if mouse_pos[0] >= self.x and mouse_pos[0] <= (self.x + self.width):
if mouse_pos[1] > self.y and mouse_pos[1] <= (self.y + self.height):
self.active = True
else:
self.active = False
else:
self.active = False
return self.active
def get_text(self):
return self.user_text