Skip to content

Commit

Permalink
feat: Viewport::bindToFit for entities is now restored, closes #1036
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Jul 12, 2022
1 parent 6cba0b4 commit 621cbd7
Showing 1 changed file with 31 additions and 35 deletions.
66 changes: 31 additions & 35 deletions fxgl/src/main/kotlin/com/almasb/fxgl/app/scene/Viewport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -152,41 +152,37 @@ class Viewport
boundY = Bindings.`when`(by.greaterThan(maxY.subtract(zoomedHeight))).then(maxY.subtract(zoomedHeight)).otherwise(boundY)
}

// fun bindToFit(xMargin: Double, yMargin: Double, vararg entities: Entity) {
// val minBindingX = entities.filter { it.hasComponent(BoundingBoxComponent::class.java) }
// .map { it.getComponent(BoundingBoxComponent::class.java) }
// .map { it.minXWorldProperty() }
// .fold(Bindings.min(SimpleIntegerProperty(Int.MAX_VALUE), Integer.MAX_VALUE), { min, x -> Bindings.min(min, x) })
// .subtract(xMargin)
//
// val minBindingY = entities.filter { it.hasComponent(BoundingBoxComponent::class.java) }
// .map { it.getComponent(BoundingBoxComponent::class.java) }
// .map { it.minYWorldProperty() }
// .fold(Bindings.min(SimpleIntegerProperty(Int.MAX_VALUE), Integer.MAX_VALUE), { min, y -> Bindings.min(min, y) })
// .subtract(yMargin)
//
// val maxBindingX = entities.filter { it.hasComponent(BoundingBoxComponent::class.java) }
// .map { it.getComponent(BoundingBoxComponent::class.java) }
// .map { it.maxXWorldProperty() }
// .fold(Bindings.max(SimpleIntegerProperty(Int.MIN_VALUE), Integer.MIN_VALUE), { max, x -> Bindings.max(max, x) })
// .add(xMargin)
//
// val maxBindingY = entities.filter { it.hasComponent(BoundingBoxComponent::class.java) }
// .map { it.getComponent(BoundingBoxComponent::class.java) }
// .map { it.maxYWorldProperty() }
// .fold(Bindings.max(SimpleIntegerProperty(Int.MIN_VALUE), Integer.MIN_VALUE), { max, y -> Bindings.max(max, y) })
// .add(yMargin)
//
// val widthBinding = maxBindingX.subtract(minBindingX)
// val heightBinding = maxBindingY.subtract(minBindingY)
//
// val ratio = Bindings.min(Bindings.divide(width, widthBinding), Bindings.divide(height, heightBinding))
//
// x.bind(minBindingX)
// y.bind(minBindingY)
//
// zoom.bind(ratio)
// }
@JvmOverloads fun bindToFit(xMargin: Double = 0.0, yMargin: Double = 0.0, vararg entities: Entity) {
val minBindingX = entities
.map { it.boundingBoxComponent.minXWorldProperty() }
.fold(Bindings.min(SimpleIntegerProperty(Int.MAX_VALUE), Integer.MAX_VALUE)) { min, x -> Bindings.min(min, x) }
.subtract(xMargin)

val minBindingY = entities
.map { it.boundingBoxComponent.minYWorldProperty() }
.fold(Bindings.min(SimpleIntegerProperty(Int.MAX_VALUE), Integer.MAX_VALUE)) { min, y -> Bindings.min(min, y) }
.subtract(yMargin)

val maxBindingX = entities
.map { it.boundingBoxComponent.maxXWorldProperty() }
.fold(Bindings.max(SimpleIntegerProperty(Int.MIN_VALUE), Integer.MIN_VALUE)) { max, x -> Bindings.max(max, x) }
.add(xMargin)

val maxBindingY = entities
.map { it.boundingBoxComponent.maxYWorldProperty() }
.fold(Bindings.max(SimpleIntegerProperty(Int.MIN_VALUE), Integer.MIN_VALUE)) { max, y -> Bindings.max(max, y) }
.add(yMargin)

val widthBinding = maxBindingX.subtract(minBindingX)
val heightBinding = maxBindingY.subtract(minBindingY)

val ratio = Bindings.min(Bindings.divide(width, widthBinding), Bindings.divide(height, heightBinding))

boundX = minBindingX
boundY = minBindingY

zoom.bind(ratio)
}

/**
* Unbind viewport.
Expand Down

0 comments on commit 621cbd7

Please sign in to comment.