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

[1.1] Button API

Started by Termanater13, 04 March 2014 - 06:32 AM
Termanater13 #1
Posted 04 March 2014 - 07:32 AM
This is a Button API I have been Working on for while now. This is the first public API I'm releasing. I have rewritten it several times each time adding more functionality to the code. This code works similar to the button API made by Direwolf20, and not satisfied with just using what people make I made this.

link in the installation area

if there is anything I should add/change on this post or the API please let me know

Screenshots
SpoilerThe setup for appearance demonstration

Basic Colors
red is off, green(lime) is on

With different colors
line 1&3 are off, 2&4 are on
line 1&2 are regular paired, 3&4 are the colored pair
left column is button and text, column middle just background, right column just text
Installation
Spoilerhttp://pastebin.com/pMSgMrEr
Installing
pastebin get pMSgMrEr button
And in startup
os.loadAPI("button")
you can name the api anything. but keep in mind your code will have to reflect the different name. so button.wrap() would now be [the name you gave it].wrap()
Full List of functions
Spoiler

button.wrap(side)
button.new.b(id, text, xmin, xmax, ymin, ymax, func, argpass)
button.new.l(id, text, x, y, func, argpass)
button.e.color(id, T, on, off)
button.e.x(id, T, value)
button.e.y(id, T, value)
button.e.text(id, value)
button.e.label(id, value)
button.e.func(id, value)
button.e.argpass(id, value)
button.e.display(id, value)
button.e.click(id, value)
button.e.status(id, value)
button.e.hidden(id,value)
button.e.s.color(T, S, color)
button.e.s.scale(scale)
button.get.b(id)
button.get.l(id)
button.get.s()
button.get.f(id)
button.delete(id)
button.display()
button.check(x,y)
button.cycle()
Use
SpoilerThe following is to set up the API to display the buttons

button.wrap(side)
side: the side the advanced monitor is on. available options: (front, back, left, right, top, bottom,term)
NOTE: term is to use the computer as the touchscreen and not an advanced monitor attached to the computer
This is required to tell the API where to display the buttons
button.new.b(id, text,xmin,xmax,ymin,ymax,func,argpass)
id: unique identifier (note buttons and labels share id)
text: the text to be shown
xmin: left most point to be used in the button
xmax: right most point to be used in the button
ymin: top most point to be used in the button
ymax: bottom most point to be used in the button
func: function to be called when the button is clicked
argpass: argument to be passed to the function
NOTE: function must be given to this function as a variable (without the "()" at the end). arg pass also will only save one item or table.
button.new.l(id,text,x,y,func,argpass)
id: unique identifier (note buttons and labels share id)
text: the text to be shown
x: the horizontal start point
y: the vertical start point
func: function to be called when the button is clicked
argpass: argument to be passed to the function
NOTE: function must be given to this function as a variable (without the "()" at the end). arg pass also will only save one item or table.

The following edit the buttons options id in the following is the button's id when button.new.b (button.new.l for labels) was used

button.e.color(id, T, on, off)
T: edit the text color or background available options: back, text, background
on: the new on color available options: 16 colors supported by computercaraft (see color API) can be named or the number
off: the new off color available options: 16 colors supported by computercaraft (see color API) can be named or the number
button.e.x(id, T, value)
T:edit mon or max available options: min, max
value the new value of min or max
NOTE: if a number is passed for T the lower pf the 2 numbers is the new min the higher is the new max
button.e.y(id, T, value)
T:edit mon or max available options: min, max
value the new value of min or max
NOTE: if a number is passed for T the lower pf the 2 numbers is the new min the higher is the new max
button.e.text(id, value)
value the new text to be used for display
NOTE: button will not resize for longer text but will keep it centered unless it would hang over the left side
button.e.label(id, value)
value the buttons new label
NOTE: this is only really used to help the API determine what default colors to use
button.e.func(id, value)
value the new function the button should use
button.e.argpass(id, value)
value the new argument the API should pass to the button's function
button.e.display(id, value)
value should the button be displayed? (y/n) available options: yes, on, true, no, off, false
NOTE: leaving value blank toggles the display setting of the button
button.e.click(id, value)
value can the button be clicked on? (y/n) available options: yes, on, true, no, off, false
NOTE: leaving value blank toggles the click setting of the button
button.e.status(id, value)
value the current status of the button available options: yes, on, true, no, off, false
NOTE: leaving value blank toggles the status setting of the button
button.e.hidden(id,value)
value can the button be clicked if it is not shown? (y/n) available options: yes, on, true, no, off, false
NOTE: leaving value blank toggles the hidden setting of the button
button.e.s.color(T, S, color)
T: edit the defult color for the text or background available options: back, text, background
S: edit the on, off, non color available options: on, off, non
color the new color to use available options: 16 colors supported by computercaraft (see color API) can be named or the number
NOTE: non refers no non button color. non button background is the background used for any area not used by a button
button.e.s.scale(scale)
scale the new scale of the monitor available options: any number usable by a computercraft monitor
NOTE: if term is used for wrap, this function does nothing.
button.get.b(id)
button.get.l(id)
id: get the information on a button/label with the id
button.get.s()
gets setting information
button.get.f(id)
id: returns a table with function and arguments to pass based on the ID does not call the function
button.delete(id)
id: the Unique id of the button you want to delete from the system
button.display()
this tells the API to draw the buttons on the monitor set by the button.wrap() command
button.check(x,y)
x: the x value of the os.pullEvent("monitor_touch")
y: the y value of the os.pullEvent("monitor_touch")
button.cycle()
this just cycles the display, os.pullEvent and check automaticly
Examples
Spoilercode used to make the screenshots pastebin FqTRzQJv

