button.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. local g = love.graphics
  2. require 'lib/component'
  3. Button = extend(Component)
  4. function Button:activate()
  5. self.hoverActive = false
  6. self.hoverFactor = 0
  7. self.prevHoverFactor = 0
  8. self.prevHoverFade = 0
  9. self.hoverX = nil
  10. self.hoverY = nil
  11. self.hoverDistance = 0
  12. self.hoverFade = 0
  13. self.disabled = false
  14. end
  15. function Button:update()
  16. self.prevHoverFactor = self.hoverFactor
  17. self.prevHoverFade = self.hoverFade
  18. if self.hoverActive then
  19. self.hoverFactor = math.lerp(self.hoverFactor, 1, math.min(8 * ls.tickrate, 1))
  20. if self.hoverFactor > .999 then
  21. self.hoverFade = math.min(self.hoverFade + ls.tickrate, 1)
  22. end
  23. else
  24. self.hoverFactor = 0
  25. self.hoverFade = 0
  26. end
  27. end
  28. function Button:mousepressed(mx, my, b)
  29. if b == 'l' and self:contains(mx, my) and not self.disabled then
  30. self.gooey.hot = self
  31. end
  32. end
  33. function Button:mousereleased(mx, my, b)
  34. if b == 'l' and self.gooey.hot == self and self:contains(mx, my) and not self.disabled then
  35. self:emit('click')
  36. ctx.sound:play('juju1', function(sound) sound:setPitch(1) end)
  37. end
  38. end
  39. function Button:render()
  40. local x, y, w, h = unpack(self.geometry())
  41. local text = self.text
  42. local mx, my = self:getMousePosition()
  43. local hover = self:contains(mx, my)
  44. local active = hover and love.mouse.isDown('l') and self.gooey.hot == self
  45. -- Button
  46. local button = data.media.graphics.menu.button
  47. local buttonActive = data.media.graphics.menu.buttonActive
  48. local diff = (button:getHeight() - buttonActive:getHeight())
  49. local image = active and buttonActive or button
  50. local bgy = y + h
  51. local yscale = h / button:getHeight()
  52. g.setColor(0, 0, 0, 85)
  53. g.rectangle('fill', x, y, w, h)
  54. local fade = math.lerp(self.prevHoverFade, self.hoverFade, ls.accum / ls.tickrate)
  55. g.setColor(0, 0, 0, 200)
  56. g.setLineWidth(2)
  57. local xx, yy = math.round(x) + .5, math.round(y) + .5
  58. w, h = math.floor(w), math.floor(h)
  59. g.line(xx, yy + h, xx + w, yy + h)
  60. g.line(xx + w, yy, xx + w, yy + h)
  61. g.setLineWidth(1)
  62. if hover then
  63. if not self.hoverActive then
  64. self.hoverX = mx
  65. self.hoverY = my
  66. local d = math.distance
  67. self.hoverDistance = math.max(d(mx, my, x, y), d(mx, my, x + w, y), d(mx, my, x, y + h), d(mx, my, x + w, y + h))
  68. ctx.sound:play('juju1', function(sound) sound:setPitch(.75) end)
  69. end
  70. g.setColor(255, 255, 255)
  71. g.setStencil(function()
  72. g.rectangle('fill', x, y, w, h)
  73. end)
  74. local factor = math.lerp(self.prevHoverFactor, self.hoverFactor, ls.accum / ls.tickrate)
  75. g.setColor(255, 255, 255, 40 * (1 - fade))
  76. g.setBlendMode('alpha')
  77. g.circle('fill', self.hoverX, self.hoverY, factor * self.hoverDistance)
  78. g.setBlendMode('alpha')
  79. g.setStencil()
  80. self.hoverActive = true
  81. else
  82. self.hoverActive = false
  83. end
  84. -- Text
  85. if active then y = y + 2 end
  86. g.setFont('mesmerize', h * .55)
  87. g.setColor(0, 0, 0, 100)
  88. g.printCenter(text, x + w / 2 + 1, y + h / 2 + 1)
  89. g.setColor(255, 255, 255)
  90. g.printCenter(text, x + w / 2, y + h / 2)
  91. end
  92. function Button:contains(x, y)
  93. return math.inside(x, y, unpack(self.geometry())) and not self.disabled
  94. end