This is from a project that I dumped. I then bought the BlueGUI v2 plugin, and I'm glad I did so, because its amazing
. In the project I was working I had code finished for checkboxs, labels, textboxs, and buttons. This the code for buttons.
mini descriptions and notes
'x' is the button's x position
'y' is the button's y position
'width' is the button's width
'height' is the button's height
'image' (optional) is the image number to use for the button(use Load Image" command first)
'lineColor' is the rgb value of the button's border(only if no image value is supplied)
'fillColor' is the rgb value of inside the button(only if no image value is supplied)
'fontColor' is the rgb value of the caption
'textXCenter' a value of 0 means not to center the text on the x-axis, 1 means to center it
'textYCenter' a value of 0 means not to center the text on the y-axis, 1 means to center it
'fontName' the font to use for the caption
'fontSize' the font size to use for the caption
'fontStyle' the font style of the caption(0 for normal, 1 for italic, 2 for bold and 3 for bold italic)
'caption' the text inside the button
'toggle' a value of either 0 or 1(use for whatever you want)
'enabled' if set to 0 the button will not detect any clicks, 1 it will
'visible' if set to 0 the button will not be visible, 1 it will
Button_show(id) shows a specific button on the screen according to properties(only if visible is 1)
Button_showAll() shows all buttons on the screen according to their properties(only if visible is 1)
Button_over(id) returns a 1 if the mouse cursor is over the button
Button_leftClick(id) returns a 1 if the button is left-clicked(only if enabled is 1)
Button_rightClick(id) returns a 1 if the button is right-clicked(only if enabled is 1)
Button_mashClick(id) returns a 1 if the button is both left and right-clicked(only if enabled is 1)
Button_getClick(id) returns the current click value of a button(1 is left-click, 2 is right-click, 3 is mash click)
Button_drag(id, showThis, showAll) centers button on current mouse cursor position. Value of 1 for 'showThis' means Button_show(this). Value of 1 for 'showAll) means Buton_showAll()
Button_toggle(id) toggles a button's toggle property. If Button(this).toggle = 1 then Button(this).toggle = 0, etc.
Button_setApperance(id, lineColor, fillColor, fontColor, fontName$, fontSize, fontSyle) sets all of the apperance properties of a specific button
Button_setApperance(lineColor, fillColor, fontColor, fontName$, fontSize, fontStyle) sets all of the apperance properties of all buttons
Button_autoSize(id) adjusts a button 'width' and 'height' according to either 'image' size or 'caption' size
Button_autoSizeAll() adjusts every button according to either their 'image' size or 'caption' size
NOTES:
-You must use the 'dim' command to set the functions up(see example)
-All of a button's properties are set to 0 to start with, make sure you change what you need to
-A button will not detect clicks when 'enabled' is set to 0
-A button will not show up on screen when 'visible' is set to 0
-A button only appears when you call either the 'Button_show' or 'Button_showAll' functions
-If a image number is not supplied, the button will be displayed according to 'lineColor' and 'fillColor'
Example (be sure to include the functions in your project!)
rem load image for button(supply your own)
Load Image "pathfile.bmp", 1, 1
dim Button(2) AS Button_Type : rem sets up two buttons ONLY
rem Button with no 'image' value
Button(1).x = 100 : Button(1).y = 100
Button(1).lineColor = rgb(255, 0, 0) : Button(1).fillColor = rgb(0, 0, 255)
Button(1).textXCenter = 1 : Button(1).textYCenter = 1
Button(1).fontName = "Courier" : Button(1).fontSize = 12
Button(1).enabled = 1 : Button(1).visible = 1
Button(1).caption = "Button 1"
rem Button with 'image' value
Button(2).x = 300 : Button(2).y = 100
Button(2).image = 1 : Button(2).fontColor = Rgb(255, 0, 0)
Button(2).enabled = 1 : Button(2).visible = 1
Button_autoSizeAll(1, 1) : rem both width and height for all buttons
rem Main Loop
do
cls
Button_showAll() : Text 10, 10, "Press 'esc' key to quit"
if Button_leftClick(1) = 1 then center text 320, 240, "button 1, left-clicked" : wait 500
if Button_leftClick(2) = 1 then center text 320, 240, "button 2, left-clicked" : wait 500
loop
This is the actual button code (just copy and paste into an include file)
GLOBAL glb_fontColor
Type Button_Type
x As Word
y As Word
width As Byte
height As Byte
image As Word
lineColor As Integer
fillColor As Integer
fontColor As Integer
textXCenter As Boolean
textYCenter As Boolean
fontName As String
fontSize As Byte
fontStyle As Byte
caption As String
toggle As Boolean
enabled As Boolean
visible As Boolean
Endtype
Function Button_show(id)
If Button(id).visible = 1
If Button(id).image > 0
If Image Exist(Button(id).image) = 1
Paste Image Button(id).image, Button(id).x, Button(id).y
oldTextFont$ = Text Font$() : oldTextSize = Text Size() : oldTextStyle = Text Style() : oldfontColor = glb_fontColor
SetText(Button(id).fontName, Button(id).fontSize, Button(id).fontStyle, Button(id).fontColor)
If Button(id).textXCenter = 1
x = (Button(id).x + (Button(id).width / 2))
Else
x = Button(id).x
Endif
If Button(id).textYCenter = 1
y = (Button(id).y + (Button(id).height / 2))
Else
y = Button(id).y
Endif
OutputText(x, y, Button(id).textXCenter, Button(id).textYCenter, Button(id).caption)
Else
error = 1
Endif
Else
Ink Button(id).fillColor,0
Box Button(id).x, Button(id).y, (Button(id).x + Button(id).width), (Button(id).y + Button(id).height)
Ink Button(id).lineColor,0
HollowBox(Button(id).x, Button(id).y, (Button(id).x + Button(id).width), (Button(id).y + Button(id).height))
oldTextFont$ = Text Font$() : oldTextSize = Text Size() : oldTextStyle = Text Style() : oldfontColor = glb_fontColor
SetText(Button(id).fontName, Button(id).fontSize, Button(id).fontStyle, Button(id).fontColor)
If Button(id).textXCenter = 1
x = (Button(id).x + (Button(id).width / 2))
Else
x = Button(id).x
Endif
If Button(id).textYCenter = 1
y = (Button(id).y + (Button(id).height / 2))
Else
y = Button(id).y
Endif
OutputText(x, y, Button(id).textXCenter, Button(id).textYCenter, Button(id).caption)
SetText(oldTextFont$, oldTextSize, oldTextStyle, oldfontColor)
Endif
Else
error = 1
Endif
Endfunction error
Function Button_showAll()
For id = 0 To Array Count(Button(0))
error = Button_show(id)
Next id
Endfunction error
Function Button_over(id)
If Mousex() => Button(id).x and Mousey() => Button(id).y and Button(id).enabled = 1
If Mousex() =< (Button(id).x + Button(id).width) and Mousey() =< (Button(id).y + Button(id).height)
overbutton = 1
Else
overbutton = 0
Endif
Endif
Endfunction overbutton
Function Button_leftClick(id)
If Button_over(id) = 1 and Mouseclick() = 1 and Button(id).enabled = 1
buttonleftclick = 1
Else
buttonleftclick = 0
Endif
Endfunction buttonleftclick
Function Button_rightClick(id)
If Button_over(id) = 1 and Mouseclick() = 1 and Button(id).enabled = 1
buttonrightclick = 1
Else
buttonrightclick = 0
Endif
Endfunction buttonrightclick
Function Button_mashClick(id)
If Button_over(id) = 1 and Mouseclick() = 3 and Button(id).enabled = 1
buttonmashclick = 1
Else
buttonmashclick = 0
Endif
Endfunction buttonmashclick
Function Button_getClick(id)
If Button_over(id) = 1 and Button(id).enabled = 1
buttongetclick = Mouseclick()
Else
Buttongetclick = -1
Endif
Endfunction buttongetclick
Function Button_drag(id, showThis, showAll)
x = (Mousex() - (Button(id).width / 2)) : If x > 0 and (x + Button(id).width) < Screen Width() Then Button(id).x = x
y = (Mousey() - (Button(id).height / 2)) : If y > 0 and (y + Button(id).height) < Screen Height() Then Button(id).y = y
Position Mouse ((Button(id).x + 1) + (Button(id).width / 2)), ((Button(id).y + 1) + (Button(id).height / 2))
If Button(id).visible = 1 and showThis = 1 Or showAll = 1
If showThis = 1 Then Button_show(id)
If showAll = 1 Then Button_showAll()
Else
error = 1
Endif
Endfunction error
Function Button_toggle(id)
If Button(id).toggle = 0
Button(id).toggle = 1
Else
Button(id).toggle = 0
Endif
Endfunction
Function Button_setApperance(id, lineColor, fillColor, fontColor, fontName$, fontSize, fontStyle)
Button(id).lineColor = lineColor
Button(id).fillColor = fillColor
Button(id).fontColor = fontColor
Button(id).fontName = fontName$
Button(id).fontSize = fontSize
Button(id).fontStyle = fontStyle
Endfunction
Function Button_setApperanceAll(lineColor, fillColor, fontColor, fontName$, fontSize, fontStyle)
For id = 0 To 255
Button_setApperance(id, lineColor, fillColor, fontColor, fontName$, fontSize, fontStyle)
Next id
Endfunction
Function Button_autoSize(id, autoWidth, autoHeight)
If autoWidth = 1
If Button(id).image > 0
If Image Exist(Button(id).image) = 1
Button(id).width = GetImageWidth(Button(id).image)
Else
error = 1
Endif
Else
If Len(Button(id).caption) > 0
oldTextFont$ = Text Font$() : oldTextSize = Text Size() : oldTextStyle = Text Style() : oldfontColor = glb_fontColor
SetText(Button(id).fontName, Button(id).fontSize, Button(id).fontStyle, Button(id).fontColor)
Button(id).width = Text Width(Button(id).caption)
SetText(oldTextFont$, oldTextSize, oldTextStyle, oldfontColor)
Else
error = 1
Endif
Endif
Endif
If autoHeight = 1
If Button(id).image > 0
If Image Exist(Button(id).image) = 1
Button(id).height = GetImageHeight(Button(id).image)
Else
error = 1
Endif
Else
If Len(Button(id).caption) > 0
oldTextFont$ = Text Font$() : oldTextSize = Text Size() : oldTextStyle = Text Style() : oldfontColor = glb_fontColor
SetText(Button(id).fontName, Button(id).fontSize, Button(id).fontStyle, Button(id).fontColor)
Button(id).height = Text Height(Button(id).caption)
SetText(oldTextFont$, oldTextSize, oldTextStyle, oldfontColor)
Else
error = 1
Endif
Endif
Endif
Endfunction
Function Button_autoSizeAll(autoWidth , autoHeight)
For id = 0 To Array Count(Button(0))
Button_autoSize(id, autoWidth, autoHeight)
Next id
Endfunction
rem **************************--------------------------------******************
rem --------------------------EXTRA FUNCTIONS*****************------------------
rem **************************--------------------------------******************
Function HollowBox(x1, y1, x2, y2)
Line x1 ,y1, x1, y2 : Line x1, y2, x2, y2
Line x2, y2, x2, y1 : Line x2, y1, x1, y1
Endfunction
Function SetTextToStyle(fontstyle)
Select fontStyle
Case 1: Set Text To Italic : Endcase
Case 2: Set Text To Bold : Endcase
Case 3: Set Text To BoldItalic : Endcase
Case Default: Set Text To Normal : Endcase
Endselect
Endfunction
Function SetText(fontName$, fontSize, fontStyle, inkF)
Set Text Font fontName$
Set Text Size fontSize
SetTextToStyle(fontStyle)
glb_fontColor = inkF
Endfunction
Function GetInkF()
oldInkF = Point(1, 1)
Dot 1, 1
newInkF = Point(1, 1)
Ink oldInkF, 0 : Dot 1, 1
Endfunction newInkF
Function OutputText(x, y, xCenter, yCenter, caption$)
oldInkF = GetInkF()
Ink glb_fontColor, 0
If yCenter = 1 Then y = (y - (Text Height("T") / 2))
If xCenter = 1
Center Text x, y, caption$
Else
Text x, y, caption$
Endif
Ink oldInkF, 0
Endfunction
Function GetImageWidth(imageNum)
memblockNum = FindFreeMemblock() : If memblockNum = -1 Then Exitfunction -1
Make Memblock From Image memblockNum, imageNum
imageWidth = Memblock Dword(memblockNum, 0)
Delete Memblock memblockNum
Endfunction imageWidth
Function GetImageHeight(imageNum)
memblockNum = FindFreeMemblock() : If memblockNum = -1 Then Exitfunction -1
Make Memblock From Image memblockNum, imageNum
imageHeight = Memblock Dword(memblockNum, 4)
Delete Memblock memblockNum
Endfunction imageHeight
Function GetImageDepth(imageNum)
memblockNum = FindFreeMemblock() : If memblockNum = -1 Then Exitfunction -1
Make Memblock From Image memblockNum, imageNum
imageDepth = Memblock Dword(memblockNum, 8)
Delete Memblock memblockNum
Endfunction imageDepth
Function FindFreeMemblock()
For memblockNum = 1 To 32
If Memblock Exist(memblockNum) = 0 Then Exit
If memblockNum = 32 Then memblockNum = -1 : Exit
Next memblockNum
Endfunction memblockNum
The extra functions are needed to run, this is because this code was meant to run with checkboxs, textboxes, etc. If you have any questions, let me know either by posting or Underworld1020@gmail.com
Enjoy
EDIT: I also have some more "complete" code from way back when at:
http://forum.thegamecreators.com/?m=forum_view&t=60281&b=6
and other stuff can be found at my site:
http://www.freewebs.com/underworld1020/functionsets.htm