Advertisement
JackMacWindows

screensaver.lua

Feb 9th, 2020
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.22 KB | None | 0 0
  1. -- Based on LDDestroier's dvdlogo
  2. local function dvdlogo()
  3.     -- adjusts walls of screen so that it will bounce further/closer to the boundries of the screen
  4.     local xMargin, yMargin = 0, 0
  5.  
  6.     local redrawDelay = nil
  7.  
  8.     local scr_x, scr_y = term.getSize()
  9.     local max, min = math.max, math.min
  10.     local floor, ceil = math.floor, math.ceil
  11.  
  12.     if scr_x >= 60 and scr_y >= 25 then
  13.         redrawDelay = redrawDelay or 0.05
  14.     else
  15.         redrawDelay = redrawDelay or 0.1
  16.     end
  17.  
  18.     local getSize = function(image)
  19.         local x, y = 0, #image[1]
  20.         for y = 1, #image[1] do
  21.             x = max(x, #image[1][y])
  22.         end
  23.         return x, y
  24.     end
  25.  
  26.     local drawImage = function(image, x, y, terminal)
  27.         terminal = terminal or term.current()
  28.         local cx, cy = terminal.getCursorPos()
  29.         for iy = 0, #image[1] + 1 do
  30.             terminal.setCursorPos(x - 1, y + (iy - 1))
  31.             if image[1][iy] then
  32.                 terminal.blit(
  33.                     " " .. image[1][iy] .. " ",
  34.                     "f" .. image[2][iy] .. "f",
  35.                     "f" .. image[3][iy] .. "f"
  36.                 )
  37.             else
  38.                 terminal.clearLine()
  39.             end
  40.         end
  41.         terminal.setCursorPos(cx,cy)
  42.     end
  43.  
  44.     local colorSwap = function(image, text, back)
  45.         local output = {{},{},{}}
  46.         for y = 1, #image[1] do
  47.             output[1][y] = image[1][y]
  48.             output[2][y] = image[2][y]:gsub(".", text)
  49.             output[3][y] = image[3][y]:gsub(".", back or text)
  50.         end
  51.         return output
  52.     end
  53.  
  54.     local logo = {
  55.         xvel = (math.random(0, 1) * 2) - 1,
  56.         yvel = (math.random(0, 1) * 2) - 1,
  57.         x = floor(scr_x / 2),
  58.         y = floor(scr_y / 2),
  59.         img = {
  60.             {
  61.                 "  —ƒƒƒƒƒƒƒƒƒ”     Ÿƒƒƒƒƒƒƒ€€",
  62.                 "  ‚ƒƒƒ€ˆƒƒ€€    ‡€€‡ƒƒƒƒ‹€‚",
  63.                 " —€—€€€€€  ”€• Ÿ€Ÿ€€  •€•",
  64.                 " €€€‡€—  ‚€€‡€€‡  —€—  Ÿ€",
  65.                 "—€€ƒƒ€€‡    ”€€Ÿ   €‚ƒ€Ÿ ",
  66.                 "‚ƒƒƒƒƒ      ‚€‡     ƒƒƒƒƒ   ",
  67.                 "      ƒƒƒ‚ƒƒƒ        ",
  68.                 " ƒ€€€€€€Ÿ€€€€€€€‚ƒ   ",
  69.                 "•€€€€€€ˆ‘€€€€€€€€€Œ€€€€€€€•  ",
  70.                 " ƒ€€€€€€‚ƒƒƒƒƒ€€€€€€€Ÿƒ   ",
  71.                 "      ƒƒƒƒƒƒƒƒ        ",
  72.             }, {
  73.                 "00ffffffffff000000fffffffffff",
  74.                 "000000f0f00000000f000000000f0",
  75.                 "0f00fff0000f000ff000ff0000f00",
  76.                 "0f00fff0000000f00000f0000ff00",
  77.                 "f00ff0000000f0000000f0fff0000",
  78.                 "00000000000000000000000000000",
  79.                 "000000fffffffffffffff00000000",
  80.                 "0fff0000000000000000000fff000",
  81.                 "f000000f0ffffffffff0000000000",
  82.                 "000f000000ffffff0000000000000",
  83.                 "00000000000000000000000000000",
  84.             }, {
  85.                 "ff0000000000ffffff000000000ff",
  86.                 "ffffff000ff00ffff000ffffff00f",
  87.                 "f00ffff00ff00ff000fff00fff00f",
  88.                 "f00fff00ffff00000fff00fff000f",
  89.                 "0000000fffff000fffff000000fff",
  90.                 "fffffffffffff0fffffffffffffff",
  91.                 "ffffff000000000000000ffffffff",
  92.                 "f000000000ffffff0000000000fff",
  93.                 "00000000ffffffffff00000000fff",
  94.                 "fff00000000000000000000ffffff",
  95.                 "fffffffffffffffffffffffffffff",
  96.             }
  97.         }
  98.     }
  99.  
  100.     local imgXsize, imgYsize = getSize(logo.img)
  101.     local xWall, yWall
  102.  
  103.     local render = function(colorReplace)
  104.         if colorReplace then
  105.             drawImage(
  106.                 colorSwap(logo.img, {["0"] = colorReplace}, {["0"] = colorReplace}),
  107.                 floor(logo.x),
  108.                 floor(logo.y)
  109.             )
  110.         else
  111.             drawImage(
  112.                 logo.img,
  113.                 floor(logo.x),
  114.                 floor(logo.y)
  115.             )
  116.         end
  117.     end
  118.  
  119.     local color = math.random(1, 15)
  120.  
  121.     local tick = function()
  122.         scr_x, scr_y = term.getSize()
  123.         xWall = scr_x - imgXsize + 1 - xMargin
  124.         yWall = scr_y - imgYsize + 1 - yMargin
  125.         logo.x = min(max(logo.x + logo.xvel, 1 + xMargin), xWall)
  126.         logo.y = min(max(logo.y + logo.yvel, 1 + yMargin), yWall)
  127.  
  128.         if floor(logo.x) == (1 + xMargin) or floor(logo.x) == xWall then
  129.             logo.xvel = -logo.xvel
  130.     color = math.random(1, 15)
  131.         end
  132.         if floor(logo.y) == (1 + yMargin) or floor(logo.y) == yWall then
  133.             logo.yvel = -logo.yvel
  134.     color = math.random(1, 15)
  135.         end
  136.         render(string.sub("0123456789abcdef", color, color))
  137.     end
  138.  
  139.     term.setBackgroundColor(colors.black)
  140.     term.clear()
  141.  
  142.     local evt
  143.     local tID = os.startTimer(redrawDelay)
  144.     while true do
  145.         evt = {os.pullEventRaw()}
  146.         if evt[1] == "timer" and evt[2] == tID then
  147.             tick()
  148.             tID = os.startTimer(redrawDelay)
  149.         elseif evt[1] == "key" or evt[1] == "key_up" or evt[1] == "mouse_click" or evt[1] == "mouse_up" or evt[1] == "mouse_drag" then
  150.             render("8")
  151.             sleep(0.05)
  152.             render("7")
  153.             sleep(0.05)
  154.             term.clear()
  155.             break
  156.         end
  157.     end
  158.  
  159.     return 0
  160. end
  161.  
  162. function _G.read( _sReplaceChar, _tHistory, _fnComplete, _sDefault )
  163.     if _sReplaceChar ~= nil and type( _sReplaceChar ) ~= "string" then
  164.         error( "bad argument #1 (expected string, got " .. type( _sReplaceChar ) .. ")", 2 )
  165.     end
  166.     if _tHistory ~= nil and type( _tHistory ) ~= "table" then
  167.         error( "bad argument #2 (expected table, got " .. type( _tHistory ) .. ")", 2 )
  168.     end
  169.     if _fnComplete ~= nil and type( _fnComplete ) ~= "function" then
  170.         error( "bad argument #3 (expected function, got " .. type( _fnComplete ) .. ")", 2 )
  171.     end
  172.     if _sDefault ~= nil and type( _sDefault ) ~= "string" then
  173.         error( "bad argument #4 (expected string, got " .. type( _sDefault ) .. ")", 2 )
  174.     end
  175.     term.setCursorBlink( true )
  176.  
  177.     local sLine
  178.     if type( _sDefault ) == "string" then
  179.         sLine = _sDefault
  180.     else
  181.         sLine = ""
  182.     end
  183.     local nHistoryPos
  184.     local nPos, nScroll = #sLine, 0
  185.     if _sReplaceChar then
  186.         _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  187.     end
  188.  
  189.     local tCompletions
  190.     local nCompletion
  191.     local function recomplete()
  192.         if _fnComplete and nPos == string.len(sLine) then
  193.             tCompletions = _fnComplete( sLine )
  194.             if tCompletions and #tCompletions > 0 then
  195.                 nCompletion = 1
  196.             else
  197.                 nCompletion = nil
  198.             end
  199.         else
  200.             tCompletions = nil
  201.             nCompletion = nil
  202.         end
  203.     end
  204.  
  205.     local function uncomplete()
  206.         tCompletions = nil
  207.         nCompletion = nil
  208.     end
  209.  
  210.     local w = term.getSize()
  211.     local sx = term.getCursorPos()
  212.  
  213.     local function redraw( _bClear )
  214.         local cursor_pos = nPos - nScroll
  215.         if sx + cursor_pos >= w then
  216.             nScroll = (sx + nPos) - w
  217.         elseif cursor_pos < 0 then
  218.             nScroll = nPos
  219.         end
  220.  
  221.         local cx,cy = term.getCursorPos()
  222.         term.setCursorPos( sx, cy )
  223.         local sReplace = (_bClear and " ") or _sReplaceChar
  224.         if sReplace then
  225.             term.write( string.rep( sReplace, math.max( string.len(sLine) - nScroll, 0 ) ) )
  226.         else
  227.             term.write( string.sub( sLine, nScroll + 1 ) )
  228.         end
  229.  
  230.         if nCompletion then
  231.             local sCompletion = tCompletions[ nCompletion ]
  232.             local oldText, oldBg
  233.             if not _bClear then
  234.                 oldText = term.getTextColor()
  235.                 oldBg = term.getBackgroundColor()
  236.                 term.setTextColor( colors.white )
  237.                 term.setBackgroundColor( colors.gray )
  238.             end
  239.             if sReplace then
  240.                 term.write( string.rep( sReplace, string.len( sCompletion ) ) )
  241.             else
  242.                 term.write( sCompletion )
  243.             end
  244.             if not _bClear then
  245.                 term.setTextColor( oldText )
  246.                 term.setBackgroundColor( oldBg )
  247.             end
  248.         end
  249.  
  250.         term.setCursorPos( sx + nPos - nScroll, cy )
  251.     end
  252.    
  253.     local function clear()
  254.         redraw( true )
  255.     end
  256.  
  257.     recomplete()
  258.     redraw()
  259.  
  260.     local function acceptCompletion()
  261.         if nCompletion then
  262.             -- Clear
  263.             clear()
  264.  
  265.             -- Find the common prefix of all the other suggestions which start with the same letter as the current one
  266.             local sCompletion = tCompletions[ nCompletion ]
  267.             sLine = sLine .. sCompletion
  268.             nPos = string.len( sLine )
  269.  
  270.             -- Redraw
  271.             recomplete()
  272.             redraw()
  273.         end
  274.     end
  275.     local tm
  276.     while true do
  277.         if tm ~= nil then os.cancelTimer(tm) end
  278.         tm = os.startTimer(60)
  279.         local sEvent, param, cursorx, cursory = os.pullEvent()
  280.         if sEvent == "char" then
  281.             -- Typed key
  282.             clear()
  283.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  284.             nPos = nPos + 1
  285.             recomplete()
  286.             redraw()
  287.  
  288.         elseif sEvent == "paste" then
  289.             -- Pasted text
  290.             clear()
  291.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  292.             nPos = nPos + string.len( param )
  293.             recomplete()
  294.             redraw()
  295.  
  296.         elseif sEvent == "key" then
  297.             if param == keys.enter then
  298.                 -- Enter
  299.                 if nCompletion then
  300.                     clear()
  301.                     uncomplete()
  302.                     redraw()
  303.                 end
  304.                 break
  305.                
  306.             elseif param == keys.left then
  307.                 -- Left
  308.                 if nPos > 0 then
  309.                     clear()
  310.                     nPos = nPos - 1
  311.                     recomplete()
  312.                     redraw()
  313.                 end
  314.                
  315.             elseif param == keys.right then
  316.                 -- Right                
  317.                 if nPos < string.len(sLine) then
  318.                     -- Move right
  319.                     clear()
  320.                     nPos = nPos + 1
  321.                     recomplete()
  322.                     redraw()
  323.                 else
  324.                     -- Accept autocomplete
  325.                     acceptCompletion()
  326.                 end
  327.  
  328.             elseif param == keys.up or param == keys.down then
  329.                 -- Up or down
  330.                 if nCompletion then
  331.                     -- Cycle completions
  332.                     clear()
  333.                     if param == keys.up then
  334.                         nCompletion = nCompletion - 1
  335.                         if nCompletion < 1 then
  336.                             nCompletion = #tCompletions
  337.                         end
  338.                     elseif param == keys.down then
  339.                         nCompletion = nCompletion + 1
  340.                         if nCompletion > #tCompletions then
  341.                             nCompletion = 1
  342.                         end
  343.                     end
  344.                     redraw()
  345.  
  346.                 elseif _tHistory then
  347.                     -- Cycle history
  348.                     clear()
  349.                     if param == keys.up then
  350.                         -- Up
  351.                         if nHistoryPos == nil then
  352.                             if #_tHistory > 0 then
  353.                                 nHistoryPos = #_tHistory
  354.                             end
  355.                         elseif nHistoryPos > 1 then
  356.                             nHistoryPos = nHistoryPos - 1
  357.                         end
  358.                     else
  359.                         -- Down
  360.                         if nHistoryPos == #_tHistory then
  361.                             nHistoryPos = nil
  362.                         elseif nHistoryPos ~= nil then
  363.                             nHistoryPos = nHistoryPos + 1
  364.                         end                        
  365.                     end
  366.                     if nHistoryPos then
  367.                         sLine = _tHistory[nHistoryPos]
  368.                         nPos, nScroll = string.len( sLine ), 0
  369.                     else
  370.                         sLine = ""
  371.                         nPos, nScroll = 0, 0
  372.                     end
  373.                     uncomplete()
  374.                     redraw()
  375.  
  376.                 end
  377.  
  378.             elseif param == keys.backspace then
  379.                 -- Backspace
  380.                 if nPos > 0 then
  381.                     clear()
  382.                     sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  383.                     nPos = nPos - 1
  384.                     if nScroll > 0 then nScroll = nScroll - 1 end
  385.                     recomplete()
  386.                     redraw()
  387.                 end
  388.  
  389.             elseif param == keys.home then
  390.                 -- Home
  391.                 if nPos > 0 then
  392.                     clear()
  393.                     nPos = 0
  394.                     recomplete()
  395.                     redraw()
  396.                 end
  397.  
  398.             elseif param == keys.delete then
  399.                 -- Delete
  400.                 if nPos < string.len(sLine) then
  401.                     clear()
  402.                     sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )                
  403.                     recomplete()
  404.                     redraw()
  405.                 end
  406.  
  407.             elseif param == keys["end"] then
  408.                 -- End
  409.                 if nPos < string.len(sLine ) then
  410.                     clear()
  411.                     nPos = string.len(sLine)
  412.                     recomplete()
  413.                     redraw()
  414.                 end
  415.  
  416.             elseif param == keys.tab then
  417.                 -- Tab (accept autocomplete)
  418.                 acceptCompletion()
  419.  
  420.             end
  421.  
  422.         -- Borrowed from [CC: Tweaked](https://github.com/SquidDev/CC-Tweaked)
  423.         elseif sEvent == "mouse_click" or sEvent == "mouse_drag" and param == 1 then
  424.             local _, cy = term.getCursorPos()
  425.             if cursorx >= sx and cursorx <= w and cursory == cy then
  426.                 -- Ensure we don't scroll beyond the current line
  427.                 nPos = math.min(math.max(nScroll + cursorx - sx, 0), #sLine)
  428.                 redraw()
  429.             end
  430.  
  431.         elseif sEvent == "term_resize" then
  432.             -- Terminal resized
  433.             w = term.getSize()
  434.             redraw()
  435.  
  436.         elseif sEvent == "timer" and param == tm then
  437.             local oldblink, oldfg, oldbg = term.getCursorBlink(), term.getTextColor(), term.getBackgroundColor()
  438.             term.setCursorBlink(false)
  439.             dvdlogo()
  440.             term.setCursorBlink(oldblink)
  441.             term.setTextColor(oldfg)
  442.             term.setBackgroundColor(oldbg)
  443.             redraw()
  444.  
  445.         end
  446.     end
  447.  
  448.     local cx, cy = term.getCursorPos()
  449.     term.setCursorBlink( false )
  450.     term.setCursorPos( w + 1, cy )
  451.     print()
  452.    
  453.     return sLine
  454. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement