mik_mead

computercraft touchscreen control system origin #direwolf20

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