Simple Menu Button / Toolbar Button Code by Mobiius6th Aug 2007 11:59
|
---|
Summary This code creates some simple random buttons, then checks if you click on one. It only signals a clicked button when you release the mouse button. this can be used as toolbar butto Description Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com Global MaxButtons = 9: ` Change this for more buttons. Remember arrays start at 0, so 0 = 1 button! Global CurrentAction: ` This holds the return value from CheckButtons. Doesn't need to be global if only used ` in the main program loop. Global LeftMouseUp: ` This holds a 1 if the left mouse buttons has been clicked Global RightMouseUp: ` This holds a 1 if the right mouse buttons has been clicked Global LeftMouseClick: ` This holds a 1 if the left mouse buttons is down Global RightMouseClick:` This holds a 1 if the right mouse buttons is down Type tButton XPos As Integer YPos As Integer Width As Integer Height As Integer `Image As Integer EndType Dim Button(MaxButtons) As tButton `This holds the data needed to create a button. I'm only drawing a box here but you could uncomment the last `variable and load an image of a button to use that instead. `Set up some random buttons. For Id = 0 To MaxButtons Button(Id).XPos = Int(Rnd(Screen Width() - 25)) Button(Id).YPos = Int(Rnd(Screen Height() - 25)) Button(Id).Width = 25 Button(Id).Height = 25 Next Id Do: `Main program loop CheckMouseButtons():` Check for mouse button clicks. CurrentAction = CheckButtons():` Check which button has been clicked. If CurrentAction <> -1 Then Exit prompt "Button " + Str$(CurrentAction) + " Clicked.", "Buttons": End Sync Loop `======================================================================================================================= `======================================================================================================================= `Check Button Clicks =================================================================================================== Function CheckButtons() For Id = 0 To MaxButtons: `Run through all buttons Ink Rgb(255, 255, 255), 0 Box Button(Id).XPos, Button(Id).YPos, Button(Id).XPos + Button(Id).Width, Button(Id).YPos + Button(Id).Height Ink Rgb(10, 10, 10), 0 Text Button(Id).XPos + 6, Button(Id).YPos + 6, Str$(Id) If LeftMouseUp: `If You click and release the left mouse button `Check mouse is over a button If MouseX() >= Button(Id).XPos And MouseX() <= (Button(Id).XPos + Button(Id).Width) And MouseY() >= Button(Id).YPos And MouseY() <= (Button(Id).YPos + Button(Id).Height) ExitFunction Id EndIf EndIf Next Id EndFunction -1 `======================================================================================================================= `======================================================================================================================= `Check For Mouse Clicks ================================================================================================ Function CheckMouseButtons() LeftMouseUp = 0: RightMouseUp = 0 If MouseClick() = 1 Then LeftMouseClick = 1 If MouseClick() = 2 Then RightMouseClick = 1 If MouseClick() = 0 If LeftMouseClick = 1 Then LeftMouseUp = 1: LeftMouseClick = 0 If RightMouseClick = 1 Then RightMouseUp = 1: RightMouseClick = 0 EndIf EndFunction `======================================================================================================================= `======================================================================================================================= `======================================================================================================================= |