Advertisement
GNOOR1S

Button API For Advanced Computers : CC: Tweaked

Aug 31st, 2022
1,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.61 KB | None | 0 0
  1. --[[
  2.     Button API for Advanced Computers
  3.  
  4.     This was originally developed for monitors
  5.         adapted for computers
  6.  
  7.     This was originally designed by Direwolf20
  8.     Updated by GNOOR1S/LostboyDev/Gunnar Jessee
  9.  
  10. ]]
  11.  
  12. term.setTextColor(colors.white)
  13. local button={}
  14. term.setBackgroundColor(colors.black)
  15.  
  16. function clearTable()
  17.    button = {}
  18.    term.clear()
  19. end
  20.                
  21. function setTable(name, func, xmin, ymin, xmax, ymax, col)
  22.    button[name] = {}
  23.    button[name]["func"] = func
  24.    button[name]["active"] = false
  25.    button[name]["xmin"] = xmin
  26.    button[name]["ymin"] = ymin
  27.    button[name]["xmax"] = xmax
  28.    button[name]["ymax"] = ymax
  29.    button[name]["color"] = col
  30. end
  31.  
  32.  
  33. function fill(text, color, bData)
  34.    term.setBackgroundColor(color)
  35.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  36.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  37.    for j = bData["ymin"], bData["ymax"] do
  38.       term.setCursorPos(bData["xmin"], j)
  39.       if j == yspot then
  40.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  41.             if k == xspot then
  42.                term.write(text)
  43.             else
  44.                term.write(" ")
  45.             end
  46.          end
  47.       else
  48.          for i = bData["xmin"], bData["xmax"] do
  49.             term.write(" ")
  50.          end
  51.       end
  52.    end
  53.    term.setBackgroundColor(colors.black)
  54. end
  55.      
  56. -- Refreshes screen
  57. function screen()
  58.    local currColor
  59.    for name,data in pairs(button) do
  60.       local on = data["active"]
  61.       if on == true then currColor = colors.lime else currColor = colors.red end
  62.       fill(name, currColor, data)
  63.    end
  64. end
  65.  
  66. -- Toggles on and off color
  67. function toggleButton(name)
  68.    button[name]["active"] = not button[name]["active"]
  69.    screen()
  70. end    
  71.  
  72. -- Put into the function that is contained in your button
  73. function flash(name)
  74.    toggleButton(name)
  75.    screen()
  76.    sleep(0.15)
  77.    toggleButton(name)
  78.    screen()
  79. end
  80.                                              
  81. function checkxy(x, y)
  82.    for name, data in pairs(button) do
  83.       if y>=data["ymin"] and  y <= data["ymax"] then
  84.          if x>=data["xmin"] and x<= data["xmax"] then
  85.             data["func"]()
  86.             return true
  87.          end
  88.       end
  89.    end
  90.    return false
  91. end
  92.  
  93. -- Centers text at the top of the computer
  94. function heading(text)
  95.    local w, h = term.getSize()
  96.    term.setCursorPos((w-string.len(text))/2+1, 1)
  97.    term.write(text)
  98. end
  99.  
  100. -- Just writes text has if it were a button
  101. -- WIP
  102. function label(w, h, text)
  103.    term.setCursorPos(w, h)
  104.    term.write(text)
  105. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement