-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathform.py
executable file
·233 lines (191 loc) · 7.94 KB
/
form.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# by Timothy Downs, inputbox written for my map editor
# This program needs a little cleaning up
# It ignores the shift key
# And, for reasons of my own, this program converts "-" to "_"
# A program to get user input, allowing backspace etc
# shown in a box in the middle of the screen
# Called by:
# import inputbox
# answer = inputbox.ask(screen, "Your name")
#
# Only near the center of the screen is blitted to
import pygame, pygame.font, pygame.event, pygame.draw, string
from pygame.locals import *
from globals import *
from MiscTools import *
import time
from text import *
#--------------------------------------------------------------------
class Form(pygame.sprite.Sprite):
"""A simple form sprite"""
#--------------------------------------------------------------------
def __init__(self, group):
pygame.sprite.Sprite.__init__(self, group)
self.mustRender = True
self.group = group
# surface size
self.image = pygame.Surface((500,180)).convert()
self.image = self.image.convert()
self.image.fill((0,60,0))
self.rect = (300,900)
self.visible = False
self.Start()
self.SetError("--------------")
#--------------------------------------------------------------------
def Start(self):
self.finished = False
self.current_field = 0
self.key = None
self.results = {}
#--------------------------------------------------------------------
def Restart(self):
self.Start()
for i in range(0, len(self.fields)):
self.fields[i].get_input = True
self.fields[i].current_string = []
#--------------------------------------------------------------------
def SetTitle(self, title):
"""Set title string"""
self.title = Text(title, 34, 150, 35, RED)
#--------------------------------------------------------------------
def SetError(self, error):
"""Set error string"""
self.error = Text(error, 24, 250, 35, RED)
#--------------------------------------------------------------------
def SetFields(self, fields=None):
x = 10
y = 50
self.fields = []
for field in fields:
y += 30
self.fields.append( InputBox(self.group, field, self.image, (x, y)) )
self.current_field = 0
#--------------------------------------------------------------------
def get_user_input(self, key):
"""Read user input"""
self.mustRender = True
if self.fields[self.current_field].get_input:
self.fields[self.current_field].get_key(key)
else:
if self.current_field < len(self.fields)-1:
self.current_field += 1
self.fields[self.current_field].get_key(key)
else:
for i in range(0, len(self.fields)):
logger.info( str(self.fields[i].result) )
self.finished = True
self.mustRender = False
self.key = None
#--------------------------------------------------------------------
def FormError(self, s):
logger.error("Form result: " + s)
self.error.UpdateText(s)
self.render()
#--------------------------------------------------------------------
def update(self):
if self.finished: return
self.get_user_input(self.key)
if self.mustRender:
self.render()
self.mustRender = False
#--------------------------------------------------------------------
def render(self):
self.fields[0].surface.fill((0,30,30))
self.image.blit(self.title.Show(), (0, 0))
self.image.blit(self.error.Show(), (0, 30))
for i in range(0, len(self.fields)):
self.fields[i].mustRender = True
self.fields[i].update()
#--------------------------------------------------------------------
def SetFocus(self, state):
self.isFocused = state
#--------------------------------------------------------------------
def ToggleVisible(self):
if self.visible: return self.SetUnVisible()
else: return self.SetVisible()
#--------------------------------------------------------------------
def SetVisible(self):
logger.debug5("SetVisible form sprite")
self.rect = (400,350)
self.visible = True
self.mustRender = True
#--------------------------------------------------------------------
def SetUnVisible(self):
logger.debug5("SetUnVisible form sprite")
self.rect = (300,900)
self.mustRender = False
self.visible = False
#--------------------------------------------------------------------
class InputBox(pygame.sprite.Sprite):
"""..."""
#--------------------------------------------------------------------
def __init__(self, group, label, surface, pos):
pygame.sprite.Sprite.__init__(self)
self.surface = surface
self.rect = self.pos = (pos[0],pos[1])
self.pos2 = (pos[0]+60,pos[1]-5)
self.get_input = True
self.inkey = None
self.current_string = []
self.label = label
self.get_key(None)
self.results = {}
self.result = ""
#--------------------------------------------------------------------
def update(self):
if self.mustRender:
self.render()
self.mustRender = False
#--------------------------------------------------------------------
def render(self):
self.image = self.display_box(self.label + " " + string.join(self.current_string,""))
self.surface.blit(self.image, (self.pos))
#--------------------------------------------------------------------
def get_key(self, key):
"""Ask and get input keys"""
self.inkey = key
input_data = self.ask(self.label, key)
if not self.get_input:
if input_data is not "":
if type(input_data) is str:
#logger.info( self.label + " : " + str(input_data) )
self.result = input_data
pass
#self.current_string = ""
#--------------------------------------------------------------------
def display_box(self, message):
"Print a message in a box in the middle of the screen"
fontobject = pygame.font.Font(None,18)
pygame.draw.rect(self.surface, (0,0,10),
(self.pos2[0],
self.pos2[1],
200,20), 0)
pygame.draw.rect(self.surface, (255,255,25),
(self.pos2[0],
self.pos2[1],
204,24), 1)
if len(message) != 0:
self.mustRender = True
return fontobject.render(message, 1, (255,255,255))
#--------------------------------------------------------------------
def ask(self, label, inkey):
"ask(screen, label) -> answer"
if not self.get_input: return self.current_string
if self.current_string is not "": self.display_box(label + ": " + string.join(self.current_string,""))
if inkey is not None:
if inkey == K_ESCAPE:
pass #self.current_string.append("AA")
elif inkey == K_BACKSPACE:
self.current_string = self.current_string[0:-1]
elif inkey == K_RETURN:
self.get_input = False
return string.join(self.current_string,"")
elif inkey == K_MINUS:
self.current_string.append("_")
elif inkey <= 127:
if inkey is not None:
self.mustRender = True
self.current_string.append(chr(inkey))
else: pass
return string.join(self.current_string,"")
#--------------------------------------------------------------------