November 25, 2020, 07:53:38 AM

Author Topic: [bb] Simple buttons by Pongo [ 1+ years ago ]  (Read 535 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Simple buttons by Pongo [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Simple buttons
Author : Pongo
Posted : 1+ years ago

Description : This code will let you create buttons purely from code or load an image button and respond to button presses. This is merely set up as a very simple way to make these without resorting to a GUI lib.

Code :
Code: BlitzBasic
  1. Graphics 640,480,0,2
  2. SetBuffer BackBuffer()
  3.  
  4. Global mx,my,info$
  5. Global buttonheld,ID_clicked
  6.  
  7. Type button
  8.         Field image$    ;image containing button graphics
  9.         Field x                         ;x position of button
  10.         Field y                         ;y pos of button
  11.         Field width             ;width of button
  12.         Field height            ;height of button
  13.         Field id                        ;so we know which button has been pressed
  14.         Field state             ;mouse over/up/down
  15. End Type
  16.  
  17. ; create text button examples
  18. ;Create a button like this: CreateTextButton.button (Label$,    x,y,    width,height,   id,     centered = False)
  19. b1.button = CreateTextButton ("Test A",         50,50100,20,  1,     0)
  20. b2.button = CreateTextButton ("Test B", 50,80100,20,  2,     0)
  21. ;if you use the centered option, the button will x position will be overidden and the button will center on the screen vertical axis
  22. centered.button = CreateTextButton ("Centered long type",50,80,150,20, 3,1)
  23.  
  24. ;##     Create an image button like this:  CreateImageButton.button (image$,x,y,id,centered = False)
  25. ;##     buttonimage = LoadAnimImage ("buttontest1.png",128,32,0,3)
  26. ;##     b.button = CreateImageButton (buttonimage,      50,250,5)
  27.  
  28. ; create image button example. You will need to supply your own image with 3 frames,... normal, hit and rollover)
  29. ;Create an image button like this:  CreateImageButton.button (image,x,y,id,centered = False)
  30.  
  31.  
  32. ;b.button = CreateImageButton (LoadAnimImage ("buttontest1.png",128,32,0,3),    50,250, 5)
  33.  
  34.  
  35. While Not KeyHit(1)
  36.         mx = MouseX () : my = MouseY() ; grab the mouse position at the top of the loop
  37.  
  38.         updatebuttons() ; check rollover state and draw buttons to screen. Call this every loop.
  39.         buttonaction() ;this will execute the results of any button presses.
  40.  
  41.         Text 50,420,Info$
  42.  
  43.         If KeyDown(57)
  44.                 ;draw a cursor crosshair on the screen with the mouse coords
  45.                 Line mx,0,mx,GraphicsHeight() : Line 0,my,GraphicsWidth(),my ; draw crosshair on screen
  46.                 Text 0,0,mx + "," + my
  47.         EndIf
  48.  
  49.         Flip
  50.         Cls
  51. Wend
  52. End
  53.  
  54. Function CreateTextButton.button (Label$,x,y,width,height,id,centered = False)
  55.         b.button = New button
  56.  
  57.         bimage = CreateImage(width,height,3)
  58.  
  59.         ;normal state of button
  60.         SetBuffer ImageBuffer(bimage,0)
  61.         Color 140,140,140
  62.         Rect 0,0,width,height,1 ;fill
  63.         Color 0,255,0
  64.         Rect 0,0,width,height,0 ;outline
  65.         Color 255,255,255
  66.         Text width*.5,height*.5,label$,1,1
  67.  
  68.         ;hit state of button
  69.         SetBuffer ImageBuffer(bimage,1)
  70.         Color 220,220,220
  71.         Rect 0,0,width,height,1 ;fill
  72.         Color 255,0,0
  73.         Rect 0,0,width,height,0 ;outline
  74.         Color 0,0,0
  75.         Text width*.5,height*.5,label$,1,1
  76.  
  77.         ;Rollover state of button
  78.         SetBuffer ImageBuffer(bimage,2)
  79.         Color 180,180,180
  80.         Rect 0,0,width,height,1 ;fill
  81.         Color 0,255,0
  82.         Rect 0,0,width,height,0 ;outline
  83.         Color 255,255,255
  84.         Text width*.5,height*.5,label$,1,1
  85.  
  86.         SetBuffer BackBuffer()
  87.  
  88.         bx = x
  89.         by = y
  90.         bwidth = ImageWidth(bimage)
  91.         bheight = ImageHeight(bimage)
  92.         bid = id
  93.         bstate = 0
  94.  
  95.         If centered Then bx = (GraphicsWidth()*.5) - (bwidth*.5) ;center on screen vertically
  96.  
  97.         Return b
  98. End Function
  99.  
  100. Function CreateImageButton.button (image,x,y,id,centered = False)
  101.         b.button = New button
  102.         bimage = image
  103.         MaskImage bimage,255,0,255 ; mask out pink color
  104.         bx = x
  105.         by = y
  106.         bwidth = ImageWidth(image)
  107.         bheight = ImageHeight(image)
  108.         bid = id
  109.         bstate = 0
  110.  
  111.         If centered Then bx = (GraphicsWidth()*.5) - (bwidth*.5) ;center on screen vertically
  112.  
  113.         Return b
  114. End Function
  115.  
  116. Function updatebuttons()
  117.         For b.button = Each button
  118.                 If RectsOverlap(mx,my,1,1,  bx, by, bwidth, bheight)
  119.                         If MouseDown(1) Then
  120.                                 bstate = 1 ; button is being held down over button
  121.                                 buttonheld = 1
  122.                         Else
  123.                                 bstate = 2 ; mouse is hovering over button with no button down
  124.                                 If buttonheld = 1       ; was the button down last time? If so, mouse has just clicked up.
  125.                                         ID_clicked = bid ;this button has been clicked. set to ID of clicked button
  126.                                         buttonheld = 0  ;reset the button down value
  127.                                 EndIf                                  
  128.                         EndIf
  129.                 Else            ; mouse is not over button
  130.                         bstate = 0      ;normal state of button                        
  131.                 EndIf
  132.                
  133.                 DrawImage       bimage,bx,by,bstate ; draw the button to screen
  134.         Next
  135. End Function
  136.  
  137. Function ButtonAction()
  138.         ;Use this function to take action based on which button has been pressed. Each time you make a button, give it a unique ID.
  139.         ;Then add a new case for that ID here.
  140.         Select ID_clicked
  141.                 Case 1
  142.                         ;this button ID has been clicked. Do stuff here
  143.                         info$ = "Button A has been clicked"
  144.                                                
  145.                 Case 2
  146.                         ;this button ID has been clicked. Do stuff here
  147.                         info$ = "Button B has been clicked"
  148.  
  149.                 Case 3
  150.                         ;this button ID has been clicked. Do stuff here
  151.                         info$ = "Centered button has been clicked"
  152.  
  153.                 Case 5
  154.                         ;this button ID has been clicked. Do stuff here
  155.                         info$ = "Image button has been pressed"
  156.  
  157.         End Select
  158.  
  159.         ID_clicked = -1
  160.  
  161. End Function


Comments :


Nate the Great(Posted 1+ years ago)

 hmmm... could be useful... thanks pongo!


pmc(Posted 1+ years ago)

 Exactly what I was looking for a couple of weeks ago. Thank you. I'll try it out.-pmc


_PJ_(Posted 1+ years ago)

 It's possible that the field ID is unnecessary - assuming that there isn't a requirement for duplicate id's.My reasoning is that as Type objects, the Handle() of each button instance can be used as the IDFurther savings on memory can be achieved by:Instead of maintaining a unique image for each especially if button images are either in multuple usage, or only ever Loaded once, Loading the correct images on creation of the first instance which uses it, but maintaining the image HANDLEs GLOBALLY could make this easier.incidentally, the Field for Image should not be declared as a string.The Position values can be stored as a single INT made of two SHORTS rather than two serparate INTSLikewiser for the Width/Height dimensions.


Yasha(Posted 1+ years ago)

 I don't think using the Handle as the ID really fits with what is going on here, because Handles are assigned at runtime and this is kinda reliant on the ID being known at compile-time so you can set up the Select structure accordingly. You can't rely on Handle returning a predetermined number, only that it will return some number; and therefore can't really rely on finding the right Case from it.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal