11package io.github.notenoughupdates.moulconfig.gui.component
22
33import io.github.notenoughupdates.moulconfig.GuiTextures
4+ import io.github.notenoughupdates.moulconfig.common.IMinecraft
45import io.github.notenoughupdates.moulconfig.gui.GuiComponent
56import io.github.notenoughupdates.moulconfig.gui.GuiImmediateContext
7+ import io.github.notenoughupdates.moulconfig.gui.KeyboardEvent
68import io.github.notenoughupdates.moulconfig.gui.MouseEvent
79import io.github.notenoughupdates.moulconfig.observer.GetSetter
10+ import java.util.function.BiFunction
811import kotlin.math.max
912import 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}
0 commit comments