Advertisement
dlpratte

Untitled

May 26th, 2013
62
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. self.clickables = {}
  8. end)
  9.  
  10. function screen:addDrawable(component)
  11. if component == nil then
  12. print("[Screen] Not adding nil drawable.")
  13. return self
  14. end
  15. local i = #self.drawables + 1
  16. self.drawables[i] = component
  17.  
  18. return self
  19. end
  20.  
  21. function screen:addClickable(component)
  22. if component == nil then
  23. print("[Screen] Not adding nil clickable.")
  24. return self
  25. end
  26. local i = #self.clickables + 1
  27. self.clickables[i] = component
  28.  
  29. return self
  30. end
  31.  
  32. function screen:addControl(component)
  33. self:addDrawable(component)
  34. self:addClickable(component)
  35. end
  36.  
  37. function screen:handleClickEvent(x, y)
  38. local arrLength = #self.clickables
  39. local handled = false
  40. for i=1, arrLength do
  41. handled = self.clickables[i]:handleClickEvent(x, y)
  42. if handled then
  43. break
  44. end
  45. end
  46.  
  47. return handled
  48. end
  49.  
  50. function screen:draw()
  51. local arrLength = #self.drawables
  52. for i=1, arrLength do
  53. self.drawables[i]:draw(self.monitor)
  54. end
  55. end
  56.  
  57. -- Program
  58. program = class.class(function(self)
  59. self.screen = nil
  60. end)
  61.  
  62. function program:screen()
  63. return self.screen
  64. end
  65.  
  66. function program:setScreen(value)
  67. self.screen = value
  68. return self
  69. end
  70.  
  71. function program:draw()
  72. mon.clear()
  73. if self.screen != nil then
  74. self.screen:draw()
  75. end
  76. end
  77. function program:waitEvent()
  78. local e,side,x,y = os.pullEvent("monitor_touch")
  79. local handled = false
  80. if self.screen != nil then
  81. handled = self.screen:handleClickEvent(x,y)
  82. end
  83. if not handled then
  84. print(string.format("[Program] Unhandled click event at %d, %d", x, y))
  85. end
  86. end
  87.  
  88. -- Button
  89. button = class.class(function(self, text, clickEvent)
  90. self.text = text
  91. self.x = 0
  92. self.y = 0
  93. self.width = 1
  94. self.height = 1
  95. self.paddedText = text
  96. self.emptyRow = ""
  97. self.clickEvent = clickEvent
  98. self.isDisabled = false
  99. end)
  100.  
  101. function button:pos()
  102. local t = {}
  103. t.x = self.x
  104. t.y = self.y
  105. return t
  106. end
  107.  
  108. function button:isDisabled()
  109. return self.isDisabled
  110. end
  111.  
  112. function button:setIsDisabled(value)
  113. self.isDisabled = value
  114. return self
  115. end
  116.  
  117. function button:handleClickEvent(x, y)
  118. local hitX = x >= self.x and x <= self.x + self.width
  119. local hitY = y >= self.y and y <= self.y + self.height
  120. if hitX and hitY then
  121.  
  122. return true
  123. end
  124.  
  125. return false
  126. end
  127.  
  128. function button:text()
  129. return self.text
  130. end
  131.  
  132. function button:setText(text)
  133. self.text = text
  134. self:updateTextData()
  135. return self
  136. end
  137.  
  138. function button:setPos(x, y)
  139. self.x = x
  140. self.y = y
  141. return self
  142. end
  143.  
  144. function button:updateTextData()
  145. local missingLength = self.width - string.len(self.text);
  146. if missingLength < 0 then
  147. self.paddedText = self.text
  148. print("[Button] Error, text too big for button")
  149. else
  150. local beg = math.floor(missingLength / 2)
  151. self.paddedText = string.rep(" ", beg) .. self.text .. string.rep(" ", missingLength - beg)
  152. end
  153. if self.height > 1 then
  154. self.emptyRow = string.rep(" ", self.width)
  155. end
  156. end
  157.  
  158. function button:setSize(width, height)
  159. if width < 1 or height < 1 then
  160. print("[Button] Error, invalid size")
  161. return self;
  162. end
  163. self.width = width
  164. self.height = height
  165.  
  166. self:updateTextData()
  167.  
  168. return self
  169. end
  170.  
  171. function button:draw(mon)
  172. if self.isDisabled then
  173. mon.setBackgroundColor(colors.gray)
  174. mon.setTextColor(colors.lightGray)
  175. else
  176. mon.setBackgroundColor(colors.red)
  177. mon.setTextColor(colors.white)
  178. end
  179. local yText = math.floor(self.height / 2 + self.y)
  180.  
  181. for y = self.y, self.y+self.height-1 do
  182. mon.setCursorPos(self.x, y)
  183. if y == yText then
  184. mon.write(self.paddedText)
  185. else
  186. mon.write(self.emptyRow)
  187. end
  188. end
  189.  
  190. return self
  191. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement