Made this for someone in DBPRO discussion as an example, but thought it might be a neat code snippet aswell.
Basically sets up a quick system for creating and checking clickable buttons with a normal, hover, and pushed state.
REM ** Buttons Example
REM ** Courtesy of Martin Enderleit. :)
REM - A few constants to represent the various button states.
#constant BUTTON_NORMAL 1
#constant BUTTON_HOVER 2
#constant BUTTON_PUSHED 3
REM - User defined type holding info about buttons.
type BUTTON_TYPE
xpos as integer REM - X Position of the button
ypos as integer REM - Y Position of the button
width as integer REM - Width of the button
height as integer REM - Height of the button
img as integer REM - Image of the button
hover as integer REM - Image for hover
pushed as integer REM - Image for pushed down
state as integer REM - The current state of the button (Normal, Hover, or Pushed down)
endtype
set window on
set display mode 1024, 768, 32
sync on : sync rate 0
randomize timer()
REM - Create some images for the buttons.
CreateButtons()
REM - Sets up global variables and dynamic array.
InitButtons()
REM - Add a few buttons
for i=0 to 9
a = NewButton(rnd(991), rnd(735), 32, 32, 1, 2, 3)
next i
REM - Main Loop
repeat
UpdateButtons()
cls
DrawButtons()
sync
until returnkey()
end
REM - Initialize the globals and the dynamic array for the buttons.
function InitButtons()
global dim gButtons() as BUTTON_TYPE
global gNumButtons as integer
gNumButtons = 0
endfunction
REM - Creates a new button at the specified position.
REM - Img, Hover, and Pushed, are image numbers to represent the different states.
function NewButton(xPos as integer, yPos as integer, Width as integer, Height as integer, Img as integer, Hover as integer, Pushed as integer)
ButtonIndex as integer
ButtonIndex = -1
array insert at bottom gButtons() REM - Adds an element to the dynamic array.
gButtons().xpos = xPos
gButtons().ypos = yPos
gButtons().width = Width
gButtons().height = Height
gButtons().img = Img
gButtons().hover = Hover
gButtons().pushed = Pushed
gButtons().state = BUTTON_NORMAL
ButtonIndex = gNumButtons REM - Assigns the index of this button to the Return value.
inc gNumButtons, 1 REM - Increases the counter for number of buttons.
endfunction ButtonIndex
REM - Deletes a button.
function DeleteButton(ButtonIndex)
if ButtonIndex >= 0 and ButtonIndex < gNumButtons
array delete element gButtons(), ButtonIndex
dec gNumButtons, 1
endif
endfunction
REM - Updates the state of the buttons based on the mouse info.
function UpdateButtons()
mClk = mouseclick()
msX = mousex()
msY = mousey()
if gNumButtons > 0
for i=0 to gNumButtons-1
cX = gButtons(i).xpos
cY = gButtons(i).ypos
cWidth = gButtons(i).width
cHeight = gButtons(i).height
if msX >= cX and msX < cX+cWidth and msY >= cY and msY < cY+cHeight
if mClk = 1
gButtons(i).state = BUTTON_PUSHED
else
gButtons(i).state = BUTTON_HOVER
endif
else
gButtons(i).state = BUTTON_NORMAL
endif
next i
endif
endfunction
REM - Draw the buttons on the screen. Must be called every time screen is updated.
function DrawButtons()
if gNumButtons > 0
for i=0 to gNumButtons-1
if gButtons(i).state = BUTTON_NORMAL then paste image gButtons(i).img, gButtons(i).xpos, gButtons(i).ypos
if gButtons(i).state = BUTTON_HOVER then paste image gButtons(i).hover, gButtons(i).xpos, gButtons(i).ypos
if gButtons(i).state = BUTTON_PUSHED then paste image gButtons(i).pushed, gButtons(i).xpos, gButtons(i).ypos
next i
endif
endfunction
REM - Create some images for the buttons. (Dark grey = Normal, Light Gray = Hover, Green = Pushed)
function CreateButtons()
ink rgb(127,127,127),0
box 0,0,32,32
get image 1,0,0,32,32
ink rgb(191,191,191),0
box 0,0,32,32
get image 2,0,0,32,32
ink rgb(0,255,0),0
box 0,0,32,32
get image 3,0,0,32,32
endfunction
Enjoy!