Advertisement
CaptainSpaceCat

Score Creator

Jun 7th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.21 KB | None | 0 0
  1. local tArgs = {...}
  2. if #tArgs ~= 1 then
  3.   error("Usage: score <filename>")
  4. end
  5. term.setBackgroundColor(colors.black)
  6. term.clear()
  7. local w, h = term.getSize()
  8. local staffline = "| | | | | | | | | | | | | | | | | | | | | | | | |"
  9. local location = 0
  10. local song = {}    --make this global for debugging
  11. local notetype = nil
  12. local tempo = 80
  13. local filename = tArgs[1]
  14.  
  15. function importSong()
  16.   local linenum = 0
  17.   for line in io.lines(filename) do
  18.     linenum = linenum + 1
  19.     if linenum > 1 then
  20.       song[linenum - 1] = {}
  21.       for i = 1, 24 do
  22.         song[linenum - 1][i] = tonumber(line:sub(i, i))
  23.       end
  24.     end
  25.   end
  26. end
  27.  
  28. if fs.exists(filename) then
  29.   importSong()
  30. end
  31. --[[if not pcall(importSong()) then
  32.   error("ERROR: Invalid song file")
  33. end]]--
  34.  
  35. function saveFile()
  36.   mSave = fs.open(tostring(filename), "w")
  37.   mSave.writeLine("tempo = " .. tostring(tempo))
  38.   for i, v in pairs(song) do
  39.     local line = ""
  40.     for k, e in pairs(v) do
  41.       line = line .. e
  42.     end
  43.     mSave.writeLine(line)
  44.   end
  45.   mSave.close()
  46. end
  47.  
  48. function initializeMain()
  49.   term.setBackgroundColor(colors.lightBlue)
  50.   term.setTextColor(colors.white)
  51.   for i = 1, h do
  52.     term.setCursorPos(1, i)
  53.     term.write(staffline)
  54.   end
  55.   term.setBackgroundColor(colors.blue)
  56.   term.setCursorPos(w - 1, 1)
  57.   term.write("  ")
  58.   term.setBackgroundColor(colors.yellow)
  59.   term.setCursorPos(w - 1, 2)
  60.   term.write("  ")
  61.   term.setBackgroundColor(colors.white)
  62.   term.setCursorPos(w - 1, 3)
  63.   term.write("  ")
  64.   term.setBackgroundColor(colors.brown)
  65.   term.setCursorPos(w - 1, 4)
  66.   term.write("  ")
  67.   term.setBackgroundColor(colors.gray)
  68.   term.setCursorPos(w - 1, 5)
  69.   term.write("  ")
  70.   term.setBackgroundColor(colors.black)
  71.   term.setCursorPos(w - 1, 6)
  72.   term.write("XX")
  73. end
  74.  
  75. initializeMain()
  76. while true do
  77.   local flag = true
  78.   for i, v in pairs(song) do
  79.     if i > location and i <= location + h then
  80.       term.setCursorPos(1, i - location)
  81.       term.setBackgroundColor(colors.lightBlue)
  82.       term.write(staffline)
  83.       flag = true
  84.       for k, e in pairs(v) do
  85.         term.setCursorPos(i, k - location)
  86.         if e > 0 then
  87.           flag = false
  88.           if e == 1 then --note
  89.             term.setBackgroundColor(colors.blue)
  90.           elseif e == 2 then --kick
  91.             term.setBackgroundColor(colors.yellow)
  92.           elseif e == 3 then --click
  93.             term.setBackgroundColor(colors.white)
  94.           elseif e == 4 then --bass
  95.             term.setBackgroundColor(colors.brown)
  96.           elseif e == 5 then --snare
  97.             term.setBackgroundColor(colors.gray)
  98.           end
  99.           term.setCursorPos(k * 2, i - location)
  100.           term.write(" ")
  101.         end
  102.       end
  103.     end
  104.     if flag then --rest
  105.       term.setCursorPos(1, i - location)
  106.       term.setBackgroundColor(colors.black)
  107.       term.write(staffline)
  108.     end
  109.   end
  110.   if song then
  111.     for i = #song, 1, -1 do
  112.       local flag = false
  113.       for k, e in pairs(song[i]) do
  114.         if e ~= 0 then
  115.           flag = true
  116.           break
  117.         end
  118.       end
  119.       if flag == true then
  120.         break
  121.       else
  122.         song[i] = nil
  123.       end
  124.     end
  125.   end
  126.   term.setCursorPos(1, #song - location + 1)
  127.   term.setBackgroundColor(colors.lightBlue)
  128.   term.write(staffline)
  129.   term.setCursorPos(1, h)
  130.   term.setBackgroundColor(colors.blue)
  131.   term.clearLine()
  132.   term.setCursorPos(w - 1 - #tostring(location), h)
  133.   term.write(location)
  134.   term.setCursorPos(1, h)
  135.   term.write("CTRL for menu")
  136.   if notetype == 1 then --note
  137.     term.setBackgroundColor(colors.blue)
  138.   elseif notetype == 2 then --kick
  139.     term.setBackgroundColor(colors.yellow)
  140.   elseif notetype == 3 then --click
  141.     term.setBackgroundColor(colors.white)
  142.   elseif notetype == 4 then --bass
  143.     term.setBackgroundColor(colors.brown)
  144.   elseif notetype == 5 then --snare
  145.     term.setBackgroundColor(colors.gray)
  146.   elseif notetype == 6 then --rest
  147.     term.setBackgroundColor(colors.black)
  148.     term.setCursorPos(w - 1, h)
  149.     term.write("XX")
  150.   end
  151.   if notetype and notetype ~= 6 then
  152.     term.setCursorPos(w - 1, h)
  153.     term.write("  ")
  154.   end
  155.   events = {os.pullEvent()}
  156.   if events[1] == "mouse_scroll" then
  157.     local dir = math.abs(events[2]) / events[2]
  158.     location = location - dir
  159.     if location < 0 then location = 0 end
  160.   end
  161.   if events[1] == "mouse_click" and events[2] == 1 and events[3] >= w - 1 and events[4] <= 6 then
  162.     notetype = events[4]
  163.   end
  164.   if notetype and events[1] == "mouse_click" and events[2] == 1 and events[3] <= w - 2 and events[3] % 2 == 0 and events[4] < h then
  165.     for i = 1 + location, events[4] + location do
  166.       if song[i] == nil then
  167.         song[i] = {}
  168.         for n = 1, 24 do
  169.           song[i][n] = 0
  170.         end
  171.       end
  172.     end
  173.     if notetype ~= 6 then
  174.       song[events[4] + location][events[3] / 2] = notetype
  175.     else
  176.       song[events[4] + location] = {}
  177.       for i = 1, 24 do
  178.         song[events[4] + location][i] = 0
  179.       end
  180.     end
  181.   end
  182.   if events[1] == "key" and events[2] == 29 then
  183.     local broken = false
  184.     while true do
  185.       term.setTextColor(colors.white)
  186.       paintutils.drawFilledBox(1, h - 9, 6, h - 1, colors.blue)
  187.       term.setCursorPos(1, h - 9)
  188.       term.write("Tempo")
  189.       term.setCursorPos(4, h - 7)
  190.       term.write("npm")
  191.       term.setCursorPos(1, h - 4)
  192.       term.write("Save")
  193.       term.setCursorPos(1, h - 2)
  194.       term.write("Exit")
  195.       term.setCursorPos(1, h - 7)
  196.       term.setBackgroundColor(colors.white)
  197.       term.setTextColor(colors.black)
  198.       term.write(tempo)
  199.       term.setCursorPos(1, h - 8)
  200.       term.setBackgroundColor(colors.gray)
  201.       term.setTextColor(colors.white)
  202.       term.write("^")
  203.       term.setCursorPos(1, h - 6)
  204.       term.write("v")
  205.       local events = {os.pullEvent()}
  206.       if events[1] == "key" and events[2] == 29 then
  207.         events = nil
  208.         initializeMain()
  209.         break
  210.       end
  211.       if events[1] == "mouse_click" and events[2] == 1 and events[3] <= 4 then
  212.         if events[4] == h - 8 and events[3] == 1 and tempo <= 178 then
  213.           term.setCursorPos(1, h - 8)
  214.           term.setBackgroundColor(colors.white)
  215.           term.setTextColor(colors.black)
  216.           term.write("^")
  217.           tempo = tempo + 1
  218.           sleep(.1)
  219.         elseif events[4] == h - 6 and events[3] == 1 and tempo >= 22 then
  220.           term.setCursorPos(1, h - 6)
  221.           term.setBackgroundColor(colors.white)
  222.           term.setTextColor(colors.black)
  223.           term.write("v")
  224.           tempo = tempo - 1
  225.           sleep(.1)
  226.         elseif events[4] == h - 4 then
  227.           term.setBackgroundColor(colors.lightBlue)
  228.           term.setTextColor(colors.yellow)
  229.           term.setCursorPos(1, h - 4)
  230.           term.write("Save")
  231.           saveFile()
  232.           sleep(.2)
  233.         elseif events[4] == h - 2 then
  234.           term.setBackgroundColor(colors.lightBlue)
  235.           term.setTextColor(colors.yellow)
  236.           term.setCursorPos(1, h - 2)
  237.           term.write("Exit")
  238.           broken = true
  239.           sleep(.2)
  240.           term.setBackgroundColor(colors.black)
  241.           term.clear()
  242.           term.setCursorPos(1, 1)
  243.           break
  244.         end
  245.       end
  246.     end
  247.     if broken then break end
  248.   end
  249. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement