ButtonApi
This is a computercraft button api. Coded in OOP fashion.
Creates easy way of managing clickable buttons on advanced monitors in computercraft
- include api in your file by
os.loadAPI("button.lua") - set monitor you want to use:
button.setMonitor(handler) - Create button:
myButton = button.create("My new button") - Set position:
myButton.setPos(1,1) - Set some callback:
myButton.onClick(function() print("CLICK!") end) - Setup loop waiting for input:
while true do button.await(myButton) end
And you're done! Once button will be clicked, "CLICK!" will be printed onto computer's term.
setText(text, resize)- sets button label, if resize=true, recalculates button widthsetAlign(align)- sets label align in button; align can be: "left", "right", "center"setPos(x, y)- sets button's position on screemsetSize(width, height)- sets button's sizesetColor(color)- sets background color (from computercraft colors api)setBlinkColor(color)- sets backgroundColor when button is clickedsetActive(state)- activates/deactivates button; state - booleanonClick(callback)- sets callback executed when button is pressedonClickReturn(value)- sets value that will be returned bybutton.awaitistead of callback function
Adidtionally, all above methods return button object so they can be chained eg:
myButton = button.create().setText("Label").setSize(10,1).setPos(2,2).setColor(colors.red)
button.create(text)- returns new button object;textparameter is optionalbutton.setMonitor(handle)- sets handle to connected advanced monitor. (Handle is peripheral.wrap())button.await(button1, button2, ...)- draws given buttons and awaits for monitor click. After click, ends execution, regardless if click hit any button.
Buttons can be supplied one after:
button.await(button1, button2, button3, ...)
Or in form of a table:
buttonTable = {button1, button2, button3}
button.await(buttonTable)
Or mixed:
buttonTable = {button1, button2, button3}
button.await(buttonTable, button4, button5)
Note: In all examples above button1, button2, etc means button objects
If some button was clicked:
- returns value set by
onClickReturnon clicked button or - executes function set by
onClickmethod on clicked button