ElijahCrafter

Ui

Nov 27th, 2025
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. -- UI Helper Module for SimpleOS
  2.  
  3. local ui = {}
  4.  
  5. -- Colors
  6. ui.colors = {
  7. desktop = colors.cyan,
  8. taskbar = colors.gray,
  9. taskbarText = colors.white,
  10. window = colors.white,
  11. windowTitle = colors.blue,
  12. windowTitleText = colors.white,
  13. button = colors.lightGray,
  14. buttonText = colors.black,
  15. buttonHover = colors.gray,
  16. icon = colors.lightBlue,
  17. iconText = colors.white,
  18. menuBg = colors.white,
  19. menuText = colors.black,
  20. menuHighlight = colors.lightBlue,
  21. }
  22.  
  23. -- Get terminal size
  24. function ui.getSize()
  25. return term.getSize()
  26. end
  27.  
  28. -- Draw a filled box
  29. function ui.drawBox(x, y, width, height, color)
  30. term.setBackgroundColor(color)
  31. for i = 0, height - 1 do
  32. term.setCursorPos(x, y + i)
  33. term.write(string.rep(" ", width))
  34. end
  35. end
  36.  
  37. -- Draw text centered in a box
  38. function ui.drawCenteredText(x, y, width, text, bgColor, textColor)
  39. term.setBackgroundColor(bgColor)
  40. term.setTextColor(textColor)
  41. local startX = x + math.floor((width - #text) / 2)
  42. term.setCursorPos(startX, y)
  43. term.write(text)
  44. end
  45.  
  46. -- Draw a button
  47. function ui.drawButton(x, y, width, height, text, bgColor, textColor)
  48. ui.drawBox(x, y, width, height, bgColor)
  49. local textY = y + math.floor(height / 2)
  50. ui.drawCenteredText(x, textY, width, text, bgColor, textColor)
  51. end
  52.  
  53. -- Draw a window
  54. function ui.drawWindow(x, y, width, height, title)
  55. -- Title bar
  56. ui.drawBox(x, y, width, 1, ui.colors.windowTitle)
  57. term.setTextColor(ui.colors.windowTitleText)
  58. term.setCursorPos(x + 1, y)
  59. term.write(title:sub(1, width - 4))
  60.  
  61. -- Close button
  62. term.setCursorPos(x + width - 2, y)
  63. term.setBackgroundColor(colors.red)
  64. term.write("X")
  65.  
  66. -- Window body
  67. ui.drawBox(x, y + 1, width, height - 1, ui.colors.window)
  68. end
  69.  
  70. -- Draw an icon (app shortcut)
  71. function ui.drawIcon(x, y, name, symbol)
  72. -- Icon box (3x2)
  73. ui.drawBox(x, y, 5, 2, ui.colors.icon)
  74. term.setTextColor(ui.colors.iconText)
  75. term.setCursorPos(x + 2, y)
  76. term.write(symbol or "#")
  77.  
  78. -- Label below
  79. term.setBackgroundColor(ui.colors.desktop)
  80. term.setTextColor(colors.white)
  81. term.setCursorPos(x, y + 2)
  82. local label = name:sub(1, 7)
  83. term.write(label)
  84. end
  85.  
  86. -- Draw input field
  87. function ui.drawInput(x, y, width, value, active)
  88. local bgColor = active and colors.white or colors.lightGray
  89. term.setBackgroundColor(bgColor)
  90. term.setTextColor(colors.black)
  91. term.setCursorPos(x, y)
  92. local display = value:sub(-width + 1)
  93. term.write(display .. string.rep(" ", width - #display))
  94. end
  95.  
  96. -- Check if click is within bounds
  97. function ui.isInBounds(clickX, clickY, x, y, width, height)
  98. return clickX >= x and clickX < x + width and
  99. clickY >= y and clickY < y + height
  100. end
  101.  
  102. -- Show a simple message box
  103. function ui.messageBox(title, message, buttons)
  104. local w, h = ui.getSize()
  105. local boxWidth = math.max(#message + 4, 20)
  106. local boxHeight = 5 + #buttons
  107. local x = math.floor((w - boxWidth) / 2)
  108. local y = math.floor((h - boxHeight) / 2)
  109.  
  110. ui.drawWindow(x, y, boxWidth, boxHeight, title)
  111.  
  112. term.setBackgroundColor(ui.colors.window)
  113. term.setTextColor(colors.black)
  114. term.setCursorPos(x + 2, y + 2)
  115. term.write(message)
  116.  
  117. -- Draw buttons
  118. local buttonY = y + 4
  119. local buttonWidth = 8
  120. local totalWidth = #buttons * (buttonWidth + 2)
  121. local startX = x + math.floor((boxWidth - totalWidth) / 2)
  122.  
  123. for i, btn in ipairs(buttons) do
  124. local btnX = startX + (i - 1) * (buttonWidth + 2)
  125. ui.drawButton(btnX, buttonY, buttonWidth, 1, btn, ui.colors.button, ui.colors.buttonText)
  126. end
  127.  
  128. return x, y, boxWidth, boxHeight, buttons
  129. end
  130.  
  131. return ui
  132.  
Advertisement
Add Comment
Please, Sign In to add comment