diff options
author | nea <nea@nea.moe> | 2023-07-23 01:35:16 +0200 |
---|---|---|
committer | nea <nea@nea.moe> | 2023-07-23 01:35:16 +0200 |
commit | 91febc31ad8b34ad9c2bd158a445f774461286c0 (patch) | |
tree | c962dd461dbfa41335672bff2b7862c101ba2aba /src/main/kotlin/moe/nea/firmament/gui | |
parent | cdf3938b778188211ad316d16381e0d8c7beac75 (diff) | |
download | firmament-91febc31ad8b34ad9c2bd158a445f774461286c0.tar.gz firmament-91febc31ad8b34ad9c2bd158a445f774461286c0.tar.bz2 firmament-91febc31ad8b34ad9c2bd158a445f774461286c0.zip |
Make image preview scalable
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/gui')
4 files changed, 113 insertions, 7 deletions
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 f298c76..af83aee 100644 --- a/src/main/kotlin/moe/nea/firmament/gui/config/GuiAppender.kt +++ b/src/main/kotlin/moe/nea/firmament/gui/config/GuiAppender.kt @@ -22,10 +22,14 @@ import io.github.cottonmc.cotton.gui.widget.WGridPanel import io.github.cottonmc.cotton.gui.widget.WLabel import io.github.cottonmc.cotton.gui.widget.WWidget import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment +import net.minecraft.client.gui.screen.Screen import net.minecraft.text.Text -class GuiAppender(val width: Int) { +class GuiAppender(val width: Int, val screenAccessor: () -> Screen) { private var row = 0 + + + 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) { diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/HudMetaHandler.kt b/src/main/kotlin/moe/nea/firmament/gui/config/HudMetaHandler.kt new file mode 100644 index 0000000..f8d2c3e --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/gui/config/HudMetaHandler.kt @@ -0,0 +1,34 @@ +package moe.nea.firmament.gui.config + +import io.github.cottonmc.cotton.gui.widget.WButton +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.jarvis.JarvisIntegration +import moe.nea.firmament.util.MC + +class HudMetaHandler(val config: ManagedConfig, val label: MutableText, val width: Int, val height: Int) : + ManagedConfig.OptionHandler<HudMeta> { + override fun toJson(element: HudMeta): JsonElement? { + return Json.encodeToJsonElement(element.position) + } + + override fun fromJson(element: JsonElement): HudMeta { + return HudMeta(Json.decodeFromJsonElement(element), label, width, height) + } + + override fun emitGuiElements(opt: ManagedConfig.Option<HudMeta>, guiAppender: GuiAppender) { + guiAppender.appendLabeledRow(opt.labelText, WButton(Text.translatable("firmament.hud.edit", label)) + .also { + it.setOnClick { + MC.screen = JarvisIntegration.jarvis.getHudEditor( + guiAppender.screenAccessor.invoke(), + listOf(opt.value) + ) + } + }) + } +} diff --git a/src/main/kotlin/moe/nea/firmament/gui/config/JAnyHud.kt b/src/main/kotlin/moe/nea/firmament/gui/config/JAnyHud.kt new file mode 100644 index 0000000..5470f9b --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/gui/config/JAnyHud.kt @@ -0,0 +1,46 @@ +package moe.nea.firmament.gui.config + +import moe.nea.jarvis.api.JarvisHud +import moe.nea.jarvis.api.JarvisScalable +import kotlinx.serialization.Serializable +import net.minecraft.text.Text + +@Serializable +data class HudPosition( + var x: Double, + var y: Double, + var scale: Float, +) + + +data class HudMeta( + val position: HudPosition, + private val label: Text, + private val width: Int, + private val height: Int, +) : JarvisScalable, JarvisHud { + override fun getX(): Double = position.x + + override fun setX(newX: Double) { + position.x = newX + } + + override fun getY(): Double = position.y + + override fun setY(newY: Double) { + position.y = newY + } + + override fun getLabel(): Text = label + + override fun getWidth(): Int = width + + override fun getHeight(): Int = height + + override fun getScale(): Float = position.scale + + override fun setScale(newScale: Float) { + position.scale = newScale + } + +} 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 0204448..83d95ca 100644 --- a/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt +++ b/src/main/kotlin/moe/nea/firmament/gui/config/ManagedConfig.kt @@ -21,6 +21,7 @@ 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.data.Insets +import moe.nea.jarvis.api.Point import kotlinx.serialization.decodeFromString import kotlinx.serialization.encodeToString import kotlinx.serialization.json.JsonElement @@ -150,6 +151,19 @@ abstract class ManagedConfig(val name: String) { } + protected fun position( + propertyName: String, + width: Int, + height: Int, + default: () -> Point, + ): Option<HudMeta> { + val label = Text.translatable("firmament.config.${name}.${propertyName}") + return option(propertyName, { + val p = default() + HudMeta(HudPosition(p.x, p.y, 1F), label, width, height) + }, HudMetaHandler(this, label, width, height)) + } + protected fun integer( propertyName: String, min: Int, @@ -175,18 +189,26 @@ abstract class ManagedConfig(val name: String) { fun getConfigEditor(parent: Screen? = null): CottonClientScreen { val lwgd = LightweightGuiDescription() - val guiapp = GuiAppender(20) + var screen: Screen? = null + val guiapp = GuiAppender(20, { requireNotNull(screen) { "Screen Accessor called too early" } }) latestGuiAppender = guiapp guiapp.panel.insets = Insets.ROOT_PANEL sortedOptions.forEach { it.appendToGui(guiapp) } guiapp.reloadables.forEach { it() } lwgd.setRootPanel(guiapp.panel) - return object : CottonClientScreen(lwgd) { - override fun close() { - latestGuiAppender = null - MC.screen = parent + screen = + object : CottonClientScreen(lwgd) { + override fun init() { + latestGuiAppender = guiapp + super.init() + } + + override fun close() { + latestGuiAppender = null + MC.screen = parent + } } - } + return screen } fun showConfigEditor(parent: Screen? = null) { |