ui_groupbox.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. local l_gfx = love.graphics
  2. -- Groupbox is a container element. it can hold other ui elements, they will move with it on every SetPosition call if its isContainer field is true
  3. -- also has caption, can be drawn borderless (showBorder = false) and can display corners
  4. -- this element also will try to update updateables, draw drawables and handle inputs if added elements are ones
  5. GroupBox = {}
  6. GroupBox.__index = GroupBox
  7. GroupBox.ident = "ui_groupbox"
  8. GroupBox.caption = "GroupBox"
  9. GroupBox.updateable = true
  10. GroupBox.stenciled = false
  11. GroupBox.name = "GroupBox"
  12. GroupBox.showBorder = true
  13. GroupBox.isContainer = true
  14. GroupBox.caption_xpad = 4
  15. GroupBox.caption_ypad = -16
  16. GroupBox.captionAlign = "center"
  17. GroupBox.colorBcgFill = {0,32,32,128}
  18. GroupBox.showBackground = false
  19. GroupBox.input = true
  20. function GroupBox:new(name)
  21. local self = {}
  22. setmetatable(self,GroupBox)
  23. self.items = {}
  24. self.drawList = {}
  25. self.updateList = {}
  26. self.inputList = {}
  27. -- topleft,topright,bottomright,bottomleft corners
  28. self.cornerLT = false
  29. self.cornerRT = false
  30. self.cornerRB = false
  31. self.cornerLB = false
  32. if name ~= nil then self.name = name end
  33. return self
  34. end
  35. -- Group box is a mix between UIManager and UIElement. You could make child of either, but i decided to redefine UIManager's methods instead of UIElement's fields
  36. setmetatable(GroupBox,{__index = UIElement})
  37. function GroupBox:draw()
  38. local r,g,b,a = l_gfx.getColor()
  39. if self.showBackground == true then
  40. l_gfx.setColor(self.colorBcgFill)
  41. l_gfx.rectangle("fill",self.x,self.y,self.w,self.h)
  42. end
  43. if self.showBorder == true then
  44. l_gfx.setColor(self.colorLine)
  45. l_gfx.rectangle("line",self.x,self.y,self.w,self.h)
  46. else
  47. local c_width,c_height = self.w/2,self.h/2
  48. if self.cornerLT == true then
  49. l_gfx.setColor(self.colorLine)
  50. l_gfx.line(self.x,self.y,self.x,self.y+c_height)
  51. l_gfx.line(self.x,self.y,self.x+c_width,self.y)
  52. end
  53. end
  54. l_gfx.setColor(self.colorFont)
  55. l_gfx.printf(self.caption,self.x,self.y+self.caption_ypad,self.w,self.captionAlign)
  56. local dl = self.drawList
  57. local c = table.getn(dl)
  58. if c>0 then
  59. for i=1,c do
  60. if dl[i].visible == true then dl[i]:draw() end
  61. end
  62. end
  63. l_gfx.setColor(r,g,b,a)
  64. end
  65. function GroupBox:update(dt)
  66. local ul = self.updateList
  67. local c = table.getn(ul)
  68. if c>0 then
  69. for i=1,c do
  70. if ul[i].active == true then ul[i]:update(dt) end
  71. end
  72. end
  73. end
  74. function GroupBox:mousemoved(x,y,dx,dy)
  75. local ill = self.inputList
  76. for i,v in ipairs(ill) do
  77. if v.active == true then v:mousemoved(x,y,dx,dy) end
  78. end
  79. end
  80. function GroupBox:mousepressed(x,y,b)
  81. local r
  82. if self:isMouseOver(x,y) == true then
  83. self:click(b)
  84. end
  85. for i,v in ipairs(self.inputList) do
  86. if v.active == true then local tr = v:mousepressed(x,y,b) if tr~=nil then r = tr end end
  87. end
  88. return r
  89. end
  90. function GroupBox:wheelmoved(x,y)
  91. local r
  92. for i,v in ipairs(self.inputList) do
  93. if v.active == true then local tr = v:wheelmoved(x,y) if tr~=nil then r = tr end end
  94. end
  95. return r
  96. end
  97. function GroupBox:mousereleased(x,y,b)
  98. local r
  99. if self:isMouseOver(x,y) == true then
  100. self:unclick(b)
  101. end
  102. for i,v in ipairs(self.inputList) do
  103. if v.active == true then local tr = v:mousereleased(x,y,b) if tr~=nil then r = tr end end
  104. end
  105. return r
  106. end
  107. function GroupBox:keypressed(key,isrepeat)
  108. for i,v in ipairs(self.inputList) do
  109. if v.active == true then v:keypressed(key,isrepeat) end
  110. end
  111. end
  112. function GroupBox:keyreleased(key)
  113. for i,v in ipairs(self.inputList) do
  114. if v.active == true then v:keyreleased(key) end
  115. end
  116. end
  117. function GroupBox:textinput(t)
  118. for i,v in ipairs(self.inputList) do
  119. if v.active == true then v:textinput(t) end
  120. end
  121. end
  122. function GroupBox:addItem(item)
  123. table.insert(self.items,item)
  124. if item.updateable == true then
  125. table.insert(self.updateList,item)
  126. end
  127. if item.drawable == true then
  128. table.insert(self.drawList,item)
  129. end
  130. if item.input == true then
  131. table.insert(self.inputList,item)
  132. end
  133. return item
  134. end
  135. function GroupBox:getItem(name,deep)
  136. local c = #self.items
  137. if c>0 then
  138. for i=1,c do
  139. if self.items[i]:getName() == name then
  140. return self.items[i]
  141. elseif self.items[i].items ~= nil and deep == true then
  142. self.items[i]:getItem(name,deep)
  143. end
  144. end
  145. end
  146. return nil
  147. end
  148. function GroupBox:setPosition(x,y)
  149. if self.isContainer == true then
  150. local dx,dy = (x or self.x) - self.x, (y or self.y) - self.y
  151. local c = #self.items
  152. if c>0 then
  153. for i=1,c do
  154. local e = self.items[i]
  155. e:setPosition(e.x+dx,e.y+dy)
  156. end
  157. end
  158. end
  159. self.x,self.y = x or self.x, y or self.y
  160. end
  161. function GroupBox:onchangewindow(w,h)
  162. for i,v in ipairs(self.items) do
  163. v:onchangewindow(w,h)
  164. end
  165. end