-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBTNode.tsx
More file actions
138 lines (119 loc) · 4.72 KB
/
BTNode.tsx
File metadata and controls
138 lines (119 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* BTNode - Custom ReactFlow node component for BehaviorTree nodes.
*
* Renders the node header (type + optional instance name), field values,
* and subtree port labels. Used as the `btNode` custom node type in the
* ReactFlow canvas.
*
* Data shape comes from BTNodeInstance, but ReactFlow passes it as
* `data: Record<string, any>`, so we define a narrowed `BTNodeData`
* interface here.
*/
import React, { memo } from 'react';
import { Handle, Position } from '@xyflow/react';
import './BTNode.css';
// ---------------------------------------------------------------------------
// Props
// ---------------------------------------------------------------------------
/** Subset of NodeField used for display purposes. */
interface DisplayField {
name: string;
value: string | number | boolean;
valueType: 'literal' | 'variable';
portDirection?: 'input' | 'output';
}
interface BTNodeData {
name: string;
category: string;
color: string;
nodeName?: string;
subtreeId?: string;
fields: DisplayField[];
}
interface BTNodeProps {
data: BTNodeData;
selected: boolean;
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/**
* Format a field value for display.
*
* Variable references and `output_key` values may already include braces
* (`{varName}`). This helper normalises the display so variables always
* show braces and literals never do.
*/
function formatDisplayValue(field: DisplayField): string {
const raw = String(field.value);
const hasBrackets = raw.startsWith('{') && raw.endsWith('}');
if (field.valueType === 'variable' && !hasBrackets) return `{${raw}}`;
return raw;
}
/** Determine the CSS class for a field value (variable refs are highlighted). */
function valueClassName(field: DisplayField): string {
const raw = String(field.value);
const isVariable = field.valueType === 'variable' || (raw.startsWith('{') && raw.endsWith('}'));
return `field-value ${isVariable ? 'variable' : 'literal'}`;
}
/** Render a single field row (shared by regular fields and port fields). */
function FieldRow({ field, portLabel }: { field: DisplayField; portLabel?: string }) {
return (
<div className={`node-field${portLabel ? ' port-field' : ''}`}>
{portLabel && <span className={`port-direction-label ${portLabel === '[IN]' ? 'input' : 'output'}`}>{portLabel}</span>}
<span className="field-name">{field.name}:</span>
<span className={valueClassName(field)}>{formatDisplayValue(field)}</span>
</div>
);
}
// ---------------------------------------------------------------------------
// Component
// ---------------------------------------------------------------------------
const BTNode: React.FC<BTNodeProps> = ({ data, selected }) => {
const displayLabel = data.nodeName || '';
const isSubtree = data.category === 'subtree';
const isRoot = data.category === 'root';
// Partition fields for subtree port display
const inputFields = isSubtree ? data.fields.filter((f) => f.portDirection === 'input') : [];
const outputFields = isSubtree ? data.fields.filter((f) => f.portDirection === 'output') : [];
const regularFields = isSubtree ? data.fields.filter((f) => !f.portDirection) : data.fields;
return (
<div
className={`bt-node ${selected ? 'selected' : ''} ${isSubtree ? 'subtree-node' : ''}`}
style={{ borderColor: data.color }}
>
{/* Root nodes have no target (incoming) handle */}
{!isRoot && <Handle type="target" position={Position.Top} className="node-handle" />}
<div className="node-header" style={{ backgroundColor: data.color }}>
<div className="node-title">{data.name}</div>
{displayLabel && <div className="node-category">{displayLabel}</div>}
</div>
{/* Subtree input ports */}
{isSubtree && inputFields.length > 0 && (
<div className="node-fields port-fields input-ports">
{inputFields.map((field, idx) => (
<FieldRow key={idx} field={field} portLabel="[IN]" />
))}
</div>
)}
{/* Regular fields */}
{regularFields.length > 0 && (
<div className="node-fields">
{regularFields.map((field, idx) => (
<FieldRow key={idx} field={field} />
))}
</div>
)}
{/* Subtree output ports */}
{isSubtree && outputFields.length > 0 && (
<div className="node-fields port-fields output-ports">
{outputFields.map((field, idx) => (
<FieldRow key={idx} field={field} portLabel="[OUT]" />
))}
</div>
)}
<Handle type="source" position={Position.Bottom} className="node-handle" />
</div>
);
};
export default memo(BTNode);