From b32f5da88c355645a9eaf343987f10506aa25bee Mon Sep 17 00:00:00 2001 From: nea Date: Fri, 11 Aug 2023 22:42:01 +0200 Subject: Add slightly more modular options --- .../moe/nea/firmament/gui/config/BooleanHandler.kt | 2 +- .../moe/nea/firmament/gui/config/ClickHandler.kt | 3 +- .../nea/firmament/gui/config/DurationHandler.kt | 2 +- .../moe/nea/firmament/gui/config/HudMetaHandler.kt | 2 +- .../moe/nea/firmament/gui/config/IntegerHandler.kt | 4 +- .../moe/nea/firmament/gui/config/ManagedConfig.kt | 88 +++++----------------- .../firmament/gui/config/ManagedConfigElement.kt | 12 +++ .../moe/nea/firmament/gui/config/ManagedOption.kt | 58 ++++++++++++++ .../moe/nea/firmament/gui/config/StringHandler.kt | 3 +- 9 files changed, 93 insertions(+), 81 deletions(-) create mode 100644 src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfigElement.kt create mode 100644 src/main/kotlin/moe/nea/firmament/gui/config/ManagedOption.kt (limited to 'src') 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, guiAppender: GuiAppender) { + override fun emitGuiElements(opt: ManagedOption, 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, guiAppender: GuiAppender) { + override fun emitGuiElements(opt: ManagedOption, 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, guiAppender: GuiAppender) { + override fun emitGuiElements(opt: ManagedOption, 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, guiAppender: GuiAppender) { + override fun emitGuiElements(opt: ManagedOption, 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 { 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, guiAppender: GuiAppender) { + override fun emitGuiElements(opt: ManagedOption, 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 { fun toJson(element: T): JsonElement? fun fromJson(element: JsonElement): T - fun emitGuiElements(opt: Option, guiAppender: GuiAppender) - } - - inner class Option internal constructor( - val config: ManagedConfig, - val propertyName: String, - val default: () -> T, - val handler: OptionHandler - ) : ReadWriteProperty { - - 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, guiAppender: GuiAppender) } val file = Firmament.CONFIG_DIR.resolve("$name.json") @@ -119,20 +61,25 @@ abstract class ManagedConfig(val name: String) { } - val allOptions = mutableMapOf>() - val sortedOptions = mutableListOf>() + val allOptions = mutableMapOf>() + val sortedOptions = mutableListOf>() private var latestGuiAppender: GuiAppender? = null - protected fun option(propertyName: String, default: () -> T, handler: OptionHandler): Option { + protected fun option( + propertyName: String, + default: () -> T, + handler: OptionHandler + ): ManagedOption { 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 { + protected fun toggle(propertyName: String, default: () -> Boolean): ManagedOption { return option(propertyName, default, BooleanHandler(this)) } @@ -141,7 +88,7 @@ abstract class ManagedConfig(val name: String) { min: Duration, max: Duration, default: () -> Duration, - ): Option { + ): ManagedOption { 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 { + ): ManagedOption { 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 { + ): ManagedOption { return option(propertyName, default, IntegerHandler(this, min, max)) } - protected fun button(propertyName: String, runnable: () -> Unit): Option { + protected fun button(propertyName: String, runnable: () -> Unit): ManagedOption { return option(propertyName, { }, ClickHandler(this, runnable)) } - protected fun string(propertyName: String, default: () -> String): Option { + protected fun string(propertyName: String, default: () -> String): ManagedOption { 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 + * + * 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 + * + * 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( + val element: ManagedConfigElement, + val propertyName: String, + val default: () -> T, + val handler: ManagedConfig.OptionHandler +) : ReadWriteProperty { + + 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, guiAppender: GuiAppender) { + override fun emitGuiElements(opt: ManagedOption, guiAppender: GuiAppender) { guiAppender.appendLabeledRow( opt.labelText, WTextField(opt.labelText).apply { -- cgit