Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local debugMode = false
- local slideShowLoc = "disk/slides.txt"
- --Utility functions-\/-----------------------------------------------------------------------------
- function getPeripheral(periph)
- sides = {"left","right","front","back","top","bottom"}
- for i, v in ipairs(sides) do
- if peripheral.isPresent(v) then
- if peripheral.getType(v) == periph then
- return peripheral.wrap(v)
- end
- end
- end
- return nil
- end
- function slowWriteAt(x,y,str,speed)
- speed = speed or 60
- term.setCursorPos(x ,y)
- debug("Slow write at x: "..x)
- textutils.slowWrite(str,speed)
- end
- function writeAt(x,y,str)
- local oldx, oldy = term.getCursorPos()
- term.setCursorPos(x ,y);
- term.write(str);
- term.setCursorPos(oldx, oldy)
- end
- function drawAllBoxes(boxes)
- for i, v in ipairs(boxes) do
- v:draw()
- end
- end
- function writeAllText(texts, slow)
- for i, v in ipairs(texts) do
- if not slow then
- v:write()
- else
- v:slowWrite()
- end
- end
- end
- function pulseOutput(sColour, n)
- for i = 1, n do
- rs.setBundledOutput("bottom", colors.combine(rs.getBundledOutput("bottom"), sColour))
- sleep(0.45)
- rs.setBundledOutput("bottom", colors.subtract(rs.getBundledOutput("bottom"), sColour))
- sleep(0.45)
- end
- end
- local loadingChars = {"/","|","\\","-"}
- local loadingPos = {x = 0, y = 0,active = false}
- function showLoading()
- local i = 1
- if loadingPos.active then
- error("Something is already loading")
- end
- loadingPos.active = true
- if loadingPos.x < 1 or loadingPos.y < 1 then
- error("loadingPos not set")
- end
- repeat
- term.setCursorPos(loadingPos.x, loadingPos.y)
- print(loadingChars[i])
- i = (i < #loadingChars) and i + 1 or 1
- sleep(0.2)
- until not loadingPos.active
- term.setCursorPos(loadingPos.x, loadingPos.y)
- print(" ")
- loadingPos.x, loadingPos.y = 0, 0
- end
- function stopLoading()
- loadingPos.active = false
- end
- function debug(val)
- if debugMode then
- oldx, oldy = term.getCursorPos()
- term.restore()
- --term.setCursorPos(6, consoleHeight)
- print(val)
- --sleep(3)
- term.redirect(monitor)
- term.setCursorPos(oldx, oldy)
- end
- end
- --Utility functions-/\-----------------------------------------------------------------------------
- --Class constructors-\/----------------------------------------------------------------------------
- function newBox(x, y, width, height)
- --creates a box with drawing and erasing methods
- local function drawBox(box)
- --renders a box onscreen at correct location and size, accepts box "class" as argument
- local hChar = "|"
- local vChar = "-"
- local cornerChar = "O"
- local currentChar = ""
- if not box.x or not box.y or not box.width or not box.height then
- --exit without attempting to render the box
- return 0
- end
- --print("y is "..y)
- for i = box.x, x + box.width do
- if i == x or i == x + width then
- currentChar = cornerChar
- else
- currentChar = vChar
- end
- writeAt(i, y, currentChar)
- writeAt(i, y + height, currentChar)
- end
- for i = y + 1, y + box.height - 1 do
- currentChar = hChar
- writeAt(x, i, currentChar)
- writeAt(x + width, i, currentChar)
- end
- end
- local function eraseBox(box)
- --clears the area that box sits in. Can be used to create a box in negative space
- if not box.x or not box.y or not box.width or not box.height then
- --exit without attempting to render the box
- return 0
- end
- for i = box.x, x + box.width do
- writeAt(i, y, "")
- writeAt(i, y + height, "")
- end
- for i = y + 1, y + box.hight - 1 do
- writeAt(x, i, "")
- writeAt(x + width, i, "")
- end
- end
- local box = {x = x, y = y, width = width, height = height, draw = drawBox, erase = eraseBox}
- return box
- end
- function newText(string, x, y, alignment, renderImmediate)
- local function renderText(text)
- --renders text onscreen at correct alignment, accepts text "class" as argument
- text.alignment = text.alignment or "left"
- if text.alignment == "left" then
- writeAt(text.x,text.y,text.string)
- elseif alignment == "centre" then
- writeAt(text.x - #text.string * 0.5, text.y, text.string)
- elseif alignment == "right" then
- writeAt(text.x - #text.string, text.y, text.string)
- else
- error(text.alignment..' alignment for string "'..text.string..'" is not a valid alignment')
- end
- end
- local function slowRenderText(text,speed)
- --slow renders text onscreen at correct alignment, accepts text "class" as argument
- text.alignment = text.alignment or "left"
- if text.alignment == "left" then
- slowWriteAt(text.x,text.y,text.string, speed)
- elseif alignment == "centre" then
- slowWriteAt(text.x - #text.string * 0.5, text.y, text.string, speed)
- elseif alignment == "right" then
- slowWriteAt(text.x - #text.string, text.y, text.string, speed)
- else
- error(text.alignment..' alignment for string "'..text.string..'" is not a valid alignment')
- end
- end
- local function deleteText(text)
- text.alignment = text.alignment or "left"
- text.string = string.rep(" ", #text.string)
- if text.alignment == "left" then
- writeAt(text.x,text.y,text.string)
- elseif alignment == "centre" then
- writeAt(text.x - #text.string * 0.5, text.y, text.string)
- elseif alignment == "right" then
- writeAt(text.x - #text.string, text.y, text.string)
- else
- error(text.alignment..' alignment for string "'..text.string..'" is not a valid alignment')
- end
- end
- local function slowDeleteText(text)
- text.alignment = text.alignment or "left"
- text.string = string.rep(" ", #text.string)
- if text.alignment == "left" then
- slowWriteAt(text.x,text.y,text.string)
- elseif alignment == "centre" then
- slowWriteAt(text.x - #text.string * 0.5, text.y, text.string)
- elseif alignment == "right" then
- slowWriteAt(text.x - #text.string, text.y, text.string)
- else
- error(text.alignment..' alignment for string "'..text.string..'" is not a valid alignment')
- end
- end
- local function getLength(text)
- return #text.string
- end
- renderImmediate = renderImmediate or "false"
- local text = {}
- text.alignment = alignment or "left"
- text.string = string or "blank"
- text.x = x or 1
- text.y = y or 1
- text.write = renderText
- text.slowWrite = slowRenderText
- text.delete = deleteText
- text.slowDelete = slowDeleteText
- text.length = getLength
- if renderImmediate == "now" then
- text:write()
- elseif renderImmediate == "slow" then
- text:slowWrite()
- end
- return text
- end
- --Class constructors-/\----------------------------------------------------------------------------
- local function showSlides(width, height)
- local diskDrive = getPeripheral("drive")
- local slideShow = {}
- local alignment = "left"
- local style = "normal"
- local bullet = 1
- local newPage = false
- local currentHeader = nil
- local y = 1
- local x = 1
- local printableText = ""
- slideShow = getSlides(diskDrive)
- if not slideShow then
- return 1
- end
- i = 1
- while slideShow[i] do
- if slideShow[i]:sub(1,1) == "[" then
- if slideShow[i]:sub(2,2) == "h" then
- style = "header"
- elseif slideShow[i]:sub(2,2) == "b" then
- style = "bullet"
- elseif slideShow[i]:sub(2,2) == "r" then
- alignment = "right"
- elseif slideShow[i]:sub(2,2) == "c" then
- alignment = "centre"
- elseif slideShow[i]:sub(2,2) == "n" then
- newPage = true
- currentHeader = nil
- elseif slideShow[i]:sub(2,3) == "/b" then
- style = "normal"
- bullet = 1
- elseif slideShow[i]:sub(2,3) == "/r" or slideShow[i]:sub(2,3) == "/c" then
- alignment = "left"
- end
- if slideShow[i]:sub(3,3) == "]" then
- printableText = slideShow[i]:sub(4)
- else
- printableText = slideShow[i]:sub(5)
- end
- else
- printableText = slideShow[i]
- end
- if y >= height - 1 or newPage then
- term.clear()
- y = 1
- if currentHeader then
- currentHeader:write()
- y = y + 2
- end
- end
- if printableText ~= "" then
- local tempAlignment = alignment
- local tempY = y
- local tempX = x
- if style == "bullet" then
- printableText = bullet..". "..printableText
- tempAlignment = "left"
- elseif style == "header" then
- printableText = printableText:upper()
- tempAlignment = "centre"
- tempY = 1
- tempX = width * 0.5
- end
- local tempText = newText(printableText, tempX, tempY, tempAlignment)
- tempText:slowWrite(40)
- if style == "header" then
- currentHeader = tempText
- style = "normal"
- elseif style == "bullet" then
- bullet = bullet + 1
- end
- y = y + 2
- while true do
- event, p1 = os.pullEventRaw()
- if event == "disk_eject" then
- return 2
- elseif event == "redstone" then
- if rs.getInput("left") or rs.getInput("right") then
- break
- end
- elseif event == "terminate" then
- return 0
- end
- end
- end
- i = i + 1
- end
- return 0
- end
- function getSlides(diskDrive)
- local slideShow = {}
- debug("getting slides")
- if diskDrive then
- if fs.exists(slideShowLoc) then
- --Check to see if save file exists and open it to save out all of the current saved statuses
- local hRead = assert(fs.open(slideShowLoc, "r")) --Will crash the program if the file cannot be opened
- local i = 1
- while true do
- slideShow[i] = hRead.readLine()
- if not slideShow[i] then
- break
- end
- i = i + 1
- end
- hRead.close()
- else
- slideShow = nil
- end
- end
- return slideShow
- end
- local function main()
- if not monitor then
- error("Monitor not found\nA monitor is required to run this program")
- end
- print("running projection on monitor")
- term.redirect(monitor)
- while true do
- event, p1 = os.pullEvent("redstone")
- if rs.getInput("left") or rs.getInput("right") then
- break
- end
- end
- monitor.clear()
- monitor.setTextScale(3)
- local width, height = monitor.getSize()
- local startupScreen = newText("B.A.M.", math.ceil((width ) * 0.5)+ 1, math.ceil((height) * 0.5), "centre")
- startupScreen:slowWrite(2)
- sleep(3)
- monitor.clear()
- sleep(1)
- monitor.setTextScale(1)
- width, height = monitor.getSize()
- while true do
- monitor.clear()
- local outcome = showSlides(width, height)
- monitor.clear()
- if outcome == 1 then
- local diskWrong = newText("Disk does not contain\nappropriate information or\ndisk drive can be found.\nPlease insert a new disk", 1, 1, "left")
- diskWrong:slowWrite(40)
- os.pullEvent("disk")
- elseif outcome == 2 then
- local diskOut = newText("Disk has been Removed,\nplease insert disk to\nbegin presentation", 1, 1, "left")
- diskOut:slowWrite(40)
- os.pullEvent("disk")
- elseif outcome == 0 then
- break
- end
- end
- monitor.clear()
- term.restore()
- end
- monitor = getPeripheral("monitor")
- while true do
- main()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement