SuperMcBrent

buttonLib

Jan 22nd, 2022 (edited)
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.68 KB | None | 0 0
  1. -- load libraries
  2. os.loadAPI("drawLib")
  3.  
  4. local buttons = {}
  5.  
  6. -- create
  7. function addButton(x,y,w,h,name,colorOn,colorOff,state,textOn,textOff,textX,textY,header)
  8.     buttons[name] = {
  9.         ["x"] = x,
  10.         ["y"] = y,
  11.         ["width"] = w,
  12.         ["height"] = h,
  13.         ["colorOn"] = colorOn,
  14.         ["colorOff"] = colorOff,
  15.         ["state"] = state,
  16.         ["textOn"] = textOn,
  17.         ["textOff"] = textOff,
  18.         ["textX"] = textX,
  19.         ["textY"] = textY,
  20.         ["header"] = header
  21.     }
  22. end
  23. -- read
  24. function getButton(buttonname)
  25.     return buttons[buttonname]
  26. end
  27. function getAllButtons()
  28.     return buttons
  29. end
  30. function isWithinBoundingBox(x,y,buttonname)
  31.     return x >= buttons[buttonname].x and x < buttons[buttonname].x + buttons[buttonname].width and
  32.         y >= buttons[buttonname].y and y < buttons[buttonname].y + buttons[buttonname].height
  33. end
  34. -- update
  35. function setButtonState(buttonname, state)
  36.     buttons[buttonname].state = state
  37. end
  38. --draw
  39. function drawButton(button,monitor)
  40.     local color = colors.red
  41.     local text = ""
  42.     if buttons[button].state==true then color = buttons[button].colorOn else color = buttons[button].colorOff end
  43.     if buttons[button].state==true then text = buttons[button].textOn else text = buttons[button].textOff end
  44.     drawLib.drawLine(buttons[button].x, buttons[button].y, buttons[button].width, buttons[button].height, color, monitor)
  45.     if buttons[button].header == "" then else
  46.     drawLib.drawTitle(buttons[button].x, buttons[button].y - 1, buttons[button].header, colors.white, colors.black, monitor) end
  47.     drawLib.drawTitle(buttons[button].textX, buttons[button].textY, text, colors.white, color, monitor)
  48. end
  49.  
  50.  
  51.  
  52.  
Add Comment
Please, Sign In to add comment