界面:补上积木的「节点信息」页(名字/说明/入口/出口)

用户:"积木的节点信息?"

问题:积木和节点信息**是脱节的**。在积木界面看不到这个节点有哪些入口/出口;
要改名字、加入口,得切到「看代码」去手改那段 Python 字典 ——
而加一个入口在字典里是 `{"type": "str", "name": "文本", "value": ""}`,
用户得记住三个键、引号别写错、逗号别忘。**这跟搭积木的初衷是反的。**

【新增 NodeDecl.kt】
把声明变成一个可编辑的结构,界面编辑它、再生成回 Python:
  · NodeDecl(name, desc, inputs, outputs)
  · toPython()    —— 生成那段声明
  · parseDecl()   —— 从已有的 Python 里解析回来(打开别人的节点时要有)
  · pyLit()       —— 生成安全的 Python 字面量(中文、引号、反斜杠都能放)

★ parseDecl 不写完整的 Python 解析器:只认我们自己生成的那种形状
  (`"key": [ {...}, ... ]`),认不出来就返回默认值,
  原文还留在「代码」页里 —— 写通用解析器不值得,而认不出来的情况用户能看见原文。

【积木编辑器加了一个视图】
顶栏变成三个:`积木 | 信息 | 代码`
信息页:
    节点名字    [我的节点]
    说明        [说明它干什么(也当工具说明给模型看)]

    入口(图上左边的引脚)
      [文本] [文本] [默认值] ✕      ← 类型点一下循环换(只有五种,比弹菜单快)
      + 加一个
    出口(图上右边的引脚)
      [执行] [好了] ✕
      [文本] [结果] ✕
      + 加一个

  类型用颜色区分:执行=灰(白三角、只排顺序),数据=蓝(彩色圆点、传值)。

【积木引用声明的名字】
★ 这是"同一份声明"的关键:改名字之后,积木里的选项要跟着变。
  从积木盒子加「读输入 / 设置输出 / 走分支」时,默认值取**这个节点声明的引脚名**,
  而不是模板里写死的"文本""列表" ——
  用户的入口可能叫「正文」「素材」,写死的名字一放上去就是 KeyError。
  取不到才退回模板默认值。

【验证】
- 会议引擎 40/40,Node 与 Python 16/16 逐用例一致
- APK 构建通过、装机通过
- 截图确认:三个视图切换、名字/说明可改、入口出口的增删、
  类型点一下换色(文本=蓝 / 执行=灰)
This commit is contained in:
l
2026-10-12 12:05:43 +08:00
parent 6c0a9736fc
commit ed21ff6640
4 changed files with 433 additions and 84 deletions

View File

