Advertisement
HawkPB

Untitled

Jul 31st, 2024 (edited)
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.03 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local gpu = component.gpu
  4. local width, height = gpu.getResolution()
  5. local callbacks = {}
  6. local callbackFuncs = {}
  7. local exitButtonSingleTile = false
  8. local exitButtonX1 = 0
  9. local exitButtonX2 = 0
  10. local exitButtonY1 = 0
  11. local exitButtonY2 = 0
  12. local settings = {
  13.     backgroundColor = 0x0000FF,
  14.     appStyle = 1,
  15.     title = "Application",
  16.     clearOnEnd = true
  17. }
  18. local colors = {
  19.     red = 0xFF0000,
  20.     green = 0x00FF00,
  21.     blue = 0x0000FF,
  22.     white = 0xFFFFFF,
  23.     black = 0x000000,
  24.     lightGray = 0xD3D3D3,
  25.     yellow = 0xFFFF00,
  26.     gray = 0x808080,
  27.     darkGray = 0xA9A9A9,
  28.     orange = 0xFFA500,
  29.     purple = 0x800080,
  30.     pink = 0xFFC0CB,
  31.     brown = 0xA52A2A,
  32.     cyan = 0x00FFFF,
  33.     magenta = 0xFF00FF,
  34.     lightBlue = 0xADD8E6,
  35.     lime = 0x00FF00,
  36.     teal = 0x008080,
  37.     navy = 0x000080,
  38.     olive = 0x808000
  39. }
  40. function clearScreen()
  41.     gpu.fill(1, 1, width, height, " ")
  42. end
  43. function init()
  44.     gpu.setBackground(settings.backgroundColor)
  45.     clearScreen()
  46.     if settings.appStyle == 1 then
  47.         exitButtonSingleTile = true
  48.         exitButtonX1 = width
  49.         exitButtonY1 = 1
  50.         gpu.setBackground(colors.lightGray)
  51.         gpu.setForeground(colors.black)
  52.         gpu.fill(1, 1, width, 1, " ")
  53.         gpu.set(width / 2 - (string.len(settings.title) / 2), 1, settings.title)
  54.         gpu.setBackground(colors.red)
  55.         gpu.setForeground(colors.white)
  56.         gpu.set(width, 1, "x")
  57.     end
  58. end
  59. local library = {
  60.     api = {
  61.         settings = settings,
  62.         colors = colors,
  63.        
  64.         init = init,
  65.         mainLoop = function()
  66.             while true do
  67.                 local eventType, button, x, y = event.pull(0.1, "touch")
  68.                 if eventType == "touch" then
  69.                     if exitButtonSingleTile then
  70.                         if x == exitButtonX1 and y == exitButtonY1 then
  71.                             break
  72.                         end
  73.                     end
  74.  
  75.                     for _, cb in pairs(callbacks) do
  76.                         if x > cb.x1 and x < cb.x2 and y > cb.y1 and y < cb.y2 then
  77.                             callbackFuncs[cb.name]()
  78.                         end
  79.                     end
  80.                 end
  81.             end
  82.  
  83.             if settings.clearOnEnd then
  84.                 gpu.setBackground(api.colors.black)
  85.                 clearScreen()
  86.             end
  87.         end,
  88.         uiElement = {
  89.             addButton = function(x, y, w, h, bgColor, textColor, text, callback)
  90.                 local api = library.api
  91.                 gpu.setBackground(bgColor)
  92.                 gpu.setForeground(textColor)
  93.                 local w2 = w / 2
  94.                 local h2 = h / 2
  95.                 gpu.fill(x - w2, y - h2, w, h, " ")
  96.                 gpu.set(x - (string.len(text) / 2), y - 1, text)
  97.                 callbacks[callback] = {
  98.                     x1 = x - w2,
  99.                     x2 = x + w2,
  100.                     y1 = y - h2,
  101.                     y2 = y + h2,
  102.                     name = callback
  103.                 }
  104.                 gpu.setBackground(settings.backgroundColor)
  105.             end,
  106.             addLabel = function(x, y, textColor, bgColor, text)
  107.                 gpu.setForeground(textColor)
  108.                 gpu.setBackground(bgColor)
  109.                 gpu.set(x - (string.len(text) / 2), y - 1, text)
  110.                 gpu.setBackground(settings.backgroundColor)
  111.             end,
  112.             addBorderedLabel = function(x, y, w, h, textColor, bgColor, borderColor, text)
  113.                 gpu.setForeground(textColor)
  114.                 gpu.setBackground(borderColor)
  115.                 local w2 = w / 2
  116.                 local h2 = h / 2
  117.                 gpu.fill(x - w2, y - h2, w, h, " ")
  118.                 gpu.setBackground(bgColor)
  119.                 gpu.fill(x - w2 + 1, y - h2 + 1, w - 2, h - 2, " ")
  120.                 gpu.set(x - (string.len(text) / 2), y - 2, text)
  121.                 gpu.setBackground(settings.backgroundColor)
  122.             end
  123.         }
  124.     }
  125. }
  126.  
  127. return library
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement