forked from KAYounes/AIProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextBox.py
More file actions
76 lines (60 loc) · 2.8 KB
/
Copy pathTextBox.py
File metadata and controls
76 lines (60 loc) · 2.8 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
67
68
69
70
71
72
73
74
75
76
import pygame
from pygame import Vector2
clock = pygame.time.Clock()
class TextBox:
def __init__(self, x, y, width = 42, hight = 28, color = (255, 255, 255), active = ((162, 237, 50)), allowZero = False, border = True):
self.center = Vector2(x, y)
self.text = '1'
self.width = width
self.hight = hight
self.main_color = color
self.active_color = active
self.color = color
self.allowZero = allowZero
self.border = border
self.rect = pygame.Rect(self.center.x, self.center.y, self.width, self.hight)
self.rect.center = self.center
self.font = pygame.font.Font(None, 30)
def takeInput(self, surface, screen, font):
#> BUG
#solved #$ if textbox is empty (no text) and then deactivated, program crashes since int("") is not valid
self.color = self.active_color
loop = True
while(loop):
for event in pygame.event.get():
if (event.type == pygame.QUIT):
self.color = self.main_color
loop = False
return int(self.text)
if(event.type == pygame.MOUSEBUTTONDOWN and self.text != ""):
if not(self.rect.collidepoint(pygame.mouse.get_pos())):
self.color = self.main_color
# print(int(self.text) + 1, self.text)
loop = False
return int(self.text)
if (event.type == pygame.KEYDOWN):
if(event.key == pygame.K_RETURN and self.text != ""):
self.color = self.main_color
# print(int(self.text) + 1, self.text)
loop = False
return int(self.text)
elif(event.key == pygame.K_BACKSPACE):
self.text = self.text[:-1]
elif(event.key >= pygame.K_0 and event.key <= pygame.K_9 and len(self.text) < 3):
if not(self.allowZero):
if (len(self.text) == 0 and event.key == pygame.K_0):
continue
self.text += event.unicode
self.render(surface, font)
clock.tick(50)
screen.blit(surface, (0,0))
pygame.display.update()
def render(self, surface, font):
font_surface = font.render(self.text, True, (0, 0, 0))
font_rect = font_surface.get_rect()
# font_rect.left = self.rect.left + 5
font_rect.center = self.rect.center
pygame.draw.rect(surface, self.color, self.rect) #206, 219, 221
if(self.border):
pygame.draw.rect(surface, (0,0,0), self.rect, 3)
surface.blit(font_surface, font_rect)