Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- UI Helper Module for SimpleOS
- local ui = {}
- -- Colors
- ui.colors = {
- desktop = colors.cyan,
- taskbar = colors.gray,
- taskbarText = colors.white,
- window = colors.white,
- windowTitle = colors.blue,
- windowTitleText = colors.white,
- button = colors.lightGray,
- buttonText = colors.black,
- buttonHover = colors.gray,
- icon = colors.lightBlue,
- iconText = colors.white,
- menuBg = colors.white,
- menuText = colors.black,
- menuHighlight = colors.lightBlue,
- }
- -- Get terminal size
- function ui.getSize()
- return term.getSize()
- end
- -- Draw a filled box
- function ui.drawBox(x, y, width, height, color)
- term.setBackgroundColor(color)
- for i = 0, height - 1 do
- term.setCursorPos(x, y + i)
- term.write(string.rep(" ", width))
- end
- end
- -- Draw text centered in a box
- function ui.drawCenteredText(x, y, width, text, bgColor, textColor)
- term.setBackgroundColor(bgColor)
- term.setTextColor(textColor)
- local startX = x + math.floor((width - #text) / 2)
- term.setCursorPos(startX, y)
- term.write(text)
- end
- -- Draw a button
- function ui.drawButton(x, y, width, height, text, bgColor, textColor)
- ui.drawBox(x, y, width, height, bgColor)
- local textY = y + math.floor(height / 2)
- ui.drawCenteredText(x, textY, width, text, bgColor, textColor)
- end
- -- Draw a window
- function ui.drawWindow(x, y, width, height, title)
- -- Title bar
- ui.drawBox(x, y, width, 1, ui.colors.windowTitle)
- term.setTextColor(ui.colors.windowTitleText)
- term.setCursorPos(x + 1, y)
- term.write(title:sub(1, width - 4))
- -- Close button
- term.setCursorPos(x + width - 2, y)
- term.setBackgroundColor(colors.red)
- term.write("X")
- -- Window body
- ui.drawBox(x, y + 1, width, height - 1, ui.colors.window)
- end
- -- Draw an icon (app shortcut)
- function ui.drawIcon(x, y, name, symbol)
- -- Icon box (3x2)
- ui.drawBox(x, y, 5, 2, ui.colors.icon)
- term.setTextColor(ui.colors.iconText)
- term.setCursorPos(x + 2, y)
- term.write(symbol or "#")
- -- Label below
- term.setBackgroundColor(ui.colors.desktop)
- term.setTextColor(colors.white)
- term.setCursorPos(x, y + 2)
- local label = name:sub(1, 7)
- term.write(label)
- end
- -- Draw input field
- function ui.drawInput(x, y, width, value, active)
- local bgColor = active and colors.white or colors.lightGray
- term.setBackgroundColor(bgColor)
- term.setTextColor(colors.black)
- term.setCursorPos(x, y)
- local display = value:sub(-width + 1)
- term.write(display .. string.rep(" ", width - #display))
- end
- -- Check if click is within bounds
- function ui.isInBounds(clickX, clickY, x, y, width, height)
- return clickX >= x and clickX < x + width and
- clickY >= y and clickY < y + height
- end
- -- Show a simple message box
- function ui.messageBox(title, message, buttons)
- local w, h = ui.getSize()
- local boxWidth = math.max(#message + 4, 20)
- local boxHeight = 5 + #buttons
- local x = math.floor((w - boxWidth) / 2)
- local y = math.floor((h - boxHeight) / 2)
- ui.drawWindow(x, y, boxWidth, boxHeight, title)
- term.setBackgroundColor(ui.colors.window)
- term.setTextColor(colors.black)
- term.setCursorPos(x + 2, y + 2)
- term.write(message)
- -- Draw buttons
- local buttonY = y + 4
- local buttonWidth = 8
- local totalWidth = #buttons * (buttonWidth + 2)
- local startX = x + math.floor((boxWidth - totalWidth) / 2)
- for i, btn in ipairs(buttons) do
- local btnX = startX + (i - 1) * (buttonWidth + 2)
- ui.drawButton(btnX, buttonY, buttonWidth, 1, btn, ui.colors.button, ui.colors.buttonText)
- end
- return x, y, boxWidth, boxHeight, buttons
- end
- return ui
Advertisement
Add Comment
Please, Sign In to add comment