Advertisement
m_pro_m

OC interface

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