Skip to content

Commit 4207793

Browse files
NopoTheGamerlineargraph
authored andcommitted
add text box to slider element
1 parent 415842c commit 4207793

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

common/src/main/java/io/github/notenoughupdates/moulconfig/gui/component/SliderComponent.kt

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package io.github.notenoughupdates.moulconfig.gui.component
22

33
import io.github.notenoughupdates.moulconfig.GuiTextures
4+
import io.github.notenoughupdates.moulconfig.common.IMinecraft
45
import io.github.notenoughupdates.moulconfig.gui.GuiComponent
56
import io.github.notenoughupdates.moulconfig.gui.GuiImmediateContext
7+
import io.github.notenoughupdates.moulconfig.gui.KeyboardEvent
68
import io.github.notenoughupdates.moulconfig.gui.MouseEvent
79
import io.github.notenoughupdates.moulconfig.observer.GetSetter
10+
import java.util.function.BiFunction
811
import kotlin.math.max
912
import kotlin.math.min
1013

@@ -29,12 +32,13 @@ open class SliderComponent(
2932
setValueFromContext(context)
3033
}
3134
val value: Float = value.get()
35+
context.renderContext.translate(-(width/3).toFloat(), 0f, 0f)
3236
context.renderContext.color(1f, 1f, 1f, 1f)
3337
mc.bindTexture(GuiTextures.SLIDER_ON_CAP)
3438
context.renderContext.drawTexturedRect(0F, 0F, 4F, context.height.toFloat())
3539
mc.bindTexture(GuiTextures.SLIDER_OFF_CAP)
3640
context.renderContext.drawTexturedRect((width - 4).toFloat(), 0F, 4F, context.height.toFloat())
37-
val sliderPosition = ((value - minValue) / (maxValue - minValue) * context.width).toInt()
41+
val sliderPosition = ((value.coerceIn(minValue..maxValue) - minValue) / (maxValue - minValue) * context.width).toInt()
3842
if (sliderPosition > 5) {
3943
mc.bindTexture(GuiTextures.SLIDER_ON_SEGMENT)
4044
context.renderContext.drawTexturedRect(4F, 0F, (sliderPosition - 4).toFloat(), context.height.toFloat())
@@ -55,25 +59,75 @@ open class SliderComponent(
5559
}
5660
mc.bindTexture(GuiTextures.SLIDER_BUTTON)
5761
context.renderContext.drawTexturedRect((sliderPosition - 4).toFloat(), 0F, 8F, context.height.toFloat())
62+
63+
context.renderContext.translate(60f, -5f, 0f)
64+
componentNumberInput.render(context.translated(60, -5, componentNumberInput.width, 18))
5865
}
5966

6067
fun setValueFromContext(context: GuiImmediateContext) {
61-
var v: Float = context.mouseX * (maxValue - minValue) / context.width + minValue
68+
var v: Float = (context.mouseX + width/3) * (maxValue - minValue) / context.width + minValue
6269
v = min(v.toDouble(), maxValue.toDouble()).toFloat()
6370
v = max(v.toDouble(), minValue.toDouble()).toFloat()
6471
v = Math.round(v / minStep) * minStep
6572
value.set(v)
6673
}
6774

75+
// I made this because I couldn't get isHovered to work with the translation
76+
private fun GuiImmediateContext.isHovered(): Boolean {
77+
return mouseX in 0 - width/3 until width && mouseY in 0 until height
78+
}
79+
6880
override fun mouseEvent(mouseEvent: MouseEvent, context: GuiImmediateContext): Boolean {
6981
if (!context.renderContext.isMouseButtonDown(0)) clicked = false
70-
if (context.isHovered && mouseEvent is MouseEvent.Click && mouseEvent.mouseState && mouseEvent.mouseButton == 0) {
82+
if (context.isHovered() && mouseEvent is MouseEvent.Click && mouseEvent.mouseState && mouseEvent.mouseButton == 0) {
7183
clicked = true
7284
}
7385
if (clicked) {
7486
setValueFromContext(context)
75-
return true
7687
}
77-
return false
88+
return componentNumberInput.mouseEvent(mouseEvent, context.translated(45, -5, componentNumberInput.width, 18))
89+
}
90+
91+
override fun keyboardEvent(event: KeyboardEvent, context: GuiImmediateContext): Boolean {
92+
return componentNumberInput.keyboardEvent(event, context)
93+
}
94+
95+
private val componentNumberInput by lazy {
96+
TextFieldComponent(
97+
object : GetSetter<String> {
98+
var editingBuffer: String = ""
99+
100+
override fun get(): String {
101+
if (isFocused) return editingBuffer
102+
var num: Float
103+
try {
104+
num = value.get()
105+
} catch (e: NumberFormatException) {
106+
num = 0f
107+
}
108+
val stringNum = num.toString().removeSuffix(".0")
109+
return stringNum.also { editingBuffer = it }
110+
}
111+
112+
override fun set(newValue: String) {
113+
editingBuffer = newValue
114+
var num: Float
115+
try {
116+
num = editingBuffer.toFloat()
117+
} catch (e: NumberFormatException) {
118+
num = 0f
119+
}
120+
value.set(num).also { editingBuffer = num.toString() }
121+
}
122+
},
123+
20,
124+
GetSetter.constant(true),
125+
"#000000",
126+
IMinecraft.instance.defaultFontRenderer
127+
)
128+
}
129+
130+
override fun <T : Any?> foldChildren(initial: T, visitor: BiFunction<GuiComponent, T, T>): T {
131+
return visitor.apply(componentNumberInput, initial)
78132
}
79133
}

common/src/main/java/io/github/notenoughupdates/moulconfig/gui/component/TextFieldComponent.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ open class TextFieldComponent(
2525
private var scrollOffset = 0
2626
private var visibleText: String? = null
2727
override fun getWidth(): Int {
28+
if (isFocused) return max(preferredWidth, font.getStringWidth(text.get()))
2829
return preferredWidth
2930
}
3031

@@ -248,10 +249,12 @@ open class TextFieldComponent(
248249
super.mouseEvent(mouseEvent, context)
249250
checkScrollOffset(context.width)
250251
updateVisibleText(context.width)
251-
if (context.isHovered && mouseEvent is Click) {
252-
if (mouseEvent.mouseState) {
252+
if (mouseEvent is Click && mouseEvent.mouseState) {
253+
if (context.isHovered) {
253254
requestFocus()
254255
return true
256+
} else {
257+
setFocus(false)
255258
}
256259
}
257260
return false

common/src/main/java/io/github/notenoughupdates/moulconfig/gui/editors/GuiOptionEditorSlider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public GuiOptionEditorSlider(ProcessedOption option, float minValue, float maxVa
1313
super(option);
1414
if (minStep < 0) minStep = 0.01f;
1515

16-
component = wrapComponent(new SliderComponent((GetSetter<Float>) option.intoProperty(), minValue, maxValue, minStep, 60));
16+
component = wrapComponent(new SliderComponent((GetSetter<Float>) option.intoProperty(), minValue, maxValue, minStep, 55));
1717
}
1818

1919
public float getFloatValue() {

0 commit comments

Comments
 (0)