button.lua 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. return function(BOXUI)
  2. local utils = BOXUI.utils
  3. local M = utils.newclass("button")
  4. local mt = M
  5. M.init = function(self, x, y, width, height)
  6. self.theme = BOXUI.theme
  7. self.x, self.y = x, y
  8. self.width, self.height = width, height
  9. self.checked = false
  10. self.checkbox = false
  11. self.radiogroup = nil
  12. self.active = nil
  13. self.selected = nil
  14. self.onClick = nil
  15. self.font = love.graphics.getFont()
  16. self.draw = self.theme.draw.button
  17. end
  18. M.refresh = function(self)
  19. self.draw = self.theme.draw.button
  20. end
  21. M.wheelmoved = function(self, x, y, dx, dy)
  22. end
  23. M.mousemoved = function(self, x, y, button)
  24. --self.active = true
  25. end
  26. M.mousepressed = function(self, x, y, button)
  27. self.selected = button == 1 and self.active
  28. end
  29. M.mousereleased = function(self, x, y, button)
  30. if self.active and self.selected then
  31. if self.onClick then self.onClick(self) end
  32. if self.radiogroup then
  33. for k, v in pairs(self.radiogroup) do
  34. v.checked = false
  35. end
  36. self.checked = true
  37. elseif self.checkbox then
  38. self.checked = not self.checked
  39. end
  40. end
  41. self.selected = false
  42. end
  43. M.update = function(self, dt)
  44. end
  45. M.focus = function(self) self.active = true end
  46. M.unfocus = function(self) self.active = false end
  47. BOXUI.add(M)
  48. end