Morphik08

Button edit

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