diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml index 69e8615..fdf8d99 100644 --- a/.idea/kotlinc.xml +++ b/.idea/kotlinc.xml @@ -1,6 +1,6 @@ - \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 0a1a519..235b349 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,7 +1,7 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar plugins { - kotlin("jvm") version "1.8.20" + kotlin("jvm") version "1.9.0" id("com.github.johnrengelman.shadow") version "8.1.1" `maven-publish` } @@ -16,7 +16,7 @@ repositories { dependencies { implementation(kotlin("stdlib")) - compileOnly("com.github.Anuken.Arc:arc-core:v145") + compileOnly("com.github.Anuken.Arc:arc-core:v146") compileOnly("com.github.anuken.mindustryjitpack:core:v145") { exclude(group = "com.github.Anuken.Arc") } diff --git a/src/main/kotlin/cf/wayzer/contentsTweaker/CTNode.kt b/src/main/kotlin/cf/wayzer/contentsTweaker/CTNode.kt index b0a077a..eec6f06 100644 --- a/src/main/kotlin/cf/wayzer/contentsTweaker/CTNode.kt +++ b/src/main/kotlin/cf/wayzer/contentsTweaker/CTNode.kt @@ -124,6 +124,14 @@ class CTNode private constructor() : ExtendableClass() { companion object { private val resolvers = ContentsTweaker.resolvers val Root = CTNode() + val Nope = CTNode() + + init { + Nope.apply { + +Modifier { } + +Indexable { Nope } + } + } } object PatchHandler { diff --git a/src/main/kotlin/cf/wayzer/contentsTweaker/ContentsTweaker.kt b/src/main/kotlin/cf/wayzer/contentsTweaker/ContentsTweaker.kt index 5375f61..14f2d25 100644 --- a/src/main/kotlin/cf/wayzer/contentsTweaker/ContentsTweaker.kt +++ b/src/main/kotlin/cf/wayzer/contentsTweaker/ContentsTweaker.kt @@ -75,6 +75,12 @@ object ContentsTweaker { Log.infoTag("ContentsTweaker", "Reload world costs $time ms") } + //Dev test, call from js + @Suppress("unused") + fun eval(content: String) { + loadPatch("console", "{$content}") + } + //Dev test, call from js @Suppress("unused") fun exportAll() { diff --git a/src/main/kotlin/cf/wayzer/contentsTweaker/resolvers/UIExtResolver.kt b/src/main/kotlin/cf/wayzer/contentsTweaker/resolvers/UIExtResolver.kt index cb0eb4a..05b61de 100644 --- a/src/main/kotlin/cf/wayzer/contentsTweaker/resolvers/UIExtResolver.kt +++ b/src/main/kotlin/cf/wayzer/contentsTweaker/resolvers/UIExtResolver.kt @@ -51,10 +51,12 @@ import mindustry.ui.Styles * ``` */ object UIExtResolver : ContentsTweaker.NodeCollector { - class TableCellObj(val cell: Cell<*>) : CTNode.CTExtInfo - override fun collectChild(node: CTNode) { - if (Vars.headless) return + if (Vars.headless) { + if (node == CTNode.Root) + node.children["uiExt"] = CTNode.Nope + return + } if (node == CTNode.Root) { node.getOrCreate("uiExt").apply { +CTNode.ObjInfo(Core.scene.root) @@ -66,36 +68,52 @@ object UIExtResolver : ContentsTweaker.NodeCollector { } node.checkObjInfoOrNull()?.extend() node.checkObjInfoOrNull()?.extendTable() + node.checkObjInfoOrNull>()?.extendCell() } private fun CTNodeTypeChecked.extend() { val obj = objInfo.obj - node.getOrCreate("+") += CTNode.Modifier { - val type = it.remove("type")?.asString() ?: error("Must provide Element type") - val id = '#' + it.getString("name") - val childNode = node.children[id] ?: createUIElement(type).let { element -> - node.getOrCreate(id).apply { - +CTNode.ObjInfo(element) - when (obj) { - is Table -> +TableCellObj(obj.add(element))//TODO save cell - is Group -> obj.addChild(element) - else -> error("Only Group can add child element") + node += object : CTNode.Indexable { + override fun resolveIndex(key: String): CTNode? = null + override fun resolve(name: String): CTNode? { + if (name.length < 2 || name[0] != '+') return null + val idStart = name.indexOf('#') + check(idStart > 0) { "Must provide element id" } + val type = name.substring(1, idStart) + val id = name.substring(idStart) + return node.children[id] ?: createUIElement(type).let { element -> + node.getOrCreate(id).apply { + +CTNode.ObjInfo(element) + when (obj) { + is Table -> { + val cell = obj.add(element) + getOrCreate("cell") += CTNode.ObjInfo(cell) + } + + is Group -> obj.addChild(element) + else -> error("Only Group can add child element") + } } } } - //通过PatchHandler设置子属性 - CTNode.PatchHandler.handle(it, childNode) + } + node.getOrCreate("+") += CTNode.Modifier { + val type = it.remove("type")?.asString() ?: error("Must provide Element type") + val child = node.resolve("+$type#${it.getString("name")}") + CTNode.PatchHandler.handle(it, child) } node.getOrCreate("-") += CTNode.Modifier { val id = it.asString() check(id.startsWith('#')) { "Must provide element #id" } node.children[id]?.getObjInfo()?.obj?.remove() + node.children.remove(id) } extendModifiers() } private fun CTNodeTypeChecked
.extendTable() { val obj = objInfo.obj + node.getOrCreate("row") += CTNode.Modifier { obj.row() } node.getOrCreate("align") += CTNode.Modifier { val v = if (it.isNumber) it.asInt() else alignMap[it.asString()] ?: error("invalid align: $it") obj.align(v) @@ -133,12 +151,13 @@ object UIExtResolver : ContentsTweaker.NodeCollector { is TextButton -> obj.setText(v) } } - node.get()?.cell?.let { cell -> - node.getOrCreate("pad") += CTNode.Modifier { json -> - val v = if (json.isNumber) json.asFloat().let { v -> FloatArray(4) { v } } - else json.asFloatArray()?.takeIf { it.size == 4 } ?: error("invalid pad: $json") - cell.pad(v[0], v[1], v[2], v[3]) - } + } + + private fun CTNodeTypeChecked>.extendCell() { + node.getOrCreate("pad") += CTNode.Modifier { json -> + val v = if (json.isNumber) json.asFloat().let { v -> FloatArray(4) { v } } + else json.asFloatArray()?.takeIf { it.size == 4 } ?: error("invalid pad: $json") + objInfo.obj.pad(v[0], v[1], v[2], v[3]) } } @@ -149,7 +168,4 @@ object UIExtResolver : ContentsTweaker.NodeCollector { "Label" -> Label("") else -> error("TODO: not support Element: $type") } -// class UIExtInfo : CTNode.CTExtInfo { -// val children = mutableMapOf() -// } } \ No newline at end of file