Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function drawButton(x, y, width, height, text, textColor, bgColor)
- paintutils.drawFilledBox(x, y, x + width - 1, y + height - 1, bgColor or colors.lightGray)
- local textX = x + math.floor((width - #text) / 2)
- local textY = y + math.floor(height / 2)
- term.setCursorPos(textX, textY)
- term.setTextColor(textColor or colors.black)
- term.write(text)
- term.setTextColor(colors.white) -- Reset to default color
- end
- local function drawExitButton()
- local width, _ = term.getSize()
- term.setCursorPos(width - 1, 1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.write("X")
- term.setBackgroundColor(colors.black)
- end
- local function isInArea(x, y, xMin, yMin, xMax, yMax)
- return x >= xMin and x <= xMax and y >= yMin and y <= yMax
- end
- local function getDiskID()
- local drive = peripheral.wrap("bottom")
- if drive and peripheral.getType("bottom") == "drive" then
- if drive.isDiskPresent() then
- local diskID = drive.getDiskID()
- return diskID
- else
- return nil, "No disk present in the drive."
- end
- else
- return nil, "No drive found at the bottom."
- end
- end
- local function listCards()
- term.setCursorPos(1, 1)
- term.clear()
- drawExitButton()
- local cardDir = "/disk/Cards/"
- local files = fs.list(cardDir)
- if #files > 0 then
- print("Cards in /disk/Cards:")
- for _, file in ipairs(files) do
- print("- " .. file)
- end
- else
- print("No cards found in /disk/Cards.")
- end
- while true do
- local event, button, x, y = os.pullEvent("mouse_click")
- local width, _ = term.getSize()
- if isInArea(x, y, width - 1, 1, width, 1) then
- return
- end
- end
- end
- local function waitForDiskInsertion(cancelCallback)
- term.setCursorPos(1, 1)
- term.clear()
- drawExitButton()
- print("Waiting for disk insertion...")
- drawButton(2, 8, 12, 2, "Cancel", colors.white, colors.red)
- while true do
- local event, button, x, y = os.pullEvent("mouse_click")
- local width, _ = term.getSize()
- if isInArea(x, y, width - 1, 1, width, 1) or isInArea(x, y, 2, 8, 13, 9) then
- cancelCallback()
- return nil, "Cancelled by user."
- end
- local diskID, errorMsg = getDiskID()
- if diskID then
- return diskID, nil
- end
- sleep(1) -- Wait for a short period before checking again
- end
- end
- local function linkCard()
- term.setCursorPos(1, 1)
- term.clear()
- drawExitButton()
- print("Link Card:")
- drawButton(2, 2, 12, 2, "Insert Card", colors.black)
- drawButton(2, 5, 12, 2, "Enter ID", colors.black)
- drawButton(2, 8, 12, 2, "Back", colors.white, colors.red)
- while true do
- local event, button, x, y = os.pullEvent("mouse_click")
- local width, _ = term.getSize()
- if isInArea(x, y, 2, 2, 13, 3) then
- local diskID, errorMsg = waitForDiskInsertion(linkCard)
- if diskID then
- local cardFileName = "/disk/Cards/" .. diskID .. ".ID"
- local file = fs.open(cardFileName, "w")
- file.close()
- print("Card linked successfully.")
- sleep(2)
- -- Eject the disk after linking the card
- local drive = peripheral.wrap("bottom")
- drive.ejectDisk()
- return
- else
- print(errorMsg)
- sleep(2)
- return
- end
- elseif isInArea(x, y, 2, 5, 13, 6) then
- term.setCursorPos(1, 10)
- term.clearLine()
- print("Enter the card ID:")
- term.setCursorPos(1, 11)
- local cardID = read()
- local cardFileName = "/disk/Cards/" .. cardID .. ".ID"
- local file = fs.open(cardFileName, "w")
- file.close()
- print("Card linked successfully.")
- sleep(2)
- return
- elseif isInArea(x, y, 2, 8, 13, 9) then
- return
- elseif isInArea(x, y, width - 1, 1, width, 1) then
- return
- end
- end
- end
- local function unlinkCard()
- term.setCursorPos(1, 1)
- term.clear()
- drawExitButton()
- print("Unlink Card:")
- drawButton(2, 2, 12, 2, "Insert Card", colors.black)
- drawButton(2, 5, 12, 2, "Enter ID", colors.black)
- drawButton(2, 8, 12, 2, "Back", colors.white, colors.red)
- while true do
- local event, button, x, y = os.pullEvent("mouse_click")
- local width, _ = term.getSize()
- if isInArea(x, y, 2, 2, 13, 3) then
- local diskID, errorMsg = waitForDiskInsertion(unlinkCard)
- if diskID then
- local cardFileName = "/disk/Cards/" .. diskID .. ".ID"
- if fs.exists(cardFileName) then
- fs.delete(cardFileName)
- print("Card unlinked successfully.")
- else
- print("No matching card found to unlink.")
- end
- sleep(2)
- -- Eject the disk after unlinking the card
- local drive = peripheral.wrap("bottom")
- drive.ejectDisk()
- return
- else
- print(errorMsg)
- sleep(2)
- return
- end
- elseif isInArea(x, y, 2, 5, 13, 6) then
- term.setCursorPos(1, 10)
- term.clearLine()
- print("Enter the card ID:")
- term.setCursorPos(1, 11)
- local cardID = read()
- local cardFileName = "/disk/Cards/" .. cardID .. ".ID"
- if fs.exists(cardFileName) then
- fs.delete(cardFileName)
- print("Card unlinked successfully.")
- else
- print("No matching card found to unlink.")
- end
- sleep(2)
- return
- elseif isInArea(x, y, 2, 8, 13, 9) then
- return
- elseif isInArea(x, y, width - 1, 1, width, 1) then
- return
- end
- end
- end
- local function mainMenu()
- term.clear()
- drawExitButton()
- drawButton(2, 2, 12, 2, "List Cards", colors.black)
- drawButton(2, 5, 12, 2, "Link Card", colors.black)
- drawButton(2, 8, 12, 2, "Unlink Card", colors.black)
- drawButton(2, 11, 12, 2, "Exit", colors.white, colors.red)
- while true do
- local event, button, x, y = os.pullEvent("mouse_click")
- local width, _ = term.getSize()
- if isInArea(x, y, 2, 2, 13, 3) then
- listCards()
- term.clear()
- mainMenu()
- elseif isInArea(x, y, 2, 5, 13, 6) then
- linkCard()
- term.clear()
- mainMenu()
- elseif isInArea(x, y, 2, 8, 13, 9) then
- unlinkCard()
- term.clear()
- mainMenu()
- elseif isInArea(x, y, 2, 11, 13, 12) then
- os.reboot()
- elseif isInArea(x, y, width - 1, 1, width, 1) then
- term.clear()
- mainMenu()
- end
- end
- end
- mainMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement