Advertisement
Duffinley

907 banner

Jan 9th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.63 KB | None | 0 0
  1. local monitor
  2. local sides = {"left", "right", "top", "bottom", "back", "front"}
  3. local str = "Hello, world! "
  4. local pos = 1
  5. local speedStep = 5.5
  6. local speed = 2
  7. local maxSpeed = 10
  8. local running = false
  9. local keys = {
  10.     backspace=14,
  11.     enter=28,
  12.     f1=59,
  13.     f3=61,
  14.     leftArrow=203,
  15.     rightArrow=205,
  16. }
  17. local locked = false
  18. local password = ""
  19. local correct_password = ""
  20.  
  21. local banner = {
  22. " /$$$$$$$            /$$$$$$$$ /$$$$$$$$ /$$     /$$",
  23. "| $$__  $$          | $$_____/| $$_____/|  $$   /$$/",
  24. "| $$  \ $$ /$$   /$$| $$      | $$       \  $$ /$$/ ",
  25. "| $$  | $$| $$  | $$| $$$$$   | $$$$$     \  $$$$/  ",
  26. "| $$  | $$| $$  | $$| $$__/   | $$__/      \  $$/   ",
  27. "| $$  | $$| $$  | $$| $$      | $$          | $$    ",
  28. "| $$$$$$$/|  $$$$$$/| $$      | $$          | $$    ",
  29. "|_______/  \______/ |__/      |__/          |__/    "  
  30. }
  31.  
  32. function setup()
  33.     -- prevent user from terminating
  34.     os.pullEvent = os.pullEventRaw
  35.  
  36.     -- attempt to load the string from disk
  37.     -- local file = io.open("marquee-text", "r")
  38.     -- if file then
  39.         str = "Abandon all hope, ye who enter here!, for we are the END GAMERS your mom warned you about! 907 64m3r5 4 l1f3         "
  40.         speed = tonumber("2") or speed
  41.         correct_password = "guest" or correct_password
  42.         if string.len(correct_password) > 0 then
  43.             -- the password was set at some point, lock automatically
  44.             locked = true
  45.         end
  46.         -- file:close()
  47.     -- end
  48.  
  49.     for i,side in ipairs(sides) do
  50.         if peripheral.getType(side) == "monitor" then
  51.             monitor = peripheral.wrap(side)
  52.             monitor.setTextScale(5)
  53.             running = true
  54.             return
  55.         end
  56.     end
  57.     print("No monitor attached")
  58.     error()
  59. end
  60.  
  61. function draw()
  62.     -- draw the string offset by pos
  63.     -- increment pos
  64.     -- when pos > str len, pos = 1
  65.     monitor.clear()
  66.     monitor.setCursorPos(1,1)
  67.  
  68.     if string.len(str) == 0 then
  69.         return
  70.     end
  71.  
  72.     -- pad the str to fit the screen
  73.     local _str = str
  74.     local len, height = monitor.getSize()
  75.     while string.len(_str) < len do
  76.         _str = _str .. _str
  77.     end
  78.  
  79.     local output = string.sub(_str, pos) .. string.sub(_str, 0, pos-1)
  80.     monitor.write(output)
  81. end
  82.  
  83. function update()
  84.     pos = pos + 1
  85.     if pos > string.len(str) then
  86.         pos = 1
  87.     end
  88. end
  89.  
  90. function marquee()
  91.     while running do
  92.         draw()
  93.         if speed > 0 then
  94.             update()
  95.             sleep(1/speed)
  96.         else
  97.             sleep(0.5)
  98.         end
  99.     end
  100. end
  101.  
  102. function drawGUI()
  103.     --always draw the banner
  104.     term.clear()
  105.     for i,v in ipairs(banner) do
  106.         term.setCursorPos(1,i)
  107.         term.write(v)
  108.     end
  109.  
  110.     if locked then
  111.         drawLockedGUI()
  112.     else
  113.         drawUnlockedGUI()
  114.     end
  115. end
  116.  
  117. function drawLockedGUI()
  118.     local line = #banner + 2
  119.     term.setCursorPos(1,line)
  120.     if string.len(correct_password) == 0 then
  121.         term.write("  Set password: ")
  122.     else
  123.         term.write("  Password: ")
  124.     end
  125.     term.write(string.rep('*', string.len(password)))
  126. end
  127.  
  128. function drawUnlockedGUI()
  129.     local line = #banner + 2
  130.  
  131.     -- lock
  132.     term.setCursorPos(1,line)
  133.     term.write("  To lock terminal, press F1")
  134.  
  135.     -- exit
  136.     line = line + 1
  137.     term.setCursorPos(1,line)
  138.     term.write("  To quit, press F3")
  139.  
  140.     -- speed gui
  141.     line = line + 2
  142.     term.setCursorPos(1,line)
  143.     term.write(
  144.         "  Speed: <" ..
  145.         string.rep('-', speed/speedStep) ..
  146.         'X' ..
  147.         string.rep('-', (maxSpeed - speed)/speedStep) ..
  148.         "> " ..
  149.         string.format("%.1f", speed) ..
  150.         " steps/sec"
  151.     )
  152.  
  153.     -- text
  154.     line = line + 2
  155.     term.setCursorPos(1,line)
  156.     term.write(
  157.         "  Text: " ..
  158.         str
  159.     )
  160.     term.setCursorBlink(true)
  161. end
  162.  
  163. function save()
  164.     local file = io.open("marquee-text", "w")
  165.     if file then
  166.         file:write(str)
  167.         file:write("\n")
  168.         file:write(speed)
  169.         file:write("\n")
  170.         file:write(password)
  171.         file:close()
  172.     end
  173. end
  174.  
  175. function gui()
  176.     drawGUI()
  177.     while running do
  178.         local evt, p1, p2, p3 = os.pullEvent()
  179.         if evt == "char" then
  180.             if locked then
  181.                 -- read the password
  182.                 password = password .. p1
  183.             else
  184.                 str = str .. p1
  185.                 save()
  186.             end
  187.         elseif evt == "key" then
  188.             if p1 == keys["backspace"] and not locked then
  189.                 str = string.sub(str, 1, string.len(str)-1)
  190.                 save()
  191.             elseif p1 == keys["leftArrow"] and not locked then
  192.                 speed = math.max(speed-speedStep, 0)
  193.                 save()
  194.             elseif p1 == keys["rightArrow"] and not locked then
  195.                 speed = math.min(speed+speedStep, maxSpeed)
  196.                 save()
  197.             elseif p1 == keys["f1"] and not locked then
  198.                 locked = true
  199.             elseif p1 == keys["f3"] and not locked then
  200.                 term.clear()
  201.                 term.setCursorPos(1,1)
  202.                 return true
  203.             elseif p1 == keys["enter"] and locked then
  204.                 if string.len(correct_password) == 0 then
  205.                     correct_password = password
  206.                     save()
  207.                 else
  208.                     if password == correct_password then
  209.                         locked = false
  210.                     end
  211.                 end
  212.                 password = ""
  213.             end
  214.         end
  215.         drawGUI()
  216.     end
  217. end
  218.  
  219.  
  220. setup()
  221. parallel.waitForAny(marquee, gui)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement