11package io.github.notenoughupdates.moulconfig.gui.component
22
33import io.github.notenoughupdates.moulconfig.GuiTextures
4- import io.github.notenoughupdates.moulconfig.common.IMinecraft
54import io.github.notenoughupdates.moulconfig.gui.GuiComponent
65import io.github.notenoughupdates.moulconfig.gui.GuiImmediateContext
7- import io.github.notenoughupdates.moulconfig.gui.KeyboardEvent
86import io.github.notenoughupdates.moulconfig.gui.MouseEvent
97import io.github.notenoughupdates.moulconfig.observer.GetSetter
10- import java.util.function.BiFunction
118import kotlin.math.max
129import kotlin.math.min
1310
@@ -32,7 +29,6 @@ open class SliderComponent(
3229 setValueFromContext(context)
3330 }
3431 val value: Float = value.get()
35- context.renderContext.translate(- (width/ 3 ).toFloat(), 0f , 0f )
3632 context.renderContext.color(1f , 1f , 1f , 1f )
3733 mc.bindTexture(GuiTextures .SLIDER_ON_CAP )
3834 context.renderContext.drawTexturedRect(0F , 0F , 4F , context.height.toFloat())
@@ -59,76 +55,25 @@ open class SliderComponent(
5955 }
6056 mc.bindTexture(GuiTextures .SLIDER_BUTTON )
6157 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 ))
6558 }
6659
67- fun setValueFromContext (context : GuiImmediateContext ) {
68- var v: Float = ( context.mouseX + width / 3 ) * (maxValue - minValue) / context.width + minValue
60+ open fun setValueFromContext (context : GuiImmediateContext ) {
61+ var v: Float = context.mouseX * (maxValue - minValue) / context.width + minValue
6962 v = min(v.toDouble(), maxValue.toDouble()).toFloat()
7063 v = max(v.toDouble(), minValue.toDouble()).toFloat()
7164 v = Math .round(v / minStep) * minStep
7265 value.set(v)
7366 }
7467
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-
8068 override fun mouseEvent (mouseEvent : MouseEvent , context : GuiImmediateContext ): Boolean {
8169 if (! context.renderContext.isMouseButtonDown(0 )) clicked = false
82- if (context.isHovered() && mouseEvent is MouseEvent .Click && mouseEvent.mouseState && mouseEvent.mouseButton == 0 ) {
70+ if (context.isHovered && mouseEvent is MouseEvent .Click && mouseEvent.mouseState && mouseEvent.mouseButton == 0 ) {
8371 clicked = true
8472 }
8573 if (clicked) {
8674 setValueFromContext(context)
75+ return true
8776 }
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 (isInFocus) 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- if (isInFocus) return
115- var num: Float
116- try {
117- num = editingBuffer.toFloat()
118- } catch (e: NumberFormatException ) {
119- num = 0f
120- }
121- value.set(num).also { editingBuffer = num.toString() }
122- }
123- },
124- 20 ,
125- GetSetter .constant(true ),
126- " " ,
127- IMinecraft .instance.defaultFontRenderer
128- )
129- }
130-
131- override fun <T : Any ?> foldChildren (initial : T , visitor : BiFunction <GuiComponent , T , T >): T {
132- return visitor.apply (componentNumberInput, initial)
77+ return false
13378 }
13479}
0 commit comments