Advertisement
SenpaiJody

buttonTerm

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