Maxstripe

3dPrintInstaller(computercraft.ru)(OC)

Jul 16th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ------------------------------------------- Библиотеки -------------------------------------------
  2.  
  3. local shell = require("shell")
  4. local component = require("component")
  5. local unicode = require("unicode")
  6. local fs = require("filesystem")
  7. local gpu = component.gpu
  8.  
  9. ------------------------------------------- Переменные -------------------------------------------
  10.  
  11. --Массив с программами, которые необходимо загрузить. Первый элемент - ссылка на файл, второй - путь для сохранения файла.
  12. local applications = {
  13.     { "https://raw.githubusercontent.com/IgorTimofeev/OpenComputers/master/lib/colorlib.lua", "lib/colorlib.lua" },
  14.     { "https://raw.githubusercontent.com/IgorTimofeev/OpenComputers/master/lib/doubleBuffering.lua", "lib/doubleBuffering.lua" },
  15.     { "https://raw.githubusercontent.com/IgorTimofeev/OpenComputers/master/lib/context.lua", "lib/context.lua" },
  16.     { "https://raw.githubusercontent.com/IgorTimofeev/OpenComputers/master/lib/palette.lua", "lib/palette.lua" },
  17.     { "https://raw.githubusercontent.com/IgorTimofeev/OpenComputers/master/lib/ECSAPI.lua", "lib/ECSAPI.lua" },
  18.     { "https://raw.githubusercontent.com/IgorTimofeev/OpenComputers/master/lib/bigLetters.lua", "lib/bigLetters.lua" },
  19.     { "https://raw.githubusercontent.com/IgorTimofeev/OpenComputers/master/lib/image.lua", "lib/image.lua" },
  20.     { "https://raw.githubusercontent.com/IgorTimofeev/OpenComputers/master/lib/colorlib.lua", "lib/colorlib.lua" },
  21.  
  22.     { "https://raw.githubusercontent.com/IgorTimofeev/OpenComputers/master/Applications/3DPrint/3DPrint.lua", "3DPrint.lua" },
  23. }
  24.  
  25. --Получаем размеры монитора
  26. local xSize, ySize = gpu.getResolution()
  27.  
  28. ------------------------------------------- Функции -------------------------------------------
  29.  
  30. --Отрисовка квадрата указанного цвета
  31. local function square(x, y, width, height, background)
  32.     gpu.setBackground(background)
  33.     gpu.fill(x, y, width, height, " ")
  34. end
  35.  
  36. --Запомнить область пикселей и возвратить ее в виде массива
  37. local function rememberOldPixels(x, y, x2, y2)
  38.     local newPNGMassiv = { ["backgrounds"] = {} }
  39.     newPNGMassiv.x, newPNGMassiv.y = x, y
  40.     --Перебираем весь массив стандартного PNG-вида по высоте
  41.     local xCounter, yCounter = 1, 1
  42.     for j = y, y2 do
  43.         xCounter = 1
  44.         for i = x, x2 do
  45.             if (i > xSize or i < 0) or (j > ySize or j < 0) then
  46.                 error("Can't remember pixel, because it's located behind the screen: x("..i.."), y("..j..") out of xSize("..xSize.."), ySize("..ySize..")\n")
  47.             end
  48.             local symbol, fore, back = gpu.get(i, j)
  49.             newPNGMassiv["backgrounds"][back] = newPNGMassiv["backgrounds"][back] or {}
  50.             newPNGMassiv["backgrounds"][back][fore] = newPNGMassiv["backgrounds"][back][fore] or {}
  51.             table.insert(newPNGMassiv["backgrounds"][back][fore], {xCounter, yCounter, symbol} )
  52.  
  53.             xCounter = xCounter + 1
  54.             back, fore, symbol = nil, nil, nil
  55.         end
  56.         yCounter = yCounter + 1
  57.     end
  58.     return newPNGMassiv
  59. end
  60.  
  61. --Нарисовать запомненные ранее пиксели из массива
  62. local function drawOldPixels(massivSudaPihay)
  63.     --Перебираем массив с фонами
  64.     for back, backValue in pairs(massivSudaPihay["backgrounds"]) do
  65.         gpu.setBackground(back)
  66.         for fore, foreValue in pairs(massivSudaPihay["backgrounds"][back]) do
  67.             gpu.setForeground(fore)
  68.             for pixel = 1, #massivSudaPihay["backgrounds"][back][fore] do
  69.                 if massivSudaPihay["backgrounds"][back][fore][pixel][3] ~= transparentSymbol then
  70.                     gpu.set(massivSudaPihay.x + massivSudaPihay["backgrounds"][back][fore][pixel][1] - 1, massivSudaPihay.y + massivSudaPihay["backgrounds"][back][fore][pixel][2] - 1, massivSudaPihay["backgrounds"][back][fore][pixel][3])
  71.                 end
  72.             end
  73.         end
  74.     end
  75. end
  76.  
  77. --Шкала прогресса
  78. local function progressBar(x, y, width, height, background, foreground, percent)
  79.     local activeWidth = math.ceil(width * percent / 100)
  80.     square(x, y, width, height, background)
  81.     square(x, y, activeWidth, height, foreground)
  82. end
  83.  
  84. --Окно загрузки
  85. local function downloadWindow()
  86.     --Задаем стартовые координаты
  87.     local width, height = 60, 7
  88.     local progressBarWidth = math.floor(width * 3 / 4)
  89.     local x, y = math.floor(xSize / 2 - width / 2), math.floor(ySize / 2 - height / 2) + 1
  90.     --Запоминаем старые пиксели
  91.     local oldPixels = rememberOldPixels(x, y, x + width - 1, y + height - 1)
  92.     --Рисуем верхний тулбар
  93.     square(x, y, width, 1, 0xcccccc)
  94.     local text = "Загрузка"
  95.     local xPos, yPos = math.floor(xSize / 2 - unicode.len(text) / 2), y
  96.     gpu.setForeground( 0x000000 )
  97.     gpu.set(xPos, yPos, text)
  98.     --Рисуем само окно
  99.     square(x, y + 1, width, height - 1, 0xeeeeee)
  100.     --Загружаем файлы и рисуем шкалу прогресса
  101.     xPos, yPos = math.floor(xSize / 2 - progressBarWidth / 2), math.floor(ySize / 2)
  102.     local percent = 0
  103.     for i = 1, #applications do
  104.         percent = math.floor(100 * i / #applications)
  105.         progressBar(xPos, yPos, progressBarWidth, 1, 0xcccccc, 0x3366CC, percent)
  106.         square(xPos, yPos + 1, progressBarWidth, 1, 0xeeeeee)
  107.         gpu.setForeground(0x262626)
  108.         gpu.set(xPos, yPos + 1, "Устанавливаю " .. applications[i][2])
  109.         fs.makeDirectory(fs.path(applications[i][2]) or "")    
  110.         shell.execute("wget " .. applications[i][1] .. " " .. applications[i][2] .. " -fQ")
  111.     end
  112.     os.sleep(0.3)
  113.     --Выпиливаем нарисованное окно
  114.     drawOldPixels(oldPixels)
  115. end
  116.  
  117. ------------------------------------------- Программа -------------------------------------------
  118.  
  119. downloadWindow()
Add Comment
Please, Sign In to add comment