If you are familiar with "Layout Group" in Unity or "stack view" in IOS then you understand what I want to achieve, if not, then it's basically a group that arranges its objects horizontally for example side by side based on their number and with. It also edits each object's width based on it.Here is the code I have:
function horizontalLayout( offset,x,y )
local group = display.newGroup( )
group.children = {}
function group.sort( )
local newOffset = x
for i=1, #group.children do
local child = group.children[i]
child.x = newOffset
child.y = y
newOffset = newOffset + child.width + offset
end
end
function group.add( obj )
group:insert( obj )
group.children[#group.children+1] = obj
group.sort()
end
return group
end
This works as it should. The only problem is that it positions the objects from the starting point onwards, while a proper Layout Group should sort the objects based on their collective width extending in both directions.
Example: If the starting x position = 150 and there are 2 objects in the group, then the first one should be positioned at (150 - object.width/2 - offset) and the second object should be the opposite. If there are 3 objects then the second object would be at exactly 150( I think), the first object on its left and the third object on its right.
The problem is that I'm unable to come up with the equation that achieves's this kind of sorting.
I hope that I have described everything as clearly as possible.
PS: I'm using Corona sdk and Lua
↧