GrooveypenguinX

test GUI

Jan 5th, 2025 (edited)
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. -- WirelessMonitor.lua
  2. -- Version 1.5.0
  3. -- Author: Modified by AshGrey
  4. -- Date: 2024-10-16
  5.  
  6. -- This script displays missing items or status updates from the AE2 Warehouse on a pocket computer, regular computer, or monitor.
  7. -- It uses a custom GUI with buttons and scrollable sections.
  8.  
  9. -------------------------------------------------------------------------------
  10. -- INITIALIZATION
  11. -------------------------------------------------------------------------------
  12.  
  13. local function init()
  14. term.clear()
  15. term.setCursorPos(1, 1)
  16. print("Initializing Wireless Monitor...")
  17.  
  18. local modem = peripheral.find("modem")
  19. if not modem then error("Ender Modem not found.") end
  20. print("Ender Modem initialized.")
  21.  
  22. local monitor = peripheral.find("monitor")
  23. local display = term -- Default display to terminal
  24.  
  25. if monitor then
  26. monitor.setTextScale(0.75) -- Adjust text scale for better readability
  27. display = monitor
  28. print("Monitor detected. Output will be displayed on the monitor.")
  29. else
  30. print("No monitor detected. Output will be displayed on the terminal.")
  31. end
  32.  
  33. return modem, display
  34. end
  35.  
  36. local modem, display = init()
  37. local channel = 1
  38. modem.open(channel)
  39.  
  40. -------------------------------------------------------------------------------
  41. -- FUNCTIONS
  42. -------------------------------------------------------------------------------
  43.  
  44. -- Strips unwanted prefixes from strings
  45. local function removePrefixes(str)
  46. return str:gsub("minecraft:", ""):gsub("minecolonies:", ""):gsub("domum_ornamentum:", ""):gsub("Missing items:", "")
  47. end
  48.  
  49. -- Basic GUI drawing helpers
  50. function pos(...) return term.setCursorPos(...) end
  51. function cls(...) return term.clear() end
  52. function tCol(...) return term.setTextColor(...) end
  53. function bCol(...) return term.setBackgroundColor(...) end
  54. function box(...) return paintutils.drawFilledBox(...) end
  55. function line(...) return paintutils.drawLine(...) end
  56.  
  57. local x, y = term.getSize()
  58.  
  59. -- Draws the main display UI with missing items or status update.
  60. function drawDisplay()
  61. cls()
  62. box(1, 1, x, y, colors.lightBlue) -- Background
  63. box(2, 2, x - 1, y - 1, colors.white) -- Content Background
  64.  
  65. tCol(colors.black)
  66. bCol(colors.lightBlue)
  67. pos(2, 2)
  68. write("Wireless Monitor")
  69. bCol(colors.white)
  70. end
  71.  
  72. -- Handles the item information section
  73. function drawItemSection(name, displayName, blocks, missingCount, startY)
  74. local sectionHeight = 4
  75.  
  76. box(2, startY, x - 1, startY + sectionHeight - 1, colors.gray)
  77. line(2, startY, x - 1, startY, colors.lightGray)
  78. tCol(colors.black)
  79. pos(3, startY + 1)
  80. write(displayName or name)
  81. pos(3, startY + 2)
  82. write("Type: " .. name)
  83. pos(3, startY + 3)
  84. write("Missing: " .. missingCount)
  85.  
  86. if blocks then
  87. local blockList = {}
  88. for block in blocks:gmatch("([^,]+)") do
  89. table.insert(blockList, block)
  90. end
  91. for i, block in ipairs(blockList) do
  92. pos(3, startY + 3 + i)
  93. write(" " .. block)
  94. end
  95. end
  96. end
  97.  
  98.  
  99. -- Wraps text for monitors
  100. local function wrapText(text, width)
  101. local wrappedText = {}
  102. for line in text:gmatch("[^\n]+") do
  103. while #line > width do
  104. local segment = line:sub(1, width):match("^(.-)%s") or line:sub(1, width)
  105. table.insert(wrappedText, segment)
  106. line = line:sub(#segment + 1):match("^%s*(.*)") or ""
  107. end
  108. if #line > 0 then
  109. table.insert(wrappedText, line)
  110. end
  111. end
  112. return wrappedText
  113. end
  114.  
  115. -- Scroll logic
  116. local scrollIndex = 1
  117. -- Handles scrollable section drawing with dynamic scrolling
  118. function drawScrollableItems(lines)
  119. local lineHeight = 3
  120. local sectionY = 6
  121. local visibleLines = math.floor((y - sectionY) / lineHeight) -- Number of visible lines
  122.  
  123. -- Draw only the visible lines, based on scrollIndex
  124. for i = scrollIndex, math.min(scrollIndex + visibleLines - 1, #lines) do
  125. pos(3, sectionY + (i - scrollIndex) * lineHeight)
  126. write(lines[i])
  127. end
  128. end
  129.  
  130. -- Scroll logic (ensure you don't go out of bounds)
  131. local function scrollUp()
  132. scrollIndex = math.max(1, scrollIndex - 1)
  133. end
  134.  
  135. local function scrollDown()
  136. scrollIndex = scrollIndex + 1
  137. end
  138.  
  139. -- Process item sections with scrollable content
  140. local function processItemSection(name, displayName, blocks, missingCount)
  141. local lines = { (displayName or "") .. "\n", "Type: " .. name, "Missing: " .. missingCount }
  142.  
  143. if blocks then
  144. local blockList = {}
  145. for block in blocks:gmatch("([^,]+)") do
  146. table.insert(blockList, block)
  147. end
  148. -- Swap block order for "shingle" items
  149. if name:lower():find("shingle") or (displayName and displayName:lower():find("shingle")) then
  150. if #blockList >= 2 then
  151. table.insert(lines, " Shingle: " .. blockList[2])
  152. table.insert(lines, " Support: " .. blockList[1])
  153. else
  154. for _, block in ipairs(blockList) do
  155. table.insert(lines, " " .. block)
  156. end
  157. end
  158. else
  159. -- Normal block display
  160. for _, block in ipairs(blockList) do
  161. table.insert(lines, " " .. block)
  162. end
  163. end
  164. end
  165.  
  166. return lines
  167. end
  168.  
  169. -- Displays missing items (with scrollable content)
  170. local function displayMissingItems(display, message)
  171. local lines = { "Missing Items Report:" }
  172. for itemSection in message:gmatch("([^\n]+:.-Missing Count: %d+)") do
  173. local name = itemSection:match("^([^:]+):")
  174. local displayName = itemSection:match("Display Name: ([^\n]+)")
  175. local blocks = itemSection:match("Blocks Needed: ([^\n]+)")
  176. local missingCount = itemSection:match("Missing Count: (%d+)")
  177. local sectionLines = processItemSection(name, displayName, blocks, missingCount)
  178. for _, line in ipairs(sectionLines) do
  179. table.insert(lines, line)
  180. end
  181. end
  182. drawDisplay()
  183. drawScrollableItems(lines)
  184. end
  185.  
  186. -- Displays non-item status updates
  187. local function displayStatusUpdate(display, message)
  188. local lines = wrapText("Status Update:\n" .. message, display.getSize())
  189. drawDisplay()
  190. drawScrollableItems(lines)
  191. end
  192.  
  193.  
  194. -------------------------------------------------------------------------------
  195. -- MAIN LOOP
  196. -------------------------------------------------------------------------------
  197.  
  198. print("Wireless Monitor is now running...")
  199. while true do
  200. local event, _, receivedChannel, _, message = os.pullEvent("modem_message")
  201. if receivedChannel == channel then
  202. message = removePrefixes(message)
  203.  
  204. if message:find("Missing Count:") then
  205. displayMissingItems(display, message)
  206. else
  207. displayStatusUpdate(display, message)
  208. end
  209.  
  210. local event, button, mx, my = os.pullEvent()
  211. if event == "mouse_click" then
  212. -- Scroll up/down logic based on mouse position (simplified for now)
  213. if my <= y - 2 then
  214. if mx >= x - 2 then
  215. scrollDown() -- Click to scroll down
  216. elseif mx <= 2 then
  217. scrollUp() -- Click to scroll up
  218. end
  219. end
  220. end
  221. end
  222. end
  223.  
Advertisement
Add Comment
Please, Sign In to add comment