Advertisement
dlpratte

Untitled

May 26th, 2013
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. os.loadAPI("class")
  2.  
  3. -- Screen
  4. screen = class.class(function(self, monitor)
  5. self.monitor = monitor
  6. self.drawables = {}
  7. end)
  8.  
  9. function screen:addDrawable(component)
  10. if component = nil then
  11. print("[Screen] Not adding nil drawable.")
  12. return self
  13. end
  14. local i = table.getn(self.drawables)
  15. self.drawables[i] = component
  16.  
  17. return self
  18. end
  19.  
  20. function screen:draw()
  21. local arrLength = table.getn(self.drawables)
  22. for i=0, arrLength do
  23. self.drawables[i]:draw(self.monitor)
  24. end
  25. end
  26.  
  27. -- Button
  28.  
  29. button = class.class(function(self, text, clickEvent)
  30. self.text = text
  31. self.x = 0
  32. self.y = 0
  33. self.width = 1
  34. self.height = 1
  35. self.paddedText = text
  36. self.emptyRow = ""
  37. self.clickEvent = clickEvent
  38. end)
  39.  
  40. function button:pos()
  41. local t = {}
  42. t.x = self.x
  43. t.y = self.y
  44. return t
  45. end
  46.  
  47. function button:text()
  48. return self.text
  49. end
  50.  
  51. function button:setText(text)
  52. self.text = text
  53. self:updateTextData()
  54. return self
  55. end
  56.  
  57. function button:setPos(x, y)
  58. self.x = x
  59. self.y = y
  60. return self
  61. end
  62.  
  63. function button:updateTextData()
  64. local missingLength = self.width - string.len(self.text);
  65. if missingLength < 0 then
  66. self.paddedText = self.text
  67. print("[Button] Error, text too big for button")
  68. else
  69. local beg = math.floor(missingLength / 2)
  70. self.paddedText = string.rep(" ", beg) .. self.text .. string.rep(" ", missingLength - beg)
  71. end
  72. if self.height > 1 then
  73. self.emptyRow = string.rep(" ", self.width)
  74. end
  75. end
  76.  
  77. function button:setSize(width, height)
  78. if width < 1 or height < 1 then
  79. print("[Button] Error, invalid size")
  80. return self;
  81. end
  82. self.width = width
  83. self.height = height
  84.  
  85. self:updateTextData()
  86.  
  87. return self
  88. end
  89.  
  90. function button:draw(mon)
  91. mon.setBackgroundColor(colors.red)
  92. local yText = math.floor(self.height / 2 + self.y)
  93.  
  94. for y = self.y, self.y+self.height-1 do
  95. mon.setCursorPos(self.x, y)
  96. if y == yText then
  97. mon.write(self.paddedText)
  98. else
  99. mon.write(self.emptyRow)
  100. end
  101. end
  102.  
  103. return self
  104. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement