Improve config gui handling
This commit is contained in:
23
src/main/kotlin/moe/nea/firmament/gui/WSplitPanel.kt
Normal file
23
src/main/kotlin/moe/nea/firmament/gui/WSplitPanel.kt
Normal file
@@ -0,0 +1,23 @@
|
||||
package moe.nea.firmament.gui
|
||||
|
||||
import io.github.cottonmc.cotton.gui.widget.WPanel
|
||||
import io.github.cottonmc.cotton.gui.widget.WPanelWithInsets
|
||||
import io.github.cottonmc.cotton.gui.widget.WWidget
|
||||
|
||||
class WSplitPanel(val left: WWidget, val right: WWidget) : WPanelWithInsets() {
|
||||
init {
|
||||
left.parent = this
|
||||
right.parent = this
|
||||
children.add(left)
|
||||
children.add(right)
|
||||
}
|
||||
|
||||
override fun layout() {
|
||||
expandToFit(left, insets)
|
||||
expandToFit(right, insets)
|
||||
(left as? WPanel)?.layout()
|
||||
(right as? WPanel)?.layout()
|
||||
left.setLocation(insets.left, insets.top)
|
||||
right.setLocation(width - insets.right - right.width, insets.top)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package moe.nea.firmament.gui.config
|
||||
|
||||
import io.github.cottonmc.cotton.gui.widget.WBox
|
||||
import io.github.cottonmc.cotton.gui.widget.WLabel
|
||||
import io.github.cottonmc.cotton.gui.widget.WSlider
|
||||
import io.github.cottonmc.cotton.gui.widget.data.Axis
|
||||
@@ -27,20 +28,11 @@ class DurationHandler(val config: ManagedConfig, val min: Duration, val max: Dur
|
||||
}
|
||||
|
||||
override fun emitGuiElements(opt: ManagedConfig.Option<Duration>, guiAppender: GuiAppender) {
|
||||
val lw = guiAppender.width / 2
|
||||
guiAppender.set(
|
||||
0, 0, lw, 1,
|
||||
WLabel(opt.labelText).setVerticalAlignment(VerticalAlignment.CENTER)
|
||||
)
|
||||
val label =
|
||||
WLabel(Text.literal(FirmFormatters.formatTimespan(opt.value))).setVerticalAlignment(VerticalAlignment.CENTER)
|
||||
guiAppender.set(lw, 0, 2, 1, label)
|
||||
guiAppender.set(
|
||||
lw + 2,
|
||||
0,
|
||||
lw - 2,
|
||||
1,
|
||||
WSlider(min.inWholeMilliseconds.toInt(), max.inWholeMilliseconds.toInt(), Axis.HORIZONTAL).apply {
|
||||
guiAppender.appendLabeledRow(opt.labelText, WBox(Axis.HORIZONTAL).also {
|
||||
it.add(label, 40, 18)
|
||||
it.add(WSlider(min.inWholeMilliseconds.toInt(), max.inWholeMilliseconds.toInt(), Axis.HORIZONTAL).apply {
|
||||
valueChangeListener = IntConsumer {
|
||||
opt.value = it.milliseconds
|
||||
label.text = Text.literal(FirmFormatters.formatTimespan(opt.value))
|
||||
@@ -50,8 +42,8 @@ class DurationHandler(val config: ManagedConfig, val min: Duration, val max: Dur
|
||||
value = opt.value.inWholeMilliseconds.toInt()
|
||||
label.text = Text.literal(FirmFormatters.formatTimespan(opt.value))
|
||||
}
|
||||
})
|
||||
guiAppender.skipRows(1)
|
||||
}, 130, 18)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,33 +18,27 @@
|
||||
|
||||
package moe.nea.firmament.gui.config
|
||||
|
||||
import io.github.cottonmc.cotton.gui.widget.WBox
|
||||
import io.github.cottonmc.cotton.gui.widget.WGridPanel
|
||||
import io.github.cottonmc.cotton.gui.widget.WLabel
|
||||
import io.github.cottonmc.cotton.gui.widget.WWidget
|
||||
import io.github.cottonmc.cotton.gui.widget.data.Axis
|
||||
import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment
|
||||
import net.minecraft.client.gui.screen.Screen
|
||||
import net.minecraft.text.Text
|
||||
import moe.nea.firmament.gui.WFixedPanel
|
||||
import moe.nea.firmament.gui.WSplitPanel
|
||||
|
||||
class GuiAppender(val width: Int, val screenAccessor: () -> Screen) {
|
||||
private var row = 0
|
||||
|
||||
|
||||
|
||||
internal val panel = WGridPanel().also { it.setGaps(4, 4) }
|
||||
internal val reloadables = mutableListOf<(() -> Unit)>()
|
||||
fun set(x: Int, y: Int, w: Int, h: Int, widget: WWidget) {
|
||||
panel.add(widget, x, y + row, w, h)
|
||||
internal val panel = WBox(Axis.VERTICAL).also {
|
||||
it.setSize(width, 200)
|
||||
}
|
||||
|
||||
internal val reloadables = mutableListOf<(() -> Unit)>()
|
||||
|
||||
fun onReload(reloadable: () -> Unit) {
|
||||
reloadables.add(reloadable)
|
||||
}
|
||||
|
||||
fun skipRows(r: Int) {
|
||||
row += r
|
||||
}
|
||||
|
||||
fun appendLabeledRow(label: Text, right: WWidget) {
|
||||
appendSplitRow(
|
||||
WLabel(label).setVerticalAlignment(VerticalAlignment.CENTER),
|
||||
@@ -53,14 +47,10 @@ class GuiAppender(val width: Int, val screenAccessor: () -> Screen) {
|
||||
}
|
||||
|
||||
fun appendSplitRow(left: WWidget, right: WWidget) {
|
||||
val lw = width / 2
|
||||
set(0, 0, lw, 1, left)
|
||||
set(lw, 0, width - lw, 1, right)
|
||||
skipRows(1)
|
||||
appendFullRow(WSplitPanel(left.also { it.setSize(width / 2, 18) }, right.also { it.setSize(width / 2, 18) }))
|
||||
}
|
||||
|
||||
fun appendFullRow(widget: WWidget) {
|
||||
set(0, 0, width, 1, widget)
|
||||
skipRows(1)
|
||||
panel.add(widget, width, 18)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package moe.nea.firmament.gui.config
|
||||
|
||||
import io.github.cottonmc.cotton.gui.widget.WBox
|
||||
import io.github.cottonmc.cotton.gui.widget.WLabel
|
||||
import io.github.cottonmc.cotton.gui.widget.WSlider
|
||||
import io.github.cottonmc.cotton.gui.widget.data.Axis
|
||||
@@ -9,7 +10,9 @@ import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import kotlinx.serialization.json.int
|
||||
import kotlinx.serialization.json.jsonPrimitive
|
||||
import kotlin.time.Duration.Companion.milliseconds
|
||||
import net.minecraft.text.Text
|
||||
import moe.nea.firmament.util.FirmFormatters
|
||||
|
||||
class IntegerHandler(val config: ManagedConfig, val min: Int, val max: Int) : ManagedConfig.OptionHandler<Int> {
|
||||
override fun toJson(element: Int): JsonElement? {
|
||||
@@ -21,20 +24,11 @@ class IntegerHandler(val config: ManagedConfig, val min: Int, val max: Int) : Ma
|
||||
}
|
||||
|
||||
override fun emitGuiElements(opt: ManagedConfig.Option<Int>, guiAppender: GuiAppender) {
|
||||
val lw = guiAppender.width / 2
|
||||
guiAppender.set(
|
||||
0, 0, lw, 1,
|
||||
WLabel(opt.labelText).setVerticalAlignment(VerticalAlignment.CENTER)
|
||||
)
|
||||
val label =
|
||||
WLabel(Text.literal(opt.value.toString())).setVerticalAlignment(VerticalAlignment.CENTER)
|
||||
guiAppender.set(lw, 0, 2, 1, label)
|
||||
guiAppender.set(
|
||||
lw + 2,
|
||||
0,
|
||||
lw - 2,
|
||||
1,
|
||||
WSlider(min, max, Axis.HORIZONTAL).apply {
|
||||
guiAppender.appendLabeledRow(opt.labelText, WBox(Axis.HORIZONTAL).also {
|
||||
it.add(label, 40, 18)
|
||||
it.add(WSlider(min, max, Axis.HORIZONTAL).apply {
|
||||
valueChangeListener = IntConsumer {
|
||||
opt.value = it
|
||||
label.text = Text.literal(opt.value.toString())
|
||||
@@ -44,8 +38,8 @@ class IntegerHandler(val config: ManagedConfig, val min: Int, val max: Int) : Ma
|
||||
value = opt.value
|
||||
label.text = Text.literal(opt.value.toString())
|
||||
}
|
||||
})
|
||||
guiAppender.skipRows(1)
|
||||
}, 130, 18)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -195,7 +195,7 @@ abstract class ManagedConfig(val name: String) {
|
||||
fun getConfigEditor(parent: Screen? = null): CottonClientScreen {
|
||||
val lwgd = LightweightGuiDescription()
|
||||
var screen: Screen? = null
|
||||
val guiapp = GuiAppender(20, { requireNotNull(screen) { "Screen Accessor called too early" } })
|
||||
val guiapp = GuiAppender(400, { requireNotNull(screen) { "Screen Accessor called too early" } })
|
||||
latestGuiAppender = guiapp
|
||||
guiapp.panel.insets = Insets.ROOT_PANEL
|
||||
guiapp.appendFullRow(WBox(Axis.HORIZONTAL).also {
|
||||
|
||||
Reference in New Issue
Block a user