Miner8149

button

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