Advertisement
Trotnic

Untitled

Dec 21st, 2021
892
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.10 KB | None | 0 0
  1. t = {
  2.     {text = "Button 1", x = 1, y = 1, txtCol = colours.red, bgCol = colours.lime},
  3.     {text = "Button 2", x = 1, y = 2, txtCol = colours.red, bgCol = colours.lime},
  4.     {text = "Button 3", x = 1, y = 3, txtCol = colours.red, bgCol = colours.lime}
  5. }
  6.  
  7.  
  8. mon = peripherial.wrap("side")
  9.  
  10. function writeButtons(_table)
  11.     for i, v in pairs(_table) do
  12.         term.setCursorPos(v.x, v.y)
  13.         term.setTextColour(v.txtCol)
  14.         term.setBackgroundColour(v.bgCol)
  15.         write(v.text)
  16.     end
  17. end
  18.  
  19. function isValidClick(_table, mx, my)
  20.  
  21.     for i, v in pairs(_table) do
  22.         if mx >= v.x and mx <= (v.x + #v.text) and my == v.y then
  23.              return true, v.text
  24.         end
  25.     end
  26.     return false, nil
  27. end
  28.  
  29. writeButtons(t)
  30.  
  31. while true do
  32.     _, but, x, y = os.pullEvent("monitor_touch")
  33.     bClick, option = isValidClick(t, x, y)
  34.     if bClick then
  35.     -- Yes, it's a valid click. Now let's do something with the returned text 'option'
  36.     if option == "Button 1" then -- button 1...
  37.  
  38.     elseif option == "Button 2" then
  39.  
  40.     elseif option == "Button 3" then
  41.  
  42.     end
  43. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement