Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. local monitor
  2. local sides = {"left", "right", "top", "bottom", "back", "front"}
  3. local str = "(╯°□°)╯︵ ┻━┻ "
  4. local pos = 1
  5. local speedStep = 0.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. "| Y |.---.-..----..-----..--.--..-----..-----.",
  24. "|. || _ || _|| _ || | || -__|| -__|",
  25. "|. \\_/ ||___._||__| |__ ||_____||_____||_____|",
  26. "|: | | |__| ",
  27. "|::.|:. | v1.1 by zapwow ",
  28. "'--- ---' "
  29. }
  30.  
  31. function setup()
  32. -- prevent user from terminating
  33. os.pullEvent = os.pullEventRaw
  34.  
  35. -- attempt to load the string from disk
  36. local file = io.open("marquee-text", "r")
  37. if file then
  38. str = file:read("*l") or str
  39. speed = tonumber(file:read("*l")) or speed
  40. correct_password = file:read("*l") or correct_password
  41. if string.len(correct_password) > 0 then
  42. -- the password was set at some point, lock automatically
  43. locked = true
  44. end
  45. file:close()
  46. end
  47.  
  48. for i,side in ipairs(sides) do
  49. if peripheral.getType(side) == "monitor" then
  50. monitor = peripheral.wrap(side)
  51. monitor.setTextScale(5)
  52. running = true
  53. return
  54. end
  55. end
  56. print("No monitor attached")
  57. error()
  58. end
  59.  
  60. function draw()
  61. -- draw the string offset by pos
  62. -- increment pos
  63. -- when pos > str len, pos = 1
  64. monitor.clear()
  65. monitor.setCursorPos(1,1)
  66.  
  67. if string.len(str) == 0 then
  68. return
  69. end
  70.  
  71. -- pad the str to fit the screen
  72. local _str = str
  73. local len, height = monitor.getSize()
  74. while string.len(_str) < len do
  75. _str = _str .. _str
  76. end
  77.  
  78. local output = string.sub(_str, pos) .. string.sub(_str, 0, pos-1)
  79. monitor.write(output)
  80. end
  81.  
  82. function update()
  83. if speed == 0 then pos = 1 return end
  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