Feature request: Make object relative positioning easier

Add object.left, .right, .top., and .bottom for easier relative positioning.

Let’s say you want to position a “Done” button to the right of a text field. For that, you calculate the button’s y position according to the text field’s y but you also have to consider the anchor property too. 
If after the fact, the text field’s anchor is changed, then the button’s position will also change and will require some tweaking to set it right.

If instead, each display object had a .right property that -no matter what the anchor was- always denoted the rightmost coordinates, then changing the anchor on an object would not affect the positioning of others.

I think this would save a lot of time fiddling and calculating object positions, especially for UIs.

If you like this idea, please vote for it here: http://feedback.coronalabs.com/forums/188732-corona-sdk-feature-requests-feedback/suggestions/15114486-add-object-left-right-top-and-bottom-for-ea

What about this instead:

local obj1 = display.newRect( display.contentCenterX, display.contentCenterY, 50, 50 ) obj1:setFillColor( 1,0,0 ) local function relativeObjMove( obj, currentObj, pos ) if (obj and pos) then local posX, posY local yAdd, xAdd = 0,0 if (pos == "right") then local objAnchorX = obj.anchorX local objAnchorY = obj.anchorY obj.anchorX = 1 obj.anchorY = .5 posX, posY =obj.x, obj.y obj.anchorX= objAnchorX obj.anchorY= objAnchorY xAdd = currentObj.width elseif (pos == "left") then local objAnchorX = obj.anchorX local objAnchorY = obj.anchorY obj.anchorX = 0 obj.anchorY = .5 posX, posY =obj.x, obj.y obj.anchorX= objAnchorX obj.anchorY= objAnchorY xAdd = -currentObj.width elseif (pos == "top") then local objAnchorX = obj.anchorX local objAnchorY = obj.anchorY obj.anchorX = .5 obj.anchorY = 0 posX, posY =obj.x, obj.y obj.anchorX= objAnchorX obj.anchorY= objAnchorY yAdd = -currentObj.width elseif (pos == "bottem") then local objAnchorX = obj.anchorX local objAnchorY = obj.anchorY obj.anchorX = .5 obj.anchorY = 1 posX, posY =obj.x, obj.y obj.anchorX= objAnchorX obj.anchorY= objAnchorY yAdd = currentObj.width end currentObj.anchorX, currentObj.anchorY = .5, .5 currentObj.x, currentObj.y = posX+xAdd, posY+yAdd else print("error missing params") end end local obj2 = display.newRect( 0, 0, 50, 50 ) obj2.anchorX = .9 obj2:setFillColor( 0,0,1 ) relativeObjMove( obj1, obj2,"right" )

On a related note, you determine the edges of any object as follows

local function oLeft( obj ) return obj.x - (obj.contentWidth \* obj.anchorX) end local function oRight( obj ) return obj.x + (obj.contentWidth \* obj.anchorX) end local function oTop( obj ) return obj.y - (obj.contentHeight \* obj.anchorY) end local function oBot( obj ) return obj.y + (obj.contentHeight \* obj.anchorY) end

Hold on… I’m not asking how to do it though people’s input is more than welcome. I’m already doing something along the lines of what has been posted. 

The point of my post was that I had to do this on every single app I made in Corona. While not a complicated thing to achieve in code, I think this should be part of the framework. Things like these are by definition what a framework should do; solve recurring problems so that they needn’t be solved in userland code. 

With that in mind I put this into the feature request section so that if anyone liked this particular suggestion they could vote it.

While it is a cool idea, I just don’t think it is necessary as a core feature. People set up their own position system, their own groups, or tile system. Plus if you do the same thing over and over, you should just make you own module for your projects. But that is just one mans opinion.

Agree. This would be compatible with all that and not only reduce the amount of code people would need to create but unify code style across projects (positioning would look the same everywhere).

Already done! As stated above, I think this belongs in the framework not in userland.

What about this instead:

local obj1 = display.newRect( display.contentCenterX, display.contentCenterY, 50, 50 ) obj1:setFillColor( 1,0,0 ) local function relativeObjMove( obj, currentObj, pos ) if (obj and pos) then local posX, posY local yAdd, xAdd = 0,0 if (pos == "right") then local objAnchorX = obj.anchorX local objAnchorY = obj.anchorY obj.anchorX = 1 obj.anchorY = .5 posX, posY =obj.x, obj.y obj.anchorX= objAnchorX obj.anchorY= objAnchorY xAdd = currentObj.width elseif (pos == "left") then local objAnchorX = obj.anchorX local objAnchorY = obj.anchorY obj.anchorX = 0 obj.anchorY = .5 posX, posY =obj.x, obj.y obj.anchorX= objAnchorX obj.anchorY= objAnchorY xAdd = -currentObj.width elseif (pos == "top") then local objAnchorX = obj.anchorX local objAnchorY = obj.anchorY obj.anchorX = .5 obj.anchorY = 0 posX, posY =obj.x, obj.y obj.anchorX= objAnchorX obj.anchorY= objAnchorY yAdd = -currentObj.width elseif (pos == "bottem") then local objAnchorX = obj.anchorX local objAnchorY = obj.anchorY obj.anchorX = .5 obj.anchorY = 1 posX, posY =obj.x, obj.y obj.anchorX= objAnchorX obj.anchorY= objAnchorY yAdd = currentObj.width end currentObj.anchorX, currentObj.anchorY = .5, .5 currentObj.x, currentObj.y = posX+xAdd, posY+yAdd else print("error missing params") end end local obj2 = display.newRect( 0, 0, 50, 50 ) obj2.anchorX = .9 obj2:setFillColor( 0,0,1 ) relativeObjMove( obj1, obj2,"right" )

On a related note, you determine the edges of any object as follows

local function oLeft( obj ) return obj.x - (obj.contentWidth \* obj.anchorX) end local function oRight( obj ) return obj.x + (obj.contentWidth \* obj.anchorX) end local function oTop( obj ) return obj.y - (obj.contentHeight \* obj.anchorY) end local function oBot( obj ) return obj.y + (obj.contentHeight \* obj.anchorY) end

Hold on… I’m not asking how to do it though people’s input is more than welcome. I’m already doing something along the lines of what has been posted. 

The point of my post was that I had to do this on every single app I made in Corona. While not a complicated thing to achieve in code, I think this should be part of the framework. Things like these are by definition what a framework should do; solve recurring problems so that they needn’t be solved in userland code. 

With that in mind I put this into the feature request section so that if anyone liked this particular suggestion they could vote it.

While it is a cool idea, I just don’t think it is necessary as a core feature. People set up their own position system, their own groups, or tile system. Plus if you do the same thing over and over, you should just make you own module for your projects. But that is just one mans opinion.

Agree. This would be compatible with all that and not only reduce the amount of code people would need to create but unify code style across projects (positioning would look the same everywhere).

Already done! As stated above, I think this belongs in the framework not in userland.