Advertisement
dlpratte

Untitled

May 26th, 2013
39
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, monitor)
  59.             self.screen = nil
  60.             self.monitor = monitor
  61.         end)
  62.  
  63. function program:screen()
  64.     return self.screen
  65. end
  66.  
  67. function program:setScreen(value)
  68.     self.screen = value
  69.     return self
  70. end
  71.  
  72. function program:draw()
  73.     self.monitor.clear()
  74.     if self.screen ~= nil then
  75.         self.screen:draw()
  76.     end
  77. end
  78. function program:waitEvent()
  79.     local e,side,x,y = os.pullEvent("monitor_touch")
  80.     local handled = false
  81.     if self.screen ~= nil then
  82.         handled = self.screen:handleClickEvent(x,y)
  83.     end
  84.     if not handled then
  85.         print(string.format("[Program] Unhandled click event at %d, %d", x, y))
  86.     end
  87. end
  88.  
  89. -- Button
  90. button = class.class(function(self, text, clickEvent, tag)
  91.             self.text = text
  92.             self.x = 0
  93.             self.y = 0
  94.             self.width = 1
  95.             self.height = 1
  96.             self.paddedText = text
  97.             self.emptyRow = ""
  98.             self.tag = tag
  99.             self.clickEvent = clickEvent
  100.             self.isDisabled = false
  101.         end)
  102.  
  103. function button:pos()
  104.     local t = {}
  105.     t.x = self.x
  106.     t.y = self.y
  107.     return t
  108. end
  109.  
  110. function button:tag()
  111.     return self.tag
  112. end
  113.  
  114. function button:setTag(value)
  115.     self.tag = value
  116.     return self
  117. end
  118.  
  119. function button:isDisabled()
  120.     return self.isDisabled
  121. end
  122.  
  123. function button:setIsDisabled(value)
  124.     self.isDisabled = value
  125.     return self
  126. end
  127.  
  128. function button:handleClickEvent(x, y)
  129.     local hitX = x >= self.x and x <= self.x + self.width
  130.     local hitY = y >= self.y and y <= self.y + self.height
  131.     if hitX and hitY then
  132.         print(string.format("[Button] Button with name %q at position %d, %d was clicked!", self.text, self.x, self.y))
  133.         if self.isDisabled == false and self.clickEvent ~= nil then
  134.             local data = {}
  135.             data.x = x
  136.             data.y = y
  137.             self.clickEvent(self, data)
  138.         end
  139.         return true
  140.     end
  141.    
  142.     return false
  143. end
  144.  
  145. function button:text()
  146.     return self.text
  147. end
  148.  
  149. function button:setText(text)
  150.     self.text = text
  151.     self:updateTextData()
  152.     return self
  153. end
  154.  
  155. function button:setPos(x, y)
  156.     self.x = x
  157.     self.y = y
  158.     return self
  159. end
  160.  
  161. function button:updateTextData()
  162.     local missingLength = self.width - string.len(self.text);
  163.     if missingLength < 0 then
  164.         self.paddedText = self.text
  165.         print("[Button] Error, text too big for button")
  166.     else
  167.         local beg = math.floor(missingLength / 2)
  168.         self.paddedText = string.rep(" ", beg) .. self.text .. string.rep(" ", missingLength - beg)
  169.     end
  170.     if self.height > 1 then
  171.         self.emptyRow = string.rep(" ", self.width)
  172.     end
  173. end
  174.  
  175. function button:setSize(width, height)
  176.     if width < 1 or height < 1 then
  177.         print("[Button] Error, invalid size")
  178.         return self;
  179.     end
  180.     self.width = width
  181.     self.height = height
  182.    
  183.     self:updateTextData()
  184.    
  185.     return self
  186. end
  187.  
  188. function button:draw(mon)
  189.     if self.isDisabled then
  190.         mon.setBackgroundColor(colors.gray)
  191.         mon.setTextColor(colors.lightGray)
  192.     else
  193.         mon.setBackgroundColor(colors.red)
  194.         mon.setTextColor(colors.white)
  195.     end
  196.     local yText = math.floor(self.height / 2 + self.y)
  197.    
  198.     for y = self.y, self.y+self.height-1 do
  199.         mon.setCursorPos(self.x, y)
  200.         if y == yText then
  201.             mon.write(self.paddedText)
  202.         else
  203.             mon.write(self.emptyRow)
  204.         end
  205.     end
  206.    
  207.     return self
  208. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement