Skip to content

Commit

Permalink
Introduce touch area binding
Browse files Browse the repository at this point in the history
  • Loading branch information
Reco1I committed Dec 28, 2024
1 parent 32305dc commit a6f7722
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
27 changes: 23 additions & 4 deletions src/com/reco1l/andengine/ExtendedEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.util.*
import com.reco1l.andengine.container.*
import com.reco1l.andengine.modifier.*
import com.reco1l.framework.*
import com.reco1l.framework.math.Vec2
import com.reco1l.framework.math.Vec4
import org.anddev.andengine.engine.camera.*
import org.anddev.andengine.entity.*
Expand Down Expand Up @@ -313,6 +312,8 @@ abstract class ExtendedEntity(

private var isVertexBufferDirty = true

private var currentBoundEntity: ITouchArea? = null


// Attachment

Expand Down Expand Up @@ -819,16 +820,34 @@ abstract class ExtendedEntity(
localY: Float
): Boolean {

val boundEntity = currentBoundEntity
if (boundEntity != null) {
boundEntity as IEntity

val transformedX = localX - boundEntity.getDrawX()
val transformedY = localY - boundEntity.getDrawY()

boundEntity.onAreaTouched(event, transformedX, transformedY)

if (event.isActionUp || event.isActionOutside || event.isActionCancel) {
currentBoundEntity = null
}
return true
}

try {
for (i in childCount - 1 downTo 0) {
val child = getChild(i)

val transformedX = localX - child.getDrawX()
val transformedY = localY - child.getDrawY()
if (child is ITouchArea && child.contains(localX, localY)) {

if (child is ITouchArea && child.contains(transformedX, transformedY)) {
val transformedX = localX - child.getDrawX()
val transformedY = localY - child.getDrawY()

if (child.onAreaTouched(event, transformedX, transformedY)) {
if (event.isActionDown) {
currentBoundEntity = child
}
return true
}
}
Expand Down
30 changes: 15 additions & 15 deletions src/com/reco1l/andengine/container/ScrollableContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -280,31 +280,33 @@ open class ScrollableContainer : Container() {
when (event.action) {

ACTION_DOWN -> {
isDragging = true

initialX = localX
initialY = localY

velocityX = 0f
velocityY = 0f

lastDragTimeSec = elapsedTimeSec
}

ACTION_MOVE -> {
isDragging = true

var deltaX = localX - initialX
var deltaY = localY - initialY
var deltaX = if (scrollAxes.isHorizontal) localX - initialX else 0f
var deltaY = if (scrollAxes.isVertical) localY - initialY else 0f

if (!isDragging) {
isDragging = abs(deltaX) > 1f || abs(deltaY) > 1f

if (!isDragging) {
return super.onAreaTouched(event, localX, localY)
}
}

val length = hypot(deltaX, deltaY)

// Slow down the scroll when reaching the bounds.
if (scrollX + deltaX < 0 || scrollX + deltaX > maxScrollX) {
if (scrollX - deltaX < 0 || scrollX - deltaX > maxScrollX) {
deltaX *= if (length <= 0) 0f else length.pow(0.7f) / length
}

if (scrollY + deltaY < 0 || scrollY + deltaY > maxScrollY) {
if (scrollY - deltaY < 0 || scrollY - deltaY > maxScrollY) {
deltaY *= if (length <= 0) 0f else length.pow(0.7f) / length
}

Expand All @@ -313,26 +315,24 @@ open class ScrollableContainer : Container() {
if (abs(deltaX) > 0.1f) {
scrollX -= deltaX
velocityX = abs(deltaX / dragTimeSec) * sign(deltaX)

initialX = localX
lastDragTimeSec = elapsedTimeSec
}

if (abs(deltaY) > 0.1f) {
scrollY -= deltaY
velocityY = abs(deltaY / dragTimeSec) * sign(deltaY)

initialY = localY
lastDragTimeSec = elapsedTimeSec
}

lastDragTimeSec = elapsedTimeSec
}

else -> {
isDragging = false
}
}

return super.onAreaTouched(event, localX, localY)
return isDragging || super.onAreaTouched(event, localX, localY)
}


Expand Down

0 comments on commit a6f7722

Please sign in to comment.