Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local bolDebug = true
- --[[ termMenu
- API used for displaying menus on the terminal (or monitor)
- Uses the same menu structure as gbMenu
- Also has some other useful term functions
- --gezepi
- ]]
- print("termMenu")
- local width, height --Size of text object
- local _width, _height --SIze of montior
- local inputTimeout = 10
- local xO = 1 --X offset for printedt text
- local yO = 1 --Y offset for printed text
- local textOnScreen = {}
- local files
- local mon = nil
- local bug
- function init(nFiles)
- files = nFiles
- os.loadAPI(files.tools)
- tools.init(files)
- bug = tools.bug(bolDebug)
- tools.unload()
- end
- --[[ Initializes the API. If given 'term' for newmon it will display on main screen of turtle or computer ]]
- function new(newX, newY, newW, newH, newmon)
- xO = newX
- yO = newY
- mon = newmon
- _width, _height = mon.getSize()
- width = newW
- height = newH
- end
- --[[ Returns what x coord to start printing the given string so it ends up centered ]]
- function centerX(s)
- if tonumber(s)==nil then s = string.len(s) end
- return ((width+1) / 2) - (s / 2)
- end
- --[[ Returns the center of the area ]]
- function centerY(s)
- if not tonumber(s) then s = string.len(s) end
- return math.ceil((height+1) / 2) - s/2
- end
- --[[ Like term.write but at the given coords ]]
- function addText(x,y,s)
- --print("x:",x,"y:",y,"s:",s)
- x=xO+x-1 ; y=yO+y-1
- mon.setCursorPos(x,y)
- mon.write(s)
- end
- --[[ Like addText but blanks the rest of the line ]]
- function addLine(y,s)
- y = yO+y
- mon.setCursorPos(xO,y)
- mon.write(string.rep(" ", width))
- addText(xO,y,s)
- end
- --[[ Clears the entire area ]]
- function clearText()
- for i=0,height do
- addLine(i,"")
- end
- end
- --[[ Clears a box area ]]
- function clearBox(x, y, w, h)
- for i=0, h do
- addText(x, y+i, string.rep(" ", w))
- end
- end
- --[[ Prints the str centered on the row ]]
- function printCenterX(row, str)
- row = row or 1
- str = str or "Nil string"
- addText(centerX(str), row, str)
- end
- --[[ Prints str centered vertically beginning at col ]]
- function printCenterY(col, str)
- addText(col, centerY(), str)
- end
- --[[ Prints str centered on screen ]]
- function printCenter(str)
- addText(center(str), centerY(), str)
- end
- --[[ Draws a box. Blanks everything inside of it ]]
- function drawBox(x, y, w, h)
- --x=xO+x ; y=yO+y
- if w<=1 then w = width * w end
- if h<=1 then h = height * h end
- addText(x,y,"+"..string.rep("-", w-2).."+")
- for i=1,h-1 do
- addText(x,y+i,"|"..string.rep(" ", w-2).."|")
- end
- addText(x,y+h,"+"..string.rep("-", w-2).."+")
- end
- --[[ Draws a UI and prompts user for a number ]]
- --[[function getNumber(maximum)
- drawBox(centerX(12), centerY()-2, 12, 4)
- addText(centerX("How many?"), centerY()-1,"How many?")
- local s = "(1-"..maximum..")"
- addText(centerX(s),centerY(),s)
- addText(centerX(1)-1, centerY()+1,"")
- num = io.read()
- if num=="" then return nil end
- num =tonumber(num)
- if num==nil or num>maximum then
- return getNumber(maximum)
- end
- return num
- end--]]
- --[[ Prints a named box ]]
- function printDialog(s, w, h)
- s = s or "Dialog"
- w = w or string.len(s) + 4
- h = h or 5
- drawBox(centerX(w), centerY(h), w, h)
- addText(centerX(string.len(s)+2), centerY(h), "+"..s.."+")
- return centerX(w), centerY(h), w, h
- end
- --[[ Shrinks a table so it only contains entries with filString in them ]]
- local function filterMenu(menu, filString)
- retM = {}
- retM.name = menu.name
- retM.selections = {}
- local i = 1
- for k,v in pairs(menu.selections) do
- if filString~="" and string.lower(v.name)==string.lower(filString) then
- retM.selections = {}
- retM.selections[1] = v
- return retM
- end
- if string.find(string.lower(v.name), string.lower(filString))~=nil then
- retM.selections[i] = v
- i = i + 1
- end
- end
- return retM
- end
- --[[ Prompts user for a boolean ]]
- function getInputBoolean(name, hVal, lVal)
- lVal = lVal or "false"
- hVal = hVal or "true"
- local w
- if string.len("*"..hVal.." "..lVal) > string.len(name) then w = string.len("*"..hVal.." "..lVal)+2 end
- local dx, dy, dw, dh = printDialog(name or "Boolean", w, 2)
- addText(dx+1, dy+1, "*"..hVal.." "..lVal)
- local choice = true
- while true do
- local e, p1, p2, p3 = os.pullEvent("key")
- p1 = keys.getName(p1)
- if p1=="left" and not choice then
- addText(dx+1, dy+1, "*"..hVal.." "..lVal)
- choice = true
- elseif p1=="right" and choice then
- addText(dx+1, dy+1, " "..hVal.." *"..lVal)
- choice = false
- elseif p1=="enter" then
- clearBox(dx, dy, dw, dh)
- return choice
- end
- end
- end
- --[[ Sets the cursor position within the alloted space ]]
- function setCursor(nX, nY)
- mon.setCursorPos(xO+nX-1, yO+nY-1)
- end
- --[[ Prompts user for a number ]]
- function getInputNum(name,minimum,maximum, err)
- local range = "From "
- if minimum then range = range..minimum else range = range.."-inf" end
- if maximum then range = range.." to "..maximum else range = range.." to inf" end
- local w
- if string.len(range)>string.len(name) then w = string.len(range)+2 end
- local dx, dy, dw, dh = printDialog((name or "Number")..(err or ""), w, 3)
- if range then termMenu.printCenterX(dy+1, range) end
- addText(dx+1, dy+2, "> ")
- setCursor(dx+2, dy+2)
- local num = io.read()
- num = tonumber(num)
- if num~=nil then
- if maximum and minimum and num <= maximum and num >= minimum then
- clearBox(dx, dy, dw, dh)
- return num
- end
- if maximum and not minimum and num <= maximum then clearText() ; return num end
- if minimum and not maximum and num >= minimum then clearText() ; return num end
- if not minimum and not maximum then clearText() ; return num end
- return getInputNum(name,minimum, maximum, "*")
- end
- clearBox(dx, dy, dw, dh)
- return nil
- end
- --[[ Prompts the user for a string ]]
- function getInputString(name)
- local dx, dy, dw, dh = printDialog(name or "String", nil, 2)
- addText(dx+1, dy+1, "> ")
- setCursor(dx+2, dy+1)
- local s = io.read()
- clearBox(dx,dy,dw,dh)
- return s
- end
- --[[ Displays a menu to the user ]]
- function dispMenu(x,y,thing,sSearch)
- clearText()
- drawBox(1,1,width,height)
- if thing == nil then error("Passed a nil menu") end
- if sSearch == nil then sSearch = "" end
- wholeMenu = thing
- thing = filterMenu(thing, sSearch)
- --bug("Menu:"..thing.name)
- local n = "+"..thing.name.."+"
- addText(centerX(n),y,n)
- for k,v in pairs(thing.selections) do
- addText(x,y+k," "..v.name)
- if k > height-2 then
- break
- end
- end
- local choice
- if #thing.selections == -1 then
- --choice = thing.selections[1]
- else
- local function dispInput(s)
- mon.setCursorPos(xO+1,yO+height)
- mon.write(">"..s)
- end
- local cursorPos = 1
- dispInput(sSearch)
- local function cursorMove(dY)
- mon.setCursorPos(xO+x-1,yO+cursorPos)
- mon.write(" ")
- cursorPos = cursorPos + dY
- if cursorPos > height-1 then cursorPos = height-1 end
- if cursorPos > #thing.selections then cursorPos = #thing.selections end
- if cursorPos < 1 then cursorPos = 1 end
- mon.setCursorPos(xO+x-1,yO+cursorPos)
- mon.write("-")
- end
- cursorMove(0)
- while 1 do
- local e,p1,p2 = os.pullEvent()
- if e=="char" then --Alphabet input
- sSearch = sSearch..p1
- return dispMenu(x, y,wholeMenu, sSearch)
- elseif e=="key" then --Arrows/Enter input
- if p1==28 then --Enter
- if cursorPos > #thing.selections then
- choice = nil
- else
- choice = thing.selections[cursorPos]
- end
- break --User chose something, exit
- elseif p1==208 then --Down
- cursorMove(1)
- elseif p1==200 then --Up
- cursorMove(-1)
- elseif p1==203 then --Left
- choice = nil
- break
- elseif p1==14 then --Backspace
- sSearch = string.sub(sSearch, 0,string.len(sSearch)-1)
- return dispMenu(x, y,wholeMenu, sSearch)
- end--keys
- end--char / key
- end--input while
- end
- if choice ~= nil then
- clearText()
- if choice.data==nil then return {name=choice.name, data=choice.name} end
- if type(choice.data)=="function" then
- choice.data()
- end
- return choice
- else
- clearText()
- return nil
- end
- end
- --[[ Test function for boxes, etc ]]
- function test()
- --mon.clear()
- mon.setCursorPos(xO,yO)
- mon.write("+")
- mon.setCursorPos(xO,yO)
- for i=10,1,-1 do
- clearText()
- drawBox(1,1,i/10,i/10)
- sleep(.1)
- end
- for i=1,10 do
- clearText()
- drawBox(1,1,i/10,i/10)
- sleep(.1)
- end
- --addLine(4, "hello world")
- local s = "hello world"
- addText(centerX(s), centerY(),s)
- mon.setCursorPos(1,_height)
- end
- --[[ Test menu ]]
- function testMenu()
- test = {
- name="Test",
- selections={
- {name="First thing", data = {0,0}},
- {name="Second thing", data={-1,0}},
- {name="Third thing", data={-1,0}},
- {name="Fourth thing"},
- {name="Fifth thing"},
- {name="Sixth thing"},
- {name="Seventh thing"},
- }
- }
- c = dispMenu(2,1,test)
- if c==nil then c = "nothing" else c = c.name end
- print("Chose: "..c)
- mon.setCursorPos(1,_height)
- end
Advertisement
Add Comment
Please, Sign In to add comment