From 42d99a0a26bed0675c102039728aa3af5f701361 Mon Sep 17 00:00:00 2001 From: mint Date: Fri, 7 May 2021 02:32:24 -0400 Subject: [PATCH] Change the event listening thing --- rhythmblock.lua | 83 ++++++++++++++++++++++--------------------------- 1 file changed, 37 insertions(+), 46 deletions(-) diff --git a/rhythmblock.lua b/rhythmblock.lua index 651241e..ae347d0 100644 --- a/rhythmblock.lua +++ b/rhythmblock.lua @@ -79,7 +79,7 @@ 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 + 1, centerHeight + 1, buttonColor) + paintutils.drawFilledBox(centerWidth - 2, centerHeight - 3, centerWidth + 2, centerHeight + 1, buttonColor) end function renderEjectButton() -- Renders the Eject button @@ -95,55 +95,46 @@ 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) - clickListen() - touchListen() - keyListen() + local eventData = {os.pullEvent()} + local event = eventData[1] + + if event == "mouse_up" then + if eventData[2] == 1 and x >= eventData[3] - 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 + ejectDisc() + end + elseif event == "monitor_touch" then + if eventData[4] >= centerWidth - 4 and eventData[5] >= centerHeight - 4 and eventData[4] <= centerWidth + 4 and eventData[5] <= centerHeight + 2 then + if playing == true then + stopDisc() + else + playDisc() + end + elseif eventData[4] >= centerWidth - 2 and eventData[5] >= centerHeight + 4 and eventData[4] <= centerWidth + 2 and eventData[5] <= centerHeight + 4 then + 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 + elseif name == "q" then + os.exit() + end + end end