Advertisement
DOGGYWOOF

Untitled

Sep 7th, 2024 (edited)
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.52 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", "Restart", "Log Off", "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) -- Green taskbar
  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) -- Dark green start menu
  33. startMenu.clear()
  34.  
  35. -- Draw the start menu title
  36. startMenu.setTextColor(colors.white)
  37. startMenu.setCursorPos(2, 1)
  38. startMenu.write("=== Start Menu ===")
  39.  
  40. -- Draw the options in the start menu
  41. for i, option in ipairs(menuOptions) do
  42. startMenu.setCursorPos(2, 2 + i)
  43. if nOption == i then
  44. startMenu.setTextColor(colors.black)
  45. startMenu.setBackgroundColor(colors.white)
  46. startMenu.write("[ " .. option .. " ]")
  47. startMenu.setBackgroundColor(colors.green)
  48. startMenu.setTextColor(colors.white)
  49. else
  50. startMenu.write(" " .. option .. " ")
  51. end
  52. end
  53. end
  54.  
  55. -- Helper function to draw the power menu
  56. local function drawPowerMenu()
  57. if not isPowerMenuOpen then
  58. powerMenu.setVisible(false)
  59. return
  60. end
  61.  
  62. powerMenu.setVisible(true)
  63. powerMenu.setBackgroundColor(colors.black) -- Black background for power menu
  64. powerMenu.clear()
  65.  
  66. -- Draw the power menu title
  67. powerMenu.setTextColor(colors.white)
  68. powerMenu.setCursorPos(2, 1)
  69. powerMenu.write("=== Power Menu ===")
  70.  
  71. -- Draw the options in the power menu
  72. for i, option in ipairs(powerOptions) do
  73. powerMenu.setCursorPos(2, 3 + i)
  74. if nOption == i then
  75. powerMenu.setTextColor(colors.black)
  76. powerMenu.setBackgroundColor(colors.white)
  77. powerMenu.write("[ " .. option .. " ]")
  78. powerMenu.setBackgroundColor(colors.black)
  79. powerMenu.setTextColor(colors.white)
  80. else
  81. powerMenu.write(" " .. option .. " ")
  82. end
  83. end
  84. end
  85.  
  86. -- Function to redraw the entire UI
  87. local function redrawUI()
  88. term.setBackgroundColor(colors.blue) -- Blue desktop
  89. term.clear()
  90. drawTaskbar()
  91. if isPowerMenuOpen then
  92. drawPowerMenu()
  93. else
  94. drawStartMenu()
  95. end
  96. end
  97.  
  98. -- Initial UI drawing
  99. redrawUI()
  100.  
  101. -- Function to handle mouse clicks on the taskbar, start menu, and power menu
  102. local function handleMouseClick(x, y)
  103. if y == h and x >= 1 and x <= 7 then
  104. isStartMenuOpen = not isStartMenuOpen
  105. isPowerMenuOpen = false
  106. redrawUI()
  107. elseif isStartMenuOpen and x >= 1 and x <= 20 and y >= h - 10 and y <= h - 1 then
  108. local clickedOption = y - (h - 10) - 1
  109. if clickedOption >= 1 and clickedOption <= #menuOptions then
  110. nOption = clickedOption
  111. if nOption == 3 then -- "Power Menu" option opens the power menu
  112. isStartMenuOpen = false
  113. isPowerMenuOpen = true
  114. redrawUI()
  115. else
  116. return true
  117. end
  118. end
  119. 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
  120. local clickedOption = y - (math.floor(h / 2) - 7) - 1
  121. if clickedOption >= 1 and clickedOption <= #powerOptions then
  122. nOption = clickedOption
  123. return true
  124. end
  125. end
  126. return false
  127. end
  128.  
  129. -- Function to manage running programs and power options
  130. local function runProgramOrAction(option)
  131. if isPowerMenuOpen then
  132. if option == 1 then
  133. term.setBackgroundColor(colors.black)
  134. term.setTextColor(colors.white)
  135. term.clear()
  136. shell.run("/disk/ACPI/shutdown") -- Shutdown
  137. elseif option == 2 then
  138. term.setBackgroundColor(colors.black)
  139. term.setTextColor(colors.white)
  140. term.clear()
  141. shell.run("/disk/ACPI/restart") -- Restart
  142. elseif option == 3 then
  143. term.setBackgroundColor(colors.black)
  144. term.setTextColor(colors.white)
  145. term.clear()
  146. shell.run("/disk/ACPI/logoff") -- Log off
  147. elseif option == 4 then
  148. -- Close the power menu and return to the desktop
  149. isPowerMenuOpen = false
  150. redrawUI()
  151. end
  152. return
  153. end
  154.  
  155. -- Conditions based on the selected option
  156. if option == 1 then
  157. shell.run("/disk/os/home.lua")
  158. elseif option == 2 then
  159. shell.run("/disk/os/programs")
  160. end
  161. end
  162.  
  163. -- Main event handling loop
  164. while true do
  165. local e, p1, p2, p3 = os.pullEvent()
  166.  
  167. if e == "mouse_click" then
  168. local button, x, y = p1, p2, p3
  169. if handleMouseClick(x, y) then
  170. runProgramOrAction(nOption)
  171. if not isPowerMenuOpen then
  172. break
  173. end
  174. end
  175. elseif e == "key" and (isStartMenuOpen or isPowerMenuOpen) then
  176. local key = p1
  177.  
  178. if key == keys.up then -- Up key
  179. if nOption > 1 then
  180. nOption = nOption - 1
  181. else
  182. nOption = #menuOptions
  183. end
  184. redrawUI()
  185. elseif key == keys.down then -- Down key
  186. if nOption < (#menuOptions or #powerOptions) then
  187. nOption = nOption + 1
  188. else
  189. nOption = 1
  190. end
  191. redrawUI()
  192. elseif key == keys.enter then -- Enter key
  193. runProgramOrAction(nOption)
  194. if not isPowerMenuOpen then
  195. break
  196. end
  197. end
  198. end
  199. end
  200.  
  201. -- Final screen settings before running the selected action
  202. term.setBackgroundColor(colors.black)
  203. term.setTextColor(colors.white)
  204. term.clear()
  205.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement