Barnet

player_monitor.lua

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