mast3rillusion

buttons.lua

Jul 27th, 2021 (edited)
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.49 KB | None | 0 0
  1. os.loadAPI("settings.lua")
  2. os.loadAPI("graphics.lua")
  3. os.loadAPI("notifications.lua")
  4.  
  5. buttons = {}
  6. hasMonitor = false
  7.  
  8. if peripheral.find("monitor") then
  9.     hasMonitor = true
  10. end
  11.  
  12. function createBtn(text,bkgColor,height,width,x,y)
  13.     btn = graphics.button:new()
  14.     btn.text = text
  15.     btn.bkgColor = bkgColor
  16.     btn.h = height
  17.     btn.w = width
  18.     btn.x = x
  19.     btn.y = y
  20.     return btn
  21. end
  22.  
  23. function createCheckbox(text,x,y,checked,bkgColor)
  24.     checkbox = graphics.checkbox:new()
  25.     checkbox.text = text
  26.     checkbox.x = x
  27.     checkbox.y = y
  28.     checkbox.checked = checked
  29.     checkbox.bkgColor = bkgColor
  30.     return checkbox
  31. end
  32.  
  33. function addButton(btn)
  34.     table.insert(buttons, btn )
  35. end
  36.  
  37. function closeProgram()
  38.     notifications.addNotification("close", 10, "Program Closing", colors.red, colors.black)
  39.     graphics.fillScreen(colors.black)
  40.     os.shutdown()
  41. end
  42.  
  43. function setupButtons()
  44.     local w, h = term.getSize()
  45.     -- shutdown button
  46.     shutdownBtn = createBtn("X",colors.red,3,3,w-2,2)
  47.     shutdownBtn.trigger = function()
  48.         closeProgram()
  49.     end
  50.     addButton(shutdownBtn)
  51. end
  52.  
  53. function drawButtons()
  54.     for k,v in pairs(buttons) do
  55.         v.draw(v)
  56.     end
  57. end
  58.  
  59. function buttonClickEvent()
  60.     eventType = ""
  61.     if hasMonitor == true then
  62.         eventType = "monitor_touch"
  63.     else
  64.         eventType = "mouse_click"
  65.     end
  66.     event, button, x, y = os.pullEvent(eventType)
  67.     for k,v in pairs(buttons) do
  68.         v.detect(v,x,y,true)
  69.     end
  70. end
  71.  
  72. function initButtons()
  73.     setupButtons()
  74.     drawButtons()
  75. end
  76.  
  77. function update()
  78.     drawButtons()
  79. end
Add Comment
Please, Sign In to add comment