Advertisement
Slaide

Untitled

Mar 31st, 2023
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. monitor = peripheral.find("monitor")
  2. width, height = monitor.getSize()
  3. title = "Ore Processing"
  4.  
  5. buttons = {}
  6.  
  7. function createButton(name, func, x, y, w, h)
  8. nameLen = string.len(name)
  9. buttons[name] = { name = name, func = func, x = x, y = y, w = w, h = h, len = nameLen }
  10. monitor.setBackgroundColor(colors.red)
  11. for i = y, y + h do
  12. monitor.setCursorPos(x, i)
  13. for j = 1, w do
  14. monitor.write(" ")
  15. end
  16. end
  17. monitor.setCursorPos((x + x + w) / 2 - nameLen / 2, (y + y + h) / 2)
  18. monitor.write(name)
  19.  
  20. --monitor.setCursorPos(5, 3)
  21. --monitor.write()
  22. end
  23.  
  24. function checkEvent()
  25. event, side, xPos, yPos = os.pullEvent("monitor_touch")
  26. print(event .. " => Side: " .. tostring(side) .. ", " ..
  27. "X: " .. tostring(xPos) .. ", " ..
  28. "Y: " .. tostring(yPos))
  29. for _, btn in pairs(buttons) do
  30. print(tostring(xPos >= btn.x))
  31. print(tostring(xPos < (btn.x + btn.w)))
  32. print(tostring(yPos >= btn.y))
  33. print(tostring(yPos < (btn.y + btn.h)))
  34. if (xPos >= btn.x) and (xPos < (btn.x + btn.w)) and (yPos >= btn.y) and (yPos < (btn.y + btn.h)) then
  35. print("clicked " .. btn.name)
  36. end
  37. end
  38. end
  39.  
  40. function draw()
  41. monitor.setBackgroundColor(colors.black)
  42. monitor.clear()
  43. monitor.setCursorPos(width / 2 - 7, 1)
  44. monitor.write(title)
  45.  
  46. createButton("Test Button", test1, 5, 3, 13, 2)
  47. end
  48.  
  49. while true do
  50. draw()
  51. checkEvent()
  52. os.sleep(0.5)
  53. end
  54.  
  55. -- while true do
  56. -- event, side, xPos, yPos = os.pullEvent("monitor_touch")
  57. -- print(event .. " => Side: " .. tostring(side) .. ", " ..
  58. -- "X: " .. tostring(xPos) .. ", " ..
  59. -- "Y: " .. tostring(yPos))
  60. -- end
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement