Guest User

menu

a guest
Dec 13th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.35 KB | None | 0 0
  1. ---------------
  2. -- Variables --
  3. ---------------
  4.  
  5. local termWidth, termHeight = term.getSize ()
  6. local selectedItem = 1
  7. local inMainMenu = true
  8. local inLightsMenu = false
  9.  
  10. ---------------
  11. -- Functions --
  12. ---------------
  13.  
  14. function Choice1 ()
  15.  
  16.   term.clear ()
  17.   term.setCursorPos (1, 1)
  18.   term.write ("Hello, my name is "..os.getComputerLabel ())
  19.  
  20.   sleep (3)
  21.  
  22. end
  23.  
  24. function Choice2 ()
  25.  
  26.   inLightsMenu = true
  27.   selectedItem = 1
  28.  
  29.   while inLightsMenu do
  30.  
  31.     term.clear ()
  32.     term.setCursorPos (1, 1)
  33.    
  34.     printMenu (lightsMenu)
  35.  
  36.     event, key = os.pullEvent ("key")
  37.  
  38.     onKeyPressed (key, lightsMenu)
  39.    
  40.   end
  41.  
  42. end
  43.  
  44. function LightsOn ()
  45.  
  46.   inLightsMenu = false
  47.  
  48. end
  49.  
  50. function LightsOff ()
  51.  
  52.   inLightsMenu = false
  53.  
  54. end
  55.  
  56. function Exit ()
  57.  
  58.   inMainMenu = false
  59.  
  60. end
  61.  
  62. ---------------
  63. -- Main Menu --
  64. ---------------
  65.  
  66. local mainMenu = {
  67.  
  68.   [1] = {text = "Choice 1", handler = Choice1},
  69.   [2] = {text = "Choice 2", handler = Choice2},
  70.   [3] = {text = "Exit"    , handler = Exit},
  71.  
  72. }
  73.  
  74. local lightsMenu = {
  75.  
  76.   [1] = {text = "Lights On" , handler = LightsOn},
  77.   [2] = {text = "Lights Off", handler = LightsOff},
  78.  
  79. }
  80.  
  81. --DebugLine, says lightsMenu is not nil, but error thinks it's nil???
  82. if (lightsMenu == nil) then print ("True") else print ("Not nil")end
  83.  
  84. sleep (3)
  85.  
  86. --------------------
  87. -- Screen Refresh --
  88. --------------------
  89.  
  90. function printMenu (menu)
  91.  
  92.   for i = 1, #menu do
  93.  
  94.     if (i == selectedItem) then
  95.    
  96.       print ("> "..menu[i].text)
  97.      
  98.     else
  99.    
  100.       print ("  "..menu[i].text)
  101.    
  102.     end
  103.    
  104.   end
  105.  
  106. end
  107.  
  108. --------------
  109. -- Handlers --
  110. --------------
  111.  
  112. function onKeyPressed (key, menu)
  113.  
  114.   if (key == keys.enter) then
  115.  
  116.     onItemSelected (menu)
  117.  
  118.   elseif (key == keys.up and selectedItem > 1) then
  119.  
  120.     selectedItem = selectedItem - 1
  121.  
  122.   elseif (key == keys.down and selectedItem < #menu) then
  123.  
  124.     selectedItem = selectedItem + 1
  125.  
  126.   end
  127.  
  128. end
  129.  
  130. function onItemSelected (menu)
  131.  
  132.   menu[selectedItem].handler ()
  133.  
  134. end
  135.  
  136. -----------------
  137. -- Main Method --
  138. -----------------
  139.  
  140. function main ()
  141.  
  142.   while inMainMenu do
  143.  
  144.     term.clear ()
  145.     term.setCursorPos (1, 1)
  146.    
  147.     printMenu (mainMenu)
  148.    
  149.     event, key = os.pullEvent ("key")
  150.    
  151.     onKeyPressed (key, mainMenu)
  152.    
  153.   end
  154.  
  155. end
  156.  
  157. main ()
Advertisement
Add Comment
Please, Sign In to add comment