Advertisement
MajorVictory

ComputerCraft Improved Button API

May 2nd, 2014
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.45 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 addButton(name, func, xmin, xmax, ymin, ymax)
  8.     setTable(name, func, xmin, xmax, ymin, ymax)
  9. end
  10.      
  11. function setTable(name, func, xmin, xmax, ymin, ymax)
  12.    button[name] = {}
  13.    button[name]["func"] = func
  14.    button[name]["active"] = false
  15.    button[name]["xmin"] = xmin
  16.    button[name]["ymin"] = ymin
  17.    button[name]["xmax"] = xmax
  18.    button[name]["ymax"] = ymax
  19. end
  20.  
  21. function fill(text, color, bData)
  22.    mon.setBackgroundColor(color)
  23.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  24.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  25.    for j = bData["ymin"], bData["ymax"] do
  26.       mon.setCursorPos(bData["xmin"], j)
  27.       if j == yspot then
  28.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  29.             if k == xspot then
  30.                mon.write(text)
  31.             else
  32.                mon.write(" ")
  33.             end
  34.          end
  35.       else
  36.          for i = bData["xmin"], bData["xmax"] do
  37.             mon.write(" ")
  38.          end
  39.       end
  40.    end
  41.    mon.setBackgroundColor(colors.black)
  42. end
  43.  
  44. function screen()
  45.    local currColor
  46.    for name,data in pairs(button) do
  47.       local on = data["active"]
  48.       if on == true then currColor = colors.lime else currColor = colors.red end
  49.       fill(name, currColor, data)
  50.    end
  51. end
  52.  
  53. function toggleButton(name)
  54.    button[name]["active"] = not button[name]["active"]
  55.    screen()
  56. end
  57.  
  58. function setState(name, state)
  59.    button[name]["active"] = state
  60.    screen()
  61. end
  62.  
  63. function getState(name)
  64.    return button[name]["active"]
  65. end
  66.  
  67. function setMonitorSide(side)
  68.     mon = peripheral.wrap(side)
  69. end
  70.  
  71. function flash(name)
  72.    toggleButton(name)
  73.    screen()
  74.    sleep(0.15)
  75.    toggleButton(name)
  76.    screen()
  77. end
  78.  
  79. function checkxy(x, y)
  80.    for name, data in pairs(button) do
  81.       if y>=data["ymin"] and  y <= data["ymax"] then
  82.          if x>=data["xmin"] and x<= data["xmax"] then
  83.             data["func"]()
  84.             return true
  85.             --data["active"] = not data["active"]
  86.             --print(name)
  87.          end
  88.       end
  89.    end
  90.    return false
  91. end
  92.  
  93. function heading(text)
  94.    w, h = mon.getSize()
  95.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  96.    mon.write(text)
  97. end
  98.  
  99. function label(w, h, text)
  100.    mon.setCursorPos(w, h)
  101.    mon.write(text)
  102. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement