SuperMcBrent

buttonLib

Feb 24th, 2023 (edited)
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.58 KB | Gaming | 0 0
  1. local buttonData, dependencies = {}, {}
  2. local composites, groups = {}, {}
  3.  
  4. local function ensure(app, view)
  5.     if not buttonData[app] then buttonData[app] = {} end
  6.     if not buttonData[app][view] then buttonData[app][view] = {} end
  7. end
  8.  
  9. local function assertParams(t)
  10.     for _, k in ipairs({ "app", "view", "name", "x", "y" }) do
  11.         if t[k] == nil then error("Missing mandatory parameter: " .. k) end
  12.     end
  13. end
  14.  
  15. local function ensureUniqueId(id)
  16.     for _, apps in pairs(buttonData) do
  17.         for _, views in pairs(apps) do
  18.             if views[id] then
  19.                 error("Duplicate button id: " .. id)
  20.             end
  21.         end
  22.     end
  23.     if composites[id] then
  24.         error("Duplicate composite id: " .. id)
  25.     end
  26. end
  27.  
  28. local function create(params)
  29.     if type(params) ~= "table" then error("create() requires a table") end
  30.  
  31.     assertParams(params)
  32.     ensureUniqueId(params.name)
  33.  
  34.     ensure(params.app, params.view)
  35.  
  36.     local text = params.textOn or "ON"
  37.     local b = {
  38.         app = params.app,
  39.         view = params.view,
  40.         name = params.name,
  41.         x = params.x,
  42.         y = params.y,
  43.         width = params.w or #text,
  44.         height = params.h or 1,
  45.         colorOn = params.colorOn or colors.lime,
  46.         colorOff = params.colorOff or colors.red,
  47.         state = params.state ~= nil and params.state or true,
  48.         visible = params.visible ~= false,
  49.         textOn = text,
  50.         textOff = params.textOff or "",
  51.         textX = params.textX or params.x,
  52.         textY = params.textY or params.y,
  53.         header = params.header or "",
  54.         group = params.group,
  55.         composite = params.composite
  56.     }
  57.  
  58.     buttonData[params.app][params.view][params.name] = b
  59.  
  60.     if b.group then
  61.         groups[b.group] = groups[b.group] or {}
  62.         table.insert(groups[b.group], b)
  63.     end
  64.  
  65.     if b.composite then
  66.         composites[b.composite] = composites[b.composite] or {}
  67.         table.insert(composites[b.composite], b)
  68.     end
  69.  
  70.     return b
  71. end
  72.  
  73. local function get(name)
  74.     for _, apps in pairs(buttonData) do
  75.         for _, views in pairs(apps) do
  76.             if views[name] then
  77.                 return views[name]
  78.             end
  79.         end
  80.     end
  81.     return nil
  82. end
  83.  
  84. local function update(name, props)
  85.     local b = get(name)
  86.     if not b or type(props) ~= "table" then return end
  87.     for k, v in pairs(props) do
  88.         if b[k] ~= nil then b[k] = v end
  89.     end
  90. end
  91.  
  92. local function isWithinBoundingBox(x, y, id)
  93.     local function check(b)
  94.         return b.visible and x >= b.x and x < b.x + b.width and y >= b.y and y < b.y + b.height
  95.     end
  96.  
  97.     if composites[id] then
  98.         for _, b in ipairs(composites[id]) do if check(b) then return true end end
  99.         return false
  100.     elseif groups[id] then
  101.         for _, b in ipairs(groups[id]) do if check(b) then return true end end
  102.         return false
  103.     else
  104.         local b = get(id)
  105.         return b and check(b) or false
  106.     end
  107. end
  108.  
  109. local function draw(id, mon)
  110.     local function drawButton(b)
  111.         if not b.visible then return end
  112.         local color = b.state and b.colorOn or b.colorOff
  113.         local text = b.state and b.textOn or b.textOff
  114.         dependencies.draw.drawLine(b.x, b.y, b.width, b.height, color, mon)
  115.         if b.header ~= "" then
  116.             dependencies.draw.drawTitle(b.x, b.y - 1, b.header, colors.white, colors.black, mon)
  117.         end
  118.         dependencies.draw.drawTitle(b.textX, b.textY, text, colors.white, color, mon)
  119.     end
  120.  
  121.     if composites[id] then
  122.         for _, b in ipairs(composites[id]) do drawButton(b) end
  123.     elseif groups[id] then
  124.         for _, b in ipairs(groups[id]) do drawButton(b) end
  125.     else
  126.         local b = get(id)
  127.         if b then drawButton(b) end
  128.     end
  129. end
  130.  
  131. local function quick(app, view, name, x, y, text, colorOn)
  132.     return create({
  133.         app = app,
  134.         view = view,
  135.         name = name,
  136.         x = x,
  137.         y = y,
  138.         w = #text,
  139.         h = 1,
  140.         colorOn = colorOn or colors.lime,
  141.         colorOff = colors.red,
  142.         state = true,
  143.         textOn = text,
  144.         textOff = "",
  145.         textX = x,
  146.         textY = y
  147.     })
  148. end
  149.  
  150. return {
  151.     id = "buttons",
  152.     name = "Button Library",
  153.     alias = "button",
  154.     dependencies = { "draw" },
  155.     init = function(deps)
  156.         dependencies = deps or {}
  157.         if not dependencies.draw then
  158.             error("Missing dependency: draw")
  159.         end
  160.     end,
  161.     create = create,
  162.     quick = quick,
  163.     update = update,
  164.     isWithinBoundingBox = isWithinBoundingBox,
  165.     draw = draw
  166. }
  167.  
Advertisement
Add Comment
Please, Sign In to add comment