forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectEntry.py
More file actions
378 lines (325 loc) · 15 KB
/
DirectEntry.py
File metadata and controls
378 lines (325 loc) · 15 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
"""Contains the DirectEntry class, a type of DirectGUI widget that accepts
text entered using the keyboard."""
__all__ = ['DirectEntry']
from panda3d.core import *
from . import DirectGuiGlobals as DGG
from .DirectFrame import *
from .OnscreenText import OnscreenText
import sys
# import this to make sure it gets pulled into the publish
import encodings.utf_8
from direct.showbase.DirectObject import DirectObject
# DirectEntry States:
ENTRY_FOCUS_STATE = PGEntry.SFocus # 0
ENTRY_NO_FOCUS_STATE = PGEntry.SNoFocus # 1
ENTRY_INACTIVE_STATE = PGEntry.SInactive # 2
class DirectEntry(DirectFrame):
"""
DirectEntry(parent) - Create a DirectGuiWidget which responds
to keyboard buttons
"""
directWtext = ConfigVariableBool('direct-wtext', 1)
AllowCapNamePrefixes = ("Al", "Ap", "Ben", "De", "Del", "Della", "Delle", "Der", "Di", "Du",
"El", "Fitz", "La", "Las", "Le", "Les", "Lo", "Los",
"Mac", "St", "Te", "Ten", "Van", "Von", )
ForceCapNamePrefixes = ("D'", "DeLa", "Dell'", "L'", "M'", "Mc", "O'", )
def __init__(self, parent = None, **kw):
# Inherits from DirectFrame
# A Direct Frame can have:
# - A background texture (pass in path to image, or Texture Card)
# - A midground geometry item (pass in geometry)
# - A foreground text Node (pass in text string or Onscreen Text)
# For a direct entry:
# Each button has 3 states (focus, noFocus, disabled)
# The same image/geom/text can be used for all three states or each
# state can have a different text/geom/image
# State transitions happen automatically based upon mouse interaction
optiondefs = (
# Define type of DirectGuiWidget
('pgFunc', PGEntry, None),
('numStates', 3, None),
('state', DGG.NORMAL, None),
('entryFont', None, DGG.INITOPT),
('width', 10, self.updateWidth),
('numLines', 1, self.updateNumLines),
('focus', 0, self.setFocus),
('cursorKeys', 1, self.setCursorKeysActive),
('obscured', 0, self.setObscureMode),
# Setting backgroundFocus allows the entry box to get keyboard
# events that are not handled by other things (i.e. events that
# fall through to the background):
('backgroundFocus', 0, self.setBackgroundFocus),
# Text used for the PGEntry text node
# NOTE: This overrides the DirectFrame text option
('initialText', '', DGG.INITOPT),
# Enable or disable text overflow scrolling
('overflow', 0, self.setOverflowMode),
# Command to be called on hitting Enter
('command', None, None),
('extraArgs', [], None),
# Command to be called when enter is hit but we fail to submit
('failedCommand', None, None),
('failedExtraArgs',[], None),
# commands to be called when focus is gained or lost
('focusInCommand', None, None),
('focusInExtraArgs', [], None),
('focusOutCommand', None, None),
('focusOutExtraArgs', [], None),
# Sounds to be used for button events
('rolloverSound', DGG.getDefaultRolloverSound(), self.setRolloverSound),
('clickSound', DGG.getDefaultClickSound(), self.setClickSound),
('autoCapitalize', 0, self.autoCapitalizeFunc),
('autoCapitalizeAllowPrefixes', DirectEntry.AllowCapNamePrefixes, None),
('autoCapitalizeForcePrefixes', DirectEntry.ForceCapNamePrefixes, None),
)
# Merge keyword options with default options
self.defineoptions(kw, optiondefs)
# Initialize superclasses
DirectFrame.__init__(self, parent)
if self['entryFont'] == None:
font = DGG.getDefaultFont()
else:
font = self['entryFont']
# Create Text Node Component
self.onscreenText = self.createcomponent(
'text', (), None,
OnscreenText,
(), parent = hidden,
# Pass in empty text to avoid extra work, since its really
# The PGEntry which will use the TextNode to generate geometry
text = '',
align = TextNode.ALeft,
font = font,
scale = 1,
# Don't get rid of the text node
mayChange = 1)
# We can get rid of the node path since we're just using the
# onscreenText as an easy way to access a text node as a
# component
self.onscreenText.removeNode()
# Bind command function
self.bind(DGG.ACCEPT, self.commandFunc)
self.bind(DGG.ACCEPTFAILED, self.failedCommandFunc)
self.accept(self.guiItem.getFocusInEvent(), self.focusInCommandFunc)
self.accept(self.guiItem.getFocusOutEvent(), self.focusOutCommandFunc)
# listen for auto-capitalize events on a separate object to prevent
# clashing with other parts of the system
self._autoCapListener = DirectObject()
# Call option initialization functions
self.initialiseoptions(DirectEntry)
if not hasattr(self, 'autoCapitalizeAllowPrefixes'):
self.autoCapitalizeAllowPrefixes = DirectEntry.AllowCapNamePrefixes
if not hasattr(self, 'autoCapitalizeForcePrefixes'):
self.autoCapitalizeForcePrefixes = DirectEntry.ForceCapNamePrefixes
# Update TextNodes for each state
for i in range(self['numStates']):
self.guiItem.setTextDef(i, self.onscreenText.textNode)
# Now we should call setup() again to make sure it has the
# right font def.
self.setup()
# Update initial text
self.unicodeText = 0
if self['initialText']:
self.enterText(self['initialText'])
def destroy(self):
self.ignoreAll()
self._autoCapListener.ignoreAll()
DirectFrame.destroy(self)
def setup(self):
self.guiItem.setupMinimal(self['width'], self['numLines'])
def updateWidth(self):
self.guiItem.setMaxWidth(self['width'])
def updateNumLines(self):
self.guiItem.setNumLines(self['numLines'])
def setFocus(self):
PGEntry.setFocus(self.guiItem, self['focus'])
def setCursorKeysActive(self):
PGEntry.setCursorKeysActive(self.guiItem, self['cursorKeys'])
def setOverflowMode(self):
PGEntry.set_overflow_mode(self.guiItem, self['overflow'])
def setObscureMode(self):
PGEntry.setObscureMode(self.guiItem, self['obscured'])
def setBackgroundFocus(self):
PGEntry.setBackgroundFocus(self.guiItem, self['backgroundFocus'])
def setRolloverSound(self):
rolloverSound = self['rolloverSound']
if rolloverSound:
self.guiItem.setSound(DGG.ENTER + self.guiId, rolloverSound)
else:
self.guiItem.clearSound(DGG.ENTER + self.guiId)
def setClickSound(self):
clickSound = self['clickSound']
if clickSound:
self.guiItem.setSound(DGG.ACCEPT + self.guiId, clickSound)
else:
self.guiItem.clearSound(DGG.ACCEPT + self.guiId)
def commandFunc(self, event):
if self['command']:
# Pass any extra args to command
self['command'](*[self.get()] + self['extraArgs'])
def failedCommandFunc(self, event):
if self['failedCommand']:
# Pass any extra args
self['failedCommand'](*[self.get()] + self['failedExtraArgs'])
def autoCapitalizeFunc(self):
if self['autoCapitalize']:
self._autoCapListener.accept(self.guiItem.getTypeEvent(), self._handleTyping)
self._autoCapListener.accept(self.guiItem.getEraseEvent(), self._handleErasing)
else:
self._autoCapListener.ignore(self.guiItem.getTypeEvent())
self._autoCapListener.ignore(self.guiItem.getEraseEvent())
def focusInCommandFunc(self):
if self['focusInCommand']:
self['focusInCommand'](*self['focusInExtraArgs'])
if self['autoCapitalize']:
self.accept(self.guiItem.getTypeEvent(), self._handleTyping)
self.accept(self.guiItem.getEraseEvent(), self._handleErasing)
def _handleTyping(self, guiEvent):
self._autoCapitalize()
def _handleErasing(self, guiEvent):
self._autoCapitalize()
def _autoCapitalize(self):
name = self.get().decode('utf-8')
# capitalize each word, allowing for things like McMutton
capName = ''
# track each individual word to detect prefixes like Mc
wordSoFar = ''
# track whether the previous character was part of a word or not
wasNonWordChar = True
for i, character in enumerate(name):
# test to see if we are between words
# - Count characters that can't be capitalized as a break between words
# This assumes that string.lower and string.upper will return different
# values for all unicode letters.
# - Don't count apostrophes as a break between words
if character.lower() == character.upper() and character != "'":
# we are between words
wordSoFar = ''
wasNonWordChar = True
else:
capitalize = False
if wasNonWordChar:
# first letter of a word, capitalize it unconditionally;
capitalize = True
elif (character == character.upper() and
len(self.autoCapitalizeAllowPrefixes) and
wordSoFar in self.autoCapitalizeAllowPrefixes):
# first letter after one of the prefixes, allow it to be capitalized
capitalize = True
elif (len(self.autoCapitalizeForcePrefixes) and
wordSoFar in self.autoCapitalizeForcePrefixes):
# first letter after one of the force prefixes, force it to be capitalized
capitalize = True
if capitalize:
# allow this letter to remain capitalized
character = character.upper()
else:
character = character.lower()
wordSoFar += character
wasNonWordChar = False
capName += character
self.enterText(capName.encode('utf-8'))
def focusOutCommandFunc(self):
if self['focusOutCommand']:
self['focusOutCommand'](*self['focusOutExtraArgs'])
if self['autoCapitalize']:
self.ignore(self.guiItem.getTypeEvent())
self.ignore(self.guiItem.getEraseEvent())
def set(self, text):
""" Changes the text currently showing in the typable region;
does not change the current cursor position. Also see
enterText(). """
if sys.version_info >= (3, 0):
assert not isinstance(text, bytes)
self.unicodeText = True
self.guiItem.setWtext(text)
else:
self.unicodeText = isinstance(text, unicode)
if self.unicodeText:
self.guiItem.setWtext(text)
else:
self.guiItem.setText(text)
def get(self, plain = False):
""" Returns the text currently showing in the typable region.
If plain is True, the returned text will not include any
formatting characters like nested color-change codes. """
wantWide = self.unicodeText or self.guiItem.isWtext()
if not self.directWtext.getValue():
# If the user has configured wide-text off, then always
# return an 8-bit string. This will be encoded if
# necessary, according to Panda's default encoding.
wantWide = False
if plain:
if wantWide:
return self.guiItem.getPlainWtext()
else:
return self.guiItem.getPlainText()
else:
if wantWide:
return self.guiItem.getWtext()
else:
return self.guiItem.getText()
def getCursorPosition(self):
return self.guiItem.getCursorPosition()
def setCursorPosition(self, pos):
if (pos < 0):
self.guiItem.setCursorPosition(self.guiItem.getNumCharacters() + pos)
else:
self.guiItem.setCursorPosition(pos)
def getNumCharacters(self):
return self.guiItem.getNumCharacters()
def enterText(self, text):
""" sets the entry's text, and moves the cursor to the end """
self.set(text)
self.setCursorPosition(self.guiItem.getNumCharacters())
def getFont(self):
return self.onscreenText.getFont()
def getBounds(self, state = 0):
# Compute the width and height for the entry itself, ignoring
# geometry etc.
tn = self.onscreenText.textNode
mat = tn.getTransform()
align = tn.getAlign()
lineHeight = tn.getLineHeight()
numLines = self['numLines']
width = self['width']
if align == TextNode.ALeft:
left = 0.0
right = width
elif align == TextNode.ACenter:
left = -width / 2.0
right = width / 2.0
elif align == TextNode.ARight:
left = -width
right = 0.0
bottom = -0.3 * lineHeight - (lineHeight * (numLines - 1))
top = lineHeight
self.ll.set(left, 0.0, bottom)
self.ur.set(right, 0.0, top)
self.ll = mat.xformPoint(Point3.rfu(left, 0.0, bottom))
self.ur = mat.xformPoint(Point3.rfu(right, 0.0, top))
vec_right = Vec3.right()
vec_up = Vec3.up()
left = (vec_right[0] * self.ll[0]
+ vec_right[1] * self.ll[1]
+ vec_right[2] * self.ll[2])
right = (vec_right[0] * self.ur[0]
+ vec_right[1] * self.ur[1]
+ vec_right[2] * self.ur[2])
bottom = (vec_up[0] * self.ll[0]
+ vec_up[1] * self.ll[1]
+ vec_up[2] * self.ll[2])
top = (vec_up[0] * self.ur[0]
+ vec_up[1] * self.ur[1]
+ vec_up[2] * self.ur[2])
self.ll = Point3(left, 0.0, bottom)
self.ur = Point3(right, 0.0, top)
# Scale bounds to give a pad around graphics. We also want to
# scale around the border width.
pad = self['pad']
borderWidth = self['borderWidth']
self.bounds = [self.ll[0] - pad[0] - borderWidth[0],
self.ur[0] + pad[0] + borderWidth[0],
self.ll[2] - pad[1] - borderWidth[1],
self.ur[2] + pad[1] + borderWidth[1]]
return self.bounds