Advertisement
dlpratte

Untitled

May 26th, 2013
63
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, tag)
  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.tag = tag
  98.             self.clickEvent = clickEvent
  99.             self.isDisabled = false
  100.         end)
  101.  
  102. function button:pos()
  103.     local t = {}
  104.     t.x = self.x
  105.     t.y = self.y
  106.     return t
  107. end
  108.  
  109. function button:tag()
  110.     return self.tag
  111. end
  112.  
  113. function button:setTag(value)
  114.     self.tag = value
  115. end
  116.  
  117. function button:isDisabled()
  118.     return self.isDisabled
  119. end
  120.  
  121. function button:setIsDisabled(value)
  122.     self.isDisabled = value
  123.     return self
  124. end
  125.  
  126. function button:handleClickEvent(x, y)
  127.     local hitX = x >= self.x and x <= self.x + self.width
  128.     local hitY = y >= self.y and y <= self.y + self.height
  129.     if hitX and hitY then
  130.         print(string.format("[Button] Button with name %q at position %d, %d was clicked!", self.text, self.x, self.y))
  131.         if self.isDisabled == false and self.clickEvent ~= nil then
  132.             local data = {}
  133.             data.x = x
  134.             data.y = y
  135.             self.clickEvent(self, data)
  136.         end
  137.         return true
  138.     end
  139.    
  140.     return false
  141. end
  142.  
  143. function button:text()
  144.     return self.text
  145. end
  146.  
  147. function button:setText(text)
  148.     self.text = text
  149.     self:updateTextData()
  150.     return self
  151. end
  152.  
  153. function button:setPos(x, y)
  154.     self.x = x
  155.     self.y = y
  156.     return self
  157. end
  158.  
  159. function button:updateTextData()
  160.     local missingLength = self.width - string.len(self.text);
  161.     if missingLength < 0 then
  162.         self.paddedText = self.text
  163.         print("[Button] Error, text too big for button")
  164.     else
  165.         local beg = math.floor(missingLength / 2)
  166.         self.paddedText = string.rep(" ", beg) .. self.text .. string.rep(" ", missingLength - beg)
  167.     end
  168.     if self.height > 1 then
  169.         self.emptyRow = string.rep(" ", self.width)
  170.     end
  171. end
  172.  
  173. function button:setSize(width, height)
  174.     if width < 1 or height < 1 then
  175.         print("[Button] Error, invalid size")
  176.         return self;
  177.     end
  178.     self.width = width
  179.     self.height = height
  180.    
  181.     self:updateTextData()
  182.    
  183.     return self
  184. end
  185.  
  186. function button:draw(mon)
  187.     if self.isDisabled then
  188.         mon.setBackgroundColor(colors.gray)
  189.         mon.setTextColor(colors.lightGray)
  190.     else
  191.         mon.setBackgroundColor(colors.red)
  192.         mon.setTextColor(colors.white)
  193.     end
  194.     local yText = math.floor(self.height / 2 + self.y)
  195.    
  196.     for y = self.y, self.y+self.height-1 do
  197.         mon.setCursorPos(self.x, y)
  198.         if y == yText then
  199.             mon.write(self.paddedText)
  200.         else
  201.             mon.write(self.emptyRow)
  202.         end
  203.     end
  204.    
  205.     return self
  206. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement