Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local screenSelection = 1
- local screen = nil
- local prevX, prevY = nil
- local maxX, maxY = term.getSize()
- os.loadAPI("DefAPI")
- os.loadAPI("TableAPI")
- function drawScreen()
- isSelectionChanged()
- isSelctionSelected()
- resetScreen()
- term.setBackgroundColor(colors.white)
- for i = 1, 3, 1 do
- term.setCursorPos(1, i)
- term.clearLine()
- end
- Print(screen["title"], getCenterX(screen["title"]), 2)
- term.setBackgroundColor(colors.gray)
- --For each option of a screen
- if type(screen["static"]) == "table" then
- local line = 0
- if type(screen["options"]) == "table" then
- line = (getCenterY() - 3 - math.floor(TableAPI.tablelength(screen["static"])) / 4)
- else
- line = (getCenterY() - 3 - math.floor(TableAPI.tablelength(screen["static"])) / 2)
- end
- if line < 1 or line > maxY then
- screenToSmall()
- else
- for k, v in ipairs(screen["static"]) do
- Print(label, 1, line + 3)
- end
- end
- end
- if type(screen["options"]) == "table" then
- local line = 0
- if type(screen["static"]) == "table" then
- line = (getCenterY() - 3 - math.floor(TableAPI.tablelength(screen["options"])) / 4) + prevY + 1
- else
- line = (getCenterY() - 3 - math.floor(TableAPI.tablelength(screen["options"])) / 2)
- end
- if line < 1 or line > maxY then
- screenToSmall()
- else
- local curSel = 1
- for k, v in ipairs(screen["options"]) do
- local label = v["option"]
- if curSel == screenSelection then
- term.setTextColor(colors.white)
- Print("[ ", getCenterX("[ " .. label .. " ]"), line)
- term.setTextColor(colors.black)
- PrintInline(label, string.len("[ "), 0, true)
- term.setTextColor(colors.white)
- PrintInline(" ]", string.len(label), 0, true)
- term.setTextColor(colors.black)
- else
- Print(label, getCenterX(label), line)
- end
- line = line + 1
- curSel = curSel + 1
- end
- end
- end
- end
- function setScreen(T)
- screen = T
- end
- function getScreen()
- return screen
- end
- function getCenterX(text)
- return (maxX - string.len(text)) / 2
- end
- function screenToSmall()
- Print("SCREEN TO SMALL FOR OPTIONS", getCenterX("SCREEN TO SMALL FOR OPTIONS"), getCenterY())
- end
- function getCenterY()
- return maxY / 2
- end
- function resetScreen()
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- end
- function PrintInline(text, x, y)
- term.setCursorPos(prevX + x, prevY + y)
- term.write(text)
- prevX = prevX + x
- prevY = prevY + y
- end
- function Print(text, x, y)
- term.setCursorPos(x, y)
- term.clearLine()
- term.write(text)
- prevX = x
- prevY = y
- end
- function isSelectionChanged()
- if event == "key" and type(screen["options"]) == "table" then
- if p1 == keys.up then
- if screenSelection == 1 then
- screenSelection = TableAPI.tablelength(screen["options"])
- else
- screenSelection = screenSelection - 1
- end
- elseif p1 == keys.down then
- if screenSelection == TableAPI.tablelength(screen["options"]) then
- screenSelection = 1
- else
- screenSelection = screenSelection + 1
- end
- end
- end
- end
- function isSelctionSelected()
- if event == "key" and type(screen["options"]) == "table" then
- if p1 == keys.enter then
- TableAPI.tableAt(screen["options"], screenSelection - 1)["func"]()
- elseif p1 == keys.backspace then
- local back = TableAPI.tableAtKey(screen["options"], "option", "Back")
- if back then
- back["func"]()
- end
- end
- end
- end
- function resetSelection()
- screenSelection = 1
- end
- function GetDefAPI()
- return DefAPI
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement