1 """All sliders and scroll bar widgets have the same parameters.
3 <pre>Slider(value,min,max,size)</pre>
5 <dt>value<dd>initial value
6 <dt>min<dd>minimum value
7 <dt>max<dd>maximum value
8 <dt>size<dd>size of bar in pixels
12 from pygame
.locals import *
20 _SLIDER_HORIZONTAL
= 0
23 class _slider(widget
.Widget
):
24 def __init__(self
,value
,orient
,min,max,size
,step
=1,**params
):
25 params
.setdefault('cls','slider')
26 widget
.Widget
.__init
__(self
,**params
)
27 self
.min,self
.max,self
.value
,self
.orient
,self
.size
,self
.step
= min,max,value
,orient
,size
,step
32 self
.value
= self
.value
33 r
= pygame
.rect
.Rect(0,0,self
.style
.width
,self
.style
.height
)
34 if self
.orient
== _SLIDER_HORIZONTAL
:
35 r
.x
= (self
.value
-self
.min) * (r
.w
-self
.size
) / max(1,self
.max-self
.min);
38 r
.y
= (self
.value
-self
.min) * (r
.h
-self
.size
) / max(1,self
.max-self
.min);
43 pguglobals
.app
.theme
.render(s
,self
.style
.bar
,r
)
47 r
= pygame
.rect
.Rect(0,0,self
.style
.width
,self
.style
.height
)
49 if e
.type == ENTER
: self
.repaint()
50 elif e
.type == EXIT
: self
.repaint()
51 elif e
.type == MOUSEBUTTONDOWN
:
52 if self
.bar
.collidepoint(e
.pos
):
53 self
.grab
= e
.pos
[0],e
.pos
[1]
54 self
.grab_value
= self
.value
56 x
,y
,adj
= e
.pos
[0],e
.pos
[1],1
59 elif e
.type == MOUSEBUTTONUP
:
60 #x,y,adj = e.pos[0],e.pos[1],1
62 elif e
.type == MOUSEMOTION
:
63 if 1 in e
.buttons
and self
.container
.myfocus
is self
:
65 rel
= e
.pos
[0]-self
.grab
[0],e
.pos
[1]-self
.grab
[1]
66 if self
.orient
== _SLIDER_HORIZONTAL
:
68 if d
!= 0: self
.value
= self
.grab_value
+ ((self
.max-self
.min) * rel
[0] / d
)
71 if d
!= 0: self
.value
= self
.grab_value
+ ((self
.max-self
.min) * rel
[1] / d
)
73 x
,y
,adj
= e
.pos
[0],e
.pos
[1],1
75 elif e
.type is KEYDOWN
:
76 if self
.orient
== _SLIDER_HORIZONTAL
and e
.key
== K_LEFT
:
77 self
.value
-= self
.step
79 elif self
.orient
== _SLIDER_HORIZONTAL
and e
.key
== K_RIGHT
:
80 self
.value
+= self
.step
82 elif self
.orient
== _SLIDER_VERTICAL
and e
.key
== K_UP
:
83 self
.value
-= self
.step
85 elif self
.orient
== _SLIDER_VERTICAL
and e
.key
== K_DOWN
:
86 self
.value
+= self
.step
90 if self
.orient
== _SLIDER_HORIZONTAL
:
91 d
= self
.size
/2 - (r
.w
/(self
.max-self
.min+1))/2
92 self
.value
= (x
-d
) * (self
.max-self
.min) / (r
.w
-self
.size
+1) + self
.min
94 d
= self
.size
/2 - (r
.h
/(self
.max-self
.min+1))/2
95 self
.value
= (y
-d
) * (self
.max-self
.min) / (r
.h
-self
.size
+1) + self
.min
98 if self
.container
.myhover
is self
: self
.pcls
= "hover"
99 if (self
.container
.myfocus
is self
and 1 in pygame
.mouse
.get_pressed()): self
.pcls
= "down"
104 def __setattr__(self
,k
,v
):
109 _v
= self
.__dict
__.get(k
,NOATTR
)
111 if k
== 'value' and _v
!= NOATTR
and _v
!= v
:
115 if hasattr(self
,'size'):
116 sz
= min(self
.size
,max(self
.style
.width
,self
.style
.height
))
117 sz
= max(sz
,min(self
.style
.width
,self
.style
.height
))
118 self
.__dict
__['size'] = sz
120 if hasattr(self
,'max') and hasattr(self
,'min'):
121 if self
.max < self
.min: self
.max = self
.min
123 class VSlider(_slider
):
124 """A verticle slider.
126 <pre>VSlider(value,min,max,size)</pre>
128 def __init__(self
,value
,min,max,size
,step
=1,**params
):
129 params
.setdefault('cls','vslider')
130 _slider
.__init
__(self
,value
,_SLIDER_VERTICAL
,min,max,size
,step
,**params
)
132 class HSlider(_slider
):
133 """A horizontal slider.
135 <pre>HSlider(value,min,max,size)</pre>
137 def __init__(self
,value
,min,max,size
,step
=1,**params
):
138 params
.setdefault('cls','hslider')
139 _slider
.__init
__(self
,value
,_SLIDER_HORIZONTAL
,min,max,size
,step
,**params
)
141 class HScrollBar(table
.Table
):
142 """A horizontal scroll bar.
144 <pre>HScrollBar(value,min,max,size,step=1)</pre>
146 def __init__(self
,value
,min,max,size
,step
=1,**params
):
147 params
.setdefault('cls','hscrollbar')
149 table
.Table
.__init
__(self
,**params
)
151 self
.slider
= _slider(value
,_SLIDER_HORIZONTAL
,min,max,size
,step
=step
,cls
=self
.cls
+'.slider')
153 self
.minus
= basic
.Image(self
.style
.minus
)
154 self
.minus
.connect(MOUSEBUTTONDOWN
,self
._click
,-1)
155 self
.slider
.connect(CHANGE
,self
.send
,CHANGE
)
157 self
.minus2
= basic
.Image(self
.style
.minus
)
158 self
.minus2
.connect(MOUSEBUTTONDOWN
,self
._click
,-1)
160 self
.plus
= basic
.Image(self
.style
.plus
)
161 self
.plus
.connect(MOUSEBUTTONDOWN
,self
._click
,1)
165 def _click(self
,value
):
166 self
.slider
.value
+= self
.slider
.step
*value
168 def resize(self
,width
=None,height
=None):
173 h
= self
.slider
.style
.height
176 if w
> (h
*2 + self
.minus
.style
.width
+self
.plus
.style
.width
):
178 ww
+= self
.minus
.style
.width
182 if w
> (h
*2 + self
.minus
.style
.width
+self
.minus2
.style
.width
+self
.plus
.style
.width
):
184 ww
+= self
.minus2
.style
.width
186 if w
> (h
*2 + self
.minus
.style
.width
+self
.plus
.style
.width
):
188 ww
+= self
.plus
.style
.width
191 #HACK: handle theme sizing properly
192 xt
,xr
,xb
,xl
= pguglobals
.app
.theme
.getspacing(self
.slider
)
195 self
.slider
.style
.width
= self
.style
.width
- ww
196 setattr(self
.slider
,'size',self
.size
* self
.slider
.style
.width
/ max(1,self
.style
.width
))
197 return table
.Table
.resize(self
,width
,height
)
200 def __setattr__(self
,k
,v
):
201 if k
in ('min','max','value','step'):
202 return setattr(self
.slider
,k
,v
)
205 def __getattr__(self
,k
):
206 if k
in ('min','max','value','step'):
207 return getattr(self
.slider
,k
)
208 return table
.Table
.__getattr
__(self
,k
) #self.__dict__[k]
210 class VScrollBar(table
.Table
):
211 """A vertical scroll bar.
213 <pre>VScrollBar(value,min,max,size,step=1)</pre>
215 def __init__(self
,value
,min,max,size
,step
=1,**params
):
216 params
.setdefault('cls','vscrollbar')
218 table
.Table
.__init
__(self
,**params
)
220 self
.minus
= basic
.Image(self
.style
.minus
)
221 self
.minus
.connect(MOUSEBUTTONDOWN
,self
._click
,-1)
223 self
.minus2
= basic
.Image(self
.style
.minus
)
224 self
.minus2
.connect(MOUSEBUTTONDOWN
,self
._click
,-1)
226 self
.plus
= basic
.Image(self
.style
.plus
)
227 self
.plus
.connect(MOUSEBUTTONDOWN
,self
._click
,1)
229 self
.slider
= _slider(value
,_SLIDER_VERTICAL
,min,max,size
,step
=step
,cls
=self
.cls
+'.slider')
230 self
.slider
.connect(CHANGE
,self
.send
,CHANGE
)
234 def _click(self
,value
):
235 self
.slider
.value
+= self
.slider
.step
*value
237 def resize(self
,width
=None,height
=None):
240 h
= self
.style
.height
241 w
= self
.slider
.style
.width
244 if h
> (w
*2 + self
.minus
.style
.height
+self
.plus
.style
.height
):
247 hh
+= self
.minus
.style
.height
252 if h
> (w
*2 + self
.minus
.style
.height
+self
.minus2
.style
.height
+self
.plus
.style
.height
):
255 hh
+= self
.minus2
.style
.height
257 if h
> (w
*2 + self
.minus
.style
.height
+self
.plus
.style
.height
):
260 hh
+= self
.plus
.style
.height
263 #HACK: handle theme sizing properly
264 xt
,xr
,xb
,xl
= pguglobals
.app
.theme
.getspacing(self
.slider
)
267 self
.slider
.style
.height
= self
.style
.height
- hh
268 setattr(self
.slider
,'size',self
.size
* self
.slider
.style
.height
/ max(1,self
.style
.height
))
269 return table
.Table
.resize(self
,width
,height
)
271 def __setattr__(self
,k
,v
):
272 if k
in ('min','max','value','step'):
273 return setattr(self
.slider
,k
,v
)
276 def __getattr__(self
,k
):
277 if k
in ('min','max','value','step'):
278 return getattr(self
.slider
,k
)
279 return table
.Table
.__getattr
__(self
,k
)