@@ -237,10 +237,188 @@ ToolName = {
// 界面
// --------------------------------------------------------------------------- //
/**
* 节点信息:名字、说明、入口、出口。
*
* 这一页决定了三件事,所以它必须好改:
* 1. 这个节点在「加节点」面板里叫什么、说明写什么
* 2. 图上这个节点有几个引脚、什么类型(界面照着声明画)
* 3. 积木里「读输入」「设置输出」「走分支」能选到哪些名字
*/
@Composable
private fun NodeInfoView(
decl: NodeDecl,
onDecl: (NodeDecl) -> Unit,
) {
Column(
Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState())
.padding(horizontal = 14.dp),
) {
Spacer(Modifier.height(10.dp))
OutlinedTextField(
value = decl.name,
onValueChange = { onDecl(decl.copy(name = it)) },
modifier = Modifier.fillMaxWidth(),
label = { Text("节点名字", fontSize = 11.sp) },
singleLine = true,
textStyle = TextStyle(fontSize = 14.sp),
)
Spacer(Modifier.height(8.dp))
OutlinedTextField(
value = decl.desc,
onValueChange = { onDecl(decl.copy(desc = it)) },
modifier = Modifier.fillMaxWidth(),
label = { Text("说明(给你自己看,也当工具说明给模型看)", fontSize = 11.sp) },
textStyle = TextStyle(fontSize = 13.sp),
maxLines = 3,
)
// ---------------- 入口 ----------------
PinListEditor(
title = "入口(图上左边的引脚)",
hint = "「执行」入口是白三角,只排顺序;其余是彩色圆点,传值。" +
"没接线时用「默认值」。",
pins = decl.inputs,
canExec = true,
showDefault = true,
onChange = { onDecl(decl.copy(inputs = it)) },
)
// ---------------- 出口 ----------------
PinListEditor(
title = "出口(图上右边的引脚)",
hint = "多个「执行」出口 = 多路分支(最多一个为真)。" +
"数据出口只有**第一个**能接线,其余的只在节点卡片上显示。",
pins = decl.outputs,
canExec = true,
showDefault = false,
onChange = { onDecl(decl.copy(outputs = it)) },
)
Spacer(Modifier.height(16.dp))
Text(
"改完这里的名字,积木里「读输入 / 设置输出 / 走分支」的选项会跟着变 —— " +
"它们是同一份声明。",
fontSize = 11.sp,
color = Doubao.Blue,
lineHeight = 16.sp,
)
Spacer(Modifier.height(30.dp))
}
}
/** 一组引脚的编辑器(入口或出口) */
@Composable
private fun PinListEditor(
title: String,
hint: String,
pins: List<PinDecl>,
canExec: Boolean,
showDefault: Boolean,
onChange: (List<PinDecl>) -> Unit,
) {
Spacer(Modifier.height(16.dp))
Text(title, fontSize = 13.sp, fontWeight = FontWeight.Medium, color = doubaoText())
Spacer(Modifier.height(3.dp))
Text(hint, fontSize = 10.sp, color = doubaoTextSecondary(), lineHeight = 14.sp)
Spacer(Modifier.height(6.dp))
pins.forEachIndexed { i, p ->
Row(
Modifier.fillMaxWidth().padding(vertical = 3.dp),
verticalAlignment = Alignment.CenterVertically,
) {
// 类型选择(点一下循环换 —— 只有五种,比弹菜单快)
val types = pinTypeOptions(canExec)
Box(
Modifier
.clip(RoundedCornerShape(7.dp))
.background(
if (p.type == "exec") Color(0xFF546E7A) else Doubao.Blue.copy(alpha = 0.85f),
)
.clickable {
val idx = types.indexOfFirst { it.first == p.type }
val next = types[(idx + 1).mod(types.size)]
onChange(
pins.toMutableList().also { it[i] = p.copy(type = next.first) },
)
}
.padding(horizontal = 8.dp, vertical = 5.dp),
) {
Text(
pinTypeLabel(p.type),
fontSize = 11.sp,
color = Color.White,
fontWeight = FontWeight.Medium,
)
}
Spacer(Modifier.width(6.dp))
OutlinedTextField(
value = p.name,
onValueChange = {
onChange(pins.toMutableList().also { l -> l[i] = p.copy(name = it) })
},
modifier = Modifier.weight(1f).height(52.dp),
placeholder = { Text("名字", fontSize = 11.sp) },
singleLine = true,
textStyle = TextStyle(fontSize = 12.sp),
)
if (showDefault && p.type != "exec") {
Spacer(Modifier.width(6.dp))
OutlinedTextField(
value = p.value,
onValueChange = {
onChange(pins.toMutableList().also { l -> l[i] = p.copy(value = it) })
},
modifier = Modifier.width(86.dp).height(52.dp),
placeholder = { Text("默认值", fontSize = 10.sp) },
singleLine = true,
textStyle = TextStyle(fontSize = 12.sp),
)
}
Text(
"✕",
fontSize = 15.sp,
color = Color(0xFFD93025),
modifier = Modifier
.clip(RoundedCornerShape(6.dp))
.clickable {
onChange(pins.toMutableList().also { it.removeAt(i) })
}
.padding(horizontal = 8.dp, vertical = 6.dp),
)
}
}
Text(
"+ 加一个",
fontSize = 13.sp,
color = Doubao.Blue,
modifier = Modifier
.clip(RoundedCornerShape(7.dp))
.clickable {
onChange(pins + PinDecl(if (showDefault) "str" else "str", "", ""))
}
.padding(horizontal = 8.dp, vertical = 6.dp),
)
}
/**
* 积木编辑器(独立全屏)。
*
* 布局照编程猫:左边积木分类、右边脚本区、底下生成的代码。
* 三个视图:
* 积木 —— 左边分类、右边脚本区、底下生成的代码(编程猫那种布局)
* 节点信息 —— 这个节点叫什么、有哪些入口/出口
* 代码 —— 声明 + 生成的 run()
*
* ★ 为什么"节点信息"必须是**独立的一屏**而不是藏在代码里:
* 加一个入口在字典里是 `{"type": "str", "name": "文本", "value": ""}` ——
* 用户得记住三个键、引号别写错、逗号别忘。在界面上它就是一行:
* 「文字入口 名字[文本] 默认值[] ✕」。
* 而且积木要**引用**这些名字(读输入选哪个、走分支选哪条),
* 引用关系必须来自同一份结构,不然改了名字积木就全断了。
*/
@Composable
fun NodeBlockEditorScreen(
@@ -255,8 +433,9 @@ fun NodeBlockEditorScreen(
) {
val blocks = remember { mutableStateListOf<Blk>().also { it.addAll(initialBlocks) } }
var cat by remember { mutableStateOf("event") }
var decl by remember { mutableStateOf(initialDecl) }
var showCode by remember { mutableStateOf(false) }
// 声明解析成结构来编辑,存的时候再生成回 Python
var decl by remember { mutableStateOf(parseDecl(initialDecl)) }
var view by remember { mutableStateOf(0) } // 0=积木 1=节点信息 2=代码
var dragging by remember { mutableStateOf(-1L) }
/**
* 新积木加到哪里:-1 = 最外层,否则是某个 C 型积木的 id(加进它肚子里)。
@@ -267,7 +446,9 @@ fun NodeBlockEditorScreen(
var into by remember { mutableStateOf(-1L) }
val scroll = rememberScrollState()
val code = genFullCode(blocks.toList(), decl)
// 生成的代码总是用**当前**声明 —— 改了节点信息,代码立刻跟着变
val declCode = decl.toPython()
val code = genFullCode(blocks.toList(), declCode)
Column(Modifier.fillMaxSize().background(doubaoBg())) {
// ---------------- 顶栏 ----------------
@@ -280,15 +461,22 @@ fun NodeBlockEditorScreen(
Spacer(Modifier.width(8.dp))
Text(fileName, fontSize = 11.sp, color = doubaoTextSecondary())
Spacer(Modifier.weight(1f))
Text(
if (showCode) "看积木" else "看代码",
fontSize = 13.sp,
color = Doubao.Blue,
modifier = Modifier
.clip(RoundedCornerShape(8.dp))
.clickable { showCode = !showCode }
.padding(horizontal = 10.dp, vertical = 6.dp),
)
// 三个视图:积木 / 节点信息 / 代码
listOf("积木", "信息", "代码").forEachIndexed { i, label ->
Text(
label,
fontSize = 13.sp,
color = if (view == i) Color.White else doubaoTextSecondary(),
fontWeight = if (view == i) FontWeight.Medium else FontWeight.Normal,
modifier = Modifier
.padding(end = 4.dp)
.clip(RoundedCornerShape(8.dp))
.background(if (view == i) Doubao.Blue else Doubao.Blue.copy(alpha = 0.08f))
.clickable { view = i }
.padding(horizontal = 9.dp, vertical = 6.dp),
)
}
Spacer(Modifier.width(4.dp))
Text(
"保存",
fontSize = 14.sp,
@@ -303,15 +491,26 @@ fun NodeBlockEditorScreen(
}
HorizontalDivider(thickness = 0.5.dp)
if (showCode) {
// ---------------- 代码视图(声明 + 生成结果都能改) ----------------
if (view == 1) {
// ---------------- 节点信息 ----------------
NodeInfoView(
decl = decl,
onDecl = { decl = it },
)
} else if (view == 2) {
// ---------------- 代码视图 ----------------
Column(Modifier.fillMaxSize().padding(10.dp)) {
Text("节点声明(入口 / 出口在这里定)", fontSize = 11.sp, color = doubaoTextSecondary())
Spacer(Modifier.height(4.dp))
// 声明那一段也是 Python,同样给高亮和补全
Text(
"这段声明是「信息」页生成出来的。要改入口/出口去那一页 ——" +
"在这儿手改也行,但保存后就以这里为准了。",
fontSize = 11.sp,
color = doubaoTextSecondary(),
lineHeight = 15.sp,
)
Spacer(Modifier.height(6.dp))
PythonCodeField(
value = decl,
onValueChange = { decl = it; onDecl(it) },
value = declCode,
onValueChange = { onDecl(it) },
modifier = Modifier.fillMaxWidth(),
label = "节点声明",
minHeight = 110,
@@ -389,7 +588,9 @@ fun NodeBlockEditorScreen(
.padding(8.dp),
) {
TPLS.filter { it.cat == cat }.forEach { t ->
BlockChip(t, active = into == -1L) { addBlock(blocks, into, fromTpl(t)) }
BlockChip(t, active = into == -1L) {
addBlock(blocks, into, fromTpl(t, decl))
}
}
}
HorizontalDivider(thickness = 0.5.dp)
@@ -446,6 +647,7 @@ fun NodeBlockEditorScreen(
onField = { which, v ->
blocks[i] = if (which == 0) b.copy(a = v) else b.copy(b = v)
},
decl = decl,
)
}
}
@@ -488,8 +690,23 @@ private fun addBlock(list: MutableList<Blk>, into: Long, b: Blk) {
list[idx] = list[idx].copy(children = list[idx].children + b)
}
private fun fromTpl(t: Tpl): Blk =
Blk(newId(), t.kind, a = t.a, b = t.b)
/**
* 从模板建一块积木。
*
* ★ 默认值优先取**这个节点声明的引脚名**,而不是模板里写死的"文本"/"列表":
* 用户的入口可能叫「正文」「素材」,模板里写死的名字一放上去就是 KeyError。
* 取不到才退回模板默认值。
*/
private fun fromTpl(t: Tpl, decl: NodeDecl = NodeDecl()): Blk {
val a = when (t.kind) {
"getinput", "setout" -> decl.dataInputs.firstOrNull()?.name
?: decl.dataOutputs.firstOrNull()?.name ?: t.a
"branch" -> decl.execOutputs.firstOrNull()?.name ?: t.a
"foreach" -> decl.inputs.firstOrNull { it.type == "list" }?.name ?: t.a
else -> t.a
}
return Blk(newId(), t.kind, a = a, b = t.b)
}
/** 左边那一列"可选积木" —— 用**真实形状**画,这样加进来之前就知道它长什么样 */
@Composable
@@ -612,6 +829,8 @@ private fun ScriptBlock(
onEnter: () -> Unit,
isTarget: Boolean,
onField: (Int, String) -> Unit,
/** 节点声明:积木上引用引脚名的地方要从这里取选项 */
decl: NodeDecl = NodeDecl(),
) {
val c = colorOf(b.kind)
val tpl = tplOf(b.kind)

View File

@@ -0,0 +1,143 @@
package com.doubao.lynxclone.ui
/*
* 节点信息:名字、说明、入口、出口。
*
* ★ 为什么要有这个结构,而不是让用户直接改那段 Python 字典:
* "给这个节点加一个文字入口"这件事,在字典里是
* {"type": "str", "name": "文本", "value": ""}
* —— 用户得记住 type/name/value 三个键、引号别写错、别忘了逗号。
* 在积木界面里它应该是**一行**:「文字入口 名字[__] 默认值[__] ✕」
*
* 而且积木要引用这些名字(读输入要选哪个入口、走分支要选哪条),
* 引用关系必须来自**同一份结构**,不然改了名字积木就全断了。
*
* 所以:界面编辑 NodeDecl → 生成那段 Python 声明;
* 反过来打开一个已有节点时,从那段 Python 里解析回 NodeDecl。
*/
/** 一个引脚的声明 */
data class PinDecl(
val type: String = "str",
val name: String = "",
/** 只有 input 用:没接线时的默认值 */
val value: String = "",
)
/** 一个节点的完整声明 */
data class NodeDecl(
val name: String = "我的节点",
val desc: String = "",
val inputs: List<PinDecl> = listOf(PinDecl("str", "文本", "")),
val outputs: List<PinDecl> = listOf(
PinDecl("exec", "好了", ""),
PinDecl("str", "结果", ""),
),
) {
val dataInputs: List<PinDecl> get() = inputs.filter { it.type != "exec" }
val execOutputs: List<PinDecl> get() = outputs.filter { it.type == "exec" }
val dataOutputs: List<PinDecl> get() = outputs.filter { it.type != "exec" }
/** 生成那段声明(用户不用手写,但"看代码"里要能看见它) */
fun toPython(): String {
fun pin(p: PinDecl, isInput: Boolean): String {
val v = if (isInput) ", \"value\": ${pyLit(p.value)}" else ""
return " {\"type\": \"${p.type}\", \"name\": ${pyLit(p.name)}$v},"
}
return buildString {
append("ToolName = {\n")
append(" \"name\": ${pyLit(name)},\n")
append(" \"desc\": ${pyLit(desc)},\n")
append(" \"input\": [\n")
inputs.forEach { append(pin(it, true)).append('\n') }
append(" ],\n")
append(" \"output\": [\n")
outputs.forEach { append(pin(it, false)).append('\n') }
append(" ],\n")
append("}")
}
}
}
/** 生成一个 Python 字面量。中文、引号、反斜杠都要能安全放进去 */
fun pyLit(s: String): String {
val body = s.replace("\\", "\\\\").replace("\"", "\\\"")
return "\"$body\""
}
private val PIN_TYPES_FOR_DECL = listOf(
"str" to "文本",
"num" to "数字",
"bool" to "真假",
"list" to "列表",
"exec" to "执行",
)
fun pinTypeLabel(t: String): String =
PIN_TYPES_FOR_DECL.firstOrNull { it.first == t }?.second ?: t
fun pinTypeOptions(includeExec: Boolean): List<Pair<String, String>> =
if (includeExec) PIN_TYPES_FOR_DECL else PIN_TYPES_FOR_DECL.filter { it.first != "exec" }
/**
* 从已有的 Python 源码里把声明解析回来。
*
* ★ 为什么需要:用户打开一个已经存在的节点(可能是别人给的),
* 界面要能显示"它有哪些入口/出口",而不是一片空白让他重填。
*
* 实现上**不写完整的 Python 解析器**:只认我们自己生成的那种形状
* (`"key": [ {...}, ... ]`),认不出来的就返回默认值并把原文留在"看代码"里 ——
* 写一个通用解析器不值得,而认不出来的情况用户能看见原文,不会卡住。
*/
fun parseDecl(code: String): NodeDecl {
fun strAfter(key: String): String {
// "name": "xxx"
val m = Regex("\"$key\"\\s*:\\s*\"((?:\\\\.|[^\"\\\\])*)\"").find(code) ?: return ""
return m.groupValues[1].replace("\\\"", "\"").replace("\\\\", "\\")
}
fun pinsOf(key: String): List<PinDecl> {
val start = code.indexOf("\"$key\"")
if (start < 0) return emptyList()
val open = code.indexOf('[', start)
if (open < 0) return emptyList()
var depth = 0
var end = open
for (i in open until code.length) {
when (code[i]) {
'[' -> depth++
']' -> {
depth--
if (depth == 0) {
end = i
break
}
}
}
}
val block = code.substring(open, end)
return Regex("\\{([^{}]*)\\}").findAll(block).mapNotNull { m ->
val body = m.groupValues[1]
fun f(k: String): String {
val mm = Regex("\"$k\"\\s*:\\s*\"((?:\\\\.|[^\"\\\\])*)\"").find(body)
return mm?.groupValues?.get(1)?.replace("\\\"", "\"") ?: ""
}
val t = f("type")
val n = f("name")
if (t.isBlank() || n.isBlank()) null else PinDecl(t, n, f("value"))
}.toList()
}
val ins = pinsOf("input")
val outs = pinsOf("output")
val nm = strAfter("name")
val dc = strAfter("desc")
// 什么都没解析出来(不是我们生成的那种)就退回默认,让用户从零填
if (ins.isEmpty() && outs.isEmpty() && nm.isBlank()) return NodeDecl()
return NodeDecl(
name = nm.ifBlank { "我的节点" },
desc = dc,
inputs = ins,
outputs = outs,
)
}