Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- locals
- local logs = {}
- local openTime = 2
- local textSize = 1
- local minimumNameLength = 4
- local buttonPadding = 1 --Min 1, Space before/after
- local centerPadding = 0 --Min 0, Space before/after
- local buttonColor = colors.blue
- local buttonActiveColor = colors.gray
- local pageBack = "Prev Page"
- local pageFore = "Next Page"
- local resetButton = "Refresh"
- -- Hub Portals
- local portals = {
- { name="Spawn", x="238", y="139", z="-3038"},
- { name="BloodMagic", x="-729", y="66", z="4116"},
- { name="Doomgull", x="-3629", y="65", z="-1070"},
- { name="DLI/Kelec", x="4569", y="90", z="-538"},
- { name="Blue", x="-619", y="64", z="1055"}
- }
- --internal
- local monitor, touch
- local transporter
- local monitorLocation
- local monX = 0
- local monY = 0
- local monMidL = 0
- local monMidR = 0
- local maxNameLength = 0
- local problem = false
- local names = {}
- local curPage = 1
- local lastPage = 1
- local namesPerPage = 1
- local pages = {}
- local transType = 'mo_transporter'
- --transporter = peripheral.find(transType)
- transporter = peripheral.wrap("mo_transporter_0")
- function writeLog(text)
- print(text)
- end
- function init()
- if not fs.exists('apis') then
- fs.makeDir('apis')
- end
- if not fs.exists('apis/touchpoint') then
- writeLog("You don't have the touchpoint API. Beaming it down.")
- shell.run('pastebin get pFHeia96 apis/touchpoint')
- end
- os.loadAPI('apis/touchpoint')
- if not fs.exists('apis/json') then
- writeLog("You don't have the JSON API. Grabboring it.")
- shell.run("pastebin get 4nRg9CHU apis/json")
- end
- os.loadAPI('apis/json')
- monitorLocation = findSideFor('monitor')
- monitor = peripheral.wrap(monitorLocation)
- setupScreen()
- end
- function handleMonitorResize()
- writeLog("Screen resized. Setting up screen!")
- setupScreen()
- refresh()
- end
- function determineLength(myString)
- local theLength = 0
- if string.len(myString) > maxNameLength then
- theLength = maxNameLength + buttonPadding * 2
- else
- theLength = string.len(myString) + buttonPadding * 2
- end
- return theLength
- end
- function setupScreen()
- monitor.clear()
- monitor.setTextScale(textSize)
- monX, monY = monitor.getSize()
- monitor.setTextColor(colors.orange)
- writeLog("Screen Dimensions: (x: "..monX..", y: "..monY..")")
- if (monY < 5) or (monX < (minimumNameLength * 2) + 4) then
- monitor.setBackgroundColor(colors.red)
- monitor.setTextColor(colors.black)
- centerText("!!!", 3)
- centerText("ERROR", 4)
- centerText("VIEW COMPUTER", 5)
- writeLog("Screen too small with current resolution, please adjust.")
- problem = true
- else
- if math.floor(monX/2) == math.ceil(monX/2) then --Even
- monMidR = monX/2 + 1
- monMidL = monMidR - 1
- maxNameLength = (monX - 4)/2 - (centerPadding + buttonPadding) * 2
- else --Odd
- monMidR = math.floor(monX/2) + 1
- monMidL = monMidR
- maxNameLength = (monX - 3)/2 - (centerPadding + buttonPadding) * 2
- end
- namesPerPage = math.ceil((monY - 4) / 2) * 2
- end
- end
- function rebuildPages(p1)
- pages[curPage]:toggleButton(resetButton)
- refresh()
- end
- function doAction(buttonName)
- local pressedButtonDesc = 0;
- writeLog("doAction("..buttonName..")")
- for k,v in ipairs(portals) do
- if v["name"] == buttonName then
- pressedButtonDesc = v
- break
- end
- end
- --writeLog("array variable = "..pressedButtonDesc["x"])
- startTransport(pressedButtonDesc["x"],pressedButtonDesc["y"],pressedButtonDesc["z"])
- pages[curPage]:flash(buttonName, openTime)
- end
- function prevPage()
- lastPage = curPage
- pages[lastPage]:flash(pageBack, 0.125)
- curPage = curPage - 1
- if curPage < 1 then
- curPage = #pages
- end
- pages[curPage]:draw()
- end
- function nextPage()
- lastPage = curPage
- pages[lastPage]:flash(pageFore, 0.125)
- curPage = curPage + 1
- if curPage > #pages then
- curPage = 1
- end
- pages[curPage]:draw()
- end
- function displayError(error)
- monitor.clear()
- monitor.setBackgroundColor(colors.red)
- monitor.setTextColor(colors.black)
- centerText("!!!", 3)
- centerText(error, 4)
- writeLog("Error: "..error)
- end
- function buildPage(pN)
- pages[pN] = touchpoint.new(monitorLocation)
- pages[pN]:add(pageBack, prevPage, 1, 1, determineLength(pageBack), 1, buttonColor, buttonActiveColor)
- pages[pN]:add(pageFore, nextPage, monX - determineLength(pageFore) + 1, 1, monX, 1, buttonColor, buttonActiveColor)
- if #portals > 0 then
- local curIndex = (pN - 1) * namesPerPage + 1
- for y = 3, monY - 2, 2 do
- if curIndex < #portals + 1 then
- pages[pN]:add(portals[curIndex]["name"], doAction, 2, y, monMidL - 1 - centerPadding, y, buttonColor, buttonActiveColor)
- else
- break
- end
- curIndex = curIndex + 1
- if curIndex < #portals + 1 then
- pages[pN]:add(portals[curIndex]["name"], doAction, monMidR + 1 + centerPadding, y, monX - 1, y, buttonColor, buttonActiveColor)
- else
- break
- end
- curIndex = curIndex + 1
- end
- end
- pages[pN]:add(resetButton, rebuildPages, 1, monY, determineLength(resetButton), monY, buttonColor, buttonActiveColor)
- end
- function refresh()
- writeLog("Refreshing Destination list.")
- pages = {}
- local myPage = 1
- writeLog("# Detinations: "..#portals)
- if #portals> 0 then
- writeLog('Names/Page: '..namesPerPage)
- while (myPage - 1) * namesPerPage < #portals do
- writeLog('Populating Page'..myPage..'.')
- buildPage(myPage)
- myPage = myPage + 1
- end
- else
- buildPage(1)
- end
- if curPage > #pages then
- curPage = #pages
- end
- writeLog('Current Page:'..curPage)
- pages[curPage]:draw()
- end
- function startTransport(Tx,Ty,Tz)
- writeLog('----')
- writeLog("Setting Transporter Address X:"..Tx.." Y:"..Ty.." Z:"..Tz)
- transporter.setX(0,tonumber(Tx))
- transporter.setY(0,tonumber(Ty))
- transporter.setZ(0,tonumber(Tz))
- end
- function centerText(text, row)
- local size = string.len(text)
- offset = math.floor((monX - size) / 2) + 1
- monitor.setCursorPos(offset, row)
- monitor.write(text)
- end
- function findSideFor(typeToFind)
- local sides = peripheral.getNames()
- for i, side in ipairs(sides) do
- local type = peripheral.getType(side)
- if type == typeToFind then
- return(side)
- end
- end
- end
- init()
- if not problem then
- refresh()
- while true do
- event, p1 = pages[curPage]:handleEvents(os.pullEvent())
- if event == "button_click" then
- pages[curPage].buttonList[p1].func(p1)
- elseif event == "monitor_resize" then
- handleMonitorResize()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement