Advertisement
DOGGYWOOF

Desktop

Jul 18th, 2024 (edited)
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. -- desktop.lua
  2.  
  3. -- Initialize the desktop environment
  4. local termWidth, termHeight = term.getSize()
  5. local startMenuVisible = false
  6. local powerOptionsVisible = false
  7.  
  8. -- Draw the desktop background
  9. local function drawBackground()
  10. term.setBackgroundColor(colors.blue)
  11. term.clear()
  12. end
  13.  
  14. -- Draw the taskbar
  15. local function drawTaskbar()
  16. term.setBackgroundColor(colors.green)
  17. term.setTextColor(colors.white)
  18. term.setCursorPos(1, termHeight)
  19. term.write(string.rep(" ", termWidth))
  20. term.setCursorPos(1, termHeight)
  21. term.write(" Start ")
  22. end
  23.  
  24. -- Draw the start menu
  25. local function drawStartMenu()
  26. local menuWidth, menuHeight = 20, 10
  27. term.setBackgroundColor(colors.gray)
  28. term.setTextColor(colors.white)
  29. for i = 1, menuHeight do
  30. term.setCursorPos(1, termHeight - menuHeight + i - 1)
  31. term.write(string.rep(" ", menuWidth))
  32. end
  33.  
  34. -- Add start menu options
  35. term.setCursorPos(2, termHeight - menuHeight + 1)
  36. term.write("Command")
  37. term.setCursorPos(2, termHeight - menuHeight + 2)
  38. term.write("Programs")
  39. term.setCursorPos(2, termHeight - menuHeight + 3)
  40. term.write("Power Options")
  41. term.setCursorPos(2, termHeight - menuHeight + 4)
  42. term.write("Lock")
  43. end
  44.  
  45. -- Draw the power options menu
  46. local function drawPowerOptions()
  47. local menuWidth, menuHeight = 20, 4
  48. term.setBackgroundColor(colors.gray)
  49. term.setTextColor(colors.white)
  50. for i = 1, menuHeight do
  51. term.setCursorPos(1, termHeight - menuHeight + i - 1)
  52. term.write(string.rep(" ", menuWidth))
  53. end
  54.  
  55. -- Add power options
  56. term.setCursorPos(2, termHeight - menuHeight + 1)
  57. term.write("Shutdown")
  58. term.setCursorPos(2, termHeight - menuHeight + 2)
  59. term.write("Reboot")
  60. end
  61.  
  62. -- Execute command based on the selected option
  63. local function executeCommand(option)
  64. if option == 1 then
  65. shell.run("/disk/os/home")
  66. elseif option == 2 then
  67. shell.run("/disk/os/programs")
  68. elseif option == 3 then
  69. powerOptionsVisible = true
  70. redraw()
  71. elseif option == 4 then
  72. shell.run("/disk/ACPI/logoff")
  73. end
  74. end
  75.  
  76. -- Handle power option selection
  77. local function handlePowerOptionsSelection(y)
  78. local option = y - (termHeight - 4) + 1
  79. if option == 1 then
  80. os.shutdown()
  81. elseif option == 2 then
  82. os.reboot()
  83. end
  84. end
  85.  
  86. -- Redraw the entire desktop
  87. local function redraw()
  88. drawBackground()
  89. drawTaskbar()
  90. if startMenuVisible then
  91. drawStartMenu()
  92. if powerOptionsVisible then
  93. drawPowerOptions()
  94. end
  95. end
  96. end
  97.  
  98. -- Main event loop
  99. local function eventLoop()
  100. while true do
  101. redraw()
  102. local event, button, x, y = os.pullEvent("mouse_click")
  103.  
  104. -- Handle taskbar interaction (toggle Start menu)
  105. if y == termHeight and x <= 7 then
  106. startMenuVisible = not startMenuVisible
  107. if not startMenuVisible then
  108. powerOptionsVisible = false
  109. end
  110. elseif startMenuVisible then
  111. if powerOptionsVisible then
  112. -- Handle power options menu interactions
  113. if y >= termHeight - 4 and y < termHeight then
  114. handlePowerOptionsSelection(y)
  115. powerOptionsVisible = false
  116. startMenuVisible = false
  117. end
  118. else
  119. -- Handle start menu interactions
  120. if y >= termHeight - 10 and y < termHeight then
  121. local selectedOption = y - (termHeight - 10) + 1
  122. executeCommand(selectedOption)
  123. startMenuVisible = false
  124. powerOptionsVisible = false
  125. end
  126. end
  127. end
  128. end
  129. end
  130.  
  131. -- Initialize desktop
  132. redraw()
  133.  
  134. -- Start the event loop
  135. eventLoop()
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement