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

Button API

Started by wieselkatze, 18 March 2014 - 07:24 PM
wieselkatze #1
Posted 18 March 2014 - 08:24 PM
Button API


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 methodsbutton.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 in



newbutton: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
Edited on 02 October 2014 - 07:22 PM
howdanrocks #2
Posted 17 June 2014 - 05:14 AM
Using your text code, I'm getting the error "button:27: index expected, got nil". Any suggestions?
wieselkatze #3
Posted 17 June 2014 - 10:09 AM
Should be fixed now. Just redownload the API and it should work. :)/>
I will probably push a new version of this API in the next time.
Konlab #4
Posted 20 June 2014 - 12:07 PM
Can I include this API in any OS/program?
wieselkatze #5
Posted 20 June 2014 - 12:56 PM
Sure, do whatever you want with it ;)/>
Konlab #6
Posted 20 June 2014 - 03:14 PM
Cool API, I have an 'upgrade' project, that auto-downloads some useful APIs and Programs, and I need some APIs in it, I'll give you Credits :)/>
wieselkatze #7
Posted 01 October 2014 - 05:30 PM
Alright, as this didn't contain much useful yet, I updated the API - much has changed and some things were implemented.
Documentation should be up to date soon.

Things that were added:
advPrint - Bounding text to left or right, centering it. Everything is possible for given coordinates in which it should be centered etc..
rectangle - Create a filled rectangle of a certain color. Filling with different characters than space + colors is also possible.
read - An advanced read function. Supports mouse-clicking and better scrolling. The width and position is also adjustable.
checkHitmap - Much changed - now returns all elements that match the given position + table depth
wieselkatze #8
Posted 02 October 2014 - 08:41 PM
Alright, fixed some bugs and added screenshots - actually quite useful for a GUI API :P/>
Edited on 02 October 2014 - 07:16 PM