-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_box.py
98 lines (85 loc) · 3.92 KB
/
input_box.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""WireWorldCl wireworld simulator
Copyright (C) 2024 Elefant-Freeciv
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA."""
import pygame
pygame.init()
class Input_Box:
def __init__(self, data, pos, sfoi="float", draw=(False, 0, 0)):
self.draw = (draw[1], draw[2])
if not draw[0]:
self.draw = (15, 150)
self.data = [*data]
self.pos = pos
self.data_type = sfoi
self.selected_charactor = 0
self.selected = False
self.font = pygame.font.SysFont("Arial", self.draw[0])
self.rect = pygame.Rect(self.pos[0], self.pos[1], self.draw[1], self.draw[0])
def set_data(self, string):
self.data = [*string]
def get_typed_data(self):
if self.data_type == "str":
return str(''.join(self.data))
elif self.data_type == "float":
if ''.join(self.data).strip() == '':
return 0.0
else:
return float(''.join(self.data).strip())
elif self.data_type == "int":
if ''.join(self.data).strip() == '':
return 0
else:
return int(''.join(self.data).strip())
def click_handler(self):
mouse_pos = pygame.mouse.get_pos()
if self.rect.collidepoint(mouse_pos):
self.selected_charactor = int((mouse_pos[0]-self.pos[0])/9)
if self.selected_charactor > len(self.data)-1:
self.selected_charactor = len(self.data)-1
self.selected = True
else:
self.selected = False
def input_handler(self, key, unicode):
#print(key)
if self.selected:
if key == pygame.K_LEFT:
self.selected_charactor -= 1
if self.selected_charactor < 0:
self.selected_charactor = 0
elif key == pygame.K_RIGHT:
self.selected_charactor += 1
if self.selected_charactor > len(self.data)-1:
self.selected_charactor = len(self.data)-1
elif key == pygame.K_BACKSPACE:
self.data.pop(self.selected_charactor)
self.selected_charactor -= 1
if self.selected_charactor < 0:
self.selected_charactor = 0
elif key == pygame.K_RETURN or key == pygame.K_KP_ENTER:
self.selected = False
else:
self.data.insert(self.selected_charactor, unicode)
self.selected_charactor += 1
if self.selected_charactor > len(self.data)-1:
self.data.append(" ")
def blitme(self, surface):
pygame.draw.rect(surface, (255,255,255), self.rect)
if self.selected:
pygame.draw.rect(surface, (100,100,100), (self.pos[0]+9*self.selected_charactor, self.pos[1], int(self.draw[0]*0.6), self.draw[0]))
else:
pygame.draw.rect(surface, (150,150,150), (self.pos[0]+9*self.selected_charactor, self.pos[1], int(self.draw[0]*0.6), self.draw[0]))
i=0
for char in self.data:
text = self.font.render(str(char), 1, (0, 0, 0))
surface.blit(text, (self.pos[0]+i*int(self.draw[0]*0.6), self.pos[1]))
i += 1