Advertisement
SuPeRMiNoR3

SuperMenu

Dec 12th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.63 KB | None | 0 0
  1. local component = require("component")
  2. local term = require("term")
  3. local superlib = require("superlib")
  4. local keyboard = require("keyboard")
  5. local event = require("event")
  6.  
  7. if component.isAvailable("abstract_bus") == false then
  8.     error("This program requires an abstact bus card.")
  9. end
  10. ab = component.abstract_bus
  11. lastmenu = false
  12.  
  13. menu = {}
  14. menu[1] = {addr="Dehsetcro Atacpra Silulf", name="Nether"}
  15. menu[2] = {name="SuPeRMiNoR2's Base", addr="Sileston Uspraac Breigon"}
  16.  
  17. function dial(addr)
  18.     ab.send(0xFFFF, {action="dial", address=addr})
  19. end
  20.  
  21. function rendermenu(mt)
  22.     term.clear()
  23.     for i=1, #mt do
  24.         print(" "..i.."  ("..mt[i]["name"]..")")
  25.     end
  26. end
  27.  
  28. function updatemenu(mt, sel)
  29.     if lastmenu ~= false then
  30.         term.setCursor(1, lastmenu)
  31.         term.clearLine()
  32.         term.write(" "..lastmenu.."  ("..mt[lastmenu]["name"]..")")
  33.     end
  34.     term.setCursor(1, sel)
  35.     term.clearLine()
  36.     term.write("["..sel.."] ("..mt[sel]["name"]..")")
  37. end
  38.  
  39. function menuloop(mt)
  40.     rendermenu(mt)
  41.     sel = 1
  42.     updatemenu(mt, sel)
  43.  
  44.     while true do
  45.         e, r, t, key = event.pull("key_down")
  46.  
  47.         if key == keyboard.keys.down then
  48.             lastmenu = sel
  49.             sel = sel + 1
  50.             if sel > #menu then
  51.                 sel = 1
  52.             end
  53.         end
  54.         if key == keyboard.keys.up then
  55.             lastmenu = sel
  56.             sel = sel - 1
  57.             if sel < 1 then
  58.                 sel = #menu
  59.             end
  60.         end
  61.         if key == keyboard.keys.enter then
  62.             return mt[sel]["addr"]
  63.         end
  64.         if key == keyboard.keys.q then
  65.             return false
  66.         end
  67.  
  68.         updatemenu(mt, sel)
  69.  
  70.     end
  71. end
  72.  
  73. while true do
  74.     addr = menuloop(menu)
  75.     term.clear()
  76.     print("You selected "..addr)
  77.     if addr ~= false then
  78.         dial(addr)
  79.     end
  80.     os.sleep(0.5)
  81.  
  82. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement