_DudeWhat_

Multi-Monitor-Buttons

Jul 29th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. monitors = {peripheral.find("monitor")}
  2. local allMon = {}
  3. for funcName,_ in pairs(monitors[1]) do
  4.     allMon[funcName] = function(...)
  5.         for i=1,#monitors-1 do monitors[i][funcName](unpack(arg)) end
  6.         return monitors[#monitors][funcName](unpack(arg))
  7.     end
  8. end
  9.  
  10. local sides = {"left", "right", "front", "back", "bottom", "top"}
  11. local monitor = {}
  12.  
  13. for a,b in pairs(sides) do
  14.     local monObject = peripheral.wrap(b)
  15.     if monObject then
  16.         monitor[b] = monObject
  17.     end
  18. end
  19.  
  20. allMon.setTextScale(1)
  21. allMon.setTextColor(colors.white)
  22. allMon.setBackgroundColor(colors.black)
  23.  
  24. local button={}
  25.  
  26. function clearAll()
  27.    button = {}
  28. end
  29.  
  30. function clearTable(name)
  31.     button[name] = {}
  32. end
  33.  
  34. function getPlayer(name)
  35.     return button[name]["player"]
  36. end
  37.  
  38. function setPlayer(name, player)
  39.     button[name]["player"] = player
  40.     screen()
  41. end
  42.  
  43. function getState(name)
  44.     return button[name]["active"]
  45. end
  46.  
  47. function setState(name, state)
  48.     button[name]["active"] = state
  49.     screen()
  50. end
  51.  
  52. -- monSide: "left", "right", "front", "back", "bottom", "top"
  53. function setTable(name, func, param, xmin, xmax, ymin, ymax, monSide)
  54.     button[name] = {}
  55.     button[name]["func"] = func
  56.     button[name]["active"] = false
  57.     button[name]["player"] = "empty"
  58.     button[name]["param"] = param
  59.     button[name]["xmin"] = xmin
  60.     button[name]["ymin"] = ymin
  61.     button[name]["xmax"] = xmax
  62.     button[name]["ymax"] = ymax
  63.     if not monitor[monSide] then
  64.         print("There is no monitor on the "..monSide.." side! The button named \""..name.."\" won't be displayed.")
  65.         button[name]["mon"] = nil
  66.     else
  67.         button[name]["mon"] = monSide
  68.     end
  69. end
  70.  
  71. -- returns true if there is a monitor connected at the given side
  72. function testForMonitor(monSide)
  73.     if not monSide then
  74.         return false
  75.     end
  76.     if not monitors[monSide] then
  77.         return false
  78.     end
  79.     return true
  80. end
  81.    
  82.  
  83. function fill(text, color, bData)
  84.     if not testForMonitor(bData["mon"]) then
  85.         return
  86.     end
  87.     local display = monitors[bData["mon"]]
  88.     display.setBackgroundColor(color)
  89.     local on = bData["player"]
  90.     local xSize = bData["xmax"] - bData["xmin"]
  91.     local ySize = bData["ymax"] - bData["ymin"]
  92.     local sizeRel = xSize / ySize
  93.     for j = bData["ymin"], bData["ymax"] do -- draws a rectangle
  94.         display.setCursorPos(bData["xmin"], j)
  95.         for i = bData["xmin"], bData["xmax"] do
  96.             display.write(" ")
  97.         end
  98.     end
  99.     if on == "x" then -- draws a cross on top
  100.         local xOffset = 0
  101.         local scaledYOffset = 0
  102.         display.setBackgroundColor(colors.red)
  103.         for j = bData["ymin"], bData["ymax"] do
  104.             scaledYOffset = (j - bData["ymin"]) * sizeRel
  105.             for i = bData["xmin"], bData["xmax"] do
  106.                 xOffset = i - bData["xmin"]
  107.                 if (math.floor(math.abs(xOffset - scaledYOffset)-2.2) < 0) or (math.floor(math.abs(xOffset + scaledYOffset - xSize)-2.2) < 0) then
  108.                     display.setCursorPos(i, j)
  109.                     display.write(" ")
  110.                 end
  111.             end
  112.         end
  113.     elseif on == "o" then -- draws a circle on top
  114.         local xOffset = 0
  115.         local scaledYOffset = 0
  116.         local halfSize = xSize / 2
  117.         display.setBackgroundColor(colors.red)
  118.         for j = bData["ymin"], bData["ymax"] do
  119.             scaledYOffset = math.abs(halfSize - ((j - bData["ymin"]) * sizeRel))
  120.             for i = bData["xmin"], bData["xmax"] do
  121.                 xOffset = math.abs(halfSize - (i - bData["xmin"]))
  122.                 if math.floor(math.abs(math.sqrt(scaledYOffset^2+xOffset^2) - halfSize)-1.9) < 0 then
  123.                     display.setCursorPos(i, j)
  124.                     display.write(" ")
  125.                 end
  126.             end
  127.         end
  128.     end
  129.     display.setBackgroundColor(colors.black)
  130. end
  131.  
  132. function screen()
  133.     local currColor
  134.     local currDisplayName
  135.     for name,data in pairs(button) do
  136.         local on = data["player"]
  137.         if on == "empty" then
  138.             currDisplayName = " "
  139.         elseif on == "x" then
  140.             currDisplayName = on
  141.         elseif on == "o" then
  142.             currDisplayName = on
  143.         end
  144.         if data["active"] then
  145.             currColor = colors.lime
  146.         else
  147.             if on == "empty" then
  148.                 currColor = colors.white
  149.             elseif on == "x" then
  150.                 currColor = colors.lightGray
  151.             elseif on == "o" then
  152.                 currColor = colors.lightGray
  153.             end
  154.         end
  155.         fill(currDisplayName, currColor, data)
  156.     end
  157. end
  158.  
  159. function toggleButton(name)
  160.     button[name]["active"] = not button[name]["active"]
  161.     screen()
  162. end    
  163.  
  164. function flash(name)
  165.     toggleButton(name)
  166.     screen()
  167.     sleep(0.15)
  168.     toggleButton(name)
  169.     screen()
  170. end
  171.  
  172. function checkxy(x, y, side)
  173.     for name, data in pairs(button) do
  174.         if data["mon"] == side then
  175.             if y>=data["ymin"] and  y <= data["ymax"] then
  176.                 if x>=data["xmin"] and x<= data["xmax"] then
  177.                     if data["param"] == "" then
  178.                         data["func"]()
  179.                     else
  180.                         data["func"](data["param"])
  181.                     end
  182.                     return true
  183.                     --data["active"] = not data["active"]
  184.                     --print(name)
  185.                 end
  186.             end
  187.         end
  188.     end
  189.     return false
  190. end
  191.  
  192. function heading(text, monSide)
  193.     if not testForMonitor(monSide) then
  194.         print("Heading cannot be displayed on "..monSide..".")
  195.         return
  196.     end
  197.     local display = monitors[bData["mon"]]
  198.     w, h = display.getSize()
  199.     display.setCursorPos((w-string.len(text))/2+1, 1)
  200.     display.write(text)
  201. end
  202.  
  203. function label(w, h, text, monSide)
  204.     if not testForMonitor(monSide) then
  205.         print("Heading cannot be displayed on "..monSide..".")
  206.         return
  207.     end
  208.     local display = monitors[bData["mon"]]
  209.     display.setCursorPos(w, h)
  210.     display.write(text)
  211. end
Add Comment
Please, Sign In to add comment