Advertisement
nik1111

progress

May 19th, 2022
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. --
  2. -- Created by Grcpils
  3. -- 03/14/2021
  4. -- Github: https://github.com/grcpils/cc-gui_api
  5. -- Please do not delete this header ;)
  6. --
  7.  
  8. local Utils = require("GUI/utils")
  9.  
  10. local drawHorizontalFill = function (xmin, xmax, ymin, ymax, progress)
  11. for cY = ymin, ymax do
  12. for cX = xmin, progress do
  13. if cX <= xmax and cY <= ymax then
  14. GUI_MONITOR.setCursorPos(cX, cY)
  15. GUI_MONITOR.write(" ")
  16. end
  17. end
  18. end
  19. end
  20.  
  21. local drawPercentHorizontal = function(prg, percent, progress)
  22. local percentText = percent.."%"
  23. local Y = math.floor((prg.ymin + prg.ymax) / 2)
  24. local X = math.floor((prg.xmax + prg.xmin - string.len(percentText)) / 2) + 1
  25.  
  26. for i = 1, #percentText do
  27. local c = percentText:sub(i,i)
  28. GUI_MONITOR.setCursorPos(X, Y)
  29. if X - 1 < progress then
  30. GUI_MONITOR.setBackgroundColor(prg.color)
  31. else
  32. GUI_MONITOR.setBackgroundColor(colors.gray)
  33. end
  34. GUI_MONITOR.write(c)
  35. GUI_MONITOR.setBackgroundColor(colors.black)
  36. X = X + 1
  37. end
  38. end
  39.  
  40. local drawLabelHorizontal = function(prg, percent, progress)
  41. local Y = math.floor(prg.ymin - 1)
  42. local X = math.floor((prg.xmax + prg.xmin - string.len(prg.label)) / 2) + 1
  43. GUI_MONITOR.setBackgroundColor(colors.black)
  44. GUI_MONITOR.setCursorPos(X, Y)
  45. GUI_MONITOR.write(prg.label)
  46. GUI_MONITOR.setBackgroundColor(colors.black)
  47. end
  48.  
  49. local fill = function (prg)
  50. GUI_MONITOR.setBackgroundColor(prg.color)
  51. if prg.type == "horizontal" then
  52. local percentStep = prg.xmax / 100
  53. local percent = math.floor((prg.value / prg.max) * 100)
  54. local progress = math.floor(percentStep * percent)
  55. if prg.colored == true then
  56. if percent < prg.coloredStep.low then
  57. prg.color = prg.coloredColors.low
  58. elseif percent >= prg.coloredStep.low and percent < prg.coloredStep.medium then
  59. prg.color = prg.coloredColors.medium
  60. else
  61. prg.color = prg.coloredColors.high
  62. end
  63. end
  64. drawHorizontalFill(prg.xmin, prg.xmax, prg.ymin, prg.ymax, progress)
  65. drawPercentHorizontal(prg, percent, progress)
  66. if prg.viewLabel == true then
  67. drawLabelHorizontal(prg)
  68. end
  69. else
  70. Utils.printErr("[GUI] Error: %s type unknown (%s)\n", prg.label, prg.type)
  71. end
  72. GUI_MONITOR.setBackgroundColor(colors.black)
  73. end
  74.  
  75. local draw = function (prg)
  76. GUI_MONITOR.setBackgroundColor(colors.gray)
  77. Utils.drawShape(prg)
  78. GUI_MONITOR.setBackgroundColor(colors.black)
  79. end
  80.  
  81. local drawColor = function (prg)
  82. GUI_MONITOR.setBackgroundColor(prg.color)
  83. Utils.drawShape(prg)
  84. GUI_MONITOR.setBackgroundColor(colors.black)
  85. end
  86.  
  87. local drawAll = function ()
  88. table.foreach(GUI_PROGRESS, function (key, prg)
  89. Utils.printInfo("[GUI] Draw %s progress '%s' (id:%d)\n", prg.type, prg.label, key)
  90. draw(prg)
  91. fill(prg)
  92. end)
  93. end
  94.  
  95. local new = function (label, type, max, pos, size, color)
  96. local xmin, xmax, ymin, ymax = Utils.getCoordonate(pos, size)
  97. if type ~= "horizontal" then
  98. Utils.printErr("[GUI] Error %s %s: %s type unknown (%s)\n", debug.getinfo(2).source, debug.getinfo(2).currentline, label, type)
  99. return
  100. end
  101. local Progress = {
  102. type = type,
  103. label = label,
  104. xmin = xmin,
  105. xmax = xmax,
  106. ymin = ymin,
  107. ymax = ymax,
  108. max = max,
  109. value = 0,
  110. color = color,
  111. colored = false,
  112. viewLabel = true,
  113. coloredStep = {
  114. low = 30,
  115. medium = 50
  116. },
  117. coloredColors = {
  118. low = colors.red,
  119. medium = colors.orange,
  120. high = colors.green
  121. }
  122. }
  123.  
  124. local setValue = function (value)
  125. Progress.value = value
  126. draw(Progress)
  127. fill(Progress)
  128. end
  129.  
  130. local getValue = function ()
  131. return Progress.value
  132. end
  133.  
  134. local setColored = function(bool)
  135. Progress.colored = bool
  136. end
  137.  
  138. local setColoredStep = function(low, medium)
  139. Progress.coloredStep = {
  140. low = low,
  141. medium = medium
  142. }
  143. end
  144.  
  145. local setColoredColors = function(low, medium, high)
  146. Progress.coloredColors = {
  147. low = low,
  148. medium = medium,
  149. high = high
  150. }
  151. end
  152.  
  153. local viewLabel = function (bool)
  154. Progress.viewLabel = bool
  155. end
  156.  
  157. local setMax = function (value)
  158. Progress.max = value
  159. end
  160.  
  161. local getMax = function ()
  162. return Progress.max
  163. end
  164.  
  165. local setColor = function (color)
  166. Progress.color = color
  167. end
  168.  
  169. table.insert(GUI_PROGRESS, Progress)
  170. return {
  171. setValue = setValue,
  172. getValue = getValue,
  173. setMax = setMax,
  174. getMax = getMax,
  175. setColor = setColor,
  176. viewLabel = viewLabel,
  177. setColored = setColored,
  178. setColoredStep = setColoredStep,
  179. setColoredColors = setColoredColors
  180. }
  181. end
  182.  
  183. return {
  184. new = new,
  185. drawAll = drawAll
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement