local Element = require("element") local List = Element:new{ children = {}, vertical = false } local function adjustPositions(elements, vertical, from) local newDims = 0 local getDim = vertical and function(e) return e:getHeight() end or function(e) return e:getWidth() end for i=1,from-1 do newDims = newDims + getDim(elements[i]) end local setDim = vertical and function(e, dim) e:setPos(0, dim) end or function(e, dim) e:setPos(dim, 0) end for i=from,#elements do setDim(elements[i]) newDims = newDims + getDim(elements[i]) end end function List:insertChild(child, atIndex) local index = math.min(math.max(1, atIndex or #self.children), #self.children) table.insert(self.children, index, child) adjustPositions(self.children, self:isVertical(), index) end function List:removeChild(child) local index local searchType = type(child) if searchType == "string" then for i,v in ipairs(self.children) do if v:getId() == child then index = i break end end return false, nil elseif searchType == "table" then for i,v in ipairs(self.children) do if v == child then index = i end end return false, nil else index = child end if index <= 0 or index > #self.children then return false, nil end local removed = table.remove(self.children, index) if index <= #self.children then adjustPositions(self.children, self:isVertical(), index) end return true, removed end function List:isVertical() return self.vertical end function List:isHorizontal() return not self:isVertical() end function List:draw() Element.draw(self) self:_getWindow().clear() for _,v in ipairs(self.children) do v:draw() end end function List:getHeight() local h = 0 for _,v in ipairs(self.children) do h = h + v:getHeight() end return h end function List:getWidth() local h = 0 for _,v in ipairs(self.children) do h = h + v:getHeight() end return h end function List:findById(id) local find = Element.findById(self, id) if find then return find end for _,v in ipairs(self.children) do local result = v:findById(id) if result then return result end end return nil end return List