aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-08-11 22:42:01 +0200
committernea <nea@nea.moe>2023-08-15 19:38:27 +0200
commitb32f5da88c355645a9eaf343987f10506aa25bee (patch)
tree0bd12cb09844907fdaf8048601b4192df17c9244
parent2b33dd1b11a0a64e1c32a3bf639d73a952b10e75 (diff)
downloadFirmament-b32f5da88c355645a9eaf343987f10506aa25bee.tar.gz
Firmament-b32f5da88c355645a9eaf343987f10506aa25bee.tar.bz2
Firmament-b32f5da88c355645a9eaf343987f10506aa25bee.zip
Add slightly more modular options
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/config/BooleanHandler.kt2
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/config/ClickHandler.kt3
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/config/DurationHandler.kt2
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/config/HudMetaHandler.kt2
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/config/IntegerHandler.kt4
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt88
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfigElement.kt12
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/config/ManagedOption.kt58
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/config/StringHandler.kt3
9 files changed, 93 insertions, 81 deletions
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 9305bad..adbeeaf 100644
--- a/src/main/kotlin/moe/nea/firmament/gui/config/BooleanHandler.kt
+++ b/src/main/kotlin/moe/nea/firmament/gui/config/BooleanHandler.kt
@@ -21,7 +21,7 @@ class BooleanHandler(val config: ManagedConfig) : ManagedConfig.OptionHandler<Bo
return element.jsonPrimitive.boolean
}
- override fun emitGuiElements(opt: ManagedConfig.Option<Boolean>, guiAppender: GuiAppender) {
+ override fun emitGuiElements(opt: ManagedOption<Boolean>, guiAppender: GuiAppender) {
guiAppender.appendLabeledRow(
opt.labelText,
WToggleButton(opt.labelText).apply {
diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/ClickHandler.kt b/src/main/kotlin/moe/nea/firmament/gui/config/ClickHandler.kt
index 33cc704..7fbad06 100644
--- a/src/main/kotlin/moe/nea/firmament/gui/config/ClickHandler.kt
+++ b/src/main/kotlin/moe/nea/firmament/gui/config/ClickHandler.kt
@@ -7,7 +7,6 @@
package moe.nea.firmament.gui.config
import io.github.cottonmc.cotton.gui.widget.WButton
-import io.github.cottonmc.cotton.gui.widget.WLabel
import kotlinx.serialization.json.JsonElement
import net.minecraft.text.Text
@@ -18,7 +17,7 @@ class ClickHandler(val config: ManagedConfig, val runnable: () -> Unit) : Manage
override fun fromJson(element: JsonElement) {}
- override fun emitGuiElements(opt: ManagedConfig.Option<Unit>, guiAppender: GuiAppender) {
+ override fun emitGuiElements(opt: ManagedOption<Unit>, guiAppender: GuiAppender) {
guiAppender.appendLabeledRow(
Text.translatable("firmament.config.${config.name}.${opt.propertyName}"),
WButton(Text.translatable("firmament.config.${config.name}.${opt.propertyName}")).apply {
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 c8de252..29a8ed6 100644
--- a/src/main/kotlin/moe/nea/firmament/gui/config/DurationHandler.kt
+++ b/src/main/kotlin/moe/nea/firmament/gui/config/DurationHandler.kt
@@ -33,7 +33,7 @@ class DurationHandler(val config: ManagedConfig, val min: Duration, val max: Dur
return element.jsonPrimitive.long.toDuration(DurationUnit.MILLISECONDS)
}
- override fun emitGuiElements(opt: ManagedConfig.Option<Duration>, guiAppender: GuiAppender) {
+ override fun emitGuiElements(opt: ManagedOption<Duration>, guiAppender: GuiAppender) {
val label =
WLabel(Text.literal(FirmFormatters.formatTimespan(opt.value))).setVerticalAlignment(VerticalAlignment.CENTER)
guiAppender.appendLabeledRow(opt.labelText, WBox(Axis.HORIZONTAL).also {
diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/HudMetaHandler.kt b/src/main/kotlin/moe/nea/firmament/gui/config/HudMetaHandler.kt
index 1e5ced8..3993bfa 100644
--- a/src/main/kotlin/moe/nea/firmament/gui/config/HudMetaHandler.kt
+++ b/src/main/kotlin/moe/nea/firmament/gui/config/HudMetaHandler.kt
@@ -26,7 +26,7 @@ class HudMetaHandler(val config: ManagedConfig, val label: MutableText, val widt
return HudMeta(Json.decodeFromJsonElement(element), label, width, height)
}
- override fun emitGuiElements(opt: ManagedConfig.Option<HudMeta>, guiAppender: GuiAppender) {
+ override fun emitGuiElements(opt: ManagedOption<HudMeta>, guiAppender: GuiAppender) {
guiAppender.appendLabeledRow(opt.labelText, WButton(Text.translatable("firmament.hud.edit", label))
.also {
it.setOnClick {
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 b8f0e2b..77d09cb 100644
--- a/src/main/kotlin/moe/nea/firmament/gui/config/IntegerHandler.kt
+++ b/src/main/kotlin/moe/nea/firmament/gui/config/IntegerHandler.kt
@@ -16,9 +16,7 @@ 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? {
@@ -29,7 +27,7 @@ class IntegerHandler(val config: ManagedConfig, val min: Int, val max: Int) : Ma
return element.jsonPrimitive.int
}
- override fun emitGuiElements(opt: ManagedConfig.Option<Int>, guiAppender: GuiAppender) {
+ override fun emitGuiElements(opt: ManagedOption<Int>, guiAppender: GuiAppender) {
val label =
WLabel(Text.literal(opt.value.toString())).setVerticalAlignment(VerticalAlignment.CENTER)
guiAppender.appendLabeledRow(opt.labelText, WBox(Axis.HORIZONTAL).also {
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 b38c8a7..56aca46 100644
--- a/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt
+++ b/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt
@@ -11,7 +11,6 @@ import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription
import io.github.cottonmc.cotton.gui.widget.WBox
import io.github.cottonmc.cotton.gui.widget.WButton
import io.github.cottonmc.cotton.gui.widget.WLabel
-import io.github.cottonmc.cotton.gui.widget.WScrollPanel
import io.github.cottonmc.cotton.gui.widget.data.Axis
import io.github.cottonmc.cotton.gui.widget.data.Insets
import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment
@@ -23,8 +22,6 @@ import kotlinx.serialization.json.JsonObject
import kotlin.io.path.createDirectories
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
@@ -33,67 +30,12 @@ import moe.nea.firmament.gui.WTightScrollPanel
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.ScreenUtil.setScreenLater
-abstract class ManagedConfig(val name: String) {
+abstract class ManagedConfig(override val name: String) : ManagedConfigElement() {
interface OptionHandler<T : Any> {
fun toJson(element: T): JsonElement?
fun fromJson(element: JsonElement): T
- fun emitGuiElements(opt: Option<T>, guiAppender: GuiAppender)
- }
-
- inner class Option<T : Any> internal constructor(
- val config: ManagedConfig,
- val propertyName: String,
- val default: () -> T,
- val handler: OptionHandler<T>
- ) : ReadWriteProperty<Any?, T> {
-
- val rawLabelText = "firmament.config.${config.name}.${propertyName}"
- val labelText = Text.translatable(rawLabelText)
-
- private lateinit var _value: T
- private var loaded = false
- var value: T
- get() {
- if (!loaded)
- load()
- return _value
- }
- set(value) {
- loaded = true
- _value = value
- }
-
- override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
- this.value = value
- }
-
- override fun getValue(thisRef: Any?, property: KProperty<*>): T {
- return value
- }
-
- private fun load() {
- if (data.containsKey(propertyName)) {
- try {
- value = handler.fromJson(data[propertyName]!!)
- } catch (e: Exception) {
- Firmament.logger.error(
- "Exception during loading of config file $name. This will reset this config.",
- e
- )
- }
- } else {
- value = default()
- }
- }
-
- internal fun toJson(): JsonElement? {
- return handler.toJson(value)
- }
-
- fun appendToGui(guiapp: GuiAppender) {
- handler.emitGuiElements(this, guiapp)
- }
+ fun emitGuiElements(opt: ManagedOption<T>, guiAppender: GuiAppender)
}
val file = Firmament.CONFIG_DIR.resolve("$name.json")
@@ -119,20 +61,25 @@ abstract class ManagedConfig(val name: String) {
}
- val allOptions = mutableMapOf<String, Option<*>>()
- val sortedOptions = mutableListOf<Option<*>>()
+ val allOptions = mutableMapOf<String, ManagedOption<*>>()
+ val sortedOptions = mutableListOf<ManagedOption<*>>()
private var latestGuiAppender: GuiAppender? = null
- protected fun <T : Any> option(propertyName: String, default: () -> T, handler: OptionHandler<T>): Option<T> {
+ protected fun <T : Any> option(
+ propertyName: String,
+ default: () -> T,
+ handler: OptionHandler<T>
+ ): ManagedOption<T> {
if (propertyName in allOptions) error("Cannot register the same name twice")
- return Option(this, propertyName, default, handler).also {
+ return ManagedOption(this, propertyName, default, handler).also {
+ it.load(data)
allOptions[propertyName] = it
sortedOptions.add(it)
}
}
- protected fun toggle(propertyName: String, default: () -> Boolean): Option<Boolean> {
+ protected fun toggle(propertyName: String, default: () -> Boolean): ManagedOption<Boolean> {
return option(propertyName, default, BooleanHandler(this))
}
@@ -141,7 +88,7 @@ abstract class ManagedConfig(val name: String) {
min: Duration,
max: Duration,
default: () -> Duration,
- ): Option<Duration> {
+ ): ManagedOption<Duration> {
return option(propertyName, default, DurationHandler(this, min, max))
}
@@ -151,7 +98,7 @@ abstract class ManagedConfig(val name: String) {
width: Int,
height: Int,
default: () -> Point,
- ): Option<HudMeta> {
+ ): ManagedOption<HudMeta> {
val label = Text.translatable("firmament.config.${name}.${propertyName}")
return option(propertyName, {
val p = default()
@@ -164,20 +111,19 @@ abstract class ManagedConfig(val name: String) {
min: Int,
max: Int,
default: () -> Int,
- ): Option<Int> {
+ ): ManagedOption<Int> {
return option(propertyName, default, IntegerHandler(this, min, max))
}
- protected fun button(propertyName: String, runnable: () -> Unit): Option<Unit> {
+ protected fun button(propertyName: String, runnable: () -> Unit): ManagedOption<Unit> {
return option(propertyName, { }, ClickHandler(this, runnable))
}
- protected fun string(propertyName: String, default: () -> String): Option<String> {
+ protected fun string(propertyName: String, default: () -> String): ManagedOption<String> {
return option(propertyName, default, StringHandler(this))
}
-
fun reloadGui() {
latestGuiAppender?.reloadables?.forEach {it() }
}
diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfigElement.kt b/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfigElement.kt
new file mode 100644
index 0000000..1674007
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfigElement.kt
@@ -0,0 +1,12 @@
+/*
+ * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+package moe.nea.firmament.gui.config
+
+abstract class ManagedConfigElement {
+ abstract val name: String
+
+}
diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/ManagedOption.kt b/src/main/kotlin/moe/nea/firmament/gui/config/ManagedOption.kt
new file mode 100644
index 0000000..3ffe0b0
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/gui/config/ManagedOption.kt
@@ -0,0 +1,58 @@
+/*
+ * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+package moe.nea.firmament.gui.config
+
+import kotlinx.serialization.json.JsonElement
+import kotlinx.serialization.json.JsonObject
+import kotlin.properties.ReadWriteProperty
+import kotlin.reflect.KProperty
+import net.minecraft.text.Text
+import moe.nea.firmament.Firmament
+
+class ManagedOption<T : Any>(
+ val element: ManagedConfigElement,
+ val propertyName: String,
+ val default: () -> T,
+ val handler: ManagedConfig.OptionHandler<T>
+) : ReadWriteProperty<Any?, T> {
+
+ val rawLabelText = "firmament.config.${element.name}.${propertyName}"
+ val labelText = Text.translatable(rawLabelText)
+
+ lateinit var value: T
+
+ override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
+ this.value = value
+ }
+
+ override fun getValue(thisRef: Any?, property: KProperty<*>): T {
+ return value
+ }
+
+ fun load(root: JsonElement) {
+ if (root is JsonObject && root.containsKey(propertyName)) {
+ try {
+ value = handler.fromJson(root[propertyName]!!)
+ return
+ } catch (e: Exception) {
+ Firmament.logger.error(
+ "Exception during loading of config file ${element.name}. This will reset this config.",
+ e
+ )
+ }
+ }
+ value = default()
+ }
+
+ fun toJson(): JsonElement? {
+ return handler.toJson(value)
+ }
+
+ fun appendToGui(guiapp: GuiAppender) {
+ handler.emitGuiElements(this, guiapp)
+ }
+}
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 d15b272..a405f47 100644
--- a/src/main/kotlin/moe/nea/firmament/gui/config/StringHandler.kt
+++ b/src/main/kotlin/moe/nea/firmament/gui/config/StringHandler.kt
@@ -6,7 +6,6 @@
package moe.nea.firmament.gui.config
-import io.github.cottonmc.cotton.gui.widget.WLabel
import io.github.cottonmc.cotton.gui.widget.WTextField
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonPrimitive
@@ -22,7 +21,7 @@ class StringHandler(val config: ManagedConfig) : ManagedConfig.OptionHandler<Str
return element.jsonPrimitive.content
}
- override fun emitGuiElements(opt: ManagedConfig.Option<String>, guiAppender: GuiAppender) {
+ override fun emitGuiElements(opt: ManagedOption<String>, guiAppender: GuiAppender) {
guiAppender.appendLabeledRow(
opt.labelText,
WTextField(opt.labelText).apply {