Barnet

player_monitor.lua

Jun 9th, 2021 (edited)
108
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- TODO much needed cleanup
  2.  
  3. local playermonitor = {}
  4.  
  5. local noteinfo = dofile("noteinfo.lua")
  6.  
  7. -- make it better
  8. local indexedColors = {}
  9. do
  10.   local seen = {} -- treat colors.gray and colors.grey as the same
  11.   local i = 0
  12.   for k,v in pairs(colors) do
  13.     if not seen[v] and type(v) == "number" then
  14.       i = i + 1
  15.       indexedColors[i] = v
  16.       seen[v] = true
  17.     end
  18.   end
  19. end
  20. table.sort(indexedColors)
  21. local maxColor = #indexedColors
  22.  
  23. local function monitor_print(monitor, str)
  24.   local old = term.redirect(monitor) -- make print() use the monitor (monitors don't have a print())
  25.   print(str .. "") -- do the print()
  26.   term.redirect(old) -- restore
  27. end
  28.  
  29. playermonitor.new = function(monitor, id, subid, settings)
  30.   return function()
  31.     local hasColor, title = settings.hasColor, settings.title
  32.  
  33.     monitor.setTextScale(0.5)
  34.  
  35.     if not hasColor then
  36.       monitor.setTextColor(colors.white)
  37.       monitor.setBackgroundColor(colors.black)
  38.     end
  39.     monitor.clear()
  40.     monitor.setCursorPos(1,1)
  41.  
  42.     local fgColor, bgColor = 1, 2
  43.  
  44.     local notesPlayed, row = 0, 0
  45.  
  46.     local sizeX, sizeY = monitor.getSize()
  47.     local halfSizeX = math.ceil(sizeX / 2) + 1
  48.  
  49.     local header = "Now playing: " .. title
  50.     local headerXPos = halfSizeX - math.ceil(#header / 2)
  51.  
  52.     local played, played2 = false, true
  53.  
  54.     while true do
  55.       local evt, p1_id, p2_subid, p3_inst_or_state, p4_note, p5_volume = os.pullEvent()
  56.       if id == p1_id and subid == p2_subid then
  57.         if evt == "player:slave" then
  58.  
  59.           if played2 then
  60.             played2 = false
  61.  
  62.             sizeX, sizeY = monitor.getSize()
  63.             halfSizeX = math.ceil(sizeX / 2) + 1
  64.  
  65.             header = "Now playing: " .. title
  66.             headerXPos = halfSizeX - math.ceil(#header / 2)
  67.  
  68.             hasColor, title = settings.hasColor, settings.title
  69.  
  70.             if hasColor then
  71.               bgColor = bgColor % maxColor + 1
  72.               while bgColor == fgColor or indexedColors[bgColor] == colors.white do
  73.                 bgColor = bgColor % maxColor + 1
  74.               end
  75.               monitor.setBackgroundColor(indexedColors[bgColor])
  76.             else
  77.               monitor.setBackgroundColor(colors.black)
  78.             end
  79.  
  80.             -- we want to be quick, so precalculate everything
  81.             local str = settings.footerRight
  82.             local str2 = settings.footerLeft
  83.             local strpos = sizeX - #str
  84.             local sizeYm1 = sizeY - 1
  85.             local completed = math.floor(row / settings.length * 100)
  86.             local np = string.format("%d notes played (%d%% completed)", notesPlayed, completed)
  87.             local combinedFooter = string.sub(str2, 1, math.max(1, strpos)) .. string.rep(" ", math.max(0, strpos - #str2)) .. str
  88.  
  89.             monitor.setCursorPos(math.max(1, headerXPos), 1)
  90.             monitor.clear()
  91.             monitor_print(monitor, header)
  92.             local posX,posY = monitor.getCursorPos()
  93.             monitor.setCursorPos(1, sizeYm1)
  94.             monitor.write(np)
  95.             monitor.setCursorPos(1, sizeY)
  96.             monitor.write(combinedFooter)
  97.             monitor.setCursorPos(posX, posY)
  98.           end
  99.  
  100.  
  101.           local inst, note = p3_inst_or_state, p4_note
  102.  
  103.           -- notes
  104.           local str = noteinfo.instnames[inst+1] .. " " .. noteinfo.notenames[note+1]
  105.           local _,pos = monitor.getCursorPos()
  106.           monitor.setCursorPos(halfSizeX - math.ceil(string.len(str) / 2), pos)
  107.  
  108.           if hasColor then
  109.             fgColor = fgColor % maxColor + 1
  110.             if fgColor == bgColor then fgColor = fgColor % maxColor + 1 end
  111.             monitor.setTextColor(indexedColors[fgColor] + 0)
  112.           end
  113.  
  114.           monitor_print(monitor, str)
  115.  
  116.           if hasColor then
  117.             monitor.setTextColor(1)
  118.           end
  119.  
  120.           notesPlayed = notesPlayed + 1
  121.  
  122.           played = true
  123.  
  124.         elseif evt == "player:monitor" and p3_inst_or_state == "pre" then
  125.           if played then
  126.             played = false
  127.             played2 = true
  128.           end
  129.         elseif evt == "player:monitor" and p3_inst_or_state == "post" then
  130.  
  131.           -- done after playing (print notes played, etc)
  132.           row = row + 1
  133.           local str = settings.footerRight
  134.           local str2 = settings.footerLeft
  135.           local strpos = sizeX - #str
  136.           local sizeYm1 = sizeY - 1
  137.           local completed = math.floor(row / settings.length * 100)
  138.           local np = string.format("%d notes played (%d%% completed)", notesPlayed, completed)
  139.           local combinedFooter = string.sub(str2, 1, math.max(1, strpos)) .. string.rep(" ", math.max(0, strpos - #str2)) .. str
  140.  
  141.           local posX,posY = monitor.getCursorPos()
  142.           monitor.setCursorPos(1, sizeYm1)
  143.           monitor.write(np)
  144.           monitor.setCursorPos(1, sizeY)
  145.           monitor.write(combinedFooter)
  146.           monitor.setCursorPos(posX, posY)
  147.  
  148.         end
  149.       end
  150.     end
  151.   end
  152. end
  153.  
  154. return playermonitor
RAW Paste Data