Nezn

Disk Drive Music GUI [CC]

Jul 8th, 2015
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.91 KB | None | 0 0
  1. lengths = {["C418 - 13"]=180, ["C418 - cat"]=186, ["C418 - blocks"]=347, ["C418 - chirp"]=186, ["C418 - far"]=172, ["C418 - mall"]=197, ["C418 - mellohi"]=98, ["C418 - stal"]=151, ["C418 - strad"]=191, ["C418 - ward"]=250, ["C418 - wait"]=238, ["C418 - 11"]=70}
  2. args = { ... }
  3. monitor, drive = nil, nil
  4. timer = 0
  5. addX = 0
  6. addY = 0
  7.  
  8. function find(name)
  9.     local e = peripheral.getNames()
  10.     for i=1,#e do
  11.         if peripheral.getType(e[i]) == "monitor" and name == "monitor" then
  12.             monitor = peripheral.wrap(e[i])
  13.             return
  14.         elseif peripheral.getType(e[i]) == "drive" and name == "drive" then
  15.             drive = e[i]
  16.             return
  17.         end
  18.     end
  19.     if name == "monitor" then
  20.         if monitor == nil then
  21.             write("Can't find monitor!")
  22.             error(r)
  23.         end
  24.     elseif name == "drive" then
  25.         if drive == nil then
  26.             write("Can't find disk drive!")
  27.             error(r)
  28.         end
  29.     end
  30. end
  31.  
  32. function start()
  33.     if args[1] == "help" then
  34.         print("You can use arguments to set names of devices.")
  35.         print("1st arg: name or side of a monitor")
  36.         print("2nd arg: name or side of a disk drive")
  37.         error(r)
  38.     elseif args[1] ~= nil then
  39.         monitor = peripheral.wrap(args[1])
  40.         if monitor == nil or peripheral.getType(args[1]) ~= "monitor" then
  41.             print("Wrong name of the monitor!")
  42.             print("Try to find it? (Y/N)")
  43.             if string.lower(read()) == "y" then
  44.                 find("monitor")
  45.             else
  46.                 error(r)
  47.             end
  48.         end
  49.     end
  50.     if args[2] ~= nil then
  51.         drive = peripheral.wrap(args[2])
  52.         if drive ~= nil and peripheral.getType(args[2]) == "drive" then
  53.             drive = args[2]
  54.         else
  55.             print("Wrong name of the drive!")
  56.             print("Try to find it? (Y/N)")
  57.             if string.lower(read()) == "y" then
  58.                 find("drive")
  59.             else
  60.                 error(r)
  61.             end
  62.         end
  63.     end
  64.     if drive == nil then
  65.         find("drive")
  66.     end
  67.     if monitor == nil then
  68.         find("monitor")
  69.     end
  70.     term.clear()
  71.     term.setCursorPos(1,1)
  72.     if monitor.isColor() == false then
  73.         print("Need advanced monitor!")
  74.         error(r)
  75.     end
  76.     print("Look at the monitor! Program has been started.")
  77.     term.redirect(monitor)
  78.     if args[3] ~= nil then
  79.         addX = tonumber(args[3])
  80.         local x, y = term.getSize()
  81.         addY = y-5
  82.     end
  83.     monitor.setTextScale(1)
  84.     term.clear()
  85. end
  86.  
  87. function line(y, backcolor, text)
  88.     term.setCursorPos(addX+1, addY+y)
  89.     term.setBackgroundColor(backcolor)
  90.     for i=1,9-string.len(text) do
  91.         text = text.." "
  92.     end
  93.     term.write(text)
  94. end
  95.  
  96. function check()
  97.         if disk.isPresent(drive) and disk.hasAudio(drive) then
  98.             line(1, colors.blue, string.sub(disk.getLabel(drive),8))
  99.         else
  100.             os.cancelTimer(timer)
  101.             line(1, colors.red, "NO DISC")
  102.         end
  103. end
  104.  
  105. function work()
  106.     start()
  107.     check()
  108.     monitor.setTextScale(1)
  109.     term.setCursorPos(1,1)
  110.     while true do
  111.         line(2, colors.gray, "PLAY")
  112.         line(3, colors.gray, "STOP")
  113.         line(4, colors.gray, "EJECT")
  114.         line(5, colors.gray, "EXIT")
  115.         local event, arg1, arg2, arg3 = os.pullEvent()
  116.         if event == "monitor_touch" then
  117.             if arg3 ~= addY+1 then
  118.                 check()
  119.             end
  120.             if arg3 == addY+2 and disk.isPresent(drive) and disk.hasAudio(drive) then
  121.                 line(1, colors.green, string.sub(disk.getLabel(drive),8))
  122.                 timer = os.startTimer(lengths[disk.getLabel(drive)])
  123.                 disk.playAudio(drive)
  124.             elseif arg3 == addY+3 and disk.isPresent(drive) and disk.hasAudio(drive) then
  125.                 disk.stopAudio(drive)
  126.                 os.cancelTimer(timer)
  127.                 line(1, colors.blue, string.sub(disk.getLabel(drive),8))
  128.             elseif arg3 == addY+4 and disk.isPresent(drive) then
  129.                 disk.eject(drive)
  130.                 os.cancelTimer(timer)
  131.                 line(1, colors.red, "NO DISC")
  132.                 sleep(1)
  133.             elseif arg3 == addY+5 then
  134.                 if disk.isPresent(drive) and disk.hasAudio(drive) then
  135.                     disk.stopAudio(drive)
  136.                 end
  137.                 term.setBackgroundColor(colors.black)
  138.                 term.clear()
  139.                 term.redirect(term.native())
  140.                 shell.run("reboot")
  141.             end
  142.         elseif event == "disk" or event == "disk_eject" then
  143.             check()
  144.         elseif event == "timer" and arg1 == timer then
  145.             disk.playAudio(drive)
  146.         end
  147.     end
  148. end
  149.  
  150. work() --Start application
Advertisement
Add Comment
Please, Sign In to add comment