Skip to content

Commit 8b4d35d

Browse files
NopoTheGamerlineargraph
authored andcommitted
add a seperate slider component that comes with the text box
1 parent e42b2be commit 8b4d35d

File tree

3 files changed

+106
-62
lines changed

3 files changed

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

33
import io.github.notenoughupdates.moulconfig.GuiTextures
4-
import io.github.notenoughupdates.moulconfig.common.IMinecraft
54
import io.github.notenoughupdates.moulconfig.gui.GuiComponent
65
import io.github.notenoughupdates.moulconfig.gui.GuiImmediateContext
7-
import io.github.notenoughupdates.moulconfig.gui.KeyboardEvent
86
import io.github.notenoughupdates.moulconfig.gui.MouseEvent
97
import io.github.notenoughupdates.moulconfig.observer.GetSetter
10-
import java.util.function.BiFunction
118
import kotlin.math.max
129
import 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
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package io.github.notenoughupdates.moulconfig.gui.component
2+
3+
import io.github.notenoughupdates.moulconfig.common.IMinecraft
4+
import io.github.notenoughupdates.moulconfig.gui.GuiComponent
5+
import io.github.notenoughupdates.moulconfig.gui.GuiImmediateContext
6+
import io.github.notenoughupdates.moulconfig.gui.KeyboardEvent
7+
import io.github.notenoughupdates.moulconfig.gui.MouseEvent
8+
import io.github.notenoughupdates.moulconfig.observer.GetSetter
9+
import java.util.function.BiFunction
10+
import kotlin.math.max
11+
import kotlin.math.min
12+
13+
open class SliderWithTextComponent(
14+
value: GetSetter<Float>,
15+
minValue: Float,
16+
maxValue: Float,
17+
minStep: Float,
18+
width: Int,
19+
) : SliderComponent(value, minValue, maxValue, minStep, width) {
20+
21+
22+
override fun render(context: GuiImmediateContext) {
23+
context.renderContext.translate(-(width/3).toFloat(), 0f, 0f)
24+
super.render(context)
25+
context.renderContext.translate(60f, -5f, 0f)
26+
componentNumberInput.width
27+
componentNumberInput.render(context.translated(60, -5, componentNumberInput.width, 18))
28+
}
29+
30+
// I made this because I couldn't get isHovered to work with the translation
31+
private fun GuiImmediateContext.isHovered(): Boolean {
32+
return mouseX in 0 - width/3 until width - 13 && mouseY in 0 until height
33+
}
34+
35+
override fun mouseEvent(mouseEvent: MouseEvent, context: GuiImmediateContext): Boolean {
36+
if (!context.renderContext.isMouseButtonDown(0)) clicked = false
37+
if (context.isHovered() && mouseEvent is MouseEvent.Click && mouseEvent.mouseState && mouseEvent.mouseButton == 0) {
38+
clicked = true
39+
}
40+
if (clicked) {
41+
setValueFromContext(context)
42+
return true
43+
}
44+
return componentNumberInput.mouseEvent(mouseEvent, context.translated(45, -5, componentNumberInput.width, 18))
45+
}
46+
47+
override fun keyboardEvent(event: KeyboardEvent, context: GuiImmediateContext): Boolean {
48+
componentNumberInput.setShouldExpandToFit(true)
49+
return componentNumberInput.keyboardEvent(event, context)
50+
}
51+
52+
override fun setValueFromContext(context: GuiImmediateContext) {
53+
var v: Float = (context.mouseX + width/3) * (maxValue - minValue) / context.width + minValue
54+
v = min(v.toDouble(), maxValue.toDouble()).toFloat()
55+
v = max(v.toDouble(), minValue.toDouble()).toFloat()
56+
v = Math.round(v / minStep) * minStep
57+
value.set(v)
58+
}
59+
60+
private val componentNumberInput by lazy {
61+
TextFieldComponent(
62+
object : GetSetter<String> {
63+
64+
var editingBuffer: String = ""
65+
66+
override fun get(): String {
67+
if (isInFocus) return editingBuffer
68+
var num: Float
69+
try {
70+
num = value.get()
71+
} catch (e: NumberFormatException) {
72+
num = 0f
73+
}
74+
val stringNum = num.toString().removeSuffix(".0")
75+
return stringNum.also { editingBuffer = it }
76+
}
77+
78+
override fun set(newValue: String) {
79+
editingBuffer = newValue
80+
var num: Float
81+
try {
82+
num = editingBuffer.toFloat()
83+
} catch (e: NumberFormatException) {
84+
num = 0f
85+
}
86+
value.set(num).also { editingBuffer = num.toString() }
87+
}
88+
},
89+
20,
90+
GetSetter.constant(true),
91+
"",
92+
IMinecraft.instance.defaultFontRenderer
93+
)
94+
}
95+
96+
override fun <T : Any?> foldChildren(initial: T, visitor: BiFunction<GuiComponent, T, T>): T {
97+
return visitor.apply(componentNumberInput, initial)
98+
}
99+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.github.notenoughupdates.moulconfig.gui.editors;
22

33
import io.github.notenoughupdates.moulconfig.gui.GuiComponent;
4-
import io.github.notenoughupdates.moulconfig.gui.component.SliderComponent;
4+
import io.github.notenoughupdates.moulconfig.gui.component.SliderWithTextComponent;
55
import io.github.notenoughupdates.moulconfig.observer.GetSetter;
66
import io.github.notenoughupdates.moulconfig.processor.ProcessedOption;
77
import org.jetbrains.annotations.NotNull;
@@ -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, 55));
16+
component = wrapComponent(new SliderWithTextComponent((GetSetter<Float>) option.intoProperty(), minValue, maxValue, minStep, 55));
1717
}
1818

1919
public float getFloatValue() {

0 commit comments

Comments
 (0)