90cdd1ef37b252b0a29de41554ac2a42969ae8b4
4 from pygame
.locals import *
10 class _button(widget
.Widget
):
11 def __init__(self
,**params
):
12 widget
.Widget
.__init
__(self
,**params
)
16 if e
.type == ENTER
: self
.repaint()
17 elif e
.type == EXIT
: self
.repaint()
18 elif e
.type == FOCUS
: self
.repaint()
19 elif e
.type == BLUR
: self
.repaint()
20 elif e
.type == KEYDOWN
:
21 if e
.key
== K_SPACE
or e
.key
== K_RETURN
:
24 elif e
.type == MOUSEBUTTONDOWN
:
29 sub
= pygame
.event
.Event(CLICK
,{'pos':(0,0),'button':1})
30 #self.send(sub.type,sub)
35 elif e
.type == MOUSEBUTTONUP
:
42 if self
.state
== 0 and self
.container
.myhover
is self
:
44 if self
.state
== 1 and self
.container
.myhover
is self
:
51 class Button(_button
):
52 """A button, buttons can be clicked, they are usually used to set up callbacks.
54 <pre>Button(value=None)</pre>
57 <dt>value<dd>either a widget or a string
60 <strong>Example</strong>
62 w = gui.Button("Click Me")
63 w.connect(gui.CLICK,fnc,value)
66 def __init__(self
,value
=None,**params
):
67 params
.setdefault('cls','button')
68 _button
.__init
__(self
,**params
)
71 def __setattr__(self
,k
,v
):
72 if k
== 'value' and type(v
) == str: v
= basic
.Label(v
,cls
=self
.cls
+".label")
73 _v
= self
.__dict
__.get(k
,NOATTR
)
75 if k
== 'value' and v
!= None:
78 if k
== 'value' and _v
!= NOATTR
and _v
!= None and _v
!= v
:
82 def resize(self
,width
=None,height
=None):
83 self
.value
.rect
.x
,self
.value
.rect
.y
= 0,0
84 self
.value
.rect
.w
,self
.value
.rect
.h
= self
.value
.resize(width
,height
)
85 return self
.value
.rect
.w
,self
.value
.rect
.h
87 # self.value._resize()
88 # self.rect.w,self.rect.h = self.value.rect_margin.w,self.value.rect_margin.h
90 # if self.style.width: self.rect.w = max(self.rect.w,self.style.width)
91 # if self.style.height: self.rect.w = max(self.rect.w,self.style.height)
93 # xt,xr,xb,xl = self.value.getspacing()
95 # self.value._resize(self.rect.w-(xl+xr),self.rect.h-(xt+xb))
98 self
.value
.pcls
= self
.pcls
99 self
.value
.paint(surface
.subsurface(s
,self
.value
.rect
))
101 class Switch(_button
):
102 """A switch can have two states, True or False.
104 <pre>Switch(value=False)</pre>
107 <dt>value<dd>initial value, (True, False)
110 <strong>Example</strong>
113 w.connect(gui.CHANGE,fnc,value)
116 def __init__(self
,value
=False,**params
):
117 params
.setdefault('cls','switch')
118 _button
.__init
__(self
,**params
)
122 self
.style
.width
= img
.get_width()
123 self
.style
.height
= img
.get_height()
127 #if self.container.myhover is self: self.pcls = "hover"
128 if self
.value
: img
= self
.style
.on
129 else: img
= self
.style
.off
132 def __setattr__(self
,k
,v
):
133 _v
= self
.__dict
__.get(k
,NOATTR
)
135 if k
== 'value' and _v
!= NOATTR
and _v
!= v
:
140 self
.value
= not self
.value
142 class Checkbox(_button
):
143 """Within a Group of Checkbox widgets several may be selected at a time.
145 <pre>Checkbox(group,value=None)</pre>
148 <dt>group<dd>a gui.Group for the Checkbox to belong to
149 <dt>value<dd>the value
152 <strong>Example</strong>
154 g = gui.Group(name='colors',value=['r','b'])
158 t.td(gui.Label('Red'))
159 t.td(gui.Checkbox(g,'r'))
161 t.td(gui.Label('Green'))
162 t.td(gui.Checkbox(g,'g'))
164 t.td(gui.Label('Blue'))
165 t.td(gui.Checkbox(g,'b'))
169 def __init__(self
,group
,value
=None,**params
):
170 params
.setdefault('cls','checkbox')
171 _button
.__init
__(self
,**params
)
174 if self
.group
.value
== None:
175 self
.group
.value
= []
179 self
.style
.width
= img
.get_width()
180 self
.style
.height
= img
.get_height()
184 #if self.container.myhover is self: self.pcls = "hover"
185 if self
.value
in self
.group
.value
: img
= self
.style
.on
186 else: img
= self
.style
.off
191 if self
.value
in self
.group
.value
:
192 self
.group
.value
.remove(self
.value
)
194 self
.group
.value
.append(self
.value
)
197 class Radio(_button
):
198 """Within a Group of Radio widgets only one may be selected at a time.
200 <pre>Radio(group,value=None)</pre>
203 <dt>group<dd>a gui.Group for the Radio to belong to
204 <dt>value<dd>the value
207 <strong>Example</strong>
209 g = gui.Group(name='colors',value='g')
213 t.td(gui.Label('Red'))
214 t.td(gui.Radio(g,'r'))
216 t.td(gui.Label('Green'))
217 t.td(gui.Radio(g,'g'))
219 t.td(gui.Label('Blue'))
220 t.td(gui.Radio(g,'b'))
225 def __init__(self
,group
=None,value
=None,**params
):
226 params
.setdefault('cls','radio')
227 _button
.__init
__(self
,**params
)
233 self
.style
.width
= img
.get_width()
234 self
.style
.height
= img
.get_height()
238 #if self.container.myhover is self: self.pcls = "hover"
239 if self
.group
.value
== self
.value
: img
= self
.style
.on
240 else: img
= self
.style
.off
244 self
.group
.value
= self
.value
247 """Within a Group of Tool widgets only one may be selected at a time.
249 <pre>Tool(group,widget=None,value=None)</pre>
252 <dt>group<dd>a gui.Group for the Tool to belong to
253 <dt>widget<dd>a widget to appear on the Tool (similar to a Button)
254 <dt>value<dd>the value
257 <strong>Example</strong>
259 g = gui.Group(name='colors',value='g')
263 t.td(gui.Tool(g,'Red','r'))
265 t.td(gui.Tool(g,'Green','g'))
267 t.td(gui.Tool(g,'Blue','b'))
271 def __init__(self
,group
,widget
=None,value
=None,**params
): #TODO widget= could conflict with module widget
272 params
.setdefault('cls','tool')
273 _button
.__init
__(self
,**params
)
279 self
.setwidget(widget
)
281 if self
.group
.value
== self
.value
: self
.pcls
= "down"
283 def setwidget(self
,w
):
286 def resize(self
,width
=None,height
=None):
287 self
.widget
.rect
.w
,self
.widget
.rect
.h
= self
.widget
.resize()
288 #self.widget._resize()
289 #self.rect.w,self.rect.h = self.widget.rect_margin.w,self.widget.rect_margin.h
291 return self
.widget
.rect
.w
,self
.widget
.rect
.h
294 _button
.event(self
,e
)
295 if self
.group
.value
== self
.value
: self
.pcls
= "down"
298 if self
.group
.value
== self
.value
: self
.pcls
= "down"
299 self
.widget
.paint(surface
.subsurface(s
,self
.widget
.rect
))
302 self
.group
.value
= self
.value
303 for w
in self
.group
.widgets
:
304 if w
!= self
: w
.pcls
= ""
308 """TODO - might be deprecated
310 def __init__(self
,cls
,**params
):
312 _button
.__init
__(self
,**params
)
314 self
.style
.width
= s
.get_width()
315 self
.style
.height
= s
.get_height()
320 #if self.state == 0 and hasattr(self.container,'myhover') and self.container.myhover is self: self.pcls = "hover"
321 #if self.state == 1 and hasattr(self.container,'myhover') and self.container.myhover is self: self.pcls = "down"
322 s
.blit(self
.style
.image
,(0,0))
325 """A link, links can be clicked, they are usually used to set up callbacks.
326 Basically the same as the button widget, just text only with a different cls. Made for
329 <pre>Link(value=None)</pre>
332 <dt>value<dd>a string
335 <strong>Example</strong>
337 w = gui.Link("Click Me")
338 w.connect(gui.CLICK,fnc,value)
341 def __init__(self
,value
,**params
):
342 params
.setdefault('focusable',True)
343 params
.setdefault('cls','link')
344 _button
.__init
__(self
,**params
)
346 self
.font
= self
.style
.font
347 self
.style
.width
, self
.style
.height
= self
.font
.size(self
.value
)
350 s
.blit(self
.font
.render(self
.value
, 1, self
.style
.color
),(0,0))