Advertisement
AdventurousMR

Touchscreen Module

Mar 23rd, 2023
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | None | 0 0
  1. --Touchscreen Module--
  2.  
  3. buttons = {}
  4.  
  5. function checkForMonitor()
  6.     local monitor = peripheral.find("monitor")
  7.     if monitor then
  8.         return monitor
  9.     else
  10.         error("No monitor found!")
  11.     end
  12. end
  13.  
  14. function createButton(x1,y1,x2,y2,text,color,func)
  15.     local monitor = checkForMonitor()
  16.     local oldTerm = term.redirect(monitor)
  17.     paintutils.drawFilledBox(x1,y1,x2,y2,color)
  18.     term.setTextColor(colors.black)
  19.     term.setCursorPos(1,y1)
  20.     term.write(text)
  21.     term.redirect(oldTerm)
  22.  
  23.     table.insert(buttons,{x1,y1,x2,y2,text,color,func})
  24. end
  25.  
  26. function render()
  27.     while true do
  28.         local monitor = checkForMonitor()
  29.         local x,y = monitor.getSize()
  30.         local oldTerm = term.redirect(monitor)
  31.         term.setBackgroundColor(colors.black)
  32.         term.clear()
  33.         paintutils.drawFilledBox(1,1,x/monitor.getTextScale(),1,colors.orange)
  34.         term.setTextColor(colors.black)
  35.         term.setCursorPos(1,1)
  36.         term.write("Lift")
  37.         for i=1, #buttons do
  38.             local x1 = buttons[i][1]
  39.             local y1 = buttons[i][2]
  40.             local x2 = buttons[i][3]
  41.             local y2 = buttons[i][4]
  42.             local text = buttons[i][5]
  43.             local color = buttons[i][6]
  44.             paintutils.drawFilledBox(x1,y1,x2,y2,color)
  45.             term.setTextColor(colors.black)
  46.             term.setCursorPos(x1,y1)
  47.             term.write(text)
  48.         end
  49.         term.redirect(oldTerm)
  50.         sleep()
  51.     end
  52. end
  53.  
  54. function mainShell()
  55.     while true do
  56.         event, side, xPos, yPos = os.pullEvent("monitor_touch")
  57.         for i=1, #buttons do
  58.             local x1 = buttons[i][1]
  59.             local y1 = buttons[i][2]
  60.             local x2 = buttons[i][3]
  61.             local y2 = buttons[i][4]
  62.             local text = buttons[i][5]
  63.             local color = buttons[i][6]
  64.             if x1 <= xPos and y1 <= yPos and x2 >= xPos and y2 >= yPos then
  65.                 buttons[i][6] = colors.gray
  66.             else
  67.                 buttons[i][6] = colors.white
  68.             end
  69.         end
  70.     end
  71. end
  72.  
  73. function boot()
  74.     parallel.waitForAll(mainShell,render)
  75. end
  76.  
  77. -- boot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement