SuicidalSTDz

Button API

Jul 14th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. local tButtons = {}
  2.  
  3. function clear()
  4. tButtons = {}
  5. end
  6.  
  7. function new(_sText, _nX, _nY, _nWidth, _nHeight, _nTextColour, _nOffColour, _nOnColour, _fClick, _nDelay)
  8. if type(_sText) ~= "string" then
  9. error("Argument 1: string expected, got "..type(text), 2)
  10. elseif type(_nX) ~= "number" then
  11. error("Argument 2: number expected, got "..type(_nX), 2)
  12. elseif type(_nY) ~= "number" then
  13. error("Argument 3: number expected, got "..type(_nY), 2)
  14. elseif type(_nWidth) ~= "number" then
  15. error("Argument 4: number expected, got "..type(_nWidth), 2)
  16. elseif type(_nHeight) ~= "number" then
  17. error("Argument 5: number expected, got "..type(_nHeight), 2)
  18. elseif type(_fClick) ~= "function" then
  19. error("Argument 8: function expected, got "..type(_fClick), 2)
  20. elseif type(_nDelay) ~= "number" then
  21. error("Argument 9: number expected, got "..type(_nDelay), 2)
  22. end
  23. table.insert(tButtons, {
  24. text = _sText,
  25. x = math.floor(_nX),
  26. endX = math.floor((_nX + _nWidth) - 1),
  27. y = _nY,
  28. endY = math.floor((_nY + _nHeight) - 1),
  29. w = math.floor(_nWidth),
  30. h = math.floor(_nHeight),
  31. textCol = _nTextColour,
  32. onCol = _nOnColour,
  33. offCol = _nOffColour,
  34. on_click = _fClick,
  35. delay = _nDelay,
  36. value = false
  37. })
  38. end
  39.  
  40. local function draw(bText, x, y)
  41. local col
  42. for _, v in pairs(tButtons) do
  43. if bText == v.text and x == v.x and y == v.y then
  44. if v.value then
  45. col = v.onCol
  46. else
  47. col = v.offCol
  48. end
  49. for y = v.y, v.endY do
  50. paintutils.drawLine(v.x, y, v.endX, y, col)
  51. end
  52. term.setTextColour(v.textCol)
  53. term.setCursorPos(((v.endX + v.x - #v.text) / 2) + 1, (v.endY + v.y) / 2)
  54. term.write(v.text)
  55. end
  56. end
  57. end
  58.  
  59. function run()
  60. for _, v in pairs(tButtons) do
  61. draw(v.text, v.x, v.y, v.uID)
  62. end
  63. while true do
  64. local _, p1, x, y = os.pullEvent("monitor_touch")
  65. for _, v in pairs(tButtons) do
  66. if (x >= v.x and x <= v.endX and y >= v.y and y <= v.endY) then
  67. parallel.waitForAll(
  68. function()
  69. v.value = true
  70. draw(v.text, v.x, v.y)
  71. sleep(v.delay)
  72. v.value = false
  73. draw(v.text, v.x, v.y)
  74. end,
  75. v.on_click
  76. )
  77. end
  78. end
  79. end
  80. end
Advertisement
Add Comment
Please, Sign In to add comment