Advertisement
ecoMeco

Computercraft Menu Demo

Jan 2nd, 2015
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.12 KB | None | 0 0
  1. local x, y = term.getSize()
  2. local sel = 1
  3.  
  4. menu = { "New", "Open", "Help", "About", "Quit"}
  5. mFuncs = {
  6.   function()
  7.     cls()
  8.     print("New file")
  9.   end,
  10.   function()
  11.     cls()
  12.     print("Open file.")
  13.   end,
  14.   function()
  15.     cls()
  16.     print("You probably got how this works. Or else you can't get on this text :)")
  17.   end,
  18.   function()
  19.     cls()
  20.     print("This is a menu demo!")
  21.     print("Made by Admicos!")
  22.   end,
  23.   function()
  24.     cls()
  25.     print("Goodbye!")
  26.   end
  27. }
  28.  
  29.  
  30. if term.isColor() then
  31.   highl = colors.yellow
  32. else
  33.   highl = colors.white
  34. end
  35.  
  36. function cls()
  37.   term.clear()
  38.   term.setCursorPos(1,1)
  39. end
  40.  
  41. function centerPr(text, ny)
  42. --  _x, _y = term.getSize()
  43.   term.setCursorPos(math.floor((x - #text) / 2), ny or y / 2)
  44.   print(text)
  45. end
  46.  
  47. function selector(n)
  48.   t = menu[n]
  49.    
  50.   term.setCursorPos(math.floor(((x - math.floor(#t / 2)) / 2) - 4), ((y / 2) + n) - 2)
  51.   term.setTextColor(highl)
  52.   print("[")
  53.   term.setCursorPos(math.floor(((x - math.floor(#t / 2)) / 2) + 4), ((y / 2) + n) - 2)
  54.   term.setTextColor(highl)
  55.   print("]")
  56. end
  57.  
  58. function clsel()
  59.   for n, t in ipairs(menu) do
  60.     term.setCursorPos(math.floor(((x - math.floor(#t / 2)) / 2) - 4), ((y / 2) + n) - 2)
  61.     print(" ")
  62.     term.setCursorPos(math.floor(((x - math.floor(#t / 2)) / 2) + 4), ((y / 2) + n) - 2)
  63.     print(" ")
  64.   end  
  65. end
  66.  
  67. function showMenu(title)
  68.   cls()
  69.  
  70.   term.setTextColor(highl)
  71.   centerPr(title, 2)
  72.  
  73.   term.setTextColor(colors.white)
  74.   for n, t in ipairs(menu) do
  75.     centerPr(t, ((y / 2) + n) - 2)
  76.   end
  77.  
  78.  --DEMO CODE
  79.  -- for n, t in ipairs(menu) do
  80.  --   clsel()
  81.  --   selector(n)
  82.  --   sleep(1)
  83.  -- end
  84. end
  85.  
  86. showMenu("Menu Demo")
  87. clsel()
  88. selector(sel)
  89.  
  90. while true do
  91.   local _, keyC = os.pullEvent("key")
  92.  
  93.   if keyC == 208 then
  94.     sel = sel + 1
  95.     if sel > #menu then
  96.       sel = #menu
  97.     end
  98.     clsel()
  99.     selector(sel)
  100.   end
  101.  
  102.   if keyC == 200 then
  103.     sel = sel - 1
  104.     if sel < 1 then
  105.       sel = 1
  106.     end  
  107.     clsel()
  108.     selector(sel)
  109.   end
  110.  
  111.   if keyC == 28 then
  112.     term.setTextColor(colors.white)
  113.     mFuncs[sel]()
  114.     break
  115.   end
  116. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement