Skip to content

Commit 62980b4

Browse files
committed
feat: RechargeableDoubleComponent also has the value in percentage format as a binding
1 parent 9624266 commit 62980b4

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

fxgl/src/main/kotlin/com/almasb/fxgl/dsl/components/RechargeableComponents.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,18 @@ abstract class RechargeableDoubleComponent
4747
) : DoubleComponent(initialValue) {
4848

4949
private val maxValueProp = SimpleDoubleProperty(maxValue)
50+
private val valuePercentBinding = valueProperty().divide(maxValueProp).multiply(100)
5051

5152
fun maxValueProperty() = maxValueProp
53+
fun valuePercentProperty() = valuePercentBinding
5254

5355
var maxValue: Double
5456
get() = maxValueProp.value
5557
set(value) { maxValueProp.value = value }
5658

59+
val valuePercent: Double
60+
get() = valuePercentBinding.value
61+
5762
/**
5863
* Set component value to 0.
5964
*/

fxgl/src/test/kotlin/com/almasb/fxgl/dsl/components/RechargeableDoubleComponentTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,16 @@ class RechargeableDoubleComponentTest {
155155
assertThat(value, `is`(30.0))
156156
assertThat(zero, `is`(false))
157157
}
158+
159+
@Test
160+
fun `value in percent`() {
161+
hp.value = 10.0
162+
hp.maxValue = 100.0
163+
164+
assertThat(hp.valuePercent, `is`(10.0))
165+
166+
hp.maxValue = 50.0
167+
168+
assertThat(hp.valuePercent, `is`(20.0))
169+
}
158170
}

0 commit comments

Comments
 (0)