Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local gpu = component.gpu
- local width, height = gpu.getResolution()
- local callbacks = {}
- local callbackFuncs = {}
- local exitButtonSingleTile = false
- local exitButtonX1 = 0
- local exitButtonX2 = 0
- local exitButtonY1 = 0
- local exitButtonY2 = 0
- local settings = {
- backgroundColor = 0x0000FF,
- appStyle = 1,
- title = "Application",
- clearOnEnd = true
- }
- local colors = {
- red = 0xFF0000,
- green = 0x00FF00,
- blue = 0x0000FF,
- white = 0xFFFFFF,
- black = 0x000000,
- lightGray = 0xD3D3D3,
- yellow = 0xFFFF00,
- gray = 0x808080,
- darkGray = 0xA9A9A9,
- orange = 0xFFA500,
- purple = 0x800080,
- pink = 0xFFC0CB,
- brown = 0xA52A2A,
- cyan = 0x00FFFF,
- magenta = 0xFF00FF,
- lightBlue = 0xADD8E6,
- lime = 0x00FF00,
- teal = 0x008080,
- navy = 0x000080,
- olive = 0x808000
- }
- function clearScreen()
- gpu.fill(1, 1, width, height, " ")
- end
- function init()
- gpu.setBackground(settings.backgroundColor)
- clearScreen()
- if settings.appStyle == 1 then
- exitButtonSingleTile = true
- exitButtonX1 = width
- exitButtonY1 = 1
- gpu.setBackground(colors.lightGray)
- gpu.setForeground(colors.black)
- gpu.fill(1, 1, width, 1, " ")
- gpu.set(width / 2 - (string.len(settings.title) / 2), 1, settings.title)
- gpu.setBackground(colors.red)
- gpu.setForeground(colors.white)
- gpu.set(width, 1, "x")
- end
- end
- local library = {
- api = {
- settings = settings,
- colors = colors,
- init = init,
- mainLoop = function()
- while true do
- local eventType, button, x, y = event.pull(0.1, "touch")
- if eventType == "touch" then
- if exitButtonSingleTile then
- if x == exitButtonX1 and y == exitButtonY1 then
- break
- end
- end
- for _, cb in pairs(callbacks) do
- if x > cb.x1 and x < cb.x2 and y > cb.y1 and y < cb.y2 then
- callbackFuncs[cb.name]()
- end
- end
- end
- end
- if settings.clearOnEnd then
- gpu.setBackground(api.colors.black)
- clearScreen()
- end
- end,
- uiElement = {
- addButton = function(x, y, w, h, bgColor, textColor, text, callback)
- local api = library.api
- gpu.setBackground(bgColor)
- gpu.setForeground(textColor)
- local w2 = w / 2
- local h2 = h / 2
- gpu.fill(x - w2, y - h2, w, h, " ")
- gpu.set(x - (string.len(text) / 2), y - 1, text)
- callbacks[callback] = {
- x1 = x - w2,
- x2 = x + w2,
- y1 = y - h2,
- y2 = y + h2,
- name = callback
- }
- gpu.setBackground(settings.backgroundColor)
- end,
- addLabel = function(x, y, textColor, bgColor, text)
- gpu.setForeground(textColor)
- gpu.setBackground(bgColor)
- gpu.set(x - (string.len(text) / 2), y - 1, text)
- gpu.setBackground(settings.backgroundColor)
- end,
- addBorderedLabel = function(x, y, w, h, textColor, bgColor, borderColor, text)
- gpu.setForeground(textColor)
- gpu.setBackground(borderColor)
- local w2 = w / 2
- local h2 = h / 2
- gpu.fill(x - w2, y - h2, w, h, " ")
- gpu.setBackground(bgColor)
- gpu.fill(x - w2 + 1, y - h2 + 1, w - 2, h - 2, " ")
- gpu.set(x - (string.len(text) / 2), y - 2, text)
- gpu.setBackground(settings.backgroundColor)
- end
- }
- }
- }
- return library
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement