rem button functions without sprites
rem IMPORTANT NOTE! TO USE PROPERLY, ALWAYS PUT BDisplay() JUST BEFORE
REM THE SYNC FUNCTION!
sync on
sync rate 45
remstart
TYPE simpleButton
x as int
y as int
width as int
height as int
rem filename is for the image, if used, for the button
filename as string
rem buttonname is a value in case you want to test strings instead of
rem array indexes.
buttonname as string
rem I'm not using ButtonText but have it in here so you can make simpler
rem ones later.
rem buttontext as string
endtype
remend
rem setting transparent color to white
set image colorkey 255,255,255
global BSize as integer
BSize = 100
rem since this is meant to be used added on to your program
rem you can make it use any starting value for image numbers
rem so it doesn't get in the way of your own image numbers
rem also, needed to index 3 different starting points due to highlighted button
rem images. If you create more than 999 buttons, remember to change
rem these numbers so not to overlap
global ImageNumStartValue as integer
ImageNumStartValue = 3000
global ImageNumStartValueH as integer
ImageNumStartValueH = 4000
global ImageNumStartValueC as integer
ImageNumStartValueC = 5000
rem set this to 0 to turn it off, but it does so for every button lest you edit
rem the BDisplay() command.
global transparencyOn as integer
transparencyOn = 1
rem lastButtonClicked is for telling if, when the mouse button is let-up,
rem which button the user pressed and released.
global LastButtonClicked as integer
LastButtonClicked = 0
rem MouseDown is for testing when the mouse comes up...
global MouseDown as integer
MouseDown = 0
rem the following is because UDT's don't work
rem x y width height, self explanatory
rem active is whether the button shows or not
rem FileName is the image's name string
rem ButtonName is the internal reference, you can leave blank
rem or use for complex systems of buttons later
rem HighFileName is for when the button is highlighted
rem ClickedFileName is for when the button is clicked
rem SButtonHighlighted is whether or not the button is highlighted
rem SButtonClicked is the same for clicked
global dim SButton(BSize) as integer
global dim SButtonX(BSize) as integer
global dim SButtonY(BSize) as integer
global dim SButtonWidth(BSize) as integer
global dim SButtonHeight(BSize) as integer
global dim SButtonHighlighted(BSize) as integer
global dim SButtonClicked(BSize) as integer
global dim SButtonActive(BSize) as integer
global dim SButtonSimpleFileName(BSize) as string
global dim SButtonHighFileName(BSize) as string
global dim SButtonClickedFileName(BSize) as string
global dim SButtonName(BSize) as string
BMakeButtonBox(1, "h", 0, 200, 300,24, 1)
BMakeButtonPictureBox(2, "but", "but.bmp", "buthigh.bmp", "butclick.bmp", 30, 250,120,60, 0)
do
rem clear screen
cls
rem Refresh the buttons to 0 before testing for collision...
BReset()
rem these two variables are used a lot, so I'm only running these two functions once per loop
mclicked = MouseClick()
highlighted = BCheckMouseInButton()
rem Temp stores which button is being clicked, 0 if none.
rem You'll notice I leave testing this to be 0 for the display functions,
rem I consider this sloppy, but because it's not a big deal or large program
rem I didn't change this.
temp = BClicked(highlighted, mclicked)
rem this next line is where I would run into problems if I used any other language
rem since Basic lets you index arrays differently, the array(0) index equals 1
rem if nothing is clicked, but since that is never displayed anyway, it saves some
rem time checking for problems when nothing is being clicked.
SButtonClicked(temp) = 1
rem call this function to get all your buttons to display.
rem Also the parameter highlighted must be assigned AFTER BReset() otherwise
rem it won't work right!
BDisplay(highlighted)
sync
loop
rem this function takes any active buttons and displays them onscreen
rem transparency is set on for paste image initially
function BDisplay(mouseInside)
for countB = 1 to BSize
if SButtonActive(countB) > 0
if LEN( SButtonSimpleFileName(countB)) < 4
Box SButtonX(countB), SButtonY(countB), SButtonX(countB) + SButtonWidth(countB), SButtonY(countB) + SButtonHeight(countB)
if mouseInside > 0 then Box SButtonX(countB), SButtonY(countB), SButtonX(countB) + SButtonWidth(countB), SButtonY(countB) + SButtonHeight(countB), RGB(0,0,255), RGB(0,0,255), RGB(0,0,255), RGB(0,0,255)
if SButtonClicked(countB) > 0 then Box SButtonX(countB), SButtonY(countB), SButtonX(countB) + SButtonWidth(countB), SButtonY(countB) + SButtonHeight(countB), RGB(250,0,255), RGB(250,0,255), RGB(250,0,255), RGB(50,0,255)
else
if mouseInside then SButtonHighlighted(mouseInside) = 1
rem remember to add AND IF CLICKED <> 0
paste image ImageNumStartValue + countB, SButtonX(countB), SButtonY(countB), transparencyOn
if SButtonHighlighted(countB) > 0 then paste image ImageNumStartValueH + countB, SButtonX(countB), SButtonY(countB), transparencyOn
if SButtonClicked(countB) = 1 then paste image ImageNumStartValueC + countB, SButtonX(countB), SButtonY(countB), transparencyOn
endif
endif
next countB
endfunction
rem BMakeButtonBox takes the indexed array, places it,
rem and if centered is 0 it does not center the box, otherwise
rem nomatter what x value, it will put it in the center
rem this function is for PICTURELESS buttons, for testing mostly
function BMakeButtonBox(ind, name$, x, y, width, height,centered)
if centered > 0 then x = BGetCenterX(width)
SButtonX(ind) = x
SButtonY(ind) = y
SButtonWidth(ind) = width
SButtonHeight(ind) = height
SButtonName(ind) = name$
SButtonActive(ind) = 1
endfunction
rem same as above, but with an image
function BMakeButtonPictureBox(ind, name$, picturefile$, highlighted$, clicked$, x, y, width, height,centered)
if centered > 0 then x = BGetCenterX(width)
SButtonX(ind) = x
SButtonY(ind) = y
SButtonWidth(ind) = width
SButtonHeight(ind) = height
SButtonName(ind) = name$
SButtonSimpleFileName(ind) = picturefile$
SButtonHighFileName(ind) = highlighted$
SButtonClickedFileName(ind) = clicked$
SButtonActive(ind) = 1
Load Image SButtonSimpleFileName(ind), ImageNumStartValue + ind
Load Image SButtonHighFileName(ind), ImageNumStartValueH + ind
Load Image SButtonClickedFileName(ind), ImageNumStartValueC + ind
endfunction
rem BGetCenterX returns the X value to place a button if you want the button
rem to be centered.
function BGetCenterX(buttonwidth)
CenterStartXPosition = Screen Width()/2 - buttonwidth/2
endfunction CenterStartXPosition
function BDestroyButton(ind)
SButtonActive(ind) = 0
endfunction
rem BClicked returns what button was clicked before the next BDisplay() is called
rem I DO NOT set the other parameters to 0 so you can easily turn the button off for a while
rem and when you want it displayed again, you only have to call BActivateButton(), and not
rem reinitialize everything.
function BClicked(onBut, clicked)
retval = 0
if onBut > 0 and clicked > 0 then retval = onBut
endfunction retval
rem this function reactivates the button for display
function BActivateButton(ind)
SButtonActive(ind) = 1
endfunction
rem returns which button the mouse was just released from
rem I could not get this to work properly, and don't need it that much
rem so I scrapped it, but left the logic here in case anyone wants
function BUnClicked()
remstart
if MouseDown > 0
if LastButtonClicked <> 0
if MouseClick() = 0
if LastButtonClicked = BCheckMouseInButton()
message STR$(LastButtonClicked)
MouseDown = 0
LastButtonClicked = 0
endif
endif
endif
endif
remend
endfunction retval
rem This is a function to see if the mouse is inside a button, and which button it is
function BCheckMouseInButton()
inButton = 0
for countB = 1 to BSize
if Mousex() > SButtonX(countB) AND MouseX() < SButtonX(countB) + SButtonWidth(countB) AND MouseY() > SButtonY(countB) AND MouseY() < SButtonY(countB) + SButtonHeight(countB) then inButton = countB
next countB
endfunction inButton
rem this function resets all Clicked and Highlighted values to NULL
rem it does NOT affect the ACTIVE parameter
function BReset()
for countB = 1 to BSize
SButtonClicked(countB) = 0
SButtonHighlighted(countB) = 0
next countB
endfunction
rem ***********************************************************************************************************************************************************************************
A simpler version of the MAIN file if you wanna try this out, though I commented out the picture box so you should be able to run both:
BMakeButtonBox(1, "h", 0, 200, 300,24, 1)
do
rem clear screen
cls
rem Refresh the buttons to 0 before testing for collision...
BReset()
rem these two variables are used a lot, so I'm only running these two functions once per loop
mclicked = MouseClick()
highlighted = BCheckMouseInButton()
rem Temp stores which button is being clicked, 0 if none.
rem You'll notice I leave testing this to be 0 for the display functions,
rem I consider this sloppy, but because it's not a big deal or large program
rem I didn't change this.
temp = BClicked(highlighted, mclicked)
rem this next line is where I would run into problems if I used any other language
rem since Basic lets you index arrays differently, the array(0) index equals 1
rem if nothing is clicked, but since that is never displayed anyway, it saves some
rem time checking for problems when nothing is being clicked.
SButtonClicked(temp) = 1
rem call this function to get all your buttons to display.
rem Also the parameter highlighted must be assigned AFTER BReset() otherwise
rem it won't work right!
BDisplay(highlighted)
sync
loop
Also note, if you want to display images and get them to work right, you have to manually enter in the Image Width and Image Height in the BMakeButtonPictureBox() function. Image Width() functions don't work that well before the image is loaded...
TUTORIAL!
Copy and paste all the functions and variables into your code!
Before the Main Loop, or anytime you wish to create a button, call BMakeButtonBox() and type in it's x, y, width, height parameters.
Within the Main loop call BReset() before anything to refresh the mouse collisions, otherwise you'll have many highlighted buttons.
mclicked = MouseClick()
highlighted = BCheckMouseInButton()
are two temp variables I added since I called those functions so many times, I just wanted them stored somewhere. You will need to send them to the other functions.
To see which button is being clicked, type
BClicked(highlighted, mclicked)
this will return the number of any button being clicked during that loop. It will return 0 if nothing is being clicked.
So if you had a start button, and it was at 1, you'd type
if BClicked(highlighted, mclicked) = 1 then startGame()
When you want the buttons to show up on screen, which should be before the sync but after everything's tested use:
BDisplay(highlighted)
sync
You send highlighted because BDisplay() would run that test 100 times if you didn't, so it saves processing power.
Also, since most of you aren't noobs, you'll notice I couldn't figure out where to put that to make it work, so it runs a simpler test anyway... but works heh.
Notes:
To make a button inactive, if you don't want it displayed, type BDestroyButton(button_number), and to make it active again use BActivateButton(button_number).
I do not have a BDestroyAll() function, just realized I should add that... but it would need to store which buttons were active before such that you could call 1 function and reactivate your menu, GUI studio does this (and bugs out)...
ENDTUTORIAL
I'll edit my code now to add those and hope you guys enjoy a no-sprite version of this.
Sixty Squares' code is, I think much cleaner but it had syntax in it that didn't load that well on DBP, which is why I reinvented the wheel.
edit: Made the functions!
First add this line to the global dim's at the top!
global dim SButtonStoredActive(BSize) as integer
then add these functions anywhere below main:
rem BDestroyAll will render all GUI buttons inactive, but stores their previous Active parameter
rem in an array used in the BReactivate() function.
function BDestroyAll()
for countB = 1 to BSize
rem without the next test, If you hold the deactivation button down, you'll lose all stored
rem buttons, as they'll all be re-assigned at null. If you call this function once and only once
rem using a click or similar, the if active > 0 test wouldn't be necessary.
if SButtonActive(countB) > 0
SButtonStoredActive(countB) = SButtonActive(countB)
SButtonActive(countB) = 0
endif
next countB
endfunction
rem BReactivate() will restore all previously deactivated buttons with DestroyAll, for redisplaying Menus
function BReactivate()
for countB = 1 to BSize
SButtonActive(countB) = SButtonStoredActive(countB)
next countB
endfunction
I used this in my main loop to test:
if spacekey() then BDestroyAll()
if upkey() then BReactivate()
and voila!
Here's the full thing, edited:
rem button header file, functions without sprites
rem IMPORTANT NOTE! TO USE PROPERLY, ALWAYS PUT BDisplay() JUST BEFORE
REM THE SYNC FUNCTION!
sync on
sync rate 45
remstart
TYPE simpleButton
x as int
y as int
width as int
height as int
rem filename is for the image, if used, for the button
filename as string
rem buttonname is a value in case you want to test strings instead of
rem array indexes.
buttonname as string
rem I'm not using ButtonText but have it in here so you can make simpler
rem ones later.
rem buttontext as string
endtype
remend
rem setting transparent color to white
set image colorkey 255,255,255
global BSize as integer
BSize = 100
rem since this is meant to be used added on to your program
rem you can make it use any starting value for image numbers
rem so it doesn't get in the way of your own image numbers
rem also, needed to index 3 different starting points due to highlighted button
rem images. If you create more than 999 buttons, remember to change
rem these numbers so not to overlap
global ImageNumStartValue as integer
ImageNumStartValue = 3000
global ImageNumStartValueH as integer
ImageNumStartValueH = 4000
global ImageNumStartValueC as integer
ImageNumStartValueC = 5000
rem set this to 0 to turn it off, but it does so for every button lest you edit
rem the BDisplay() command.
global transparencyOn as integer
transparencyOn = 1
rem lastButtonClicked is for telling if, when the mouse button is let-up,
rem which button the user pressed and released.
global LastButtonClicked as integer
LastButtonClicked = 0
rem MouseDown is for testing when the mouse comes up...
global MouseDown as integer
MouseDown = 0
rem the following is because UDT's don't work
rem x y width height, self explanatory
rem active is whether the button shows or not
rem FileName is the image's name string
rem ButtonName is the internal reference, you can leave blank
rem or use for complex systems of buttons later
rem HighFileName is for when the button is highlighted
rem ClickedFileName is for when the button is clicked
rem SButtonHighlighted is whether or not the button is highlighted
rem SButtonClicked is the same for clicked
rem SButtonStoredActive is for destroying all buttons and being able to undestroy them later
rem for menu collapsing and redisplays
global dim SButton(BSize) as integer
global dim SButtonX(BSize) as integer
global dim SButtonY(BSize) as integer
global dim SButtonWidth(BSize) as integer
global dim SButtonHeight(BSize) as integer
global dim SButtonHighlighted(BSize) as integer
global dim SButtonClicked(BSize) as integer
global dim SButtonActive(BSize) as integer
global dim SButtonSimpleFileName(BSize) as string
global dim SButtonHighFileName(BSize) as string
global dim SButtonClickedFileName(BSize) as string
global dim SButtonName(BSize) as string
global dim SButtonStoredActive(BSize) as integer
BMakeButtonBox(1, "h", 0, 200, 300,24, 1)
`BMakeButtonPictureBox(2, "but", "but.bmp", "buthigh.bmp", "butclick.bmp", 30, 250,120,60, 0)
do
rem clear screen
cls
rem Refresh the buttons to 0 before testing for collision...
BReset()
rem these two variables are used a lot, so I'm only running these two functions once per loop
mclicked = MouseClick()
highlighted = BCheckMouseInButton()
rem Temp stores which button is being clicked, 0 if none.
rem You'll notice I leave testing this to be 0 for the display functions,
rem I consider this sloppy, but because it's not a big deal or large program
rem I didn't change this.
temp = BClicked(highlighted, mclicked)
rem this next line is where I would run into problems if I used any other language
rem since Basic lets you index arrays differently, the array(0) index equals 1
rem if nothing is clicked, but since that is never displayed anyway, it saves some
rem time checking for problems when nothing is being clicked.
SButtonClicked(temp) = 1
rem call this function to get all your buttons to display.
rem Also the parameter highlighted must be assigned AFTER BReset() otherwise
rem it won't work right!
BDisplay(highlighted)
sync
if spacekey() then BDestroyAll()
if upkey() then BReactivate()
loop
rem this function takes any active buttons and displays them onscreen
rem transparency is set on for paste image initially
function BDisplay(mouseInside)
for countB = 1 to BSize
if SButtonActive(countB) > 0
if LEN( SButtonSimpleFileName(countB)) < 4
Box SButtonX(countB), SButtonY(countB), SButtonX(countB) + SButtonWidth(countB), SButtonY(countB) + SButtonHeight(countB)
if mouseInside > 0 then Box SButtonX(countB), SButtonY(countB), SButtonX(countB) + SButtonWidth(countB), SButtonY(countB) + SButtonHeight(countB), RGB(0,0,255), RGB(0,0,255), RGB(0,0,255), RGB(0,0,255)
if SButtonClicked(countB) > 0 then Box SButtonX(countB), SButtonY(countB), SButtonX(countB) + SButtonWidth(countB), SButtonY(countB) + SButtonHeight(countB), RGB(250,0,255), RGB(250,0,255), RGB(250,0,255), RGB(50,0,255)
else
if mouseInside then SButtonHighlighted(mouseInside) = 1
rem remember to add AND IF CLICKED <> 0
paste image ImageNumStartValue + countB, SButtonX(countB), SButtonY(countB), transparencyOn
if SButtonHighlighted(countB) > 0 then paste image ImageNumStartValueH + countB, SButtonX(countB), SButtonY(countB), transparencyOn
if SButtonClicked(countB) = 1 then paste image ImageNumStartValueC + countB, SButtonX(countB), SButtonY(countB), transparencyOn
endif
endif
next countB
endfunction
rem BMakeButtonBox takes the indexed array, places it,
rem and if centered is 0 it does not center the box, otherwise
rem nomatter what x value, it will put it in the center
rem this function is for PICTURELESS buttons, for testing mostly
function BMakeButtonBox(ind, name$, x, y, width, height,centered)
if centered > 0 then x = BGetCenterX(width)
SButtonX(ind) = x
SButtonY(ind) = y
SButtonWidth(ind) = width
SButtonHeight(ind) = height
SButtonName(ind) = name$
SButtonActive(ind) = 1
endfunction
rem same as above, but with an image
function BMakeButtonPictureBox(ind, name$, picturefile$, highlighted$, clicked$, x, y, width, height,centered)
if centered > 0 then x = BGetCenterX(width)
SButtonX(ind) = x
SButtonY(ind) = y
SButtonWidth(ind) = width
SButtonHeight(ind) = height
SButtonName(ind) = name$
SButtonSimpleFileName(ind) = picturefile$
SButtonHighFileName(ind) = highlighted$
SButtonClickedFileName(ind) = clicked$
SButtonActive(ind) = 1
Load Image SButtonSimpleFileName(ind), ImageNumStartValue + ind
Load Image SButtonHighFileName(ind), ImageNumStartValueH + ind
Load Image SButtonClickedFileName(ind), ImageNumStartValueC + ind
endfunction
rem BGetCenterX returns the X value to place a button if you want the button
rem to be centered.
function BGetCenterX(buttonwidth)
CenterStartXPosition = Screen Width()/2 - buttonwidth/2
endfunction CenterStartXPosition
function BDestroyButton(ind)
SButtonActive(ind) = 0
endfunction
rem BClicked returns what button was clicked before the next BDisplay() is called
rem I DO NOT set the other parameters to 0 so you can easily turn the button off for a while
rem and when you want it displayed again, you only have to call BActivateButton(), and not
rem reinitialize everything.
function BClicked(onBut, clicked)
retval = 0
if onBut > 0 and clicked > 0 then retval = onBut
endfunction retval
rem this function reactivates the button for display
function BActivateButton(ind)
SButtonActive(ind) = 1
endfunction
rem returns which button the mouse was just released from
rem I could not get this to work properly, and don't need it that much
rem so I scrapped it, but left the logic here in case anyone wants
function BUnClicked()
remstart
if MouseDown > 0
if LastButtonClicked <> 0
if MouseClick() = 0
if LastButtonClicked = BCheckMouseInButton()
message STR$(LastButtonClicked)
MouseDown = 0
LastButtonClicked = 0
endif
endif
endif
endif
remend
endfunction retval
rem This is a function to see if the mouse is inside a button, and which button it is
function BCheckMouseInButton()
inButton = 0
for countB = 1 to BSize
if Mousex() > SButtonX(countB) AND MouseX() < SButtonX(countB) + SButtonWidth(countB) AND MouseY() > SButtonY(countB) AND MouseY() < SButtonY(countB) + SButtonHeight(countB) then inButton = countB
next countB
endfunction inButton
rem this function resets all Clicked and Highlighted values to NULL
rem it does NOT affect the ACTIVE parameter
function BReset()
for countB = 1 to BSize
SButtonClicked(countB) = 0
SButtonHighlighted(countB) = 0
next countB
endfunction
rem BDestroyAll will render all GUI buttons inactive, but stores their previous Active parameter
rem in an array used in the BReactivate() function.
function BDestroyAll()
for countB = 1 to BSize
rem without the next test, If you hold the deactivation button down, you'll lose all stored
rem buttons, as they'll all be re-assigned at null. If you call this function once and only once
rem using a click or similar, the if active > 0 test wouldn't be necessary.
if SButtonActive(countB) > 0
SButtonStoredActive(countB) = SButtonActive(countB)
SButtonActive(countB) = 0
endif
next countB
endfunction
rem BReactivate() will restore all previously deactivated buttons with DestroyAll, for redisplaying Menus
function BReactivate()
for countB = 1 to BSize
SButtonActive(countB) = SButtonStoredActive(countB)
next countB
endfunction
rem ***********************************************************************************************************************************************************************************
SButton stands for SimpleButton... if anyone aks
and have fun!
Signed
------