rokkwarr

marquee v2.0

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