BigSHinyToys

Computer Craft movie player controller

Dec 20th, 2012
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.56 KB | None | 0 0
  1. --[[
  2.         remote cincima control
  3.         by BigShinyToys
  4. ]]--
  5. local page = 0
  6. local movies = {}
  7.  
  8. local function openDevice(sType)
  9.     for i,v in pairs(rs.getSides()) do
  10.         if peripheral.isPresent(v) and peripheral.getType(v) == sType then
  11.             return peripheral.wrap(v),v
  12.         end
  13.     end
  14. end
  15.  
  16. local _notUsed,sSide = openDevice("modem")
  17. if sSide then
  18.     rednet.open(sSide)
  19. else
  20.     error("No WIFI modem atatched")
  21. end
  22.  
  23. local function clear()
  24.     term.setBackgroundColor(colors.black)
  25.     term.setTextColor(colors.white)
  26.     term.clear()
  27.     term.setCursorPos(1,1)
  28. end
  29.  
  30. local function ping()
  31.     rednet.broadcast("pingMovie")
  32.     movies = {}
  33.     local timeOut = os.startTimer(2)
  34.     while true do
  35.         local event = {os.pullEvent()}
  36.         if event[1] == "rednet_message" then
  37.             if event[3] == "IamMovie" then
  38.                 table.insert(movies,event[2])
  39.             end
  40.         elseif event[1] == "timer" and event[2] == timeOut then
  41.             return
  42.         end
  43.     end
  44.     if #movies > 1 then
  45.         page = 1
  46.     else
  47.         page = 0
  48.     end
  49. end
  50.  
  51. local function printC(posX,posY,textCol,backCol,text)
  52.     term.setCursorPos(posX,posY)
  53.     term.setTextColor(colors[textCol] or textCol)
  54.     term.setBackgroundColor(colors[backCol] or backCol)
  55.     term.write(text)
  56. end
  57.  
  58. local tList = {{itm = "* play",mes = "play"},
  59. {itm = "* pause",mes = "pause"},
  60. {itm = "* Reset Film",mes = "newFilm"},
  61. {itm = "* clear",mes = "clear"},
  62. {itm = "* play all"},
  63. {itm = "* Reset All"}
  64. }
  65. clear()
  66. ping()
  67. while true do
  68.     printC(1,1,"blue","lightBlue"," <   > "..page.." of "..#movies)
  69.     printC(1,2,"blue","lightBlue","* Ping")
  70.     for i = 1,#tList do
  71.         printC(1,i+2,"green","lime",tList[i].itm)
  72.     end
  73.     local event = {os.pullEvent()}
  74.     if event[1] == "mouse_click" then
  75.         if event[2] == 1 then
  76.             if event[4] == 1 then
  77.                 if event[3] <= 3 then
  78.                     page = page + 1
  79.                 elseif event[3] > 3 and event[3] < 6 then
  80.                     page = page - 1
  81.                 end
  82.                 if #movies > 1 then
  83.                     if page < 1 then
  84.                         page = #movies
  85.                     elseif page > #movies then
  86.                         page = 0
  87.                     end
  88.                 elseif #movies == 1 then
  89.                     page = 1
  90.                 else
  91.                     page = 0
  92.                 end
  93.             elseif event[4] == 2 then
  94.                 if event[3] < 6 then
  95.                     ping()
  96.                 end
  97.             elseif tList[event[4]-2] then
  98.                 if tList[event[4]-2].mes then
  99.                     if movies[i] then
  100.                         rednet.send(movies[page],tList[event[4]-2].mes)
  101.                     end
  102.                 else
  103.                     if tList[event[4]-2].itm == "* play all" then
  104.                         for i = 1,#movies do
  105.                             rednet.send(movies[i],"play")
  106.                         end
  107.                     elseif tList[event[4]-2].itm == "* Reset All" then
  108.                         for i = 1,#movies do
  109.                             rednet.send(movies[i],"newFilm")
  110.                         end
  111.                     end
  112.                 end
  113.             end
  114.         end
  115.     end
  116. end
Add Comment
Please, Sign In to add comment