From c3e1bab941f47dfd5ef5c55805015ee5d47da802 Mon Sep 17 00:00:00 2001 From: nea Date: Mon, 31 Jul 2023 21:27:07 +0200 Subject: Improve config gui handling --- .../kotlin/moe/nea/firmament/gui/WSplitPanel.kt | 23 ++++++++++++++++++ .../nea/firmament/gui/config/DurationHandler.kt | 20 +++++----------- .../moe/nea/firmament/gui/config/GuiAppender.kt | 28 +++++++--------------- .../moe/nea/firmament/gui/config/IntegerHandler.kt | 22 +++++++---------- .../moe/nea/firmament/gui/config/ManagedConfig.kt | 2 +- 5 files changed, 47 insertions(+), 48 deletions(-) create mode 100644 src/main/kotlin/moe/nea/firmament/gui/WSplitPanel.kt diff --git a/src/main/kotlin/moe/nea/firmament/gui/WSplitPanel.kt b/src/main/kotlin/moe/nea/firmament/gui/WSplitPanel.kt new file mode 100644 index 0000000..0d13e42 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/gui/WSplitPanel.kt @@ -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) + } +} diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/DurationHandler.kt b/src/main/kotlin/moe/nea/firmament/gui/config/DurationHandler.kt index bf25691..e5d2621 100644 --- a/src/main/kotlin/moe/nea/firmament/gui/config/DurationHandler.kt +++ b/src/main/kotlin/moe/nea/firmament/gui/config/DurationHandler.kt @@ -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, 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) + }) } } diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/GuiAppender.kt b/src/main/kotlin/moe/nea/firmament/gui/config/GuiAppender.kt index af83aee..7b7186a 100644 --- a/src/main/kotlin/moe/nea/firmament/gui/config/GuiAppender.kt +++ b/src/main/kotlin/moe/nea/firmament/gui/config/GuiAppender.kt @@ -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) } } diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/IntegerHandler.kt b/src/main/kotlin/moe/nea/firmament/gui/config/IntegerHandler.kt index c705f7f..c7a9a55 100644 --- a/src/main/kotlin/moe/nea/firmament/gui/config/IntegerHandler.kt +++ b/src/main/kotlin/moe/nea/firmament/gui/config/IntegerHandler.kt @@ -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 { 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, 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) + }) } } diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt b/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt index 61d23c0..8b4fc43 100644 --- a/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt +++ b/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt @@ -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 { -- cgit