Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.pullEvent = os.pullEventRaw
- local w, h = term.getSize()
- local nOption = 1
- local isStartMenuOpen = false
- local isPowerMenuOpen = false
- local menuOptions = {"Command", "Programs", "Power Menu"}
- local powerOptions = {"Shutdown", "Restart", "Log Off", "Cancel"}
- -- Create windows for taskbar, start menu, and power menu
- local taskbar = window.create(term.current(), 1, h, w, 1)
- local startMenu = window.create(term.current(), 1, h - 10, 20, 10, false)
- local powerMenu = window.create(term.current(), math.floor(w / 2) - 12, math.floor(h / 2) - 7, 24, 15, false)
- -- Helper function to draw the taskbar
- local function drawTaskbar()
- taskbar.setBackgroundColor(colors.green) -- Green taskbar
- taskbar.clear()
- taskbar.setCursorPos(1, 1)
- taskbar.setTextColor(colors.white)
- taskbar.write("[ Start ]")
- end
- -- Helper function to draw the start menu
- local function drawStartMenu()
- if not isStartMenuOpen then
- startMenu.setVisible(false)
- return
- end
- startMenu.setVisible(true)
- startMenu.setBackgroundColor(colors.green) -- Dark green start menu
- startMenu.clear()
- -- Draw the start menu title
- startMenu.setTextColor(colors.white)
- startMenu.setCursorPos(2, 1)
- startMenu.write("=== Start Menu ===")
- -- Draw the options in the start menu
- for i, option in ipairs(menuOptions) do
- startMenu.setCursorPos(2, 2 + i)
- if nOption == i then
- startMenu.setTextColor(colors.black)
- startMenu.setBackgroundColor(colors.white)
- startMenu.write("[ " .. option .. " ]")
- startMenu.setBackgroundColor(colors.green)
- startMenu.setTextColor(colors.white)
- else
- startMenu.write(" " .. option .. " ")
- end
- end
- end
- -- Helper function to draw the power menu
- local function drawPowerMenu()
- if not isPowerMenuOpen then
- powerMenu.setVisible(false)
- return
- end
- powerMenu.setVisible(true)
- powerMenu.setBackgroundColor(colors.black) -- Black background for power menu
- powerMenu.clear()
- -- Draw the power menu title
- powerMenu.setTextColor(colors.white)
- powerMenu.setCursorPos(2, 1)
- powerMenu.write("=== Power Menu ===")
- -- Draw the options in the power menu
- for i, option in ipairs(powerOptions) do
- powerMenu.setCursorPos(2, 3 + i)
- if nOption == i then
- powerMenu.setTextColor(colors.black)
- powerMenu.setBackgroundColor(colors.white)
- powerMenu.write("[ " .. option .. " ]")
- powerMenu.setBackgroundColor(colors.black)
- powerMenu.setTextColor(colors.white)
- else
- powerMenu.write(" " .. option .. " ")
- end
- end
- end
- -- Function to redraw the entire UI
- local function redrawUI()
- term.setBackgroundColor(colors.blue) -- Blue desktop
- term.clear()
- drawTaskbar()
- if isPowerMenuOpen then
- drawPowerMenu()
- else
- drawStartMenu()
- end
- end
- -- Initial UI drawing
- redrawUI()
- -- Function to handle mouse clicks on the taskbar, start menu, and power menu
- local function handleMouseClick(x, y)
- if y == h and x >= 1 and x <= 7 then
- isStartMenuOpen = not isStartMenuOpen
- isPowerMenuOpen = false
- redrawUI()
- elseif isStartMenuOpen and x >= 1 and x <= 20 and y >= h - 10 and y <= h - 1 then
- local clickedOption = y - (h - 10) - 1
- if clickedOption >= 1 and clickedOption <= #menuOptions then
- nOption = clickedOption
- if nOption == 3 then -- "Power Menu" option opens the power menu
- isStartMenuOpen = false
- isPowerMenuOpen = true
- redrawUI()
- else
- return true
- end
- end
- elseif isPowerMenuOpen and x >= math.floor(w / 2) - 12 and x <= math.floor(w / 2) + 12 and y >= math.floor(h / 2) - 7 and y <= math.floor(h / 2) + 7 then
- local clickedOption = y - (math.floor(h / 2) - 7) - 1
- if clickedOption >= 1 and clickedOption <= #powerOptions then
- nOption = clickedOption
- return true
- end
- end
- return false
- end
- -- Function to manage running programs and power options
- local function runProgramOrAction(option)
- if isPowerMenuOpen then
- if option == 1 then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- shell.run("/disk/ACPI/shutdown") -- Shutdown
- elseif option == 2 then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- shell.run("/disk/ACPI/restart") -- Restart
- elseif option == 3 then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- shell.run("/disk/ACPI/logoff") -- Log off
- elseif option == 4 then
- -- Close the power menu and return to the desktop
- isPowerMenuOpen = false
- redrawUI()
- end
- return
- end
- -- Conditions based on the selected option
- if option == 1 then
- shell.run("/disk/os/home.lua")
- elseif option == 2 then
- shell.run("/disk/os/programs")
- end
- end
- -- Main event handling loop
- while true do
- local e, p1, p2, p3 = os.pullEvent()
- if e == "mouse_click" then
- local button, x, y = p1, p2, p3
- if handleMouseClick(x, y) then
- runProgramOrAction(nOption)
- if not isPowerMenuOpen then
- break
- end
- end
- elseif e == "key" and (isStartMenuOpen or isPowerMenuOpen) then
- local key = p1
- if key == keys.up then -- Up key
- if nOption > 1 then
- nOption = nOption - 1
- else
- nOption = #menuOptions
- end
- redrawUI()
- elseif key == keys.down then -- Down key
- if nOption < (#menuOptions or #powerOptions) then
- nOption = nOption + 1
- else
- nOption = 1
- end
- redrawUI()
- elseif key == keys.enter then -- Enter key
- runProgramOrAction(nOption)
- if not isPowerMenuOpen then
- break
- end
- end
- end
- end
- -- Final screen settings before running the selected action
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement