Ewgeniy

Untitled

Sep 24th, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.20 KB | None | 0 0
  1.  
  2. ------------------------------------------ Библиотеки ---------------------------------------------------------------
  3.  
  4. local ecs = require("ECSAPI")
  5. local fs = require("filesystem")
  6. local gpu = require("component").gpu
  7. local unicode = require("unicode")
  8. local event = require("event")
  9. local serialization = require("serialization")
  10.  
  11. ------------------------------------------ Переменные ---------------------------------------------------------------
  12.  
  13. local width, height = 70, 30
  14. local x, y = ecs.correctStartCoords("auto", "auto", width, height)
  15. local oldPixels = ecs.rememberOldPixels(x, y, x + width - 1, y + height - 1)
  16. local dataWidth, dataHeight = width - 6, height - 14
  17. local drawListFrom = 1
  18. local selectedObject = 3
  19. local pathToList = "MineOS/System/AutorunManager/Filelist.txt"
  20. local autorunObjects = {
  21.     -- { path = "OS.lua", enabled = true, size = 30 },
  22.     -- { path = "Cyka/Home.lua", enabled = true, size = 30 },
  23.     -- { path = "Pidar/Lalra/Cyka.lua", enabled = true, size = 30 },
  24.     -- { path = "322File.lua", enabled = false, size = 30 },
  25.     -- { path = "SasiHyu.lua", enabled = true, size = 30 },
  26.     -- { path = "Blabla.cfg", enabled = false, size = 30 },
  27. }
  28.  
  29. ------------------------------------------ Функции ---------------------------------------------------------------
  30.  
  31. --Объекты для тача
  32. local obj = {}
  33. local function newObj(class, name, ...)
  34.     obj[class] = obj[class] or {}
  35.     obj[class][name] = {...}
  36. end
  37.  
  38. local function saveAutorun()
  39.     local file = io.open("autorun.lua", "w")
  40.     file:write("local success, reason\n")
  41.     for i = 1, #autorunObjects do
  42.         if autorunObjects[i].enabled then
  43.             file:write("success, reason = pcall(loadfile(\"" .. autorunObjects[i].path .. "\")); if not success then print(\"Ошибка: \" .. tostring(reason)) end\n")
  44.         end
  45.     end
  46.     file:close()
  47. end
  48.  
  49. local function saveList()
  50.     local file = io.open(pathToList, "w")
  51.     file:write(serialization.serialize(autorunObjects))
  52.     file:close()
  53. end
  54.  
  55. local function loadList()
  56.     if fs.exists(pathToList) then
  57.         local file = io.open(pathToList, "r")
  58.         local text = file:read("*a")
  59.         autorunObjects = serialization.unserialize(text)
  60.         file:close()
  61.     else
  62.         fs.makeDirectory(fs.path(pathToList))
  63.         saveList()
  64.     end
  65. end
  66.  
  67. local function drawFiles(x, y)
  68.  
  69.     local limit, from = dataHeight, drawListFrom
  70.  
  71.     obj["List"] = {}
  72.  
  73.     local yPos = y + 1
  74.  
  75.     ecs.square(x, y, dataWidth, dataHeight, 0xFFFFFF)
  76.     ecs.square(x, y, dataWidth, 1, ecs.colors.blue)
  77.     gpu.setForeground(0xFFFFFF)
  78.     gpu.set(x + 1, y, "Запуск")
  79.     gpu.set(x + 9, y, "Файл")
  80.     gpu.set(x + 48, y, "Размер")
  81.  
  82.     ecs.srollBar(x + dataWidth - 1, yPos, 1, dataHeight - 1, #autorunObjects == 0 and from or #autorunObjects, from, 0xCCCCCC, ecs.colors.lightBlue)
  83.  
  84.  
  85.     local color, color2
  86.     for i = from, (from + #autorunObjects - 1) do
  87.         if i > limit then break end
  88.         if i % 2 == 0 then color = 0xFFFFFF; color2 = 0x262626 else color = 0xEEEEEE; color2 = 0x262626 end
  89.         if i == selectedObject then color = ecs.colors.green; color2 = 0xFFFFFF end
  90.  
  91.         if autorunObjects[i] then
  92.  
  93.             ecs.square(x, yPos, dataWidth - 1, 1, color)
  94.  
  95.             if autorunObjects[i].enabled then
  96.                 ecs.colorTextWithBack(x + 3, yPos, ecs.colors.blue, color, "✔")
  97.             else
  98.                 ecs.colorTextWithBack(x + 3, yPos, ecs.colors.red, color, "❌")
  99.             end
  100.  
  101.             gpu.setBackground(color)
  102.             gpu.setForeground(color2)
  103.             gpu.set(x + 9, yPos, ecs.stringLimit("start", autorunObjects[i].path, 37))
  104.  
  105.             gpu.set(x + 48, yPos, autorunObjects[i].size .. " КБ")
  106.  
  107.             newObj("List", i, x, yPos, x + dataWidth - 1, yPos)
  108.  
  109.             yPos = yPos + 1
  110.         end
  111.     end
  112. end
  113.  
  114. local function drawWindow()
  115.     local xPos, yPos
  116.  
  117.     ecs.square(x, y, width, height, 0xDDDDDD)
  118.  
  119.     xPos = x + 3
  120.     yPos = y + 4
  121.  
  122.     ecs.colorText(xPos, yPos, 0x262626, "Эти объекты будут запускаться автоматически при загрузке:")
  123.     yPos = yPos + 2
  124.  
  125.     ecs.square(x, y, width, 3, 0xCCCCCC)
  126.  
  127.     ecs.centerText("x", y + 1, "Менеджер автозагрузки")
  128.  
  129.     drawFiles(x + 3, y + 6)
  130.  
  131.     yPos = y + height - 7
  132.     ecs.colorTextWithBack(xPos, yPos, 0x262626, 0xDDDDDD, "Чтобы отключить загрузку файла, снимите галочку рядом с именем")
  133.     yPos = yPos + 1
  134.     gpu.set(xPos, yPos, "программы. Приоритет загузки снижается сверху вниз.")
  135.     yPos = yPos + 2
  136.  
  137.     local name
  138.     name = "+"; newObj("Buttons", name, ecs.drawAdaptiveButton(xPos, yPos, 2, 1, name, 0xFFFFFF, 0x262626)); xPos = obj["Buttons"][name][3] + 2
  139.     name = "-"; newObj("Buttons", name, ecs.drawAdaptiveButton(xPos, yPos, 2, 1, name, 0xFFFFFF, 0x262626)); xPos = obj["Buttons"][name][3] + 2
  140.  
  141.     name = "Выше"; newObj("Buttons", name, ecs.drawAdaptiveButton(xPos, yPos, 2, 1, name, 0xFFFFFF, 0x262626)); xPos = obj["Buttons"][name][3] + 2
  142.     name = "Ниже"; newObj("Buttons", name, ecs.drawAdaptiveButton(xPos, yPos, 2, 1, name, 0xFFFFFF, 0x262626)); xPos = obj["Buttons"][name][3] + 2
  143.     -- if fs.isAutorunEnabled() then
  144.     --  name = "Выключить автозапуск"
  145.     -- else
  146.     --  name = "Включить автозапуск "
  147.     -- end
  148.     -- newObj("Buttons", name, ecs.drawAdaptiveButton(xPos, yPos, 2, 1, name, 0xAAAAAA, 0xFFFFFF)); xPos = obj["Buttons"][name][3] + 2
  149.  
  150.     xPos = x + width - 12
  151.     name = "Выход"; newObj("Buttons", name, ecs.drawAdaptiveButton(xPos, yPos, 2, 1, name, 0x888888, 0xFFFFFF))
  152. end
  153.  
  154. ------------------------------------------ Программа ---------------------------------------------------------------
  155.  
  156. loadList()
  157. drawWindow()
  158.  
  159. while true do
  160.     local e = {event.pull()}
  161.     if e[1] == "touch" then
  162.         --if obj["List"] and #obj["List"] > 0 then
  163.             for key in pairs(obj["List"]) do
  164.                 if ecs.clickedAtArea(e[3], e[4], obj["List"][key][1], obj["List"][key][2], obj["List"][key][3], obj["List"][key][4]) then
  165.                     if selectedObject ~= key then
  166.                         selectedObject = key
  167.                         drawFiles(x + 3, y + 6)
  168.                     else
  169.                         if e[3] >= obj["List"][key][1] + 2 and e[3] <= obj["List"][key][1] + 4 then
  170.                             autorunObjects[key].enabled = not autorunObjects[key].enabled
  171.                             drawFiles(x + 3, y + 6)
  172.                             saveList()
  173.                             saveAutorun()
  174.                         end
  175.                     end
  176.                     break
  177.                 end
  178.             end
  179.         --end
  180.  
  181.         for key in pairs(obj["Buttons"]) do
  182.             if ecs.clickedAtArea(e[3], e[4], obj["Buttons"][key][1], obj["Buttons"][key][2], obj["Buttons"][key][3], obj["Buttons"][key][4]) then
  183.                 ecs.drawAdaptiveButton(obj["Buttons"][key][1], obj["Buttons"][key][2], 2, 1, key, ecs.colors.blue, 0xFFFFFF)
  184.                 os.sleep(0.2)
  185.                 ecs.drawAdaptiveButton(obj["Buttons"][key][1], obj["Buttons"][key][2], 2, 1, key, 0xFFFFFF, 0x262626)
  186.                
  187.                 if key == "-" then
  188.                     table.remove(autorunObjects, selectedObject)
  189.                     if drawListFrom == selectedObject then drawListFrom = 1 end
  190.                     drawFiles(x + 3, y + 6)
  191.                     saveList()
  192.                     saveAutorun()
  193.                 elseif key == "+" then
  194.                     local data = ecs.universalWindow("auto", "auto", 36, 0xeeeeee, true, {"EmptyLine"}, {"CenterText", 0x880000, "Добавить новый файл"}, {"EmptyLine"}, {"Input", 0x262626, 0x880000, "Путь к файлу"}, {"EmptyLine"}, {"Button", {0x888888, 0xffffff, "Добавить"}, {0xAAAAAA, 0xffffff, "Отмена"}})
  195.                     if data[2] == "Добавить" then
  196.                         if fs.exists(data[1]) then
  197.                             local cyka = false
  198.                             for i = 1, #autorunObjects do
  199.                                 if autorunObjects[i].path == data[1] then cyka = true end
  200.                             end
  201.                             if not cyka then
  202.                                 table.insert(autorunObjects, { path = data[1], enabled = true, size = math.ceil(fs.size(data[1]) / 1024) })
  203.                                 drawFiles(x + 3, y + 6)
  204.                                 saveList()
  205.                                 saveAutorun()
  206.                             else
  207.                                 ecs.error("Файл \"" .. data[1] .. "\" уже есть в этом списке!")
  208.                             end
  209.                         else
  210.                             ecs.error("Файл \"" .. data[1] .. "\" не существует!")
  211.                         end
  212.                     end
  213.                 elseif key == "Выше" then
  214.                     if selectedObject > 1 then
  215.                         local cyka = autorunObjects[selectedObject]
  216.                         table.remove(autorunObjects, selectedObject)
  217.                         table.insert(autorunObjects, selectedObject - 1, cyka)
  218.                         selectedObject = selectedObject - 1
  219.                         drawFiles(x + 3, y + 6)
  220.                         saveList()
  221.                         saveAutorun()
  222.                     end
  223.                 elseif key == "Ниже" then
  224.                     if selectedObject < #autorunObjects then
  225.                         local cyka = autorunObjects[selectedObject]
  226.                         table.remove(autorunObjects, selectedObject)
  227.                         table.insert(autorunObjects, selectedObject + 1, cyka)
  228.                         selectedObject = selectedObject + 1
  229.                         drawFiles(x + 3, y + 6)
  230.                         saveList()
  231.                         saveAutorun()
  232.                     end
  233.                 elseif key == "Выход" then
  234.                     ecs.drawOldPixels(oldPixels)
  235.                     return
  236.                 -- elseif key == "Включить автозапуск " then
  237.                 --  fs.setAutorunEnabled(true)
  238.                 --  drawWindow()
  239.                 -- elseif key == "Выключить автозапуск" then
  240.                 --  fs.setAutorunEnabled(false)
  241.                 --  drawWindow()
  242.                 end
  243.  
  244.                 break
  245.             end
  246.         end
  247.  
  248.     elseif e[1] == "scroll" then
  249.         if e[5] == 1 then
  250.             if drawListFrom > 1 then drawListFrom = drawListFrom - 1; drawFiles(x + 3, y + 6) end
  251.         else
  252.             if drawListFrom < #autorunObjects then drawListFrom = drawListFrom + 1; drawFiles(x + 3, y + 6) end
  253.         end
  254.     end
  255. end
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
Advertisement
Add Comment
Please, Sign In to add comment