Skip to content

Commit

Permalink
Merge pull request #37 from way-zer/3.0
Browse files Browse the repository at this point in the history
扩展更多 UIExt的属性
  • Loading branch information
way-zer authored Jan 30, 2024
2 parents e264ac8 + b5dab74 commit a09bb0c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 18 deletions.
6 changes: 5 additions & 1 deletion src/main/kotlin/cf/wayzer/contentsTweaker/CTNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ class CTNode private constructor() : ExtendableClass<CTExtInfo>() {
fun collectAll(): CTNode {
if (collected) return this
collected = true
resolvers.forEach { it.collectChild(this) }
for (resolver in resolvers) {
kotlin.runCatching { resolver.collectChild(this) }.onFailure { e ->
throw Exception("Fail to collectChild in $resolver", e)
}
}
get<Modifiable<Any?>>()?.let { modifiable ->
getOrCreate("=").apply {
+Modifier { json ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ object ReflectResolver : ContentsTweaker.NodeCollector {

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()
?.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)
}
val fields = runCatching { JsonIO.json.getFields(objInfo.type) }.getOrNull() ?: return
for (entry in fields) {
if (!filter(entry.value.field)) continue
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
@@ -1,8 +1,14 @@
package cf.wayzer.contentsTweaker.resolvers

import arc.Core
import arc.func.Cons
import arc.input.KeyCode
import arc.math.geom.Vec2
import arc.scene.Element
import arc.scene.Group
import arc.scene.event.ClickListener
import arc.scene.event.InputEvent
import arc.scene.event.InputListener
import arc.scene.style.Drawable
import arc.scene.ui.Button
import arc.scene.ui.Label
Expand All @@ -12,6 +18,7 @@ import arc.scene.ui.TextButton
import arc.scene.ui.layout.Cell
import arc.scene.ui.layout.Table
import arc.util.Align
import arc.util.Tmp
import cf.wayzer.contentsTweaker.*
import mindustry.Vars
import mindustry.gen.Call
Expand Down Expand Up @@ -110,6 +117,7 @@ object UIExtResolver : ContentsTweaker.NodeCollector {

private fun CTNodeTypeChecked<Table>.extendTable() {
val obj = objInfo.obj
node.getOrCreate("cellDefaults") += CTNode.ObjInfo(obj.defaults())
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")
Expand All @@ -122,11 +130,33 @@ object UIExtResolver : ContentsTweaker.NodeCollector {
}
}

class DragHandler(private val element: Element) : InputListener() {
private val last = Vec2()
override fun touchDown(event: InputEvent, x: Float, y: Float, pointer: Int, button: KeyCode?): Boolean {
event.stop()
last.set(x, y)
return true
}

override fun touchDragged(event: InputEvent, x: Float, y: Float, pointer: Int) {
event.stop()
val v = element.localToStageCoordinates(Tmp.v1.set(x, y))
element.setPosition(v.x - last.x, v.y - last.y)
element.keepInStage()
}
}

private fun CTNodeTypeChecked<Element>.extendModifiers() {
val obj = objInfo.obj
node.getOrCreate("onClick") += CTNode.Modifier {
val message = it.asString()
obj.tapped { Call.sendChatMessage(message) }
node.getOrCreate("draggable") += CTNode.Modifier { json ->
obj.listeners.removeAll { it is DragHandler }
if (json.asBoolean())
obj.addListener(DragHandler(obj))
}
node.getOrCreate("onClick") += CTNode.Modifier { json ->
val message = json.asString()
obj.listeners.removeAll { it is ClickListener }
obj.clicked(Cons {}, Cons { Call.sendChatMessage(message) })
}

node.getOrCreate("style") += CTNode.Modifier {
Expand All @@ -148,6 +178,20 @@ object UIExtResolver : ContentsTweaker.NodeCollector {
is TextButton -> obj.setText(v)
}
}

if (obj is Label) {
node.getOrCreate("fontScale") += CTNode.Modifier { json ->
val v = if (json.isNumber) json.asFloat().let { v -> FloatArray(2) { v } }
else json.asFloatArray()?.takeIf { it.size == 2 } ?: error("invalid fontScale: $json")
obj.setFontScale(v[0], v[1])
}
node.getOrCreate("fontScaleX") += CTNode.Modifier {
obj.fontScaleX = it.asFloat()
}
node.getOrCreate("fontScaleY") += CTNode.Modifier {
obj.fontScaleY = it.asFloat()
}
}
}

//Reference arc.scene.ui.layout.Cell.clear
Expand Down Expand Up @@ -175,6 +219,7 @@ object UIExtResolver : ContentsTweaker.NodeCollector {
fun createUIElement(type: String): Element = when (type) {
"Table" -> Table()
"Label" -> Label("")
"TextButton" -> TextButton("")
else -> error("TODO: not support Element: $type")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ abstract class ExtendableClass<Ext : Any> {
inline fun <reified T : Ext> get(): T? {
if (this is T) return this
return mixins.filterIsInstance<T>().let {
require(it.size <= 1) { "More than one ${T::class.java} mixin" }
require(it.size <= 1) { "More than one ${T::class.java} mixin: $it" }
it.firstOrNull()
}
}
Expand Down

0 comments on commit a09bb0c

Please sign in to comment.