following code can be used for toggling the status of a button and passing the staus of that button to the output

function redstone ()
  button.status("redstone")
  rs.setOutput("left", button.get.b("redstone").status)
end
Button & Settings variable layout
SpoilerButton/Label/Func
The following is the table structure of a variable if you store data using button.get.b() when passing an id, if no id is passed the entire table is given and is id indexed. Note that button in the following is if you used the variable b

b.x.min
b.x.max
b.y.min
b.y.max
b.text.on
b.text.off
b.text.show
b.back.on
b.back.off
b.display
b.click
b.hidden
b.label
b.status
Setting
this is the variable structure if button.get.s() was used and stored in the Setting variable

Setting.back.on
Setting.back.off
Setting.back.non
Setting.text
Setting.text.on
Setting.text.off
Setting.text.non
Setting.scale
Setting.size
Setting.size.x
Setting.size.y
this is the variable structure if button.get.f() was used and stored in the func variable
func.func
func.argpass
To Do list
Spoilerget it to use the terminal as well as peripherals
have it use different monitors at the same time
simplify function calls
better/more examples
Support
Spoilerbefore I can help you with my API, I will need the code you are working with and the error that is being given. I will only do support for the API in this thread, or ask a pro of I see it there.
Change Log/Previous versions
SpoilerThe date given is the date of the update, the pastebin link is for the version associated pastebin
3/12/14 Pastebin
  • Added support for the computer you are using as the touchscreen
  • Added a way to delete unwanted buttons buttons
3/7/14 Pastebin
  • Fixed an error that would cause buttons/labels with nil functions to crash the API, and they now behave properly. (paste bin code not changed)
3/4/14 Pastebin
  • Initial release
Edited on 12 March 2014 - 07:34 PM
ByteZz #2
Posted 12 March 2014 - 02:26 PM
I think this is quite interesting! I am going to give this a spin as I seem to like this. I hope it works for what I want it to do!
Termanater13 #3
Posted 12 March 2014 - 08:33 PM
Just released new version that now supports the computer in the same way as a advanced monitor.

I think this is quite interesting! I am going to give this a spin as I seem to like this. I hope it works for what I want it to do!
thanks, if you have any issues I'll do my best to help out
mkoster2000 #4
Posted 29 April 2015 - 09:09 PM
if got an problem with the api

when i do

pastebin get pMSgMrEr button
it i good it's says
Connting to pastebin.com…Success.
Downloaded as button

but when i do
edit startup and type
os.loadAPI("button")
click save end then exit
and then do reboot

it doesn't do anything

i just get the Craftos 1.6

pls help
KingofGamesYami #5
Posted 29 April 2015 - 11:01 PM
Um, os.loadAPI will only load the api, it won't do anything with it. You'll have to make some buttons if you want it to do something.
mkoster2000 #6
Posted 30 April 2015 - 06:31 AM
Um, os.loadAPI will only load the api, it won't do anything with it. You'll have to make some buttons if you want it to do something.

how do i make some buttons?
WilkerS1 #7
Posted 01 May 2015 - 04:15 PM
i want to see it on my computer but i don't have internet in my house. does this api have a help topic just like the command

help redstoneapi
KingofGamesYami #8
Posted 01 May 2015 - 04:25 PM
No. The in-game help script only has data hardcoded into it, so nothing other than default programs will give you something.
Lupus590 #9
Posted 01 May 2015 - 05:21 PM
there is an API though (I think that will allow you to add files to the default help program)