Advertisement
Guest User

Sky

a guest
Jun 20th, 2021
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.17 KB | None | 0 0
  1. local sky = {}
  2. local component = require("component")
  3. local computer=require("computer")
  4. local serial = require("serialization")
  5. local term = require("term")
  6. local event = require("event")
  7. local unicode = require("unicode")
  8. local fs = require("filesystem")
  9. local internet = require("internet")
  10. local g = component.gpu
  11. local back = 0xffffff
  12.  
  13. function sky.mid(w,y,text) --Центровка
  14.     local _,n = string.gsub(text, "&","")
  15.     local l = unicode.len(text) - n * 2
  16.     x = (w / 2) - (l / 2)
  17.     sky.text(x, y, text)
  18. end
  19.  
  20. function sky.drawImage(x,y,path) --Отрисовка картинок
  21.     local back, font = g.getBackground(), g.getForeground()
  22.     if (fs.exists(path)) then
  23.         local start_x = x
  24.         local image
  25.         local text = ""
  26.         local file = fs.open(path)
  27.         while true do
  28.           local temp = file:read(9999999)
  29.           if (temp) then
  30.             text = text .. temp
  31.           else
  32.             break
  33.           end
  34.         end
  35.         local text2 = "{" .. text .. "}"
  36.         image = serial.unserialize(text2)
  37.         for i = 1, #image / 2 do
  38.             x = start_x
  39.             for j = 1, #image[i] do
  40.                 g.setBackground(image[i * 2 - 1][j])
  41.                 g.setForeground(image[i * 2][j])
  42.                 g.set(x,y,"▄")
  43.                 x = x + 1
  44.             end
  45.             y = y + 1
  46.         end
  47.     end
  48.     g.setBackground(back)
  49.     g.setForeground(font)
  50. end
  51.  
  52. function sky.com(command) --Выполнить команду
  53.     if (component.isAvailable("opencb")) then
  54.         local _,c = component.opencb.execute(command)
  55.         return c
  56.     end
  57. end
  58.  
  59. function sky.money(nick) --Баланс игрока
  60.     local c = sky.com("money " .. nick)
  61.     local _, b = string.find(c, "Баланс: §f")
  62.     local balance
  63.     if string.find(c, "Emeralds") ~= nil then
  64.         balance = unicode.sub(c, b - 16, unicode.len(c) - 10)
  65.     else
  66.         balance = unicode.sub(c, b - 16, unicode.len(c) - 9)
  67.     end
  68.     return (balance)
  69. end
  70.  
  71. function sky.checkMoney(nick,price) --Чекнуть, баланс, если хватает, то снять бабки
  72.     local balance = sky.money(nick)
  73.     balance = string.sub(balance, 1, string.len(balance) - 3)
  74.     if string.find(balance, "-") ~= nil then
  75.         return false
  76.     else
  77.         balance = string.gsub(balance,",","")
  78.         if tonumber(balance) < price then
  79.             return false
  80.         else
  81.             sky.com("money take " .. nick .. " " .. price)
  82.             return true
  83.         end
  84.     end
  85. end
  86.  
  87. function sky.logo(name,col1,col2,w,h,offset) --Рамка P.S. Луфф питух
  88.     term.clear()
  89.     g.setBackground(0x000000)
  90.     g.setForeground(col2)
  91.     for i = 1, w do
  92.         g.set(i,1,"=")
  93.         g.set(i,h,"=")
  94.     end
  95.     for i = 1, h do
  96.         g.set(1, i, "||")
  97.         g.set(w-1, i, "||")
  98.     end
  99.     if offset == nil then
  100.         offset = 0
  101.     end
  102.     sky.text(w/2 + offset - unicode.len("[ " .. name .. " ]")/2, 1, "[ " .. name .. " ]")
  103.     g.set(w-42, h, "[ Автор: SkyDrive_ - Проект: McSkill ]")
  104.     g.setForeground(col1)
  105.     g.set(w/2+1 + offset - unicode.len(name)/2, 1, name)
  106.     g.set(w-40, h, "Автор: SkyDrive_ - Проект: McSkill")
  107. end
  108.  
  109. function sky.setColor(index) --Список цветов
  110.     if (index ~= "r") then back = g.getForeground() end
  111.     if (index == "0") then g.setForeground(0x333333) end
  112.     if (index == "1") then g.setForeground(0x0000ff) end
  113.     if (index == "2") then g.setForeground(0x00ff00) end
  114.     if (index == "3") then g.setForeground(0x24b3a7) end
  115.     if (index == "4") then g.setForeground(0xff0000) end
  116.     if (index == "5") then g.setForeground(0x8b00ff) end
  117.     if (index == "6") then g.setForeground(0xffa500) end
  118.     if (index == "7") then g.setForeground(0xbbbbbb) end
  119.     if (index == "8") then g.setForeground(0x808080) end
  120.     if (index == "9") then g.setForeground(0x0000ff) end
  121.     if (index == "a") then g.setForeground(0x66ff66) end
  122.     if (index == "b") then g.setForeground(0x00ffff) end
  123.     if (index == "c") then g.setForeground(0xff6347) end
  124.     if (index == "d") then g.setForeground(0xff00ff) end
  125.     if (index == "e") then g.setForeground(0xffff00) end
  126.     if (index == "f") then g.setForeground(0xffffff) end
  127.     if (index == "g") then g.setForeground(0x00ff00) end
  128.     if (index == "r") then g.setForeground(back) end
  129. end
  130.  
  131. function sky.text(x,y,text) --Цветной текст
  132.     local n = 1
  133.     for i = 1, unicode.len(text) do
  134.         if unicode.sub(text, i,i) == "&" then
  135.             sky.setColor(unicode.sub(text, i + 1, i + 1))
  136.         elseif unicode.sub(text, i - 1, i - 1) ~= "&" then
  137.             g.set(x+n,y, unicode.sub(text, i,i))
  138.             n = n + 1
  139.         end
  140.     end
  141. end
  142.  
  143. function sky.button(x,y,w,h,col1,col2,text) -- Кнопка
  144.     g.setForeground(col1)
  145.     g.set(x + w/2 - unicode.len(text)/2, y+h/2, text)
  146.     g.setForeground(col2)
  147.     for i = 1, w-2 do
  148.         g.set(x+i,y,"─")
  149.         g.set(x+i,y+h-1,"─")
  150.     end
  151.     for i = 1, h-2 do
  152.         g.set(x,y+i,"│")
  153.         g.set(x+w-1,y+i,"│")
  154.     end
  155.     g.set(x,y,"┌")
  156.     g.set(x+w-1,y,"┐")
  157.     g.set(x,y+h-1,"└")
  158.     g.set(x+w-1,y+h-1,"┘")
  159. end
  160.  
  161. function sky.takeItem(nick, item, numb) --Забрать итем
  162.     if string.find(sky.com("clear " .. nick .. " " .. item .. " " .. numb), "Убрано") ~= nil then
  163.         return true
  164.     else
  165.         return false
  166.     end
  167. end
  168.  
  169. function sky.giveItem(nick, item, numb) --Выдать предмет и чекнуть влезло ли в инвентарь, если нет, вернуть остаток
  170.     local text = sky.com("egive " .. nick .. " " .. item .. " " .. numb)
  171.  
  172.     if string.find(text, "Недостаточно свободного места") ~= nil then
  173.         local _, b = string.find(text, "Недостаточно свободного места, §c")
  174.        
  175.         local i = 0
  176.         local ostatok = ""
  177.         while (ostatok ~= " ") do
  178.             i = i + 1
  179.             ostatok = string.sub(text, b+i, b+i)
  180.         end
  181.         ostatok = string.sub(text, b+1, b+i-1)
  182.         ostatok = string.gsub(ostatok,",","")
  183.         return ostatok
  184.     else
  185.         return 0
  186.     end
  187. end
  188.  
  189. function sky.mathRound(roundIn , roundDig) --Округлить число
  190.     local mul = math.pow(10, roundDig)
  191.     return ( math.floor(( roundIn * mul) + 0.5)/mul)
  192. end
  193.  
  194. function sky.swap(array, index1, index2) --Свап
  195.     array[index1], array[index2] = array[index2], array[index1]
  196. end
  197.  
  198. function sky.shake(array) --Шафл
  199.     local counter = #array
  200.     while counter > 1 do
  201.         local index = math.random(counter)
  202.         sky.swap(array, index, counter)
  203.         counter = counter - 1
  204.     end
  205. end
  206.  
  207. function sky.get(url, filename,x,y) --Получить поток
  208.     local f, reason = io.open(filename, "w")
  209.     if not f then
  210.         g.set(x,y,"         Ошибка чтения файла         ")
  211.         os.sleep(2)
  212.         return
  213.     end
  214.  
  215.     g.set(x,y,"          Идёт скачивание...         ")
  216.     os.sleep(2)
  217.     local result, response = pcall(internet.request, url)
  218.     if result then
  219.         g.set(x,y,"        Файл успешно загружен        ")
  220.         os.sleep(2)
  221.         for chunk in response do
  222.             f:write(chunk)
  223.         end
  224.         f:close()
  225.     else
  226.         f:close()
  227.         fs.remove(filename)
  228.         g.set(x,y,"   HTTP запрос не дал результатов    ")
  229.         os.sleep(2)
  230.     end
  231. end
  232.  
  233. function sky.run(url, ...) --Запуск и удаление файла
  234.     local tmpFile = os.tmpname()
  235.     sky.read(url, tmpFile)
  236.     term.clear() -- <=== Очистка экрана перед запуском проги, если чё, она тута
  237.     local success, reason = shell.execute(tmpFile, nil, ...)
  238.     if not success then
  239.         --mid(23,"             Битый файлик             ")
  240.         --os.sleep(2)
  241.     end
  242.     fs.remove(tmpFile)
  243. end
  244.  
  245. function sky.checkOP(nick) --Чек на опку
  246.     local c = sky.com("whois " .. nick)
  247.     local _, b = string.find(c, "OP:§r ")
  248.     local text = string.sub(c, b+1, string.find(c, "Режим полета:"))
  249.     if string.find(text, "§aистина§r") ~= nil then
  250.         return true
  251.     else
  252.         return false
  253.     end
  254. end
  255.  
  256. function sky.playtime(nick) --Плейтайм
  257.     local c = sky.com("playtime " .. nick)
  258.     local _, b = string.find(c, "на сервере ")
  259.     local text = "error"
  260.     if string.find(c, "час") then
  261.         text = string.sub(c, b+1, string.find(c, " час")) .. " ч."
  262.     else
  263.         text = string.sub(c, b+1, string.find(c, " минут")) .. " мин."
  264.     end
  265.     return text
  266. end
  267.  
  268. function sky.checkMute(nick) --Чекнуть висит ли мут
  269.     local c = sky.com("checkban " .. nick)
  270.     if string.find(c, "Muted: §aFalse") ~= nil then
  271.         return false
  272.     else
  273.         return true
  274.     end
  275. end
  276.  
  277. function sky.getHostTime(timezone) --Получить текущее реальное время компьютера, хостящего сервер майна
  278.     timezone = timezone or 2
  279.     local file = io.open("/HostTime.tmp", "w")
  280.     file:write("123")
  281.     file:close()
  282.     local timeCorrection = timezone * 3600
  283.     local lastModified = tonumber(string.sub(fs.lastModified("/HostTime.tmp"), 1, -4)) + timeCorrection
  284.     fs.remove("HostTime.tmp")
  285.     local year, month, day, hour, minute, second = os.date("%Y", lastModified), os.date("%m", lastModified), os.date("%d", lastModified), os.date("%H", lastModified), os.date("%M", lastModified), os.date("%S", lastModified)
  286.     return tonumber(day), tonumber(month), tonumber(year), tonumber(hour), tonumber(minute), tonumber(second)
  287. end
  288.  
  289. function sky.time(timezone) --Получет настоящее время, стоящее на Хост-машине
  290.     local time = {sky.getHostTime(timezone)}
  291.     local text = string.format("%02d:%02d:%02d", time[4], time[5], time[6])
  292.     return text
  293. end
  294.  
  295. function sky.hex(Hcolor) --Конвертация Dec в Hex
  296.     local hex = "000000" .. string.format('%x', Hcolor)
  297.     hex = string.sub(hex, unicode.len(hex)-5, unicode.len(hex))
  298.     return hex
  299. end
  300.  
  301. function sky.dec(Dcolor) --Конвертация Hex в Dec
  302.     if Dcolor == "" then
  303.         Dcolor = "ffffff"
  304.     end
  305.     local dec = string.format('%d', '0x'.. Dcolor)
  306.     return tonumber(dec)
  307. end
  308.  
  309. function sky.TF(value) --Если false, то вернёт true и наоборот
  310.     if value then
  311.         return false
  312.     end
  313.     return true
  314. end
  315.  
  316. function sky.palitra(col) --Палитра
  317.     local OldColor = g.getForeground()
  318.     if col ~= nil then
  319.         OldColor = col
  320.         NewColor = col
  321.     end
  322.     local NewColor = g.getForeground()
  323.     local x,y = g.getResolution()
  324.     x = x/2-14
  325.     y = y/2-5
  326.     local palitra = {
  327.     {0x9b2d30, 0xff0000, 0xff9900, 0xffff00},
  328.     {0x66ff00, 0x008000, 0x00ffff, 0x0000ff},
  329.     {0x00cccc, 0x8b00ff, 0xff00ff, 0xf78fa7},
  330.     {0x666666, 0x222222, 0xffffff},
  331.     }
  332.  
  333.     g.setForeground(0x333333)
  334.  
  335.     g.set(x,y,  "█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█▀▀▀▀▀▀▀▀█")
  336.     g.set(x,y+1,"█                █ ██████ █")
  337.     g.set(x,y+2,"█                █ ██████ █")
  338.     g.set(x,y+3,"█                █▄▄▄▄▄▄▄▄█")
  339.     g.set(x,y+4,"█                █        █")
  340.     g.set(x,y+5,"█                █        █")
  341.     g.set(x,y+6,"█                █        █")
  342.     g.set(x,y+7,"█                █   Ок   █")
  343.     g.set(x,y+8,"█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█▄▄▄▄▄▄▄▄█")
  344.  
  345.     for i = 1, #palitra do
  346.         for j = 1, #palitra[i] do
  347.             g.setForeground(palitra[i][j])
  348.             g.set(j*4-4+x+2,i*2-2+y+1, "██")
  349.         end
  350.     end
  351.  
  352.     g.setForeground(OldColor)
  353.     g.set(x+19,y+1,"██████")
  354.     g.set(x+19,y+2,"██████")
  355.     g.set(x+19, y+5, sky.hex(OldColor))
  356.  
  357.     while true do
  358.         local e,_,w,h = event.pull("touch")
  359.         if e == "touch" then
  360.             for i = 1, #palitra do
  361.                 for j = 1, #palitra[i] do
  362.                     if w>=j*4-4+x+2 and w<=j*4-4+x+3 and h==i*2-2+y+1 then
  363.                         NewColor = palitra[i][j]
  364.                         g.setForeground(NewColor)
  365.                         g.set(x+19,y+1,"██████")
  366.                         g.set(x+19,y+2,"██████")
  367.                         g.set(x+19, y+5, sky.hex(NewColor))
  368.                     end
  369.                 end
  370.             end
  371.             if w>=x+19 and w<=x+24 and h==y+5 then
  372.                 g.set(x+19,y+5,"      ")
  373.                 term.setCursor(x+19,y+5)
  374.                 NewColor = sky.read({max = 6, accept = "0-9a-f", blink = true})
  375.                 NewColor = sky.dec(NewColor)
  376.                 g.setForeground(NewColor)
  377.                 g.set(x+19,y+1,"██████")
  378.                 g.set(x+19,y+2,"██████")
  379.                 g.set(x+19, y+5, sky.hex(NewColor))
  380.             elseif w>=x+21 and w<=x+22 and h==y+7 then
  381.                 g.setForeground(OldColor)
  382.                 g.fill(x,y,x+26,y+8," ")
  383.                 return NewColor
  384.             end
  385.         end
  386.     end
  387. end
  388.  
  389. function sky.pressButton(Pw,Ph,mass)
  390.     local x,y,w,h = mass[1], mass[2], mass[3], mass[4]
  391.     if Pw>=x and Pw<=x+w-1 and Ph>=y and Ph<=y+h-1 then
  392.         return true
  393.     end
  394.     return false
  395. end
  396.  
  397. function sky.drawButton(mass)
  398.     local x,y,w,h,text,col1,col2 = mass[1], mass[2], mass[3], mass[4], mass[5], mass[6], mass[7]
  399.     g.fill(x,y,w,h," ")
  400.     g.setForeground(col1)
  401.     g.set(x + w/2 - unicode.len(text)/2, y+h/2, text)
  402.     g.setForeground(col2)
  403.     for i = 1, w-2 do
  404.         g.set(x+i,y,"─")
  405.         g.set(x+i,y+h-1,"─")
  406.     end
  407.     for i = 1, h-2 do
  408.         g.set(x,y+i,"│")
  409.         g.set(x+w-1,y+i,"│")
  410.     end
  411.     g.set(x,y,"┌")
  412.     g.set(x+w-1,y,"┐")
  413.     g.set(x,y+h-1,"└")
  414.     g.set(x+w-1,y+h-1,"┘")
  415. end
  416.  
  417. function sky.pressSwitch(Pw,Ph, mass)
  418.     local x,y = mass[1], mass[2]
  419.     if Pw>=x and Pw<=x+4 and Ph>=y and Ph<=y+2 then
  420.         return true
  421.     end
  422.     return false
  423. end
  424.  
  425. function sky.drawSwitch(mass) --Свич
  426.     local x,y,col1,col2,value = mass[1], mass[2], mass[3], mass[4], mass[5]
  427.     g.setForeground(col2)
  428.     g.set(x,y,  "┌───┐")
  429.     g.set(x,y+1,"│   │")
  430.     g.set(x,y+2,"└───┘")
  431.     if value == true then
  432.         g.setForeground(col1)
  433.         g.set(x+2,y+1,"√")
  434.     elseif value ~= false then
  435.         g.setForeground(value)
  436.         g.set(x+1,y+1,"▐█▌")
  437.         g.setForeground(col2)
  438.     end
  439. end
  440.  
  441. function sky.word(x,y,text,ramka) --Шрифт
  442.     text = unicode.lower(text)
  443.     for i = 1, unicode.len(text) do
  444.         sky.symbol(i*8-8 + x, y, string.sub(text,i,i), ramka)
  445.     end
  446. end
  447.  
  448. function sky.symbol(x,y,symbol,ramka) --Символы шрифта
  449.     local WBack = g.getBackground()
  450.    
  451.     if ramka ~= nil then
  452.         local WColor = g.getForeground()
  453.         g.setForeground(ramka)
  454.         g.set(x,y,  "███████")
  455.         g.set(x,y+1,"███████")
  456.         g.set(x,y+2,"███████")
  457.         g.set(x,y+3,"▀▀▀▀▀▀▀")
  458.         g.setBackground(ramka)
  459.         g.setForeground(WColor)
  460.     end
  461.    
  462.     if symbol == "a" then
  463.         g.set(x+1,y,  "▄▄▄▄▄")
  464.         g.set(x+1,y+1,"█▄▄▄█")
  465.         g.set(x+1,y+2,"█   █")
  466.     elseif symbol == "b" then
  467.         g.set(x+1,y,  "▄▄▄▄")
  468.         g.set(x+1,y+1,"█▄▄█▄")
  469.         g.set(x+1,y+2,"█▄▄▄█")
  470.     elseif symbol == "c" then
  471.         g.set(x+1,y,  "▄▄▄▄▄")
  472.         g.set(x+1,y+1,"█")
  473.         g.set(x+1,y+2,"█▄▄▄▄")
  474.     elseif symbol == "d" then
  475.         g.set(x+1,y,  "▄▄▄▄")
  476.         g.set(x+1,y+1,"█   █")
  477.         g.set(x+1,y+2,"█▄▄▄▀")
  478.     elseif symbol == "e" then
  479.         g.set(x+1,y,  "▄▄▄▄▄")
  480.         g.set(x+1,y+1,"█▄▄▄")
  481.         g.set(x+1,y+2,"█▄▄▄▄")
  482.     elseif symbol == "f" then
  483.         g.set(x+1,y,  "▄▄▄▄▄")
  484.         g.set(x+1,y+1,"█▄▄")
  485.         g.set(x+1,y+2,"█")
  486.     elseif symbol == "g" then
  487.         g.set(x+1,y,  "▄▄▄▄▄")
  488.         g.set(x+1,y+1,"█  ▄▄")
  489.         g.set(x+1,y+2,"█▄▄▄█")
  490.     elseif symbol == "h" then
  491.         g.set(x+1,y,  "▄   ▄")
  492.         g.set(x+1,y+1,"█▄▄▄█")
  493.         g.set(x+1,y+2,"█   █")
  494.     elseif symbol == "i" then
  495.         g.set(x+2,y,  "▄▄▄")
  496.         g.set(x+2,y+1," █")
  497.         g.set(x+2,y+2,"▄█▄")
  498.     elseif symbol == "j" then
  499.         g.set(x+1,y,  "▄▄▄▄▄")
  500.         g.set(x+1,y+1,"    █")
  501.         g.set(x+1,y+2,"█▄▄▄█")
  502.     elseif symbol == "k" then
  503.         g.set(x+1,y,  "▄  ▄")
  504.         g.set(x+1,y+1,"█▄▀")
  505.         g.set(x+1,y+2,"█ ▀▄")
  506.     elseif symbol == "l" then
  507.         g.set(x+1,y,  "▄")
  508.         g.set(x+1,y+1,"█")
  509.         g.set(x+1,y+2,"█▄▄▄")
  510.     elseif symbol == "m" then
  511.         g.set(x+1,y,  "▄   ▄")
  512.         g.set(x+1,y+1,"█▀▄▀█")
  513.         g.set(x+1,y+2,"█   █")
  514.     elseif symbol == "n" then
  515.         g.set(x+1,y,  "▄▄  ▄")
  516.         g.set(x+1,y+1,"█ █ █")
  517.         g.set(x+1,y+2,"█  ▀█")
  518.     elseif symbol == "o" then
  519.         g.set(x+1,y,  "▄▄▄▄▄")
  520.         g.set(x+1,y+1,"█   █")
  521.         g.set(x+1,y+2,"█▄▄▄█")
  522.     elseif symbol == "p" then
  523.         g.set(x+1,y,  "▄▄▄▄▄")
  524.         g.set(x+1,y+1,"█▄▄▄█")
  525.         g.set(x+1,y+2,"█")
  526.     elseif symbol == "q" then
  527.         g.set(x+1,y,  "▄▄▄▄▄")
  528.         g.set(x+1,y+1,"█   █")
  529.         g.set(x+1,y+2,"█▄▄██")
  530.     elseif symbol == "r" then
  531.         g.set(x+1,y,  "▄▄▄▄▄")
  532.         g.set(x+1,y+1,"█▄▄▄█")
  533.         g.set(x+1,y+2,"█  ▀▄")
  534.     elseif symbol == "s" then
  535.         g.set(x+1,y,  "▄▄▄▄▄")
  536.         g.set(x+1,y+1,"█▄▄▄▄")
  537.         g.set(x+1,y+2,"▄▄▄▄█")
  538.     elseif symbol == "t" then
  539.         g.set(x+1,y,  "▄▄▄▄▄")
  540.         g.set(x+1,y+1,"  █")
  541.         g.set(x+1,y+2,"  █")
  542.     elseif symbol == "u" then
  543.         g.set(x+1,y,  "▄   ▄")
  544.         g.set(x+1,y+1,"█   █")
  545.         g.set(x+1,y+2,"▀▄▄▄▀")
  546.     elseif symbol == "v" then
  547.         g.set(x+1,y,  "▄   ▄")
  548.         g.set(x+1,y+1,"█   █")
  549.         g.set(x+1,y+2," ▀▄▀")
  550.     elseif symbol == "w" then
  551.         g.set(x+1,y,  "▄   ▄")
  552.         g.set(x+1,y+1,"█ █ █")
  553.         g.set(x+1,y+2,"▀▄█▄▀")
  554.     elseif symbol == "x" then
  555.         g.set(x+1,y,  "▄   ▄")
  556.         g.set(x+1,y+1," ▀▄▀")
  557.         g.set(x+1,y+2,"▄▀ ▀▄")
  558.     elseif symbol == "y" then
  559.         g.set(x+1,y,  "▄   ▄")
  560.         g.set(x+1,y+1," ▀▄▀ ")
  561.         g.set(x+1,y+2,"  █")
  562.     elseif symbol == "z" then
  563.         g.set(x+1,y,  "▄▄▄▄▄")
  564.         g.set(x+1,y+1," ▄▄▄▀")
  565.         g.set(x+1,y+2,"█▄▄▄▄")
  566.     end
  567.     g.setBackground(WBack)
  568. end
  569.  
  570. function sky.read(settings)
  571.     --mask, max, rim - returnIfMax, accept, blink, center, nick, clip, maxDisplay
  572.     if not settings then
  573.         settings = {}
  574.     end
  575.     local nextBlink = 0
  576.     if not settings.text then
  577.         settings.text = ""
  578.     end
  579.     if settings.blink ~= false then
  580.         settings.blink = true
  581.     end
  582.     if settings.max then
  583.         settings.max = settings.max - 1
  584.     else
  585.         settings.max = 999
  586.     end
  587.     local sx, sy = term.getCursor()
  588.     local oldFg = g.getForeground()
  589.     local oldBg = g.getBackground()
  590.     local SX = sx
  591.     while true do
  592.        
  593.         function maxD(maxd, text)
  594.             if maxd and maxd <= unicode.len(text) then
  595.                 return string.sub(text, unicode.len(text)-maxd, unicode.len(text))
  596.             else
  597.                 return text
  598.             end
  599.         end
  600.        
  601.         local tempText = maxD(settings.maxDisplay, settings.text)
  602.        
  603.         if settings.center then
  604.             SX = sx - unicode.len(tempText) / 2
  605.         end
  606.        
  607.         local event, address, char, code, nick = event.pull(settings.blink and math.min(0, nextBlink - computer.uptime())) --, "key_down"
  608.         if char == 13 and event == "key_down" then --Enter
  609.             local char, fg, bg = g.get(SX + unicode.len(tempText), sy)
  610.             if settings.center then
  611.                 SX = sx - unicode.len(tempText) / 2
  612.             end
  613.             g.set(SX + unicode.len(tempText), sy, char or " ")
  614.             if settings.nick then
  615.                 return settings.text, nick
  616.             end
  617.             return settings.text
  618.         elseif char == 8 and event == "key_down" then --Backspace
  619.             settings.text = unicode.sub(settings.text, 1, -2)
  620.             tempText = unicode.sub(tempText, 1, -2)
  621.             if settings.center then
  622.                 SX = sx - unicode.len(tempText) / 2
  623.             end
  624.             g.set(SX, sy, (not settings.mask and tempText or settings.mask:rep(unicode.len(tempText))) .. "  ")
  625.             if settings.center then
  626.                 g.set(SX-1, sy, " ")
  627.             end
  628.         elseif char and char ~= 0 and event == "key_down" then --Keyboard
  629.             local acceptRegx = settings.accept and ("[" .. settings.accept .. "]") or "."
  630.             if unicode.char(char):find(acceptRegx) then
  631.                 if settings.max and settings.max >= unicode.len(settings.text) then
  632.                     settings.text = settings.text .. unicode.char(char)
  633.                     tempText = tempText .. unicode.char(char)
  634.                     if settings.center then
  635.                         SX = sx - unicode.len(tempText) / 2
  636.                     end
  637.                     g.set(SX, sy, not settings.mask and tempText or settings.mask:rep(unicode.len(tempText)))
  638.                     if settings.max and settings.rim and settings.max == unicode.len(settings.text) then
  639.                         local char, fg, bg = g.get(SX + unicode.len(tempText), sy)
  640.                         g.set(SX + unicode.len(tempText), sy, char or " ")
  641.                         return settings.text
  642.                     end
  643.                 end
  644.             end
  645.         elseif event == "clipboard" and settings.clip then --Paste
  646.             settings.text = settings.text .. char
  647.             if settings.max and settings.max < unicode.len(settings.text) then
  648.                 settings.text = string.sub(settings.text, 1, settings.max - 3) .. "..."
  649.             end
  650.             tempText = maxD(settings.maxDisplay, settings.text)
  651.             if settings.center then
  652.                 SX = sx - unicode.len(tempText) / 2
  653.             end
  654.             g.set(SX, sy, not settings.mask and tempText or settings.mask:rep(unicode.len(tempText)))
  655.         end
  656.  
  657.         if settings.blink and nextBlink <= computer.uptime() then
  658.             oldFg = g.getForeground()
  659.             oldBg = g.getBackground()
  660.             local char, fg, bg = g.get(SX + unicode.len(tempText), sy)
  661.             g.setForeground(bg)
  662.             g.setBackground(fg)
  663.             g.set(SX + unicode.len(tempText), sy, char or " ")
  664.             g.setForeground(oldFg)
  665.             g.setBackground(oldBg)
  666.             nextBlink = computer.uptime() + 0.5
  667.         end
  668.     end
  669. end
  670.  
  671. --Old function
  672. function sky.clearL(h) --Очистка левой части
  673.     g.fill(3,2,26,h-2," ")
  674. end
  675.  
  676. function sky.clearR(w,h) --Очистка правой части
  677.     g.fill(31,2,w-32,h-2," ")
  678. end
  679.  
  680. function sky.midL(w,y,text) --Центровка слева
  681.     local _,n = string.gsub(text, "&","")
  682.     local l = unicode.len(text) - n * 2
  683.     x = 13 - (l / 2)
  684.     sky.text(x+2, y, text)
  685. end
  686.  
  687. function sky.midR(w,y,text) --Центровка справа
  688.     local _,n = string.gsub(text, "&","")
  689.     local l = unicode.len(text) - n * 2
  690.     x = ((w - 34) / 2) - (l / 2)
  691.     sky.text(x+31, y, text)
  692. end
  693.  
  694. return sky
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement