There are already a lot of Button APIs out there, but I wanted to combine the most useful things of them in my creatively named "Button API". It has a multi-level hitmap and some other functions to offer.
To download the API just type
pastebin get hYN4QA4c button
in your CC shell.Creating a new button is as simple as this:
os.loadAPI("button") -- Assuming you saved the file as "button"
newbutton = button.button({ x1=1; x2=5; y1=1; y2=4; bg=128; txt="hellou" obj=peripheral.wrap("right") }) --# This will create a new button with the monitor on the right
Available methods
button.advPrint( txt, y1, ort, x1, x2 )Prints text with specific orientations at certain areas or to the whole screen
txt - The text to print
y1 - The height the text should be printed at
Optional:
ort - The orientation of the text, see more below
x1 - First X-position to be used for printing the text in certain areas (Default: 1)
x2 - Second X-position to be used for printing the text in certain areas (Default: Screen size)
Available Orientations:
ul - Print exactly on the left - default would result in printing the text at position 1
l - Print with margin - default would result in printing the text at position 2
c - Center the text
r - Print with margin on the right - default would result in printing the text at (screensize)-#text+1
ur - Print exactly at the right - self explaining
button.rectangle( x1, y1, x2, y2, clr, char, charclr )
Draws a filled rectangle at given position with given color
x1 - Min X
x2 - Max X
y1 - Min Y
y2 - Max Y
clr - The color the rectangle should be filled with
Optional:
char - The character the rectangle should be filled with (Default: Space " ")
charclr - The color of that character
button.read( arg ) -> arg is a table
Returns a table containing all the functions
arg.x1 - Min X
arg.x2 - Max X
arg.y1 - Y-position
Optional:
arg.txt - Predefined text
arg.bg - Background color
arg.fg - Text color
arg.obj - A peripheral wrapper to use this on monitors
arg.filter - A function to limit/filter the text input. For example the following
button.read( { x1=1; x2=6; y1=3; filter=
function(text)
return tonumber(text)
end
} )
would limit the input so that only numbers could be typed innewbutton:evhandle( … ) -> Only for read
Handles key, paste and mouse-click events and changes properties of the read object
… - unpacked os.pullEvent() variables, e.g.
newbutton:evhandle( os.pullEvent() )
or
local e = { os.pullEvent() }
newbutton:evhandle( unpack ( e ) )
button.button( arg ) -> arg is a table
Returns a table containing all the functions
arg.x1 - Min X
arg.y1 - Min Y
arg.x2 - Max X
arg.y2 - Max Y
Optional:
arg.bg - Button color
arg.txt - Text (string or table, table should look like this: {"this is line one", "this is line two"}) so with numbered index
arg.obj - A peripheral wrapper to use this on monitors
newbutton:draw()
No arguments required
Draws the button
newbutton:addLine( arg ) -> arg is a table
arg.txt - The text to add
Optional:
arg.pos - ul, l, c, r, ur are allowed - positioning of the text
arg.fg - text color
newbutton:setLine( arg ) -> arg is a table
arg.index - The line of which the settings should be changed
Optional:
arg.txt - The text to add
arg.pos - ul, l, c, r, ur are allowed - positioning of the text
arg.fg - text color
button.checkHitmap( arg ) -> arg is a table
arg.x1 - X coordinate of the click
arg.y1 - Y coordinate of the click
arg.hitmap - The hitmap to check
Hitmaps are just tables containing the button elements, e.g.:
{
no1 = button.new( { x1=1; x2=2; y1=1; y2=2; txt="test" } )
}
Screenshots
Button - Read - Button
Button
The API is still not finished and of course needs work.
Feedback is greatly appreciated
~wieselkatze