Advertisement
DOGGYWOOF

Doggy OS New desktop

Sep 23rd, 2024 (edited)
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 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
  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. end
  23.  
  24. -- Helper function to draw the start menu
  25. local function drawStartMenu()
  26. if not isStartMenuOpen then
  27. startMenu.setVisible(false)
  28. return
  29. end
  30.  
  31. startMenu.setVisible(true)
  32. startMenu.setBackgroundColor(colors.green)
  33. startMenu.clear()
  34.  
  35. startMenu.setTextColor(colors.white)
  36. startMenu.setCursorPos(2, 1)
  37. startMenu.write("=== Start Menu ===")
  38.  
  39. for i, option in ipairs(menuOptions) do
  40. startMenu.setCursorPos(2, 2 + i)
  41. if nOption == i then
  42. startMenu.setTextColor(colors.black)
  43. startMenu.setBackgroundColor(colors.white)
  44. startMenu.write("[ " .. option .. " ]")
  45. startMenu.setBackgroundColor(colors.green)
  46. startMenu.setTextColor(colors.white)
  47. else
  48. startMenu.write("[ " .. option .. " ]")
  49. end
  50. end
  51. end
  52.  
  53. -- Helper function to draw the power menu
  54. local function drawPowerMenu()
  55. if not isPowerMenuOpen then
  56. powerMenu.setVisible(false)
  57. return
  58. end
  59.  
  60. powerMenu.setVisible(true)
  61. powerMenu.setBackgroundColor(colors.black)
  62. powerMenu.clear()
  63.  
  64. powerMenu.setTextColor(colors.white)
  65. powerMenu.setCursorPos(2, 1)
  66. powerMenu.write("=== Power Menu ===")
  67.  
  68. for i, option in ipairs(powerOptions) do
  69. powerMenu.setCursorPos(2, 3 + i)
  70. if nOption == i then
  71. powerMenu.setTextColor(colors.black)
  72. powerMenu.setBackgroundColor(colors.white)
  73. powerMenu.write("[ " .. option .. " ]")
  74. powerMenu.setBackgroundColor(colors.black)
  75. powerMenu.setTextColor(colors.white)
  76. else
  77. powerMenu.write("[ " .. option .. " ]")
  78. end
  79. end
  80. end
  81.  
  82. -- Helper function to draw ASCII art in the center
  83. local function drawASCIIArt()
  84. local art = {
  85. " |\\_/| ",
  86. " | @ @ Woof! ",
  87. " | <> _ ",
  88. " | _/\\------____ ((| |))",
  89. " | `--' | ",
  90. " ____|_ ___| |___.' ",
  91. "/_/_____/____/_______| "
  92. }
  93. local startX = math.floor((w - 26) / 2) -- Centering the art horizontally
  94. local startY = math.floor((h - #art) / 2) -- Centering the art vertically
  95.  
  96. term.setTextColor(colors.white)
  97. for i, line in ipairs(art) do
  98. term.setCursorPos(startX, startY + i - 1)
  99. term.write(line)
  100. end
  101. end
  102.  
  103. -- Function to redraw the entire UI
  104. local function redrawUI()
  105. term.setBackgroundColor(colors.blue)
  106. term.clear()
  107. drawTaskbar()
  108. drawASCIIArt() -- Draw ASCII art
  109. if isPowerMenuOpen then
  110. drawPowerMenu()
  111. else
  112. drawStartMenu()
  113. end
  114. end
  115.  
  116. -- Initial UI drawing
  117. redrawUI()
  118.  
  119. -- Function to handle mouse clicks
  120. local function handleMouseClick(x, y)
  121. if y == h and x >= 1 and x <= 7 then
  122. isStartMenuOpen = not isStartMenuOpen
  123. isPowerMenuOpen = false
  124. redrawUI()
  125. elseif isStartMenuOpen and x >= 1 and x <= 20 and y >= h - 10 and y <= h - 1 then
  126. local clickedOption = y - (h - 10) - 1
  127. if clickedOption >= 1 and clickedOption <= #menuOptions then
  128. nOption = clickedOption
  129. if nOption == 3 then
  130. isStartMenuOpen = false
  131. isPowerMenuOpen = true
  132. redrawUI()
  133. else
  134. return true
  135. end
  136. end
  137. end
  138. return false
  139. end
  140.  
  141. -- Function to manage running programs and power options
  142. local function runProgramOrAction(option)
  143. if isPowerMenuOpen then
  144. if option == 1 then
  145. shell.run("/disk/ACPI/shutdown") -- Shutdown
  146. elseif option == 2 then
  147. shell.run("/disk/ACPI/reboot") -- Reboot
  148. elseif option == 3 then
  149. shell.run("/disk/ACPI/logoff") -- Sign Out
  150. elseif option == 4 then
  151. shell.run("/disk/os/gui") -- Run GUI when Cancel is selected
  152. return true
  153. end
  154. return true -- Indicate an action was taken
  155. end
  156.  
  157. if option == 1 then
  158. shell.run("/disk/os/home.lua")
  159. elseif option == 2 then
  160. shell.run("/disk/os/programs")
  161. end
  162.  
  163. return false -- Indicate no action was taken
  164. end
  165.  
  166. -- Main event handling loop
  167. while true do
  168. local e, p1, p2, p3 = os.pullEvent()
  169.  
  170. if e == "mouse_click" then
  171. local button, x, y = p1, p2, p3
  172. if handleMouseClick(x, y) then
  173. runProgramOrAction(nOption) -- Execute action
  174. end
  175. elseif e == "key" and (isStartMenuOpen or isPowerMenuOpen) then
  176. local key = p1
  177.  
  178. if key == keys.up then
  179. if nOption > 1 then
  180. nOption = nOption - 1
  181. else
  182. nOption = #menuOptions
  183. end
  184. redrawUI()
  185. elseif key == keys.down then
  186. if isPowerMenuOpen then
  187. if nOption < #powerOptions then
  188. nOption = nOption + 1
  189. else
  190. nOption = 1 -- Wrap around to "Shutdown"
  191. end
  192. else
  193. if nOption < #menuOptions then
  194. nOption = nOption + 1
  195. else
  196. nOption = 1
  197. end
  198. end
  199. redrawUI()
  200. elseif key == keys.enter then
  201. if runProgramOrAction(nOption) then
  202. -- Do nothing, action taken
  203. else
  204. break -- Exit loop only if no action taken
  205. end
  206. end
  207. end
  208. end
  209.  
  210. -- Final screen settings before running the selected action
  211. term.setBackgroundColor(colors.black)
  212. term.setTextColor(colors.white)
  213. term.clear()
  214.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement