Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Is there a reason why contentBounds are just provided as integers?
Started by henkieniets Nov 12 2017 02:23 PM

1 reply to this topic
contentbounds
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

henkieniets

[GLOBAL: userInfoPane.html]
henkieniets
  • Observer

  • 1 posts
  • Corona SDK

I was wondering why contentBound values are represented in just integers? I understand this can't be changed all of a sudden because it could break existing changes, but wouldn't it be an idea to add an additional one? (eg. preciseContentBounds or display.setDefault("contentBoundMode", "float"))

 

Have these just as integers prevents us from using it for positioning. Since an object with 'x' property of 0 has the same xMin as an object with 'x' property of 0.4

 

The code below shows how the contentBounds could be used, it works most of the time, but it will break when non integer parent 'x' or 'y' positions are used. Or when the target object position 'x' or 'y' should be non-integer

local t = {}

-- Aligns the top of obj at the top of given parent, if parent is nil, obj aligns to the top of the screen
function t:top(obj, marginY, parent)
    obj.y = obj.y + (parent and parent.contentBounds.yMin or 0) - obj.contentBounds.yMin + marginY
end

-- Aligns the bottom of obj at the bottom of given parent, if parent is nil, obj aligns to the bottom of the screen
function t:bottom(obj, marginY, parent)
    obj.y = obj.y + (parent and parent.contentBounds.yMax or display.actualContentHeight) - obj.contentBounds.yMax - marginY
end

-- Aligns the left of obj at the left of given parent, if parent is nil, obj aligns to the left of the screen
function t:left(obj, marginX, parent)
    obj.x = obj.x + (parent and parent.contentBounds.xMin or 0) - obj.contentBounds.xMin + marginX
end

-- Aligns the right of obj at the right of given parent, if parent is nil, obj aligns to the right of the screen
function t:right(obj, marginX, parent)
    obj.x = obj.x + (parent and parent.contentBounds.xMax or display.actualContentWidth) - obj.contentBounds.xMax - marginX
end

return t


[TOPIC: post.html]
#2

pouwelsjochem8

[GLOBAL: userInfoPane.html]
pouwelsjochem8
  • Enthusiast

  • 90 posts
  • Corona SDK

If developers will be able to add this, I will promise to release my improved 'magnet' module to the public as free marketplace plugin. It is currently based on https://github.com/radixzz/magnet/blob/master/lib/magnet.lua but has loads of functions added, an overview of those can be seen at the bottom of this post.

 

It can align any object to another object so you can easily read how all of your objects in the screen is set up. For example the pseudo code below.

local informationContainer = createInformationContainer(50, 50)
magnet:center(informationContainer, 0, 0) -- Aligns in the center of the screen

local healthIcon = createHealthIcon(12, 12)
magnet:topLeft(healthIcon, 2, 2, informationContainer)
local manaIcon = createManaIcon(12, 12)
magnet:topRight(manaIcon, 2, 2, informationContainer)

local nameText = createNameText()
magnet:atCenterRight(nameText, 0, 0, informationContainer)

This would result in something like this.

 

As of now, it depends on some hacked display.new* methods to work almost all of the time, even with very complex situations. So I can't really release it, but adding more precise contentBounds would remove the need for this hack, and would also be a nice performance boost since I won't have to resolve the object position by looping through all of the parents recursively.

function magnet:top(obj, marginY, parent)
function magnet:right(obj, marginX, parent)
function magnet:bottom(obj, marginY, parent)
function magnet:left(obj, marginX, parent)
function magnet:horizontalCenter(obj, marginX, parent)
function magnet:verticalCenter(obj, marginY, parent)

function magnet:topLeft(obj, marginX, marginY, parent)
function magnet:topRight(obj, marginX, marginY, parent)
function magnet:bottomLeft(obj, marginX, marginY, parent)
function magnet:bottomRight(obj, marginX, marginY, parent)

function magnet:center(obj, marginX, marginY, parent)       
function magnet:topCenter(obj, marginX, marginY, parent)
function magnet:centerRight(obj, marginX, marginY, parent)
function magnet:bottomCenter(obj, marginX, marginY, parent)
function magnet:centerLeft(obj, marginX, marginY, parent)

function magnet:atTop(obj, marginY, parent)
function magnet:atBottom(obj, marginY, parent)
function magnet:atRight(obj, marginX, parent)
function magnet:atLeft(obj, marginX, parent)

function magnet:atTopLeft(obj, marginX, marginY, parent)
function magnet:atTopCenter(obj, marginX, marginY, parent)
function magnet:atTopRight(obj, marginX, marginY, parent)
function magnet:atBottomLeft(obj, marginX, marginY, parent)
function magnet:atBottomCenter(obj, marginX, marginY, parent)
function magnet:atBottomRight(obj, marginX, marginY, parent)

function magnet:atLeftTop(obj, marginX, marginY, parent)
function magnet:atLeftCenter(obj, marginX, marginY, parent)
function magnet:atLeftBottom(obj, marginX, marginY, parent)
function magnet:atRightTop(obj, marginX, marginY, parent)
function magnet:atRightCenter(obj, marginX, marginY, parent)
function magnet:atRightBottom(obj, marginX, marginY, parent)

function magnet:topCenterBetween(obj, marginX, marginY, parent, yPosition)
function magnet:bottomCenterBetween(obj, marginX, marginY, parent, yPosition)
function magnet:centerLeftBetween(obj, marginX, marginY, parent, xPosition)
function magnet:centerRightBetween(obj, marginX, marginY, parent, xPosition)

function magnet:atTopLeftBetween(obj, marginX, marginY, parent, yPosition)
function magnet:atTopCenterBetween(obj, marginX, marginY, parent, yPosition)
function magnet:atTopRightBetween(obj, marginX, marginY, parent, yPosition)
function magnet:atBottomLeftBetween(obj, marginX, marginY, parent, yPosition)
function magnet:atBottomCenterBetween(obj, marginX, marginY, parent, yPosition)
function magnet:atBottomRightBetween(obj, marginX, marginY, parent, yPosition)
function magnet:atLeftTopBetween(obj, marginX, marginY, parent, xPosition)
function magnet:atLeftCenterBetween(obj, marginX, marginY, parent, xPosition)
function magnet:atLeftBottomBetween(obj, marginX, marginY, parent, xPosition)
function magnet:atRightTopBetween(obj, marginX, marginY, parent, xPosition)
function magnet:atRightCenterBetween(obj, marginX, marginY, parent, xPosition)
function magnet:atRightBottomBetween(obj, marginX, marginY, parent, xPosition)

function magnet:getLocationFor(...)
function magnet:getXLocationFor(...)
function magnet:getYLocationFor(...)

setmetatable(magnet, {__call = 
    function(...)
        magnet[select(2, ...)](magnet, select(3, ...))
    end
})



[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: contentbounds