Skip to content

Commit ce390e7

Browse files
committed
Another scale fix
1 parent 1cf4cbe commit ce390e7

File tree

5 files changed

+43
-29
lines changed

5 files changed

+43
-29
lines changed

common/src/main/kotlin/com/lambda/config/AbstractSetting.kt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ abstract class AbstractSetting<T : Any>(
5656
val description: String,
5757
val visibility: () -> Boolean,
5858
) : Jsonable, Nameable {
59-
private val listeners = mutableListOf<(from: T, to: T) -> Unit>()
59+
private val listeners = mutableListOf<ValueListener<T>>()
6060

6161
var value: T by Delegates.observable(defaultValue) { _, from, to ->
62-
if (from == to) return@observable
63-
listeners.forEach { it(from, to) }
62+
listeners.forEach {
63+
if (it.requiresValueChange && from == to) return@forEach
64+
it.execute(from, to)
65+
}
6466
}
6567

6668
private val isVisible get() = visibility()
@@ -79,18 +81,24 @@ abstract class AbstractSetting<T : Any>(
7981
}
8082

8183
fun onValueChange(block: SafeContext.(from: T, to: T) -> Unit) {
82-
listeners.add { from, to ->
84+
listeners.add(ValueListener(true) { from, to ->
8385
runSafe {
8486
block(from, to)
8587
}
86-
}
88+
})
8789
}
8890

8991
fun onValueChangeUnsafe(block: (from: T, to: T) -> Unit) {
90-
listeners.add(block)
92+
listeners.add(ValueListener(true, block))
93+
}
94+
95+
fun onValueSet(block: (from: T, to: T) -> Unit) {
96+
listeners.add(ValueListener(false, block))
9197
}
9298

9399
private fun reset() {
94100
value = defaultValue
95101
}
102+
103+
class ValueListener <T> (val requiresValueChange: Boolean, val execute: (from: T, to: T) -> Unit)
96104
}

common/src/main/kotlin/com/lambda/gui/impl/clickgui/buttons/setting/EnumSlider.kt

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ class EnumSlider <T : Enum<T>> (
2121
private val values = setting.enumValues
2222
private val enumSize = values.size
2323

24-
override val progress get() = if (dragProgress != -1.0) dragProgress else
25-
transform(value.ordinal.toDouble(), 0.0, enumSize - 1.0, 0.0, 1.0)
26-
27-
private var dragProgress = -1.0
24+
override val progress get() = transform(value.ordinal.toDouble(), 0.0, enumSize - 1.0, 0.0, 1.0)
25+
private var valueSetByDrag = false
2826

2927
override fun onEvent(e: GuiEvent) {
3028
super.onEvent(e)
@@ -47,17 +45,16 @@ class EnumSlider <T : Enum<T>> (
4745
override fun setValueByProgress(progress: Double) {
4846
val entryIndex = floor(progress * enumSize).toInt().coerceIn(0, enumSize - 1)
4947
value = values[entryIndex]
50-
dragProgress = progress
48+
valueSetByDrag = true
5149
}
5250

53-
override fun onPress(e: GuiEvent.MouseClick) {}
51+
override fun onPress(e: GuiEvent.MouseClick) {
52+
valueSetByDrag = false
53+
}
5454

5555
override fun onRelease(e: GuiEvent.MouseClick) {
56-
if (dragProgress == -1.0) {
57-
setting.next()
58-
playClickSound()
59-
}
60-
61-
dragProgress = -1.0
56+
if (valueSetByDrag) return
57+
playClickSound()
58+
setting.next()
6259
}
6360
}

common/src/main/kotlin/com/lambda/gui/impl/clickgui/buttons/setting/NumberSlider.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ class NumberSlider <N>(
6262
inputBar.toggle()
6363
}
6464

65-
override fun slide(mouse: Vec2d) {
66-
if (!inputBar.isActive) super.slide(mouse)
65+
override fun slide() {
66+
if (!inputBar.isActive) super.slide()
6767
}
6868

6969
override fun setValueByProgress(progress: Double) {

common/src/main/kotlin/com/lambda/gui/impl/clickgui/buttons/setting/Slider.kt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@ abstract class Slider <V : Any, T : AbstractSetting<V>>(
1919
setting: T, owner: ChildLayer.Drawable<SettingButton<*, *>, ModuleButton>
2020
) : SettingButton<V, T>(setting, owner) {
2121
protected abstract val progress: Double
22-
private val progressAnimation by animation.exp(::progress, 0.6)
22+
// Force this slider to follow mouse when dragging instead of rounding to the closest setting value
23+
private val progressAnimation by animation.exp({ mouseX?.let(::getProgressByMouse) ?: progress }, 0.6)
2324
private val renderProgress get() = lerp(0.0, progressAnimation, showAnimation)
2425

2526
protected abstract fun setValueByProgress(progress: Double)
2627
private var lastPlayedValue = value
2728
private var lastPlayedTiming = 0L
2829

30+
private var mouseX: Double? = null; get() {
31+
if (activeButton != Mouse.Button.Left) field = null
32+
return field
33+
}
34+
2935
override fun onEvent(e: GuiEvent) {
3036
super.onEvent(e)
3137

@@ -38,23 +44,23 @@ abstract class Slider <V : Any, T : AbstractSetting<V>>(
3844
color = GuiSettings.mainColor.multAlpha(showAnimation * 0.3),
3945
shade = GuiSettings.shade
4046
)
47+
48+
slide()
4149
}
4250

4351
is GuiEvent.MouseMove -> {
44-
slide(e.mouse)
52+
mouseX = e.mouse.x
4553
}
4654
}
4755
}
4856

4957
override fun onPress(e: GuiEvent.MouseClick) {
5058
super.onPress(e)
51-
slide(e.mouse)
59+
mouseX = e.mouse.x
5260
}
5361

54-
protected open fun slide(mouse: Vec2d) {
55-
if (activeButton != Mouse.Button.Left) return
56-
57-
setValueByProgress(transform(mouse.x, rect.left, rect.right, 0.0, 1.0).coerceIn(0.0, 1.0))
62+
protected open fun slide() = mouseX?.let { mouseX ->
63+
setValueByProgress(getProgressByMouse(mouseX))
5864
playClickSound()
5965
}
6066

@@ -67,4 +73,7 @@ abstract class Slider <V : Any, T : AbstractSetting<V>>(
6773

6874
playSound(LambdaSound.BUTTON_CLICK.event, lerp(0.9, 1.2, progress))
6975
}
76+
77+
private fun getProgressByMouse(mouseX: Double) =
78+
transform(mouseX, rect.left, rect.right, 0.0, 1.0).coerceIn(0.0, 1.0)
7079
}

common/src/main/kotlin/com/lambda/module/modules/client/GuiSettings.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ object GuiSettings : Module(
1919

2020
// General
2121
private val scaleSetting by setting("Scale", 100, 50..300, 1, visibility = { page == Page.General }).apply {
22-
onValueChangeUnsafe { _, _ ->
22+
onValueSet { _, _ ->
2323
lastChange = System.currentTimeMillis()
2424
}
2525
}
@@ -45,7 +45,7 @@ object GuiSettings : Module(
4545
}
4646

4747
private var targetScale = 2.0; get() {
48-
val update = System.currentTimeMillis() - lastChange > 1000 || !LambdaClickGui.isOpen
48+
val update = System.currentTimeMillis() - lastChange > 200 || !LambdaClickGui.isOpen
4949
if (update) field = scaleSetting / 100.0 * 2.0
5050
return field
5151
}

0 commit comments

Comments
 (0)