Advertisement
Belzebub

installButton.lua

Apr 7th, 2018
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.81 KB | None | 0 0
  1. local component = require("component")
  2. local unicode = require("unicode")
  3. local fs = require("filesystem")
  4. local event = require("event")
  5. local gpu = component.gpu
  6. local internet = component.internet
  7.  
  8. ---------------------------------------------------------------------------------------------------------------------------------
  9.  
  10. -- Specify required files for downloading
  11. local files = {
  12. {
  13. url = "https://raw.githubusercontent.com/Be1zebub/Button.lua/master/button.txt",
  14. path = "/button/1.pic"
  15. },
  16. {
  17. url = "https://raw.githubusercontent.com/Be1zebub/Button.lua/master/button2.txt",
  18. path = "/button/2.pic"
  19. },
  20. {
  21. url = "https://raw.githubusercontent.com/Be1zebub/Button.lua/master/button3.txt",
  22. path = "/button/3.pic"
  23. },
  24. {
  25. url = "https://raw.githubusercontent.com/Be1zebub/Button.lua/master/button4.txt",
  26. path = "/button/4.pic"
  27. },
  28. {
  29. url = "https://raw.githubusercontent.com/Be1zebub/Button.lua/master/button5.txt",
  30. path = "/button/5.pic"
  31. },
  32. {
  33. url = "https://raw.githubusercontent.com/Be1zebub/Button.lua/master/button6.txt",
  34. path = "/button/6.pic"
  35. },
  36. {
  37. url = "https://raw.githubusercontent.com/Be1zebub/Button.lua/master/button7.txt",
  38. path = "/button/7.pic"
  39. },
  40. {
  41. url = "https://raw.githubusercontent.com/Be1zebub/Button.lua/master/button8.txt",
  42. path = "/button/8.pic"
  43. },
  44. {
  45. url = "https://raw.githubusercontent.com/Be1zebub/Button.lua/master/button9.txt",
  46. path = "/button/9.pic"
  47. },
  48. {
  49. url = "https://raw.githubusercontent.com/Be1zebub/Button.lua/master/button10.txt",
  50. path = "/button/10.pic"
  51. },
  52. {
  53. url = "https://raw.githubusercontent.com/Be1zebub/Button.lua/master/button11.txt",
  54. path = "/button/11.pic"
  55. },
  56. {
  57. url = "https://raw.githubusercontent.com/Be1zebub/Button.lua/master/button12.txt",
  58. path = "/button/12.pic"
  59. },
  60. {
  61. url = "https://raw.githubusercontent.com/Be1zebub/Button.lua/master/button.lua",
  62. path = "/button/button.lua"
  63. },
  64.  
  65. }
  66.  
  67. local properties = {
  68. -- Comment any coordinate to calculate it automatically (will centerize window on screen by specified axis)
  69. -- windowX = 2,
  70. -- windowY = 2,
  71. -- Set window width value lower than zero (0.5 for example) to calculate it dependent on screen width
  72. windowWidth = 56,
  73. -- Customize offset by X axis from window corners
  74. GUIElementsOffset = 2,
  75. -- Customize localization as you want to
  76. localization = {
  77. -- Specify title of your installer
  78. title = "Image framework installer",
  79. -- Use <currentProgress>, <totalProgress> and <currentFile> text insertions to automatically display their values
  80. currentFile = "Downloading \"<currentFile>\"",
  81. totalProgress = "Total progress: <totalProgress>%",
  82. -- Comment this lines to automatically close installer window
  83. finished1 = "Image framework has been successfully installed",
  84. finished2 = "Press any key to quit"
  85. },
  86. -- Customize color scheme as you want to
  87. colors = {
  88. window = {
  89. background = 0xEEEEEE,
  90. text = 0x999999,
  91. shadow = 0x3C3C3C
  92. },
  93. title = {
  94. background = 0xCCCCCC,
  95. text = 0x555555,
  96. },
  97. progressBar = {
  98. active = 0x0092FF,
  99. passive = 0xCCCCCC
  100. }
  101. }
  102. }
  103.  
  104. ---------------------------------------------------------------------------------------------------------------------------------
  105.  
  106. local screenWidth, screenHeight = gpu.getResolution()
  107. properties.windowHeight = 8
  108.  
  109. if properties.windowWidth < 1 then
  110. properties.windowWidth = math.floor(screenWidth * properties.windowWidth)
  111. end
  112. progressBarWidth = properties.windowWidth - properties.GUIElementsOffset * 2
  113.  
  114. if not properties.windowX then
  115. properties.windowX = math.floor(screenWidth / 2 - properties.windowWidth / 2)
  116. end
  117.  
  118. if not properties.windowY then
  119. properties.windowY = math.floor(screenHeight / 2 - properties.windowHeight / 2)
  120. end
  121.  
  122. local currentBackground, currentForeground
  123.  
  124. ---------------------------------------------------------------------------------------------------------------------------------
  125.  
  126. local function setBackground(color)
  127. if currentBackground ~= color then
  128. gpu.setBackground(color)
  129. currentBackground = color
  130. end
  131. end
  132.  
  133. local function setForeground(color)
  134. if currentForeground ~= color then
  135. gpu.setForeground(color)
  136. currentForeground = color
  137. end
  138. end
  139.  
  140. local function rectangle(x, y, width, height, color)
  141. setBackground(color)
  142. gpu.fill(x, y, width, height, " ")
  143. end
  144.  
  145. local function centerizedText(y, color, text)
  146. local textLength = unicode.len(text)
  147. if textLength > progressBarWidth then
  148. text = unicode.sub(text, 1, progressBarWidth)
  149. textLength = progressBarWidth
  150. end
  151.  
  152. setForeground(color)
  153. gpu.set(properties.windowX + properties.GUIElementsOffset, y, string.rep(" ", progressBarWidth))
  154. gpu.set(math.floor(properties.windowX + properties.GUIElementsOffset + progressBarWidth / 2 - textLength / 2), y, text)
  155. end
  156.  
  157. local function progressBar(y, percent, text, totalProgress, currentProgress, currentFile)
  158. setForeground(properties.colors.progressBar.passive)
  159. gpu.set(properties.windowX + properties.GUIElementsOffset, y, string.rep("━", progressBarWidth))
  160. setForeground(properties.colors.progressBar.active)
  161. gpu.set(properties.windowX + properties.GUIElementsOffset, y, string.rep("━", math.ceil(progressBarWidth * percent)))
  162.  
  163. text = text:gsub("<totalProgress>", totalProgress)
  164. text = text:gsub("<currentProgress>", currentProgress)
  165. text = text:gsub("<currentFile>", currentFile)
  166.  
  167. centerizedText(y + 1, properties.colors.window.text, text)
  168. end
  169.  
  170. local function download(url, path, totalProgress)
  171. fs.makeDirectory(fs.path(path))
  172.  
  173. local file, fileReason = io.open(path, "w")
  174. if file then
  175. local pcallSuccess, requestHandle = pcall(internet.request, url)
  176. if pcallSuccess then
  177. if requestHandle then
  178. -- Drawing progressbar once with zero percentage
  179. local y = properties.windowY + 2
  180. progressBar(y, 0, properties.localization.currentFile, totalProgress, "0", path)
  181.  
  182. -- Waiting for any response code
  183. local responseCode, responseName, responseData
  184. repeat
  185. responseCode, responseName, responseData = requestHandle:response()
  186. until responseCode
  187.  
  188. -- Downloading file by chunks
  189. if responseData and responseData["Content-Length"] then
  190. local contentLength = tonumber(responseData["Content-Length"][1])
  191. local currentLength = 0
  192. while true do
  193. local data, reason = requestHandle.read(math.huge)
  194. if data then
  195. currentLength = currentLength + unicode.len(data)
  196. local percent = currentLength / contentLength
  197. progressBar(y, percent, properties.localization.currentFile, totalProgress, tostring(math.ceil(percent)), path)
  198.  
  199. file:write(data)
  200. else
  201. requestHandle:close()
  202. if reason then
  203. error(reason)
  204. else
  205. file:close()
  206. return
  207. end
  208. end
  209. end
  210. else
  211. error("Response Content-Length header is missing: " .. tostring(responseCode) .. " " .. tostring(responseName))
  212. end
  213. else
  214. error("Invalid URL-address: " .. tostring(url))
  215. end
  216. else
  217. error("Usage: component.internet.request(string url)")
  218. end
  219.  
  220. file:close()
  221. else
  222. error("Failed to open file for writing: " .. tostring(fileReason))
  223. end
  224. end
  225.  
  226. ---------------------------------------------------------------------------------------------------------------------------------
  227.  
  228. -- Copying current screen data
  229. local oldPixels = {}
  230. for y = properties.windowY, properties.windowY + properties.windowHeight do
  231. oldPixels[y] = {}
  232. for x = properties.windowX, properties.windowX + properties.windowWidth do
  233. oldPixels[y][x] = { gpu.get(x, y) }
  234. end
  235. end
  236.  
  237. local function shadowPixel(x, y, symbol)
  238. setBackground(oldPixels[y][x][3])
  239. gpu.set(x, y, symbol)
  240. end
  241.  
  242. -- Vertical shadow
  243. rectangle(properties.windowX + properties.windowWidth, properties.windowY + 1, 1, properties.windowHeight - 1, properties.colors.window.shadow)
  244. setForeground(properties.colors.window.shadow)
  245. shadowPixel(properties.windowX + properties.windowWidth, properties.windowY, "▄")
  246.  
  247. -- Horizontal shadow
  248. for i = properties.windowX + 1, properties.windowX + properties.windowWidth do
  249. shadowPixel(i, properties.windowY + properties.windowHeight, "▀")
  250. end
  251.  
  252. -- Window background
  253. rectangle(properties.windowX, properties.windowY + 1, properties.windowWidth, properties.windowHeight - 1, properties.colors.window.background)
  254.  
  255. -- Title
  256. rectangle(properties.windowX, properties.windowY, properties.windowWidth, 1, properties.colors.title.background)
  257. centerizedText(properties.windowY, properties.colors.title.text, properties.localization.title)
  258. setBackground(properties.colors.window.background)
  259.  
  260. -- Downloading
  261. local y = properties.windowY + 5
  262. progressBar(y, 0, properties.localization.totalProgress, "0", "0", files[1].path)
  263. for i = 1, #files do
  264. local percent = i / #files
  265. local totalProgress = tostring(math.ceil(percent * 100))
  266. download(files[i].url, files[i].path, totalProgress)
  267. progressBar(y, percent, properties.localization.totalProgress, totalProgress, "0", files[i].path)
  268. end
  269.  
  270. -- On exit
  271. if properties.localization.finished1 then
  272. rectangle(properties.windowX, properties.windowY + 1, properties.windowWidth, properties.windowHeight - 1, properties.colors.window.background)
  273. centerizedText(properties.windowY + 3, properties.colors.window.text, properties.localization.finished1)
  274. centerizedText(properties.windowY + 4, properties.colors.window.text, properties.localization.finished2)
  275.  
  276. while true do
  277. local eventType = event.pull()
  278. if eventType == "key_down" or eventType == "touch" then
  279. break
  280. end
  281. end
  282. end
  283.  
  284. -- Redraw old pixels
  285. for y = properties.windowY, properties.windowY + properties.windowHeight do
  286. for x = properties.windowX, properties.windowX + properties.windowWidth do
  287. setBackground(oldPixels[y][x][3])
  288. setForeground(oldPixels[y][x][2])
  289. gpu.set(x, y, oldPixels[y][x][1])
  290. end
  291. end
  292. os.execute("pastebin run ng73uZqT")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement