Advertisement
UpZone

CC - Button

Jun 16th, 2016
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.14 KB | None | 0 0
  1. ---------------
  2. -- BY UPZONE --
  3. ---------------
  4.  
  5. os.loadAPI("builder\\apis\\Paint")
  6. os.loadAPI("builder\\apis\\Colors")
  7.  
  8. local Button = {
  9. }
  10.  
  11. function Button.__init__(baseClass, button)
  12.   self = button
  13.   setmetatable (self, {__index=Button})
  14.   return self
  15. end
  16.  
  17. setmetatable(Button, {__call=Button.__init__})
  18.  
  19. function Button:getText()
  20.   return self.text
  21. end
  22.  
  23. function Button:getPosition()
  24.   return self.position
  25. end
  26.  
  27. function Button:getPositionX()
  28.   return tonumber(self:getPosition()["x"])
  29. end
  30.  
  31. function Button:getPositionY()
  32.   return tonumber(self:getPosition()["y"])
  33. end
  34.  
  35. function Button:raiseOnClick()
  36.   self:setHighlighted(true)
  37.   self:onClick()
  38.     sleep(.075)
  39.   self:setHighlighted(false)
  40. end
  41.  
  42. function Button:getSize()
  43.   return self.size
  44. end
  45.  
  46. function Button:getSizeWidth()
  47.   return tonumber(self:getSize()["width"])
  48. end
  49.  
  50. function Button:getSizeHeight()
  51.   return tonumber(self:getSize()["height"])
  52. end
  53.  
  54. function Button:intersects(posX, posY)
  55.   x = self:getPositionX()
  56.   y = self:getPositionY()
  57.   width = self:getSizeWidth()
  58.   height = self:getSizeHeight()
  59.   endX = x + width - 1
  60.   endY = y + height - 1
  61.  
  62.   intersectsX = ( posX >= x and posX <= endX )
  63.   intersectsY = ( posY >= y and posY <= endY )
  64.  
  65.   return ( intersectsX and intersectsY )
  66. end
  67.  
  68. function Button:getColors()
  69.   return self.colors
  70. end
  71.  
  72. function Button:getTextColor()
  73.   return self:getColors()["textColor"]
  74. end
  75.  
  76. function Button:getHighlightedTextColor()
  77.   return self:getColors()["highlightedTextColor"]
  78. end
  79.  
  80. function Button:getBackgroundColor()
  81.   return self:getColors()["backgroundColor"]
  82. end
  83.  
  84. function Button:getHighlightedBackgroundColor()
  85.   return self:getColors()["highlightedBackgroundColor"]
  86. end
  87.  
  88. function Button:setHighlighted(highlighted)
  89.   bgColor = nil
  90.   txtColor = nil
  91.  
  92.   if highlighted then
  93.     bgColor = self:getHighlightedBackgroundColor()
  94.     txtColor = self:getHighlightedTextColor()
  95.   else
  96.     bgColor = self:getBackgroundColor()
  97.     txtColor = self:getTextColor()
  98.   end
  99.  
  100.   -- background
  101.   Paint.fill(self:getPositionX(), self:getPositionY(), ( self:getPositionX() + self:getSizeWidth() - 1), ( self:getPositionY() + self:getSizeHeight() - 1), bgColor)
  102.  
  103.   textLength = string.len(self:getText())
  104.   offsetLeft = math.floor((self:getSizeWidth() - textLength) / 2)
  105.   offsetTop = math.floor((self:getSizeHeight() - 1) / 2)
  106.  
  107.   -- text
  108.   term.setCursorPos(self:getPositionX() + offsetLeft, self:getPositionY() + offsetTop)
  109.   term.blit(self:getText(), string.rep(txtColor, textLength), string.rep(bgColor, textLength))
  110. end
  111.  
  112.  
  113.  
  114. function Button:draw()
  115.   self:setHighlighted(false)
  116. end
  117.  
  118. -- Factory
  119. function create(text, x, y, width, height, onClick, textColor, backgroundColor, highlightedTextColor, highlightedBackgroundColor)
  120.   return Button({
  121.     text = text,
  122.     position = {
  123.       x = x,
  124.       y = y
  125.     },
  126.     size = {
  127.       width = width,
  128.       height = height
  129.     },
  130.     onClick = onClick,
  131.     colors = {
  132.       textColor = textColor,
  133.       backgroundColor = backgroundColor,
  134.       highlightedTextColor = highlightedTextColor,
  135.       highlightedBackgroundColor = highlightedBackgroundColor
  136.     }})
  137. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement