Skip to content

Commit

Permalink
Improve Padding compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Reco1I committed Dec 28, 2024
1 parent 3e87067 commit 32305dc
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 46 deletions.
54 changes: 36 additions & 18 deletions src/com/reco1l/andengine/ExtendedEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import org.anddev.andengine.opengl.util.*
import org.anddev.andengine.opengl.vertex.*
import org.anddev.andengine.util.Transformation
import javax.microedition.khronos.opengles.*
import kotlin.math.max


/**
Expand Down Expand Up @@ -284,7 +283,7 @@ abstract class ExtendedEntity(
x *= parent.getPaddedWidth()
}

return max(parent.getPadding().left, x) + totalOffsetX
return x + totalOffsetX
}


Expand All @@ -304,7 +303,7 @@ abstract class ExtendedEntity(
y *= parent.getPaddedHeight()
}

return max(parent.getPadding().top, y) + totalOffsetY
return y + totalOffsetY
}


Expand Down Expand Up @@ -476,26 +475,20 @@ abstract class ExtendedEntity(
}

override fun onDrawChildren(gl: GL10, camera: Camera) {
super.onDrawChildren(gl, camera)

foreground?.setSize(drawWidth, drawHeight)
foreground?.onDraw(gl, camera)
}

override fun onManagedDraw(gl: GL10, camera: Camera) {
val hasPaddingApplicable = padding.left > 0f || padding.top > 0f

if (isVertexBufferDirty) {
isVertexBufferDirty = false
onUpdateVertexBuffer()
if (hasPaddingApplicable) {
gl.glTranslatef(padding.left, padding.top, 0f)
}

if (clipChildren) {
GLHelper.enableScissorTest(gl)

var (bottomLeftX, bottomLeftY) = camera.getScreenSpaceCoordinates(convertLocalToSceneCoordinates(0f, 0f))
var (topLeftX, topLeftY) = camera.getScreenSpaceCoordinates(convertLocalToSceneCoordinates(0f, drawHeight))
var (topRightX, topRightY) = camera.getScreenSpaceCoordinates(convertLocalToSceneCoordinates(drawWidth, drawHeight))
var (bottomRightX, bottomRightY) = camera.getScreenSpaceCoordinates(convertLocalToSceneCoordinates(drawWidth, 0f))
var (topLeftX, topLeftY) = camera.getScreenSpaceCoordinates(convertLocalToSceneCoordinates(0f, getPaddedHeight()))
var (topRightX, topRightY) = camera.getScreenSpaceCoordinates(convertLocalToSceneCoordinates(getPaddedWidth(), getPaddedHeight()))
var (bottomRightX, bottomRightY) = camera.getScreenSpaceCoordinates(convertLocalToSceneCoordinates(getPaddedWidth(), 0f))

// Flip the Y axis to match the OpenGL coordinate system.
bottomLeftY = camera.surfaceHeight - bottomLeftY
Expand All @@ -517,11 +510,28 @@ abstract class ExtendedEntity(
)
}

super.onManagedDraw(gl, camera)
super.onDrawChildren(gl, camera)

if (clipChildren) {
GLHelper.disableScissorTest(gl)
}

if (hasPaddingApplicable) {
gl.glTranslatef(-padding.right, -padding.top, 0f)
}

foreground?.setSize(drawWidth, drawHeight)
foreground?.onDraw(gl, camera)
}

override fun onManagedDraw(gl: GL10, camera: Camera) {

if (isVertexBufferDirty) {
isVertexBufferDirty = false
onUpdateVertexBuffer()
}

super.onManagedDraw(gl, camera)
}

override fun onInitDraw(pGL: GL10) {
Expand Down Expand Up @@ -584,11 +594,19 @@ abstract class ExtendedEntity(
if (contentWidth != width || contentHeight != height) {

if (autoSizeAxes.isHorizontal) {
width = if (relativeSizeAxes.isHorizontal) contentWidth / parent.getPaddedWidth() else contentWidth
width = contentWidth + padding.horizontal

if (relativeSizeAxes.isHorizontal) {
width /= parent.getPaddedWidth()
}
}

if (autoSizeAxes.isVertical) {
height = if (relativeSizeAxes.isVertical) contentHeight / parent.getPaddedHeight() else contentHeight
height = contentHeight + padding.vertical

if (relativeSizeAxes.isVertical) {
height /= parent.getPaddedHeight()
}
}

updateVertexBuffer()
Expand Down
40 changes: 24 additions & 16 deletions src/com/reco1l/andengine/container/ConstraintContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ class ConstraintContainer : Container() {

val target = constraints[child] ?: this

val targetX = if (target == this) 0f else target.getDrawX()
val targetWidth = if (target == this) getPaddedWidth() else target.getDrawWidth()
var targetX = target.getDrawX()
var targetWidth = target.getDrawWidth()

val paddingLeft = if (target == this) getPadding().left else 0f
val anchorOffsetX = targetWidth * child.anchor.x
if (target == this) {
targetX = 0f
targetWidth = getPaddedWidth()
}

var childX = max(paddingLeft, child.x)
val anchorOffsetX = targetWidth * child.anchor.x

// Relative positions will be multiplied by the remaining space from the
// target's position to the edge of the container.
var childX = child.x
if (child.relativePositionAxes.isHorizontal) {
childX *= drawWidth - targetX

// Relative positions will be multiplied by the remaining space from the
// target's position to the edge of the container.
childX *= getPaddedWidth() - targetX
}

return targetX + childX + child.originOffsetX + anchorOffsetX + child.translationX
Expand All @@ -41,18 +45,22 @@ class ConstraintContainer : Container() {

val target = constraints[child] ?: this

val targetY = if (target == this) 0f else target.getDrawY()
val targetHeight = if (target == this) getPaddedHeight() else target.getDrawHeight()
var targetY = target.getDrawY()
var targetHeight = target.getDrawHeight()

val paddingTop = if (target == this) getPadding().top else 0f
val anchorOffsetY = targetHeight * child.anchor.y
if (target == this) {
targetY = 0f
targetHeight = getPaddedHeight()
}

var childY = max(paddingTop, child.y)
val anchorOffsetY = targetHeight * child.anchor.y

// Relative positions will be multiplied by the remaining space from the
// target's position to the edge of the container.
var childY = child.y
if (child.relativePositionAxes.isVertical) {
childY *= drawHeight - targetY

// Relative positions will be multiplied by the remaining space from the
// target's position to the edge of the container.
childY *= getPaddedHeight() - targetY
}

return targetY + childY + child.originOffsetY + anchorOffsetY + child.translationY
Expand Down
4 changes: 2 additions & 2 deletions src/com/reco1l/andengine/container/Container.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ open class Container : ExtendedEntity() {
x *= getPaddedWidth()
}

return max(getPadding().left, x) + child.totalOffsetX
return x + child.totalOffsetX
}

open fun getChildDrawY(child: ExtendedEntity): Float {
Expand All @@ -74,7 +74,7 @@ open class Container : ExtendedEntity() {
y *= getPaddedHeight()
}

return max(getPadding().top, y) + child.totalOffsetY
return y + child.totalOffsetY
}


Expand Down
8 changes: 0 additions & 8 deletions src/com/reco1l/andengine/container/LinearContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ open class LinearContainer : Container() {
contentWidth += child.getDrawWidth()
contentHeight = max(contentHeight, child.getDrawHeight())

if (i == 0) {
contentWidth += getPadding().left
}

if (i < childCount - 1) {
contentWidth += spacing
}
Expand All @@ -66,10 +62,6 @@ open class LinearContainer : Container() {
contentWidth = max(contentWidth, child.getDrawWidth())
contentHeight += child.getDrawHeight()

if (i == 0) {
contentHeight += getPadding().top
}

if (i < childCount - 1) {
contentHeight += spacing
}
Expand Down
4 changes: 2 additions & 2 deletions src/com/reco1l/andengine/container/ScrollableContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ open class ScrollableContainer : Container() {
return super.getChildDrawX(child)
}

return -scrollX + max(getPadding().left, child.x) - child.originOffsetX + child.translationX
return -scrollX + child.x - child.originOffsetX + child.translationX
}

override fun getChildDrawY(child: ExtendedEntity): Float {
Expand All @@ -351,7 +351,7 @@ open class ScrollableContainer : Container() {
return super.getChildDrawY(child)
}

return -scrollY + max(getPadding().top, child.y) - child.originOffsetY + child.translationY
return -scrollY + child.y - child.originOffsetY + child.translationY
}


Expand Down

0 comments on commit 32305dc

Please sign in to comment.