diff options
author | Linnea Gräf <nea@nea.moe> | 2024-07-01 03:42:51 +0200 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-07-03 21:05:51 +0200 |
commit | 5ee4b8d925eb12e068038a1fd2e1f35cdd8ef87e (patch) | |
tree | a5b0a6fbc8878ae62bb2c3a01dbb255388353fda /src/main/kotlin/moe/nea/firmament/gui/config | |
parent | dff1f9c0e2b728dba902d72816104abccc61f511 (diff) | |
download | firmament-5ee4b8d925eb12e068038a1fd2e1f35cdd8ef87e.tar.gz firmament-5ee4b8d925eb12e068038a1fd2e1f35cdd8ef87e.tar.bz2 firmament-5ee4b8d925eb12e068038a1fd2e1f35cdd8ef87e.zip |
[WIP] Remove LibGUI
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/gui/config')
10 files changed, 226 insertions, 183 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 adbeeaf..85afeb4 100644 --- a/src/main/kotlin/moe/nea/firmament/gui/config/BooleanHandler.kt +++ b/src/main/kotlin/moe/nea/firmament/gui/config/BooleanHandler.kt @@ -6,7 +6,9 @@ package moe.nea.firmament.gui.config -import io.github.cottonmc.cotton.gui.widget.WToggleButton +import io.github.notenoughupdates.moulconfig.gui.component.CenterComponent +import io.github.notenoughupdates.moulconfig.gui.component.SwitchComponent +import io.github.notenoughupdates.moulconfig.observer.GetSetter import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.boolean @@ -24,13 +26,16 @@ class BooleanHandler(val config: ManagedConfig) : ManagedConfig.OptionHandler<Bo override fun emitGuiElements(opt: ManagedOption<Boolean>, guiAppender: GuiAppender) { guiAppender.appendLabeledRow( opt.labelText, - WToggleButton(opt.labelText).apply { - guiAppender.onReload { toggle = opt.value } - setOnToggle { - opt.value = it + CenterComponent(SwitchComponent(object : GetSetter<Boolean> { + override fun get(): Boolean { + return opt.get() + } + + override fun set(newValue: Boolean) { + opt.set(newValue) config.save() } - } - ) + }, 200) + )) } } 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 7fbad06..eef6559 100644 --- a/src/main/kotlin/moe/nea/firmament/gui/config/ClickHandler.kt +++ b/src/main/kotlin/moe/nea/firmament/gui/config/ClickHandler.kt @@ -6,9 +6,9 @@ package moe.nea.firmament.gui.config -import io.github.cottonmc.cotton.gui.widget.WButton +import io.github.notenoughupdates.moulconfig.gui.component.TextComponent import kotlinx.serialization.json.JsonElement -import net.minecraft.text.Text +import moe.nea.firmament.gui.FirmButtonComponent class ClickHandler(val config: ManagedConfig, val runnable: () -> Unit) : ManagedConfig.OptionHandler<Unit> { override fun toJson(element: Unit): JsonElement? { @@ -19,12 +19,10 @@ class ClickHandler(val config: ManagedConfig, val runnable: () -> Unit) : Manage 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 { - setOnClick { - runnable() - } - }, + opt.labelText, + FirmButtonComponent( + TextComponent(opt.labelText.string), + action = runnable), ) } } 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 29a8ed6..c4eac03 100644 --- a/src/main/kotlin/moe/nea/firmament/gui/config/DurationHandler.kt +++ b/src/main/kotlin/moe/nea/firmament/gui/config/DurationHandler.kt @@ -6,18 +6,16 @@ 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 -import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment -import java.util.function.IntConsumer +import io.github.notenoughupdates.moulconfig.common.IMinecraft +import io.github.notenoughupdates.moulconfig.gui.component.RowComponent +import io.github.notenoughupdates.moulconfig.gui.component.SliderComponent +import io.github.notenoughupdates.moulconfig.gui.component.TextComponent +import io.github.notenoughupdates.moulconfig.observer.GetSetter 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 @@ -34,22 +32,31 @@ class DurationHandler(val config: ManagedConfig, val min: Duration, val max: Dur } 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 { - 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)) - config.save() - } - guiAppender.onReload { - value = opt.value.inWholeMilliseconds.toInt() - label.text = Text.literal(FirmFormatters.formatTimespan(opt.value)) - } - }, 130, 18) - }) + guiAppender.appendLabeledRow( + opt.labelText, + RowComponent( + TextComponent(IMinecraft.instance.defaultFontRenderer, + { FirmFormatters.formatTimespan(opt.value) }, + 40, + TextComponent.TextAlignment.CENTER, + true, + false), + SliderComponent( + object : GetSetter<Float> { + override fun get(): Float { + return opt.value.toDouble(DurationUnit.SECONDS).toFloat() + } + + override fun set(newValue: Float) { + opt.value = newValue.toDouble().toDuration(DurationUnit.SECONDS) + } + }, + min.toDouble(DurationUnit.SECONDS).toFloat(), + max.toDouble(DurationUnit.SECONDS).toFloat(), + 0.1F, + 130 + ) + )) } } 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 3c6e502..aabcd24 100644 --- a/src/main/kotlin/moe/nea/firmament/gui/config/GuiAppender.kt +++ b/src/main/kotlin/moe/nea/firmament/gui/config/GuiAppender.kt @@ -6,37 +6,39 @@ 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.WWidget -import io.github.cottonmc.cotton.gui.widget.data.Axis -import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment +import io.github.notenoughupdates.moulconfig.gui.GuiComponent +import io.github.notenoughupdates.moulconfig.gui.component.RowComponent +import io.github.notenoughupdates.moulconfig.gui.component.TextComponent +import io.github.notenoughupdates.moulconfig.observer.GetSetter import net.minecraft.client.gui.screen.Screen import net.minecraft.text.Text -import moe.nea.firmament.gui.WSplitPanel +import moe.nea.firmament.gui.FixedComponent class GuiAppender(val width: Int, val screenAccessor: () -> Screen) { - val panel = WBox(Axis.VERTICAL).also { - it.setSize(width, 200) - } + val panel = mutableListOf<GuiComponent>() internal val reloadables = mutableListOf<(() -> Unit)>() fun onReload(reloadable: () -> Unit) { reloadables.add(reloadable) } - fun appendLabeledRow(label: Text, right: WWidget) { + fun appendLabeledRow(label: Text, right: GuiComponent) { appendSplitRow( - WLabel(label).setVerticalAlignment(VerticalAlignment.CENTER), + TextComponent(label.string), right ) } - fun appendSplitRow(left: WWidget, right: WWidget) { - appendFullRow(WSplitPanel(left.also { it.setSize(width / 2, 18) }, right.also { it.setSize(width / 2, 18) })) + fun appendSplitRow(left: GuiComponent, right: GuiComponent) { + // TODO: make this more dynamic + // i could just make a component that allows for using half the available size + appendFullRow(RowComponent( + FixedComponent(GetSetter.constant(width / 2), null, left), + FixedComponent(GetSetter.constant(width / 2), null, right), + )) } - fun appendFullRow(widget: WWidget) { - panel.add(widget, width, 18) + fun appendFullRow(widget: GuiComponent) { + panel.add(widget) } } 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 7df98e3..a2147e8 100644 --- a/src/main/kotlin/moe/nea/firmament/gui/config/HudMetaHandler.kt +++ b/src/main/kotlin/moe/nea/firmament/gui/config/HudMetaHandler.kt @@ -6,13 +6,14 @@ package moe.nea.firmament.gui.config -import io.github.cottonmc.cotton.gui.widget.WButton +import io.github.notenoughupdates.moulconfig.gui.component.TextComponent import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.decodeFromJsonElement import kotlinx.serialization.json.encodeToJsonElement import net.minecraft.text.MutableText import net.minecraft.text.Text +import moe.nea.firmament.gui.FirmButtonComponent import moe.nea.firmament.jarvis.JarvisIntegration import moe.nea.firmament.util.MC @@ -27,14 +28,16 @@ class HudMetaHandler(val config: ManagedConfig, val label: MutableText, val widt } override fun emitGuiElements(opt: ManagedOption<HudMeta>, guiAppender: GuiAppender) { - guiAppender.appendLabeledRow(opt.labelText, WButton(Text.stringifiedTranslatable("firmament.hud.edit", label)) - .also { - it.setOnClick { - MC.screen = JarvisIntegration.jarvis.getHudEditor( - guiAppender.screenAccessor.invoke(), - listOf(opt.value) - ) - } + guiAppender.appendLabeledRow( + opt.labelText, + FirmButtonComponent( + TextComponent( + Text.stringifiedTranslatable("firmament.hud.edit", label).string), + ) { + MC.screen = JarvisIntegration.jarvis.getHudEditor( + guiAppender.screenAccessor.invoke(), + listOf(opt.value) + ) }) } } 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 77d09cb..bb6f5c4 100644 --- a/src/main/kotlin/moe/nea/firmament/gui/config/IntegerHandler.kt +++ b/src/main/kotlin/moe/nea/firmament/gui/config/IntegerHandler.kt @@ -6,17 +6,16 @@ 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 -import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment -import java.util.function.IntConsumer +import io.github.notenoughupdates.moulconfig.common.IMinecraft +import io.github.notenoughupdates.moulconfig.gui.component.RowComponent +import io.github.notenoughupdates.moulconfig.gui.component.SliderComponent +import io.github.notenoughupdates.moulconfig.gui.component.TextComponent +import io.github.notenoughupdates.moulconfig.observer.GetSetter 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 +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? { @@ -28,22 +27,32 @@ class IntegerHandler(val config: ManagedConfig, val min: Int, val max: Int) : Ma } 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 { - 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()) - config.save() - } - guiAppender.onReload { - value = opt.value - label.text = Text.literal(opt.value.toString()) - } - }, 130, 18) - }) + guiAppender.appendLabeledRow( + opt.labelText, + RowComponent( + TextComponent(IMinecraft.instance.defaultFontRenderer, + { FirmFormatters.formatCommas(opt.value, 0) }, + 40, + TextComponent.TextAlignment.CENTER, + true, + false), + SliderComponent( + object : GetSetter<Float> { + override fun get(): Float { + return opt.value.toFloat() + } + + override fun set(newValue: Float) { + opt.value = newValue.toInt() + } + }, + min.toFloat(), + max.toFloat(), + 0.1F, + 130 + ) + )) + } } diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/KeyBindingHandler.kt b/src/main/kotlin/moe/nea/firmament/gui/config/KeyBindingHandler.kt index a9d5a2e..d347101 100644 --- a/src/main/kotlin/moe/nea/firmament/gui/config/KeyBindingHandler.kt +++ b/src/main/kotlin/moe/nea/firmament/gui/config/KeyBindingHandler.kt @@ -6,8 +6,12 @@ package moe.nea.firmament.gui.config -import io.github.cottonmc.cotton.gui.widget.WButton -import io.github.cottonmc.cotton.gui.widget.data.InputResult +import io.github.notenoughupdates.moulconfig.common.IMinecraft +import io.github.notenoughupdates.moulconfig.common.MyResourceLocation +import io.github.notenoughupdates.moulconfig.deps.libninepatch.NinePatch +import io.github.notenoughupdates.moulconfig.gui.GuiImmediateContext +import io.github.notenoughupdates.moulconfig.gui.KeyboardEvent +import io.github.notenoughupdates.moulconfig.gui.component.TextComponent import org.lwjgl.glfw.GLFW import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonElement @@ -16,6 +20,7 @@ import kotlinx.serialization.json.encodeToJsonElement import net.minecraft.client.util.InputUtil import net.minecraft.text.Text import net.minecraft.util.Formatting +import moe.nea.firmament.gui.FirmButtonComponent import moe.nea.firmament.keybindings.FirmamentKeyBindings import moe.nea.firmament.keybindings.SavedKeyBinding @@ -36,19 +41,66 @@ class KeyBindingHandler(name: String, val managedConfig: ManagedConfig) : Manage var editing = false var lastPressed = 0 var lastPressedNonModifier = 0 - var updateButton: (() -> Unit)? = null - val button = object : WButton() { - override fun onKeyPressed(ch: Int, key: Int, modifiers: Int): InputResult { + var label: String = "" + var button: FirmButtonComponent? = null + fun updateLabel() { + val stroke = Text.literal("") + if (opt.value.shift) { + stroke.append("SHIFT + ") // TODO: translations? + } + if (opt.value.alt) { + stroke.append("ALT + ") + } + if (opt.value.ctrl) { + stroke.append("CTRL + ") + } + stroke.append(InputUtil.Type.KEYSYM.createFromCode(opt.value.keyCode).localizedText) + if (editing) + stroke.styled { it.withColor(Formatting.YELLOW) } + label = (stroke).string + managedConfig.save() + } + button = object : FirmButtonComponent( + TextComponent( + IMinecraft.instance.defaultFontRenderer, + { label }, + 130, + TextComponent.TextAlignment.LEFT, + false, + false + ), action = { + if (editing) { + button!!.blur() + } else { + editing = true + button!!.requestFocus() + updateLabel() + } + }) { + override fun keyboardEvent(event: KeyboardEvent, context: GuiImmediateContext): Boolean { + if (event is KeyboardEvent.KeyPressed) { + if (event.pressed) onKeyPressed(event.keycode, SavedKeyBinding.getModInt()) + else onKeyReleased(event.keycode, SavedKeyBinding.getModInt()) + } + return super.keyboardEvent(event, context) + } + + override fun getBackground(context: GuiImmediateContext): NinePatch<MyResourceLocation> { + if (editing) return activeBg + return super.getBackground(context) + } + + fun onKeyPressed(ch: Int, modifiers: Int): Boolean { if (!editing) { - return super.onKeyPressed(ch, key, modifiers) + return false } if (ch == GLFW.GLFW_KEY_ESCAPE) { lastPressedNonModifier = 0 editing = false lastPressed = 0 opt.value = SavedKeyBinding(GLFW.GLFW_KEY_UNKNOWN) - updateButton!!() - return InputResult.PROCESSED + updateLabel() + return true } if (ch == GLFW.GLFW_KEY_LEFT_SHIFT || ch == GLFW.GLFW_KEY_RIGHT_SHIFT || ch == GLFW.GLFW_KEY_LEFT_ALT || ch == GLFW.GLFW_KEY_RIGHT_ALT @@ -63,58 +115,31 @@ class KeyBindingHandler(name: String, val managedConfig: ManagedConfig) : Manage lastPressed = 0 lastPressedNonModifier = 0 } - updateButton!!() - return InputResult.PROCESSED + updateLabel() + return true } - override fun onFocusLost() { - super.onFocusLost() + override fun onLostFocus() { lastPressedNonModifier = 0 editing = false lastPressed = 0 - updateButton!!() + updateLabel() } - override fun onKeyReleased(ch: Int, key: Int, modifiers: Int): InputResult { + fun onKeyReleased(ch: Int, modifiers: Int): Boolean { if (!editing) - return super.onKeyReleased(ch, key, modifiers) + return false if (lastPressedNonModifier == ch || (lastPressedNonModifier == 0 && ch == lastPressed)) { - opt.value = SavedKeyBinding( - ch, modifiers - ) + opt.value = SavedKeyBinding(ch, modifiers) editing = false lastPressed = 0 lastPressedNonModifier = 0 } - updateButton!!() - return InputResult.PROCESSED - } - } - - fun updateLabel() { - val stroke = Text.literal("") - if (opt.value.shift) { - stroke.append("SHIFT + ") // TODO: translations? + updateLabel() + return true } - if (opt.value.alt) { - stroke.append("ALT + ") - } - if (opt.value.ctrl) { - stroke.append("CTRL + ") - } - stroke.append(InputUtil.Type.KEYSYM.createFromCode(opt.value.keyCode).localizedText) - if (editing) - stroke.styled { it.withColor(Formatting.YELLOW) } - button.setLabel(stroke) - managedConfig.save() - } - updateButton = ::updateLabel - updateButton() - button.setOnClick { - editing = true - button.requestFocus() - updateButton() } + updateLabel() guiAppender.appendLabeledRow(opt.labelText, button) } 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 8109aed..104cd52 100644 --- a/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt +++ b/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt @@ -6,17 +6,17 @@ package moe.nea.firmament.gui.config -import io.github.cottonmc.cotton.gui.client.CottonClientScreen -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.data.Axis -import io.github.cottonmc.cotton.gui.widget.data.Insets -import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment +import io.github.notenoughupdates.moulconfig.gui.CloseEventListener +import io.github.notenoughupdates.moulconfig.gui.GuiComponentWrapper +import io.github.notenoughupdates.moulconfig.gui.GuiContext +import io.github.notenoughupdates.moulconfig.gui.component.CenterComponent +import io.github.notenoughupdates.moulconfig.gui.component.ColumnComponent +import io.github.notenoughupdates.moulconfig.gui.component.PanelComponent +import io.github.notenoughupdates.moulconfig.gui.component.RowComponent +import io.github.notenoughupdates.moulconfig.gui.component.ScrollPanelComponent +import io.github.notenoughupdates.moulconfig.gui.component.TextComponent import moe.nea.jarvis.api.Point import org.lwjgl.glfw.GLFW -import kotlinx.serialization.decodeFromString import kotlinx.serialization.encodeToString import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonObject @@ -27,9 +27,8 @@ import kotlin.time.Duration import net.minecraft.client.gui.screen.Screen import net.minecraft.text.Text import moe.nea.firmament.Firmament -import moe.nea.firmament.gui.WTightScrollPanel +import moe.nea.firmament.gui.FirmButtonComponent import moe.nea.firmament.keybindings.SavedKeyBinding -import moe.nea.firmament.util.MC import moe.nea.firmament.util.ScreenUtil.setScreenLater abstract class ManagedConfig(override val name: String) : ManagedConfigElement() { @@ -150,45 +149,30 @@ abstract class ManagedConfig(override val name: String) : ManagedConfigElement() val labelText = Text.translatable("firmament.config.${name}") - fun getConfigEditor(parent: Screen? = null): CottonClientScreen { - val lwgd = LightweightGuiDescription() + fun getConfigEditor(parent: Screen? = null): Screen { var screen: Screen? = null 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 { - it.add(WButton(Text.literal("←")).also { - it.setOnClick { - if (parent != null) { - save() - setScreenLater(parent) - } else { - AllConfigsGui.showAllGuis() - } + guiapp.appendFullRow(RowComponent( + FirmButtonComponent(TextComponent("←")) { + if (parent != null) { + save() + setScreenLater(parent) + } else { + AllConfigsGui.showAllGuis() } - }) - it.add(WLabel(labelText).also { - it.verticalAlignment = VerticalAlignment.CENTER - }) - }) + } + )) sortedOptions.forEach { it.appendToGui(guiapp) } guiapp.reloadables.forEach { it() } - lwgd.setRootPanel(WTightScrollPanel(guiapp.panel).also { - it.setSize(400, 300) - }) - screen = - object : CottonClientScreen(lwgd) { - override fun init() { - latestGuiAppender = guiapp - super.init() - } - - override fun close() { - latestGuiAppender = null - save() - MC.screen = parent + val component = CenterComponent(PanelComponent(ScrollPanelComponent(400, 300, ColumnComponent(guiapp.panel)), 10, PanelComponent.DefaultBackgroundRenderer.VANILLA)) + screen = object : GuiComponentWrapper(GuiContext(component)) { + override fun close() { + if (context.onBeforeClose() == CloseEventListener.CloseAction.NO_OBJECTIONS_TO_CLOSE) { + client!!.setScreen(parent) } } + } return screen } diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/ManagedOption.kt b/src/main/kotlin/moe/nea/firmament/gui/config/ManagedOption.kt index 3ffe0b0..b3eb75e 100644 --- a/src/main/kotlin/moe/nea/firmament/gui/config/ManagedOption.kt +++ b/src/main/kotlin/moe/nea/firmament/gui/config/ManagedOption.kt @@ -6,6 +6,7 @@ package moe.nea.firmament.gui.config +import io.github.notenoughupdates.moulconfig.observer.GetSetter import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonObject import kotlin.properties.ReadWriteProperty @@ -18,7 +19,14 @@ class ManagedOption<T : Any>( val propertyName: String, val default: () -> T, val handler: ManagedConfig.OptionHandler<T> -) : ReadWriteProperty<Any?, T> { +) : ReadWriteProperty<Any?, T>, GetSetter<T> { + override fun set(newValue: T) { + this.value = newValue + } + + override fun get(): T { + return this.value + } val rawLabelText = "firmament.config.${element.name}.${propertyName}" val labelText = Text.translatable(rawLabelText) 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 a405f47..5a525c9 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,8 @@ package moe.nea.firmament.gui.config -import io.github.cottonmc.cotton.gui.widget.WTextField +import io.github.notenoughupdates.moulconfig.gui.component.TextFieldComponent +import io.github.notenoughupdates.moulconfig.observer.GetSetter import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.jsonPrimitive @@ -24,15 +25,16 @@ class StringHandler(val config: ManagedConfig) : ManagedConfig.OptionHandler<Str override fun emitGuiElements(opt: ManagedOption<String>, guiAppender: GuiAppender) { guiAppender.appendLabeledRow( opt.labelText, - WTextField(opt.labelText).apply { - maxLength = 1000 - suggestion = Text.translatableWithFallback(opt.rawLabelText + ".hint", "") - guiAppender.onReload { text = opt.value } - setChangedListener { - opt.value = it - config.save() - } - } + TextFieldComponent( + object : GetSetter<String> by opt { + override fun set(newValue: String) { + opt.set(newValue) + config.save() + } + }, + 130, + suggestion = Text.translatableWithFallback(opt.rawLabelText + ".hint", "").string + ), ) } } |