ui.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. require(ui_scrdir.."ui_element")
  2. require(ui_scrdir.."ui_uielement")
  3. require(ui_scrdir.."ui_button")
  4. require(ui_scrdir.."ui_label")
  5. require(ui_scrdir.."ui_groupbox")
  6. require(ui_scrdir.."ui_spin")
  7. require(ui_scrdir.."ui_shape")
  8. require(ui_scrdir.."ui_checkbox")
  9. require(ui_scrdir.."ui_pageswitch")
  10. require(ui_scrdir.."ui_radiobutton")
  11. require(ui_scrdir.."ui_progressbar")
  12. require(ui_scrdir.."ui_particleemitter")
  13. require(ui_scrdir.."ui_imagecollection")
  14. require(ui_scrdir.."ui_listbox")
  15. require(ui_scrdir.."ui_image")
  16. require(ui_scrdir.."ui_collection")
  17. require(ui_scrdir.."ui_timer")
  18. local l_gfx = love.graphics
  19. -- define UIManager "class": the one that handles all the buttons and labels,draws them, updates them and handles inputs
  20. UIManager = {}
  21. UIManager.__index = UIManager
  22. function UIManager:new()
  23. local self = {}
  24. setmetatable(self,UIManager)
  25. self.items = {}
  26. self.drawList = {}
  27. self.updateList = {}
  28. self.inputList = {}
  29. return self
  30. end
  31. function UIManager:addItem(item)
  32. table.insert(self.items,item)
  33. if item.updateable == true then
  34. table.insert(self.updateList,item)
  35. end
  36. if item.drawable == true then
  37. table.insert(self.drawList,item)
  38. end
  39. if item.input == true then
  40. table.insert(self.inputList,item)
  41. end
  42. item:oncreate()
  43. return item
  44. end
  45. function UIManager:getItem(name,deep)
  46. local c = #self.items
  47. if c>0 then
  48. for i=1,c do
  49. if self.items[i]:getName() == name then
  50. return self.items[i]
  51. elseif self.items[i].items ~= nil and deep == true then
  52. self.items[i]:getItem(name,deep)
  53. end
  54. end
  55. end
  56. return nil
  57. end
  58. -- element deep search
  59. function UIManager:draw()
  60. local dl = self.drawList
  61. local c = table.getn(dl)
  62. if c>0 then
  63. for i=1,c do
  64. if dl[i].visible == true then dl[i]:draw() end
  65. end
  66. end
  67. end
  68. function UIManager:update(dt)
  69. local ul = self.updateList
  70. local c = table.getn(ul)
  71. if c>0 then
  72. for i=1,c do
  73. if ul[i].active == true then ul[i]:update(dt) end
  74. end
  75. end
  76. end
  77. function UIManager:mousemoved(x,y,dx,dy)
  78. local ill = self.inputList
  79. for i,v in ipairs(ill) do
  80. if v.active == true then v:mousemoved(x,y,dx,dy) end
  81. end
  82. end
  83. function UIManager:mousepressed(x,y,b)
  84. for i,v in ipairs(self.inputList) do
  85. if v.active == true then v:mousepressed(x,y,b) end
  86. end
  87. end
  88. function UIManager:mousereleased(x,y,b)
  89. for i,v in ipairs(self.inputList) do
  90. if v.active == true then v:mousereleased(x,y,b) end
  91. end
  92. end
  93. function UIManager:wheelmoved(x,y)
  94. for i,v in ipairs(self.inputList) do
  95. if v.active == true then v:wheelmoved(x,y) end
  96. end
  97. end
  98. function UIManager:keypressed(key,isrepeat)
  99. for i,v in ipairs(self.inputList) do
  100. if v.active == true then v:keypressed(key,isrepeat) end
  101. end
  102. end
  103. function UIManager:keyreleased(key)
  104. for i,v in ipairs(self.inputList) do
  105. if v.active == true then v:keyreleased(key) end
  106. end
  107. end
  108. function UIManager:textinput(t)
  109. for i,v in ipairs(self.inputList) do
  110. if v.active == true then v:textinput(t) end
  111. end
  112. end
  113. function UIManager:onchangewindow(w,h)
  114. for i,v in ipairs(self.items) do
  115. v:onchangewindow(w,h)
  116. end
  117. end
  118. function UIManager:init()
  119. uistartup(self)
  120. end