Guest User

MenuByGrimReaper

a guest
Sep 15th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. local bRunning = true
  2. local nScreenWidth, nScreenHeight = term.getSize()
  3. local nSelection = 1
  4. local tMainMenu = {
  5. [1] = { sTitle = "First Option", fAssociatedFunction = FirstOption },
  6. [2] = { sTitle = "Second Option", fAssociatedFunction = SecondOption },
  7. [3] = { sTitle = "Exit", fAssociatedFunction = function() bRunning = false end }
  8. }
  9. function PrintCentered( nHeight, sString )
  10. term.setCursorPos( nScreenWidth/2 - string.len( sString )/2, nHeight )
  11. term.write( sString )
  12. end
  13. function ClearScreen()
  14. term.clear()
  15. term.setCursorPos( 1, 1 )
  16. end
  17. function PrintMenu( tMenu, nStartHeight )
  18. for index = 1, #tMenu do
  19. if index == nSelection then
  20. PrintCentered( nStartHeight + ( index - 1 ), "[" .. tMenu[index].sTitle .. "]" )
  21. else
  22. PrintCentered( nStartHeight + ( index - 1 ), " " .. tMenu[index].sTitle .. " " )
  23. end
  24. end
  25. end
  26. function HandleKeyPress( nKey, tMenu )
  27. if nKey == 28 then
  28. tMenu[nSelection].fAssociatedFunction()
  29. elseif nKey == 200 and nSelection > 1 then
  30. nSelection = nSelection - 1
  31. elseif nKey == 208 and nSelection < 3 then
  32. nSelection = nSelection + 1
  33. end
  34. end
  35. function FirstOption()
  36. ClearScreen()
  37. PrintCentered( 5, "First option selected." )
  38. sleep( 2 )
  39. end
  40. function SecondOption()
  41. ClearScreen()
  42. PrintCentered( 5, "Second option selected." )
  43. sleep( 2 )
  44. end
  45.  
  46. while bRunning do
  47. ClearScreen()
  48. PrintMenu( tMainMenu, 5, nSelect )
  49.  
  50. local sEvent, nKey = os.pullEvent( "key" )
  51. HandleKeyPress( nKey, tMainMenu )
  52. end
Advertisement
Add Comment
Please, Sign In to add comment