diff --git a/src/components/gui/gui.jsx b/src/components/gui/gui.jsx index ba24710de85..754971f1c5d 100644 --- a/src/components/gui/gui.jsx +++ b/src/components/gui/gui.jsx @@ -39,6 +39,8 @@ import TWRestorePointManager from '../../containers/tw-restore-point-manager.jsx import TWUnknownPlatformModal from '../../containers/tw-unknown-platform-modal.jsx'; import TWInvalidProjectModal from '../../containers/tw-invalid-project-modal.jsx'; import MWExtensionManagerModal from '../../containers/mw-extension-manager-modal.jsx'; +import VisualExtensionEditor from '../../containers/visual-extension-editor-modal.jsx'; +import VisualAddonEditor from '../../containers/visual-addon-editor-modal.jsx'; import MWHelpModal from '../../containers/mw-help-modal.jsx'; import MWProjectThemeModal from '../../containers/mw-project-theme-modal.jsx'; import RoturSession from '../../containers/rotur-session.jsx'; @@ -995,6 +997,8 @@ const GUIComponent = props => { + + {usernameModalVisible && ( diff --git a/src/components/menu-bar/menu-bar.jsx b/src/components/menu-bar/menu-bar.jsx index 67e92af2e49..51da04711bd 100644 --- a/src/components/menu-bar/menu-bar.jsx +++ b/src/components/menu-bar/menu-bar.jsx @@ -62,7 +62,9 @@ import { openGitModal, openExtensionManagerModal, openHelp, - openSimpleDialog + openSimpleDialog, + openVisualExtensionEditor, + openVisualAddonEditor } from '../../reducers/modals'; import {openCollaborationModal} from '../../reducers/collaboration'; import {setPlayer} from '../../reducers/mode'; @@ -2210,6 +2212,32 @@ class MenuBar extends React.Component { open={this.props.toolsMenuOpen} place={this.props.isRtl ? 'left' : 'right'} > + + { + this.props.onClickVisualExtensionEditor(); + this.props.onRequestCloseTools(); + }} + > + + + { + this.props.onClickVisualAddonEditor(); + this.props.onRequestCloseTools(); + }} + > + + + { @@ -2654,6 +2682,8 @@ MenuBar.propTypes = { onClickPreferencesModal: PropTypes.func, onClickGitModal: PropTypes.func, onClickHelp: PropTypes.func, + onClickVisualExtensionEditor: PropTypes.func, + onClickVisualAddonEditor: PropTypes.func, onOpenSettingsModal: PropTypes.func, onLogOut: PropTypes.func, @@ -2783,6 +2813,8 @@ const mapDispatchToProps = dispatch => ({ }, onClickHelp: () => dispatch(openHelp()), onOpenSettingsModal: () => dispatch(openSettingsModal()), + onClickVisualExtensionEditor: () => dispatch(openVisualExtensionEditor()), + onClickVisualAddonEditor: () => dispatch(openVisualAddonEditor()), onClickNew: needSave => { dispatch(setPlayer(false)); dispatch(requestNewProject(needSave)); diff --git a/src/components/visual-editor/node-canvas.jsx b/src/components/visual-editor/node-canvas.jsx new file mode 100644 index 00000000000..78716231189 --- /dev/null +++ b/src/components/visual-editor/node-canvas.jsx @@ -0,0 +1,372 @@ +/** + * 可视化节点编辑器 · 画布(UE 蓝图风格) + * 平移、Ctrl+滚轮缩放、节点拖动、端口拖拽连线、点选节点/连线、双击子图节点。 + * 样式经 CSS Modules(styles.xxx);分类色标题栏 + 类型着色端口。 + */ +import React from 'react'; +import PropTypes from 'prop-types'; +import bindAll from 'lodash.bindall'; +import {getType} from '../../lib/visual-node-engine/node-types.js'; +import styles from './visual-editor-modal.css'; + +// 分类标题栏颜色(UE 蓝图式分类色带) +const CATEGORY_COLORS = { + '事件': '#2e5f9e', + '流程控制': '#2e7d54', + '变量': '#6d54a0', + '运算': '#a2701f', + '函数': '#4a5cc0', + 'Scratch 交互': '#d99e2b', + 'DOM/UI': '#b85c4e', + '网络/存储': '#3a8e68', + '扩展积木': '#8e44ad', + '插件 API': '#16867a' +}; + +// 数据端口类型 → 颜色(近似 UE:exec 灰白、number 绿、string 品红、bool 红、any 青) +const PORT_TYPE_COLORS = { + number: '#7ce87c', + string: '#ff7ce8', + bool: '#ff6b6b', + any: '#56c8f0' +}; + +const portTypeOf = (def, portName, isInput) => { + const list = isInput ? (def.inputs || []) : (def.outputs || []); + const port = list.find(p => p.name === portName); + if (!port) return 'any'; + if (port.kind === 'exec') return 'exec'; + // 从 params 定义猜类型 + const paramDef = (def.params || []).find(p => p.name === portName); + if (paramDef) { + if (paramDef.type === 'number') return 'number'; + if (paramDef.type === 'bool') return 'bool'; + if (paramDef.type === 'text' && portName !== 'condition') return 'string'; + } + return 'any'; +}; + +const portDotClass = type => { + switch (type) { + case 'exec': return styles.vePortDotExec; + case 'number': return styles.vePortDotNumber; + case 'string': return styles.vePortDotString; + case 'bool': return styles.vePortDotBool; + default: return styles.vePortDotAny; + } +}; + +class NodeCanvas extends React.Component { + constructor (props) { + super(props); + bindAll(this, [ + 'toWorld', + 'portPos', + 'onWheel', + 'onCanvasMouseDown', + 'onMouseMove', + 'onMouseUp', + 'onNodeTitleMouseDown', + 'onPortMouseDown', + 'onPortMouseUp', + 'onEdgeClick', + 'onNodeMouseDown', + 'onNodeDoubleClick' + ]); + this.wrapRef = React.createRef(); + this.state = { + pan: {x: 60, y: 40}, + zoom: 1, + dragNode: null, + panning: false, + panStart: null, + linkFrom: null, + mouseWorld: null + }; + } + + toWorld (clientX, clientY) { + const rect = this.wrapRef.current.getBoundingClientRect(); + return { + x: (clientX - rect.left - this.state.pan.x) / this.state.zoom, + y: (clientY - rect.top - this.state.pan.y) / this.state.zoom + }; + } + + portPos (node, portName, isInput, def) { + const inputs = (def.inputs || []); + const outputs = (def.outputs || []); + const inIdx = inputs.findIndex(p => p.name === portName); + const outIdx = outputs.findIndex(p => p.name === portName); + if (isInput && inIdx === -1) return null; + if (!isInput && outIdx === -1) return null; + const row = isInput ? inIdx : outIdx; + return { + x: node.x + (isInput ? 0 : node.width || 200), + y: node.y + 31 + 4 + row * 20 + 9 + }; + } + + onWheel (e) { + e.preventDefault(); + if (e.ctrlKey || e.metaKey) { + const factor = e.deltaY > 0 ? 0.9 : 1.1; + this.setState(s => ({zoom: Math.min(2.5, Math.max(0.3, s.zoom * factor))})); + } else { + this.setState(s => ({pan: {x: s.pan.x - e.deltaX, y: s.pan.y - e.deltaY}})); + } + } + + onCanvasMouseDown (e) { + if (e.target === e.currentTarget || e.target.classList.contains(styles.veViewport)) { + this.setState({panning: true, panStart: {x: e.clientX, y: e.clientY, base: this.state.pan}}); + } + } + + onMouseMove (e) { + if (this.state.panning && this.state.panStart) { + this.setState({ + pan: { + x: this.state.panStart.base.x + (e.clientX - this.state.panStart.x), + y: this.state.panStart.base.y + (e.clientY - this.state.panStart.y) + } + }); + return; + } + if (this.state.dragNode) { + const world = this.toWorld(e.clientX, e.clientY); + this.props.onMoveNode(this.state.dragNode.id, + world.x - this.state.dragNode.dx, world.y - this.state.dragNode.dy); + return; + } + if (this.state.linkFrom) { + this.setState({mouseWorld: this.toWorld(e.clientX, e.clientY)}); + } + } + + onMouseUp () { + if (this.state.panning) this.setState({panning: false, panStart: null}); + if (this.state.dragNode) this.setState({dragNode: null}); + if (this.state.linkFrom) this.setState({linkFrom: null, mouseWorld: null}); + } + + onNodeTitleMouseDown (e, node) { + e.stopPropagation(); + const world = this.toWorld(e.clientX, e.clientY); + this.props.onSelectNode(node.id); + this.setState({dragNode: {id: node.id, dx: world.x - node.x, dy: world.y - node.y}}); + } + + onPortMouseDown (e, node, portName, isInput, def) { + e.stopPropagation(); + const pos = this.portPos(node, portName, isInput, def); + this.props.onSelectNode(node.id); + const w = this.toWorld(e.clientX, e.clientY); + this.setState({linkFrom: {nodeId: node.id, port: portName, isInput, x: pos.x, y: pos.y}, mouseWorld: w}); + } + + onPortMouseUp (e, node, portName, isInput) { + e.stopPropagation(); + const link = this.state.linkFrom; + if (!link) return; + let fromNode = link.nodeId; + let fromPort = link.port; + let toNode = node.id; + let toPort = portName; + if (link.isInput && !isInput) { + fromNode = node.id; + fromPort = portName; + toNode = link.nodeId; + toPort = link.port; + } else if (link.isInput === isInput) { + this.setState({linkFrom: null, mouseWorld: null}); + return; + } + this.props.onConnect(fromNode, fromPort, toNode, toPort); + this.setState({linkFrom: null, mouseWorld: null}); + } + + onEdgeClick (e, edge) { + e.stopPropagation(); + this.props.onRemoveEdge(edge); + } + + onNodeMouseDown (e, node) { + e.stopPropagation(); + this.props.onSelectNode(node.id); + } + + onNodeDoubleClick (e, node) { + e.stopPropagation(); + this.props.onOpenSubGraph(node.id); + } + + renderPortDot (node, def, port, isInput) { + const type = portTypeOf(def, port.name, isInput); + const isExec = type === 'exec'; + const dotClass = `${styles.vePortDot} ${portDotClass(type)}`; + const inlineStyle = (!isExec && type !== 'any') ? + {borderColor: PORT_TYPE_COLORS[type]} : null; + const key = `${node.id}:${isInput ? 'i' : 'o'}:${port.name}`; + return ( +
+ {isInput && ( +
this.onPortMouseDown(e, node, port.name, true, def)} + onMouseUp={e => this.onPortMouseUp(e, node, port.name, true)} + /> + )} + {port.label || port.name} + {!isInput && ( +
this.onPortMouseDown(e, node, port.name, false, def)} + onMouseUp={e => this.onPortMouseUp(e, node, port.name, false)} + /> + )} +
+ ); + } + + render () { + const {graph, selectedNodeId} = this.props; + const {pan, zoom, linkFrom, mouseWorld} = this.state; + const nodes = graph.nodes.map(n => { + const def = getType(n.type); + const width = def && (def.isExtensionBlock || def.isFunction) ? 230 : 200; + return {...n, width, __def: def}; + }); + + const edgePaths = graph.edges.map((edge, i) => { + const fromNode = nodes.find(n => n.id === edge.from.node); + const toNode = nodes.find(n => n.id === edge.to.node); + if (!fromNode || !toNode) return null; + const fromDef = fromNode.__def; + const toDef = toNode.__def; + if (!fromDef || !toDef) return null; + const fromPortDef = (fromDef.outputs || []).find(p => p.name === edge.from.port); + const toPortDef = (toDef.inputs || []).find(p => p.name === edge.to.port); + if (!fromPortDef || !toPortDef) return null; + const a = this.portPos(fromNode, edge.from.port, false, fromDef); + const b = this.portPos(toNode, edge.to.port, true, toDef); + if (!a || !b) return null; + const isExec = fromPortDef.kind === 'exec'; + const type = portTypeOf(fromDef, edge.from.port, false); + const color = isExec ? '#cfd9e8' : (PORT_TYPE_COLORS[type] || '#56c8f0'); + let path; + if (isExec) { + // UE exec:直角折线 + const midX = a.x + Math.max(24, Math.abs(b.x - a.x) / 2); + path = `M ${a.x} ${a.y} L ${midX} ${a.y} L ${midX} ${b.y} L ${b.x} ${b.y}`; + } else { + const dx = Math.max(30, Math.abs(b.x - a.x) / 2); + path = `M ${a.x} ${a.y} C ${a.x + dx} ${a.y}, ${b.x - dx} ${b.y}, ${b.x} ${b.y}`; + } + return ( + this.onEdgeClick(e, edge)} + /> + ); + }); + + let ghost = null; + if (linkFrom && mouseWorld) { + const a = linkFrom.isInput ? mouseWorld : {x: linkFrom.x, y: linkFrom.y}; + const b = linkFrom.isInput ? {x: linkFrom.x, y: linkFrom.y} : mouseWorld; + const dx = Math.max(30, Math.abs(b.x - a.x) / 2); + ghost = (`M ${a.x} ${a.y} C ${a.x + dx} ${a.y}, ${b.x - dx} ${b.y}, ${b.x} ${b.y}`); + } + + return ( +
+
+ + {edgePaths} + {ghost ? : null} + + {nodes.length === 0 && ( +
+ 从左侧面板点击/拖入节点开始搭建… +
+ )} + {nodes.map(node => { + const def = node.__def; + if (!def) return null; + const titleColor = CATEGORY_COLORS[def.category] || '#3c4a6b'; + return ( +
this.onNodeMouseDown(e, node)} + onDoubleClick={e => this.onNodeDoubleClick(e, node)} + onContextMenu={e => { + e.preventDefault(); + e.stopPropagation(); + this.props.onRemoveNode(node.id); + }} + > +
this.onNodeTitleMouseDown(e, node)} + > + {def.title} + {node.id} +
+
+ {(def.inputs || []).map(p => this.renderPortDot(node, def, p, true))} + {(def.outputs || []).map(p => this.renderPortDot(node, def, p, false))} + {(def.params || []).some(p => p.type === 'graph') && ( +
双击节点进入子图
+ )} +
+
+ ); + })} +
+
+ 拖空白平移 · Ctrl+滚轮缩放 · 拖端口连线 · 点连线删除 · 右键节点删除 · 双击进子图 +
+
+ ); + } +} + +NodeCanvas.propTypes = { + graph: PropTypes.object.isRequired, + selectedNodeId: PropTypes.string, + onMoveNode: PropTypes.func, + onSelectNode: PropTypes.func, + onConnect: PropTypes.func, + onRemoveEdge: PropTypes.func, + onRemoveNode: PropTypes.func, + onOpenSubGraph: PropTypes.func +}; + +export default NodeCanvas; diff --git a/src/components/visual-editor/node-inspector.jsx b/src/components/visual-editor/node-inspector.jsx new file mode 100644 index 00000000000..a4ba68f3076 --- /dev/null +++ b/src/components/visual-editor/node-inspector.jsx @@ -0,0 +1,187 @@ +/** + * 可视化节点编辑器 · 属性面板(右侧) + * 选中节点的参数编辑(text/number/bool/select/code/graph)。 + */ +import React from 'react'; +import PropTypes from 'prop-types'; +import bindAll from 'lodash.bindall'; +import {getType} from '../../lib/visual-node-engine/node-types.js'; +import styles from './visual-editor-modal.css'; + +class NodeInspector extends React.Component { + constructor (props) { + super(props); + bindAll(this, ['handleChange', 'renderParam', 'renderMeta']); + } + + handleChange (name, value) { + this.props.onParamChange(this.props.node.id, name, value); + } + + renderMeta () { + const {mode, meta, onMetaChange} = this.props; + const metaFields = mode === 'addon' ? [ + ['name', '插件名称'], + ['description', '插件描述'] + ] : [ + ['name', '扩展名称'], + ['id', '扩展 ID'], + ['description', '扩展描述'] + ]; + return ( +
+

信息

+ {metaFields.map(([k, label]) => ( +
+ + onMetaChange(k, e.target.value)} + /> +
+ ))} +
+
+ ); + } + + renderParam (p, node) { + const value = node.params ? node.params[p.name] : null; + switch (p.type) { + case 'bool': + return ( +
+ +
+ this.handleChange(p.name, e.target.checked)} + /> + {value ? '开' : '关'} +
+
+ ); + case 'number': + return ( +
+ + this.handleChange(p.name, Number(e.target.value))} + /> +
+ ); + case 'select': + return ( +
+ + +
+ ); + case 'code': + return ( +
+ +