Skip to content

Commit 928f976

Browse files
committed
NumberSlider char filter
1 parent 3c062b3 commit 928f976

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

common/src/main/kotlin/com/lambda/gui/api/component/button/InputBarOverlay.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ abstract class InputBarOverlay (renderer: LayerEntry, owner: ChildLayer.Drawable
3535
open fun setStringValue(string: String) {}
3636
open fun setKeyValue(key: KeyCode) {}
3737

38-
open fun isCharAllowed(char: Char): Boolean = true
38+
open fun isCharAllowed(string: String, char: Char): Boolean = true
3939

4040
private val field = renderer.font {
4141
scale = lerp(0.5, 1.0, activeAnimation) - pressAnimation * 0.08
@@ -78,7 +78,7 @@ abstract class InputBarOverlay (renderer: LayerEntry, owner: ChildLayer.Drawable
7878
}
7979

8080
is GuiEvent.CharTyped -> {
81-
if (!isActive || !isCharAllowed(e.char) || isKeyBind) return
81+
if (!isActive || !isCharAllowed(field.text, e.char) || isKeyBind) return
8282
field.text += e.char
8383
typeAnimation = 1.0
8484
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ class NumberSlider <N>(
3131
override val hoverFontAnimation get() = this@NumberSlider.hoverFontAnimation
3232
override val showAnimation get() = this@NumberSlider.showAnimation
3333

34+
override fun isCharAllowed(string: String, char: Char): Boolean {
35+
return when (char) {
36+
'.' -> char !in string
37+
'-' -> string.isEmpty()
38+
else -> char.isDigit()
39+
}
40+
}
41+
3442
override fun getText() = value.let(Number::toString)
3543
override fun setStringValue(string: String) {
3644
string.toDoubleOrNull()?.let(::setValue)

0 commit comments

Comments
 (0)