panel.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. -- PANEL
  2. goo.panel = class('goo panel', goo.object)
  3. goo.panel.image = {}
  4. function goo.panel:initialize(parent)
  5. super.initialize(self,parent)
  6. self.title = "title"
  7. self.close = goo.close:new(self)
  8. self.dragState = false
  9. self.draggable = true
  10. end
  11. function goo.panel:setSkin()
  12. goo.panel.image.corner = love.graphics.newImage(goo.skin..'box_corner.png')
  13. goo.panel.image.edge = love.graphics.newImage(goo.skin..'box_edge.png')
  14. end
  15. function goo.panel:update(dt)
  16. super.update(self,dt)
  17. if self.dragState and self.draggable then
  18. self.x = love.mouse.getX() - self.dragOffsetX
  19. self.y = love.mouse.getY() - self.dragOffsetY
  20. --self:updateBounds()
  21. end
  22. end
  23. function goo.panel:drawbox(x,y)
  24. local cornerH = self.image.corner:getHeight()
  25. local cornerW = self.image.corner:getWidth()
  26. local edgeH = self.image.edge:getHeight()
  27. local edgeW = self.image.edge:getWidth()
  28. self:setColor( self.style.borderColor )
  29. love.graphics.draw( self.image.corner, -cornerH, -cornerH )
  30. love.graphics.draw( self.image.corner, self.w+cornerH, -cornerH, math.pi/2 )
  31. love.graphics.draw( self.image.corner, self.w+cornerH, self.h+cornerH, math.pi )
  32. love.graphics.draw( self.image.corner, -cornerH, self.h+cornerH, 3*math.pi/2 )
  33. love.graphics.draw( self.image.edge, 0, -edgeH, 0, self.w, 1)
  34. love.graphics.draw( self.image.edge, self.w+edgeH, 0, math.pi/2, self.h, 1)
  35. love.graphics.draw( self.image.edge, self.w, self.h+edgeH, math.pi, self.w, 1)
  36. love.graphics.draw( self.image.edge, -edgeH, self.h, 3*math.pi/2, self.h, 1)
  37. self:setColor( self.style.backgroundColor )
  38. love.graphics.rectangle('fill', 0, 0, self.w, self.h)
  39. end
  40. function goo.panel:draw( x, y )
  41. super.draw(self)
  42. self:drawbox()
  43. self:setColor( self.style.separatorColor )
  44. love.graphics.setLine(1, 'smooth')
  45. love.graphics.line( 0, 8, self.w, 8)
  46. self:setColor( self.style.titleColor )
  47. love.graphics.setFont( self.style.titleFont )
  48. love.graphics.print( self.title, 0, -12)
  49. end
  50. function goo.panel:mousepressed(x,y,button)
  51. super.mousepressed(self,x,y,button)
  52. if self.hoverState then
  53. if not self.dragState then
  54. self.dragOffsetX = x - self.x
  55. self.dragOffsetY = y - self.y
  56. end
  57. self.dragState = true
  58. -- Move to top.
  59. if self.z < #self.parent.children then
  60. self:removeFromParent()
  61. self:addToParent( self.parent )
  62. end
  63. end
  64. end
  65. function goo.panel:mousereleased(x,y,button)
  66. end
  67. function goo.panel:setTitle( title )
  68. self.title = title
  69. end
  70. function goo.panel:setPos( x, y )
  71. super.setPos(self, x, y)
  72. self:setClosePos()
  73. self:updateBounds()
  74. end
  75. function goo.panel:setSize( w, h )
  76. super.setSize(self, w, h)
  77. self:setClosePos()
  78. self:updateBounds()
  79. end
  80. function goo.panel:setClosePos()
  81. local a = self.image.edge:getHeight()/2
  82. self.close:setPos( self.w - 4, -a + 2 )
  83. end
  84. function goo.panel:setDraggable( draggable )
  85. self.draggable = draggable
  86. end
  87. function goo.panel:showCloseButton( bool )
  88. self.close:setVisible(bool)
  89. end
  90. function goo.panel:updateBounds()
  91. local edgeH = goo.panel.image.edge:getHeight()/2
  92. local x, y = self:getAbsolutePos()
  93. self.bounds.x1 = x - edgeH
  94. self.bounds.y1 = y - edgeH
  95. self.bounds.x2 = x + self.w + edgeH
  96. self.bounds.y2 = y + self.h + edgeH
  97. end
  98. return goo.panel