MinoCraft72

Button API 1.20+

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