Advertisement
DOGGYWOOF

Untitled

Jan 3rd, 2025 (edited)
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. os.pullEvent = os.pullEventRaw
  2.  
  3. local w, h = term.getSize()
  4. local nOption = 1
  5. local isStartMenuOpen = false
  6. local isPowerMenuOpen = false
  7. local menuOptions = {"Command", "Programs", "Power Menu"}
  8. local powerOptions = {"Shutdown", "Reboot", "Sign Out", "Cancel"}
  9.  
  10. -- Create windows for taskbar, start menu, and power menu
  11. local taskbar = window.create(term.current(), 1, h, w, 1)
  12. local startMenu = window.create(term.current(), 1, h - 10, 20, 10, false)
  13. local powerMenu = window.create(term.current(), math.floor(w / 2) - 12, math.floor(h / 2) - 7, 24, 15, false)
  14.  
  15. -- Helper function to draw the taskbar with the current time
  16. local function drawTaskbar()
  17. taskbar.setBackgroundColor(colors.green)
  18. taskbar.clear()
  19. taskbar.setCursorPos(1, 1)
  20. taskbar.setTextColor(colors.white)
  21. taskbar.write("[ Start ]")
  22.  
  23. -- Display the current time on the right side
  24. local currentTime = os.date("%H:%M:%S")
  25. local timeX = w - string.len(currentTime) - 1
  26. taskbar.setCursorPos(timeX, 1)
  27. taskbar.write(currentTime)
  28. end
  29.  
  30. -- Helper function to draw the start menu
  31. local function drawStartMenu()
  32. if not isStartMenuOpen then
  33. startMenu.setVisible(false)
  34. return
  35. end
  36.  
  37. startMenu.setVisible(true)
  38. startMenu.setBackgroundColor(colors.green)
  39. startMenu.clear()
  40.  
  41. startMenu.setTextColor(colors.white)
  42. startMenu.setCursorPos(2, 1)
  43. startMenu.write("=== Start Menu ===")
  44.  
  45. for i, option in ipairs(menuOptions) do
  46. startMenu.setCursorPos(2, 2 + i)
  47. if nOption == i then
  48. startMenu.setTextColor(colors.black)
  49. startMenu.setBackgroundColor(colors.white)
  50. startMenu.write("[ " .. option .. " ]")
  51. startMenu.setBackgroundColor(colors.green)
  52. startMenu.setTextColor(colors.white)
  53. else
  54. startMenu.write("[ " .. option .. " ]")
  55. end
  56. end
  57. end
  58.  
  59. -- Helper function to draw the power menu
  60. local function drawPowerMenu()
  61. if not isPowerMenuOpen then
  62. powerMenu.setVisible(false)
  63. return
  64. end
  65.  
  66. powerMenu.setVisible(true)
  67. powerMenu.setBackgroundColor(colors.black)
  68. powerMenu.clear()
  69.  
  70. powerMenu.setTextColor(colors.white)
  71. powerMenu.setCursorPos(2, 1)
  72. powerMenu.write("=== Power Menu ===")
  73.  
  74. for i, option in ipairs(powerOptions) do
  75. powerMenu.setCursorPos(2, 3 + i)
  76. if nOption == i then
  77. powerMenu.setTextColor(colors.black)
  78. powerMenu.setBackgroundColor(colors.white)
  79. powerMenu.write("[ " .. option .. " ]")
  80. powerMenu.setBackgroundColor(colors.black)
  81. powerMenu.setTextColor(colors.white)
  82. else
  83. powerMenu.write("[ " .. option .. " ]")
  84. end
  85. end
  86. end
  87.  
  88. -- Helper function to draw ASCII art in the center
  89. local function drawASCIIArt()
  90. local art = {
  91. " |\\_/| ",
  92. " | @ @ Woof! ",
  93. " | <> _ ",
  94. " | _/\\------____ ((| |))",
  95. " | `--' | ",
  96. " ____|_ ___| |___.' ",
  97. "/_/_____/____/_______| "
  98. }
  99. local startX = math.floor((w - 26) / 2) -- Centering the art horizontally
  100. local startY = math.floor((h - #art) / 2) -- Centering the art vertically
  101.  
  102. term.setTextColor(colors.white)
  103. for i, line in ipairs(art) do
  104. term.setCursorPos(startX, startY + i - 1)
  105. term.write(line)
  106. end
  107. end
  108.  
  109. -- Function to redraw the entire UI
  110. local function redrawUI()
  111. term.setBackgroundColor(colors.blue)
  112. term.clear()
  113. drawTaskbar()
  114. drawASCIIArt() -- Draw ASCII art
  115. if isPowerMenuOpen then
  116. drawPowerMenu()
  117. else
  118. drawStartMenu()
  119. end
  120. end
  121.  
  122. -- Function to display an error message in the GUI window
  123. local function showError(message)
  124. local errorWindow = window.create(term.current(), math.floor(w / 2) - 15, math.floor(h / 2) - 5, 30, 10, true)
  125.  
  126. errorWindow.setBackgroundColor(colors.red)
  127. errorWindow.setTextColor(colors.white)
  128. errorWindow.clear()
  129.  
  130. -- Title
  131. errorWindow.setCursorPos(2, 2)
  132. errorWindow.write("=== ERROR ===")
  133.  
  134. -- Message
  135. local lines = {}
  136. for line in message:gmatch("[^\n]+") do
  137. table.insert(lines, line)
  138. end
  139.  
  140. for i, line in ipairs(lines) do
  141. errorWindow.setCursorPos(2, 3 + i)
  142. errorWindow.write(line:sub(1, 28)) -- Limit line length to fit the box
  143. end
  144.  
  145. -- Close instruction
  146. errorWindow.setCursorPos(2, 9)
  147. errorWindow.write("Press any key to continue...")
  148.  
  149. -- Wait for user input
  150. os.pullEvent("key")
  151. redrawUI() -- Restore the UI after closing the error message
  152. end
  153.  
  154. -- Function to handle running programs and power options
  155. local function runProgramOrAction(option)
  156. term.setBackgroundColor(colors.black)
  157. term.setTextColor(colors.white)
  158. term.clear()
  159.  
  160. local success, err
  161. if isPowerMenuOpen then
  162. if option == 1 then
  163. success, err = pcall(shell.run, "/disk/ACPI/shutdown")
  164. elseif option == 2 then
  165. success, err = pcall(shell.run, "/disk/ACPI/reboot")
  166. elseif option == 3 then
  167. success, err = pcall(shell.run, "/disk/ACPI/logoff")
  168. elseif option == 4 then
  169. shell.run("/disk/os/gui")
  170. return true
  171. end
  172. else
  173. if option == 1 then
  174. success, err = pcall(shell.run, "/disk/os/command.lua")
  175. elseif option == 2 then
  176. success, err = pcall(shell.run, "/disk/os/programs.lua")
  177. end
  178. end
  179.  
  180. if not success then
  181. showError("An error occurred:\n" .. (err or "Unknown error"))
  182. return false
  183. end
  184.  
  185. return true
  186. end
  187.  
  188. -- Function to handle running applications from the Start Menu
  189. local function runAppFromStartMenu(option)
  190. local success, err
  191. if option == 1 then
  192. success, err = pcall(shell.run, "/disk/os/command.lua")
  193. elseif option == 2 then
  194. success, err = pcall(shell.run, "/disk/os/programs.lua")
  195. end
  196.  
  197. if not success then
  198. showError("An error occurred when running the app:\n" .. (err or "Unknown error"))
  199. end
  200. end
  201.  
  202. -- Initial UI drawing
  203. redrawUI()
  204.  
  205. -- Start the timer for updating the time every second
  206. local timerId = os.startTimer(1)
  207.  
  208. -- Function to handle mouse clicks
  209. local function handleMouseClick(x, y)
  210. if y == h and x >= 1 and x <= 7 then
  211. isStartMenuOpen = not isStartMenuOpen
  212. isPowerMenuOpen = false
  213. redrawUI()
  214. elseif isStartMenuOpen and x >= 1 and x <= 20 and y >= h - 10 and y <= h - 1 then
  215. local clickedOption = y - (h - 10) - 1
  216. if clickedOption >= 1 and clickedOption <= #menuOptions then
  217. nOption = clickedOption
  218. if nOption == 3 then
  219. isStartMenuOpen = false
  220. isPowerMenuOpen = true
  221. redrawUI()
  222. else
  223. runAppFromStartMenu(nOption) -- Run selected app with error handling
  224. end
  225. end
  226. end
  227. return false
  228. end
  229.  
  230. -- Start listening for events and handle termination event
  231. parallel.waitForAny(function()
  232. while true do
  233. local e, p1, p2, p3 = os.pullEventRaw()
  234.  
  235. if e == "mouse_click" then
  236. local button, x, y = p1, p2, p3
  237. handleMouseClick(x, y) -- Handle mouse clicks for menu options
  238. elseif e == "key" and (isStartMenuOpen or isPowerMenuOpen) then
  239. local key = p1
  240.  
  241. if key == keys.up then
  242. if nOption > 1 then
  243. nOption = nOption - 1
  244. else
  245. nOption = #menuOptions
  246. end
  247. redrawUI()
  248. elseif key == keys.down then
  249. if isPowerMenuOpen then
  250. if nOption < #powerOptions then
  251. nOption = nOption + 1
  252. else
  253. nOption = 1 -- Wrap around to "Shutdown"
  254. end
  255. else
  256. if nOption < #menuOptions then
  257. nOption = nOption + 1
  258. else
  259. nOption = 1
  260. end
  261. end
  262. redrawUI()
  263. elseif key == keys.enter then
  264. if runProgramOrAction(nOption) then
  265. -- Do nothing, action taken
  266. else
  267. break -- Exit loop only if no action taken
  268. end
  269. end
  270. elseif e == "timer" and p1 == timerId then
  271. drawTaskbar() -- Redraw taskbar to update time
  272. timerId = os.startTimer(1) -- Reset the timer for the next second
  273. elseif e == "terminate" then
  274. showError("Access Denied") -- Show termination message in GUI
  275. end
  276. end
  277. end, function()
  278. while true do
  279. local event = os.pullEventRaw("terminate")
  280. if event == "terminate" then
  281. showError("Access Denied") -- Show termination message in GUI
  282. end
  283. end
  284. end)
  285.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement