Advertisement
LK005

Touchscreen

Jan 22nd, 2021 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.60 KB | None | 0 0
  1. local tArgs = { ... }
  2. local SIDES = {"front", "left", "back", "right", "top", "bottom"}
  3. local listenChannel = 1
  4. local buttons = {}
  5.  
  6. function isSide(side)
  7.     for i=1,6 do
  8.         if side == SIDES[i] then
  9.             return true
  10.         end
  11.     end
  12.     return false
  13. end
  14.  
  15. function findPeripherals()
  16.     local foundPeripherals = {}
  17.     for i=1,6 do
  18.         if peripheral.isPresent(SIDES[i]) == true then
  19.             local peripheralType = peripheral.getType(SIDES[i])
  20.             foundPeripherals[peripheralType] = {side = SIDES[i]}
  21.  
  22.             if peripheralType == "modem" then
  23.                 modemSetup(foundPeripherals)
  24.             elseif peripheralType == "monitor" then
  25.                 monitorSetup(foundPeripherals)
  26.             end
  27.         end
  28.     end
  29.     return foundPeripherals
  30. end
  31.  
  32. function modemSetup(foundPeripherals)
  33.     print("Modem is setting up...")
  34.     modem = peripheral.wrap(foundPeripherals.modem.side)
  35.     modem.open(listenChannel)  -- Open channel 3 so that we can listen on it
  36.     print("Modem is on the "..foundPeripherals.modem.side.." side of the computer...")
  37. end
  38.  
  39. function monitorSetup(foundPeripherals)
  40.     print("Monitor is setting up...")
  41.     monitor = peripheral.wrap(foundPeripherals.monitor.side)
  42.     print("Monitor is on the "..foundPeripherals.monitor.side.." side of the computer...")
  43.     monitor.setBackgroundColor(colors["black"])
  44.     monitor.clear()
  45. end
  46.  
  47. function buttonAdd(tag, xPos, yPos, widthPar, clr)
  48.     if widthPar < 1 then
  49.         local width = string.len(tag)
  50.     else
  51.         local width  = widthPar
  52.     end
  53.  
  54.     table.insert(buttons, {tag = tag, xPos = xPos, yPos = yPos, width = widthPar, color = clr, func = buttonsFunctionSet(tag), active = false})
  55. end
  56.  
  57. function buttonsDraw()
  58.     local n = 1;
  59.  
  60.     while buttons[n] ~= nil do
  61.         monitor.setBackgroundColor(colors[buttons[n].color])
  62.  
  63.         monitor.setCursorPos(buttons[n].xPos, buttons[n].yPos-1)
  64.         for i=1,buttons[n].width do
  65.             monitor.write(" ")
  66.         end
  67.  
  68.         monitor.setCursorPos(buttons[n].xPos, buttons[n].yPos+1)
  69.         for i=1,buttons[n].width do
  70.             monitor.write(" ")
  71.         end
  72.  
  73.         monitor.setCursorPos(buttons[n].xPos, buttons[n].yPos)
  74.         monitor.setTextColor(colors["white"])
  75.  
  76.         local restLength = (buttons[n].width - string.len(buttons[n].tag))/2
  77.  
  78.         if (buttons[n].width - string.len(buttons[n].tag)) % 2 ~= 1 then
  79.             for i=1,restLength do
  80.                 monitor.write(" ")
  81.             end
  82.         else
  83.             for i=1,(restLength+1) do
  84.                 monitor.write(" ")
  85.             end
  86.         end
  87.  
  88.         monitor.write(buttons[n].tag)
  89.  
  90.         for i=1,restLength do
  91.             monitor.write(" ")
  92.         end
  93.  
  94.         n = n+1
  95.     end
  96. end
  97.  
  98. function buttonsSet()
  99.     buttonAdd("spawner", 3, 3, 15, "green")
  100. end
  101.  
  102. function buttonFind(tag)
  103.     for i=1,#buttons do
  104.         if buttons[i].tag == tag then
  105.             return i
  106.         end
  107.     end
  108. end
  109.  
  110. function buttonsFunctionSet(tag)
  111.     if tag == "spawner" then
  112.         local spawner
  113.         function spawner(active)
  114.             if active==false then
  115.                 modem.transmit(3, 1, "on")
  116.                 buttons[buttonFind(tag)].active = true
  117.                 buttons[buttonFind(tag)].color = "red"
  118.             else
  119.                 modem.transmit(3, 1, "off")
  120.                 buttons[buttonFind(tag)].active = false
  121.                 buttons[buttonFind(tag)].color = "green"
  122.             end
  123.         end
  124.  
  125.         return spawner
  126.     end
  127. end
  128.  
  129. function manageActions()
  130.     while true do
  131.         event, side, xPos, yPos = os.pullEvent("monitor_touch")
  132.  
  133.         local n = 1
  134.         local found = false
  135.  
  136.         for i=1,#buttons do
  137.             if xPos >= buttons[n].xPos and xPos <= (buttons[n].xPos + buttons[n].width) then
  138.                 if yPos >= (buttons[n].yPos-1) and yPos <= (buttons[n].yPos+1) then
  139.                     print("Button pressed at: x="..xPos.." y="..yPos)
  140.                     buttons[n].func(buttons[n].active)
  141.                     break
  142.                 end
  143.             end
  144.         end
  145.  
  146.         buttonsDraw()
  147.     end
  148. end
  149.  
  150. -- os.sleep(10)
  151.  
  152. local foundPeripherals = findPeripherals()
  153.  
  154. buttonsSet()
  155. buttonsDraw()
  156.  
  157. manageActions()
  158.  
  159. read()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement