From b8f1445f11e678401916d0217f89587ce8a33328 Mon Sep 17 00:00:00 2001 From: mint Date: Fri, 7 May 2021 02:19:01 -0400 Subject: [PATCH] WOAH THAT'S A LOT OF PROGRESS --- rhythmblock.lua | 136 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 106 insertions(+), 30 deletions(-) diff --git a/rhythmblock.lua b/rhythmblock.lua index 0ad1a6d..2666f7d 100644 --- a/rhythmblock.lua +++ b/rhythmblock.lua @@ -1,20 +1,20 @@ +-- 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) +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() -centerWidth = round(width / 2) -centerHeight = round(height / 2) -peripherals = peripheral.getNames() -playing = false +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" + os.exit() -- Exits if there's no disk drive else driveCount = 0 for n = 1, #peripherals do @@ -25,49 +25,125 @@ else end end if driveCount > 1 then - print("Too many disk drives. Specify where the disk drive is by running Rhythmblock with the argument") + 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 0.0.1a" invalidFormatStatus = "Not a music disc" noDiscStatus = "No disc" -notPlayingStatus ="No disc playing" -noDiscInDriveStatus = "No disc in drive" -status = defaultStatus +ejectText = "Eject" function playDisc() -if disk.isPresent(drive) and disk.hasAudio(drive) then + 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) - status = disk.getAudioTitle(drive) + renderStopButton() playing = true - elseif disk.isPresent(drive) then - status = "Not a music disc" - else - print("No disc") + status = disk.getAudioTitle(drive) + elseif disk.isPresent(drive) then -- If there's a floppy disk for example, Rhythmbox won't play it. + status = invalidFormatStatus + else -- If none of these checks are passed then it just means there's no disc in the drive. + status = noDiscStatus end end -function ejectDisc() - disk.eject() -end - -function stopDisc() +function ejectDisc() -- Ejects the disc! How cool is that? if playing == true then - disk.stopAudio() - playing = 0 - status = defaultStatus - else - status = "No disc is playing" - end + stopDisc() + disk.eject() + elseif disk.isPresent(drive) then -- If there's a disc, it'll be ejected. + disk.eject() + else -- If not it'll report there's no disc. + status = noDiscStatus + end end -function renderPlayButton() +function stopDisc() -- Stops the music + disk.stopAudio() + renderPlayButton() + playing = false + status = defaultStatus +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 -playDisc() + +function renderStopButton() -- Renders the Stop button + paintutils.drawFilledBox(centerWidth - 4, centerHeight - 4, centerWidth + 4, centerHeight + 2, accentColor) + paintutils.drawFilledBox(centerWidth - 2, centerHeight - 3, centerWidth + 1, centerHeight + 1, buttonColor) +end + +function renderEjectButton() -- Renders the Eject button + paintutils.drawLine(centerWidth - 2, centerHeight + 4, centerWidth + 2, centerHeight + 4, buttonColor) + term.setTextColor(buttonColor) + term.setCursorPos(centerWidth - 2, centerHeight + 4) + term.write(ejectText) + statusPos() +end + +function statusPos() -- Resets the status position +term.setCursorPos(1, 1) +term.setTextColor(textColor) +end + +function clickListen() + local event, button, x, y = os.pullEvent("mouse_up") + if button == 1 and x >= centerWidth - 4 and y >= centerHeight - 4 and x <= centerWidth + 4 and y <= centerHeight + 2 then + if playing == true then + stopDisc() + else + playDisc() + end + elseif button == 1 and x >= centerWidth - 2 and y >= centerHeight + 4 and x <= centerWidth + 2 and y <= centerHeight + 4 then + ejectDisc() + end +end + +function touchEvent() + local event, side, x, y = os.pullEvent("monitor_touch") + if x >= centerWidth - 4 and y >= centerHeight - 4 and x <= centerWidth + 4 and y <= centerHeight + 2 then + if playing == true then + stopDisc() + else + playDisc() + end + elseif x >= centerWidth - 2 and y >= centerHeight + 4 and x <= centerWidth + 2 and y <= centerHeight + 4 then + ejectDisc() + end +end + +function keyEvent() + local event, key = os.pullEvent("key_up") + local name = keys.getName(key) or "unknown key" + + if name == "space" then + if playing == true then + stopDisc() + else + playDisc() + end + elseif name == "q" then + os.exit() + end +end + +term.setBackgroundColor(backgroundColor) -- Setting the background color +renderPlayButton() -- Rendering the play button +renderEjectButton() -- Rendering the eject button +status = defaultStatus --Setting the +term.clear() +while true do + term.write(status) + spawn(clickListen) + spawn(touchListen) + spawn(keyListen) +end