-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextBox.py
241 lines (205 loc) · 6.81 KB
/
TextBox.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
234
235
236
237
238
239
240
241
import pygame
from pygame.locals import *
class TextBox (pygame.sprite.DirtySprite):
__fonts = {}
splitChars = (' ', '-')
_changed = False
_text = ""
_rect = None
_fontName = ""
_fontSize = 0
_italic = False
_bold = False
_underline = False
_antialias = True
_color = None
_myFont = None
_image = None
def __init__(self, text = "", x = 0, y = 0, width = 50, height = 12, fontName = " ", fontSize = 10, italic = False, bold = False, underline = False, antialias = True, color = (255, 255, 255, 255), *args, **kwargs):
super(TextBox, self). __init__(*args, **kwargs)
self._rect = pygame.Rect(x, y, width, height)
self._fontName = fontName
self._fontSize = fontSize
self._myFont = self._getFont(fontName, fontSize)
self._text = text
self._italic = italic
self._bold = bold
self._underline = underline
self._antialias = antialias
self._color = color
self._update()
def _getFont(self, fontName, size):
choices = filter(lambda (x):x.find(fontName) != -1 , pygame.font.get_fonts())
if len(choices) == 0:
fontFile = pygame.font.get_default_font()
else:
fontFile = pygame.font.match_font(choices[0])
key =(fontFile, size)
if key not in TextBox.__fonts:
TextBox.__fonts[key] = pygame.font.Font(fontFile, size)
return TextBox.__fonts[key]
def _update(self):
"""Update the surface taht contains our text."""
if self._image == None or self._image.get_size() != self._rect.size:
self._image = pygame.Surface(self._rect.size, SRCALPHA)
self._image.fill((0, 0, 0, 0))
self._renderText()
self.dirty = 1
self._changed = False
def _renderText(self):
self._myFont.set_italic(self._italic)
self._myFont.set_bold(self._bold)
self._myFont.set_underline(self._underline)
image = self._myFont.render(self._text, self._antialias, self._color)
self._image.blit(image, (0, self._image.get_height() - image.get_height()))
@property
def rect(self):
"""Return the bounding box for this object. Recalculate the object if
it has changed."""
if self._changed:
self._update()
return self._rect
#No setter property for rect.
@property
def image(self):
"""Return the rendered image for this object. Recalculate the object
if it has changed."""
if self._changed:
self._update()
return self._image
#No setter property fot image.
@property
def x(self):
"""Return the left bound for this object."""
return self.rect.left
@x.setter
def x(self, value):
"""Set the left bound for this object. Mark this object as dirty."""
self._rect.left = value
if self.dirty == 0: self.dirty = 1
@property
def y(self):
"""Return the top bound for this object."""
return self.rect.top
@y.setter
def y(self, value):
"""Set the left bound for this object. Mark the object as dirty."""
self._rect.top = value
if self.dirty == 0: self.dirty = 1
@property
def width(self):
"""Return the left bound for this object."""
return self.rect.width
@width.setter
def width(self, value):
"""Set the left bound for this object. Mark this object as dirty."""
self._rect.width = value
self._changed = True
@property
def height(self):
"""Return the top bound for this object."""
return self.rect.height
@y.setter
def height(self, value):
"""Set the left bound for this object. Mark the object as dirty."""
self._rect.height = value
self._changed = True
@property
def bold(self):
"""Return the bold for this object."""
return self._bold
@bold.setter
def bold(self, value):
"""Set the bold for this object."""
self._bold = value
self._changed = True
@property
def italic(self):
"""Return the italic for this object."""
return self._italic
@italic.setter
def italic(self, value):
"""Set the italic for this object."""
self._italic = value
self._changed = True
@property
def underline(self):
"""Return the underline for this object."""
return self._underline
@underline.setter
def underline(self, value):
"""Set the underline for this object."""
self._underline = value
self._changed = True
@property
def color(self):
"""Return the color for this object."""
return self._color
@color.setter
def color(self, value):
"""Set the color for this object."""
self._color = value
self._changed = True
@property
def antialias(self):
"""Return the antialias for this object."""
return self._antialias
@antialias.setter
def antialias(self, value):
"""Set the antialias for this object."""
self._antialias = value
self._changed = True
@property
def fontName(self):
"""Return the fontName for this object."""
return self._fontName
@fontName.setter
def fontName(self, value):
"""Set the fontName for this object."""
self._fontName = value
self._changed = True
@property
def fontSize(self):
"""Return the fontSize for this object."""
return self._fontSize
@fontSize.setter
def fontSize(self, value):
"""Set the fontSize for this object."""
self._fontSize = value
self._changed = True
@property
def text(self):
"""Return the text this object is displaying."""
return self._text
@text.setter
def text(self, value):
"""Set the text this object displays."""
self._text = value
self._changed = True
def test():
import test
pygame.init()
tb = TextBox(text = "Fishy")
clock = pygame.time.Clock()
group = pygame.sprite.LayeredDirty(tb, layer = 0, _use_update = True)
def testlogic(event):
if event == None:
return
if event.type == pygame.KEYDOWN:
if event.key == 27:
return True
if event.key == K_BACKSPACE:
if len(tb.text) > 0:
tb.text = tb.text[:-1]
return
if event.key >= 256:
return
tb.text += chr(event.key)
def testrender(screen):
clock.tick()
time = clock.get_time()
group.update(time)
bgd = pygame.Surface((screen.get_width(), screen.get_height()))
group.draw(screen, bgd = bgd)
test.test(testlogic, testrender)
if __name__ == '__main__': test()