Advertisement
MeXaN1cK

zth_gui

Feb 6th, 2022
888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.59 KB | None | 0 0
  1. local cmp = require("component")
  2. local os = require("os")
  3. local table = require("table")
  4. local gpu = cmp.gpu
  5. local term = require("term")
  6. local event = require("event")
  7. local uc = require("unicode")
  8. local ret = {}
  9. local widgets = {}
  10.  
  11. function ret.getWidgets()
  12.   return widgets
  13. end
  14.  
  15. function ret.drawProgressBar(x, y, width, current, max, color1, color2)
  16.   term.setCursor(x, y)
  17.   local prc = current * 100 / max
  18.   local prc_w = prc * width / 100
  19.   local inText = current.."/"..max
  20.  
  21.   ret.drawSpaces(x, y, color1, prc_w)
  22.   ret.drawSpaces(x + prc_w, y, color2, width-prc_w-1)
  23. end
  24.  
  25. function ret.addButton(x, y, title, color, color_active, isSwitch, callback)
  26.   local lastID = #widgets+1
  27.   table.insert(widgets, {})
  28.   widgets[lastID]["x"] = x
  29.   widgets[lastID]["y"] = y
  30.   widgets[lastID]["title"] = title
  31.   widgets[lastID]["color"] = color
  32.   widgets[lastID]["color_active"] = color_active
  33.   widgets[lastID]["callback"] = callback
  34.   widgets[lastID]["isSwitch"] = isSwitch
  35.   widgets[lastID]["state"] = "idle"
  36.   widgets[lastID]["type"] = "button"
  37.   return lastID
  38. end
  39.  
  40. function ret.addLabel(x, y, width, colorFG, colorBG, title)
  41.   local lastID = #widgets+1
  42.   table.insert(widgets, {})
  43.   widgets[lastID]["type"] = "label"
  44.   widgets[lastID]["x"] = x
  45.   widgets[lastID]["y"] = y
  46.   widgets[lastID]["width"] = width
  47.   widgets[lastID]["colorFG"] = colorFG
  48.   widgets[lastID]["colorBG"] = colorBG
  49.   widgets[lastID]["title"] = title
  50.   return lastID
  51. end
  52.  
  53. function ret.drawSpaces(x, y, color, num)
  54.   local oldBG = gpu.setBackground(color)
  55.   term.setCursor(x, y)
  56.   for i = 1, num do term.write(" ") end
  57.   gpu.setBackground(oldBG)
  58. end
  59.  
  60. function ret.printAt(x, y, colorFG, colorBG, text)
  61.   local oldBG = gpu.setBackground(colorBG)
  62.   local oldFG = gpu.setForeground(colorFG)
  63.   term.setCursor(x, y)
  64.   term.write(text)
  65.   gpu.setBackground(oldBG)
  66.   gpu.setForeground(oldFG)
  67. end
  68.  
  69. function drawButton(x, y, title, color)  
  70.   local oldBG = gpu.setBackground(color)
  71.   ret.drawSpaces(x, y, color, uc.len(title)+4)
  72.   term.setCursor(x, y+1)
  73.   term.write("  "..title.."  ")
  74.   ret.drawSpaces(x, y+2, color, uc.len(title)+4)
  75.   gpu.setBackground(oldBG)
  76. end
  77.  
  78. function drawLabel(key)
  79.   local excess = key["width"] - uc.len(key["title"])  
  80.   ret.drawSpaces(key["x"]+uc.len(key["title"]), key["y"], key["colorBG"], excess)
  81.   ret.printAt(key["x"], key["y"], key["colorFG"], key["colorBG"], key["title"])  
  82. end
  83.  
  84. function drawButtons(key)
  85.   if key["type"] == "button" then
  86.     if key["state"] == "idle" then
  87.       drawButton(key["x"], key["y"], key["title"], key["color"])
  88.     else
  89.       drawButton(key["x"], key["y"], key["title"], key["color_active"])
  90.     end
  91.     if key["isSwitch"] == false then
  92.       if key["state"] == "active" then
  93.         key["state"] = "idle"
  94.       end
  95.     end
  96.   end
  97. end
  98.  
  99. function ret.drawUI()
  100.   for elem, key in pairs(widgets) do
  101.     if key["type"] == "button" then drawButtons(key) end
  102.     if key["type"] == "label" then drawLabel(key) end  
  103.   end
  104. end
  105.  
  106.  
  107. function ret.callbackTouch(addr, x, y, btn, player)
  108.   for elem, key in pairs(widgets) do
  109.     if x >= key["x"] and x <= key["x"]+uc.len(key["title"])+4 then
  110.       if y >= key["y"] and y <= key["y"]+2 then
  111.         if key["isSwitch"] then
  112.           if key["state"] == "idle" then
  113.             widgets[elem]["state"] = "active"
  114.           else
  115.             widgets[elem]["state"] = "idle"
  116.           end
  117.         else
  118.           widgets[elem]["state"] = "active"
  119.         end
  120.         pcall(key["callback"], widgets[elem]["state"])
  121.       end
  122.     end
  123.   end
  124.  
  125. end
  126.  
  127. --event.listen("touch", callbackTouch)
  128.  
  129. return ret;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement