Advertisement
SirBaconBitz

NumberMenu.lua

Jul 1st, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.01 KB | None | 0 0
  1. os.loadAPI("/rom/apis/colors")
  2. _G.shell = shell
  3. os.loadAPI("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("[ "..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. runMenu = function(tbl)
  61.  running = true
  62.  while running do
  63.   term.setCursorBlink(false)
  64.   os.queueEvent("")
  65.   os.pullEvent()
  66.   redraw(tbl, sel, offset)
  67.   local ev = {os.pullEvent()}
  68.   if ev[1] == "key" then
  69.    if ev[2] == keys.up then
  70.     if sel > 1 then sel = sel - 1 end
  71.     if offset > 0 then offset = offset - 1 end
  72.    elseif ev[2] == keys.down then
  73.     if sel < #tbl then sel = sel + 1 end
  74.     if offset < math.max(#tbl - maxY, 0) then offset = offset + 1 end
  75.    elseif ev[2] == keys.enter then
  76.     term.setBackgroundColour(colors.black)
  77.     term.setTextColour(colors.white)
  78.     clear()
  79.     foodstuffs.craft(tbl[sel].text)
  80.     end
  81.    end
  82.   elseif ev[1] == "mouse_scroll" then
  83.    if ev[2] == -1 then
  84.     if sel > 1 then sel = sel - 1 end
  85.     if offset > 0 then offset = offset - 1 end
  86.    elseif ev[2] == 1 then
  87.     if sel < #tbl then sel = sel + 1 end
  88.     if offset < math.max(#tbl - maxY, 0) then offset = offset + 1 end
  89.    end
  90.   elseif ev[1] == "mouse_click" then
  91.    if tbl[(ev[4] + offset)] ~= nil then
  92.     sel = ev[4] + offset
  93.     redraw(tbl, sel, offset)
  94.     sleep(.1)
  95.     term.setBackgroundColour(colors.black)
  96.     term.setTextColour(colors.white)
  97.     clear()
  98.     foodstuffs.craft(tbl[(ev[4] + offset)].text)
  99.    end
  100.   end
  101.  end
  102. end
  103.  
  104. stopMenu = function()
  105.  running = false
  106.  term.setBackgroundColour(colors.black)
  107.  if printThanks == true then
  108.   if term.isColour() == true then
  109.    term.setTextColour(colors.yellow)
  110.   end
  111.   clear()
  112.   centerWrite("Thank you for using HD's menu api.")
  113.   print("")
  114.  else
  115.   clear()
  116.  end
  117. end
  118.  
  119. listMethods = function()
  120.  local tmp = {}
  121.  for i,v in pairs(menuApi) do
  122.   table.insert(tmp, i.."()")
  123.  end
  124.  textutils.pagedTabulate(tmp)
  125.  local tmp = nil
  126. end
  127.  
  128. local function isColour(color)
  129.  if term.isColour() then
  130.   if type(color) == "string" then
  131.    if colors[color] ~= nil then
  132.     return {true, colors[color]}
  133.    else
  134.     return false
  135.    end
  136.   elseif type(color) == "number" then
  137.    if color >= 1 and color <= colors.black then
  138.     return {true, color}
  139.    else
  140.     return false
  141.    end
  142.   else
  143.    return false
  144.   end
  145.  else
  146.   return false
  147.  end
  148. end
  149.  
  150. setBackgroundColour = function(color)
  151.  local tmp = isColour(color)
  152.  if tmp[1] then
  153.   bgColour = tmp[2]
  154.  end
  155. end
  156.  
  157. setBarColour = function(color)
  158.  local tmp = isColour(color)
  159.  if tmp[1] then
  160.   selectedBgColour = tmp[2]
  161.  end
  162. end
  163.  
  164. setTextColour = function(color)
  165.  local tmp = isColour(color)
  166.  if tmp[1] then
  167.   textColour = tmp[2]
  168.  end
  169. end
  170.  
  171. setBarTextColour = function(color)
  172.  local tmp = isColour(color)
  173.  if tmp[1] then
  174.   selectedTextColour = tmp[2]
  175.  end
  176. end
  177.  
  178. setBarTextColor = setBarTextColour
  179. setTextColor = setTextColour
  180. setBarColor = setBarColour
  181. setBackgroundColor = setBackgroundColour
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement