Skip to content

Commit

Permalink
Merge pull request #33 from way-zer/3.0
Browse files Browse the repository at this point in the history
修复UIExt若干问题
  • Loading branch information
way-zer authored Nov 23, 2023
2 parents 8243ba8 + 258af3c commit 815b4cd
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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`
}
Expand All @@ -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")
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/cf/wayzer/contentsTweaker/CTNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ class CTNode private constructor() : ExtendableClass<CTExtInfo>() {
companion object {
private val resolvers = ContentsTweaker.resolvers
val Root = CTNode()
val Nope = CTNode()

init {
Nope.apply {
+Modifier { }
+Indexable { Nope }
}
}
}

object PatchHandler {
Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/cf/wayzer/contentsTweaker/ContentsTweaker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -66,36 +68,52 @@ object UIExtResolver : ContentsTweaker.NodeCollector {
}
node.checkObjInfoOrNull<Element>()?.extend()
node.checkObjInfoOrNull<Table>()?.extendTable()
node.checkObjInfoOrNull<Cell<*>>()?.extendCell()
}

private fun CTNodeTypeChecked<Element>.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<Element>()?.obj?.remove()
node.children.remove(id)
}
extendModifiers()
}

private fun CTNodeTypeChecked<Table>.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)
Expand Down Expand Up @@ -133,12 +151,13 @@ object UIExtResolver : ContentsTweaker.NodeCollector {
is TextButton -> obj.setText(v)
}
}
node.get<TableCellObj>()?.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<Cell<*>>.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])
}
}

Expand All @@ -149,7 +168,4 @@ object UIExtResolver : ContentsTweaker.NodeCollector {
"Label" -> Label("")
else -> error("TODO: not support Element: $type")
}
// class UIExtInfo : CTNode.CTExtInfo {
// val children = mutableMapOf<CTNode>()
// }
}

0 comments on commit 815b4cd

Please sign in to comment.