Advertisement
DOGGYWOOF

Disk Registrar touch screen

Jun 17th, 2024 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. local function drawButton(x, y, width, height, text)
  2. paintutils.drawFilledBox(x, y, x + width - 1, y + height - 1, colors.lightGray)
  3. local textX = x + math.floor((width - #text) / 2)
  4. local textY = y + math.floor(height / 2)
  5. term.setCursorPos(textX, textY)
  6. term.setTextColor(colors.black)
  7. term.write(text)
  8. end
  9.  
  10. local function drawExitButton()
  11. local width, height = term.getSize()
  12. term.setCursorPos(width, 1)
  13. term.setTextColor(colors.red)
  14. term.write("X")
  15. end
  16.  
  17. local function isInArea(x, y, xMin, yMin, xMax, yMax)
  18. return x >= xMin and x <= xMax and y >= yMin and y <= yMax
  19. end
  20.  
  21. local function getDiskID()
  22. local drive = peripheral.wrap("bottom")
  23. if drive and peripheral.getType("bottom") == "drive" then
  24. if drive.isDiskPresent() then
  25. local diskID = drive.getDiskID()
  26. return diskID
  27. else
  28. return nil, "No disk present in the drive."
  29. end
  30. else
  31. return nil, "No drive found at the bottom."
  32. end
  33. end
  34.  
  35. local function listCards()
  36. term.setCursorPos(1, 1)
  37. term.clear()
  38. drawExitButton()
  39. local cardDir = "/disk/Cards/"
  40. local files = fs.list(cardDir)
  41. if #files > 0 then
  42. print("Cards in /disk/Cards:")
  43. for _, file in ipairs(files) do
  44. print("- " .. file)
  45. end
  46. else
  47. print("No cards found in /disk/Cards.")
  48. end
  49.  
  50. while true do
  51. local event, button, x, y = os.pullEvent("mouse_click")
  52. local width, _ = term.getSize()
  53. if isInArea(x, y, width, 1, width, 1) then
  54. return
  55. end
  56. end
  57. end
  58.  
  59. local function linkCard()
  60. term.setCursorPos(1, 1)
  61. term.clear()
  62. drawExitButton()
  63. print("Link Card:")
  64. drawButton(2, 2, 12, 2, "Insert Card")
  65. drawButton(2, 5, 12, 2, "Enter ID")
  66. drawButton(2, 8, 12, 2, "Back")
  67.  
  68. while true do
  69. local event, button, x, y = os.pullEvent("mouse_click")
  70. local width, _ = term.getSize()
  71. if isInArea(x, y, 2, 2, 13, 3) then
  72. print("Insert a disk to link...")
  73. local diskID, errorMsg = getDiskID()
  74. if diskID then
  75. local cardFileName = "/disk/Cards/" .. diskID .. ".ID"
  76. local file = fs.open(cardFileName, "w")
  77. file.close()
  78. print("Card linked successfully.")
  79. else
  80. print(errorMsg)
  81. end
  82. elseif isInArea(x, y, 2, 5, 13, 6) then
  83. term.setCursorPos(1, 10)
  84. term.clearLine()
  85. print("Enter the card ID:")
  86. term.setCursorPos(1, 11)
  87. local cardID = read()
  88. local cardFileName = "/disk/Cards/" .. cardID .. ".ID"
  89. local file = fs.open(cardFileName, "w")
  90. file.close()
  91. print("Card linked successfully.")
  92. elseif isInArea(x, y, 2, 8, 13, 9) then
  93. return
  94. elseif isInArea(x, y, width, 1, width, 1) then
  95. return
  96. end
  97. end
  98. end
  99.  
  100. local function unlinkCard()
  101. term.setCursorPos(1, 1)
  102. term.clear()
  103. drawExitButton()
  104. print("Unlink Card:")
  105. drawButton(2, 2, 12, 2, "Insert Card")
  106. drawButton(2, 5, 12, 2, "Enter ID")
  107. drawButton(2, 8, 12, 2, "Back")
  108.  
  109. while true do
  110. local event, button, x, y = os.pullEvent("mouse_click")
  111. local width, _ = term.getSize()
  112. if isInArea(x, y, 2, 2, 13, 3) then
  113. print("Insert a disk to unlink...")
  114. local diskID, errorMsg = getDiskID()
  115. if diskID then
  116. local cardFileName = "/disk/Cards/" .. diskID .. ".ID"
  117. if fs.exists(cardFileName) then
  118. fs.delete(cardFileName)
  119. print("Card unlinked successfully.")
  120. else
  121. print("No matching card found to unlink.")
  122. end
  123. else
  124. print(errorMsg)
  125. end
  126. elseif isInArea(x, y, 2, 5, 13, 6) then
  127. term.setCursorPos(1, 10)
  128. term.clearLine()
  129. print("Enter the card ID:")
  130. term.setCursorPos(1, 11)
  131. local cardID = read()
  132. local cardFileName = "/disk/Cards/" .. cardID .. ".ID"
  133. if fs.exists(cardFileName) then
  134. fs.delete(cardFileName)
  135. print("Card unlinked successfully.")
  136. else
  137. print("No matching card found to unlink.")
  138. end
  139. elseif isInArea(x, y, 2, 8, 13, 9) then
  140. return
  141. elseif isInArea(x, y, width, 1, width, 1) then
  142. return
  143. end
  144. end
  145. end
  146.  
  147. local function mainMenu()
  148. term.clear()
  149. drawExitButton()
  150. drawButton(2, 2, 12, 2, "List Cards")
  151. drawButton(2, 5, 12, 2, "Link Card")
  152. drawButton(2, 8, 12, 2, "Unlink Card")
  153. drawButton(2, 11, 12, 2, "Exit")
  154.  
  155. while true do
  156. local event, button, x, y = os.pullEvent("mouse_click")
  157. local width, _ = term.getSize()
  158. if isInArea(x, y, 2, 2, 13, 3) then
  159. listCards()
  160. term.clear()
  161. mainMenu()
  162. elseif isInArea(x, y, 2, 5, 13, 6) then
  163. linkCard()
  164. term.clear()
  165. mainMenu()
  166. elseif isInArea(x, y, 2, 8, 13, 9) then
  167. unlinkCard()
  168. term.clear()
  169. mainMenu()
  170. elseif isInArea(x, y, 2, 11, 13, 12) then
  171. print("Exiting...")
  172. break
  173. elseif isInArea(x, y, width, 1, width, 1) then
  174. term.clear()
  175. mainMenu()
  176. end
  177. end
  178. end
  179.  
  180. mainMenu()
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement