Advertisement
Guest User

button

a guest
Jul 15th, 2015
7,645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.95 KB | None | 0 0
  1. monitors = {peripheral.find("monitor")}
  2. local mon = {}
  3. for funcName,_ in pairs(monitors[1]) do
  4.     mon[funcName] = function(...)
  5.         for i=1,#monitors-1 do monitors[i][funcName](unpack(arg)) end
  6.         return monitors[#monitors][funcName](unpack(arg))
  7.     end
  8. end
  9.  
  10. mon.setTextScale(1)
  11. mon.setTextColor(colors.white)
  12. local button={}
  13. mon.setBackgroundColor(colors.black)
  14.  
  15. function clearTable()
  16.    button = {}
  17. end
  18.  
  19. function setButton(name, buttonOn)
  20.    print(name)
  21.    print(button[name]["active"])
  22.    button[name]["active"] = buttonOn
  23.    screen()
  24. end
  25.                                              
  26. function setTable(name, func, param, xmin, xmax, ymin, ymax)
  27.    button[name] = {}
  28.    button[name]["func"] = func
  29.    button[name]["active"] = false
  30.    button[name]["param"] = param
  31.    button[name]["xmin"] = xmin
  32.    button[name]["ymin"] = ymin
  33.    button[name]["xmax"] = xmax
  34.    button[name]["ymax"] = ymax
  35. end
  36.  
  37. function funcName()
  38.    print("You clicked buttonText")
  39. end
  40.        
  41. function fillTable()
  42.    setTable("ButtonText", funcName, 5, 25, 4, 8)
  43. end    
  44.  
  45. function fill(text, color, bData)
  46.    mon.setBackgroundColor(color)
  47.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  48.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  49.    for j = bData["ymin"], bData["ymax"] do
  50.       mon.setCursorPos(bData["xmin"], j)
  51.       if j == yspot then
  52.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  53.             if k == xspot then
  54.                mon.write(text)
  55.             else
  56.                mon.write(" ")
  57.             end
  58.          end
  59.       else
  60.          for i = bData["xmin"], bData["xmax"] do
  61.             mon.write(" ")
  62.          end
  63.       end
  64.    end
  65.    mon.setBackgroundColor(colors.black)
  66. end
  67.      
  68. function screen()
  69.    local currColor
  70.    for name,data in pairs(button) do
  71.       local on = data["active"]
  72.       if on == true then currColor = colors.lime else currColor = colors.red end
  73.       fill(name, currColor, data)
  74.    end
  75. end
  76.  
  77. function toggleButton(name)
  78.    button[name]["active"] = not button[name]["active"]
  79.    screen()
  80. end    
  81.  
  82. function flash(name)
  83.    toggleButton(name)
  84.    screen()
  85.    sleep(0.15)
  86.    toggleButton(name)
  87.    screen()
  88. end
  89.                                              
  90. function checkxy(x, y)
  91.    for name, data in pairs(button) do
  92.       if y>=data["ymin"] and  y <= data["ymax"] then
  93.          if x>=data["xmin"] and x<= data["xmax"] then
  94.             if data["param"] == "" then
  95.               data["func"]()
  96.             else
  97.               data["func"](data["param"])
  98.             end
  99.             return true
  100.             --data["active"] = not data["active"]
  101.             --print(name)
  102.          end
  103.       end
  104.    end
  105.    return false
  106. end
  107.      
  108. function heading(text)
  109.    w, h = mon.getSize()
  110.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  111.    mon.write(text)
  112. end
  113.      
  114. function label(w, h, text)
  115.    mon.setCursorPos(w, h)
  116.    mon.write(text)
  117. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement