Posted 02 November 2014 - 03:35 PM
This is a small function I wrote to emulate the miniature menu of CraftOS's "edit" program. It is very easy to use, and can be used on both advanced and normal computers. It currently will not correct for smaller screen width, if you give it too many options (if you need more, see my menu). If this is a problem, I can update it, but I don't see anyone needing that many options out of this menu.
pastebin get V455UuCr
Questions? Suggestions? Post them below!
The Code
local function minimenu( ... )
local tArgs = { ... }
local tSelections = {}
local _, y = term.getSize()
local n = 1
for i, str in ipairs( tArgs ) do
tSelections[ i ] = { str = str }
tSelections[ i ][ "x1" ] = n
tSelections[ i ][ "x2" ] = n + #str + 1
n = n + 2 + #str
end
for k, v in ipairs( tSelections ) do
term.setCursorPos( v["x1"], y )
term.write( ' ' .. v.str )
end
local slc = 1
local last = 1
while true do
term.setCursorPos( tSelections[ last ]["x1"], y )
term.write( ' ' )
term.setCursorPos( tSelections[ last ]["x2"], y )
term.write( ' ' )
term.setCursorPos( tSelections[ slc ]["x1"], y )
term.setTextColor( term.isColor() and colors.yellow or colors.white )
term.write( "[" )
term.setCursorPos( tSelections[ slc ]["x2"], y )
term.write( "]" )
while true do
local event, key = os.pullEvent( "key" )
if key == 203 and tSelections[ slc - 1 ] then
last = slc
slc = slc - 1
break
elseif key == 205 and tSelections[ slc + 1 ] then
last = slc
slc = slc + 1
break
elseif key == 28 then
return tSelections[ slc ].str
end
end
end
end
pastebin get V455UuCr
Usage
This menu is simple to use, simply call the function like so:
local selection = minimenu( "Option1", "Option2" )
And it will return either "Option1" or "Option2". You can add as many selections as you want, just remember to use string values.Questions? Suggestions? Post them below!
Edited on 02 November 2014 - 02:37 PM