Advertisement
Guest User

buttons5

a guest
Aug 14th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.60 KB | None | 0 0
  1. t = {
  2. { label = "Button1", x = 1, y = 1, tCol = colors.lime, bgCol = colors.red },
  3. { label = "Button2", x = 1, y = 2, tCol = colors.lime, bgCol = colors.red },
  4. { label = "Button3", x = 1, y = 3, tCol = colors.lime, bgCol = colors.red },
  5. { label = "Button4", x = 1, y = 4, tCol = colors.lime, bgCol = colors.red }
  6. }
  7.  
  8. m = peripheral.wrap("left")
  9. rednet.open("right")
  10.  
  11. m.setBackgroundColor(colors.black)
  12. m.clear()
  13.  
  14. --function function1()
  15. --print("function 1 running!")
  16. --sleep(5)
  17. --end
  18.  
  19. --function function2()
  20. --print("function 2 running!")
  21. --sleep(5)
  22. --end
  23.  
  24. --function function3()
  25. --print("function 3 running!")
  26. --sleep(5)
  27. --end
  28.  
  29. --function function4()
  30. --print("function 4 running!")
  31. --sleep(5)
  32. --end
  33.  
  34.  
  35. function createButtons(_table)
  36.   for i, v in pairs(_table) do
  37.   m.setCursorPos(v.x, v.y)
  38.   m.setTextColor(v.tCol)
  39.   m.setBackgroundColor(v.bgCol)
  40.   m.write(v.label)
  41.   end
  42. end
  43.  
  44. function isButtonClicked(_table, mx, my)
  45.   for i, v in pairs(_table) do
  46.     if mx >= v.x and mx <= (v.x + #v.label) and my == v.y then
  47.     return true, v.label
  48.     end
  49.   end
  50. return false, nil
  51. end
  52.  
  53. createButtons(t)
  54.  
  55. while true do
  56.   e, s, x, y = os.pullEvent("monitor_touch")
  57.   isClicked, option = isButtonClicked(t, x, y)
  58.   if isClicked then
  59.     if option == "Button1" then
  60.     --function1()
  61.     rednet.broadcast(option)
  62.     elseif option == "Button2" then
  63.     --function2()
  64.     rednet.broadcast(option)
  65.     elseif option == "Button3" then
  66.    -- function3()
  67.     rednet.broadcast(option)
  68.     elseif option == "Button4" then
  69.    -- function4()
  70.     rednet.broadcast(option)
  71.     end
  72.   end
  73. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement