Advertisement
kaibochan

testbuttons.lua

Feb 16th, 2025 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.30 KB | None | 0 0
  1. local gui = require("gui")
  2.  
  3. function findMonitor()
  4.     local available_perphs = peripheral.getNames()
  5.     local selected_monitor = nil
  6.     for index, perph_name in ipairs(available_perphs) do
  7.         if string.find(perph_name, "monitor") then
  8.             selected_monitor = perph_name
  9.             break
  10.         end
  11.     end
  12.  
  13.     return peripheral.wrap(selected_monitor)
  14. end
  15.  
  16. function main()
  17.  
  18.     local monitor = findMonitor()
  19.     local buffer = createBuffer(monitor, "buffer", colors.black)
  20.  
  21.     local start_button = Button:new {
  22.         name = "start_button",
  23.         buffer = buffer,
  24.         parent = buffer,
  25.         x = 2,
  26.         y = 2,
  27.         text = "Start",
  28.         horizontalAlignment = align.center,
  29.         verticalAlignment = align.center,
  30.         backgroundColor = colors.red,
  31.         width = 5,
  32.         height = 1,
  33.         padding = 0,
  34.         onClickName = "start_click",
  35.         onClick = function(self)
  36.             print("Clicked!")
  37.             if (self.backgroundColor == colors.red) then
  38.                 self:setBackgroundColor(colors.green)
  39.             else
  40.                 self:setBackgroundColor(colors.red)
  41.             end
  42.         end,
  43.     }
  44.  
  45.     while true do
  46.         buffer:draw()
  47.        
  48.         parallel.waitForAny(handleInputEvents())
  49.     end
  50. end
  51.  
  52. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement