DeliciousJaffa

Redstone toggle

Mar 22nd, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.36 KB | None | 0 0
  1. local mon = peripheral.wrap("top")
  2. mon.setTextScale(1)
  3. mon.setTextColor(colors.white)
  4. local button={}
  5. mon.setBackgroundColor(colors.black)
  6.      
  7. function setTable(name, func, xmin, xmax, ymin, ymax)
  8.    button[name] = {}
  9.    button[name]["func"] = func
  10.    button[name]["active"] = false
  11.    button[name]["xmin"] = xmin
  12.    button[name]["ymin"] = ymin
  13.    button[name]["xmax"] = xmax
  14.    button[name]["ymax"] = ymax
  15. end
  16.  
  17. function click(state)
  18.   print(state)
  19.   redstone.setOutput("bottom",state)
  20. end
  21.        
  22. function fillTable()
  23.    setTable("Toggle Spawner", click, 2, 17, 3, 11)
  24. end    
  25.  
  26. function fill(text, color, bData)
  27.    mon.setBackgroundColor(color)
  28.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  29.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  30.    for j = bData["ymin"], bData["ymax"] do
  31.       mon.setCursorPos(bData["xmin"], j)
  32.       if j == yspot then
  33.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  34.             if k == xspot then
  35.                mon.write(text)
  36.             else
  37.                mon.write(" ")
  38.             end
  39.          end
  40.       else
  41.          for i = bData["xmin"], bData["xmax"] do
  42.             mon.write(" ")
  43.          end
  44.       end
  45.    end
  46.    mon.setBackgroundColor(colors.black)
  47. end
  48.      
  49. function screen()
  50.    local currColor
  51.    for name,data in pairs(button) do
  52.       local on = data["active"]
  53.       if on == true then currColor = colors.lime else currColor = colors.red end
  54.       fill(name, currColor, data)
  55.    end
  56. end
  57.      
  58. function checkxy(x, y)
  59.    for name, data in pairs(button) do
  60.       if y>=data["ymin"] and  y <= data["ymax"] then
  61.          if x>=data["xmin"] and x<= data["xmax"] then
  62.             data["active"] = not data["active"]
  63.             data["func"](data["active"])
  64.          end
  65.       end
  66.    end
  67. end
  68.      
  69. function heading(text)
  70.    w, h = mon.getSize()
  71.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  72.    mon.write(text)
  73. end
  74.      
  75. fillTable()
  76.  
  77. while true do
  78.    mon.clear()
  79.    heading("Spawner On/Off")
  80.    screen()
  81.    local e,side,x,y = os.pullEvent()
  82.    if e == "redstone" then
  83.     if redstone.getInput("right") == false then
  84.       click(false)
  85.       button["Toggle Spawner"]["active"] = false
  86.     end
  87.    elseif e == "monitor_touch" then
  88.     checkxy(x,y)
  89.    else
  90.     sleep(0.9)
  91.    end
  92.    sleep(0.1)
  93. end
Advertisement
Add Comment
Please, Sign In to add comment