basic 2D button subroutine with draw, hover and select by dark nooby6th Sep 2008 4:20
|
---|
Summary This routine is designed to be modified and pasted into existing projects but probably needs some further integrating and speed enhancing done to it. Description The first section of the code is only there to show what variables i used. Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com rem this show variables and prerequisites to the subroutine rem rem x = 0 : y = 0 : xscale = 50 : yscale = 50 : rem used for button position and size mousepx = 0 : mousepy = 0 : rem used for mouse pointer position and testing purposes colorflag = 0 : rem not used this time around :( selection = 0 : rem used to store which button was clicked by mouse buttontrace = 0 : rem used with selection variable to store which button was clicked buttonname$ = " " : textwidth = 0 : textheight = 0 : rem used to center text on the button sync on rem main loop used for testing this routine do if buttontrace = 8 then cls rem plot mouse co-ordinates mousepx = mousex() mousepy = mousey() rem display menu buttontrace = 1 :buttonname$ = "10": x = 5 : y = 300 : gosub drawbutton buttontrace = 2 :buttonname$ = "2": x = 5 : y = 375 : gosub drawbutton buttontrace = 3 :buttonname$ = "300": x = 110 : y = 300 : gosub drawbutton buttontrace = 4 :buttonname$ = "4": x = 110 : y = 375 : gosub drawbutton buttontrace = 5 :buttonname$ = "5": x = 210 : y = 300 : gosub drawbutton buttontrace = 6 :buttonname$ = "6": x = 210 : y = 375 : gosub drawbutton buttontrace = 7 :buttonname$ = "7": x = 310 : y = 300 : gosub drawbutton buttontrace = 8 :buttonname$ = "8000": x = 310 : y = 375 : gosub drawbutton set cursor 0,0 : ink rgb(250,250,250),0 print "x = ";mousepx;" y = ";mousepy sync if escapekey() = 1 then end loop rem ========================================================================= rem this is the actual routine!!!!!!!! rem ========================================================================= rem draw button then check for hover and mouseclick drawbutton: textwidth = text width(buttonname$) textwidth = int(xscale/2)-(textwidth/2) textheight = text height(buttonname$) textheight = int(yscale/2)-(textheight/2) if mousepx > x and mousepy > y and mousepx < x+xscale and mousepy < y+yscale ink rgb(0,250,0),0 box x,y,x + xscale, y + yscale ink 0,rgb(0,250,0) text x + textwidth ,y + textheight,buttonname$ if mouseclick() = 1 ink rgb(250,0,0),0 box x,y,x + xscale, y + yscale ink 0,rgb(250,0,0) text x + textwidth ,y + textheight,buttonname$ selection = buttontrace endif else ink rgb(250,250,250),0 box x,y,x + xscale, y + yscale ink 0,rgb(250,250,250) text x + textwidth ,y + textheight,buttonname$ endif return |