Advertisement
dlpratte

Untitled

May 26th, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.82 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.             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.     return self
  36. end
  37.  
  38. function screen:handleClickEvent(x, y)
  39.     local arrLength = #self.clickables
  40.     local handled = false
  41.     for i=1, arrLength do
  42.         handled = self.clickables[i]:handleClickEvent(x, y)
  43.         if handled then
  44.             break
  45.         end
  46.     end
  47.    
  48.     return handled
  49. end
  50.  
  51. function screen:draw()
  52.     local arrLength = #self.drawables
  53.     for i=1, arrLength do
  54.         self.drawables[i]:draw(self.monitor)
  55.     end
  56. end
  57.  
  58. -- Program
  59. program = class.class(function(self, monitor)
  60.             self.screen = nil
  61.             self.monitor = monitor
  62.         end)
  63.  
  64. function program:screen()
  65.     return self.screen
  66. end
  67.  
  68. function program:setScreen(value)
  69.     self.screen = value
  70.     return self
  71. end
  72.  
  73. function program:draw()
  74.     self.monitor.setBackgroundColor(colors.black)
  75.     self.monitor.setTextColor(colors.white)
  76.     self.monitor.clear()
  77.     if self.screen ~= nil then
  78.         self.screen:draw()
  79.     end
  80. end
  81. function program:waitEvent()
  82.     local e,side,x,y = os.edit u("monitor_touch")
  83.     local handled = false
  84.     if self.screen ~= nil then
  85.         handled = self.screen:handleClickEvent(x,y)
  86.     end
  87.     if not handled then
  88.         print(string.format("[Program] Unhandled click event at %d, %d", x, y))
  89.     end
  90. end
  91.  
  92. -- Button
  93. button = class.class(function(self, text, clickEvent, tag)
  94.             self.text = text
  95.             self.x = 0
  96.             self.y = 0
  97.             self.width = 1
  98.             self.height = 1
  99.             self.paddedText = text
  100.             self.emptyRow = ""
  101.             self.tag = tag
  102.             self.clickEvent = clickEvent
  103.             self.isDisabled = false
  104.         end)
  105.  
  106. function button:pos()
  107.     return self.x, self.y
  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
  209.  
  210. -- Text
  211. text = class.class(function(self, text, x, y)
  212.             self.x = x
  213.             self.y = y
  214.             self.text = text
  215.         end)
  216.  
  217. function text:text()
  218.     return self.text
  219. end
  220.  
  221. function text:setText(value)
  222.     self.text = value
  223.     return self
  224. end
  225.  
  226. function text:pos()
  227.     return self.x, self.y
  228. end
  229.  
  230. function text:setPos(x, y)
  231.     self.x = x
  232.     self.y = y
  233.     return self
  234. end
  235.  
  236. function text:handleClickEvent(x, y)
  237.     return false
  238. end
  239.  
  240. function text:draw(mon)
  241.     mon.setBackgroundColor(colors.black)
  242.     mon.setTextColor(colors.white)
  243.     mon.setCursorPos(self.x, self.y)
  244.     mon.write(self.text)
  245. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement