Today, I bring you the most simple button API you've ever seen! It's easy to use, the methods it gives you are self-explaining, and *yay* it's "colorful"!
I give you pastebin link, cuz I know you like it.
If you want to download it in-game, type "pastebin get gvtZBAsF buttons".
Tutorial:
Spoiler
(I could have posted this in Tutorials, cuz this is basically a tutorial, but it's an API too, and it fits here more)Suppose the file is named "buttons" on the computer.
Step 1: Load the API
os.unloadAPI("buttons") -- better safe than sorry - maybe you didn't unload it properly previously
os.loadAPI("buttons")
Now every method is under "button" namespace (ok, table)…
Step 2: Set up an event processing loop
local running = true
while running do
local eventTable = {os.pullEvent()}
-- do something here if you want
end
Step 3: Call the two most important functions
local running = true
while running do
local eventTable = {os.pullEvent()}
buttons.event(eventTable) -- place it before the actual event processing, pass the event table
-- do something here if you want
buttons.draw() -- place it after the event processing
-- if you want to draw anything else, do it here, because buttons.draw() clears the terminal
end
Step 4: Set up the buttons and the functions you want to hook to them
-- load the API here
local buttonExit, buttonHideMe -- it's highly recommended to make these variables local at the beginning of the code
local exit = function() running = false end
local hide = function() buttons.show(buttonHideMe, false) end
-- and the buttons
-- buttons.register returns an ID, that's the reference you use when you want to do something with the button
buttonClose = buttons.register(3, 3, 6, 1, 128, "Exit", exit)
buttonSelfHiding = buttons.register(10, 3, 10, 1, 128, "Hide me!", hide)
--[[ the parameters:
- x position
- y position
- width
- height
- color (I'll explain later)
- text
- function pointer
]]
-- here is the loop
Step 5: Unload the API
os.unloadAPI("buttons")
How do colors work here?
Spoiler
I'm too tired to explain… You'll figure it out eventually…BUT I can give you a function that can convert colors API's colors:
function convertColor(cBackground, cText)
local color
for i = 0, 15 do if 2 ^ i == cBackground then color = i * 16 end end
for i = 0, 15 do if 2 ^ i == cText then color = color + i end end
return color
end
A test program can be found here. (pastebin 9uV85WKc)
Sorry if I made a mistake somewhere, feel free to shout my head off - I'm a bit tired, but I hope I gave yuo something useful.
EDIT: Now the API supports monitors (term.redirect), but you can force it to wait for a specific event.
LBPHacker