Advertisement
Guest User

button

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