Skip to content

Commit f015d38

Browse files
committed
fix: swap inverse interplote methods
1 parent 4f94235 commit f015d38

File tree

3 files changed

+43
-40
lines changed

3 files changed

+43
-40
lines changed

demo/src/main/java/io/monstarlab/mosaic/features/SliderDemo.kt

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,17 @@ import androidx.compose.ui.unit.dp
4040
import io.monstarlab.mosaic.slider.Slider
4141
import io.monstarlab.mosaic.slider.SliderColors
4242
import io.monstarlab.mosaic.slider.SliderValueDistribution
43-
import io.monstarlab.mosaic.slider.rememberSliderState
4443
import kotlin.math.roundToInt
4544
import androidx.compose.material3.Slider as MaterialSlider
4645

4746
@Composable
4847
fun SliderDemo() = Scaffold(modifier = Modifier) {
49-
var materialSliderValue by remember { mutableFloatStateOf(50f) }
50-
5148
Column(
5249
modifier = Modifier
5350
.padding(it)
5451
.padding(16.dp),
5552
verticalArrangement = Arrangement.spacedBy(32.dp),
5653
) {
57-
MaterialSlider(
58-
value = materialSliderValue,
59-
onValueChange = { materialSliderValue = it },
60-
valueRange = 0f..100f,
61-
)
62-
6354
MosaicSliderDemo()
6455
}
6556
}
@@ -76,6 +67,14 @@ fun MosaicSliderDemo() {
7667
var isCustom by remember { mutableStateOf(false) }
7768
var linearDistribution by remember { mutableStateOf(false) }
7869

70+
var sliderValue by remember { mutableFloatStateOf(500f) }
71+
72+
MaterialSlider(
73+
value = sliderValue,
74+
onValueChange = { sliderValue = it },
75+
valueRange = 0f..1000f,
76+
)
77+
7978
val modifier = if (isCustom) {
8079
Modifier
8180
.clip(RoundedCornerShape(16.dp))
@@ -85,28 +84,21 @@ fun MosaicSliderDemo() {
8584
}
8685

8786
val parabolic: SliderValueDistribution = remember {
88-
SliderValueDistribution.parabolic(
89-
a = (1000 - 100 * 0.1f) / (1000 * 1000),
90-
b = 0.1f,
91-
c = 1f,
92-
)
87+
SliderValueDistribution.parabolic(a = 0.005f)
9388
}
9489

95-
val state = rememberSliderState(
96-
value = 500f,
90+
Slider(
91+
value = sliderValue,
92+
onValueChange = { sliderValue = it },
93+
modifier = modifier,
94+
enabled = enabled,
95+
colors = colors,
96+
range = 0f..1000f,
9797
valueDistribution = if (linearDistribution) {
9898
SliderValueDistribution.Linear
9999
} else {
100100
parabolic
101101
},
102-
range = 0f..1000f,
103-
)
104-
105-
Slider(
106-
modifier = modifier,
107-
enabled = enabled,
108-
state = state,
109-
colors = colors,
110102
thumb = {
111103
if (isCustom) {
112104
val transition = rememberInfiniteTransition(label = "")
@@ -148,7 +140,7 @@ fun MosaicSliderDemo() {
148140
)
149141

150142
Text(
151-
text = "Current value: ${state.value.roundToInt()}",
143+
text = "Current value: ${sliderValue.roundToInt()}",
152144
modifier = Modifier.align(Alignment.CenterHorizontally),
153145
textAlign = TextAlign.Center,
154146
style = MaterialTheme.typography.bodyLarge,

slider/src/main/java/io/monstarlab/mosaic/slider/SliderState.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ public class SliderState(
6565
*/
6666
internal val valueAsFraction: Float
6767
get() {
68-
val interpolated = valueDistribution.interpolate(value)
69-
return calcFraction(range.start, range.endInclusive, interpolated)
68+
val inverted = valueDistribution.inverse(value)
69+
val invertedRange = valueDistribution.inverse(range)
70+
return calcFraction(invertedRange.start, invertedRange.endInclusive, inverted)
7071
}
7172

7273
internal val disabledRangeAsFractions: ClosedFloatingPointRange<Float>
@@ -114,22 +115,22 @@ public class SliderState(
114115
* Scales offset in to the value that user should see
115116
*/
116117
private fun scaleToUserValue(offset: Float): Float {
117-
val range = valueDistribution.interpolate(range)
118-
val scaledUserValue = scale(0f, totalWidth, offset, range.start, range.endInclusive)
119-
return coerceValue(valueDistribution.inverse(scaledUserValue))
118+
val invertedRange = valueDistribution.inverse(range)
119+
val value = scale(0f, totalWidth, offset, invertedRange.start, invertedRange.endInclusive)
120+
return coerceValue(valueDistribution.interpolate(value))
120121
}
121122

122123
/**
123124
* Converts value of the user into the raw offset on the track
124125
*/
125126
private fun scaleToOffset(value: Float): Float {
126127
val coerced = coerceValue(value)
127-
val interpolatedRange = valueDistribution.interpolate(range)
128-
val interpolated = valueDistribution.interpolate(coerced)
128+
val invertedRange = valueDistribution.inverse(range)
129+
val invertedValue = valueDistribution.inverse(coerced)
129130
return scale(
130-
interpolatedRange.start,
131-
interpolatedRange.endInclusive,
132-
interpolated,
131+
invertedRange.start,
132+
invertedRange.endInclusive,
133+
invertedValue,
133134
0f,
134135
totalWidth,
135136
)

slider/src/main/java/io/monstarlab/mosaic/slider/SliderValueDistribution.kt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ public interface SliderValueDistribution {
3535
* @param c constant term in the parabolic equation
3636
* @return a [SliderValueDistribution] instance with a parabolic distribution strategy
3737
*/
38-
public fun parabolic(a: Float, b: Float, c: Float): SliderValueDistribution {
38+
public fun parabolic(
39+
a: Float,
40+
b: Float = 0f,
41+
c: Float = 0f,
42+
): SliderValueDistribution {
3943
return ParabolicValueDistribution(a, b, c)
4044
}
4145

@@ -61,6 +65,12 @@ internal fun SliderValueDistribution.interpolate(
6165
return interpolate(range.start)..interpolate(range.endInclusive)
6266
}
6367

68+
internal fun SliderValueDistribution.inverse(
69+
range: ClosedFloatingPointRange<Float>,
70+
): ClosedFloatingPointRange<Float> {
71+
return inverse(range.start)..inverse(range.endInclusive)
72+
}
73+
6474
/**
6575
* Represents a parabolic distribution strategy for slider values.
6676
*
@@ -79,13 +89,13 @@ public class ParabolicValueDistribution(
7989
) : SliderValueDistribution {
8090

8191
override fun inverse(value: Float): Float {
82-
return a * (value * value) + b * value + c
83-
}
84-
85-
override fun interpolate(value: Float): Float {
8692
if (value == 0f) return 0f
8793

8894
val d = (b * b) - 4 * a * (c - value)
8995
return (-b + sqrt(d)) / (2 * a)
9096
}
97+
98+
override fun interpolate(value: Float): Float {
99+
return a * (value * value) + b * value + c
100+
}
91101
}

0 commit comments

Comments
 (0)