Repository navigation
Expand file tree
/
Copy pathinputBox.py
More file actions
53 lines (47 loc) · 1.79 KB
/
Copy pathinputBox.py
File metadata and controls
53 lines (47 loc) · 1.79 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
import pygame
class InputBox:
def __init__(self, pos, size, screenPtr, maxChar = 4, font_size=48, color=(255, 255, 255), bg_color=(80, 80, 80), visible=True):
self.pos = pos
self.size = size
self.screenPtr = screenPtr
self.maxChar = maxChar
self.font_size = font_size
self.color = color
self.visible = visible
self.bg_color = bg_color
self.hover_color = (120, 120, 120)
self.curr_color = bg_color
self.font = pygame.font.SysFont(None, self.font_size)
self.rect = pygame.Rect(pos, size)
self.text = ""
if not hasattr(screenPtr, 'allInputs'):
screenPtr.allInputs = []
screenPtr.allInputs.append(self)
def onChar(self,char):
if char == "<-": # Handle backspace
if self.text:
self.text = self.text[:-1]
return
if len(self.text) < self.maxChar:
self.text += char
def setVisible(self,visible):
self.visible = visible
def draw(self, screen):
if not self.visible:
return
# Draw shadow for the background rectangle
margin = 16
shadow_offset = (6, 6)
shadow_color = (30, 30, 30)
bg_rect = pygame.Rect(
self.pos[0] - margin,
self.pos[1] - margin,
self.size[0] + 2 * margin,
self.size[1] + 2 * margin
)
pygame.draw.rect(screen, shadow_color, bg_rect, border_radius=10)
# Draw the input box background and text
pygame.draw.rect(screen, self.curr_color, self.rect, border_radius=10)
text_surface = self.font.render(self.text, True, self.color)
text_rect = text_surface.get_rect(center=self.rect.center)
screen.blit(text_surface, text_rect)