Plasmacore:GUI Buttons

From Plasmaworks

Jump to: navigation, search

Overview

  • There are no built-in GUI classes in Plasmacore - you must roll your own.
  • The standard approach is to load a button image, draw it on screen, and use a hit Box to see if the mouse is inside it when clicked.
  • You may wish to wrap the button image and functionality in a custom Button class.

Example: Simple image-based button

 class ButtonTest
   PROPERTIES
     img_click_me("b_click_me.png") : Image
 
   METHODS
     method init
       img_click_me.handle = Handle.center
       img_click_me.position = Display.size / 2
         # This trick won't work if you use the same image
         # for more than one button!
 
     method draw
       img_click_me.draw
 
     method on( MouseEvent e )
       if (e.is_button_press(1))
         if (img_click_me.bounding_box.contains(e.position))
           println( "Button clicked!" )
         endIf
       endIf
 endClass


Example 2: Button wrapper class

 class ButtonTest
   PROPERTIES
     b_click_me("Click Me",Box(20,20,80,30),"button.png") : Button
     b_okay : Button
 
   METHODS
     method init
       b_okay = Button("Okay",Box(20,50,80,30),"button.png") with
           method on_select
             println( "Okay!" )
         endWith
 
     method update
       b_click_me.update
       b_okay.update
 
     method draw
       b_click_me.draw
       b_okay.draw
 
     method on( MouseEvent e )
       if (e.is_button_press(1))
         if (b_click_me.select(e.position)) println( "Button clicked!" )
         b_okay.select(e.position)
       endIf
 endClass
 
 class Button
   PROPERTIES
     title  : String
     image  : Image
     bounds : Box
     flash  : Int32
     font=SystemFont : Font
 
   METHODS
     method init( title, bounds, String filename )
       image = Image(filename)
 
     method on_select
       # Override this or just see if select(Vector2) returns true
 
     method select( Vector2 pos ).Logical
       if (bounds.contains(pos)) flash=128; on_select; return true
       return false
 
     method draw
       image.size = bounds.size
       font.handle = Handle.center
       font.draw( title, bounds )
       image.draw_flash( bounds.position, flash )
         # Draw flash draws the image once normally, then again
         # in translucent solid white from transparent (flash=0)
         # to opaque (flash=255)
 
     method update
       flash = max( 0, flash - 4 )
   endClass
Personal tools