Posted 06 January 2013 - 02:58 PM
I'm currently trying to modify someone else's code for a menu which I've seen a couple times but know very little of Lua and require some assistance. Currently I've added all 16 options that I want but I need them to be side by side rather than centred in the middle of the screen. Below is the code I have up to this point.
I also require a left and right option to move freely between the columns.
Any help with this would be greatly appreciated.
local w,h = term.getSize()
function printCentred( y, s )
local x = math.floor((w - string.len(s)) / 2)
term.setCursorPos(x,y)
term.clearLine()
term.write( s )
end
local nOption = 1
-- Display menu
local function drawMenu()
term.clear()
term.setCursorPos(1,1)
term.write( "LVC Activation" )
term.setCursorPos(w-17,1)
if nOption == 1 then
term.write( "1" )
elseif nOption == 2 then
term.write( "2" )
elseif nOption == 3 then
term.write( "3" )
elseif nOption == 4 then
term.write( "4" )
elseif nOption == 5 then
term.write( "5" )
elseif nOption == 6 then
term.write( "6" )
elseif nOption == 7 then
term.write( "7" )
elseif nOption == 8 then
term.write( "8" )
elseif nOption == 9 then
term.write( "9" )
elseif nOption == 10 then
term.write( "10" )
elseif nOption == 11 then
term.write( "11" )
elseif nOption == 12 then
term.write( "12" )
elseif nOption == 13 then
term.write( "13" )
elseif nOption == 14 then
term.write( "14" )
elseif nOption == 15 then
term.write( "15" )
else
term.write( "16" )
end
end
-- Display the frontend
term.clear()
local function drawFrontend()
printCentred( math.floor(h/2) - 6, "SELECT AN OPTION" )
printCentred( math.floor(h/2) - 5, ((nOption == 1) and "[ 1 ]") or "1" )
printCentred( math.floor(h/2) - 4, ((nOption == 2) and "[ 2 ]") or "2" )
printCentred( math.floor(h/2) - 3, ((nOption == 3) and "[ 3 ]") or "3" )
printCentred( math.floor(h/2) - 2, ((nOption == 4) and "[ 4 ]") or "4" )
printCentred( math.floor(h/2) + 1, ((nOption == 5) and "[ 5 ]") or "5" )
printCentred( math.floor(h/2) + 0, ((nOption == 6) and "[ 6 ]") or "6" )
printCentred( math.floor(h/2) + 1, ((nOption == 7) and "[ 7 ]") or "7" )
printCentred( math.floor(h/2) + 2, ((nOption == 8) and "[ 8 ]") or "8" )
printCentred( math.floor(h/2) + 3, ((nOption == 9) and "[ 9 ]") or "9" )
printCentred( math.floor(h/2) + 4, ((nOption == 10) and "[ 10 ]") or "10" )
printCentred( math.floor(h/2) + 5, ((nOption == 11) and "[ 11 ]") or "11" )
printCentred( math.floor(h/2) + 6, ((nOption == 12) and "[ 12 ]") or "12" )
printCentred( math.floor(h/2) + 7, ((nOption == 13) and "[ 13 ]") or "13" )
printCentred( math.floor(h/2) + 8, ((nOption == 14) and "[ 14 ]") or "14" )
printCentred( math.floor(h/2) + 9, ((nOption == 15) and "[ 15 ]") or "15" )
printCentred( math.floor(h/2) + 10, ((nOption == 16) and "[ 16 ]") or "16" )
end
-- Call functions for display menu
drawMenu()
drawFrontend()
while true do
local e,p = os.pullEvent()
if e == "key" then
local key = p
if key == 17 or key == 200 then
-- Up
if nOption > 1 then
nOption = nOption - 1
drawMenu()
drawFrontend()
end
elseif key == 31 or key == 208 then
-- Down
if nOption < 16 then -- Change 3 by the number of option.
nOption = nOption + 1
drawMenu()
drawFrontend()
end
elseif key == 28 then
-- Enter
break
end
end
end
term.clear()
-- Conditions
if nOption == 1 then
print("Option 1 selected")
elseif nOption == 2 then
print("Option 2 selected")
elseif nOption == 3 then
print("Option 3 selected")
elseif nOption == 4 then
print("Option 4 selected")
elseif nOption == 5 then
print("Option 5 selected")
elseif nOption == 6 then
print("Option 6 selected")
elseif nOption == 7 then
print("Option 7 selected")
elseif nOption == 8 then
print("Option 8 selected")
elseif nOption == 9 then
print("Option 9 selected")
elseif nOption == 10 then
print("Option 10 selected")
elseif nOption == 11 then
print("Option 11 selected")
elseif nOption == 12 then
print("Option 12 selected")
elseif nOption == 13 then
print("Option 13 selected")
elseif nOption == 14 then
print("Option 14 selected")
elseif nOption == 15 then
print("Option 15 selected")
else
print("Option 16 selected")
end
I also require a left and right option to move freely between the columns.
Any help with this would be greatly appreciated.