"Slider" object (using new touch statement)
Posted: Tue May 07, 2013 10:05 am
' SLIDER OBJECT
' usage: value=slider(title$,xtop,ytop,height,xtouch,ytouch,mode)
' where:
' value = return value (percentage between 0 and 100)
' title$ = name, displayed at the bottom
' xtop and ytop = left upper corner of slider
' height = length of the scale in the slider
' xtouch and ytouch = touch coordinates (when mode=0)
' mode = 1 for creation of the slider
' mode = 0 for polling the slider if it was touched
' mode =-1 for removing the slider from the screen
'
' The width of a slider is fixed: 70 pixels
' The value of the slider is displayed at the top of the slider
' The amount of sliders on the screen is not restricted. Just
' take care that they do not overlap on the screen.
'
graphics
graphics clear .8,.8,.8
fill color .8,.8,.8
draw color 0,0,0
' put two sliders on the screen
slider("volume",300,200,300,0,0,1)
slider("pitch",500,300,250,0,0,1)
' scan for screen-touch
loop2:
if touch_y(0)=-1 then loop2
' and pass the x- and y- values to the sliders
xtc=touch_x(0) ! ytc=touch_y(0)
vol=slider("volume",300,200,300,xtc,ytc,0)
pit=slider("pitch",500,300,250,xtc,ytc,0)
' use the return values "vol" and "pit" for
' whatever they serve for
goto loop2
end
def slider (tit$,xt,yt,hgt,xtouch,ytouch,ind)
xb=xt+70 ! if hgt<120 then hgt=120 ! yb=yt+hgt+70 ! dy=hgt/10
if ind=-1 then
fill rect xt-2,yt-2 to xb+2,yb+2 ! return
end if
if ind=1 then
draw size 3 ! draw font size 14
draw rect xt,yt to xb,yb
draw line xt,yt+20 to xb,yt+20
draw text tit$ at xt+(70-8*len(tit$))/2,yb-20
ys=yb-30
else
if xtouch<xt or xtouch>xb or ytouch<yt or ytouch>yb then return
ys=ytouch
if ys<yt+40 then ys=yt+40 ! if ys>yb-30 then ys=yb-30
end if
draw size 2
graphics lock
fill color .8,.8,.8 ! draw font size 14
fill rect xt+2,yt+33 to xb-2,yb-25
draw rect xt+28,yt+40 to xt+32,yb-30
y=yb-30+dy
for i=0 to 100 step 10
y=y-dy ! i$=i
draw line xt+24,y to xt+36,y
draw text i$ at xt+40,y-9
next i
fill color 0,0,.6
fill rect xt+6,ys-4 to xt+54,ys+4 ! fill color .8,.8,.8
tt=(yb-ys-30)*100/(yb-yt-70)
fill rect xt+8,yt+2 to xb-2,yt+15
draw text n2a$(tt,5,1) at xt+8,yt
graphics unlock
draw font size 20
slider=tt
end def
def n2a$(num,lang,dec)
b$=" "
fac=10^dec
num$=int(fac*num+.5)/fac
tot=lang-len(num$)
if tot<1 then tot=1
a$=substr$(b$,1,tot) & num$
n2a$=a$
end def
' usage: value=slider(title$,xtop,ytop,height,xtouch,ytouch,mode)
' where:
' value = return value (percentage between 0 and 100)
' title$ = name, displayed at the bottom
' xtop and ytop = left upper corner of slider
' height = length of the scale in the slider
' xtouch and ytouch = touch coordinates (when mode=0)
' mode = 1 for creation of the slider
' mode = 0 for polling the slider if it was touched
' mode =-1 for removing the slider from the screen
'
' The width of a slider is fixed: 70 pixels
' The value of the slider is displayed at the top of the slider
' The amount of sliders on the screen is not restricted. Just
' take care that they do not overlap on the screen.
'
graphics
graphics clear .8,.8,.8
fill color .8,.8,.8
draw color 0,0,0
' put two sliders on the screen
slider("volume",300,200,300,0,0,1)
slider("pitch",500,300,250,0,0,1)
' scan for screen-touch
loop2:
if touch_y(0)=-1 then loop2
' and pass the x- and y- values to the sliders
xtc=touch_x(0) ! ytc=touch_y(0)
vol=slider("volume",300,200,300,xtc,ytc,0)
pit=slider("pitch",500,300,250,xtc,ytc,0)
' use the return values "vol" and "pit" for
' whatever they serve for
goto loop2
end
def slider (tit$,xt,yt,hgt,xtouch,ytouch,ind)
xb=xt+70 ! if hgt<120 then hgt=120 ! yb=yt+hgt+70 ! dy=hgt/10
if ind=-1 then
fill rect xt-2,yt-2 to xb+2,yb+2 ! return
end if
if ind=1 then
draw size 3 ! draw font size 14
draw rect xt,yt to xb,yb
draw line xt,yt+20 to xb,yt+20
draw text tit$ at xt+(70-8*len(tit$))/2,yb-20
ys=yb-30
else
if xtouch<xt or xtouch>xb or ytouch<yt or ytouch>yb then return
ys=ytouch
if ys<yt+40 then ys=yt+40 ! if ys>yb-30 then ys=yb-30
end if
draw size 2
graphics lock
fill color .8,.8,.8 ! draw font size 14
fill rect xt+2,yt+33 to xb-2,yb-25
draw rect xt+28,yt+40 to xt+32,yb-30
y=yb-30+dy
for i=0 to 100 step 10
y=y-dy ! i$=i
draw line xt+24,y to xt+36,y
draw text i$ at xt+40,y-9
next i
fill color 0,0,.6
fill rect xt+6,ys-4 to xt+54,ys+4 ! fill color .8,.8,.8
tt=(yb-ys-30)*100/(yb-yt-70)
fill rect xt+8,yt+2 to xb-2,yt+15
draw text n2a$(tt,5,1) at xt+8,yt
graphics unlock
draw font size 20
slider=tt
end def
def n2a$(num,lang,dec)
b$=" "
fac=10^dec
num$=int(fac*num+.5)/fac
tot=lang-len(num$)
if tot<1 then tot=1
a$=substr$(b$,1,tot) & num$
n2a$=a$
end def