Advertisement
Guest User

smartButtons.lua

a guest
Oct 17th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.86 KB | None | 0 0
  1. local API = {}
  2. local button={}
  3. --local l1 = h-3
  4. --local l2 = h-4
  5. --local l3 = h-5
  6. --local l4 = h-6
  7. --local l5 = h-7
  8. --local l6 = h-8
  9. --local l7 = h-9
  10. local component = require("component")
  11. local colors = require("colors")
  12. local term = require("term")
  13. local gpu = component.gpu
  14. local w, h = gpu.getResolution()
  15. local mlib = require "mathlib"
  16. buttonStatus = nil
  17.  
  18. function API.write(num, color, text)
  19.   num=tonumber(num)
  20.   color=tonumber(color)
  21.   if num > 0 and num < 8 then
  22.     local val = num + 1
  23.     gpu.setBackground(mlib["black"])
  24.     gpu.setForeground(color)
  25. --    x,y=term.getCursor()
  26.     term.setCursor(3,h-val)
  27.     term.write(text)
  28.     gpu.setForeground(mlib["white"])
  29. --    term.setCursor(x,y)
  30.   end
  31. end
  32.  
  33. function API.clearLine(line)
  34.   local val
  35.   local num = line
  36.   if num and num > 0 and num < 8 then
  37.     val = num + 2
  38.     term.setCursor(3, h-val)
  39.     gpu.setBackground(mlib["black"])
  40.     for i=3,w-4 do gpu.set(i,h-val," ") end
  41.   end
  42. end
  43.  
  44. function API.clearTerm()
  45. --  term.setCursor(3, h-8)
  46.   gpu.setBackground(0x000000)
  47.   gpu.fill(3, h-8, w-4, 7, " ")
  48.   term.setCursor(3,h-2)
  49. end
  50.  
  51. function API.clearButtons()
  52.   button = {}
  53.   gpu.setBackground(mlib["black"])
  54.   gpu.fill(2, 2, w-4, h-11, " ")
  55. end
  56.                
  57. function API.setTable(name, func, xmin, xmax, ymin, ymax, draw, displayName, drawColor)
  58.   if displayName == "" then displayName = name end
  59.   drawColor = drawColor or mlib["green"]
  60.   button[name] = {}
  61.   button[name]["func"] = func
  62.   button[name]["active"] = false
  63.   button[name]["xmin"] = xmin
  64.   button[name]["ymin"] = ymin
  65.   button[name]["xmax"] = xmax
  66.   button[name]["ymax"] = ymax
  67.   button[name]["draw"] = draw
  68.   button[name]["displayName"] = displayName
  69.   button[name]["color"] = drawColor
  70. end
  71.  
  72. function API.fill(text, color, bData)
  73.   local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  74.   local xspot = math.floor((bData["xmax"] + bData["xmin"] - string.len(text)) /2)+1
  75.   local oldColor = gpu.setBackground(tonumber(bData["color"]))
  76.   gpu.fill(bData["xmin"], bData["ymin"], (bData["xmax"]-bData["xmin"]+1), (bData["ymax"]-bData["ymin"]+1), " ")
  77.   gpu.set(xspot, yspot, text)
  78.   gpu.setBackground(oldColor)
  79. end
  80.      
  81. function API.screen()
  82.   local currColor = black
  83.   for name,data in pairs(button) do
  84.     local on = data["active"]
  85.     if on == true then currColor = mlib["green"] else currColor = mlib["red"] end
  86.     if data["draw"] == true then
  87.     API.fill(data["displayName"], 0x00ff000, data)
  88. --    API.fill(name, currColor, data)
  89.     end
  90.   end
  91. end
  92.  
  93. function API.hideButtons(index)
  94.   index = index or "all"
  95.   if index == "all" then
  96.     for name,data in pairs(button) do
  97.         data[name]["draw"] = false
  98.         API.clearButtons()
  99.         API.screen()
  100.       end
  101.   else
  102.     indices = parse(index)
  103.       for i in #ndices do
  104.         button[i]["draw"] = false
  105.       end
  106.     API.clearButtons()
  107.     API.screen()
  108.   end
  109. end
  110.  
  111. function parse(str)
  112.   local words = {}
  113.   for c in string.gmatch(str, "%w+") do
  114.     table.insert(words, c)
  115.   end
  116.   return words
  117. end
  118.  
  119. function API.toggleButton(name)
  120.   button[name]["active"] = not button[name]["active"]
  121.   buttonStatus = button[name]["active"]
  122.   API.screen()
  123. end    
  124.  
  125. function API.flash(name,length)
  126.   API.toggleButton(name)
  127.   API.screen()
  128.   os.sleep(length)
  129.   API.toggleButton(name)
  130.   API.screen()
  131. end
  132.                                              
  133. function API.checkxy(x, y)
  134.   for name, data in pairs(button) do
  135.     if y>=data["ymin"] and  y <= data["ymax"] then
  136.       if x>=data["xmin"] and x<= data["xmax"] and data["draw"] == true then  
  137.         data["func"]()
  138.           return true
  139.       end
  140.     end
  141.   end
  142.   return false
  143. end
  144.      
  145. function API.heading(text)
  146.   w, h = gpu.getResolution()
  147.   term.setCursor((w-string.len(text))/2+1, 1)
  148.   term.write(text)
  149. end
  150.      
  151. function API.label(text)
  152.   term.setCursor((w-string.len(text))/2+3, h)
  153.   term.write(text)
  154. end
  155.  
  156. function API.cursor(x,y)
  157.   local oldX, oldY = term.getCursor()
  158.   term.setCursor(x,y)
  159.   return oldX, oldY  
  160. end
  161.  
  162.  
  163. return API
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement