Always hated to make all the menu code? You want to save time by not having to do it? Then you must read all this!

This API will not only draw your menu, but also will check for the mouse clicks in the selections and the keyboard selections! Thats right, it will return the selection that the user made.

You will first need to make a table with the selections:


yourTable = {
"Selection1!",
"Selection2!",
"so on"}

Then you call the API function drawMenu(), like this:


var1, var2 = APIname.drawMenu(yourTable, xpos, ypos)

Note the var1, var2, xpos and ypos? The var1 is the name of the selection the user made, the var2 is the number of the selection the user made, the xpos is the x-coordinate where the menu starts (if set to true, then it will be centrered!), and the ypos is where the menu starts a y-coordinate.

Thats it! No more extensing coding for a single menu!

Pastebin: rYRwZnKi

Code:
Spoiler


function drawMenu(tMenu, xpos, ypos, curSel)
if xpos == true then
centered = true
else
centered = false
end
if curSel ~= nil or curSel~= "" then
sel = 1
else
sel = curSel
end
tPlaces = {}
while true do
for i=1, #tMenu do
iSel = i-1
local w, h = term.getSize()
table.remove(tPlaces, i)
if i == sel then
if centered == false then
term.setCursorPos(xpos, ypos+iSel)
term.clearLine()
table.insert(tPlaces, i, term.getCursorPos()+2)
write("[ "..tMenu[i].." ]")
else
term.setCursorPos(w/2-#tMenu[i]/2-2, ypos+iSel)
term.clearLine()
table.insert(tPlaces, i, term.getCursorPos()+2)
write("[ "..tMenu[i].." ]")
end
else
if centered == false then
term.setCursorPos(xpos, ypos+iSel)
term.clearLine()
table.insert(tPlaces, i, term.getCursorPos())
write(tMenu[i])
else
term.setCursorPos(w/2-#tMenu[i]/2, ypos+iSel)
term.clearLine()
table.insert(tPlaces, i, term.getCursorPos())
write(tMenu[i])
end
end
end
local event, key, x, y = os.pullEvent()
if event == "key" then
if key == 200 and sel > 1 and sel <= #tMenu then sel = sel-1
elseif key == 208 and sel >= 1 and sel < #tMenu then sel = sel+1
elseif key == 208 and sel == #tMenu then sel = 1
elseif key == 200 and sel == 1 then sel = #tMenu
end
for v = 1, #tMenu do
if key == 28 and sel == v then
return tMenu[v], sel
end
end
elseif event == "mouse_click" then
for s = 1, #tMenu do
iSel = s-1
if s == sel then
if x >= tPlaces[s] and x < tPlaces[s]+#tMenu[s] and y == ypos+iSel then
return tMenu[s], s
end
end
end
end
end
end

Note that this is my ever first attempt at tables, sorry if I mess up :3