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

Simple Button API

Started by Lewisk3, 06 September 2015 - 04:21 AM
Lewisk3 #1
Posted 06 September 2015 - 06:21 AM
RedButton


What is Redbutton?
redbutton is a simple and easy way to create buttons on computercraft
all click event handling done in the API no need to make your own calculations!



How do i use Redbutton?
Spoiler 1. Creating buttons


-- Welcome to Redbutton, lets start by creating our first button!
-- first, we need to load the api
   os.loadAPI("redbutton")
-- now lets add our button and draw it to the screen
local mybutton = redbutton.addButton(
						5,5,15,2,colors.white,colors.blue,"My Button XD")
	   -- add button takes the arguments (startx, starty,
	   --  width, height, textcolor, backcolor, text)
-- now to draw it
	
	  mybutton:draw()
-- and your all done XD

2. Handling events


-- now to handle our button clicks!
os.loadAPI("redbutton")
local button = redbutton.addButton(
					 5,5,15,2,colors.white,colors.blue,"Click Me!")
-- now for a while loop for checking click events
	
while true do
		 -- ok now lets check if our button is pressed
		ev = {os.pullEvent()} -- make sure your event is in a table
	
		-- now to pass our event table onto our button
	   if(redbutton:pressed(ev))then
		  -- exit the program if our button was pressed		  
			 break
	   end	  
end


3. Changing colors


-- to change colors just call this method from the button --
mybutton:colorize(textcolor, backcolor)



4. Editing position

-- to change position just call this method from the button --
mybutton:setPos(x,y,clear previous position with color)


5. clearing a button

-- to clear a button use -
mybutton:clear(clear with color)


6. renaming a button

-- to rename a button use -
mybutton:rename(newname)

Download : http://pastebin.com/uks1ZeP2
Edited on 07 September 2015 - 12:51 PM
Lewisk3 #2
Posted 06 September 2015 - 07:57 PM
feedback welcomed!
i want to know if you guys like my API or not, and what other functionalities to add.
pfgcomputercraft #3
Posted 25 September 2015 - 07:16 PM
Nice api, I'm using it for a test os I'm making. Is there any way to have an image button?
Lewisk3 #4
Posted 24 November 2015 - 11:12 PM
Nice api, I'm using it for a test os I'm making. Is there any way to have an image button?

no, sorry.
the way the program works is with drawing a rectangle arround an area using math.
so im not sure having a image as a button would fit into that.

Also, i am developing a more advanced menu/button api its experimental though the download is below
http://pastebin.com/VxCKrEAp
Edited on 24 November 2015 - 10:19 PM
Awe2K #5
Posted 25 November 2015 - 01:38 PM
Cool API, you should probably look at Surface API, I use it in my App Framework (for example, for ImageView or ImageButton).
I know it will make your API bigger a bit (~35 kB), but it is the best way to prevent tearing when you redraw the whole screen.
Lewisk3 #6
Posted 27 November 2015 - 04:52 AM
Cool API, you should probably look at Surface API, I use it in my App Framework (for example, for ImageView or ImageButton).
I know it will make your API bigger a bit (~35 kB), but it is the best way to prevent tearing when you redraw the whole screen.

there is a easier way to prevent when redrawing the entire screen just use

term.current().setVisible(false)
-- draw code
term.current().setVisible(true)