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
|
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.WLabel
|
||||||
import io.github.cottonmc.cotton.gui.widget.WSlider
|
import io.github.cottonmc.cotton.gui.widget.WSlider
|
||||||
import io.github.cottonmc.cotton.gui.widget.data.Axis
|
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) {
|
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 =
|
val label =
|
||||||
WLabel(Text.literal(FirmFormatters.formatTimespan(opt.value))).setVerticalAlignment(VerticalAlignment.CENTER)
|
WLabel(Text.literal(FirmFormatters.formatTimespan(opt.value))).setVerticalAlignment(VerticalAlignment.CENTER)
|
||||||
guiAppender.set(lw, 0, 2, 1, label)
|
guiAppender.appendLabeledRow(opt.labelText, WBox(Axis.HORIZONTAL).also {
|
||||||
guiAppender.set(
|
it.add(label, 40, 18)
|
||||||
lw + 2,
|
it.add(WSlider(min.inWholeMilliseconds.toInt(), max.inWholeMilliseconds.toInt(), Axis.HORIZONTAL).apply {
|
||||||
0,
|
|
||||||
lw - 2,
|
|
||||||
1,
|
|
||||||
WSlider(min.inWholeMilliseconds.toInt(), max.inWholeMilliseconds.toInt(), Axis.HORIZONTAL).apply {
|
|
||||||
valueChangeListener = IntConsumer {
|
valueChangeListener = IntConsumer {
|
||||||
opt.value = it.milliseconds
|
opt.value = it.milliseconds
|
||||||
label.text = Text.literal(FirmFormatters.formatTimespan(opt.value))
|
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()
|
value = opt.value.inWholeMilliseconds.toInt()
|
||||||
label.text = Text.literal(FirmFormatters.formatTimespan(opt.value))
|
label.text = Text.literal(FirmFormatters.formatTimespan(opt.value))
|
||||||
}
|
}
|
||||||
})
|
}, 130, 18)
|
||||||
guiAppender.skipRows(1)
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,33 +18,27 @@
|
|||||||
|
|
||||||
package moe.nea.firmament.gui.config
|
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.WGridPanel
|
||||||
import io.github.cottonmc.cotton.gui.widget.WLabel
|
import io.github.cottonmc.cotton.gui.widget.WLabel
|
||||||
import io.github.cottonmc.cotton.gui.widget.WWidget
|
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 io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment
|
||||||
import net.minecraft.client.gui.screen.Screen
|
import net.minecraft.client.gui.screen.Screen
|
||||||
import net.minecraft.text.Text
|
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) {
|
class GuiAppender(val width: Int, val screenAccessor: () -> Screen) {
|
||||||
private var row = 0
|
internal val panel = WBox(Axis.VERTICAL).also {
|
||||||
|
it.setSize(width, 200)
|
||||||
|
|
||||||
|
|
||||||
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 reloadables = mutableListOf<(() -> Unit)>()
|
||||||
|
|
||||||
fun onReload(reloadable: () -> Unit) {
|
fun onReload(reloadable: () -> Unit) {
|
||||||
reloadables.add(reloadable)
|
reloadables.add(reloadable)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun skipRows(r: Int) {
|
|
||||||
row += r
|
|
||||||
}
|
|
||||||
|
|
||||||
fun appendLabeledRow(label: Text, right: WWidget) {
|
fun appendLabeledRow(label: Text, right: WWidget) {
|
||||||
appendSplitRow(
|
appendSplitRow(
|
||||||
WLabel(label).setVerticalAlignment(VerticalAlignment.CENTER),
|
WLabel(label).setVerticalAlignment(VerticalAlignment.CENTER),
|
||||||
@@ -53,14 +47,10 @@ class GuiAppender(val width: Int, val screenAccessor: () -> Screen) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun appendSplitRow(left: WWidget, right: WWidget) {
|
fun appendSplitRow(left: WWidget, right: WWidget) {
|
||||||
val lw = width / 2
|
appendFullRow(WSplitPanel(left.also { it.setSize(width / 2, 18) }, right.also { it.setSize(width / 2, 18) }))
|
||||||
set(0, 0, lw, 1, left)
|
|
||||||
set(lw, 0, width - lw, 1, right)
|
|
||||||
skipRows(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun appendFullRow(widget: WWidget) {
|
fun appendFullRow(widget: WWidget) {
|
||||||
set(0, 0, width, 1, widget)
|
panel.add(widget, width, 18)
|
||||||
skipRows(1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package moe.nea.firmament.gui.config
|
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.WLabel
|
||||||
import io.github.cottonmc.cotton.gui.widget.WSlider
|
import io.github.cottonmc.cotton.gui.widget.WSlider
|
||||||
import io.github.cottonmc.cotton.gui.widget.data.Axis
|
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.JsonPrimitive
|
||||||
import kotlinx.serialization.json.int
|
import kotlinx.serialization.json.int
|
||||||
import kotlinx.serialization.json.jsonPrimitive
|
import kotlinx.serialization.json.jsonPrimitive
|
||||||
|
import kotlin.time.Duration.Companion.milliseconds
|
||||||
import net.minecraft.text.Text
|
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> {
|
class IntegerHandler(val config: ManagedConfig, val min: Int, val max: Int) : ManagedConfig.OptionHandler<Int> {
|
||||||
override fun toJson(element: Int): JsonElement? {
|
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) {
|
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 =
|
val label =
|
||||||
WLabel(Text.literal(opt.value.toString())).setVerticalAlignment(VerticalAlignment.CENTER)
|
WLabel(Text.literal(opt.value.toString())).setVerticalAlignment(VerticalAlignment.CENTER)
|
||||||
guiAppender.set(lw, 0, 2, 1, label)
|
guiAppender.appendLabeledRow(opt.labelText, WBox(Axis.HORIZONTAL).also {
|
||||||
guiAppender.set(
|
it.add(label, 40, 18)
|
||||||
lw + 2,
|
it.add(WSlider(min, max, Axis.HORIZONTAL).apply {
|
||||||
0,
|
|
||||||
lw - 2,
|
|
||||||
1,
|
|
||||||
WSlider(min, max, Axis.HORIZONTAL).apply {
|
|
||||||
valueChangeListener = IntConsumer {
|
valueChangeListener = IntConsumer {
|
||||||
opt.value = it
|
opt.value = it
|
||||||
label.text = Text.literal(opt.value.toString())
|
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
|
value = opt.value
|
||||||
label.text = Text.literal(opt.value.toString())
|
label.text = Text.literal(opt.value.toString())
|
||||||
}
|
}
|
||||||
})
|
}, 130, 18)
|
||||||
guiAppender.skipRows(1)
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -195,7 +195,7 @@ abstract class ManagedConfig(val name: String) {
|
|||||||
fun getConfigEditor(parent: Screen? = null): CottonClientScreen {
|
fun getConfigEditor(parent: Screen? = null): CottonClientScreen {
|
||||||
val lwgd = LightweightGuiDescription()
|
val lwgd = LightweightGuiDescription()
|
||||||
var screen: Screen? = null
|
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
|
latestGuiAppender = guiapp
|
||||||
guiapp.panel.insets = Insets.ROOT_PANEL
|
guiapp.panel.insets = Insets.ROOT_PANEL
|
||||||
guiapp.appendFullRow(WBox(Axis.HORIZONTAL).also {
|
guiapp.appendFullRow(WBox(Axis.HORIZONTAL).also {
|
||||||
|
|||||||
Reference in New Issue
Block a user