Created
May 24, 2020 16:08
-
-
Save GeePawHill/6c2012eebc9504d9d313e85f6cd8de9f 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.geometry | |
import javafx.beans.property.SimpleDoubleProperty | |
import tornadofx.* | |
class AspectRatio { | |
val widthToHeightProperty = SimpleDoubleProperty(16.0 / 9.0) | |
var widthToHeight by widthToHeightProperty | |
val hostWidthProperty = SimpleDoubleProperty(0.0) | |
var hostWidth by hostWidthProperty | |
val hostHeightProperty = SimpleDoubleProperty(0.0) | |
var hostHeight by hostHeightProperty | |
val widthProperty = SimpleDoubleProperty(0.0) | |
var width by widthProperty | |
val heightProperty = SimpleDoubleProperty(0.0) | |
var height by heightProperty | |
val xProperty = SimpleDoubleProperty(0.0) | |
var x by xProperty | |
val yProperty = SimpleDoubleProperty(0.0) | |
var y by yProperty | |
init { | |
hostWidthProperty.addListener { _ -> recalculate() } | |
hostHeightProperty.addListener { _ -> recalculate() } | |
widthToHeightProperty.addListener { _ -> recalculate() } | |
} | |
private fun recalculate() { | |
val impliedHeightForWidth = (1.0 / widthToHeight) * hostWidth | |
if (impliedHeightForWidth < hostHeight) { | |
width = hostWidth | |
height = impliedHeightForWidth | |
} else { | |
val impliedWidthForHeight = widthToHeight * hostHeight | |
width = impliedWidthForHeight | |
height = hostHeight | |
} | |
x = (hostWidth - width) / 2.0 | |
y = (hostHeight - height) / 2.0 | |
} | |
} |
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.geometry | |
import org.assertj.core.api.Assertions.assertThat | |
import org.assertj.core.data.Percentage | |
import org.junit.jupiter.api.Test | |
class AspectRatioTest { | |
private val ratio = AspectRatio() | |
@Test | |
fun `defaults to 16 x 9`() { | |
assertThat(ratio.widthToHeight).isCloseTo(16.0 / 9.0, Percentage.withPercentage(0.01)) | |
} | |
@Test | |
fun `exact dimension match`() { | |
ratio.hostWidth = 1600.0 | |
ratio.hostHeight = 900.0 | |
assertThat(ratio.width).isCloseTo(1600.0, Percentage.withPercentage(0.01)) | |
assertThat(ratio.height).isCloseTo(900.0, Percentage.withPercentage(0.01)) | |
} | |
@Test | |
fun `smaller dimension match`() { | |
ratio.hostWidth = 1280.0 | |
ratio.hostHeight = 720.0 | |
assertThat(ratio.width).isCloseTo(1280.0, Percentage.withPercentage(0.01)) | |
assertThat(ratio.height).isCloseTo(720.0, Percentage.withPercentage(0.01)) | |
} | |
@Test | |
fun `larger dimension match`() { | |
ratio.hostWidth = 1920.0 | |
ratio.hostHeight = 1080.0 | |
assertThat(ratio.width).isCloseTo(1920.0, Percentage.withPercentage(0.01)) | |
assertThat(ratio.height).isCloseTo(1080.0, Percentage.withPercentage(0.01)) | |
} | |
@Test | |
fun `not enough width`() { | |
ratio.hostWidth = 1600.0 | |
ratio.hostHeight = 1080.0 | |
assertThat(ratio.width).isCloseTo(1600.0, Percentage.withPercentage(0.01)) | |
assertThat(ratio.height).isCloseTo(900.0, Percentage.withPercentage(0.01)) | |
assertThat(ratio.y).isCloseTo(90.0, Percentage.withPercentage(0.01)) | |
assertThat(ratio.x).isCloseTo(0.0, Percentage.withPercentage(0.01)) | |
} | |
@Test | |
fun `not enough height`() { | |
ratio.hostWidth = 1920.0 | |
ratio.hostHeight = 720.0 | |
assertThat(ratio.width).isCloseTo(1280.0, Percentage.withPercentage(0.01)) | |
assertThat(ratio.height).isCloseTo(720.0, Percentage.withPercentage(0.01)) | |
assertThat(ratio.y).isCloseTo(0.0, Percentage.withPercentage(0.01)) | |
assertThat(ratio.x).isCloseTo(320.0, Percentage.withPercentage(0.01)) | |
} | |
@Test | |
fun `ratio change`() { | |
ratio.hostWidth = 1600.0 | |
ratio.hostHeight = 1200.0 | |
assertThat(ratio.height).isCloseTo(900.0, Percentage.withPercentage(0.01)) | |
ratio.widthToHeight = 4.0 / 3.0 | |
assertThat(ratio.width).isCloseTo(1600.0, Percentage.withPercentage(0.01)) | |
assertThat(ratio.height).isCloseTo(1200.0, Percentage.withPercentage(0.01)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment