1
0
Fork 0
mirror of https://fem.mint.lgbt/m/Rhythmblock.git synced 2024-08-14 20:27:11 +00:00
Rhythmblock/rhythmblock.lua
2021-05-07 03:06:51 -04:00

158 lines
5.5 KiB
Lua

-- Change these lines to change the GUI colors.
accentColor = colors.gray
buttonColor = colors.lightGray
textColor = colors.lightGray
altTextColor = colors.gray
backgroundColor = colors.black
function round(n) -- We need a function to round the center of the terminal
return n % 1 >= 0.5 and math.ceil(n) or math.floor(n)
end
width, height = term.getSize() -- Gets the terminal size to determine the center
centerWidth = round(width / 2) -- Defines the horizontal center
centerHeight = round(height / 2) -- Defines the vertical center
peripherals = peripheral.getNames() -- Gets peripherals to check if any disk drives are avavailable
if #peripherals < 0 then
print "No drive"
return true -- Exits if there's no disk drive
else
driveCount = 0
for n = 1, #peripherals do
local driveCheck = peripherals[n]
if peripheral.getType(driveCheck) == "drive" then
drive = driveCheck
driveCount = driveCount + 1
end
end
if driveCount > 1 then
print("Too many disk drives. Specify where the disk drive is by running rhythmbox [drive position]") -- For safety reasons
os.exit()
end
end
--[[ Instead of calling every status by string, we call the variables storing the strings. This may ease translation if we plan to do it in the future.
]]
defaultStatus = "Rhythmblock 1.0"
invalidFormatStatus = "Not a music disc"
noDiscStatus = "No disc"
noAudioStatus = "No audio is playing"
ejectText = "Eject"
function playDisc()
term.clear()
if disk.isPresent(drive) and disk.hasAudio(drive) then -- Determines if there's a disc or not and if it's a music disc
disk.playAudio(drive)
renderStopButton()
renderEjectButton()
playing = true
status = disk.getAudioTitle(drive)
elseif disk.isPresent(drive) then -- If there's a floppy disk for example, Rhythmbox won't play it.
status = invalidFormatStatus
renderPlayButton()
renderEjectButton()
else -- If none of these checks are passed then it just means there's no disc in the drive.
status = noDiscStatus
renderPlayButton()
renderEjectButton()
end
end
function ejectDisc() -- Ejects the disc! How cool is that?
renderPlayButton()
renderEjectButton()
if playing == true then
stopDisc()
disk.eject()
elseif disk.isPresent(drive) then -- If there's a disc, it'll be ejected.
disk.eject(drive)
else -- If not it'll report there's no disc.
status = noDiscStatus
end
end
function stopDisc() -- Stops the music
playing = false
status = defaultStatus
term.clear()
renderPlayButton()
renderEjectButton()
end
function renderPlayButton() -- Renders the Play button
paintutils.drawFilledBox(centerWidth - 4, centerHeight - 4, centerWidth + 4, centerHeight + 2, accentColor)
paintutils.drawFilledBox(centerWidth - 2, centerHeight - 3, centerWidth - 1, centerHeight + 1, buttonColor)
paintutils.drawFilledBox(centerWidth, centerHeight - 2, centerWidth + 1, centerHeight, buttonColor)
paintutils.drawPixel(centerWidth + 2, centerHeight - 1, buttonColor)
end
function renderStopButton() -- Renders the Stop button
paintutils.drawFilledBox(centerWidth - 4, centerHeight - 4, centerWidth + 4, centerHeight + 2, accentColor)
paintutils.drawFilledBox(centerWidth - 2, centerHeight - 3, centerWidth + 2, centerHeight + 1, buttonColor)
end
function renderEjectButton() -- Renders the Eject button
paintutils.drawLine(centerWidth - 2, centerHeight + 4, centerWidth + 2, centerHeight + 4, accentColor)
term.setTextColor(buttonColor)
term.setCursorPos(centerWidth - 2, centerHeight + 4)
term.write(ejectText)
end
function statusRender(s) -- Resets the status position
term.setCursorPos(1, 1)
term.setTextColor(textColor)
term.setBackgroundColor(backgroundColor)
term.write(s)
end
term.clear()
term.setBackgroundColor(backgroundColor) -- Setting the background color
renderPlayButton() -- Rendering the play button
renderEjectButton() -- Rendering the eject button
status = defaultStatus -- Setting the value to the default
while true do
statusRender(status)
local eventData = {os.pullEvent()}
local event = eventData[1]
if event == "mouse_up" then
if eventData[2] == 1 and eventData[3] >= centerWidth - 4 and eventData[4] >= centerHeight - 4 and eventData[3] <= centerWidth + 4 and eventData[4] <= centerHeight + 2 then
if playing == true then
stopDisc()
else
playDisc()
end
elseif eventData[2] == 1 and eventData[3] >= centerWidth - 2 and eventData[4] >= centerHeight + 4 and eventData[3] <= centerWidth + 2 and eventData[4] <= centerHeight + 4 then
term.clear()
ejectDisc()
end
elseif event == "monitor_touch" then
if eventData[3] >= centerWidth - 4 and eventData[4] >= centerHeight - 4 and eventData[3] <= centerWidth + 4 and eventData[4] <= centerHeight + 2 then
if playing == true then
stopDisc()
else
playDisc()
end
elseif eventData[3] >= centerWidth - 2 and eventData[4] >= centerHeight + 4 and eventData[3] <= centerWidth + 2 and eventData[4] <= centerHeight + 4 then
term.clear()
ejectDisc()
end
elseif event == "key_up" then
local name = keys.getName(eventData[2]) or "unknown key"
if name == "space" then
if playing == true then
stopDisc()
else
playDisc()
end
if name == "e" then
ejectDisc()
elseif name == "q" then
term.clear()
term.setCursorPos(1, 1)
return true
end
end
end