Advertisement
sEi_

button

May 8th, 2023
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.98 KB | None | 0 0
  1. local monitor = peripheral.wrap("right") -- Change "right" to the side where the monitor is connected.
  2. local button = {x1 = 2, y1 = 2, x2 = 10, y2 = 4, label = "Click me"}
  3.  
  4. function drawButton()
  5.   monitor.setBackgroundColor(colors.white)
  6.   monitor.setTextColor(colors.black)
  7.   monitor.setCursorPos(button.x1, button.y1)
  8.   for i = button.y1, button.y2 do
  9.     monitor.setCursorPos(button.x1, i)
  10.     monitor.write(string.rep(" ", button.x2 - button.x1 + 1))
  11.   end
  12.   monitor.setCursorPos(button.x1 + 2, button.y1 + 1)
  13.   monitor.write(button.label)
  14. end
  15.  
  16. function isInsideButton(x, y)
  17.   return x >= button.x1 and x <= button.x2 and y >= button.y1 and y <= button.y2
  18. end
  19.  
  20. function waitForClick()
  21.   while true do
  22.     local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  23.     if isInsideButton(xPos, yPos) then
  24.       print("Button clicked!")
  25.       -- Add any action you want to perform when the button is clicked.
  26.     end
  27.   end
  28. end
  29.  
  30. monitor.clear()
  31. drawButton()
  32. waitForClick()
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement