Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local bRunning = true
- local nScreenWidth, nScreenHeight = term.getSize()
- local nSelection = 1
- local tMainMenu = {
- [1] = { sTitle = "First Option", fAssociatedFunction = FirstOption },
- [2] = { sTitle = "Second Option", fAssociatedFunction = SecondOption },
- [3] = { sTitle = "Exit", fAssociatedFunction = function() bRunning = false end }
- }
- function PrintCentered( nHeight, sString )
- term.setCursorPos( nScreenWidth/2 - string.len( sString )/2, nHeight )
- term.write( sString )
- end
- function ClearScreen()
- term.clear()
- term.setCursorPos( 1, 1 )
- end
- function PrintMenu( tMenu, nStartHeight )
- for index = 1, #tMenu do
- if index == nSelection then
- PrintCentered( nStartHeight + ( index - 1 ), "[" .. tMenu[index].sTitle .. "]" )
- else
- PrintCentered( nStartHeight + ( index - 1 ), " " .. tMenu[index].sTitle .. " " )
- end
- end
- end
- function HandleKeyPress( nKey, tMenu )
- if nKey == 28 then
- tMenu[nSelection].fAssociatedFunction()
- elseif nKey == 200 and nSelection > 1 then
- nSelection = nSelection - 1
- elseif nKey == 208 and nSelection < 3 then
- nSelection = nSelection + 1
- end
- end
- function FirstOption()
- ClearScreen()
- PrintCentered( 5, "First option selected." )
- sleep( 2 )
- end
- function SecondOption()
- ClearScreen()
- PrintCentered( 5, "Second option selected." )
- sleep( 2 )
- end
- while bRunning do
- ClearScreen()
- PrintMenu( tMainMenu, 5, nSelect )
- local sEvent, nKey = os.pullEvent( "key" )
- HandleKeyPress( nKey, tMainMenu )
- end
Advertisement
Add Comment
Please, Sign In to add comment