Dynamic Button generator with an event handler by jestermon29th Nov 2011 11:16
|
---|
Summary Dynamic Button generator with an event handler This little program builds buttons on the fly, without using any images Description A sample program that generates buttons on the fly, without using images. Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com // DarkBasicPro // Dynamic Button generator with an event handler // This little program builds buttons on the fly, without using any images // It is an upgrade to my previous dynamic buttons function. // You call the _NewButton() function to generate tha buttons for you. // The _DisplayButtons() fuction displays the buttons you have made // The _ButtonEvents()function checks for a mouse click on a button // // _NewButton() Parameters // x The screen x location to place the button // y The screen y loation to place the button // width The width of the button in pixels // height The height of the button in pixels // r The Red base RGB color for the button // g The Green base RGB color for the button // b The Blue base RGB color for the button // txt$ The Text to appear on the button // // How to use // Call the _NewButton fuvtion for every button you wish to make // Call the _DisplayButtons function inside the 'do' loop to display buttons // Call the _ButtonEvents function inside the 'do' loop to get info // The _ButtonEvents returns the actual text that you placed on the button // Check the returned button text to call anything you wish in your program // // Author: jestermon@live.com SYNC ON TYPE _dynabutton _sprite AS INTEGER _image AS INTEGER _txt AS STRING _x AS INTEGER _y AS INTEGER ENDTYPE DIM _Button() AS _dynabutton DIM _MousePointer(1) AS _dynabutton _NewButton(80, 65, 180,22,50,0,0,"SIMPLE DEMO MENU") _NewButton(100,100,140,25,0,0,150,"START") _NewButton(100,130,140,25,0,0,150,"OPTIONS") _NewButton(100,160,140,25,0,0,150,"VIDEO") _NewButton(100,190,140,25,0,0,150,"SOUND") _NewButton(100,220,140,25,0,0,150,"QUIT") DO _DisplayButtons() `must be called _command$ = _ButtonEvents() `must be called //Do your own processing if the _command$ matches the text for your button IF _command$ = "START" THEN TEXT 100,420,"The " + _command$ +" button has been selected" IF _command$ = "VIDEO" THEN TEXT 100,420,"Yeah! you selected " + _command$ TEXT 100,400,_command$ SYNC LOOP END FUNCTION _ButtonEvents() _mouse = MOUSECLICK() id = SPRITE COLLISION (9999,0) IF id <> 0 AND _mouse = 1 count = ARRAY COUNT (_Button()) FOR i = 0 TO count IF id = _Button(i)._sprite txt$ = _Button(i)._txt EXITFUNCTION txt$ ENDIF NEXT i ENDIF ENDFUNCTION "" FUNCTION _DisplayButtons() count = ARRAY COUNT (_Button()) FOR i = 0 TO count SPRITE _Button(i)._sprite,_Button(i)._x,_Button(i)._y,_Button(i)._image NEXT i SPRITE 9999,mousex(),mousey(),9999 ENDFUNCTION FUNCTION _NewButton(x,y,width,height,r,g,b,txt$) IF _MousePointer(0)._txt <> "ready" _CreateMousePointer() ENDIF count = ARRAY COUNT (_Button()) idi = 9000 + count //Image ID ids = 9050 + count //sprite ID ARRAY INSERT AT BOTTOM _Button(0) _CreateButton(ids,idi,width,height,r,g,b,txt$) _Button()._txt = txt$ _Button()._image = idi _Button()._sprite = ids _Button()._x = x _Button()._y = y ENDFUNCTION FUNCTION _CreateMousePointer() CREATE BITMAP 31,1,1 DOT 0,0 ,RGB(0,0,0) GET IMAGE 9999,0,0,1,1 SET CURRENT BITMAP 0 DELETE BITMAP 31 _MousePointer(0)._txt = "ready" ENDFUNCTION FUNCTION _CreateButton(spriteNo,imageNo,width,height,inkR,inkG,inkB,buttonText$) INK RGB(inkR,inkG,inkB),0 left = 0 top = 0 CREATE BITMAP 31,width,height FOR i = 0 TO 180 r = height/2 x = SIN(i)*r y = COS(i)*r x1 = left + r - x x2 = left + width - r + x y = top + y + r INC inkR IF inkR > 255 THEN inkR = 255 INC inkG IF inkG > 255 THEN inkG = 255 INC inkB IF inkB > 255 THEN inkB = 255 INK RGB(inkR,inkG,inkB),0 LINE x1,y,x2,y NEXT i SET TEXT FONT "arial" SET TEXT SIZE height - 5 INK RGB(0,0,0),0 SET TEXT TO BOLD sz = TEXT SIZE() txtTop = top + (height - sz)/2 +1 BLUR BITMAP 31,3 INK RGB(255,255,255),0 CENTER TEXT width/2,txtTop,buttonText$ GET IMAGE imageNo,0,0,width,height SET CURRENT BITMAP 0 DELETE BITMAP 31 SPRITE spriteNo,-1000,-1001,imageNo ENDFUNCTION |