Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- call newmenu with a table as the first parameter, the table is a list of all options in the menu, if you like you can specify a table as one/more of the options, it will then show the first value in that table as the choice and will call the second value as a function if you select that option, it will call the function with the option as a parameter and the newmenu function will return whatever your choice returns. if the choice is not a table then it will simply return the string that is the choice. example:
- newmenu({1,2,3,4},2,2)
- would generate a menu where you can choose 1; 2; 3 or 4 and if you choose 2 it will return 2. the menu will start at CDS 2,2
- newmenu({1,{2,function(param) return print('you selected '..param) end},3,4},2,2)
- would generate an identical looking menu but this time if you select 2 it will call the function there with param=2 and the newmenu function will return whatever the function returns
- ]]
- local function newmenu(tList,x,y)
- x=x or 1
- y=y or 1
- y=y-1
- local selected=1
- local function render()
- for num,item in ipairs(tList) do
- term.setCursorPos(x,y+num)
- write((num==selected and '[' or ' ')..(type(item)=='table' and item[1] or item)..(num==selected and ']' or ' '))
- end
- end
- while true do
- render()
- local evts={os.pullEvent('key')}
- if evts[1]=="key" and evts[2]==200 and selected>1 then
- selected=selected-1
- elseif evts[1]=="key" and evts[2]==208 and selected<#tList then
- selected=selected+1
- elseif evts[1]=="key" and evts[2]==28 or evts[2]==156 then
- return (type(tList[selected])=='table' and tList[selected][2](tList[selected][1]) or tList[selected])
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment