Advertisement
k2green

Button API

Aug 16th, 2023 (edited)
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.63 KB | Gaming | 0 0
  1. local api = {
  2.     ["buttons"] = {}
  3. }
  4.  
  5. local function createButtonModel(side, x, y, w, h, text, textColor, colorA, colorB, toggle, callback)
  6.     return {
  7.         ["side"] = side,
  8.         ["x"] = x,
  9.         ["y"] = y,
  10.         ["width"] = w,
  11.         ["height"] = h,
  12.         ["text"] = text,
  13.         ["textColor"] = textColor,
  14.         ["backgroundColorA"] = colorA,
  15.         ["backgroundColorB"] = colorB,
  16.         ["toggle"] = toggle,
  17.         ["state"] = false,
  18.         ["callback"] = callback,
  19.     }
  20. end
  21.  
  22. local function drawButton(canvas, button)
  23.     local oldTextColor = canvas.getTextColor()
  24.     local oldBackgroundColor = canvas.getBackgroundColor()
  25.  
  26.     canvas.setCursorBlink(false)
  27.     canvas.setTextColor(button.textColor)
  28.  
  29.     if button.state then
  30.         canvas.setBackgroundColor(button.backgroundColorB)
  31.     else
  32.         canvas.setBackgroundColor(button.backgroundColorA)
  33.     end
  34.  
  35.     for y = 0, button.height - 1, 1 do
  36.         for x = 0, button.width - 1, 1 do
  37.             canvas.setCursorPos(button.x + x, button.y + y)
  38.             canvas.write(" ")
  39.         end
  40.     end
  41.  
  42.     local x = button.x + (button.width - #button.text) / 2
  43.     local y = button.y + (button.height - 1) / 2
  44.     canvas.setCursorPos(x, y)
  45.     canvas.write(button.text)
  46.  
  47.     canvas.setTextColor(oldTextColor)
  48.     canvas.setBackgroundColor(oldBackgroundColor)
  49. end
  50.  
  51. function api.createButton(side, sx, sy, ex, ey, text, textColor, colorA, colorB, toggle, callback)
  52.     table.insert(api.buttons, createButtonModel(side, sx, sy, ex, ey, text, textColor, colorA, colorB, toggle, callback))
  53. end
  54.  
  55. function api.drawButtons()
  56.     for i, button in ipairs(api.buttons) do
  57.         if button.side ~= nil then
  58.             peripheral.wrap(button.side).clear()
  59.         end
  60.     end
  61.  
  62.     for i, button in ipairs(api.buttons) do
  63.         if button.side == nil then
  64.             drawButton(term, button)
  65.         else
  66.             drawButton(peripheral.wrap(button.side), button)
  67.         end
  68.     end
  69. end
  70.  
  71. function api.interact(side, x, y)
  72.     for i, button in ipairs(api.buttons) do
  73.         local endX = button.x + button.width
  74.         local endY = button.y + button.height
  75.  
  76.         if x >= button.x and x < endX and y >= button.y and y < endY and side == button.side then
  77.             if button.toggle then
  78.                 button.state = not button.state
  79.                 api.drawButtons()
  80.                 button.callback(button.state)
  81.             else
  82.                 button.state = true
  83.                 api.drawButtons()
  84.                 button.callback()
  85.                 button.state = false
  86.             end
  87.         end
  88.     end
  89. end
  90.  
  91. return api
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement