kd8lvt

scrollText.lua

Feb 20th, 2022
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. ------------
  2. -- Config --
  3. ------------
  4.  
  5. --Time in seconds between scroll updates
  6. local updateSpeed = 0.1
  7.  
  8. --Text to scroll
  9. local text = "This is a test!"
  10.  
  11. --Whether or not to scroll left-to-right
  12. local scrollLeftToRight = false
  13.  
  14. ---------------------------------
  15. -- Dont edit past here, thanks --
  16. ---------------------------------
  17.  
  18. --Initial Variable Setup
  19. local m = peripheral.find("monitor")
  20. local mX,mY = m.getSize()
  21. local cX = 0
  22.  
  23. if (scrollLeftToRight == true) then
  24. cX = -string.len(text)
  25. end
  26.  
  27. --Function to run if scrollLeftToRight == false
  28. function rtl()
  29. cX = cX+1; --Add one to the current X position of the text
  30. m.clear() --Clear the monitor
  31. m.setCursorPos(mX-cX,mY/2) --Set the cursor position of the monitor to the calculated coordinates
  32. m.write(text) --Write our text
  33. if (mX-cX < 0) then --If we are starting to lose text to the side of the screen...
  34. m.setCursorPos(mX-(cX-mX),mY/2) --Set the cursor pos to the end of the monitor, relative to where the main text is
  35. m.write(text) --Write the same text - this gives the illusion of it wrapping around.
  36. end
  37. if (cX-mX >= string.len(text)) then --If the text is all the way off the screen, reset the current position.
  38. cX = cX-mX
  39. end
  40. end
  41.  
  42. --Function to run if scrollLeftToRight == true
  43. --(Nearly identical to rtl(), comments show differences)
  44. function ltr()
  45. cX = cX+1
  46. m.clear()
  47. m.setCursorPos(cX,mY/2) --Start from front of monitor instead of end
  48. m.write(text)
  49. if (cX > mX-string.len(text)) then
  50. m.setCursorPos(cX-mX,mY/2) --Start from front of monitor instead of end
  51. m.write(text)
  52. end
  53. if (cX-mX >= 0) then
  54. cX = cX-mX
  55. end
  56. end
  57.  
  58. --Loop function. Simply chooses which direction to scroll in, based on config.
  59. -- Technically this COULD be in the while loop instead of its own function, but meh.
  60. function main()
  61. if scrollLeftToRight then
  62. ltr()
  63. else
  64. rtl()
  65. end
  66. end
  67.  
  68. --Main loop. Not that interesting.
  69. while true do
  70. main()
  71. sleep(updateSpeed)
  72. end
Advertisement
Add Comment
Please, Sign In to add comment