gooey.lua 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. Gooey = class()
  2. function Gooey:init()
  3. self.components = {}
  4. self.focused = nil
  5. end
  6. function Gooey:update()
  7. self:call('update')
  8. end
  9. function Gooey:draw(component)
  10. if type(component) == 'string' then component = self:get(component) end
  11. if not component then return end
  12. component.lastDraw = tick
  13. component:render()
  14. end
  15. function Gooey:keypressed(key)
  16. self:call('keypressed', key)
  17. end
  18. function Gooey:keyreleased(key)
  19. self:call('keyreleased', key)
  20. end
  21. function Gooey:mousepressed(mx, my, b)
  22. self.hot = nil
  23. self:call('mousepressed', mx, my, b)
  24. end
  25. function Gooey:mousereleased(mx, my, b)
  26. self:call('mousereleased', mx, my, b)
  27. self.hot = nil
  28. end
  29. function Gooey:resize()
  30. self:call('resize')
  31. end
  32. function Gooey:get(code)
  33. return self.components[code]
  34. end
  35. function Gooey:call(method, ...)
  36. if self.focused then
  37. if self.focused[method] then
  38. if self.focused[method](self.focused, ...) then return end
  39. end
  40. end
  41. local components = table.filter(self.components, function(c) return c.lastDraw and tick - c.lastDraw <= 1 and c ~= self.focused end)
  42. return table.with(components, method, ...)
  43. end
  44. function Gooey:add(class, code, vars)
  45. local component = class()
  46. table.merge(vars, component, true)
  47. component.code = code
  48. component.gooey = self
  49. f.exe(component.activate, component)
  50. self.components[code] = component
  51. return component
  52. end
  53. function Gooey:focus(component)
  54. self.focused = component
  55. end
  56. function Gooey:unfocus()
  57. self.focused = nil
  58. end