aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/util/MoulConfigUtils.kt
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-06-15 19:54:47 +0200
committerLinnea Gräf <nea@nea.moe>2024-06-15 19:54:47 +0200
commitdff1f9c0e2b728dba902d72816104abccc61f511 (patch)
tree4b6df287f876851bb6e8b47657d90d0d10917e6f /src/main/kotlin/moe/nea/firmament/util/MoulConfigUtils.kt
parentdf0a0ded977a3613e57b64512ea77ff992387d43 (diff)
downloadfirmament-dff1f9c0e2b728dba902d72816104abccc61f511.tar.gz
firmament-dff1f9c0e2b728dba902d72816104abccc61f511.tar.bz2
firmament-dff1f9c0e2b728dba902d72816104abccc61f511.zip
WIP: Terrible MoulConfig rewrite
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/util/MoulConfigUtils.kt')
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/MoulConfigUtils.kt62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/util/MoulConfigUtils.kt b/src/main/kotlin/moe/nea/firmament/util/MoulConfigUtils.kt
index 34188a2..3c77dc4 100644
--- a/src/main/kotlin/moe/nea/firmament/util/MoulConfigUtils.kt
+++ b/src/main/kotlin/moe/nea/firmament/util/MoulConfigUtils.kt
@@ -1,5 +1,6 @@
/*
* SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
+ * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
@@ -7,7 +8,10 @@
package moe.nea.firmament.util
import io.github.notenoughupdates.moulconfig.common.MyResourceLocation
+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.observer.GetSetter
import io.github.notenoughupdates.moulconfig.xml.ChildCount
import io.github.notenoughupdates.moulconfig.xml.XMLContext
import io.github.notenoughupdates.moulconfig.xml.XMLGuiLoader
@@ -17,7 +21,10 @@ import java.io.File
import javax.xml.namespace.QName
import me.shedaniel.math.Color
import org.w3c.dom.Element
+import net.minecraft.client.gui.screen.Screen
import moe.nea.firmament.gui.BarComponent
+import moe.nea.firmament.gui.FirmButtonComponent
+import moe.nea.firmament.gui.FixedComponent
object MoulConfigUtils {
val firmUrl = "http://firmament.nea.moe/moulconfig"
@@ -62,6 +69,51 @@ object MoulConfigUtils {
return mapOf("progress" to true, "total" to true, "emptyColor" to true, "fillColor" to true)
}
})
+ uni.registerLoader(object : XMLGuiLoader.Basic<FirmButtonComponent> {
+ override fun getName(): QName {
+ return QName(firmUrl, "Button")
+ }
+
+ override fun createInstance(context: XMLContext<*>, element: Element): FirmButtonComponent {
+ return FirmButtonComponent(
+ context.getChildFragment(element),
+ context.getMethodFromAttribute(element, QName("onClick")),
+ context.getPropertyFromAttribute(element, QName("enabled"), Boolean::class.java)
+ ?: GetSetter.constant(true)
+ )
+ }
+
+ override fun getChildCount(): ChildCount {
+ return ChildCount.ONE
+ }
+
+ override fun getAttributeNames(): Map<String, Boolean> {
+ return mapOf("onClick" to true, "enabled" to false)
+ }
+ })
+ uni.registerLoader(object : XMLGuiLoader.Basic<FixedComponent> {
+ override fun createInstance(context: XMLContext<*>, element: Element): FixedComponent {
+ return FixedComponent(
+ context.getPropertyFromAttribute(element, QName("width"), Int::class.java)
+ ?: error("Requires width specified"),
+ context.getPropertyFromAttribute(element, QName("height"), Int::class.java)
+ ?: error("Requires height specified"),
+ context.getChildFragment(element)
+ )
+ }
+
+ override fun getName(): QName {
+ return QName(firmUrl, "Fixed")
+ }
+
+ override fun getChildCount(): ChildCount {
+ return ChildCount.ONE
+ }
+
+ override fun getAttributeNames(): Map<String, Boolean> {
+ return mapOf("width" to true, "height" to true)
+ }
+ })
}
fun generateXSD(
@@ -86,6 +138,16 @@ object MoulConfigUtils {
""".trimIndent())
}
+ fun loadScreen(name: String, bindTo: Any, parent: Screen?): Screen {
+ return object : GuiComponentWrapper(loadGui(name, bindTo)) {
+ override fun close() {
+ if (context.onBeforeClose() == CloseEventListener.CloseAction.NO_OBJECTIONS_TO_CLOSE) {
+ client!!.setScreen(parent)
+ }
+ }
+ }
+ }
+
fun loadGui(name: String, bindTo: Any): GuiContext {
return GuiContext(universe.load(bindTo, MyResourceLocation("firmament", "gui/$name.xml")))
}