Posted 30 June 2017 - 06:44 AM
YABA
Yet another button API
I didn't personally like any of the currently-implemented button apis out there because they were either way too complicated when all I wanted were simple clickable buttons, or they were too difficult to use. And so, I have created a button api that aims to be as simple and easy to use as possible, without loosing too much functionality.
What can it do?
- Create buttons (duh!)
- Dynamically change existing buttons (including color, text, size, and position)
- If using the window api to house all non-button objects, it can clean itself up when buttons are moved or resized
- Multiple functions can be called from the same button
- Buttons identify themselves to functions they call, allowing hidden information to be stored inside each button
- Yaba provides an object-oriented style to lua, allowing the creation and duplication of a button style using inheritence
- YABA automatically draws buttons for you as their attributes are changed
- Multiple displays are supported through the button.setDisplay(str side) or buttonObj:setDisplay(str side)
- Buttons can be set at active or inactive to allow for multiple pages, etc.
- Buttons support listeners, or the registration and de-registration (using names) of a function subsequently
- Because of the OOP style of this api, inheritance is an option, allowing button styles to be created and then duplicated, reducing the amount of code needed in a program
YABA is available through pastebin at https://pastebin.com/Usr8bRML
pastebin get Usr8bRML button
Documentation:
Spoiler
Format:
buttons = {
button = { --Defines button prototype--
x=int,
y=int,
width=int,
height=int,
color=int color,
textColor=int color,
text=string,
listeners={function,...},
active=boolean,
side=string,
display=Table term (eg. peripheral.wrap("left") or term.native())
},
...
}
Functions:
API:
button.button --original button to be duplicated
button.setDisplay(string side) --Sets default display for all buttons (useful if you don't care about multi-display)
button.draw(win) --Uses win to reassert all content behind buttons, and redraws all buttons
button.act(table event) --Event can be made a table using {os.pullEvent()}.
--This function takes an event table and executes all callbacks on all active buttons that were clicked
--Callbacks are given callback(button,event)
Button Object:
button:new(x,y,w,h,color,text,textColor)
button:setDisplay(string side) --"native" uses the computer's local display
button:setText(text)
button:setPosition(x,y) NOTE: changing position does not remove the old button from the display
button:setSize(w,h) NOTE: changing size does not remove the old button from the display
button:setColor(color)
button:addListener(func,[name])
button:removeListener(name)
button:clearListeners()
button:setActive(isActive)