zamkamec

Gui v3

Sep 24th, 2026
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. -- Pocket Computer Desktop Environment (26 x 20)
  2. local w, h = term.getSize()
  3.  
  4. -- System State
  5. local currentPage = 1
  6. local maxPages = 2
  7. local lastApp = nil
  8.  
  9. -- Define Desktop Apps (Distributed across pages)
  10. local allApps = {
  11. -- PAGE 1
  12. { page = 1, name = "Shell", color = colors.cyan, symbol = ">_", run = function() shell.run("shell") end },
  13. { page = 1, name = "Edit", color = colors.lime, symbol = "ED", run = function() shell.run("edit") end },
  14. { page = 1, name = "Files", color = colors.yellow, symbol = "LS", run = function() shell.run("list") sleep(2) end },
  15. { page = 1, name = "Clock", color = colors.orange, symbol = "12", run = function()
  16. term.setBackgroundColor(colors.black)
  17. term.clear()
  18. term.setCursorPos(2, 8)
  19. term.setTextColor(colors.yellow)
  20. term.write("Time: " .. textutils.formatTime(os.time(), true))
  21. sleep(2)
  22. end },
  23. { page = 1, name = "Off", color = colors.red, symbol = "OFF", run = function() os.shutdown() end },
  24.  
  25. -- PAGE 2
  26. { page = 2, name = "Chat", color = colors.purple, symbol = "MSG", run = function() print("Opening Chat...") sleep(1) end },
  27. { page = 2, name = "GPS", color = colors.blue, symbol = "GPS", run = function() print("Searching satellites...") sleep(1) end },
  28. { page = 2, name = "Lua", color = colors.magenta,symbol = "LUA", run = function() shell.run("lua") end }
  29. }
  30.  
  31. -- Forward declaration of functions
  32. local launchApp, openSettings
  33.  
  34. -- Bottom Bar Action Buttons
  35. local bottomButtons = {
  36. { label = " [<] ", x = 2, run = function()
  37. if lastApp then
  38. launchApp(lastApp)
  39. end
  40. end },
  41. { label = " [O] ", x = 11, run = function()
  42. currentPage = 1
  43. end },
  44. { label = " [*] ", x = 20, run = function()
  45. openSettings()
  46. end }
  47. }
  48.  
  49. -- Grid Layout Config for 3x3 per page
  50. local cols = 3
  51. local iconWidth = 5
  52. local iconHeight = 2
  53. local startX = 2
  54. local startY = 2
  55. local gapX = 3
  56. local gapY = 2
  57.  
  58. -- Pre-calculate icon grid positions based on their grid index on that page
  59. local pageCounts = {}
  60. for _, icon in ipairs(allApps) do
  61. local p = icon.page
  62. pageCounts[p] = (pageCounts[p] or 0) + 1
  63. local idx = pageCounts[p] - 1
  64.  
  65. local col = idx % cols
  66. local row = math.floor(idx / cols)
  67. icon.x = startX + col * (iconWidth + gapX)
  68. icon.y = startY + row * (iconHeight + gapY)
  69. end
  70.  
  71. -- Settings Sub-App
  72. function openSettings()
  73. term.setBackgroundColor(colors.gray)
  74. term.clear()
  75. term.setTextColor(colors.white)
  76.  
  77. term.setCursorPos(2, 2)
  78. term.write("--- Settings ---")
  79. term.setCursorPos(2, 4)
  80. term.write("ID: " .. os.getComputerID())
  81. term.setCursorPos(2, 5)
  82. term.write("Label: " .. (os.getComputerLabel() or "Unlabeled"))
  83. term.setCursorPos(2, 6)
  84. term.write("Modem: " .. (peripheral.find("modem") and "Online" or "None"))
  85.  
  86. term.setTextColor(colors.yellow)
  87. term.setCursorPos(2, 18)
  88. term.write("Tap anywhere to exit")
  89.  
  90. os.pullEvent("mouse_click")
  91. end
  92.  
  93. -- Helper to launch apps safely and track history
  94. function launchApp(icon)
  95. lastApp = icon
  96. term.setBackgroundColor(colors.black)
  97. term.setTextColor(colors.white)
  98. term.clear()
  99. term.setCursorPos(1, 1)
  100. icon.run()
  101. end
  102.  
  103. local function drawDesktop()
  104. term.setBackgroundColor(colors.black)
  105. term.clear()
  106.  
  107. -- 1. Draw App Grid for Current Page
  108. for _, icon in ipairs(allApps) do
  109. if icon.page == currentPage then
  110. -- Top row of 5x2 square icon
  111. term.setBackgroundColor(icon.color)
  112. term.setTextColor(colors.white)
  113. term.setCursorPos(icon.x, icon.y)
  114. term.write(string.format(" %-3s ", icon.symbol))
  115.  
  116. -- Bottom row of 5x2 square icon
  117. term.setCursorPos(icon.x, icon.y + 1)
  118. term.write(" ")
  119.  
  120. -- Icon Label
  121. term.setBackgroundColor(colors.black)
  122. term.setTextColor(colors.lightGray)
  123. term.setCursorPos(icon.x, icon.y + 2)
  124. term.write(string.sub(icon.name .. " ", 1, iconWidth))
  125. end
  126. end
  127.  
  128. -- 2. Draw Page Indicator Dots (above bottom bar, row 18)
  129. term.setBackgroundColor(colors.black)
  130. term.setTextColor(colors.gray)
  131. term.setCursorPos(11, 18)
  132. local pageDots = ""
  133. for p = 1, maxPages do
  134. if p == currentPage then
  135. pageDots = pageDots .. "o "
  136. else
  137. pageDots = pageDots .. ". "
  138. end
  139. end
  140. term.write(pageDots)
  141.  
  142. -- 3. Draw Bottom Navigation Bar
  143. term.setBackgroundColor(colors.gray)
  144. term.setTextColor(colors.white)
  145. term.setCursorPos(1, h)
  146. term.write(string.rep(" ", w))
  147.  
  148. for _, btn in ipairs(bottomButtons) do
  149. term.setCursorPos(btn.x, h)
  150. term.write(btn.label)
  151. end
  152. end
  153.  
  154. -- Main Event Loop
  155. while true do
  156. drawDesktop()
  157.  
  158. local event, p1, p2, p3 = os.pullEvent()
  159.  
  160. -- 1. Handle Mouse Clicks
  161. if event == "mouse_click" and p1 == 1 then
  162. local clickX, clickY = p2, p3
  163.  
  164. -- Check App Icons on current page
  165. for _, icon in ipairs(allApps) do
  166. if icon.page == currentPage then
  167. if clickX >= icon.x and clickX < (icon.x + iconWidth) and
  168. clickY >= icon.y and clickY <= (icon.y + 1) then
  169.  
  170. -- Tap Highlight Effect
  171. term.setBackgroundColor(colors.white)
  172. term.setTextColor(colors.black)
  173. term.setCursorPos(icon.x, icon.y)
  174. term.write(string.format(" %-3s ", icon.symbol))
  175. term.setCursorPos(icon.x, icon.y + 1)
  176. term.write(" ")
  177. sleep(0.08)
  178.  
  179. launchApp(icon)
  180. break
  181. end
  182. end
  183. end
  184.  
  185. -- Check Bottom Navigation Bar
  186. if clickY == h then
  187. for _, btn in ipairs(bottomButtons) do
  188. if clickX >= btn.x and clickX < (btn.x + #btn.label) then
  189. btn.run()
  190. break
  191. end
  192. end
  193. end
  194.  
  195. -- 2. Handle Mouse Scroll (Page Switching)
  196. elseif event == "mouse_scroll" then
  197. local direction = p1
  198. if direction > 0 and currentPage < maxPages then
  199. currentPage = currentPage + 1 -- Scroll Down -> Next Page
  200. elseif direction < 0 and currentPage > 1 then
  201. currentPage = currentPage - 1 -- Scroll Up -> Prev Page
  202. end
  203. end
  204. end
Advertisement
Add Comment
Please, Sign In to add comment