Skip to content

Button Object (Cobalt UI)

ebernerd edited this page Aug 14, 2016 · 4 revisions

Button Object

The button object is, well... a button. It can have varying widths and heights (which can be percentages, i.e. 50%) and actions.

This object requires cobalt.mousepressed( x, y, button ) and cobalt.mousereleased( x, y, button ) to be defined, with Cobalt UI's callbacks inserted.

Attributes

  • x - the x position, relative to the parent
  • y - the y position, relative to the parent
  • w - the width of the object
  • h - the height of the object
  • text - the text to display on the button
  • backColour - the background colour of the object
  • foreColour - the colour of the text
  • marginleft - the left margin, relative to the parent
  • margintop - the top margin, relative to the parent
  • wrap - tells the object how to wrap (left, center) *Note - as of Cobalt v1.0, this only works on the x axis)
  • state - determines if the object should draw, depending on cobalt.state. Useful for multi-screen apps. Default is its parent's state.

Methods

  • :resize( w, h ) - tells the button to resize. Using Button.w or Button.h to resize may cause unexpected results.
  • :setMargins( top, left ) - sets the margins
  • .onclick() - defined by the user to tell the object what to do when it is clicked.

Example

This example will output a simple button that changes colour when you press it.

local cobalt = dofile( "cobalt" )
cobalt.ui = dofile( "cobalt-ui/init.lua" )

local Panel = cobalt.ui.new({ w = "50%", marginleft="25%", h = "50%", margintop = "25%" })
local Button = Panel:add("button", { wrap="center", y = 4})
Button.onclick = function()
	Button.backColour = 2 ^ math.random( 1, 15 )
end

function cobalt.draw()
  cobalt.ui.draw()
end

function cobalt.mousepressed( x, y, button )
	cobalt.ui.mousepressed( x, y, button )
end

function cobalt.mousereleased( x, y, button )
	cobalt.ui.mousereleased( x, y, button )
end

cobalt.initLoop()

Result: result

Clone this wiki locally