This is an API for buttons and support for multiple GUIs.

GUIs

In this API, GUIs are just functions running as coroutines (threads). This allows the GUI to run alongside the event listener (which handles button clicks and key presses) similar to the Parallel API. The benefit of this API is that buttons can change the active GUI dynamically, instead of using Parallel.waitForAny and return statements. To create a GUI, simply make a function that draws what you want, creates the buttons you want, and does any other functions such as read(). Changing GUIs is done by buttons (see below).

Buttons

Buttons are objects of a Button class, and instantiated using

gui.Button.new(topLeftX, topLeftY, width, height, maincolor, textColor, text, target, callsGUI)

Buttons do one of two things. They either call a function normally, or they create a coroutine of a function, which will become the active GUI. The latter is the case when callsGUI is true. Otherwise, the function will simply be called upon clicking of the button. In both cases, the function called or coroutined is the parameter target.

Note that in the case of the "target" parameter, references to functions themselves must not include ().

Example:

gui.Button.new(45, 1, 6, 1, colors.blue, colors.lightBlue, "reboot", os.reboot, false)

This will create a 6x1 button in the top right corner that, when clicked, reboots the computer. The final parameter is false because os.reboot is just a function to be called.

gui.Button.new( 2, 1, 8, 1, colors.magenta, colors.red, "Next Page" , nextGUI, true)

This button, when clicked, creates a coroutine of the function nextGUI, which will run as the new active coroutine. nextGUI would contain all drawing (or call another function to do it), starting by setting the BG color and clearing the screen.

How to use

Put this at the bottom of your script.


os.loadAPI("gui")
activeGUI = firstGUI()	 				 	  --NOTE: replace firstGUI with your own first GUI
while true do
	coroutine.resume(activeGUI)		 			--resume active GUI handler (if it is alive)
	local action = gui.eventHandler(os.pullEvent())			  --get event
	if type(action) == "function" then
		action()
	end
	if type(action) == "thread" then
		for k,v in ipairs(gui.buttons) do			  --removing last instance's buttons
			gui.buttons[k] = nil
		end
		activeGUI = action					   	 --set to active gui.
	  
	end
end

gui.eventHandler() calls gui.clickHandler(), and if the click is within a button, it does what the button says to do.

You also need this file:

http://pastebin.com/TBYrvx0X

pastebin get TBYrvx0X gui

If you find any bugs let me know.