base.lua 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. local base = {}
  2. function base:new( parent )
  3. local instance = {}
  4. setmetatable( instance, self.meta )
  5. instance:setparent( parent )
  6. instance.visible = true
  7. instance.children = {}
  8. instance.AABB = {0,0,0,0}
  9. instance.x = 0
  10. instance.y = 0
  11. instance.w = 0
  12. instance.h = 0
  13. instance.hoverstate = 'off'
  14. goo.newinstance( instance, parent )
  15. instance:init( parent )
  16. return instance
  17. end
  18. function base:setparent( parent )
  19. if not parent then return end
  20. if self.parent then self.parent:removechild( self ) end
  21. self.parent = parent
  22. self.parent:addchild( self )
  23. end
  24. function base:addchild( child )
  25. table.insert( self.children, child )
  26. end
  27. function base:removechild( child )
  28. for k, existing_child in ipairs( self.children ) do
  29. if existing_child == child then table.remove( self.children, k ) end
  30. end
  31. end
  32. function base:init()
  33. end
  34. function base:update(dt)
  35. self:updatebounds()
  36. local mx, my = love.mouse.getPosition()
  37. local m_left = love.mouse.isDown('l')
  38. if self:inbounds( mx, my ) then
  39. if self.hoverstate ~= 'click' then
  40. self.hoverstate = 'over'
  41. end
  42. else
  43. self.hoverstate = 'off'
  44. end
  45. end
  46. function base:draw() end
  47. function base:drawall()
  48. self:draw()
  49. for k, child in ipairs( self.children ) do
  50. child:drawall()
  51. end
  52. end
  53. function base:mousepressed() self.hoverstate = 'click' end
  54. function base:mousereleased() self.hoverstate = 'off' end
  55. function base:keypressed() end
  56. function base:keyreleased() end
  57. function base:inbounds( x, y )
  58. local ax, ay = self.AABB[1], self.AABB[2]
  59. local bx, by = self.AABB[3], self.AABB[4]
  60. if x > ax and x < bx and y > ay and y < by then
  61. return true
  62. else
  63. return false
  64. end
  65. end
  66. function base:setbounds( ax, ay, bx, by )
  67. self.AABB = {ax,ay,bx,by}
  68. self.x = ax
  69. self.y = ay
  70. self.w = bx-ax
  71. self.h = by-ay
  72. end
  73. function base:updatebounds()
  74. local ax, ay = self.x, self.y
  75. local bx, by = self.x + self.w, self.y + self.h
  76. self.AABB = {ax,ay,bx,by}
  77. end
  78. function base:setpos( x, y )
  79. self.x = x
  80. self.y = y
  81. end
  82. function base:setsize( w, h )
  83. self.w = w
  84. self.h = h
  85. end
  86. function base:setcolor( colorname )
  87. love.graphics.setColor( unpack( goo.skin[ self.name ][ colorname ] ) )
  88. end
  89. function base:getskinvar( varname )
  90. return goo.skin[ self.name ][ varname ]
  91. end
  92. return base