vreezer

Touchscreen

Jul 19th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.69 KB | None | 0 0
  1. -- Declare a table with the buttons that will be printed,
  2. --    with it's own Text Colour and Background Colour and
  3. --    unique x and y values.
  4.  
  5. mon = peripheral.wrap("right")
  6. mon1 = peripheral.wrap("top")
  7. mon.setTextScale(0.5)
  8. mon1.setTextColor(colors.black)
  9. mon1.setBackgroundColor(colors.yellow)
  10. mon1.clear()
  11. mon.setBackgroundColor(colors.yellow)
  12. mon.clear()
  13.  
  14. t = {
  15.     {text = "1 Rezept", x = 4, y = 1, txtCol = colours.yellow, bgCol = colors.black},
  16.     {text = "2 Rezepte", x = 4, y = 3, txtCol = colours.yellow, bgCol = colors.black},
  17.     {text = "3 Rezepte", x = 4, y = 5, txtCol = colours.yellow, bgCol = colors.black},
  18.     {text = "4 Rezepte", x = 4, y = 7, txtCol = colours.yellow, bgCol = colors.black},
  19.     {text = "5 Rezepte", x = 4, y = 9, txtCol = colours.yellow, bgCol = colors.black},
  20. }
  21.  
  22. function writeButtons(_table)
  23.     for i, v in pairs(_table) do
  24.         mon.setCursorPos(v.x, v.y)
  25.         mon.setTextColour(v.txtCol)
  26.         mon.setBackgroundColour(v.bgCol)
  27.         mon.write(v.text)
  28.     end
  29.     return false, nil
  30. end
  31.  
  32. function isValidClick(_table, mx, my)
  33.  
  34.     for i, v in pairs(_table) do
  35.         if mx >= v.x and mx <= (v.x + #v.text) and my == v.y then
  36.              return true, v.text
  37.         end
  38.     end
  39. end
  40.  
  41. function redDown(k)
  42.     while k>0 do
  43.     redstone.setOutput("bottom", true)
  44.     sleep(0.2)
  45.     redstone.setOutput("bottom", false)
  46.     k = k-1
  47.     end
  48. end
  49.  
  50. function countDown()
  51.     i = 35
  52.     while i>0 do
  53.        mon1.setCursorPos(1,2)
  54.        mon1.write("Rezept(e)")
  55.        mon1.setCursorPos(1,3)
  56.        mon1.write("verfügbar")
  57.        mon1.setCursorPos(1,4)
  58.        mon1.write("in:")
  59.        mon1.setCursorPos(17,3)
  60.        mon1.write(i)
  61.        mon1.setCursorPos(17,3)
  62.        i = i-1
  63.        sleep(1)
  64.     end
  65.     mon1.clear()
  66. end
  67.  
  68. writeButtons(t)
  69.  
  70. while true do
  71.     _, but, x, y = os.pullEvent("monitor_touch")
  72.     bClick, option = isValidClick(t, x, y)
  73.     if bClick then
  74.     -- Yes, it's a valid click. Now let's do something with the returned text 'option'
  75.     if option == "1 Rezept" then
  76.     i = 3
  77.     redDown(3)
  78.     countDown()
  79.  
  80.     elseif option == "2 Rezepte" then
  81.     redDown()
  82.     sleep(1)
  83.     redDown()
  84.     countDown()
  85.  
  86.     elseif option == "3 Rezepte" then
  87.     redDown()
  88.     sleep(1)
  89.     redDown()
  90.     sleep(1)
  91.     redDown()
  92.     countDown()
  93.  
  94.     elseif option == "4 Rezepte" then
  95.     redDown()
  96.     sleep(1)
  97.     redDown()
  98.     sleep(1)
  99.     redDown()
  100.     sleep(1)
  101.     redDown()
  102.     countDown()
  103.  
  104.     elseif option == "5 Rezepte" then
  105.     redDown()
  106.     sleep(1)
  107.     redDown()
  108.     sleep(1)
  109.     redDown()
  110.     sleep(1)
  111.     redDown()
  112.     sleep(1)
  113.     redDown()
  114.     countDown()
  115.    
  116.     end
  117. end
  118. end
Advertisement
Add Comment
Please, Sign In to add comment