From ac151c8ebc4c5546795cdbf5b0c179183e2c71d1 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Mon, 15 Jan 2024 00:32:43 +0100 Subject: Add Pristine Profit Tracker --- .../moe/nea/firmament/util/MoulConfigUtils.kt | 52 +++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) (limited to 'src/main/kotlin/moe/nea/firmament/util/MoulConfigUtils.kt') diff --git a/src/main/kotlin/moe/nea/firmament/util/MoulConfigUtils.kt b/src/main/kotlin/moe/nea/firmament/util/MoulConfigUtils.kt index bea3bc6..9ca0a73 100644 --- a/src/main/kotlin/moe/nea/firmament/util/MoulConfigUtils.kt +++ b/src/main/kotlin/moe/nea/firmament/util/MoulConfigUtils.kt @@ -8,10 +8,60 @@ package moe.nea.firmament.util import io.github.moulberry.moulconfig.common.MyResourceLocation import io.github.moulberry.moulconfig.gui.GuiContext +import io.github.moulberry.moulconfig.xml.ChildCount +import io.github.moulberry.moulconfig.xml.XMLContext +import io.github.moulberry.moulconfig.xml.XMLGuiLoader import io.github.moulberry.moulconfig.xml.XMLUniverse +import javax.xml.namespace.QName +import me.shedaniel.math.Color +import org.w3c.dom.Element +import moe.nea.firmament.gui.BarComponent object MoulConfigUtils { - val universe = XMLUniverse.getDefaultUniverse() + val firmUrl = "http://nea.moe/Firmament" + val universe = XMLUniverse.getDefaultUniverse().also { uni -> + uni.registerMapper(java.awt.Color::class.java) { + if (it.startsWith("#")) { + val hexString = it.substring(1) + val hex = hexString.toInt(16) + if (hexString.length == 6) { + return@registerMapper java.awt.Color(hex) + } + if (hexString.length == 8) { + return@registerMapper java.awt.Color(hex, true) + } + error("Hexcolor $it needs to be exactly 6 or 8 hex digits long") + } + return@registerMapper java.awt.Color(it.toInt(), true) + } + uni.registerMapper(Color::class.java) { + val color = uni.mapXMLObject(it, java.awt.Color::class.java) + Color.ofRGBA(color.red, color.green, color.blue, color.alpha) + } + uni.registerLoader(object : XMLGuiLoader { + override fun getName(): QName { + return QName(firmUrl, "Bar") + } + + override fun createInstance(context: XMLContext<*>, element: Element): BarComponent { + return BarComponent( + context.getPropertyFromAttribute(element, QName("progress"), Double::class.java)!!, + context.getPropertyFromAttribute(element, QName("total"), Double::class.java)!!, + context.getPropertyFromAttribute(element, QName("fillColor"), Color::class.java)!!.get(), + context.getPropertyFromAttribute(element, QName("emptyColor"), Color::class.java)!!.get(), + ) + } + + override fun getChildCount(): ChildCount { + return ChildCount.NONE + } + + override fun getAttributeNames(): Map { + return mapOf("progress" to true, "total" to true, "emptyColor" to true, "fillColor" to true) + } + }) + } + fun loadGui(name: String, bindTo: Any): GuiContext { return GuiContext(universe.load(bindTo, MyResourceLocation("firmament", "gui/$name.xml"))) } -- cgit