MarcosKoco

MButtonAPI

Jun 19th, 2021 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.57 KB | None | 0 0
  1. local monitor = true
  2. local mon
  3.  
  4. if monitor then
  5.    
  6.     for i,name in pairs(peripheral.getNames()) do
  7.        
  8.         for j,method in pairs(peripheral.getMethods(name)) do
  9.            
  10.             if (method == 'isColor') then
  11.                
  12.                 mon = peripheral.wrap(name)
  13.                 mon.clear()
  14.                 mon.setTextScale(1)
  15.                
  16.                 local x, y = mon.getSize()
  17.                
  18.                 if x < 40 or y < 27 then
  19.                    
  20.                     mon.setTextScale(0.5)
  21.                    
  22.                 else
  23.                    
  24.                     mon.setTextScale(1)
  25.                    
  26.                 end
  27.                
  28.             end
  29.            
  30.         end
  31.        
  32.     end
  33.  
  34. else
  35.    
  36.     mon = term
  37.    
  38. end
  39.  
  40. local button={}
  41. local color = {
  42.     normale = {
  43.         text = colors.black,
  44.         background = colors.red
  45.     },
  46.     clicked = {
  47.         text = colors.black,
  48.         background = colors.lime
  49.     }
  50. }
  51.  
  52. function setColors(c1, c2, c3, c4)
  53.    
  54.     color = {
  55.     normale = {
  56.         text = c2,
  57.         background = c1
  58.     },
  59.     clicked = {
  60.         text = c4,
  61.         background = c3
  62.     }
  63. }
  64.    
  65. end
  66.  
  67. mon.setTextColor(colors.white)
  68. mon.setBackgroundColor(colors.black)
  69.  
  70. function clearTable()
  71.    button = {}
  72. end
  73.  
  74. function setShow(name, data)
  75.    
  76.     button[name]["show"] = data
  77.    
  78. end
  79.  
  80. function setTable(name, func, xmin, xmax, ymin, ymax, extra)
  81.     button[name] = {}
  82.     button[name]["func"] = func
  83.     button[name]["extra"] = extra
  84.     button[name]["active"] = false
  85.     button[name]["show"] = true
  86.     button[name]["xmin"] = xmin
  87.     button[name]["ymin"] = ymin
  88.     button[name]["xmax"] = xmax
  89.     button[name]["ymax"] = ymax
  90. end
  91.  
  92. function fill(text, backgroundcolor, textcolor, bData)
  93.    mon.setBackgroundColor(backgroundcolor)
  94.    mon.setTextColor(textcolor)
  95.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  96.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  97.    for j = bData["ymin"], bData["ymax"] do
  98.       mon.setCursorPos(bData["xmin"], j)
  99.       if j == yspot then
  100.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  101.             if k == xspot then
  102.                mon.write(text)
  103.             else
  104.                mon.write(" ")
  105.             end
  106.          end
  107.       else
  108.          for i = bData["xmin"], bData["xmax"] do
  109.             mon.write(" ")
  110.          end
  111.       end
  112.    end
  113.    mon.setBackgroundColor(colors.black)
  114.    mon.setTextColor(colors.white)
  115. end
  116.  
  117. function screen()
  118.    local background
  119.    local text
  120.    for name,data in pairs(button) do
  121.       local on = data["active"]
  122.       if mon.isColor then
  123.         if on == true then
  124.             background = color.clicked.background
  125.             text =  color.clicked.text
  126.         else
  127.             background = color.normale.background
  128.             text = color.normale.text
  129.         end
  130.       else
  131.         if on == true then
  132.             background = colors.white
  133.             text = colors.black
  134.         else
  135.             background = colors.black
  136.             text = colors.white
  137.         end
  138.       end
  139.       if data["show"] then
  140.        
  141.         fill(name, background, text, data)
  142.        
  143.       end
  144.    end
  145. end
  146.  
  147. function toggleButton(name)
  148.    button[name]["active"] = not button[name]["active"]
  149.    screen()
  150. end    
  151.  
  152. function flash(name)
  153.    toggleButton(name)
  154.    screen()
  155.    sleep(0.15)
  156.    toggleButton(name)
  157.    screen()
  158. end
  159.  
  160. function checkxy(x, y)
  161.    for name, data in pairs(button) do
  162.     if data["show"] then
  163.       if y>=data["ymin"] and  y <= data["ymax"] then
  164.          if x>=data["xmin"] and x<= data["xmax"] then
  165.             flash(name)
  166.             if data["extra"] then
  167.                 data["func"](data["extra"])
  168.             else
  169.                 data["func"]()
  170.             end
  171.             return true
  172.            
  173.          end
  174.       end
  175.     end
  176.    end
  177.    return false
  178. end
  179.  
  180. function heading(text)
  181.    w, h = mon.getSize()
  182.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  183.    mon.write(text)
  184. end
  185.  
  186. function label(w, h, text)
  187.     mon.setCursorPos(w, h)
  188.     mon.write(text)
  189. end
Advertisement
Add Comment
Please, Sign In to add comment