I've seen quite a few people asking how to make buttons/guis so here are some useful functions to help.
MakeButton(ButtonName$,Left,Top,Right,Bottom) - Adds a button
MouseOver(ButtonName$) - Returns a 1 if the mouse is over the button
DeleteButton(ButtonName$) - Deletes the button
DeleteAllButtons() - Deletes all buttons
gosub InitialiseMenu - A sub routine that must be called before you can use any other functions
gosub StoreMouseData - A sub routine that must be called at the start of your main loop
Here are the functions in an example:
rem Project: Button Example
rem Created: 21/02/2005 19:46:44
rem Author: Andrew Neale
remstart
Feel free to use this as much as you want but please give me
credit!
remend
rem Setup display
set display mode 640,480,32
rem Make a cube
make object cube 1,50
rem *** Initialise menu - This must be done! ***
gosub InitialiseMenu
rem Setup buttons
MakeButton("RedButton",0,0,32,32)
MakeButton("GreenButton",0,32,32,64)
MakeButton("BlueButton",0,64,32,96)
rem Activate the 3D backdrop
backdrop on
rem Setup synchronisation
sync on
sync rate 0
rem Start the main loop
do
rem *** Store mouse data -This must be done! ***
gosub StoreMouseData
rem Make and use some buttons
ink rgb(255,0,0),0
box 0,0,32,32
if Mouse.Click=1
if MouseOver("RedButton")=1
color object 1,rgb(255,0,0)
endif
endif
ink rgb(0,255,0),0
box 0,32,32,64
if Mouse.Click=1
if MouseOver("GreenButton")=1
color object 1,rgb(0,255,0)
endif
endif
ink rgb(0,0,255),0
box 0,64,32,96
if Mouse.Click=1
if MouseOver("BlueButton")=1
color object 1,rgb(0,0,255)
endif
endif
rem Update the screen
sync
rem End the loop
loop
rem *************************************************************
rem Here are the functions!
rem *************************************************************
rem MouseOver function
function MouseOver(ButtonName$)
MouseOver=0
for b=1 to 100
if Button(b).Name=ButtonName$
if Mouse.XPosition>=Button(b).Left
if Mouse.XPosition=<Button(b).Right
if Mouse.YPosition>=Button(b).Top
if Mouse.YPosition=<Button(b).Bottom
MouseOver=1
endif
endif
endif
endif
endif
next b
endfunction MouseOver
rem MakeButton function
function MakeButton(ButtonName$,Left,Top,Right,Bottom)
Button(LastButton).Name=ButtonName$
Button(LastButton).Left=Left
Button(LastButton).Top=Top
Button(LastButton).Right=Right
Button(LastButton).Bottom=Bottom
inc LastButton,1
endfunction
rem DeleteButton function
function DeleteButton(ButtonName$)
for b=1 to 100
if Button(b).Name=ButtonName$
Button(b).Name=""
Button(b).Left=0
Button(b).Top=0
Button(b).Right=0
Button(b).Bottom=0
endif
next b
endfunction
rem DeleteAllButtons function
function DeleteAllButtons()
for b=1 to 100
Button(b).Name=""
Button(b).Left=0
Button(b).Top=0
Button(b).Right=0
Button(b).Bottom=0
next b
endfunction
rem InitialiseMenu sub routine
InitialiseMenu:
type Button
Name as string
Left as integer
Top as integer
Right as integer
Bottom as integer
endtype
global dim Button(100) as Button
global LastButton as integer
LastButton=1
type Mouse
XPosition as integer
YPosition as integer
ZPosition as integer
XSpeed as float
YSpeed as float
ZSpeed as float
Click as integer
endtype
global Mouse as Mouse
return
rem StoreMouseData sub routine
StoreMouseData:
Mouse.Click=mouseclick()
Mouse.XPosition=mousex()
Mouse.YPosition=mousey()
Mouse.ZPosition=mousez()
Mouse.XSpeed=mousemovex()
Mouse.YSpeed=mousemovey()
Mouse.ZSpeed=mousemovez()
return
I plan to add updates for drop down menus and right click menus etc. I've already made them just not as simple functions.
As mentioned in the example, feel free to use this as much as you want but please give me credit.