80 lines
1.9 KiB

  1. Button = Class {}
  2. function Button:init(tempX, tempY, tempW, tempH, tempText, tempTC, tempBC, tempActive)
  3. -- Position and Dimensions:
  4. self.x = tempX
  5. self.y = tempY
  6. self.w = tempW
  7. self.h = tempH
  8. -- Status:
  9. self.isActive = tempActive
  10. -- Text and Colours:
  11. self.text = tempText
  12. self.colour = {
  13. text = calc.colour(tempTC[1], tempTC[2], tempTC[3]),
  14. background = calc.colour(tempBC[1], tempBC[2], tempBC[3])
  15. }
  16. -- Click Cooldown:
  17. self.cooldownLimit = 30
  18. self.cooldown = 0
  19. end
  20. -- FUNCTIONS
  21. function Button:hover()
  22. local hover = false
  23. local x,y = love.mouse.getPosition()
  24. if x > self.x and x < self.x + self.w and y > self.y and y < self.y + self.h then
  25. hover = true
  26. end
  27. return hover
  28. end
  29. function Button:click()
  30. local click = false
  31. if self:hover() and love.mouse.isDown(1) then
  32. click = true
  33. end
  34. return click
  35. end
  36. -- MAIN
  37. function Button:update(dt)
  38. if self:click() and self.cooldown <= 0 then
  39. self.isActive = not self.isActive
  40. self.cooldown = self.cooldownLimit
  41. end
  42. self.cooldown = self.cooldown - 1
  43. end
  44. function Button:draw()
  45. local x, y, w, h = self.x, self.y, self.w, self.h
  46. local bg, tx = self.colour.background, self.colour.text
  47. -- Hover Effects
  48. if self:hover() and self.cooldown <= 0 then
  49. --[[ Slight Colour Lightup -- broken and idk why qwq
  50. for i = 1, #bg do
  51. bg[i] = bg[i]*1.1
  52. end]]
  53. -- Slight pop up effect (purly visual)
  54. local pop = 1
  55. x, y, w, h = x-pop, y-pop, w+pop*2, h+pop*2
  56. end
  57. -- Draw Background
  58. love.graphics.setColor(bg[1], bg[2], bg[3])
  59. love.graphics.rectangle("fill", x, y, w, h)
  60. -- Draw Text
  61. love.graphics.setFont(font.default)
  62. love.graphics.setColor(tx[1], tx[2], tx[3])
  63. love.graphics.printf(self.text, x, y, w, "center")
  64. end