Advertisement
DOGGYWOOF

Untitled

Jan 15th, 2025 (edited)
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.47 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 draw an error screen
  110. local function drawErrorScreen(message)
  111. -- Create an error window with a red background
  112. local errorWindow = window.create(term.current(), math.floor(w / 2) - 12, math.floor(h / 2) - 5, 24, 7, false)
  113. errorWindow.setBackgroundColor(colors.red)
  114. errorWindow.clear()
  115.  
  116. -- Set up text color and positioning for the error message
  117. errorWindow.setTextColor(colors.white)
  118. errorWindow.setCursorPos(2, 2)
  119. errorWindow.write("ERROR: " .. message)
  120.  
  121. -- Provide an option for the user to close the error screen
  122. errorWindow.setCursorPos(2, 5)
  123. errorWindow.setTextColor(colors.yellow)
  124. errorWindow.write("Press any key to return.")
  125.  
  126. -- Wait for any key press to close the error window
  127. os.pullEvent("key")
  128. errorWindow.setVisible(false) -- Hide the error window after key press
  129. end
  130.  
  131. -- Check if the user is an admin (assuming .currentusr contains the current user)
  132. local function isAdminUser()
  133. local userFile = "/disk/users/" .. fs.readFile(".currentusr") .. "/admin.txt"
  134. return fs.exists(".currentusr")
  135. end
  136.  
  137. -- Function to run the program or action based on the selected option
  138. local function runProgramOrAction(option)
  139. -- Check if the user is allowed to run commands
  140. if option == 1 and not isAdminUser() then
  141. -- Show the error screen if the user is not an admin
  142. drawErrorScreen("You must be an admin to run this.")
  143. return true -- Prevent running the command
  144. end
  145.  
  146. -- Normal handling for power menu actions
  147. if isPowerMenuOpen then
  148. if option == 1 then
  149. shell.run("/disk/ACPI/shutdown") -- Shutdown
  150. elseif option == 2 then
  151. shell.run("/disk/ACPI/reboot") -- Reboot
  152. elseif option == 3 then
  153. shell.run("/disk/ACPI/logoff") -- Sign Out
  154. elseif option == 4 then
  155. shell.run("/disk/os/gui") -- Run GUI when Cancel is selected
  156. return true
  157. end
  158. return true -- Indicate an action was taken
  159. end
  160.  
  161. -- Handle running programs for the main menu
  162. if option == 1 then
  163. shell.run("/disk/os/home.lua")
  164. elseif option == 2 then
  165. shell.run("/disk/os/programs")
  166. end
  167.  
  168. return false -- Indicate no action was taken
  169. end
  170.  
  171. -- Function to redraw the entire UI
  172. local function redrawUI()
  173. term.setBackgroundColor(colors.blue)
  174. term.clear()
  175. drawTaskbar()
  176. drawASCIIArt() -- Draw ASCII art
  177. if isPowerMenuOpen then
  178. drawPowerMenu()
  179. else
  180. drawStartMenu()
  181. end
  182. end
  183.  
  184. -- Initial UI drawing
  185. redrawUI()
  186.  
  187. -- Start the timer for updating the time every second
  188. local timerId = os.startTimer(1)
  189.  
  190. -- Function to handle mouse clicks
  191. local function handleMouseClick(x, y)
  192. if y == h and x >= 1 and x <= 7 then
  193. isStartMenuOpen = not isStartMenuOpen
  194. isPowerMenuOpen = false
  195. redrawUI()
  196. elseif isStartMenuOpen and x >= 1 and x <= 20 and y >= h - 10 and y <= h - 1 then
  197. local clickedOption = y - (h - 10) - 1
  198. if clickedOption >= 1 and clickedOption <= #menuOptions then
  199. nOption = clickedOption
  200. if nOption == 3 then
  201. isStartMenuOpen = false
  202. isPowerMenuOpen = true
  203. redrawUI()
  204. else
  205. return true
  206. end
  207. end
  208. end
  209. return false
  210. end
  211.  
  212. -- Main event handling loop
  213. while true do
  214. local e, p1, p2, p3 = os.pullEvent()
  215.  
  216. if e == "mouse_click" then
  217. local button, x, y = p1, p2, p3
  218. if handleMouseClick(x, y) then
  219. runProgramOrAction(nOption) -- Execute action
  220. end
  221. elseif e == "key" and (isStartMenuOpen or isPowerMenuOpen) then
  222. local key = p1
  223.  
  224. if key == keys.up then
  225. if nOption > 1 then
  226. nOption = nOption - 1
  227. else
  228. nOption = #menuOptions
  229. end
  230. redrawUI()
  231. elseif key == keys.down then
  232. if isPowerMenuOpen then
  233. if nOption < #powerOptions then
  234. nOption = nOption + 1
  235. else
  236. nOption = 1 -- Wrap around to "Shutdown"
  237. end
  238. else
  239. if nOption < #menuOptions then
  240. nOption = nOption + 1
  241. else
  242. nOption = 1
  243. end
  244. end
  245. redrawUI()
  246. elseif key == keys.enter then
  247. if runProgramOrAction(nOption) then
  248. -- Do nothing, action taken
  249. else
  250. break -- Exit loop only if no action taken
  251. end
  252. end
  253. elseif e == "timer" and p1 == timerId then
  254. drawTaskbar() -- Redraw taskbar to update time
  255. timerId = os.startTimer(1) -- Reset the timer for the next second
  256. end
  257. end
  258.  
  259. -- Final screen settings before running the selected action
  260. term.setBackgroundColor(colors.black)
  261. term.setTextColor(colors.white)
  262. term.clear()
  263.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement