Skip to content

Commit

Permalink
Merge pull request #36 from way-zer/3.0
Browse files Browse the repository at this point in the history
解锁 更多Cell的属性
  • Loading branch information
way-zer authored Jan 30, 2024
2 parents 06f000a + 371abbd commit e264ac8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,33 @@ import cf.wayzer.contentsTweaker.CTNode
import cf.wayzer.contentsTweaker.ContentsTweaker
import cf.wayzer.contentsTweaker.getObjInfo
import mindustry.io.JsonIO
import java.lang.reflect.Field
import java.lang.reflect.Modifier

object ReflectResolver : ContentsTweaker.NodeCollector {
override fun collectChild(node: CTNode) {
val objInfo = node.getObjInfo<Any?>() ?: return
if (objInfo.obj == null) return
extend(node, objInfo)
}

fun extend(node: CTNode, objInfo: CTNode.ObjInfo<*>, filter: (Field) -> Boolean = { Modifier.isPublic(it.modifiers) }) {
val obj = objInfo.obj ?: return
runCatching { JsonIO.json.getFields(objInfo.type) }.getOrNull()?.forEach { entry ->
val meta = entry.value
if (!Modifier.isPublic(meta.field.modifiers)) return@forEach
node.getOrCreate(entry.key).apply {
var cls = meta.field.type
if (cls.isAnonymousClass) cls = cls.superclass
+CTNode.ObjInfo<Any?>(meta.field.get(obj), cls, meta.elementType, meta.keyType)
+object : CTNode.Modifiable<Any?>(this) {
override val currentValue: Any? get() = meta.field.get(obj)
override fun setValue0(value: Any?) {
meta.field.set(obj, value)
runCatching { JsonIO.json.getFields(objInfo.type) }.getOrNull()
?.filter { filter(it.value.field) }
?.forEach { entry ->
val meta = entry.value
node.getOrCreate(entry.key).apply {
var cls = meta.field.type
if (cls.isAnonymousClass) cls = cls.superclass
+CTNode.ObjInfo<Any?>(meta.field.get(obj), cls, meta.elementType, meta.keyType)
+object : CTNode.Modifiable<Any?>(this) {
override val currentValue: Any? get() = meta.field.get(obj)
override fun setValue0(value: Any?) {
meta.field.set(obj, value)
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,24 @@ object UIExtResolver : ContentsTweaker.NodeCollector {
}
}

//Reference arc.scene.ui.layout.Cell.clear
private val cellFields = setOf(
"minWidth", "maxWidth", "minHeight", "maxHeight",
"padTop", "padLeft", "padBottom", "padRight",
"fillX", "fillY", "expandX", "expandY", "uniformX", "uniformY", "align", "colspan",
)

private fun CTNodeTypeChecked<Cell<*>>.extendCell() {
ReflectResolver.extend(node, objInfo) { it.name in cellFields }
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])
}
node.getOrCreate("align") += CTNode.Modifier {
val v = if (it.isNumber) it.asInt() else alignMap[it.asString()] ?: error("invalid align: $it")
objInfo.obj.align(v)
}
}

private val alignMap by lazy { Align::class.java.declaredFields.associate { it.name to it.getInt(null) } }
Expand Down

0 comments on commit e264ac8

Please sign in to comment.