Advertisement
DabDaddy6223

os_gui

Mar 24th, 2023 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.89 KB | None | 0 0
  1. local gfx = require("gfx")
  2. local utils = require("utils")
  3.  
  4. function createParent()
  5.     return {isVisible = true, children = {}}
  6. end
  7.  
  8.  
  9.  
  10. -- Buttons
  11. function setAllButtonsOff(parent)
  12.     for i = 1, #(parent["children"]) do
  13.         local current = parent["children"][i]
  14.         if current["type"] == "button" then
  15.             parent["children"][i]["selected"] = false
  16.         end
  17.     end
  18. end
  19.  
  20. function toggleButton(parent, buttonID)
  21.     local data = parent["children"][buttonID]["selected"]
  22.     parent["children"][buttonID]["selected"] = not data
  23. end
  24.  
  25. function addCentralXButton(parent, message, posY, callback)
  26.     local x, y = utils.midpoint(Width, Height)
  27.     local newX = utils.getXPosOfString(message, x)
  28.     return addButton(parent, message, newX, posY, callback)
  29. end
  30.  
  31. function addButton(parent, message, posX, posY, callback)
  32.     local data = {}
  33.     data["type"] = "button"
  34.     data["selected"] = false
  35.     data["message"] = message
  36.     data["posX"] = posX
  37.     data["posY"] = posY
  38.     data["callback"] = callback
  39.    
  40.     local lengthOfChildren = #(parent["children"])
  41.     parent["children"][lengthOfChildren + 1] = data
  42.     return lengthOfChildren
  43. end
  44.  
  45.  
  46.  
  47. function show(parent)
  48.     for item = 1, #(parent["children"]) do
  49.         local current = parent["children"][item]
  50.         if current["type"] == "button" then
  51.             local message, posX, posY = current["message"], current["posX"], current["posY"]
  52.             local selected = current["selected"]
  53.  
  54.             if selected then
  55.                 gfx.addText("[ " .. message .. " ]", posX - 2, posY)
  56.             else
  57.                 gfx.addText(message, posX, posY)
  58.             end
  59.         end
  60.     end
  61. end
  62.  
  63. return {
  64.     createParent = createParent,
  65.     show = show,
  66.     setAllButtonsOff = setAllButtonsOff,
  67.     toggleButton = toggleButton,
  68.     addButton = addButton,
  69.     addCentralXButton = addCentralXButton
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement