sidekick_

Portal Song Player - PC

Jan 13th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.25 KB | None | 0 0
  1. --[[ Startup file for the computer,
  2.     sends signal to turtle to receive
  3.     table of names of the discs,
  4.     then creates a menu of which to play
  5.     ]]
  6.  
  7. --[[ Variables ]]--
  8. rednet.open("left")
  9. turtleID = 4
  10. t_songs = {}
  11. sX, sY = term.getSize()
  12. running = true
  13. -- Tables
  14. t_songPlaying = {"Stop", "Replay"}
  15.  
  16. local function colorW(...)
  17.   local curColor
  18.   for i=1, #arg do -- arg is ...
  19.     if type(arg[i]) == 'number' then
  20.       curColor = arg[i]
  21.     else
  22.       if curColor then
  23.         term.setTextColor(curColor)
  24.       end
  25.       write(arg[i])
  26.     end
  27.   end
  28. end
  29.  
  30. local function menu(_table)
  31.     local sel = 1
  32.     while true do
  33.         tempSpace = 0
  34.         yPos = 7
  35.         if sel > #_table then sel = 1 end
  36.         if sel < 1 then sel = #_table end
  37.         for i = 1,#_table do
  38.             if i == 10 then tempSpace = 23 yPos = 7 end
  39.             term.setCursorPos(3 + tempSpace, yPos)
  40.             yPos = yPos + 1
  41.             if sel == i then
  42.                 colorW(colours.red, "[  ", colours.lightBlue, _table[i], colours.red, "  ]")
  43.             else
  44.                 colorW("   ", colours.lightBlue, _table[i], "   ")
  45.             end
  46.         end
  47.         local e, key = os.pullEvent("key")
  48.         if key == 200 then -- up key
  49.             sel = sel-1
  50.         elseif key == 208 then -- down key
  51.             sel = sel+1
  52.         elseif key == 28 then
  53.             return _table[sel], sel
  54.         end
  55.     end
  56. end
  57.  
  58. function cWrite(text, x, y, colour)
  59.     colour = colour or colours.black
  60.     term.setTextColour(colour)
  61.     term.setCursorPos(x, y)
  62.     write(text)
  63. end
  64.  
  65. function drawBackground()
  66.     -- Print background
  67.     term.setBackgroundColour(colours.black)
  68.     term.setTextColour(colours.white)
  69.     term.clear()
  70.     term.setCursorPos(1,1)
  71.     bg = paintutils.loadImage("background")
  72.     paintutils.drawImage(bg, 1, 1)
  73.     t = "Music disk player"
  74.     term.setCursorPos( (sX - #t)/2, 2)
  75.     term.setTextColour(colours.lime)
  76.     term.setBackgroundColour(colours.cyan)
  77.     write(t)
  78.     term.setBackgroundColour(colours.grey)
  79. end
  80.  
  81. function songPlaying(songName)
  82.     drawBackground()
  83.     text = "Playing " .. songName
  84.     cWrite(text, (sX-#text)/2, 5, colours.red)
  85.     nSelected = 1
  86.     while true do
  87.         for i = 1, #t_songPlaying do
  88.             term.setCursorPos(i*15, 7)
  89.             if nSelected == i then
  90.                 colorW(colours.red, "[", colours.lime, t_songPlaying[i], colours.red, "]")
  91.             else
  92.                 colorW(" ", colours.lime, t_songPlaying[i], " ")
  93.             end
  94.         end
  95.         e, key = os.pullEvent("key")
  96.         if key == keys.left and nSelected > 1 then nSelected = nSelected - 1
  97.         elseif key == keys.right and nSelected < #t_songPlaying then nSelected = nSelected + 1
  98.         elseif key == keys.enter then break end
  99.     end
  100.     if nSelected == 1 then
  101.         -- Code to stop playing the song
  102.         rednet.send(turtleID, "stop")
  103.         drawBackground()
  104.         text = "Last played song: " .. songName
  105.         cWrite(text, (sX - #text)/2, 5, colours.red)
  106.     elseif nSelected == 2 then
  107.         -- Code to restart the disk
  108.         rednet.send(turtleID, "restart")
  109.         sleep(1)
  110.         return songPlaying(songName)
  111.     end
  112. end
  113.  
  114. function getDiskTitles()
  115.     drawBackground()
  116.     t_songs = {} -- reset the table of songs
  117.     term.setBackgroundColour(colours.grey)
  118.     cWrite("Getting information from turtle...", 3, 5, colours.black)
  119.     rednet.send(turtleID, "getDiskTitles")
  120.     repeat id, message = rednet.receive() until id == turtleID
  121.     if message == "no discs" then
  122.         cWrite("No disks in turtle.               ", 3, 5, colours.black)
  123.         term.setCursorPos(3, 6)
  124.         colorW(colours.lightBlue, "[Try again]")
  125.         repeat e, k = os.pullEvent() until k == keys.enter
  126.         return getDiskTitles()
  127.     else
  128.         t_songs = textutils.unserialize(message)
  129.         table.insert(t_songs, "Exit")
  130.         table.insert(t_songs, "Get Titles")
  131.     end
  132.     drawBackground()
  133. end
  134.  
  135. drawBackground()
  136. getDiskTitles()
  137.  
  138. while running do
  139.     term.setBackgroundColour(colours.grey)
  140.     songName, songNumber = menu(t_songs)
  141.     if songName == "Exit" then
  142.         term.setBackgroundColour(colours.black)
  143.         term.setTextColour(colours.white)
  144.         term.setCursorPos(1, 1)
  145.         term.clear()
  146.         running = false
  147.     elseif songName == "Get Titles" then
  148.         getDiskTitles()
  149.     else
  150.         drawBackground()
  151.         cWrite("Attempting to play " .. songName, 3, 5, colours.red)
  152.         rednet.send(turtleID, songName)
  153.         repeat id, message = rednet.receive() until id == turtleID
  154.         if message == "songFound" then
  155.             songPlaying(songName)
  156.         elseif message == "songNotFound" then
  157.             drawBackground()
  158.             text = "Unable to find " .. songName
  159.             cWrite(text, (sX-#text)/2, 5, colours.red)
  160.         end
  161.     end
  162. end
Advertisement
Add Comment
Please, Sign In to add comment