This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
LBPHacker's profile picture

The most simple Buttons API

Started by LBPHacker, 20 March 2013 - 08:54 AM
LBPHacker #1
Posted 20 March 2013 - 09:54 AM
Hey dudes, LBPHacker here!

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?
SpoilerI'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
TheSimonizer #2
Posted 05 April 2013 - 08:56 AM
Can we change the text of the button?

like have a button "Hide Me" that hides everything and switches to "Show Me" when you click it and vice versa?
LBPHacker #3
Posted 05 April 2013 - 09:00 AM
You'll be able to do that in 5 minutes ;)/>
LBPHacker #4
Posted 05 April 2013 - 09:06 AM
OK, added two functions: setText and setPosition - removed parameter filtering (actually just commented it out)

Same pastebin ID, it's updated now.

EDIT: It's 100 lines now (No, that wasn't on purpose…) :D/>
theoriginalbit #5
Posted 05 April 2013 - 05:30 PM
what the hell is that convertColour thing? o.O
why can't you just take in two numbers? why does it have to be a single colour?
shazbot #6
Posted 06 April 2013 - 09:45 AM
Mind if I edit this so support monitor touch? :)/>
LBPHacker #7
Posted 06 April 2013 - 09:47 AM
Mind if I edit this so support monitor touch? :)/>

Do as you wish :D/> But like I said at the beginning of the code: give credit, at least something like "LBP".

EDIT: I'm gonna make it support monitor_touch in 5 mins.

EDIT #2: Done. Now it automatically waits for the appropraite event both on monitors and default terminals, but you can force the API to wait for a specific event using setForcedEvent. Of course, you can pass a nil instead of the event you want to force - in that case, the API will no longer wait for the event you've specified earlier and switches back to default, where it detects monitors automatically.

EDIT #3: Hotfix in another 5 minutes…
LBPHacker #8
Posted 06 April 2013 - 11:03 PM
Ugh… I've really messed up something… Next update soon…

EDIT: Everything solved. Updated OP.
shazbot #9
Posted 11 April 2013 - 05:13 AM
I found an issue with monitor_touch events. The code checks to see if the mouse_click event was the left mouse button (the event[2] == 1 in that case), but a monitor_touch puts the side of the monitor there, meaning that it never calls the button function for monitor_touch events.

Also, it would be nice to not have the draw function always leave the background and text colors set to whatever was used to draw the button.

I made an edited version that includes these changes, setting the background and text colors to black and white respectively by default but also including two functions to change those default values (setBgColorDefault and setTextColorDefault).

Here's a version that addresses both of those things: http://pastebin.com/RXaSjKZ8
LBPHacker #10
Posted 11 April 2013 - 06:07 AM
-snip-
Thanks, everything fixed. Help is really appreciated, pastebin links are not though (don't want that few pople who get here get confused :D/>) Now the default colors are white for text and black for background, but you can change that by .setDefaultColor. And removed that crappy color-coding - yup, no more 128, but colors.white, colors.lightGray instead.

Both pastebin links (API and test) updated.
mad-murdock #11
Posted 16 April 2013 - 09:57 PM
you should supply the buttonID to a registered function. this way, the invoking program could assign the same function for similar buttons and branch inside those functions if needed. simple example: extend the buttons to a checkbox class. i will add this now, and hope, you do, too - so i can use future releases.

oh, apart from that remark, i like the simple interface - hence my choice over writing a button handler myself (:
theoctagon #12
Posted 07 May 2013 - 07:22 PM
This looks like an awesome API - however I am unclear on its proper usage. Could you provide an example please? I have no idea how to utilize this without some examples. I have my own program that I'm working on for touch screen buttons but hadn't gotten to the stage of drawing them out yet. Then I found this. How do I utilize this from within another peice of code?

Thanks!