-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_input.py
More file actions
66 lines (50 loc) · 2.06 KB
/
Copy pathtext_input.py
File metadata and controls
66 lines (50 loc) · 2.06 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
57
58
59
60
61
62
63
64
65
66
import pygame
from pygame import surface
class Text:
def __init__(self, x, y, font, color, mode="text", width=200, height=40):
self.x = x
self.y = y
self.font = font
self.color = color
self.text = ""
self.mode = mode # "text" or "number"
self.active = False
self.rect = pygame.Rect(x, y, width, height)
def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
# check if mouse clicked inside textbox
mouse_pos = event.pos
# simple textbox area
box_rect = pygame.Rect(self.x, self.y, 200, 40)
if box_rect.collidepoint(mouse_pos):
self.active = True
else:
self.active = False
# ONLY type if active (textbox clicked)
if self.active and event.type == pygame.KEYDOWN:
if event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
char = event.unicode
if self.mode == "text":
if char.isalnum() or char == " ":
self.text += char
elif self.mode == "number":
if char.isdigit() or char == ".":
self.text += char
def draw(self, surface):
# active color
if self.active:
box_color = (200, 200, 200)
else:
box_color = (100, 100, 100)
pygame.draw.rect(surface, box_color, self.rect, 2)
text_surface = self.font.render(self.text, True, self.color)
surface.blit(text_surface, (self.rect.x + 5, self.rect.y + 5))
def drawPrompt(self, surface, prompt_text):
prompt_surface = self.font.render(prompt_text, True, self.color)
surface.blit(prompt_surface, (self.x, self.y))
@staticmethod
def create_text_input(x, y, font, color=(255, 255, 255), mode="text", width=200, height=40):
# Helper Functions make everythign look nicer so im just gonna roll with it
return Text(x, y, font, color, mode, width, height)