I'm writing a program and the function menu does not work properly. The menu function finction accept a table as argument. The table contains the different menus that are to be displayed. There are two options. If the number of elements id lower than height of display - 4 then it displays everything normally. The problem is when there are more elements. 1st, the elements are displayed in inversed order. 2nd, I want o be able to click on an element with a mouse instead of pressing enter.
Thanks for your help.
Here is the program:
modem = peripheral.wrap("bottom")
iniOpts = {"Reboot All PCs","Change Password","Add Door","Remove Door","Lock All Doors","Open All Doors","Reset All Doors"}
lime = colors.lime
black = colors.black
function clear()
term.clear()
term.setCursorPos(1,1)
end
function menu(opts)
doIt = true
currSelect = 1
while doIt do
x,y = term.getSize()
clear()
term.setCursorPos(x,1)
term.setBackgroundColor(lime)
print("^")
term.setBackgroundColor(black)
if #opts <= y - 4 then
for i,v in pairs(opts) do
yPos = 2 + i
term.setCursorPos(math.floor(x - #v-4)/2,yPos)
if currSelect == i then
print("[["..v.."]]")
else
print(" "..v.." ")
end
end
event , button , xPos , yPos = os.pullEvent()
if event == "key" then
if button == keys.up then
currSelect = currSelect - 1
elseif button == keys.down then
currSelect = currSelect + 1
elseif button == keys.enter then
doIt = false
clear()
end
elseif event == "mouse_click" then
clear()
doIt = false
currSelect = yPos - 2
clear()
elseif event == "monitor_touch" then
clear()
doIt = false
currSelect = yPos - 2
clear()
end
if currSelect == 0 then
currSelect = #opts
elseif currSelect == math.floor(#opts + 1) then
currSelect = 1
end
else
for i,v in pairs(opts) do
yPos = math.floor(y/2) - currSelect + i
term.setCursorPos(math.floor(x - #v-4)/2,yPos)
if currSelect == i then
print("[["..v.."]]"..i)
else
print(" "..v.." "..i)
end
end
event , button , xPos , yPos = os.pullEvent()
if event == "key" then
if button == keys.up then
currSelect = currSelect + 1
elseif button == keys.down then
currSelect = currSelect - 1
elseif button == keys.enter then
doIt = false
clear()
end
elseif event == "mouse_click" then
doIt = false
currSelect = yPos - currSelect + math.floor(y/2)
clear()
elseif event == "monitor_touch" then
doIt = false
currSelect = yPos - math.floor(y/2) + currSelect
clear()
end
if currSelect == 0 then
currSelect = 1
elseif currSelect == #opts + 1 then
currSelect = #opts
end
if currSelect < 1 then
currSelect = 1
doIt = true
elseif currSelect > #opts then
currSelect = #opts
doIt = true
end
end
end
return currSelect
end
function rebootPc()
modem.transmit(3721,1,"reboot")
end
function changePass()
clear()
print("These are the available doors:")
print()
fil = {}
fil = fs.list("disk")
print("What password do you wish to change? ")
sel = menu(fil)
clear()
if fs.exists("disk/"..fil[sel]) then
files = fs.open("disk/"..fil[sel], "r")
print("Current password is: "..files.readAll())
files.close()
files = fs.open("disk/"..fil[sel], "w")
write("Input new password: ")
files.write(read())
files.close()
rebootPc()
else
print("This door does not exist!")
sleep(3)
changePass()
end
end
function addDoor()
clear()
print("What is the name of the new door? ")
print("(Type <nodoor> if you do not want to create a door.) ")
write(">")
desDoor = read()
if fs.exists("disk/"..desDoor) then
print("Door does already exist....")
sleep(2)
addDoor()
elseif desDoor == "nodoor" then
clear()
else
clear()
write("Input desired password: ")
doorN = fs.open("disk/"..desDoor,"w")
newP = read()
doorN.write(newP)
doorN.close()
shell.run("startup")
end
end
function deleteDoor()
clear()
listF = fs.list("disk")
delD = menu(listF)
write("Are you sure you want to delete door "..listF[delD].."? (y/n)")
sureOrNot = read()
if sureOrNot == "y" then
fs.delete("disk/"..listF[delD])
end
end
function lock()
modem.transmit(3721,1,"lock")
end
function rst()
modem.transmit(3721,1,"reset")
end
function openD()
modem.transmit(3721,1,"open")
end
function options()
if iniSelect == 1 then
rebootPc()
elseif iniSelect == 2 then
changePass()
elseif iniSelect == 3 then
addDoor()
elseif iniSelect == 4 then
deleteDoor()
elseif iniSelect == 5 then
lock()
elseif iniSelect == 6 then
openD()
elseif iniSelect == 7 then
rst()
end
end
iniSelect = menu(iniOpts)
clear()
options()
shell.run("startup")