@lucifer 1101:
Welcome back! Pretty cool medialess button, but I noticed a few things. First off, and this is just a tiny, meaningless thing, you define variables for color values that will never change. I suggest you use the
#constant keyword. Secondly, you use two global variables to determine when the mouse has been released and to determine which button has been pressed. This is fine, but I also see that you have functions returning these values. If it's global, it doesn't need to be returned, as any other function that needs the result of these functions can use it already. Finally, I noticed that in your MouseRelease() function, you use the
wait command. While the program is in the main loop with other things that could be happening in the meantime, it's never a good idea to pause execution of anything else while it just waits. I've modified your code and implemented a GUI flag system to control all of this. Here it is:
set display mode 800,600,32,1
sync on
#constant Gray 0xFF969696 `Since these values will never change, why not just use constants?
#constant DarkGray 0xFF5A5A5A `And by putting them in hex, we don't need to call rgb()
#constant White 0xFFFFFFFF
#constant Blue128 0xFF000080
#constant Red255 0xFFFF0000
global GUIFlags as dword `Holds 32 flags (indexed 0-31)
do
cls
`Normal buttons
Button(0,10,10,300,100,Gray,DarkGray,Blue128,"Button 1",Red255,50)
Button(1,320,10,300,100,Gray,DarkGray,Blue128,"Button 2",Red255,50)
Button(2,630,10,160,100,Gray,DarkGray,Blue128,"3",Red255,50)
text 10,120,str$(FlagSet(0))
text 320,120,str$(FlagSet(1))
text 630,120,str$(FlagSet(2))
`Button with trappable release using two flags
ClearFlag(4) `Clear the release flag
if FlagSet(3) then SetFlag(4) `If the button is already pressed, set the release flag
Button(3,10,250,300,100,Gray,DarkGray,Blue128,"Click Me",Red255,50)
if FlagSet(4)=1 and FlagSet(3)=1 then ClearFlag(4) `If the button used to be pressed and is still pressed, clear the release flag; the button was not released.
if FlagSet(3) then text 10,360,"Button Pressed" `If the button flag is set, do something
if FlagSet(4) then text 10,360+text height("|"),"Button Released" `If the button release flag is set, do something else
`Simple exit
Button(5,10,490,300,100,Gray,DarkGray,Blue128,"Exit",Red255,50)
if FlagSet(5) then exit
sync
loop
end
`The next three functions are simple getters/setters for GUIFlags
function SetFlag(Number) `Sets bit at Number
GUIFlags=GUIFlags||(1<<Number)
endfunction
function ClearFlag(Number) `Clears bit at Number
GUIFlags=GUIFlags&&(not (1<<Number))
endfunction
function FlagSet(Number) `Returns bit at Number (0 or 1)
Flag as dword
Flag=GUIFlags&&(1<<Number)
if Flag then exitfunction 1
endfunction 0
function Button(Flag,XPos,YPos,Width,Height,Color1 as dword,Color2 as dword,Hover as dword,Label$,LabelColor as dword,LabelSize)
ClearFlag(Flag)
if mousex()>=XPos and mousey()>=YPos and mousex()<XPos+Width and mousey()<YPos+Height
Color1=Hover
Color2=Hover
if mouseclick()=1 then SetFlag(Flag)
endif
box XPos,YPos,XPos+Width,YPos+Height,Color2,Color1,Color2,Color1
ink LabelColor,0
set text size LabelSize
center text XPos+Width/2,YPos+Height/2-text height(Label$)/2,Label$
endfunction
I hope I don't sound harsh... I say what I do in the interest of group learning. I feel that's important for people to understand with any criticism.
In my system, I use a dword variable called GUIFlags to store up to 32 flags, one in each bit. I wrote three little functions to get/set/clear these bits so people don't have to mess around with the masking and whatnot. If anyone has
any questions about
anything I did, please ask.
Edit: Some typos in the code. All fixed now.