DarkSiders061

test

Oct 3rd, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.98 KB | None | 0 0
  1. local component = require("component")
  2. local colors = require("colors")
  3.  
  4. local objects = {}
  5. local API = {}
  6.  
  7. local gpu = component.gpu
  8. local w,h = gpu.getResolution()
  9.  
  10. function API.clearScreen()
  11.     gpu.setForeground(colors.white,true)
  12.     gpu.setBackground(colors.black,true)
  13.     gpu.fill(1,1,w,h," ")
  14. end
  15.  
  16. function API.clamp(n,min,max) return math.min(math.max(n, min), max) end
  17.  
  18. function API.newButton(ID,label,x,y,width,height,func,params,oncolor,offcolor,toggle)
  19.     local table = {}
  20.     table["type"] = "button"
  21.     table["label"] = label
  22.     table["x"] = x
  23.     table["y"] = y
  24.     table["width"] = width
  25.     table["height"] = height
  26.     table["active"] = false
  27.     table["func"] = func
  28.     table["params"] = params
  29.     table["oncolor"] = oncolor
  30.     table["offcolor"] = offcolor
  31.     table["toggle"] = toggle
  32.     objects[ID] = table
  33. end
  34.  
  35. function API.newLabel(ID,label,x,y,width,height,color)
  36.     local table = {}
  37.     table["type"] = "label"
  38.     table["label"] = label
  39.     table["x"] = x
  40.     table["y"] = y
  41.     table["width"] = width
  42.     table["height"] = height
  43.     table["color"] = color
  44.     objects[ID] = table
  45. end
  46.    
  47. function API.newBar(ID,x,y,width,height,color1,color2,value)
  48.     local table = {}
  49.     table["type"] = "bar"
  50.     table["x"] = x
  51.     table["y"] = y
  52.     table["width"] = width
  53.     table["height"] = height
  54.     table["color1"] = color1 --Left color
  55.     table["color2"] = color2 --Right color
  56.     table["value"] = value
  57.     objects[ID] = table
  58. end
  59.  
  60. function API.removeObject(ID)
  61.     objects[ID] = {}
  62. end
  63.  
  64. function API.clearAllObjects()
  65.     API.clearScreen()
  66.     objects = {}
  67. end
  68.  
  69. function API.draw(ID)
  70.     data = objects[ID]
  71.     local objtype = data["type"]
  72.     local label = data["label"]
  73.     local x = data["x"]
  74.     local y = data["y"]
  75.     local width = data["width"]
  76.     local height = data["height"]
  77.     if objtype == "button" then
  78.         local drawColor = 0x000000
  79.         if data["active"] == true then
  80.             drawColor = data["oncolor"]
  81.         else
  82.             drawColor = data["offcolor"]
  83.         end
  84.         gpu.setBackground(drawColor,false)
  85.         gpu.fill(x,y,width,height," ")
  86.         gpu.set((x+width/2)-string.len(label)/2,y+height/2,label)
  87.         gpu.setBackground(colors.black,true)
  88.     elseif objtype == "label" then
  89.         gpu.setBackground(data["color"],false)
  90.         gpu.fill(x,y,width,height," ")
  91.         gpu.set((x+width/2)-string.len(label)/2,y+height/2,label)
  92.         gpu.setBackground(colors.black,true)
  93.     elseif objtype == "bar" then
  94.         gpu.setBackground(data["color2"],false)
  95.         gpu.fill(x,y,width,height," ")
  96.         local amount = math.floor((width/100)*data["value"])
  97.         gpu.setBackground(data["color1"],false)
  98.         gpu.fill(x,y,amount,height," ")
  99.         gpu.setBackground(colors.black,true)
  100.     end
  101. end
  102.  
  103. function API.toggleButton(ID)
  104.     local objtype = objects[ID]["type"]
  105.     if not objtype == "button" then return end
  106.     objects[ID]["active"] = not objects[ID]["active"]
  107.     API.draw(ID)
  108. end
  109.  
  110. function API.flashButton(ID)
  111.     local objtype = objects[ID]["type"]
  112.     if not objtype == "button" then return end
  113.     API.toggleButton(ID)
  114.     os.sleep(0.15)
  115.     API.toggleButton(ID)
  116. end
  117.  
  118. function API.getButtonState(ID)
  119.     local objtype = objects[ID]["type"]
  120.     if not objtype == "button" then return end
  121.     return objects[ID]["active"]
  122. end
  123.  
  124. function API.getButtonClicked(x,y)
  125.     for ID,data in pairs(objects) do
  126.         local xmax = data["x"]+data["width"]-1
  127.         local ymax = data["y"]+data["height"]-1
  128.         if data["type"] == "button" then
  129.             if x >= data["x"] and x <= xmax then
  130.                 if y >= data["y"] and y <= ymax then
  131.                     return ID
  132.                 end
  133.             end
  134.         end
  135.     end
  136.     return nil
  137. end
  138.  
  139. function API.activateButton(ID)
  140.     local objtype = objects[ID]["type"]
  141.     if not objtype == "button" then return end
  142.     objects[ID]["func"](objects[ID]["params"])
  143. end
  144.  
  145. function API.updateAll()
  146.     API.clearScreen()
  147.     for ID,data in pairs(objects) do
  148.         API.draw(ID)
  149.     end
  150. end
  151.  
  152. function API.processClick(x,y)
  153.     local ID = API.getButtonClicked(x,y)
  154.     if not ID then return end
  155.     local objtype = objects[ID]["type"]
  156.     if not objtype == "button" then return end
  157.     if not ID == false then
  158.         if objects[ID]["toggle"] == true then
  159.             API.toggleButton(ID)
  160.             API.activateButton(ID)
  161.         else
  162.             API.flashButton(ID)
  163.             API.activateButton(ID)
  164.         end
  165.     end
  166. end
  167.  
  168. function API.setBarValue(ID,value)
  169.     local objtype = objects[ID]["type"]
  170.     if not objtype == "bar" then return end
  171.     objects[ID]["value"] = API.clamp(value,0,100)
  172.     API.draw(ID)
  173. end
  174.  
  175. function API.setLabelText(ID,label)
  176.     local objtype = objects[ID]["type"]
  177.     if not objtype == "label" or not objtype == "button" then return end
  178.     if not label then label = " " end
  179.     objects[ID]["label"] = label
  180.     API.draw(ID)
  181. end
  182.  
  183. return API
Add Comment
Please, Sign In to add comment