Advertisement
gravitowl

UI API

Mar 11th, 2021
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. -- GLOBALS
  2. termWidth, termHeight = term.getSize()
  3.  
  4. -- FUNCTIONS
  5.  
  6. ---- BUTTON
  7.  
  8. Button = {
  9. x = 0,
  10. y = 0,
  11. w = 0,
  12. h = 0,
  13. callBack = nil,
  14. backColor = nil,
  15. textColor = nil,
  16. label = "button"
  17. }
  18.  
  19. function Button:new (o, x, y, w, h, label, textColor, backColor, callBack)
  20. local o = setmetatable({}, {__index = self})
  21. o.__index = o
  22. o.x = x or 1
  23. if o.x < 1 or o.x > termWidth then o.x = 1 end
  24. o.y = y or 1
  25. if o.y < 1 or o.y > termHeight then o.y = 1 end
  26. o.w = w or 10
  27. if o.x + o.w > termWidth or o.w < 1 then o.w = 1 end
  28. o.h = h or 10
  29. if o.y + o.h > termHeight or o.h < 1 then o.h = 1 end
  30. o.label = label or "button"
  31. o.textColor = textColor or colors.white
  32. o.backColor = backColor or colors.green
  33. o.callBack = callBack
  34.  
  35. return o
  36. end
  37.  
  38. function Button:draw ()
  39. term.setCursorPos(self.x, self.y)
  40. local textLength = #self.label
  41. local buttonLength = math.max(self.w, textLength)
  42. for i = 1, self.h, 1 do
  43. term.blit(string.rep(" ", buttonLength), string.rep(colors.toBlit(self.textColor), buttonLength), string.rep(colors.toBlit(self.backColor), buttonLength))
  44. term.setCursorPos(self.x, self.y + i)
  45. end
  46. term.setCursorPos(buttonLength/2 - textLength/2 + self.x, self.h/2 + self.y)
  47. term.setTextColor(self.textColor)
  48. term.blit(self.label, string.rep(colors.toBlit(self.textColor), textLength), string.rep(colors.toBlit(self.backColor), textLength))
  49. end
  50.  
  51. function Button:isClicked(x, y)
  52. if x >= self.x and x <= self.x + self.w - 1 and y >= self.y and y <= self.y + self.h - 1 then
  53. if type(self.callBack) == "function" then
  54. return self.callBack()
  55. elseif type(self.callBack) == "table" then
  56. return self.callBack[1](self.callBack[2])
  57. end
  58. local func = load(self.callBack)
  59. return func()
  60. end
  61. end
  62.  
  63. ---- RECTANGLE
  64.  
  65. function drawRectangle(x, y, w, h, color, text, textcolor)
  66. term.setCursorPos(x, y)
  67. for i = 1, h, 1 do
  68. term.blit(string.rep(" ", w), string.rep(1, w), string.rep(colors.toBlit(color), w))
  69. term.setCursorPos(x, y + i)
  70. end
  71.  
  72. if text ~= nil then
  73. term.setCursorPos(w / 2 - #text / 2 + x, h / 2 + x)
  74. term.blit(text, string.rep(colors.toBlit(textcolor), #text), string.rep(colors.toBlit(color), #text))
  75. end
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement