Guest User

Untitled

a guest
Mar 29th, 2020
414
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- (c) ProgramCrafter
  2. -- Program that creates and shows different banners.
  3.  
  4. local s = require('serialization')
  5. local c = require('component')
  6. local u = require('unicode')
  7. local e = require('event')
  8.  
  9. local args = {...}
  10. local banner_path = args[1] or '/tmp/last_banner.dat'
  11.  
  12. -- configured for OCEmu
  13. local arrows_behavior = {
  14.     [200] = {0, -1}, [203] = {-1, 0},
  15.     [205] = {1, 0},  [208] = {0, 1}
  16. }
  17. local colors = {
  18.     0x0000FF, 0x00FFFF, 0x00FF00, 0xFFFF00, 0xFF0000, 0xFF00FF,
  19.     0x000000, 0x333333, 0x666666, 0x999999, 0xAAAAAA, 0xFFFFFF
  20. }
  21. local screen_bcol = 7
  22.  
  23.  
  24. local gpu = c.gpu
  25. local w,h = gpu.getResolution()
  26. if args[2] and tonumber(args[2]) then w = tonumber(args[2]) end
  27. if args[3] and tonumber(args[3]) then h = tonumber(args[3]) end
  28. gpu.setResolution(w, h)
  29.  
  30. local function changeColors(fcol, bcol)
  31.     return gpu.setForeground(fcol), gpu.setBackground(bcol)
  32. end
  33. local function drawText(x, y, fcol, bcol, txt)
  34.     fcol, bcol = changeColors(fcol, bcol)
  35.     gpu.set(x, y, txt)
  36.     changeColors(fcol, bcol)
  37. end
  38. local function clearScreen()
  39.     gpu.fill(1, 1, w, h, ' ')
  40. end
  41.  
  42. local messages = {back = {x=w+1,y=h+1,text='',backcolor=screen_bcol}}
  43. local selection = nil
  44. local editing = false
  45.  
  46. messages.back.w = w
  47. messages.back.h = h
  48.  
  49. local function msgIntersectXY(msg, x,y)
  50.     return x >= msg.x and x < msg.x + msg.w and y >= msg.y and y < msg.y + msg.h
  51. end
  52. local function msgUnderXY(x,y)
  53.     for k,v in pairs(messages) do
  54.         if msgIntersectXY(v, x, y) and k ~= 'back' then return v end
  55.     end
  56. end
  57. local function drawMessage(msg)
  58.     drawText(msg.x, msg.y, colors[msg.fcol or 1], colors[msg.bcol or 1], msg.text)
  59. end
  60. local function drawMessages()
  61.     for _,v in pairs(messages) do
  62.         if type(v) == 'table' then drawMessage(v) end
  63.     end
  64. end
  65. local function removeMessage(msg)
  66.     for k,v in pairs(messages) do
  67.         if v == selection and type(k) == 'number' and k > 0 then
  68.             table.remove(messages, k)
  69.         elseif v == selection then
  70.             messages[k] = nil
  71.         end
  72.     end
  73. end
  74. local function loadMessages()
  75.     local f = io.open(banner_path, 'r')
  76.     if not f then return end
  77.     messages = s.unserialize(f:read())
  78.     f:close()
  79.    
  80.     gpu.setBackground(colors[messages.back.backcolor or 1])
  81. end
  82. local function dumpMessages()
  83.     local tmp_msg = messages[-1]
  84.     messages[-1] = nil
  85.    
  86.     local f = io.open(banner_path, 'w')
  87.     if not f then return end
  88.     f:write(s.serialize(messages))
  89.     f:close()
  90.    
  91.     messages[-1] = tmp_msg
  92. end
  93.  
  94. local admin_nick = nil
  95. drawText(3, h, 0xFF9999, 0, 'WAITING FOR ADMIN')
  96. admin_nick = ( {e.pull("touch")} )[6]
  97. drawText(3, h, 0xFF9999, 0, 'ADMIN FOUND      ')
  98. os.sleep(1)
  99. clearScreen()
  100.  
  101. -- print(messages.back.w)
  102. -- io.read()
  103. -- clearScreen()
  104.  
  105. loadMessages()
  106. clearScreen()
  107. drawMessages()
  108. while true do
  109.     local e = {e.pull()}
  110.     if e[1] == 'touch' and e[6] == admin_nick then
  111.         local x,y = e[3], e[4]
  112.         if e[5] == 1 and editing then
  113.             messages[-1] = nil
  114.             editing = false
  115.             if selection then
  116.                 selection.bcol = selection.obcol
  117.                 selection = nil
  118.             end
  119.         elseif e[5] == 1 then
  120.             messages[-1] = {x=3, y=h, w=13, h=1, fcol=9, bcol=7, text='-- Editing --'}
  121.             editing = true
  122.         elseif editing then
  123.             if selection then selection.bcol = selection.obcol end
  124.             selection = msgUnderXY(x,y)
  125.             if not selection then
  126.                 selection = {x=x, y=y, w=3, h=1, text='Abc', fcol=#colors, bcol=screen_bcol}
  127.                 table.insert(messages, selection)
  128.             end
  129.             selection.obcol = selection.bcol
  130.             selection.bcol = 8
  131.         end
  132.     elseif e[1] == 'key_up' and e[5] == admin_nick and editing then
  133.         if selection then
  134.             local txt = selection.text
  135.             local t_len = u.len(txt)
  136.             if e[3] == 8 and t_len then -- backspace
  137.                 selection.text = u.sub(txt, 1, t_len - 1)
  138.                 selection.w = u.len(selection.text)
  139.             elseif e[4] == 211 then
  140.                 removeMessage(selection)
  141.                 selection = nil
  142.             elseif e[3] == 9 then
  143.                 if selection == messages[-1] then
  144.                     local bcol = screen_bcol
  145.                     bcol = bcol + 1
  146.                     while bcol > #colors do bcol = bcol - #colors end
  147.                    
  148.                     for k,v in pairs(messages) do
  149.                         if type(v) == 'table' then
  150.                             v.bcol = bcol
  151.                             v.obcol = bcol
  152.                         end
  153.                     end
  154.                     selection.bcol = 8
  155.                    
  156.                     screen_bcol = bcol
  157.                     messages.back.backcolor = bcol
  158.                     gpu.setBackground(colors[bcol])
  159.                 else
  160.                     local fcol = selection.fcol
  161.                     fcol = fcol + 1
  162.                     while fcol > #colors do fcol = fcol - #colors end
  163.                     selection.fcol = fcol
  164.                 end
  165.             elseif arrows_behavior[e[4]] then
  166.                 local cx, cy = selection.x, selection.y
  167.                 local move = arrows_behavior[e[4]]
  168.                 local nx, ny = cx + move[1], cy + move[2]
  169.                
  170.                 local posValid = true
  171.                 if nx + selection.w - 1 > w then posValid = false end
  172.                 if ny + selection.h - 1 > h then posValid = false end
  173.                 if nx < 1 then posValid = false end
  174.                 if ny < 1 then posValid = false end
  175.                 for tx = nx, nx + selection.w - 1 do
  176.                     for ty = ny, ny + selection.h - 1 do
  177.                         local m = msgUnderXY(tx, ty)
  178.                         if m and m ~= selection then posValid = false break end
  179.                     end
  180.                     if not posValid then break end
  181.                 end
  182.                 if posValid then selection.x, selection.y = nx, ny end
  183.             elseif e[3] ~= 0 then
  184.                 selection.text = txt .. string.gsub(u.char(e[3]), '%c', '')
  185.                 selection.w = u.len(selection.text)
  186.             end
  187.         elseif e[3] == 9 then
  188.             local bcol = screen_bcol
  189.             bcol = bcol + 1
  190.             while bcol > #colors do bcol = bcol - #colors end
  191.            
  192.             for k,v in pairs(messages) do
  193.                 if type(v) == 'table' then
  194.                     v.bcol = bcol
  195.                     v.obcol = bcol
  196.                 end
  197.             end
  198.            
  199.             screen_bcol = bcol
  200.             messages.back.backcolor = bcol
  201.             gpu.setBackground(colors[bcol])
  202.         end
  203.     elseif e[1] == 'interrupted' then break end
  204.     clearScreen()
  205.     drawMessages()
  206.     dumpMessages()
  207. end
  208. gpu.setForeground(0xFFFFFF)
  209. gpu.setBackground(0)
  210. gpu.setResolution(40, 16)
  211. clearScreen()
RAW Paste Data