Guest User

Untitled

a guest
Mar 29th, 2020
111
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- (c) ProgramCrafter
  2. -- Program that shows different banners.
  3.  
  4. local s = require('serialization')
  5. local cmp = require('computer')
  6. local c = require('component')
  7. local e = require('event')
  8.  
  9. local time_change = 60
  10. local banners = {...}
  11. local loaded_banners = {}
  12.  
  13. if tonumber(banners[1]) then
  14.     time_change = tonumber(banners[1])
  15.     table.remove(banners, 1)
  16. elseif not banners[1] then
  17.     banners[1] = '/tmp/last_banner.dat'
  18. end
  19.  
  20. local banners_count = #banners
  21. local cur_banner = 1
  22. local banner_path = banners[cur_banner]
  23.  
  24. if banners_count == 1 then time_change = math.huge end
  25.  
  26. local colors = {
  27.     0x0000FF, 0x00FFFF, 0x00FF00, 0xFFFF00, 0xFF0000, 0xFF00FF,
  28.     0x000000, 0x333333, 0x666666, 0x999999, 0xAAAAAA, 0xFFFFFF
  29. }
  30.  
  31.  
  32. local gpu = c.gpu
  33. local w,h = gpu.getResolution()
  34.  
  35. local function changeColors(fcol, bcol)
  36.     return gpu.setForeground(fcol), gpu.setBackground(bcol)
  37. end
  38. local function drawText(x, y, fcol, bcol, txt)
  39.     fcol, bcol = changeColors(fcol, bcol)
  40.     gpu.set(x, y, txt)
  41.     changeColors(fcol, bcol)
  42. end
  43. local function clearScreen()
  44.     gpu.fill(1, 1, w, h, ' ')
  45. end
  46.  
  47. local messages = {}
  48.  
  49. local function drawMessage(msg)
  50.     drawText(msg.x, msg.y, colors[msg.fcol or 1], colors[msg.bcol or 1], msg.text)
  51. end
  52. local function drawMessages()
  53.     for _,v in pairs(messages) do drawMessage(v) end
  54. end
  55. local function loadMessages()
  56.     if loaded_banners[banner_path] then
  57.         messages = loaded_banners[banner_path]
  58.     else
  59.         local f = io.open(banner_path, 'r')
  60.         if not f then return end
  61.         messages = s.unserialize(f:read())
  62.         f:close()
  63.     end
  64.    
  65.     if messages.back.w then w = messages.back.w end
  66.     if messages.back.h then h = messages.back.h end
  67.     gpu.setResolution(w, h)
  68.     gpu.setBackground(colors[messages.back.backcolor or 1])
  69.    
  70.     loaded_banners[banner_path] = messages
  71. end
  72.  
  73. loadMessages()
  74. clearScreen()
  75. drawMessages()
  76. while true do
  77.     local e = {e.pull(time_change, 'interrupted')}
  78.     if e[1] == 'interrupted' then break end
  79.    
  80.     cur_banner = cur_banner + 1
  81.     while cur_banner > banners_count do
  82.         cur_banner = cur_banner - banners_count
  83.     end
  84.     banner_path = banners[cur_banner]
  85.    
  86.     loadMessages()
  87.     clearScreen()
  88.     drawMessages()
  89. end
  90. gpu.setForeground(0xFFFFFF)
  91. gpu.setBackground(0)
  92. gpu.setResolution(40, 16)
  93. clearScreen()
RAW Paste Data