Advertisement
Kimchomat

button

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