Advertisement
Samurai_12

Untitled

Jun 7th, 2023 (edited)
887
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.17 KB | None | 0 0
  1. --Look throughout the code for comments like this. They explain what to do to change things.
  2.  
  3. --What side the monitor will be on (change this if needed)
  4. side = "right"
  5.  
  6. -- Create table of connected monitors
  7. local myPeripherals = {}
  8.  
  9. for _, name in pairs(peripheral.getNames()) do
  10.   if peripheral.getType(name) == "monitor" then --# change "monitor" to your peripheral type.
  11.     table.insert(myPeripherals, name)
  12.   end
  13. end
  14.  
  15. --wrap monitor as display
  16. m = peripheral.wrap(myPeripherals[1])
  17.  
  18. --button on/off color
  19. bactive = colors.green
  20. binactive=colors.red
  21. --text on/off color
  22. tactive=colors.white
  23. tinactive=colors.black
  24. --Background color
  25. bgcolor = colors.black
  26.  
  27. buttons = {}
  28.        
  29.       function newButton(id,xmin,xmax,ymin,ymax,text,func)
  30.         buttons[id] = {}
  31.         buttons[id]["xmin"] = xmin
  32.         buttons[id]["xmax"] = xmax
  33.         buttons[id]["ymin"] = ymin
  34.         buttons[id]["ymax"] = ymax
  35.         buttons[id]["active"] = false
  36.         buttons[id]["text"] = text
  37.         buttons[id]["func"] = func
  38.        
  39.       end
  40.       ---------------------------------------------------------------------------------
  41.       function printButton(id)
  42.         ymin = buttons[id]["ymin"]
  43.         ymax = buttons[id]["ymax"]
  44.         xmin = buttons[id]["xmin"]
  45.         xmax = buttons[id]["xmax"]
  46.         text = buttons[id]["text"]
  47.         ia = buttons[id]["active"]
  48.  
  49.             width = xmax - xmin
  50.             height = ymax - ymin
  51.            
  52.             if ia then m.setBackgroundColor(bactive) m.setTextColor(tactive)
  53.             else m.setBackgroundColor(binactive) m.setTextColor(tinactive) end
  54.  
  55.             for j = ymin,ymax do
  56.              m.setCursorPos(xmin,j)
  57.               for i = xmin,xmax do
  58.  
  59.                 m.write(" ")
  60.  
  61.              end
  62.  
  63.            end
  64.  
  65.          
  66.         m.setCursorPos(xmin + width / 2 - #text / 2 + 1,ymin + height / 2)
  67.  
  68.        m.write(text)
  69.        m.setBackgroundColor(bgcolor)
  70.        
  71.       end
  72.       ----------------------------------------------------------------------------------
  73.       function refreshButtons()
  74.         for i = 1,#buttons do
  75.           printButton(i)
  76.         end
  77.       end
  78.     function checkxy( x,y )
  79.         for i = 1, #buttons do
  80.           if y >= buttons[i]["ymin"] and y <= buttons[i]["ymax"] then
  81.             if x >= buttons[i]["xmin"] and x <= buttons[i]["xmax"] then
  82.  
  83.              
  84.               buttons[i]["active"] = not buttons[i]["active"]
  85.               clicked = i
  86.               if buttons[i]["func"] ~= nil then
  87.  
  88.                 buttons[i]["func"]()
  89.                
  90.               end
  91.             end
  92.           end
  93.         end
  94.       end
  95.  
  96. bool1 = false
  97. bool2 = false
  98. bool3 = false
  99. bool4 = false
  100. bool5 = false
  101. bool6 = false
  102. bool8 = false
  103. bool9 = false
  104. rs.setBundledOutput("left",0)
  105.  
  106. --functions for buttons, need to be added for more Buttons to work
  107. function func1()
  108.   current = rs.getBundledOutput("left")
  109.     bool1 = not bool1
  110.   if bool1 then
  111.      rs.setBundledOutput("left",current+colors.white)
  112.   else
  113.     rs.setBundledOutput("left",current-colors.white)
  114.   end
  115. end
  116.  
  117. function func2()
  118.   current = rs.getBundledOutput("left")
  119.     bool2 = not bool2
  120.     if bool2 then
  121.    rs.setBundledOutput("left",current+colors.blue)
  122.   else
  123.     rs.setBundledOutput("left",current-colors.blue)
  124.   end
  125. end
  126.  
  127. function func3()
  128.   current = rs.getBundledOutput("left")
  129.     bool3 = not bool3
  130.     if bool3 then
  131.    rs.setBundledOutput("left",current+colors.red)
  132.   else
  133.     rs.setBundledOutput("left",current-colors.red)
  134.   end
  135. end
  136.  
  137. function func4()
  138.   current = rs.getBundledOutput("left")
  139.     bool4 = not bool4
  140.   if bool4 then
  141.      rs.setBundledOutput("left",current+colors.green)
  142.   else
  143.     rs.setBundledOutput("left",current-colors.green)
  144.   end
  145. end
  146.  
  147. function func5()
  148.   current = rs.getBundledOutput("left")
  149.     bool5 = not bool5
  150.   if bool5 then
  151.      rs.setBundledOutput("left",current+colors.yellow)
  152.   else
  153.     rs.setBundledOutput("left",current-colors.yellow)
  154.   end
  155. end
  156.  
  157. function func6()
  158.   current = rs.getBundledOutput("left")
  159.     bool6 = not bool6
  160.   if bool6 then
  161.      rs.setBundledOutput("left",current+colors.black)
  162.   else
  163.     rs.setBundledOutput("left",current-colors.black)
  164.   end
  165. end
  166.  
  167. function func7()
  168.   current = rs.getBundledOutput("left")
  169.     bool7 = not bool7
  170.   if bool7 then
  171.      rs.setBundledOutput("left",current+colors.cyan)
  172.   else
  173.     rs.setBundledOutput("left",current-colors.cyan)
  174.   end
  175. end
  176.  
  177. function func8()
  178.   current = rs.getBundledOutput("left")
  179.     bool8 = not bool8
  180.   if bool8 then
  181.      rs.setBundledOutput("left",current+colors.gray)
  182.   else
  183.     rs.setBundledOutput("left",current-colors.gray)
  184.   end
  185. end
  186.  
  187. --You can add more buttons and also change the size of them. The format is startingx,startingy,endingx,endingy,text,function
  188.  
  189. --The buttons
  190. newButton(1, 3,24,2,9,"Farm 1",func2)
  191. newButton(2, 27,48,2,9,"Farm 2",func3)
  192. newButton(3, 3,24,11,18,"Farm 3",func4)
  193. newButton(4, 27,48,11,18,"Farm 4",func5)
  194. ------------------------------------------------
  195.  
  196. m.clear()
  197. refreshButtons()
  198.  
  199.  
  200. --main loop
  201. while true do
  202.     e,side,x,y = os.pullEvent("monitor_touch")
  203.     checkxy(x,y)
  204.  
  205.     refreshButtons()
  206. end
Advertisement
Comments
  • Rodin172
    339 days
    # text 0.11 KB | 0 0
    1. Am I right in assuming that this is a variable button system for ComputerCraft? because if it is this is amazing.
Add Comment
Please, Sign In to add comment
Advertisement