Advertisement
SirBaconBitz

Revised Menu

Jul 1st, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.00 KB | None | 0 0
  1. os.loadAPI("/rom/apis/colors")
  2. os.loadAPI("foodstuffs")
  3. _G.shell = shell
  4. --[[ Variables ]]--
  5. local offset = 0
  6. local sel = 1
  7. local printThanks = false
  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("[ "..tostring(tbl[i + offset].text).." ]")
  50.    else
  51.     term.setBackgroundColour(bgColour)
  52.     term.clearLine()
  53.     term.setTextColour(textColour)
  54.     centerWrite(tostring(tbl[i + offset].text))
  55.    end
  56.   end
  57.  end
  58. end
  59.  
  60. runMenu = function(tbl)
  61.  offset = 0
  62.  sel = 1
  63.  running = true
  64.  while running do
  65.   term.setCursorBlink(false)
  66.   os.queueEvent("")
  67.   os.pullEvent()
  68.   redraw(tbl, sel, offset)
  69.   local ev = {os.pullEvent()}
  70.   if ev[1] == "key" then
  71.    if ev[2] == keys.up then
  72.     if sel > 1 then sel = sel - 1 end
  73.     if offset > 0 then offset = offset - 1 end
  74.    elseif ev[2] == keys.down then
  75.     if sel < #tbl then sel = sel + 1 end
  76.     if offset < math.max(#tbl - maxY, 0) then offset = offset + 1 end
  77.    elseif ev[2] == keys.enter then
  78.     term.setBackgroundColour(colors.black)
  79.     term.setTextColour(colors.white)
  80.     clear()
  81.      if type(tbl[sel].text) == "string" then
  82.       if tbl[sel].text == "Add Food" then
  83.         shell.run("addfood")
  84.       elseif tbl[sel].text == "Add Recipe" then
  85.         shell.run("createrecipe")
  86.       elseif tbl[sel].text == "Update" then
  87.         shell.run("update")
  88.       elseif tbl[sel].text == "Lua" then
  89.         shell.run("lua")
  90.       elseif tbl[sel].text == "Search" then
  91.         shell.run("search")
  92.       elseif tbl[sel].text == "Back" then
  93.         shell.run("startup")
  94.       else
  95.         tocraft = tbl[sel].text
  96.         handle = fs.open("queue", "w")
  97.         handle.write(tocraft)
  98.         handle.close()
  99.         shell.run("multifood "..tbl[sel].text)
  100.       end
  101.      else
  102.       handle = fs.open("queue", "r")
  103.       output = handle.readAll()
  104.       handle.close()
  105.       queuetable = {}
  106.       queuetable.food = output
  107.       queuetable.sNum = tbl[sel].text
  108.       handle = fs.open("foodqueue", "w")
  109.       handle.write(textutils.serialize(queuetable))
  110.       handle.close()
  111.       shell.run("multifood craft")
  112.      end
  113.    end
  114.   elseif ev[1] == "mouse_scroll" then
  115.    if ev[2] == -1 then
  116.     if sel > 1 then sel = sel - 1 end
  117.     if offset > 0 then offset = offset - 1 end
  118.    elseif ev[2] == 1 then
  119.     if sel < #tbl then sel = sel + 1 end
  120.     if offset < math.max(#tbl - maxY, 0) then offset = offset + 1 end
  121.    end
  122.   elseif ev[1] == "mouse_click" then
  123.    if tbl[(ev[4] + offset)] ~= nil then
  124.     sel = ev[4] + offset
  125.     redraw(tbl, sel, offset)
  126.     sleep(.1)
  127.     term.setBackgroundColour(colors.black)
  128.     term.setTextColour(colors.white)
  129.     clear()
  130.    end
  131.   end
  132.  end
  133. end
  134.  
  135. stopMenu = function()
  136.  running = false
  137.  term.setBackgroundColour(colors.black)
  138.  if printThanks == true then
  139.   if term.isColour() == true then
  140.    term.setTextColour(colors.yellow)
  141.   end
  142.   clear()
  143.   centerWrite("Thank you for using HD's menu api.")
  144.   print("")
  145.  else
  146.   clear()
  147.  end
  148. end
  149.  
  150. listMethods = function()
  151.  local tmp = {}
  152.  for i,v in pairs(menuApi) do
  153.   table.insert(tmp, i.."()")
  154.  end
  155.  textutils.pagedTabulate(tmp)
  156.  local tmp = nil
  157. end
  158.  
  159. local function isColour(color)
  160.  if term.isColour() then
  161.   if type(color) == "string" then
  162.    if colors[color] ~= nil then
  163.     return {true, colors[color]}
  164.    else
  165.     return false
  166.    end
  167.   elseif type(color) == "number" then
  168.    if color >= 1 and color <= colors.black then
  169.     return {true, color}
  170.    else
  171.     return false
  172.    end
  173.   else
  174.    return false
  175.   end
  176.  else
  177.   return false
  178.  end
  179. end
  180.  
  181. setBackgroundColour = function(color)
  182.  local tmp = isColour(color)
  183.  if tmp[1] then
  184.   bgColour = tmp[2]
  185.  end
  186. end
  187.  
  188. setBarColour = function(color)
  189.  local tmp = isColour(color)
  190.  if tmp[1] then
  191.   selectedBgColour = tmp[2]
  192.  end
  193. end
  194.  
  195. setTextColour = function(color)
  196.  local tmp = isColour(color)
  197.  if tmp[1] then
  198.   textColour = tmp[2]
  199.  end
  200. end
  201.  
  202. setBarTextColour = function(color)
  203.  local tmp = isColour(color)
  204.  if tmp[1] then
  205.   selectedTextColour = tmp[2]
  206.  end
  207. end
  208.  
  209. setBarTextColor = setBarTextColour
  210. setTextColor = setTextColour
  211. setBarColor = setBarColour
  212. setBackgroundColor = setBackgroundColour
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement