1
1
package io.monstarlab.mosaic.slider
2
2
3
+ // TODO refactor code to not use builder
3
4
public class FragmentedLinearDistribution private constructor(
4
5
equationMap : Map <Float , LinearEquation >,
5
6
) : SliderValueDistribution {
6
7
private var _equationList : MutableList <RangedLinearEquation > = mutableListOf ()
7
8
public val equationList: List <RangedLinearEquation > get() = _equationList .toList()
8
9
9
- private lateinit var valueRange: ClosedFloatingPointRange <Float >
10
+ private var valueRange: ClosedFloatingPointRange <Float >
10
11
11
12
init {
12
13
var previous: Pair <Float , LinearEquation >? = null
@@ -48,10 +49,11 @@ public class FragmentedLinearDistribution private constructor(
48
49
49
50
private fun valueFromOffset (offset : Float ): Float =
50
51
_equationList .firstOrNull { offset in it.offsetRange }?.equation?.valueFromOffset(offset)
51
- ? : 0f
52
+ ? : if (offset > 1f ) 1f else 0f
52
53
53
54
private fun offsetFromValue (value : Float ): Float =
54
- _equationList .firstOrNull { value in it.valueRange }?.equation?.offsetFromValue(value) ? : 0f
55
+ _equationList .firstOrNull { value in it.valueRange }?.equation?.offsetFromValue(value)
56
+ ? : if (value > 1f ) 1f else 0f
55
57
56
58
57
59
public class Builder {
@@ -73,20 +75,19 @@ public class FragmentedLinearDistribution private constructor(
73
75
offsetRangeStart : Float ,
74
76
offsetRangeEnd : Float ,
75
77
initialSensitivity : Float
76
- ) {
78
+ ): Builder = apply {
77
79
rangeStart = offsetRangeStart
78
80
rangeEnd = offsetRangeEnd
79
81
sliceAt(initialSensitivity, fractionalValue(rangeStart))
80
82
}
81
83
82
-
83
84
private fun fractionalValue (value : Float ) = value.valueToFraction(rangeStart, rangeEnd)
84
85
85
86
public fun sliceAt (
86
87
sensitivity : Float ,
87
88
position : Float ,
88
89
): Builder = apply {
89
- val relativePosition = fractionalValue(position)
90
+ val relativePosition = fractionalValue(position)
90
91
if (equationRangeStartMap.isEmpty() && relativePosition != 0f ) {
91
92
throw FirstValueNotZeroException ()
92
93
}
@@ -125,4 +126,5 @@ public class FragmentedLinearDistribution private constructor(
125
126
126
127
public class OverlappingRangeException :
127
128
Exception (" can't add overlapping ranges with different sensitivity" )
129
+
128
130
}
0 commit comments