Rhythmblock/rhythmblock.lua

180 lines
6.1 KiB
Lua
Raw Normal View History

2021-05-07 06:19:01 +00:00
-- Change these lines to change the GUI colors.
accentColor = colors.gray
buttonColor = colors.lightGray
textColor = colors.lightGray
altTextColor = colors.gray
backgroundColor = colors.black
-- Code starts from here
2021-05-07 06:19:01 +00:00
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
if arg[1] == nil then
peripherals = peripheral.getNames() -- Gets peripherals to check if any disk drives are avavailable
2021-05-07 07:45:42 +00:00
if #peripherals == 0 then
2021-05-07 07:51:40 +00:00
term.setTextColor(colors.red)
print "No drive"
2021-05-07 07:45:42 +00:00
return false -- Exits if there's no disk drive
2021-05-07 07:36:40 +00:00
else
driveCount = 0
for n = 1, #peripherals do
local driveCheck = peripherals[n]
if peripheral.getType(driveCheck) == "drive" then
drive = driveCheck
driveCount = driveCount + 1
2021-05-07 07:36:40 +00:00
end
end
if driveCount > 1 then
2021-05-07 07:51:40 +00:00
term.setTextColor(colors.red)
print("Too many disk drives. Specify where the desired disk drive is by running 'rhythmblock [drive position]'") -- For safety reasons
return false
2021-05-07 07:47:27 +00:00
elseif driveCount == 0 then
2021-05-07 07:51:40 +00:00
term.setTextColor(colors.red)
print "No drive"
2021-05-07 07:47:27 +00:00
return false -- Exits if there's no disk drive
2021-05-06 14:22:19 +00:00
end
end
2021-05-07 07:36:40 +00:00
else
drive = arg[1]
2021-05-06 14:22:19 +00:00
end
2021-05-07 06:19:01 +00:00
--[[ 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"
invalidFormatStatus = "Not a music disc"
noDiscStatus = "No disc"
noAudioStatus = "No audio is playing"
ejectText = "Eject"
2021-05-06 14:22:19 +00:00
function playDisc()
2021-05-07 07:06:51 +00:00
term.clear()
2021-05-07 06:19:01 +00:00
if disk.isPresent(drive) and disk.hasAudio(drive) then -- Determines if there's a disc or not and if it's a music disc
2021-05-06 14:22:19 +00:00
disk.playAudio(drive)
buttonRender(true)
playing = true
status = disk.getAudioTitle(drive)
buttonRender(true)
2021-05-07 08:05:41 +00:00
elseif disk.isPresent(drive) then
status = invalidFormatStatus
buttonRender(false)
2021-05-07 06:19:01 +00:00
else -- If none of these checks are passed then it just means there's no disc in the drive.
status = noDiscStatus
buttonRender(false)
2021-05-06 14:22:19 +00:00
end
end
2021-05-07 06:19:01 +00:00
function ejectDisc() -- Ejects the disc! How cool is that?
term.clear()
buttonRender(false)
if playing then
2021-05-07 06:19:01 +00:00
stopDisc()
disk.eject()
elseif disk.isPresent(drive) then -- If there's a disc, it'll be ejected.
2021-05-07 06:52:13 +00:00
disk.eject(drive)
2021-05-07 06:19:01 +00:00
else -- If not it'll report there's no disc.
status = noDiscStatus
2021-05-07 06:19:01 +00:00
end
2021-05-06 14:22:19 +00:00
end
2021-05-07 06:19:01 +00:00
function stopDisc() -- Stops the music
playing = false
status = defaultStatus
2021-05-07 07:09:10 +00:00
disk.stopAudio(drive)
2021-05-07 07:06:51 +00:00
term.clear()
buttonRender(false)
2021-05-06 14:22:19 +00:00
end
2021-05-07 06:19:01 +00:00
function renderPlayButton() -- Renders the Play button
paintutils.drawFilledBox(centerWidth - 4, centerHeight - 4, centerWidth + 4, centerHeight + 2, accentColor)
2021-05-07 04:48:14 +00:00
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
2021-05-07 06:19:01 +00:00
function renderStopButton() -- Renders the Stop button
paintutils.drawFilledBox(centerWidth - 4, centerHeight - 4, centerWidth + 4, centerHeight + 2, accentColor)
2021-05-07 06:32:24 +00:00
paintutils.drawFilledBox(centerWidth - 2, centerHeight - 3, centerWidth + 2, centerHeight + 1, buttonColor)
2021-05-07 06:19:01 +00:00
end
function renderEjectButton() -- Renders the Eject button
2021-05-07 06:44:08 +00:00
paintutils.drawLine(centerWidth - 2, centerHeight + 4, centerWidth + 2, centerHeight + 4, accentColor)
2021-05-07 06:19:01 +00:00
term.setTextColor(buttonColor)
term.setCursorPos(centerWidth - 2, centerHeight + 4)
term.write(ejectText)
end
function renderCloseButton() -- Renders the Close button
term.setCursorPos(width, 1)
term.setTextColor(textColor)
term.setBackgroundColor(backgroundColor)
term.write("x")
end
function buttonRender(play) -- Render some buttons depending if the computer is advanced or not
if play then
renderStopButton()
else
renderPlayButton()
end
if term.isColour() then
renderEjectButton()
renderCloseButton()
end
end
function statusRender(s) -- Renders the status bar
2021-05-07 06:56:46 +00:00
term.setCursorPos(1, 1)
term.setTextColor(textColor)
term.setBackgroundColor(backgroundColor)
term.write(s)
2021-05-07 06:19:01 +00:00
end
2021-05-07 06:36:21 +00:00
term.clear()
2021-05-07 06:32:24 +00:00
term.setBackgroundColor(backgroundColor) -- Setting the background color
2021-05-07 08:03:42 +00:00
buttonRender(false)
status = defaultStatus -- Setting the value to the default
2021-05-07 06:19:01 +00:00
2021-05-07 06:32:24 +00:00
while true do
2021-05-07 08:27:15 +00:00
statusRender(status) -- Renders the status
2021-05-07 06:32:24 +00:00
local eventData = {os.pullEvent()}
local event = eventData[1]
2021-05-07 06:44:08 +00:00
2021-05-07 08:27:15 +00:00
if event == "mouse_up" then -- If the event triggered is the mouse clicking it'll do what's next
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 the user clicks on the play/stop button it'll begin playing the disc
if playing then
2021-05-07 06:19:01 +00:00
stopDisc()
else
playDisc()
end
2021-05-07 08:27:15 +00:00
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 -- If the user clicks on the eject button it'll eject the disc
2021-05-07 06:19:01 +00:00
ejectDisc()
2021-05-07 08:27:15 +00:00
elseif eventData[2] == 1 and eventData[3] == width and eventData[4] == 1 then -- If the user presses the X it'll quit
term.clear()
term.setCursorPos(1, 1)
return true
end
2021-05-07 08:27:15 +00:00
elseif event == "key_up" then -- Same order but now with keys
2021-05-07 06:32:24 +00:00
local name = keys.getName(eventData[2]) or "unknown key"
if name == "space" then
if playing then
2021-05-07 06:32:24 +00:00
stopDisc()
else
playDisc()
end
2021-05-07 07:07:51 +00:00
elseif name == "e" then
2021-05-07 07:06:51 +00:00
ejectDisc()
2021-05-07 06:32:24 +00:00
elseif name == "q" then
2021-05-07 06:56:46 +00:00
term.clear()
term.setCursorPos(1, 1)
2021-05-07 06:40:18 +00:00
return true
2021-05-07 06:32:24 +00:00
end
end
2021-05-07 06:19:01 +00:00
end