button

A simple button api with the option of drawing an image on the button.

new(x, y, width, height, function [, image [, enabled]])Create a button, and add it to the internal table of buttons.
delete(id)Remove a button from being clicked.
enable(id, enable)Enable or disable a button.
exec(event [, drag [, monitor]])Takes an event in a table, checks if it's a mouse_click or mouse_drag, and sees if it's within a button, if so, execute its function.
drawButton(id)Draw a button again, drawing its tDraw, or nothing if there is no image.
drawButtons()Draw every button, this loops through the buttons table.
edit(id [, x [, y [, width [, height [, func [, image]]]]]])Edit a button, changing its function, position, or whatnot.
run(drag, touch)Run buttons forever.
new(x, y, width, height, function [, image [, enabled]])Source

Create a button, and add it to the internal table of buttons.

Parameters

  1. x number X coordinate of the button.
  2. y number Y coordinate of the button.
  3. width number Width of the button.
  4. height number Height of the button.
  5. function function The function to run when the button is clicked.
  6. image? table Table of blit lines to draw on the button.
  7. enabled? boolean Whether or not the button is active. Defaults to true.

Returns

  1. string id id of the button

Usage

  • local button = require("libs.button")
    local id = button.new(1,1,5,5,function()
      print("Hi!")
    end)
    
  • Create a button with an image

    local button = require("libs.button")
    local image = {
      {
        "+-----+",
        "0000000",
        "fffffff",
      },
      {
        "|Image|",
        "0000000",
        "fffffff",
      }
      {
        "+-----+",
        "0000000",
        "fffffff",
      },
    }
    local id = button.new(1,1,7,3,function() print("Clicked!") end,image) -- Note the `image` parameter passed here.
    
delete(id)Source

Remove a button from being clicked.

Parameters

  1. id string button id to remove.
enable(id, enable)Source

Enable or disable a button.

Parameters

  1. id string Button to enable or disable.
  2. enable boolean Whether the button is enabled or not.
exec(event [, drag [, monitor]])Source

Takes an event in a table, checks if it's a mouse_click or mouse_drag, and sees if it's within a button, if so, execute its function.

Parameters

  1. event table Event table to check for mouse_click or mouse_drag.
  2. drag? boolean Enable button trigger on a mouse_drag event. Defaults to false.
  3. monitor? boolean Enable button trigger on a monitor_touch event. Defaults to false.
drawButton(id)Source

Draw a button again, drawing its tDraw, or nothing if there is no image.

Parameters

  1. id string Button ID to draw image of.
drawButtons()Source

Draw every button, this loops through the buttons table.

edit(id [, x [, y [, width [, height [, func [, image]]]]]])Source

Edit a button, changing its function, position, or whatnot.

Parameters

  1. id string ID of the button to change.
  2. x? number X coordinate of the button.
  3. y? number Y coordinate of the button.
  4. width? number Width of the button.
  5. height? number Height of the button.
  6. func? function Function to execute when the button is clicked.
  7. image? table Table of blit lines to draw where the button is.
run(drag, touch)Source

Run buttons forever. Useful in parallel.

Parameters

  1. drag boolean Whether or not dragging triggers buttons.
  2. touch boolean Whether or not monitor touches triggers buttons

Returns

  1. function A runnable function that runs buttons forever.

Usage

  • parallel.run(button.run(false,true))