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

[API] Menu / GUI maker - Scrolling, mouse support, customisation

Started by kylergs, 05 December 2012 - 10:45 AM
kylergs #1
Posted 05 December 2012 - 11:45 AM
As an extension from my past program I decided to create an API to allow you to create fully navigable menu's and interfaces.

Almost everything is customisable, but it can become a bit complicated if you want to do things seamlessly.
There is same automatic function built in but for more interactive menus you should set it up yourself.

Here are the functions:
SpoilerWhere 'guiapi' is the name of the loaded API:
guiapi.msgBox(table Messages)
–>> Draws a message box in the center of the screen
–>> Each line should be an entry in a table

guiapi.inpBox(table Messages)
–>> The same as a message box except returns an inputed value

guiapi.printCentred(string text, integer y-value)
guiapi.printLeft(string text, integer y-value)
guiapi.printRight(string text, integer y-value)

–>> Prints the 'text' value at the height 'y-value' on the left, right or center of the screen respectively

guiapi.defineDefaultColour(<See Description>)
–>> This will define a different set of default colours, these need to be in the correct order
–>> This is:
> Background of Header/Footer, Background of Menu, Header / Footer Text Colour
> Default item colour, msgBoxTextolour, msgBox Background,inpBoxText,inpBoxBackground,inpBox Input Text colour
–>> This is not necessary - Only for experienced users!
–>> If you want to use the easier way of doing you can use:
> defineDefaultColourTable(tColours)
–>> With tColours as a table in the correct form shown around Line 10

guiapi.defineDefaultValues(<See Description>)
–>> Same as above but with:
> Header Text, Footer Text, Item Name
–>> Arguments - Also not neccaccry but useful sometimes for reducing code
–>> Remember that overlay Header / footers use tables for text so arguments must be tables - see later

guiapi.newOverlay()
–>>For custom header / footer values, returns an object as a table

guiapi.newItem()
–>> Creates an individual item for a list, returns an object

guiapi.newMenu()
–>> Creates the Menu Object, again is returned

———
An example Item wrap
exampleItem = guiapi.newItem()

Now here are the overlay functions
Spoiler

Where 'overlay' is the saved object
overlay:setHeaderTitle(...)
-->> you can define any number of strings as arguments. Each one gets a line above the menu ( like a header )
overlay:setFooterText(...)
-->> Same as the above, but on the bottom on the screen
overlay:setOverlayColour(number/colour)
-->> Self explanatory , use the colour API or an equivalent number (sets the colour of the overlay)
overlay:setBackgroundColour(colour)
-->> same as above except changes the colour of the background of the overlay
overlay:setBackgroundHF(colour)
-->Sets the header/footerbackfround
overlay:draw()
-->> Prints the overlay, may or may not be useful (done automatically as is shown later)
-->> [b]Does [/b]clear the term before-hand.

Item commands
Spoiler

Where 'item' is the saved object
Each entry in a menu is an item, this item contains everything that is returned when you click that entry

item:newName(string name)
-->> changes the displayed text for that item/ entry
item:newType(string type)
-->> changes one of the returned values
-->> A few spacial cases that are shown later
item:newColour(number colour)
-->> changes the colour of the entry
item:setData(table data)
-->> sets the data table to what is entered (the data table is returned along with the type)
item:addData(anything)
-->> adds an item to the table
-->> this table is returned when the item is selected


Menu Commands
Spoiler

Where 'menu' is the saved object

menu:newOverlay(object overlay) -->> Changes the overlay for that menu
menu:resetItems() -->> clears the menu of items
menu:newItem(object item) -->> adds an item to the menu
menu:draw() -->> draws up the menu and allows the user to navigate


Automatic use:

There is a little bit of automation available here.
Initially, you do not need an Overlay, a small, default one is created automatically
Secondly, in a few cases, you can use item types to navigate menus better
e.g.
if the type is "text" (default) - Nothing will happen on click/ enter
if the type is "menu" and a menu (the object itself) is saved to the data, it will automatically link there.
if the type is "msgbox" and there is a table in the data, a msg box will be drawn


Finally, an image
Spoiler


And the message box
Spoiler

And the code:
http://pastebin.com/PmCUHuTr

and the code I used for that menu shown:

http://pastebin.com/s8PkZRjs


Enjoy and please respond with any suggestions / bugs / comments!
kylergs #2
Posted 07 December 2012 - 07:12 AM
Updated functions, also, please post any example programs :)/>
Stupidcreeper123 #3
Posted 08 December 2012 - 07:16 AM
Looks cool I've tried making GUIs before but there just a bit beyond my level of lua so this might help quite a lot :D/>

It Worked
kylergs #4
Posted 08 December 2012 - 08:51 AM
Thanks for the response, anything I can do to make your life easier?

I plan on making a few example / stock programs for it such as file navigators (with extensions), menus from text files etc
kylergs #5
Posted 09 December 2012 - 08:17 AM
Fixed Argument problems in post
makerimages #6
Posted 09 December 2012 - 09:05 AM
hmm, this could use better function explanation…
kylergs #7
Posted 09 December 2012 - 09:28 AM
How so? Which parts are confusing?
makerimages #8
Posted 09 December 2012 - 10:03 AM
ths still runs off of menu elements ina text file?
kylergs #9
Posted 09 December 2012 - 10:13 AM
No, this is just an API, everything has to be entered within the program
kylergs #10
Posted 09 December 2012 - 10:48 AM
Haha thanks, any improvements needed?
Sammich Lord #11
Posted 09 December 2012 - 02:31 PM
Me <3 this :D/>
kylergs #12
Posted 10 December 2012 - 04:07 AM
Well first off, can the "———" horizontal lines just be White pixels? It'd look pretty!
Thats a good idea, at the moment its in a sort of Non-colour computer compatibly mode

edit: I'm not too sure….

Spoiler


Edit 2 : But what do you thing about this? :

Spoiler
kylergs #13
Posted 10 December 2012 - 08:26 AM
Updated overlay stuff - next, general background for the menu, a scroll bar, better mouse detection and tool-bars (useful for OS's)!
Sammich Lord #14
Posted 10 December 2012 - 08:28 AM
Keep up the good work!
kylergs #15
Posted 10 December 2012 - 08:29 AM
Haha thanks. I would do more but Exams …
kylergs #16
Posted 11 December 2012 - 03:00 AM
Added more customisability - 1.3. Background Colours and stuff
Jode #17
Posted 16 December 2012 - 10:01 AM
Looks awesome. If I don't use this, I'll definitely learn something from examining the source code.

Also, the text of the code link (which is valid) is different from where it actually leads (which is not)?
kylergs #18
Posted 17 December 2012 - 08:22 AM
Glad that I helped, not sure what happened with the link :)/>
apoc123 #19
Posted 05 January 2013 - 05:19 PM
Im a noob sorry if noob question.

to add this api do i name it what ever or name it guiapi for first pastebin then menu or colors for the name of the second file.

does the name matter when manually placing your api in the lua/api directory
kylergs #20
Posted 12 January 2013 - 03:57 PM
The name of the api doesn't matter
JeRoNiMoKaNT #21
Posted 12 April 2013 - 07:03 AM
Hello there,

I'm far from experienced with ComputerCraft, but I'm setting up a railway system on my FTB server.
I want to use 1 or 2 touchscreen monitors with a GUI to have the players select their destinations, and then send signals to the wireless redstone for the tracks. Do you have any tips on how to make a GUI for that?

Thanks in advance
Fenthis #22
Posted 03 May 2013 - 07:01 PM
Syggestion(s):

Have the message box accept a second table of button names, so you can do things like { "OK", "Cancel" }.