Advertisement
balloonanimal

SPSecurityControl

May 17th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.93 KB | None | 0 0
  1. --pastebin code: GzeD85fR
  2.  
  3. rednet.open("left")
  4. local mon = peripheral.wrap("top")
  5. local mX, mY = mon.getSize()
  6. mon.setTextScale(1)
  7. mon.setTextColor(colors.white)
  8. local button={}
  9. mon.setBackgroundColor(colors.black)
  10.  
  11. function CreateButton(name, func, minX, minY, maxX, maxY)
  12.     button[name] = {}
  13.     button[name]["func"] = func
  14.     button[name]["active"] = false
  15.     button[name]["xmin"] = minX
  16.     button[name]["ymin"] = minY
  17.     button[name]["xmax"] = maxX
  18.     button[name]["ymax"] = maxY
  19. end
  20.  
  21. function SetUpButtons()
  22.     CreateButton("Lockdown", LockDown, 2, 3, 20, 5)
  23.     CreateButton("Main Door", CloseMain, 2, 7, 20, 9)
  24.     CreateButton("Alarm", Alarm, mX - 21, 3, mX - 1, 5)
  25.  
  26. end
  27.  
  28. function Alarm()
  29.     for funcName, funcData in pairs(button) do
  30.         if funcName == "Alarm" then
  31.             if funcData["active"] ~= true then
  32.                 rs.setOutput("back", false)
  33.                 print("Alarm off")
  34.             else
  35.                 rs.setOutput("back", true)
  36.                 print("Alarm on")
  37.             end
  38.         end
  39.     end
  40. end
  41.  
  42. function Title(text)
  43.     mon.setCursorPos((mX - string.len(text)) / 2 + 1, 1)
  44.     mon.write(text)
  45. end
  46.  
  47. function Screen()
  48.     local currColor
  49.     for name,data in pairs(button) do
  50.         local on = data["active"]
  51.         if on == true then
  52.             currColor = colors.lime
  53.         else currColor = colors.red
  54.     end
  55.    
  56.     fill(name, currColor, data)
  57.     end
  58. end
  59.  
  60. function checkxy(x, y)
  61.    local activeFunc = "none"
  62.    print(x.."  "..y)
  63.    for name, data in pairs(button) do
  64.       if y>=data["ymin"] and  y <= data["ymax"] then
  65.          if x>=data["xmin"] and x<= data["xmax"] then
  66.         data["active"] = not data["active"]
  67.         activeFunc = name
  68.             print(name)
  69.          end
  70.       end
  71.    end
  72.    if (activeFunc ~= "none") then
  73.     button[activeFunc]["func"]()
  74.    end
  75. end
  76.  
  77. function fill(text, color, bData)
  78.    mon.setBackgroundColor(color)
  79.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  80.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  81.    for j = bData["ymin"], bData["ymax"] do
  82.       mon.setCursorPos(bData["xmin"], j)
  83.       if j == yspot then
  84.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  85.             if k == xspot then
  86.                mon.write(text)
  87.             else
  88.                mon.write(" ")
  89.             end
  90.          end
  91.       else
  92.          for i = bData["xmin"], bData["xmax"] do
  93.             mon.write(" ")
  94.          end
  95.       end
  96.    end
  97.    mon.setBackgroundColor(colors.black)
  98. end
  99.  
  100. function CloseMain()
  101.     for funcName, funcData in pairs(button) do
  102.                 local on = funcData["active"]
  103.         if funcName == "Main Door" then
  104.                     if on ~= true then
  105.                             rednet.send(35, "open")                        
  106.                     else
  107.                             rednet.send(35, "close")                                        
  108.                     end
  109.         end
  110.         end
  111.     return true
  112. end
  113.  
  114. function LockDown()
  115.     for name2,data2 in pairs(button) do
  116.         local on = data2["active"]
  117.         if name2 == "Lockdown" then
  118.             if on ~= true then
  119.                 print("OPEN")
  120.                 rs.setOutput("back", false)
  121.                 rednet.send(35, "open")
  122.                 rednet.send(36, "open")
  123.                 rednet.send(30, "open")
  124.                 rednet.send(37, "open")
  125.                 rednet.send(38, "open")
  126.                 rednet.send(39, "open")
  127.                 rednet.send(40, "open")
  128.                 rednet.send(41, "open")
  129.                 rednet.send(42, "open")
  130.                 button["Alarm"]["active"] = false
  131.             else
  132.                 print("CLOSE")
  133.                 rs.setOutput("back", true)
  134.                 rednet.send(35, "close")
  135.                 rednet.send(36, "close")
  136.                 rednet.send(30, "close")
  137.                 rednet.send(37, "close")
  138.                 rednet.send(38, "close")
  139.                 rednet.send(39, "close")
  140.                 rednet.send(40, "close")
  141.                 rednet.send(41, "close")
  142.                 rednet.send(42, "close")
  143.                 button["Alarm"]["active"] = true           
  144.             end
  145.         end
  146.     end
  147.     return true
  148. end
  149.  
  150. SetUpButtons()
  151. while true do
  152.     mon.clear()
  153.     Title("Security Control")
  154.     Screen()   
  155.     local event, side, x, y = os.pullEvent()
  156.     if event == "monitor_touch" then
  157.         checkxy(x, y)
  158.     end
  159.     sleep(0.1) 
  160. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement