Advertisement
Hikooshi

progressBars

Sep 10th, 2017
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.82 KB | None | 0 0
  1. -- pastebin get tpj7K41D scrollBars
  2. local component = require("component")
  3. local gpu = component.gpu
  4. local event = require("event")
  5. local unicode = require("unicode")
  6. local term = require("term")
  7. local shift = 1
  8.  
  9.  
  10.  
  11. fPix = unicode.char(9608)
  12. uPix = unicode.char(9600)
  13. dPix = unicode.char(9604)
  14. progressBarsTitles = {[5]="124", [10]="New title", [15]="New Title again", [20]="20 %", [25]="One more Title", [30]="30-percents title", [35]="VeryLongString", [40]="'Path Of File On Computer'", [45]="And it is fourty-five percents", [50]="Half of Line", [55]="55 percents", [60]="Going a way", [65]="Faster and faster", [70]="Almost done", [75]="3/4", [80]="20 percents more", [85]="Just idle", [90]="Keep going", [95]="It's posible", [100]="Job done"}
  15.  
  16. progressBars = {progressBar = {x=8, y=8, length=32, title="Тестовый прогрессбар", width=2, barSize = 4, active = true, showPercents = true, fill = false, percents = 0, cFore = 0x00FF00, cForePrepare = 0x009900, cForeProgress = 0x0000FF, cForeTitle = 0xFFFFFF, cBackTitle = 0x000000, cForePercents = 0xFFFFFF},
  17.                 progressBar1 = {x=8, y=16, length=32, title="Test progressbar", width=1, active = true, showPercents = false, fill = true, percents = 0, cFore = 0x00FF00, cForePrepare = 0x009900, cForeProgress = 0x0000FF, cForeTitle = 0xFFFFFF, cBackTitle = 0x000000, cForePercents = 0xFFFFFF},
  18.                 progressBar2 = {x=56, y=8, length=32, title="Новый прогрессбар", width=4, active = true, showPercents = true, fill = true, percents = 0, cFore = 0x00FF00, cForePrepare = 0x009900, cForeProgress = 0x0000FF, cForeTitle = 0xFFFFFF, cBackTitle = 0x000000, cForePercents = 0xFFFFFF}}
  19.  
  20. gpu.setForeground(progressBars.progressBar.cFore)
  21. gpu.fill(4, 4, 88, 16, fPix)
  22.  
  23. function drawProgressBar(tbl1)
  24.  
  25. if tbl1.active then
  26. if not tbl1.fill then
  27. tbl1.position = tbl1.x - tbl1.barSize
  28. end
  29. gpu.setForeground(tbl1.cForePrepare)
  30. if tbl1.width%2 == 1 then
  31. gpu.fill(tbl1.x, tbl1.y, tbl1.length, tbl1.width, fPix)
  32. elseif tbl1.width%2 == 0 then
  33. gpu.setBackground(tbl1.cFore)
  34. gpu.fill(tbl1.x, tbl1.y, tbl1.length, 1, dPix)
  35. gpu.fill(tbl1.x, tbl1.y + 1, tbl1.length, tbl1.width - 1, fPix)
  36. gpu.fill(tbl1.x, tbl1.y + tbl1.width, tbl1.length, 1, uPix)
  37. end
  38.     if tbl1.showPercents then
  39.     gpu.setForeground(tbl1.cForePercents)
  40.       if tbl1.percents <= 9 then
  41.       percents = tostring(tbl1.percents) .. "%"
  42.         for i = tbl1.x + tbl1.length/2 - 1, tbl1.x + tbl1.length/2 do
  43.         local _, color = gpu.get(i, tbl1.y)
  44.         gpu.setBackground(color)
  45.         gpu.set(i, tbl1.y + math.floor(tbl1.width/2), string.sub(percents, i - tbl1.x - (tbl1.length/2 - 2)))
  46.         end
  47.       elseif tbl1.percents > 9 and tbl1.percents < 100 then
  48.       percents = tostring(tbl1.percents) .. "%"
  49.         for i = tbl1.x + tbl1.length/2 - 1, tbl1.x + tbl1.length/2 + 1 do
  50.         local _, color = gpu.get(i, tbl1.y)
  51.         gpu.setBackground(color)
  52.         gpu.set(i, tbl1.y + math.floor(tbl1.width/2), string.sub(percents, i - tbl1.x - (tbl1.length/2 - 2)))
  53.         end
  54.       else
  55.       percents = tostring(tbl1.percents) .. "%"
  56.         for i = tbl1.x + tbl1.length/2 - 2, tbl1.x + tbl1.length/2 + 1 do
  57.         local _, color = gpu.get(i, tbl1.y)
  58.         gpu.setBackground(color)
  59.         gpu.set(i, tbl1.y + math.floor(tbl1.width/2), string.sub(percents, i - tbl1.x - (tbl1.width/2 - 3)))
  60.         end
  61.       end
  62.     end
  63. if unicode.len(tbl1.title) > tbl1.length - 4 then
  64. title = unicode.wtrunc(tbl1.title, (tbl1.length - 3) - 2) .. ".."
  65. else
  66. title = tbl1.title
  67. end
  68. gpu.setForeground(tbl1.cForeTitle)
  69. gpu.setBackground(tbl1.cBackTitle)
  70. gpu.set(tbl1.x + 2, tbl1.y - 1, title)
  71. end
  72. oldTitle = tbl1.title
  73.  
  74. end
  75.  
  76. function drawProgressBars()
  77.  
  78. for k,v in pairs(progressBars) do
  79. drawProgressBar(v)
  80. end
  81.  
  82. end
  83.  
  84. function processingProgressBar(tbl)
  85.  
  86. if tbl.percents < 100 then
  87. tbl.percents = tbl.percents + 1
  88. percents = tostring(tbl.percents) .. "%"
  89. if tbl.fill == false then
  90. tbl.position = tbl.position + shift
  91. end
  92. end
  93. if not tbl.fill then
  94.     if tbl.position + tbl.barSize > tbl.x and tbl.position + tbl.barSize <= tbl.x + tbl.length then
  95.     if tbl.position + tbl.barSize - tbl.x < tbl.barSize then
  96.     barSize = tbl.position + tbl.barSize - tbl.x
  97.     barPosition = tbl.x
  98.     else
  99.     barSize = tbl.barSize
  100.     barPosition = tbl.position
  101.     end
  102.     elseif tbl.position + tbl.barSize > tbl.x + tbl.length and tbl.position + tbl.barSize < tbl.x + tbl.length + tbl.barSize then
  103.     barSize = tbl.x + tbl.length - tbl.position
  104.     barPosition = tbl.position
  105.     elseif tbl.position >= tbl.x + tbl.length then
  106.     tbl.position = tbl.x - tbl.barSize
  107.     barSize = 0
  108.     barPosition = tbl.position
  109.     elseif tbl.position + tbl.barSize <= tbl.x then
  110.     barSize = 0
  111.     barPosition = tbl.position
  112.     end
  113.     if tbl.width%2 == 0 then
  114.     gpu.setForeground(tbl.cForePrepare)
  115.     gpu.setBackground(tbl.cFore)
  116.     gpu.fill(tbl.x, tbl.y, tbl.length, 1, dPix)
  117.     gpu.fill(tbl.x, tbl.y + 1, tbl.length, tbl.width - 1, fPix)
  118.     gpu.fill(tbl.x, tbl.y + tbl.width, tbl.length, 1, uPix)
  119.     gpu.setForeground(tbl.cForeProgress)
  120.     gpu.fill(barPosition, tbl.y, barSize, 1, dPix)
  121.     gpu.fill(barPosition, tbl.y + 1, barSize, tbl.width - 1, fPix)
  122.     gpu.fill(barPosition, tbl.y + tbl.width, barSize, 1, uPix)
  123.     elseif tbl.width%2 == 1 then
  124.     gpu.setForeground(tbl.cForePrepare)
  125.     gpu.fill(tbl.x, tbl.y, tbl.length, tbl.width, fPix)
  126.     gpu.setForeground(tbl.cForeProgress)
  127.     gpu.fill(barPosition, tbl.y, barSize, tbl.width, fPix)
  128.     end
  129. else
  130.     if tbl.width%2 == 1 then
  131.     gpu.setForeground(tbl.cForeProgress)
  132.     gpu.fill(tbl.x, tbl.y, math.floor(tbl.percents/(100/tbl.length)), tbl.width, fPix)
  133.     elseif tbl.width%2 == 0 then
  134.     gpu.setForeground(tbl.cForeProgress)
  135.     gpu.setBackground(tbl.cFore)
  136.     gpu.fill(tbl.x, tbl.y, math.floor(tbl.percents/(100/tbl.length)), 1, dPix)
  137.     gpu.fill(tbl.x, tbl.y + 1, math.floor(tbl.percents/(100/tbl.length)), tbl.width - 1, fPix)
  138.     gpu.fill(tbl.x, tbl.y + tbl.width, math.floor(tbl.percents/(100/tbl.length)), 1, uPix)
  139.     end
  140. end
  141. if tbl.showPercents == true then
  142. gpu.setForeground(tbl.cForePercents)
  143. if tbl.percents >= 0 and tbl.percents <= 9 then
  144. for i = tbl.x + tbl.length/2 - 1, tbl.x + tbl.length/2 do
  145. local _, color = gpu.get(i, tbl.y)
  146. gpu.setBackground(color)
  147. gpu.set(i, tbl.y + math.floor(tbl.width/2), string.sub(percents, i - tbl.x - (tbl.length/2 - 2)))
  148. end
  149. elseif tbl.percents > 9 and tbl.percents < 100 then
  150. for i = tbl.x + tbl.length/2 - 1, tbl.x + tbl.length/2 + 1 do
  151. local _, color = gpu.get(i, tbl.y)
  152. gpu.setBackground(color)
  153. gpu.set(i, tbl.y + math.floor(tbl.width/2), string.sub(percents, i - tbl.x - (tbl.length/2 - 2)))
  154. end
  155. elseif tbl.percents == 100 then
  156. for i = tbl.x + tbl.length/2 - 2, tbl.x + tbl.length/2 + 1 do
  157. local _, color = gpu.get(i, tbl.y)
  158. gpu.setBackground(color)
  159. gpu.set(i, tbl.y + math.floor(tbl.width/2), string.sub(percents, i - tbl.x - (tbl.length/2 - 3)))
  160. end
  161. end
  162. end
  163. if progressBarsTitles[tbl.percents] ~= nil then
  164. tbl.title = progressBarsTitles[tbl.percents]
  165. end
  166. if tbl.title ~= oldTitle then
  167. gpu.setForeground(tbl.cFore)
  168. gpu.fill(tbl.x, tbl.y - 1, tbl.length, 1, fPix)
  169. if unicode.len(tbl.title) > tbl.length - 4 then
  170. title = unicode.wtrunc(tbl.title, (tbl.length - 3) - 2) .. ".."
  171. else
  172. title = tbl.title
  173. end
  174. gpu.setForeground(tbl.cForeTitle)
  175. gpu.setBackground(tbl.cBackTitle)
  176. gpu.set(tbl.x + 2, tbl.y - 1, title)
  177. end
  178. oldTitle = tbl.title
  179. gpu.setForeground(0xFFFFFF)
  180. gpu.setBackground(0x000000)
  181.  
  182. end
  183.  
  184. drawProgressBars()
  185.  
  186. while true do
  187.  
  188. local event, _, key, key1, _ = event.pull()
  189. if event == "key_down" and key == 113 then
  190. processingProgressBar(progressBars.progressBar)
  191. elseif event == "key_down" and key == 101 then
  192. processingProgressBar(progressBars.progressBar1)
  193. elseif event == "key_down" and key == 114 then
  194. processingProgressBar(progressBars.progressBar2)
  195. end
  196.  
  197. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement