masterdisasterHD

menuApi

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