Advertisement
dlpratte

Untitled

May 26th, 2013
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 = #self.drawables + 1
  15. self.drawables[i] = component
  16.  
  17. return self
  18. end
  19.  
  20. function screen:draw()
  21. local arrLength = #self.drawables
  22. for i=1, 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. self.isDisabled = false
  39. end)
  40.  
  41. function button:pos()
  42. local t = {}
  43. t.x = self.x
  44. t.y = self.y
  45. return t
  46. end
  47.  
  48. function button:isDisabled()
  49. return self.isDisabled
  50. end
  51.  
  52. function button:isDisabled(value)
  53. self.isDisabled = true
  54. end
  55.  
  56. function button:text()
  57. return self.text
  58. end
  59.  
  60. function button:setText(text)
  61. self.text = text
  62. self:updateTextData()
  63. return self
  64. end
  65.  
  66. function button:setPos(x, y)
  67. self.x = x
  68. self.y = y
  69. return self
  70. end
  71.  
  72. function button:updateTextData()
  73. local missingLength = self.width - string.len(self.text);
  74. if missingLength < 0 then
  75. self.paddedText = self.text
  76. print("[Button] Error, text too big for button")
  77. else
  78. local beg = math.floor(missingLength / 2)
  79. self.paddedText = string.rep(" ", beg) .. self.text .. string.rep(" ", missingLength - beg)
  80. end
  81. if self.height > 1 then
  82. self.emptyRow = string.rep(" ", self.width)
  83. end
  84. end
  85.  
  86. function button:setSize(width, height)
  87. if width < 1 or height < 1 then
  88. print("[Button] Error, invalid size")
  89. return self;
  90. end
  91. self.width = width
  92. self.height = height
  93.  
  94. self:updateTextData()
  95.  
  96. return self
  97. end
  98.  
  99. function button:draw(mon)
  100. if self.isDisabled then
  101. mon.setBackgroundColor(colors.gray)
  102. mon.setTextColor(colors.lightGray)
  103. else
  104. mon.setBackgroundColor(colors.red)
  105. mon.setTextColor(color.white)
  106. end
  107. local yText = math.floor(self.height / 2 + self.y)
  108.  
  109. for y = self.y, self.y+self.height-1 do
  110. mon.setCursorPos(self.x, y)
  111. if y == yText then
  112. mon.write(self.paddedText)
  113. else
  114. mon.write(self.emptyRow)
  115. end
  116. end
  117.  
  118. return self
  119. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement