Last active
March 15, 2020 22:11
-
-
Save GeePawHill/bdba04b61b1f4e7f18b82bb16a68faf0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.geepawhill.contentment.jfx | |
import javafx.beans.property.Property | |
import javafx.beans.property.ReadOnlyProperty | |
import javafx.beans.property.SimpleObjectProperty | |
import javafx.beans.value.ChangeListener | |
import javafx.beans.value.ObservableValue | |
import javafx.collections.ObservableList | |
import javafx.scene.transform.Scale | |
import javafx.scene.transform.Transform | |
class AspectRatioConstraint( | |
private val hostWidth: ReadOnlyProperty<Number>, | |
private val hostHeight: ReadOnlyProperty<Number>, | |
private val transforms: ObservableList<Transform>, | |
private val mediaWidth: Property<Number>) | |
: ChangeListener<Number> { | |
init { | |
hostWidth.addListener(this) | |
hostHeight.addListener(this) | |
changed(SimpleObjectProperty(0.0), 300, 300) | |
} | |
override fun changed(observable: ObservableValue<out Number>, oldValue: Number, newValue: Number) { | |
val newWidth = hostWidth.value.toDouble() | |
val newHeight = hostHeight.value.toDouble() | |
val scaleFactor = if (newWidth / newHeight > ASPECT_RATIO) newHeight / FORCED_HEIGHT else newWidth / FORCED_WIDTH | |
val newScale = Scale(scaleFactor, scaleFactor) | |
newScale.pivotX = 0.0 | |
newScale.pivotY = 0.0 | |
mediaWidth.value = newWidth | |
transforms.setAll(newScale) | |
} | |
companion object { | |
private const val FORCED_WIDTH = 1600.0 | |
private const val FORCED_HEIGHT = 900.0 | |
private const val ASPECT_RATIO = FORCED_WIDTH / FORCED_HEIGHT | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.geepawhill.contentment.jfx | |
import javafx.beans.property.SimpleObjectProperty | |
import javafx.beans.value.ChangeListener | |
import javafx.beans.value.ObservableValue | |
import javafx.scene.Group | |
import javafx.scene.layout.Pane | |
import javafx.scene.media.MediaView | |
import javafx.scene.transform.Scale | |
class ScaleListener(private val host: Pane, private val child: Group, private val media: MediaView) : ChangeListener<Number> { | |
init { | |
host.widthProperty().addListener(this) | |
host.heightProperty().addListener(this) | |
changed(SimpleObjectProperty(0.0), 300, 300) | |
} | |
override fun changed(observable: ObservableValue<out Number>, oldValue: Number, newValue: Number) { | |
val newWidth = host.width | |
media.fitWidth = newWidth | |
val newHeight = host.height | |
val scaleFactor = if (newWidth / newHeight > ASPECT_RATIO) newHeight / FORCED_HEIGHT else newWidth / FORCED_WIDTH | |
val newScale = Scale(scaleFactor, scaleFactor) | |
newScale.pivotX = 0.0 | |
newScale.pivotY = 0.0 | |
child.transforms.setAll(newScale) | |
} | |
companion object { | |
private const val FORCED_WIDTH = 1600.0 | |
private const val FORCED_HEIGHT = 900.0 | |
private const val ASPECT_RATIO = FORCED_WIDTH / FORCED_HEIGHT | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment