3f3f653e6f46f0288ffbc1ff80803db2e1bae0b6
4 from pygame
.locals import *
9 class Input(widget
.Widget
):
10 """A single line text input.
12 <pre>Input(value="",size=20)</pre>
15 <dt>value<dd>initial text
16 <dt>size<dd>size for the text box, in characters
19 <strong>Example</strong>
21 w = Input(value="Cuzco the Goat",size=20)
27 def __init__(self
,value
="",size
=20,customFont
= None,**params
):
28 params
.setdefault('cls','input')
29 widget
.Widget
.__init
__(self
,**params
)
31 self
.pos
= len(str(value
))
33 if customFont
!= None:
34 self
.font
= customFont
36 self
.font
= self
.style
.font
37 w
,h
= self
.font
.size("e"*size
)
38 if not self
.style
.height
: self
.style
.height
= h
39 if not self
.style
.width
: self
.style
.width
= w
40 #self.style.height = max(self.style.height,h)
41 #self.style.width = max(self.style.width,w)
42 #self.rect.w=w+self.style.padding_left+self.style.padding_right;
43 #self.rect.h=h+self.style.padding_top+self.style.padding_bottom;
47 r
= pygame
.Rect(0,0,self
.rect
.w
,self
.rect
.h
)
49 cs
= 2 #NOTE: should be in a style
51 w
,h
= self
.font
.size(self
.value
[0:self
.pos
])
53 if x
< 0: self
.vpos
-= -x
54 if x
+cs
> s
.get_width(): self
.vpos
+= x
+cs
-s
.get_width()
56 s
.blit(self
.font
.render(self
.value
, 1, self
.style
.color
),(-self
.vpos
,0))
58 if self
.container
.myfocus
is self
:
59 w
,h
= self
.font
.size(self
.value
[0:self
.pos
])
63 s
.fill(self
.style
.color
,r
)
65 def _setvalue(self
,v
):
66 self
.__dict
__['value'] = v
72 if e
.key
== K_BACKSPACE
:
74 self
._setvalue
(self
.value
[:self
.pos
-1] + self
.value
[self
.pos
:])
76 elif e
.key
== K_DELETE
:
77 if len(self
.value
) > self
.pos
:
78 self
._setvalue
(self
.value
[:self
.pos
] + self
.value
[self
.pos
+1:])
82 self
.pos
= len(self
.value
)
84 if self
.pos
> 0: self
.pos
-= 1
86 elif e
.key
== K_RIGHT
:
87 if self
.pos
< len(self
.value
): self
.pos
+= 1
89 elif e
.key
== K_RETURN
:
96 c
= (e
.unicode).encode('latin-1')
98 self
._setvalue
(self
.value
[:self
.pos
] + c
+ self
.value
[self
.pos
:])
100 except: #ignore weird characters
103 elif e
.type == FOCUS
:
109 if self
.container
.myfocus
is self
: self
.pcls
= "focus"
113 def __setattr__(self
,k
,v
):
118 _v
= self
.__dict
__.get(k
,NOATTR
)
120 if k
== 'value' and _v
!= NOATTR
and _v
!= v
:
124 class Password(Input
):
125 """A password input, text is *-ed out.
127 <pre>Password(value="",size=20)</pre>
130 <dt>value<dd>initial text
131 <dt>size<dd>size for the text box, in characters
134 <strong>Example</strong>
136 w = Password(value="password",size=20)
138 w = Password("53[r3+")
145 show
=len(self
.value
)*hidden
147 #print "self.value:",self.value
149 if self
.pos
== None: self
.pos
= len(self
.value
)
151 r
= pygame
.Rect(0,0,self
.rect
.w
,self
.rect
.h
)
153 cs
= 2 #NOTE: should be in a style
155 w
,h
= self
.font
.size(show
)
157 if x
< 0: self
.vpos
-= -x
158 if x
+cs
> s
.get_width(): self
.vpos
+= x
+cs
-s
.get_width()
160 s
.blit(self
.font
.render(show
, 1, self
.style
.color
),(-self
.vpos
,0))
162 if self
.container
.myfocus
is self
:
163 #w,h = self.font.size(self.value[0:self.pos])
164 w
,h
= self
.font
.size(show
[0:self
.pos
])
168 s
.fill(self
.style
.color
,r
)