MarcosKoco

MTIButtonAPI

Jun 19th, 2021 (edited)
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.42 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.yellow
  45.     },
  46.     clicked = {
  47.         text = colors.black,
  48.         background = colors.lime
  49.     }
  50. }
  51.  
  52. mon.setTextColor(colors.white)
  53. mon.setBackgroundColor(colors.black)
  54.  
  55. function clearTable()
  56.    button = {}
  57. end
  58.  
  59. function setShow(name, data)
  60.    
  61.     button[name]["show"] = data
  62.    
  63. end
  64.  
  65. function setTable(name, func, xmin, xmax, ymin, ymax, extra)
  66.     button[name] = {}
  67.     button[name]["func"] = func
  68.     button[name]["extra"] = extra
  69.     button[name]["active"] = false
  70.     button[name]["show"] = true
  71.     button[name]["xmin"] = xmin
  72.     button[name]["ymin"] = ymin
  73.     button[name]["xmax"] = xmax
  74.     button[name]["ymax"] = ymax
  75. end
  76.  
  77. function fill(text, backgroundcolor, textcolor, bData)
  78.    mon.setBackgroundColor(backgroundcolor)
  79.    mon.setTextColor(textcolor)
  80.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  81.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  82.    for j = bData["ymin"], bData["ymax"] do
  83.       mon.setCursorPos(bData["xmin"], j)
  84.       if j == yspot then
  85.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  86.             if k == xspot then
  87.                mon.write(text)
  88.             else
  89.                mon.write(" ")
  90.             end
  91.          end
  92.       else
  93.          for i = bData["xmin"], bData["xmax"] do
  94.             mon.write(" ")
  95.          end
  96.       end
  97.    end
  98.    mon.setBackgroundColor(colors.black)
  99.    mon.setTextColor(colors.white)
  100. end
  101.  
  102. function screen()
  103.    local background
  104.    local text
  105.    for name,data in pairs(button) do
  106.       local on = data["active"]
  107.       if mon.isColor then
  108.         if on == true then
  109.             background = color.clicked.background
  110.             text =  color.clicked.text
  111.         else
  112.             background = color.normale.background
  113.             text = color.normale.text
  114.         end
  115.       else
  116.         if on == true then
  117.             background = colors.white
  118.             text = colors.black
  119.         else
  120.             background = colors.black
  121.             text = colors.white
  122.         end
  123.       end
  124.       if data["show"] then
  125.        
  126.         fill(name, background, text, data)
  127.        
  128.       end
  129.    end
  130. end
  131.  
  132. function toggleButton(name)
  133.    button[name]["active"] = not button[name]["active"]
  134.    screen()
  135. end    
  136.  
  137. function flash(name)
  138.    toggleButton(name)
  139.    screen()
  140.    sleep(0.15)
  141.    toggleButton(name)
  142.    screen()
  143. end
  144.  
  145. function checkxy(x, y)
  146.    for name, data in pairs(button) do
  147.     if data["show"] then
  148.       if y>=data["ymin"] and  y <= data["ymax"] then
  149.          if x>=data["xmin"] and x<= data["xmax"] then
  150.             toggleButton(name)
  151.             if data["extra"] then
  152.                 data["func"](data["extra"])
  153.             else
  154.                 data["func"]()
  155.             end
  156.             return true
  157.            
  158.          end
  159.       end
  160.     end
  161.    end
  162.    return false
  163. end
  164.  
  165. function heading(text)
  166.    w, h = mon.getSize()
  167.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  168.    mon.write(text)
  169. end
  170.  
  171. function label(w, h, text)
  172.     mon.setCursorPos(w, h)
  173.     mon.write(text)
  174. end
Add Comment
Please, Sign In to add comment