MarcosKoco

API_Button

Sep 24th, 2021 (edited)
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.02 KB | None | 0 0
  1. local monitorState
  2. local mon = term
  3.  
  4. local button = {}
  5. local color = {
  6.     normale = {
  7.         text = colors.white,
  8.         background = colors.blue
  9.     },
  10.     clicked = {
  11.         text = colors.blue,
  12.         background = colors.white
  13.     }
  14. }
  15.  
  16. function setMonitor(state)
  17.    
  18.     monitorState = state
  19.    
  20.     if monitorState then
  21.        
  22.         for i,name in pairs(peripheral.getNames()) do
  23.            
  24.             for j,method in pairs(peripheral.getMethods(name)) do
  25.                
  26.                 if (method == 'isColor') then
  27.                    
  28.                     mon = peripheral.wrap(name)
  29.                     mon.clear()
  30.                     mon.setTextScale(1)
  31.                    
  32.                     local x, y = mon.getSize()
  33.                    
  34.                     if x < 40 or y < 27 then
  35.                        
  36.                         mon.setTextScale(0.5)
  37.                        
  38.                     else
  39.                        
  40.                         mon.setTextScale(1)
  41.                        
  42.                     end
  43.                    
  44.                 end
  45.                
  46.             end
  47.            
  48.         end
  49.  
  50.     else
  51.        
  52.         mon = term
  53.        
  54.     end
  55.    
  56.     mon.setBackgroundColor(colors.black)
  57.     mon.setTextColor(colors.white)
  58.    
  59. end
  60.  
  61. function setColors(c1, c2, c3, c4)
  62.    
  63.     color = {
  64.     normale = {
  65.         text = c2,
  66.         background = c1
  67.     },
  68.     clicked = {
  69.         text = c4,
  70.         background = c3
  71.     }
  72. }
  73.    
  74. end
  75.  
  76. function setTitle(text)
  77.  
  78.   w, h = mon.getSize()
  79.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  80.    mon.write(text)
  81.    
  82. end
  83.  
  84. function setClearLabel(w, h, text)
  85.    
  86.     mon.setCursorPos(w, h)
  87.     mon.clearLine()
  88.     mon.write(text)
  89.    
  90. end
  91.  
  92. function setLabel(w, h, text)
  93.    
  94.     mon.setCursorPos(w, h)
  95.     mon.write(text)
  96.    
  97. end
  98.  
  99. function setColorLabel(w, h, c1, c2, text)
  100.    
  101.     mon.setBackgroundColor(c1)
  102.     mon.setTextColor(c2)
  103.     mon.setCursorPos(w, h)
  104.     mon.write(text)
  105.    
  106. end
  107.  
  108. function clearButtons()
  109.    
  110.    button = {}
  111.    
  112. end
  113.  
  114. function setButton(name, func, xmin, xmax, ymin, ymax, state, extra)
  115.    
  116.     button[name] = {}
  117.     button[name]["func"] = func
  118.     button[name]["extra"] = extra
  119.     button[name]["active"] = false
  120.     button[name]["show"] = state
  121.     button[name]["xmin"] = xmin
  122.     button[name]["ymin"] = ymin
  123.     button[name]["xmax"] = xmax
  124.     button[name]["ymax"] = ymax
  125.    
  126. end
  127.  
  128. function setButtonShow(name, data)
  129.    
  130.     button[name]["show"] = data
  131.    
  132. end
  133.  
  134. function Clear()
  135.     mon.clear()
  136. end
  137.  
  138. function onScreen()
  139.    
  140.    local background
  141.    local text
  142.    
  143.     for name,data in pairs(button) do
  144.        
  145.         if data["show"] then
  146.            
  147.             if mon.isColor then
  148.                
  149.                 if (data["active"]) then
  150.                    
  151.                     background = color.clicked.background
  152.                     text =  color.clicked.text
  153.                    
  154.                 else
  155.                    
  156.                     background = color.normale.background
  157.                     text = color.normale.text
  158.                    
  159.                 end
  160.                
  161.             else
  162.                
  163.                 if (data["active"]) then
  164.                    
  165.                     background = colors.white
  166.                     text = colors.black
  167.                    
  168.                 else
  169.                    
  170.                     background = colors.black
  171.                     text = colors.white
  172.                    
  173.                 end
  174.                
  175.             end
  176.  
  177.             mon.setBackgroundColor(background)
  178.             mon.setTextColor(text)
  179.            
  180.             local yspot = math.floor((data["ymin"] + data["ymax"]) /2)
  181.             local xspot = math.floor((data["xmax"] - data["xmin"] - string.len(name)) /2) +1
  182.            
  183.             for j = data["ymin"], data["ymax"] do
  184.                
  185.                 mon.setCursorPos(data["xmin"]-2, j)
  186.                
  187.                 if j == yspot then
  188.                    
  189.                     mon.write(name)
  190.                    
  191.                 else
  192.                    
  193.                     for i = data["xmin"], data["xmax"] do
  194.                        
  195.                         mon.write(" ")
  196.                        
  197.                     end
  198.                    
  199.                 end
  200.                
  201.             end
  202.            
  203.             mon.setBackgroundColor(colors.black)
  204.             mon.setTextColor(colors.white)
  205.            
  206.         end
  207.        
  208.     end
  209.    
  210. end
  211.  
  212. function toggleButton(name)
  213.    
  214.    button[name]["active"] = not button[name]["active"]
  215.    onScreen()
  216.    
  217. end
  218.  
  219. function flashButton(name)
  220.    
  221.    toggleButton(name)
  222.    onScreen()
  223.    sleep(0.15)
  224.    toggleButton(name)
  225.    onScreen()
  226.    
  227. end
  228.  
  229. function buttonPressed(x, y)
  230.    
  231.     for name, data in pairs(button) do
  232.        
  233.         if data["show"] then
  234.            
  235.             if y>=data["ymin"] and  y <= data["ymax"] then
  236.                
  237.                 if x>=data["xmin"] and x<= data["xmax"] then
  238.                    
  239.                     flashButton(name)
  240.                    
  241.                     if data["extra"] then
  242.                        
  243.                         data["func"](data["extra"])
  244.                        
  245.                     else
  246.                        
  247.                         data["func"]()
  248.                        
  249.                     end
  250.                    
  251.                     return true
  252.                    
  253.                 end
  254.                
  255.             end
  256.            
  257.         end
  258.        
  259.     end
  260.    
  261.     return false
  262.    
  263. end
Advertisement
Add Comment
Please, Sign In to add comment