mik_mead

computercraft touchscreen control system origin #direwolf20

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