Skip to content

Commit 621cbd7

Browse files
committed
feat: Viewport::bindToFit for entities is now restored, closes #1036
1 parent 6cba0b4 commit 621cbd7

File tree

1 file changed

+31
-35
lines changed
  • fxgl/src/main/kotlin/com/almasb/fxgl/app/scene

1 file changed

+31
-35
lines changed

fxgl/src/main/kotlin/com/almasb/fxgl/app/scene/Viewport.kt

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -152,41 +152,37 @@ class Viewport
152152
boundY = Bindings.`when`(by.greaterThan(maxY.subtract(zoomedHeight))).then(maxY.subtract(zoomedHeight)).otherwise(boundY)
153153
}
154154

155-
// fun bindToFit(xMargin: Double, yMargin: Double, vararg entities: Entity) {
156-
// val minBindingX = entities.filter { it.hasComponent(BoundingBoxComponent::class.java) }
157-
// .map { it.getComponent(BoundingBoxComponent::class.java) }
158-
// .map { it.minXWorldProperty() }
159-
// .fold(Bindings.min(SimpleIntegerProperty(Int.MAX_VALUE), Integer.MAX_VALUE), { min, x -> Bindings.min(min, x) })
160-
// .subtract(xMargin)
161-
//
162-
// val minBindingY = entities.filter { it.hasComponent(BoundingBoxComponent::class.java) }
163-
// .map { it.getComponent(BoundingBoxComponent::class.java) }
164-
// .map { it.minYWorldProperty() }
165-
// .fold(Bindings.min(SimpleIntegerProperty(Int.MAX_VALUE), Integer.MAX_VALUE), { min, y -> Bindings.min(min, y) })
166-
// .subtract(yMargin)
167-
//
168-
// val maxBindingX = entities.filter { it.hasComponent(BoundingBoxComponent::class.java) }
169-
// .map { it.getComponent(BoundingBoxComponent::class.java) }
170-
// .map { it.maxXWorldProperty() }
171-
// .fold(Bindings.max(SimpleIntegerProperty(Int.MIN_VALUE), Integer.MIN_VALUE), { max, x -> Bindings.max(max, x) })
172-
// .add(xMargin)
173-
//
174-
// val maxBindingY = entities.filter { it.hasComponent(BoundingBoxComponent::class.java) }
175-
// .map { it.getComponent(BoundingBoxComponent::class.java) }
176-
// .map { it.maxYWorldProperty() }
177-
// .fold(Bindings.max(SimpleIntegerProperty(Int.MIN_VALUE), Integer.MIN_VALUE), { max, y -> Bindings.max(max, y) })
178-
// .add(yMargin)
179-
//
180-
// val widthBinding = maxBindingX.subtract(minBindingX)
181-
// val heightBinding = maxBindingY.subtract(minBindingY)
182-
//
183-
// val ratio = Bindings.min(Bindings.divide(width, widthBinding), Bindings.divide(height, heightBinding))
184-
//
185-
// x.bind(minBindingX)
186-
// y.bind(minBindingY)
187-
//
188-
// zoom.bind(ratio)
189-
// }
155+
@JvmOverloads fun bindToFit(xMargin: Double = 0.0, yMargin: Double = 0.0, vararg entities: Entity) {
156+
val minBindingX = entities
157+
.map { it.boundingBoxComponent.minXWorldProperty() }
158+
.fold(Bindings.min(SimpleIntegerProperty(Int.MAX_VALUE), Integer.MAX_VALUE)) { min, x -> Bindings.min(min, x) }
159+
.subtract(xMargin)
160+
161+
val minBindingY = entities
162+
.map { it.boundingBoxComponent.minYWorldProperty() }
163+
.fold(Bindings.min(SimpleIntegerProperty(Int.MAX_VALUE), Integer.MAX_VALUE)) { min, y -> Bindings.min(min, y) }
164+
.subtract(yMargin)
165+
166+
val maxBindingX = entities
167+
.map { it.boundingBoxComponent.maxXWorldProperty() }
168+
.fold(Bindings.max(SimpleIntegerProperty(Int.MIN_VALUE), Integer.MIN_VALUE)) { max, x -> Bindings.max(max, x) }
169+
.add(xMargin)
170+
171+
val maxBindingY = entities
172+
.map { it.boundingBoxComponent.maxYWorldProperty() }
173+
.fold(Bindings.max(SimpleIntegerProperty(Int.MIN_VALUE), Integer.MIN_VALUE)) { max, y -> Bindings.max(max, y) }
174+
.add(yMargin)
175+
176+
val widthBinding = maxBindingX.subtract(minBindingX)
177+
val heightBinding = maxBindingY.subtract(minBindingY)
178+
179+
val ratio = Bindings.min(Bindings.divide(width, widthBinding), Bindings.divide(height, heightBinding))
180+
181+
boundX = minBindingX
182+
boundY = minBindingY
183+
184+
zoom.bind(ratio)
185+
}
190186

191187
/**
192188
* Unbind viewport.

0 commit comments

Comments
 (0)