Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("/rom/apis/colors")
- os.loadAPI("foodstuffs")
- _G.shell = shell
- --[[ Variables ]]--
- local offset = 0
- local sel = 1
- local printThanks = false
- local bgColour, textColour, selectedBgColour, selectedTextColour
- if term.isColour() then
- bgColour = colors.white
- textColour = colors.gray
- selectedBgColour = colors.lightBlue
- selectedTextColour = colors.white
- else
- bgColour = colors.white
- textColour = colors.black
- selectedBgColour = colors.black
- selectedTextColour = colors.white
- end
- local maxX, maxY = term.getSize()
- local running
- --[[ Functions ]]--
- clear = function()
- term.clear()
- term.setCursorPos(1,1)
- end
- local function centerWrite(txt)
- local x, y = term.getCursorPos()
- term.setCursorPos(math.floor(maxX/2-#tostring(txt)/2),y)
- write(txt)
- end
- local function redraw(tbl, sel, offset)
- term.setBackgroundColour(bgColour)
- clear()
- for i=1, maxY do
- if tbl[i] ~= nil then
- term.setCursorPos(1, i)
- if (i+offset) == sel then
- term.setBackgroundColour(selectedBgColour)
- term.clearLine()
- term.setTextColour(selectedTextColour)
- centerWrite("[ "..tostring(tbl[i + offset].text).." ]")
- else
- term.setBackgroundColour(bgColour)
- term.clearLine()
- term.setTextColour(textColour)
- centerWrite(tostring(tbl[i + offset].text))
- end
- end
- end
- end
- runMenu = function(tbl)
- offset = 0
- sel = 1
- running = true
- while running do
- term.setCursorBlink(false)
- os.queueEvent("")
- os.pullEvent()
- redraw(tbl, sel, offset)
- local ev = {os.pullEvent()}
- if ev[1] == "key" then
- if ev[2] == keys.up then
- if sel > 1 then sel = sel - 1 end
- if offset > 0 then offset = offset - 1 end
- elseif ev[2] == keys.down then
- if sel < #tbl then sel = sel + 1 end
- if offset < math.max(#tbl - maxY, 0) then offset = offset + 1 end
- elseif ev[2] == keys.enter then
- term.setBackgroundColour(colors.black)
- term.setTextColour(colors.white)
- clear()
- if type(tbl[sel].text) == "string" then
- if tbl[sel].text == "Add Food" then
- shell.run("addfood")
- elseif tbl[sel].text == "Add Recipe" then
- shell.run("createrecipe")
- elseif tbl[sel].text == "Update" then
- shell.run("update")
- elseif tbl[sel].text == "Lua" then
- shell.run("lua")
- elseif tbl[sel].text == "Search" then
- shell.run("search")
- elseif tbl[sel].text == "Back" then
- shell.run("startup")
- else
- tocraft = tbl[sel].text
- handle = fs.open("queue", "w")
- handle.write(tocraft)
- handle.close()
- shell.run("multifood "..tbl[sel].text)
- end
- else
- handle = fs.open("queue", "r")
- output = handle.readAll()
- handle.close()
- queuetable = {}
- queuetable.food = output
- queuetable.sNum = tbl[sel].text
- handle = fs.open("foodqueue", "w")
- handle.write(textutils.serialize(queuetable))
- handle.close()
- shell.run("multifood craft")
- end
- end
- elseif ev[1] == "mouse_scroll" then
- if ev[2] == -1 then
- if sel > 1 then sel = sel - 1 end
- if offset > 0 then offset = offset - 1 end
- elseif ev[2] == 1 then
- if sel < #tbl then sel = sel + 1 end
- if offset < math.max(#tbl - maxY, 0) then offset = offset + 1 end
- end
- elseif ev[1] == "mouse_click" then
- if tbl[(ev[4] + offset)] ~= nil then
- sel = ev[4] + offset
- redraw(tbl, sel, offset)
- sleep(.1)
- term.setBackgroundColour(colors.black)
- term.setTextColour(colors.white)
- clear()
- end
- end
- end
- end
- stopMenu = function()
- running = false
- term.setBackgroundColour(colors.black)
- if printThanks == true then
- if term.isColour() == true then
- term.setTextColour(colors.yellow)
- end
- clear()
- centerWrite("Thank you for using HD's menu api.")
- print("")
- else
- clear()
- end
- end
- listMethods = function()
- local tmp = {}
- for i,v in pairs(menuApi) do
- table.insert(tmp, i.."()")
- end
- textutils.pagedTabulate(tmp)
- local tmp = nil
- end
- local function isColour(color)
- if term.isColour() then
- if type(color) == "string" then
- if colors[color] ~= nil then
- return {true, colors[color]}
- else
- return false
- end
- elseif type(color) == "number" then
- if color >= 1 and color <= colors.black then
- return {true, color}
- else
- return false
- end
- else
- return false
- end
- else
- return false
- end
- end
- setBackgroundColour = function(color)
- local tmp = isColour(color)
- if tmp[1] then
- bgColour = tmp[2]
- end
- end
- setBarColour = function(color)
- local tmp = isColour(color)
- if tmp[1] then
- selectedBgColour = tmp[2]
- end
- end
- setTextColour = function(color)
- local tmp = isColour(color)
- if tmp[1] then
- textColour = tmp[2]
- end
- end
- setBarTextColour = function(color)
- local tmp = isColour(color)
- if tmp[1] then
- selectedTextColour = tmp[2]
- end
- end
- setBarTextColor = setBarTextColour
- setTextColor = setTextColour
- setBarColor = setBarColour
- setBackgroundColor = setBackgroundColour
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement