Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Pocket Computer Desktop Environment (26 x 20)
- local w, h = term.getSize()
- -- System State
- local currentPage = 1
- local maxPages = 2
- local lastApp = nil
- -- Define Desktop Apps (Distributed across pages)
- local allApps = {
- -- PAGE 1
- { page = 1, name = "Shell", color = colors.cyan, symbol = ">_", run = function() shell.run("shell") end },
- { page = 1, name = "Edit", color = colors.lime, symbol = "ED", run = function() shell.run("edit") end },
- { page = 1, name = "Files", color = colors.yellow, symbol = "LS", run = function() shell.run("list") sleep(2) end },
- { page = 1, name = "Clock", color = colors.orange, symbol = "12", run = function()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(2, 8)
- term.setTextColor(colors.yellow)
- term.write("Time: " .. textutils.formatTime(os.time(), true))
- sleep(2)
- end },
- { page = 1, name = "Off", color = colors.red, symbol = "OFF", run = function() os.shutdown() end },
- -- PAGE 2
- { page = 2, name = "Chat", color = colors.purple, symbol = "MSG", run = function() print("Opening Chat...") sleep(1) end },
- { page = 2, name = "GPS", color = colors.blue, symbol = "GPS", run = function() print("Searching satellites...") sleep(1) end },
- { page = 2, name = "Lua", color = colors.magenta,symbol = "LUA", run = function() shell.run("lua") end }
- }
- -- Forward declaration of functions
- local launchApp, openSettings
- -- Bottom Bar Action Buttons
- local bottomButtons = {
- { label = " [<] ", x = 2, run = function()
- if lastApp then
- launchApp(lastApp)
- end
- end },
- { label = " [O] ", x = 11, run = function()
- currentPage = 1
- end },
- { label = " [*] ", x = 20, run = function()
- openSettings()
- end }
- }
- -- Grid Layout Config for 3x3 per page
- local cols = 3
- local iconWidth = 5
- local iconHeight = 2
- local startX = 2
- local startY = 2
- local gapX = 3
- local gapY = 2
- -- Pre-calculate icon grid positions based on their grid index on that page
- local pageCounts = {}
- for _, icon in ipairs(allApps) do
- local p = icon.page
- pageCounts[p] = (pageCounts[p] or 0) + 1
- local idx = pageCounts[p] - 1
- local col = idx % cols
- local row = math.floor(idx / cols)
- icon.x = startX + col * (iconWidth + gapX)
- icon.y = startY + row * (iconHeight + gapY)
- end
- -- Settings Sub-App
- function openSettings()
- term.setBackgroundColor(colors.gray)
- term.clear()
- term.setTextColor(colors.white)
- term.setCursorPos(2, 2)
- term.write("--- Settings ---")
- term.setCursorPos(2, 4)
- term.write("ID: " .. os.getComputerID())
- term.setCursorPos(2, 5)
- term.write("Label: " .. (os.getComputerLabel() or "Unlabeled"))
- term.setCursorPos(2, 6)
- term.write("Modem: " .. (peripheral.find("modem") and "Online" or "None"))
- term.setTextColor(colors.yellow)
- term.setCursorPos(2, 18)
- term.write("Tap anywhere to exit")
- os.pullEvent("mouse_click")
- end
- -- Helper to launch apps safely and track history
- function launchApp(icon)
- lastApp = icon
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
- icon.run()
- end
- local function drawDesktop()
- term.setBackgroundColor(colors.black)
- term.clear()
- -- 1. Draw App Grid for Current Page
- for _, icon in ipairs(allApps) do
- if icon.page == currentPage then
- -- Top row of 5x2 square icon
- term.setBackgroundColor(icon.color)
- term.setTextColor(colors.white)
- term.setCursorPos(icon.x, icon.y)
- term.write(string.format(" %-3s ", icon.symbol))
- -- Bottom row of 5x2 square icon
- term.setCursorPos(icon.x, icon.y + 1)
- term.write(" ")
- -- Icon Label
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.lightGray)
- term.setCursorPos(icon.x, icon.y + 2)
- term.write(string.sub(icon.name .. " ", 1, iconWidth))
- end
- end
- -- 2. Draw Page Indicator Dots (above bottom bar, row 18)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.gray)
- term.setCursorPos(11, 18)
- local pageDots = ""
- for p = 1, maxPages do
- if p == currentPage then
- pageDots = pageDots .. "o "
- else
- pageDots = pageDots .. ". "
- end
- end
- term.write(pageDots)
- -- 3. Draw Bottom Navigation Bar
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.setCursorPos(1, h)
- term.write(string.rep(" ", w))
- for _, btn in ipairs(bottomButtons) do
- term.setCursorPos(btn.x, h)
- term.write(btn.label)
- end
- end
- -- Main Event Loop
- while true do
- drawDesktop()
- local event, p1, p2, p3 = os.pullEvent()
- -- 1. Handle Mouse Clicks
- if event == "mouse_click" and p1 == 1 then
- local clickX, clickY = p2, p3
- -- Check App Icons on current page
- for _, icon in ipairs(allApps) do
- if icon.page == currentPage then
- if clickX >= icon.x and clickX < (icon.x + iconWidth) and
- clickY >= icon.y and clickY <= (icon.y + 1) then
- -- Tap Highlight Effect
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(icon.x, icon.y)
- term.write(string.format(" %-3s ", icon.symbol))
- term.setCursorPos(icon.x, icon.y + 1)
- term.write(" ")
- sleep(0.08)
- launchApp(icon)
- break
- end
- end
- end
- -- Check Bottom Navigation Bar
- if clickY == h then
- for _, btn in ipairs(bottomButtons) do
- if clickX >= btn.x and clickX < (btn.x + #btn.label) then
- btn.run()
- break
- end
- end
- end
- -- 2. Handle Mouse Scroll (Page Switching)
- elseif event == "mouse_scroll" then
- local direction = p1
- if direction > 0 and currentPage < maxPages then
- currentPage = currentPage + 1 -- Scroll Down -> Next Page
- elseif direction < 0 and currentPage > 1 then
- currentPage = currentPage - 1 -- Scroll Up -> Prev Page
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment