Guest User

menu

a guest
Dec 11th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.30 KB | None | 0 0
  1. os.loadAPI("/rom/apis/colors")
  2.  
  3. --[[ Variables ]]--
  4. local offset = 0
  5. local sel = 1
  6. local printThanks = false
  7. local buffer = {}
  8.  
  9. local bgColour, textColour, selectedBgColour, selectedTextColour
  10.  
  11. if term.isColour() then
  12.  bgColour = colors.white
  13.  textColour = colors.gray
  14.  selectedBgColour = colors.lightBlue
  15.  selectedTextColour = colors.white
  16. else
  17.  bgColour = colors.white
  18.  textColour = colors.black
  19.  selectedBgColour = colors.black
  20.  selectedTextColour = colors.white
  21. end
  22.  
  23. local maxX, maxY = term.getSize()
  24. local running
  25.  
  26. --[[ Functions ]]--
  27.  
  28. clear = function()
  29.  term.clear()
  30.  term.setCursorPos(1,1)
  31. end
  32.  
  33. local function centerWrite(txt)
  34.  local x, y = term.getCursorPos()
  35.  term.setCursorPos(math.floor(maxX/2-#tostring(txt)/2),y)
  36.  write(txt)
  37. end
  38.  
  39. local function redraw(tbl, sel, offset)
  40.  term.setBackgroundColour(bgColour)
  41.  clear()
  42.  for i=1, maxY do
  43.   if tbl[i] ~= nil then
  44.    term.setCursorPos(1, i)
  45.    if (i+offset) == sel then
  46.     term.setBackgroundColour(selectedBgColour)
  47.     term.clearLine()
  48.     term.setTextColour(selectedTextColour)
  49.     centerWrite("[ "..tbl[i + offset].text.." ]")
  50.    else
  51.     term.setBackgroundColour(bgColour)
  52.     term.clearLine()
  53.     term.setTextColour(textColour)
  54.     centerWrite(tbl[i + offset].text)
  55.    end
  56.   end
  57.  end
  58. end
  59.  
  60. local function checkTable(tbl)
  61.  for i,v in ipairs(tbl) do
  62.   if v.handler == nil or type(v.handler) ~= "function" then
  63.    if term.isColour() then
  64.     term.setTextColour(colors.red)
  65.    end
  66.    print("Menu item \""..i.."\" has no valid handler!")
  67.    local txt = textutils.serialize(tostring(v.handler))
  68.    error("handler = "..txt, 0)
  69.   elseif v.text == nil then
  70.    if term.isColour() then
  71.     term.setTextColour(colors.red)
  72.    end
  73.    print("Menu item \""..i.."\" has no text!")
  74.    local txt = textutils.serialize(tostring(v.text))
  75.    error("text = "..txt, 0)
  76.   end
  77.  end
  78. end
  79.  
  80. runMenu = function(tbl)
  81.  if type(tbl) ~= "table" then
  82.   error("Invalid arguments!\nUsage: menuApi.runMenu(menu_table)", 0)
  83.  elseif #tbl < 2 then
  84.   error("Not enough items in menu!\nAt least 2 items are required!", 0)
  85.  end
  86.  
  87.  checkTable(tbl)
  88.  
  89.  running = true
  90.  while running do
  91.   if sel > #tbl then sel = #tbl end
  92.   term.setCursorBlink(false)
  93.   os.queueEvent("")
  94.   os.pullEvent()
  95.   redraw(tbl, sel, offset)
  96.   local ev = {os.pullEvent()}
  97.   if ev[1] == "key" then
  98.    if ev[2] == keys.up then
  99.     if sel > 1 then sel = sel - 1 end
  100.     if offset > 0 then offset = offset - 1 end
  101.    elseif ev[2] == keys.down then
  102.     if sel < #tbl then sel = sel + 1 end
  103.     if offset < math.max(#tbl - maxY, 0) then offset = offset + 1 end
  104.    elseif ev[2] == keys.enter then
  105.     term.setBackgroundColour(colors.black)
  106.     term.setTextColour(colors.white)
  107.     clear()
  108.     tbl[sel].handler()  
  109.    end
  110.   elseif ev[1] == "mouse_scroll" then
  111.    if ev[2] == -1 then
  112.     if sel > 1 then sel = sel - 1 end
  113.     if offset > 0 then offset = offset - 1 end
  114.    elseif ev[2] == 1 then
  115.     if sel < #tbl then sel = sel + 1 end
  116.     if offset < math.max(#tbl - maxY, 0) then offset = offset + 1 end
  117.    end
  118.   elseif ev[1] == "mouse_click" then
  119.    if tbl[(ev[4] + offset)] ~= nil then
  120.     sel = ev[4] + offset
  121.     redraw(tbl, sel, offset)
  122.     sleep(.1)
  123.     term.setBackgroundColour(colors.black)
  124.     term.setTextColour(colors.white)
  125.     clear()
  126.     tbl[(ev[4] + offset)].handler()
  127.    end
  128.   end
  129.  end
  130. end
  131.  
  132. stopMenu = function()
  133.  running = false
  134.  term.setBackgroundColour(colors.black)
  135.  if printThanks == true then
  136.   if term.isColour() == true then
  137.    term.setTextColour(colors.yellow)
  138.   end
  139.   clear()
  140.   centerWrite("Thank you for using HD's menu api.")
  141.   print("")
  142.  else
  143.   clear()
  144.  end
  145. end
  146.  
  147. listMethods = function()
  148.  local tmp = {}
  149.  for i,v in pairs(menu) do
  150.   table.insert(tmp, i.."()")
  151.  end
  152.  textutils.pagedTabulate(tmp)
  153.  local tmp = nil
  154. end
  155.  
  156. local function isColour(color)
  157.  if term.isColour() then
  158.   if type(color) == "string" then
  159.    if colors[color] ~= nil then
  160.     return {true, colors[color]}
  161.    else
  162.     return false
  163.    end
  164.   elseif type(color) == "number" then
  165.    if color >= 1 and color <= colors.black then
  166.     return {true, color}
  167.    else
  168.     return false
  169.    end
  170.   else
  171.    return false
  172.   end
  173.  else
  174.   return false
  175.  end
  176. end
  177.  
  178. setBackgroundColour = function(color)
  179.  local tmp = isColour(color)
  180.  if tmp[1] then
  181.   bgColour = tmp[2]
  182.  end
  183. end
  184.  
  185. setBarColour = function(color)
  186.  local tmp = isColour(color)
  187.  if tmp[1] then
  188.   selectedBgColour = tmp[2]
  189.  end
  190. end
  191.  
  192. setTextColour = function(color)
  193.  local tmp = isColour(color)
  194.  if tmp[1] then
  195.   textColour = tmp[2]
  196.  end
  197. end
  198.  
  199. setBarTextColour = function(color)
  200.  local tmp = isColour(color)
  201.  if tmp[1] then
  202.   selectedTextColour = tmp[2]
  203.  end
  204. end
  205.  
  206. setBarTextColor = setBarTextColour
  207. setTextColor = setTextColour
  208. setBarColor = setBarColour
  209. setBackgroundColor = setBackgroundColour
  210.  
  211. saveMenu = function(tbl, name)
  212.  if tbl == nil or type(tbl) ~= "table" then
  213.   error("Argument 1 must be table!", 0)
  214.  elseif name == nil or type(name) ~= "string" then
  215.   error("Argument 2 must be string!")
  216.  end
  217.  
  218.  buffer[name] = tbl
  219. end
  220.  
  221. loadMenu = function(name)
  222.  if name == nil or type(name) ~= "string" then
  223.   error("Argument invalid!\nname is not a string or nil!")
  224.  end
  225.  
  226.  if buffer[name] == nil then
  227.   error("Menu '" .. name .. "' does not exist!", 0)
  228.  else
  229.   runMenu(buffer[name])
  230.  end
  231. end
Advertisement
Add Comment
Please, Sign In to add comment