aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/gui
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-07-11 21:01:58 +0200
committernea <nea@nea.moe>2023-07-11 21:01:58 +0200
commit4d93f475aadc42c4bf83c3a0749af41659235c71 (patch)
treec8e01710defe66e06c50fa962c72fdac66a35a1f /src/main/kotlin/moe/nea/firmament/gui
parent4444fcca44d9a53c8162d69e0e9f19fd214c2f54 (diff)
downloadfirmament-4d93f475aadc42c4bf83c3a0749af41659235c71.tar.gz
firmament-4d93f475aadc42c4bf83c3a0749af41659235c71.tar.bz2
firmament-4d93f475aadc42c4bf83c3a0749af41659235c71.zip
Bulk commit
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/gui')
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/WSpacer.kt19
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/config/BooleanHandler.kt3
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/config/DurationHandler.kt57
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/config/GuiAppender.kt8
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/config/IntegerHandler.kt51
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt20
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/config/StringHandler.kt1
7 files changed, 152 insertions, 7 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/gui/WSpacer.kt b/src/main/kotlin/moe/nea/firmament/gui/WSpacer.kt
new file mode 100644
index 0000000..42d08fe
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/gui/WSpacer.kt
@@ -0,0 +1,19 @@
+package moe.nea.firmament.gui
+
+import io.github.cottonmc.cotton.gui.widget.WPanel
+import io.github.cottonmc.cotton.gui.widget.WWidget
+
+class WSpacer(val child: WWidget, val spaceLeft: Int) : WPanel() {
+ init {
+ children.add(child)
+ child.setLocation(spaceLeft, 0)
+ }
+
+ override fun getWidth(): Int {
+ return child.width + spaceLeft
+ }
+
+ override fun getHeight(): Int {
+ return child.height
+ }
+}
diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/BooleanHandler.kt b/src/main/kotlin/moe/nea/firmament/gui/config/BooleanHandler.kt
index 7808192..df30f6d 100644
--- a/src/main/kotlin/moe/nea/firmament/gui/config/BooleanHandler.kt
+++ b/src/main/kotlin/moe/nea/firmament/gui/config/BooleanHandler.kt
@@ -18,14 +18,11 @@
package moe.nea.firmament.gui.config
-import io.github.cottonmc.cotton.gui.widget.WLabel
import io.github.cottonmc.cotton.gui.widget.WToggleButton
-import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.boolean
import kotlinx.serialization.json.jsonPrimitive
-import net.minecraft.text.Text
class BooleanHandler(val config: ManagedConfig) : ManagedConfig.OptionHandler<Boolean> {
override fun toJson(element: Boolean): JsonElement? {
diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/DurationHandler.kt b/src/main/kotlin/moe/nea/firmament/gui/config/DurationHandler.kt
new file mode 100644
index 0000000..bf25691
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/gui/config/DurationHandler.kt
@@ -0,0 +1,57 @@
+package moe.nea.firmament.gui.config
+
+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
+import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment
+import java.util.function.IntConsumer
+import kotlinx.serialization.json.JsonElement
+import kotlinx.serialization.json.JsonPrimitive
+import kotlinx.serialization.json.jsonPrimitive
+import kotlinx.serialization.json.long
+import kotlin.time.Duration
+import kotlin.time.Duration.Companion.milliseconds
+import kotlin.time.DurationUnit
+import kotlin.time.toDuration
+import net.minecraft.text.Text
+import moe.nea.firmament.util.FirmFormatters
+
+class DurationHandler(val config: ManagedConfig, val min: Duration, val max: Duration) :
+ ManagedConfig.OptionHandler<Duration> {
+ override fun toJson(element: Duration): JsonElement? {
+ return JsonPrimitive(element.inWholeMilliseconds)
+ }
+
+ override fun fromJson(element: JsonElement): Duration {
+ return element.jsonPrimitive.long.toDuration(DurationUnit.MILLISECONDS)
+ }
+
+ 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 {
+ valueChangeListener = IntConsumer {
+ opt.value = it.milliseconds
+ label.text = Text.literal(FirmFormatters.formatTimespan(opt.value))
+ config.save()
+ }
+ guiAppender.onReload {
+ value = opt.value.inWholeMilliseconds.toInt()
+ label.text = Text.literal(FirmFormatters.formatTimespan(opt.value))
+ }
+ })
+ guiAppender.skipRows(1)
+ }
+
+}
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 d9d32f0..f298c76 100644
--- a/src/main/kotlin/moe/nea/firmament/gui/config/GuiAppender.kt
+++ b/src/main/kotlin/moe/nea/firmament/gui/config/GuiAppender.kt
@@ -29,7 +29,7 @@ class GuiAppender(val width: Int) {
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, w, h)
+ panel.add(widget, x, y + row, w, h)
}
@@ -50,13 +50,13 @@ class GuiAppender(val width: Int) {
fun appendSplitRow(left: WWidget, right: WWidget) {
val lw = width / 2
- set(0, row, lw, 1, left)
- set(lw, row, width - lw, 1, right)
+ set(0, 0, lw, 1, left)
+ set(lw, 0, width - lw, 1, right)
skipRows(1)
}
fun appendFullRow(widget: WWidget) {
- set(0, row, width, 1, widget)
+ set(0, 0, width, 1, widget)
skipRows(1)
}
}
diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/IntegerHandler.kt b/src/main/kotlin/moe/nea/firmament/gui/config/IntegerHandler.kt
new file mode 100644
index 0000000..c705f7f
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/gui/config/IntegerHandler.kt
@@ -0,0 +1,51 @@
+package moe.nea.firmament.gui.config
+
+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
+import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment
+import java.util.function.IntConsumer
+import kotlinx.serialization.json.JsonElement
+import kotlinx.serialization.json.JsonPrimitive
+import kotlinx.serialization.json.int
+import kotlinx.serialization.json.jsonPrimitive
+import net.minecraft.text.Text
+
+class IntegerHandler(val config: ManagedConfig, val min: Int, val max: Int) : ManagedConfig.OptionHandler<Int> {
+ override fun toJson(element: Int): JsonElement? {
+ return JsonPrimitive(element)
+ }
+
+ override fun fromJson(element: JsonElement): Int {
+ return element.jsonPrimitive.int
+ }
+
+ 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 {
+ valueChangeListener = IntConsumer {
+ opt.value = it
+ label.text = Text.literal(opt.value.toString())
+ config.save()
+ }
+ guiAppender.onReload {
+ value = opt.value
+ label.text = Text.literal(opt.value.toString())
+ }
+ })
+ guiAppender.skipRows(1)
+ }
+
+}
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 417a470..2115fb0 100644
--- a/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt
+++ b/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt
@@ -30,6 +30,7 @@ import kotlin.io.path.readText
import kotlin.io.path.writeText
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
+import kotlin.time.Duration
import net.minecraft.client.gui.screen.Screen
import net.minecraft.text.Text
import moe.nea.firmament.Firmament
@@ -139,6 +140,25 @@ abstract class ManagedConfig(val name: String) {
return option(propertyName, default, BooleanHandler(this))
}
+ protected fun duration(
+ propertyName: String,
+ min: Duration,
+ max: Duration,
+ default: () -> Duration,
+ ): Option<Duration> {
+ return option(propertyName, default, DurationHandler(this, min, max))
+ }
+
+
+ protected fun integer(
+ propertyName: String,
+ min: Int,
+ max: Int,
+ default: () -> Int,
+ ): Option<Int> {
+ return option(propertyName, default, IntegerHandler(this, min, max))
+ }
+
protected fun button(propertyName: String, runnable: () -> Unit): Option<Unit> {
return option(propertyName, { }, ClickHandler(this, runnable))
}
diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/StringHandler.kt b/src/main/kotlin/moe/nea/firmament/gui/config/StringHandler.kt
index 84d24b5..a9c7ebf 100644
--- a/src/main/kotlin/moe/nea/firmament/gui/config/StringHandler.kt
+++ b/src/main/kotlin/moe/nea/firmament/gui/config/StringHandler.kt
@@ -38,6 +38,7 @@ class StringHandler(val config: ManagedConfig) : ManagedConfig.OptionHandler<Str
guiAppender.appendLabeledRow(
opt.labelText,
WTextField(opt.labelText).apply {
+ maxLength = 1000
suggestion = Text.translatableWithFallback(opt.rawLabelText + ".hint", "")
guiAppender.onReload { text = opt.value }
setChangedListener {