Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("nbs") -- load the API
- local function endsWith(input, endPattern)
- if input == nil or (not type(string) == string) then
- return false
- else
- local length = string.len(input)
- if endPattern == nil or (not type(endPattern) == string) then
- return false
- else
- local patternLength = string.len(endPattern)
- local inputEnd = string.sub(input, 0 - string.len(endPattern))
- if inputEnd == endPattern then
- return true
- else
- return false
- end
- end
- end
- end
- local function getSongNames(dirPath)
- if fs.isDir(dirPath) then
- files = fs.list(dirPath)
- songFiles = {}
- local i = 1
- for key, value in pairs(files) do
- if endsWith(value, ".nbs") then
- songFiles[i] = value
- i = i + 1
- end
- end
- return songFiles
- else
- return nil
- end
- end
- local function printSongLabel(monitor, text, maxLength, x, y)
- text = string.sub(text, 1, maxLength)
- for j = 0, 2, 1 do
- monitor.setCursorPos(x, y + j)
- for i = 1, maxLength + 2, 1 do
- monitor.write(" ")
- end
- end
- local shiftX = math.floor((maxLength - string.len(text)) / 2)
- monitor.setCursorPos(x + 1 + shiftX, y + 1)
- monitor.write(text)
- end
- local function getSelectedIdx(monitor, maxFileNameLength, pageNumber, x, y)
- if x == nil or y == nil then
- return nil
- end
- local sizeX, sizeY = monitor.getSize()
- local songsPerRow = math.floor(sizeX / (maxFileNameLength + 4))
- local songsPerColumn = math.floor(sizeY / 5)
- local songsPerPage = songsPerRow * songsPerColumn
- local shiftX = math.floor((sizeX - songsPerRow * (maxFileNameLength + 4)) / 2)
- local shiftY = math.floor((sizeY - songsPerColumn * 5) / 2)
- local col = nil
- local row = nil
- local xStart = nil
- for k = 1, songsPerRow, 1 do
- xStart = 2 + shiftX + (k-1) * (maxFileNameLength + 4)
- if x >= xStart and x <= xStart + maxFileNameLength + 2 then
- col = k
- end
- end
- local yStart = nil
- for k = 1, songsPerColumn, 1 do
- yStart = 2 + shiftY + (k - 1) * 5
- if y >= yStart and y <= yStart + 2 then
- row = k
- end
- end
- if col and row then
- local selectedIdx = (pageNumber - 1) * songsPerPage + (col - 1) * songsPerColumn + row
- return selectedIdx
- end
- return nil
- end
- local function updateMonitor(monitor, songFiles, maxFileNameLength, pageNumber, selectedIdx)
- monitor.clear()
- local sizeX, sizeY = monitor.getSize()
- local songsPerRow = math.floor(sizeX / (maxFileNameLength + 4))
- local songsPerColumn = math.floor(sizeY / 5)
- local songsPerPage = songsPerRow * songsPerColumn
- local firstSongIdx = 1
- if selectedIdx then
- firstSongIdx = math.floor(selectedIdx / songsPerPage) + 1
- end
- local x = 1
- local y = 1
- local shiftX = math.floor((sizeX - songsPerRow * (maxFileNameLength + 4)) / 2)
- local shiftY = math.floor((sizeY - songsPerColumn * 5) / 2)
- local defaultBgColor = colors.lightGray
- local selectedBgColor = colors.lime
- local k = 1
- for i = firstSongIdx, firstSongIdx + songsPerPage - 1, 1 do
- if i == selectedIdx then
- monitor.setBackgroundColor(selectedBgColor)
- else
- monitor.setBackgroundColor(defaultBgColor)
- end
- x = 2 + shiftX + math.floor((k - 1) / songsPerColumn) * (maxFileNameLength + 4)
- y = 2 + shiftY + ((k - 1) % songsPerColumn) * 5
- if songFiles[i] then
- printSongLabel(monitor, songFiles[i], maxFileNameLength, x, y)
- end
- k = k + 1
- end
- monitor.setBackgroundColor(colors.black)
- end
- local modemSide, monitorSide = ...
- rednet.open(modemSide)
- local monitor = peripheral.wrap(monitorSide)
- monitor.setTextScale(1)
- monitor.clear()
- local maxFileNameLength = 16
- local pageNumber = 1
- local selectedIdx = nil
- local selectedSong = nil
- local songFiles = getSongNames("music")
- updateMonitor(monitor, songFiles, maxFileNameLength, pageNumber)
- while true do
- event, par1, x, y = os.pullEvent("monitor_touch")
- selectedIdx = getSelectedIdx(monitor, maxFileNameLength, pageNumber, x, y)
- if selectedIdx then
- selectedSong = songFiles[selectedIdx]
- if selectedSong then
- updateMonitor(monitor, songFiles, maxFileNameLength, pageNumber, selectedIdx)
- nbs.play("music/"..selectedSong, modemSide, 3, true)
- updateMonitor(monitor, songFiles, maxFileNameLength, pageNumber)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement