[Edit]
I hadn't realized how much of performance hit this costs. It's ok for a menu selection screen, but using it as the background for a constantly updating application isn't very efficient. If any one has some ideas on how to improve the Frames for this, I'd be happy to hear! I've started work on a different button routine that uses sprites. It's much better - and I can get 300 buttons simultatneously without a performance hit right now. Anyway, if you have any, please post any improvements on this old routine.
[Old]
Here's a nifty button function to use. I was messing around with some of the examples that come with the DBC documentation and there was a simple selection menu on one of the progs. I used it's premise and designed a button function that has a few more bells and whistles.
It will autosize the button based on the text font and size, but there seems to be a problem overall that I can't quite track down. It seems that the last font size selected influences all the other button sizes. I don't get it because I thought variables in functions were local, and every instance of the function call should be "clean." If anyone has any ideas, I'd love to hear them.
Here's the code. If you decide to use it in your apps, a nanosecond of credit would be nice!
`**************************************
`* Title : make button function
`* Author : latch grapple
`* Date : 24/10/2006
`* Update : 25/10/2006
`* Version: v.02
`**************************************
remstart
------------------------------------------------------------------------
------------------------------------------------------------------------
Make button function - based on a DBC example. I added a few enhancments
including drawing a "raised" button image, automatic sizing, and font
selection. The background color can be changed in the function. It's
labled and pretty easy.
The function and parameters are:
makebutton(x1,y1,x2,y2,fontsize,typeface$,desc$,color$)
x1 = Left x screen coordinate of the button
y1 = Top y screen corrdinate of the button
x2 = Right x screen coordinate of the button
y2 = Bottom y screen corrdinate of the button
fontsize = point size of the descriptive text
typeface$ = the name of the font for the button
desc$ = the descriptive text of the button
color$ = color of buttons currently defined grey, red, green, and blue
grey is the default
Notes about font and textsize in the makebutton() function:
When several font sizes are chosen for different buttons, the last font
size will affect all of the buttons text position and size. It's best
to keep the fonts the same for all of the buttons - at least until I
fix this bug.
One feature is the automatic sizing based on the text. The parameters that
are absolutely necessary are x1,y1 and the description.
makebutton(100,100,0,0,0,"","Button 1","") is perfectly legit and the function
will figure out acceptible dimensions. But this also goes back to the bug.
If you fully define a button and also create a button with the minimum
parameters, the buttons don't size as I would expect. It has something to
do with the text size holding over from the last button to the next...
Latch
-------------------------------------------------------------------------
-------------------------------------------------------------------------
remend
rem ------ Set Up Display -------------
autocam off
set display mode 800,600,32
sync on
sync rate 60
do
cls
if makebutton(100,100,0,0,0,"","Button 1","red") > 0 then click=mouseclick():gosub button1
if makebutton(100,150,300,200,32,"Times New Roman","Button 2","grey") > 0 then click=mouseclick():gosub button2
if makebutton(100,225,700,300,24,"Brush Script MT","Button 3","blue") > 0 then gosub button3
if makebutton(350,325,0,525,0,"","Blah!","green") > 0 then print "BLAH!"
if makebutton(600,20,700,70,0,"","Peep","") > 0 then print "Peep! Peep!"
if makebutton(375,0,0,0,0,"Arial Bold","X","red") > 0 then print "Close":end
sync
loop
end
button1:
cls
If click=1
Print "You have left clicked on Button 1"
endif
if click=2
Print "You have right clicked on Button 1"
endif
Print "Press any key."
suspend for key
return
button2:
cls
If click=1
Print "You have left clicked on Button 2"
endif
if click=2
Print "You have right clicked on Button 2"
endif
Print "Press any key."
suspend for key
return
button3:
cls
Print "Button 3 has been pressed, but I don't care by which mouse button."
Print "Press any key."
suspend for key
return
rem when several font sizes are chosen for different buttons, the last font
rem size will affect all of the buttons text position and size. It's best
rem to keep the fonts the same for all of the buttons - at least until I
rem fix this bug.
function makebutton(x1,y1,x2,y2,fontsize,typeface$,desc$,color$)
rem test valid button limits
if x2 <= x1 then x2 = x1+4
if y2 <= y1 then y2 = y1+4
buttonpressed=0
rem set font
if fontsize <= 0 then fontsize=16
if typeface$="" then typeface$="Arial"
set text font typeface$
`if text size() <> fontsize
set text size fontsize
`endif
rem set x and y based on button text
`print text width(desc$)
`print (y2-y1)/2
if (text width(desc$)+8) > x2-x1 then inc x2,text width(desc$)+8
if (text height(desc$)+8) > y2-y1 then inc y2,text height(desc$)+8
rem draw button background
select color$
case "grey" : ink rgb(192,192,192),0 : endcase
case "red" : ink rgb(255,0,0),0 : endcase
case "green" : ink rgb(0,255,0),0 : endcase
case "blue" : ink rgb(0,0,255),0 : endcase
case default : ink rgb(192,192,192),0 : endcase
endselect
`ink rgb(192,192,192),0
box x1,y1,x2,y2
rem left side
ink rgb(255,255,255),0
line x1+1,y1+1,x1+1,y2-1
line x1+2,y1+1,x1+2,y2-1
rem top
line x1+1,y1+1,x2-1,y1+1
line x1+1,y1+2,x2-1,y1+2
rem right side
ink 0,0
line x2-2,y1+2,x2-2,y2-1
line x2-1,y1+1,x2-1,y2-1
rem bottom
line x1+3,y2-2,x2-1,y2-1
line x1+2,y2-1,x2-1,y2-1
rem code from DBC examples - modified by latch
myx=mousex() : myy=mousey()
if myx>x1+2 and myx<x2-2
if myy>y1+2 and myy<y2-2
buttonpressed=1
endif
endif
if buttonpressed=1 then ink RGB(255,255,0),0
center text (x1+x2)/2,y1+((y2-y1)/2)-fontsize/2,desc$
if mouseclick()=0 then buttonpressed=0
endfunction buttonpressed
Enjoy your day.