Iterate over children as normal

This commit is contained in:
Gabriel Tofvesson 2024-10-09 23:18:37 +02:00
parent dd04c0c449
commit 2b24904c6f
2 changed files with 13 additions and 8 deletions

View File

@ -10,14 +10,15 @@ function List:adjustPositions(from)
local vertical = self:isVertical() local vertical = self:isVertical()
local newDims = 1 local newDims = 1
local getDim = vertical and function(e) return e:getHeight() end or function(e) return e:getWidth() end local getDim = vertical and function(e) return e:getHeight() end or function(e) return e:getWidth() end
for _,element in self:_iterateChildren{ to = from } do local children = self:_children()
newDims = newDims + getDim(element) for i=1,from-1 do
newDims = newDims + getDim(children[i])
end end
local setDim = vertical and function(e, dim) e:setY(dim) end or function(e, dim) e:setX(dim) end local setDim = vertical and function(e, dim) e:setY(dim) end or function(e, dim) e:setX(dim) end
for _, element in self:_iterateChildren{ from = from } do for i=from,#children do
setDim(element, newDims) setDim(children[i], newDims)
newDims = newDims + getDim(element) newDims = newDims + getDim(children[i])
end end
end end

View File

@ -6,15 +6,19 @@ local Children = Prop:new{ defaultState = {}, uid = "CHILDREN" }
function Children:with(elementType) function Children:with(elementType)
local propSelf = self local propSelf = self
function elementType:_iterateChildren(opts) function elementType:_iterateChildren(opts)
return ipairs(propSelf:getState(self)) return ipairs(self:_children())
end
function elementType:_children()
return propSelf:getState(self)
end end
function elementType:childCount() function elementType:childCount()
return #propSelf:getState(self) return #self:_children()
end end
function elementType:_childAt(index) function elementType:_childAt(index)
return propSelf:getState(self)[elementType:_validChildIndex(index)] return self:_children()[elementType:_validChildIndex(index)]
end end
function elementType:_validChildIndex(from) function elementType:_validChildIndex(from)