cocos2d-cli 1.5.1 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cocos2d-cli.js +56 -33
- package/package.json +6 -2
- package/src/commands/create-scene.js +2 -7
- package/src/commands/get.js +25 -53
- package/src/commands/screenshot.js +95 -0
- package/src/commands/set-component.js +119 -0
- package/src/commands/set.js +23 -64
- package/src/lib/cc/CCCanvas.js +10 -20
- package/src/lib/cc/CCComponent.js +13 -13
- package/src/lib/cc/CCLabel.js +15 -3
- package/src/lib/cc/CCNode.js +41 -27
- package/src/lib/cc/CCSceneAsset.js +35 -3
- package/src/lib/cc/CCSprite.js +17 -5
- package/src/lib/screenshot/favicon.ico +0 -0
- package/src/lib/screenshot/index.html +30 -0
- package/src/lib/screenshot-core.js +261 -0
package/bin/cocos2d-cli.js
CHANGED
|
@@ -11,13 +11,15 @@ const commands = {
|
|
|
11
11
|
tree: '../src/commands/tree',
|
|
12
12
|
get: '../src/commands/get',
|
|
13
13
|
set: '../src/commands/set',
|
|
14
|
+
'set-component': '../src/commands/set-component',
|
|
14
15
|
add: '../src/commands/add',
|
|
15
16
|
'add-component': '../src/commands/add-component',
|
|
16
17
|
'remove-component': '../src/commands/remove-component',
|
|
17
18
|
'remove': '../src/commands/remove',
|
|
18
19
|
build: '../src/commands/build',
|
|
19
20
|
'create-prefab': '../src/commands/prefab-create',
|
|
20
|
-
'create-scene': '../src/commands/create-scene'
|
|
21
|
+
'create-scene': '../src/commands/create-scene',
|
|
22
|
+
'screenshot': '../src/commands/screenshot'
|
|
21
23
|
};
|
|
22
24
|
|
|
23
25
|
// 帮助信息
|
|
@@ -29,42 +31,59 @@ Cocos Creator CLI - 场景/预制体操作工具集
|
|
|
29
31
|
cocos2d-cli <command> [options]
|
|
30
32
|
|
|
31
33
|
命令:
|
|
32
|
-
tree <场景.fire | 预制体.prefab>
|
|
33
|
-
get <场景.fire | 预制体.prefab> <节点路径>
|
|
34
|
-
set <场景.fire | 预制体.prefab> <节点路径>
|
|
35
|
-
|
|
36
|
-
add
|
|
37
|
-
|
|
38
|
-
remove <文件> <节点路径>
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
create-
|
|
34
|
+
tree <场景.fire | 预制体.prefab> 查看节点树
|
|
35
|
+
get <场景.fire | 预制体.prefab> <节点路径> [属性名|组件类型] 获取节点或组件属性
|
|
36
|
+
set <场景.fire | 预制体.prefab> <节点路径> <属性名> <值> 修改节点属性
|
|
37
|
+
set-component <文件> <节点路径> <组件类型> <属性名> <值> 修改组件属性
|
|
38
|
+
add <场景.fire | 预制体.prefab> <父节点路径> <名称> 添加节点
|
|
39
|
+
add-component <文件> <节点路径> <类型> 给节点添加组件
|
|
40
|
+
remove-component <文件> <节点路径> <类型> 删除节点组件
|
|
41
|
+
remove <文件> <节点路径> 删除节点
|
|
42
|
+
build <项目目录> 构建组件映射
|
|
43
|
+
create-prefab [JSON文件路径] <输出.prefab> 创建预制体(不传JSON则创建默认)
|
|
44
|
+
create-scene [JSON文件路径] <输出.fire> 创建场景(不传JSON则创建默认)
|
|
45
|
+
screenshot <json文件> [选项] 渲染JSON并截图
|
|
42
46
|
|
|
43
47
|
节点路径格式:
|
|
44
48
|
Canvas - 根节点下的 Canvas
|
|
45
49
|
Canvas/MidNode - Canvas 下的 MidNode
|
|
46
50
|
Canvas/GameScene/NodeA - 多层嵌套路径
|
|
47
51
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
52
|
+
set 支持的节点属性:
|
|
53
|
+
name 节点名称
|
|
54
|
+
active 激活状态 (true/false)
|
|
55
|
+
x X 坐标
|
|
56
|
+
y Y 坐标
|
|
57
|
+
width 宽度
|
|
58
|
+
height 高度
|
|
59
|
+
anchorX 锚点 X (0-1)
|
|
60
|
+
anchorY 锚点 Y (0-1)
|
|
61
|
+
opacity 透明度 (0-255)
|
|
62
|
+
scaleX X 缩放
|
|
63
|
+
scaleY Y 缩放
|
|
64
|
+
rotation 旋转角度
|
|
65
|
+
|
|
66
|
+
set-component 示例:
|
|
67
|
+
cocos2d-cli set-component xxx.fire Canvas/Label Label string "Hello"
|
|
68
|
+
cocos2d-cli set-component xxx.fire Canvas/Label Label fontSize 32
|
|
69
|
+
cocos2d-cli set-component xxx.fire Canvas/Sprite Sprite sizeMode 0
|
|
70
|
+
|
|
71
|
+
add 支持的选项:
|
|
72
|
+
--x=<数值> 设置 X 坐标
|
|
73
|
+
--y=<数值> 设置 Y 坐标
|
|
74
|
+
--width=<数值> 设置宽度
|
|
75
|
+
--height=<数值> 设置高度
|
|
76
|
+
--scaleX=<数值> 设置 X 缩放
|
|
77
|
+
--scaleY=<数值> 设置 Y 缩放
|
|
78
|
+
--rotation=<角度> 设置旋转角度
|
|
79
|
+
--active=true/false 设置激活状态
|
|
65
80
|
--type=sprite/label/button 添加节点时指定组件类型
|
|
81
|
+
--string=<文字> Label 文字内容 (配合 --type=label)
|
|
82
|
+
--fontSize=<数值> Label 字体大小 (配合 --type=label)
|
|
66
83
|
|
|
67
84
|
JSON 格式 (create-prefab / create-scene):
|
|
85
|
+
注意: 参数必须是 JSON 文件路径,不支持直接传递 JSON 字符串
|
|
86
|
+
|
|
68
87
|
{
|
|
69
88
|
"name": "节点名称",
|
|
70
89
|
"width": 400,
|
|
@@ -98,14 +117,14 @@ JSON 格式 (create-prefab / create-scene):
|
|
|
98
117
|
camera - 相机
|
|
99
118
|
particle - 粒子效果
|
|
100
119
|
|
|
101
|
-
注意:
|
|
102
|
-
- color 写在节点或 label 组件均可
|
|
103
|
-
- button 需要配合 sprite 才能看见按钮外观
|
|
104
|
-
|
|
105
120
|
示例:
|
|
106
121
|
cocos2d-cli tree assets/main.fire
|
|
107
122
|
cocos2d-cli get assets/main.fire Canvas/MidNode
|
|
108
|
-
cocos2d-cli
|
|
123
|
+
cocos2d-cli get assets/main.fire Canvas/MidNode x
|
|
124
|
+
cocos2d-cli get assets/main.fire Canvas/MidNode Label
|
|
125
|
+
cocos2d-cli set assets/main.fire Canvas/MidNode x 100
|
|
126
|
+
cocos2d-cli set assets/main.fire Canvas/MidNode width 200
|
|
127
|
+
cocos2d-cli set-component assets/main.fire Canvas/Label Label string "Hello World"
|
|
109
128
|
cocos2d-cli add assets/main.fire Canvas NewSprite --type=sprite --x=100
|
|
110
129
|
cocos2d-cli add-component assets/main.fire Canvas/MidNode label
|
|
111
130
|
cocos2d-cli remove-component assets/main.fire Canvas/MidNode label
|
|
@@ -119,6 +138,10 @@ JSON 格式 (create-prefab / create-scene):
|
|
|
119
138
|
|
|
120
139
|
# 创建默认预制体(不传JSON)
|
|
121
140
|
cocos2d-cli create-prefab assets/NewNode.prefab
|
|
141
|
+
|
|
142
|
+
# 截图
|
|
143
|
+
cocos2d-cli screenshot data.json
|
|
144
|
+
cocos2d-cli screenshot data.json -o ./screenshots -w 1080 -h 1920
|
|
122
145
|
`);
|
|
123
146
|
}
|
|
124
147
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cocos2d-cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.1",
|
|
4
4
|
"description": "Command-line tools for AI to read and manipulate Cocos Creator 2.4.x project scenes",
|
|
5
5
|
"main": "bin/cocos2d-cli.js",
|
|
6
6
|
"bin": {
|
|
@@ -19,11 +19,15 @@
|
|
|
19
19
|
"cocos-creator",
|
|
20
20
|
"cli",
|
|
21
21
|
"scene",
|
|
22
|
-
"fire"
|
|
22
|
+
"fire",
|
|
23
|
+
"screenshot"
|
|
23
24
|
],
|
|
24
25
|
"author": "",
|
|
25
26
|
"license": "MIT",
|
|
26
27
|
"engines": {
|
|
27
28
|
"node": ">=14.0.0"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"playwright": "^1.58.2"
|
|
28
32
|
}
|
|
29
33
|
}
|
|
@@ -82,8 +82,8 @@ function createDefaultScene(asset, resolution) {
|
|
|
82
82
|
|
|
83
83
|
// Canvas 组件
|
|
84
84
|
const canvasComp = new CCCanvas();
|
|
85
|
-
canvasComp.
|
|
86
|
-
canvasComp
|
|
85
|
+
canvasComp._designResolution.set(width, height);
|
|
86
|
+
canvas.addChild(canvasComp);
|
|
87
87
|
|
|
88
88
|
// Widget 组件
|
|
89
89
|
const widget = new CCWidget();
|
|
@@ -95,11 +95,6 @@ function createDefaultScene(asset, resolution) {
|
|
|
95
95
|
|
|
96
96
|
// 创建 Main Camera 节点
|
|
97
97
|
const camera = new CCNode('Main Camera');
|
|
98
|
-
camera._contentSize.width = 0;
|
|
99
|
-
camera._contentSize.height = 0;
|
|
100
|
-
camera._anchorPoint.x = 0.5;
|
|
101
|
-
camera._anchorPoint.y = 0.5;
|
|
102
|
-
camera._is3DNode = false;
|
|
103
98
|
camera._id = generateCompressedUUID();
|
|
104
99
|
|
|
105
100
|
// Camera 组件
|
package/src/commands/get.js
CHANGED
|
@@ -9,10 +9,10 @@ const { CCSceneAsset, CCPrefab } = require('../lib/cc');
|
|
|
9
9
|
/**
|
|
10
10
|
* 查找节点
|
|
11
11
|
*/
|
|
12
|
-
function findNode(root,
|
|
13
|
-
if (!
|
|
12
|
+
function findNode(root, nodePath) {
|
|
13
|
+
if (!nodePath) return root;
|
|
14
14
|
|
|
15
|
-
const parts =
|
|
15
|
+
const parts = nodePath.split('/').filter(p => p);
|
|
16
16
|
if (parts.length === 0) return root;
|
|
17
17
|
|
|
18
18
|
let current = root;
|
|
@@ -43,37 +43,15 @@ function findComponent(node, compType) {
|
|
|
43
43
|
return node._components.find(c => c.__type__ === typeName);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
/**
|
|
47
|
-
* 获取节点属性值
|
|
48
|
-
*/
|
|
49
|
-
function getNodeProp(node, prop) {
|
|
50
|
-
switch (prop.toLowerCase()) {
|
|
51
|
-
case 'name': return node._name;
|
|
52
|
-
case 'active': return node._active;
|
|
53
|
-
case 'x': return node._trs.array[0];
|
|
54
|
-
case 'y': return node._trs.array[1];
|
|
55
|
-
case 'width': return node._contentSize.width;
|
|
56
|
-
case 'height': return node._contentSize.height;
|
|
57
|
-
case 'scalex': return node._trs.array[7];
|
|
58
|
-
case 'scaley': return node._trs.array[8];
|
|
59
|
-
case 'rotation': return node._eulerAngles.z;
|
|
60
|
-
case 'anchorx': return node._anchorPoint.x;
|
|
61
|
-
case 'anchory': return node._anchorPoint.y;
|
|
62
|
-
case 'opacity': return node._opacity;
|
|
63
|
-
case 'color': return node._color;
|
|
64
|
-
default: return undefined;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
46
|
function run(args) {
|
|
69
|
-
if (args.length <
|
|
70
|
-
console.log(JSON.stringify({ error: '用法: cocos2d-cli get <场景.fire|预制体.prefab> <节点路径>
|
|
47
|
+
if (args.length < 2) {
|
|
48
|
+
console.log(JSON.stringify({ error: '用法: cocos2d-cli get <场景.fire|预制体.prefab> <节点路径> [属性名|组件类型]' }));
|
|
71
49
|
return;
|
|
72
50
|
}
|
|
73
51
|
|
|
74
52
|
const filePath = args[0];
|
|
75
53
|
const nodePath = args[1];
|
|
76
|
-
const
|
|
54
|
+
const propOrComp = args[2];
|
|
77
55
|
|
|
78
56
|
const ext = path.extname(filePath).toLowerCase();
|
|
79
57
|
|
|
@@ -99,33 +77,27 @@ function run(args) {
|
|
|
99
77
|
return;
|
|
100
78
|
}
|
|
101
79
|
|
|
102
|
-
//
|
|
103
|
-
|
|
80
|
+
// 没有指定属性或组件,返回节点所有属性
|
|
81
|
+
if (!propOrComp) {
|
|
82
|
+
const props = node.getProp ? node.getProp() : {};
|
|
83
|
+
console.log(JSON.stringify(props, null, 2));
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// 检查是否是组件类型
|
|
88
|
+
const comp = findComponent(node, propOrComp);
|
|
89
|
+
if (comp) {
|
|
90
|
+
const props = comp.getProp ? comp.getProp() : {};
|
|
91
|
+
console.log(JSON.stringify(props, null, 2));
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
104
94
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
console.log(JSON.stringify({ error: `组件不存在: ${parts[0]}` }));
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const value = comp[parts[1]];
|
|
114
|
-
if (value === undefined) {
|
|
115
|
-
console.log(JSON.stringify({ error: `属性不存在: ${parts[1]}` }));
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
console.log(JSON.stringify({ value }));
|
|
95
|
+
// 返回节点的单个属性
|
|
96
|
+
const nodeProps = node.getProp ? node.getProp() : {};
|
|
97
|
+
if (nodeProps[propOrComp] !== undefined) {
|
|
98
|
+
console.log(JSON.stringify({ [propOrComp]: nodeProps[propOrComp] }, null, 2));
|
|
120
99
|
} else {
|
|
121
|
-
|
|
122
|
-
const value = getNodeProp(node, propPath);
|
|
123
|
-
if (value === undefined) {
|
|
124
|
-
console.log(JSON.stringify({ error: `属性不存在: ${propPath}` }));
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
console.log(JSON.stringify({ value }));
|
|
100
|
+
console.log(JSON.stringify({ error: `属性不存在: ${propOrComp}` }));
|
|
129
101
|
}
|
|
130
102
|
|
|
131
103
|
} catch (err) {
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* screenshot 命令
|
|
3
|
+
* 渲染 JSON 数据并截图
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const { takeScreenshot } = require('../lib/screenshot-core');
|
|
8
|
+
|
|
9
|
+
function showHelp() {
|
|
10
|
+
console.log(`
|
|
11
|
+
用法:
|
|
12
|
+
cocos2d-cli screenshot <json文件> [选项]
|
|
13
|
+
|
|
14
|
+
选项:
|
|
15
|
+
-o, --output <目录> 输出目录,默认当前目录
|
|
16
|
+
-w, --width <数值> 视口宽度,默认 750
|
|
17
|
+
-h, --height <数值> 视口高度,默认 1334
|
|
18
|
+
--full-page 全页截图(默认)
|
|
19
|
+
--no-full-page 仅视口截图
|
|
20
|
+
--timeout <毫秒> 页面加载超时,默认 30000
|
|
21
|
+
--wait <毫秒> 截图前等待时间,默认 1000
|
|
22
|
+
|
|
23
|
+
示例:
|
|
24
|
+
cocos2d-cli screenshot data.json
|
|
25
|
+
cocos2d-cli screenshot data.json -o ./screenshots
|
|
26
|
+
cocos2d-cli screenshot data.json -w 1080 -h 1920
|
|
27
|
+
cocos2d-cli screenshot data.json --no-full-page
|
|
28
|
+
`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function parseArgs(args) {
|
|
32
|
+
const options = {
|
|
33
|
+
jsonPath: null,
|
|
34
|
+
outputDir: '.',
|
|
35
|
+
viewport: { width: 750, height: 1334 },
|
|
36
|
+
fullPage: true,
|
|
37
|
+
timeout: 30000,
|
|
38
|
+
waitTime: 1000
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
let i = 0;
|
|
42
|
+
while (i < args.length) {
|
|
43
|
+
const arg = args[i];
|
|
44
|
+
|
|
45
|
+
if (arg === '--help' || arg === '-h') {
|
|
46
|
+
showHelp();
|
|
47
|
+
process.exit(0);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (arg === '-o' || arg === '--output') {
|
|
51
|
+
options.outputDir = args[++i];
|
|
52
|
+
} else if (arg === '-w' || arg === '--width') {
|
|
53
|
+
options.viewport.width = parseInt(args[++i], 10);
|
|
54
|
+
} else if (arg === '-h' || arg === '--height') {
|
|
55
|
+
options.viewport.height = parseInt(args[++i], 10);
|
|
56
|
+
} else if (arg === '--full-page') {
|
|
57
|
+
options.fullPage = true;
|
|
58
|
+
} else if (arg === '--no-full-page') {
|
|
59
|
+
options.fullPage = false;
|
|
60
|
+
} else if (arg === '--timeout') {
|
|
61
|
+
options.timeout = parseInt(args[++i], 10);
|
|
62
|
+
} else if (arg === '--wait') {
|
|
63
|
+
options.waitTime = parseInt(args[++i], 10);
|
|
64
|
+
} else if (!arg.startsWith('-')) {
|
|
65
|
+
options.jsonPath = arg;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
i++;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return options;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function run(args) {
|
|
75
|
+
const options = parseArgs(args);
|
|
76
|
+
|
|
77
|
+
if (!options.jsonPath) {
|
|
78
|
+
console.error('错误: 请指定 JSON 文件路径');
|
|
79
|
+
showHelp();
|
|
80
|
+
process.exit(1);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
options.jsonPath = path.resolve(options.jsonPath);
|
|
84
|
+
options.outputDir = path.resolve(options.outputDir);
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
const result = await takeScreenshot(options);
|
|
88
|
+
console.log(`\n截图成功: ${result.screenshotPath}`);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
console.error(`\n截图失败: ${error.message}`);
|
|
91
|
+
process.exit(1);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
module.exports = { run };
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* set-component 命令 - 设置组件属性
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const { CCSceneAsset, CCPrefab } = require('../lib/cc');
|
|
8
|
+
const { buildTree } = require('../lib/node-utils');
|
|
9
|
+
const { loadScriptMap, isPrefab } = require('../lib/fire-utils');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 查找节点
|
|
13
|
+
*/
|
|
14
|
+
function findNode(root, nodePath) {
|
|
15
|
+
if (!nodePath) return root;
|
|
16
|
+
|
|
17
|
+
const parts = nodePath.split('/').filter(p => p);
|
|
18
|
+
if (parts.length === 0) return root;
|
|
19
|
+
|
|
20
|
+
let current = root;
|
|
21
|
+
|
|
22
|
+
if (parts[0] === root._name) {
|
|
23
|
+
parts.shift();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
for (const part of parts) {
|
|
27
|
+
if (!current._children || current._children.length === 0) return null;
|
|
28
|
+
const found = current._children.find(c => c._name === part);
|
|
29
|
+
if (!found) return null;
|
|
30
|
+
current = found;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return current;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 查找组件
|
|
38
|
+
*/
|
|
39
|
+
function findComponent(node, compType) {
|
|
40
|
+
if (!node._components) return null;
|
|
41
|
+
|
|
42
|
+
const type = compType.toLowerCase();
|
|
43
|
+
const typeName = 'cc.' + type.charAt(0).toUpperCase() + type.slice(1);
|
|
44
|
+
|
|
45
|
+
return node._components.find(c => c.__type__ === typeName);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function run(args) {
|
|
49
|
+
if (args.length < 4) {
|
|
50
|
+
console.log(JSON.stringify({
|
|
51
|
+
error: '用法: cocos2d-cli set-component <场景.fire|预制体.prefab> <节点路径> <组件类型> <属性名> <值>'
|
|
52
|
+
}));
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const filePath = args[0];
|
|
57
|
+
const nodePath = args[1];
|
|
58
|
+
const compType = args[2];
|
|
59
|
+
const prop = args[3];
|
|
60
|
+
const value = args[4];
|
|
61
|
+
|
|
62
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
let root;
|
|
66
|
+
let asset;
|
|
67
|
+
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
68
|
+
|
|
69
|
+
if (ext === '.fire') {
|
|
70
|
+
asset = CCSceneAsset.fromJSON(json);
|
|
71
|
+
root = asset._scene;
|
|
72
|
+
} else if (ext === '.prefab') {
|
|
73
|
+
asset = CCPrefab.fromJSON(json);
|
|
74
|
+
root = asset._root;
|
|
75
|
+
} else {
|
|
76
|
+
console.log(JSON.stringify({ error: '不支持的文件类型,仅支持 .fire 和 .prefab' }));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const node = findNode(root, nodePath);
|
|
81
|
+
|
|
82
|
+
if (!node) {
|
|
83
|
+
console.log(JSON.stringify({ error: `节点不存在: ${nodePath}` }));
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const comp = findComponent(node, compType);
|
|
88
|
+
|
|
89
|
+
if (!comp) {
|
|
90
|
+
console.log(JSON.stringify({ error: `组件不存在: ${compType}` }));
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// 调用组件的 setProp 方法
|
|
95
|
+
if (typeof comp.setProp !== 'function') {
|
|
96
|
+
console.log(JSON.stringify({ error: '组件不支持 setProp 方法' }));
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// 构造属性对象并调用 setProp
|
|
101
|
+
const props = { [prop]: value };
|
|
102
|
+
comp.setProp(props);
|
|
103
|
+
|
|
104
|
+
// 保存
|
|
105
|
+
const data = asset.toJSON();
|
|
106
|
+
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
|
|
107
|
+
|
|
108
|
+
// 输出节点树
|
|
109
|
+
const scriptMap = loadScriptMap(filePath);
|
|
110
|
+
const prefab = isPrefab(data);
|
|
111
|
+
const startIndex = prefab ? 0 : 1;
|
|
112
|
+
console.log(buildTree(data, scriptMap, startIndex).trim());
|
|
113
|
+
|
|
114
|
+
} catch (err) {
|
|
115
|
+
console.log(JSON.stringify({ error: err.message }));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
module.exports = { run };
|
package/src/commands/set.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* set 命令 -
|
|
2
|
+
* set 命令 - 设置节点属性
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
const path = require('path');
|
|
@@ -11,10 +11,10 @@ const { loadScriptMap, isPrefab } = require('../lib/fire-utils');
|
|
|
11
11
|
/**
|
|
12
12
|
* 查找节点
|
|
13
13
|
*/
|
|
14
|
-
function findNode(root,
|
|
15
|
-
if (!
|
|
14
|
+
function findNode(root, nodePath) {
|
|
15
|
+
if (!nodePath) return root;
|
|
16
16
|
|
|
17
|
-
const parts =
|
|
17
|
+
const parts = nodePath.split('/').filter(p => p);
|
|
18
18
|
if (parts.length === 0) return root;
|
|
19
19
|
|
|
20
20
|
let current = root;
|
|
@@ -33,51 +33,15 @@ function findNode(root, path) {
|
|
|
33
33
|
return current;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
/**
|
|
37
|
-
* 查找组件
|
|
38
|
-
*/
|
|
39
|
-
function findComponent(node, compType) {
|
|
40
|
-
if (!node._components) return null;
|
|
41
|
-
|
|
42
|
-
const type = compType.toLowerCase();
|
|
43
|
-
const typeName = 'cc.' + type.charAt(0).toUpperCase() + type.slice(1);
|
|
44
|
-
|
|
45
|
-
return node._components.find(c => c.__type__ === typeName);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 设置节点属性值
|
|
50
|
-
*/
|
|
51
|
-
function setNodeProp(node, prop, value) {
|
|
52
|
-
switch (prop.toLowerCase()) {
|
|
53
|
-
case 'name': node._name = value; return true;
|
|
54
|
-
case 'active': node._active = value === 'true' || value === true; return true;
|
|
55
|
-
case 'x': node._trs.array[0] = parseFloat(value); return true;
|
|
56
|
-
case 'y': node._trs.array[1] = parseFloat(value); return true;
|
|
57
|
-
case 'width': node._contentSize.width = parseFloat(value); return true;
|
|
58
|
-
case 'height': node._contentSize.height = parseFloat(value); return true;
|
|
59
|
-
case 'scalex': node._trs.array[7] = parseFloat(value); return true;
|
|
60
|
-
case 'scaley': node._trs.array[8] = parseFloat(value); return true;
|
|
61
|
-
case 'rotation':
|
|
62
|
-
node._trs.array[5] = parseFloat(value) * Math.PI / 180;
|
|
63
|
-
node._eulerAngles.z = parseFloat(value);
|
|
64
|
-
return true;
|
|
65
|
-
case 'anchorx': node._anchorPoint.x = parseFloat(value); return true;
|
|
66
|
-
case 'anchory': node._anchorPoint.y = parseFloat(value); return true;
|
|
67
|
-
case 'opacity': node._opacity = parseInt(value); return true;
|
|
68
|
-
default: return false;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
36
|
function run(args) {
|
|
73
37
|
if (args.length < 4) {
|
|
74
|
-
console.log(JSON.stringify({ error: '用法: cocos2d-cli set <场景.fire|预制体.prefab> <节点路径>
|
|
38
|
+
console.log(JSON.stringify({ error: '用法: cocos2d-cli set <场景.fire|预制体.prefab> <节点路径> <属性名> <值>' }));
|
|
75
39
|
return;
|
|
76
40
|
}
|
|
77
41
|
|
|
78
42
|
const filePath = args[0];
|
|
79
43
|
const nodePath = args[1];
|
|
80
|
-
const
|
|
44
|
+
const prop = args[2];
|
|
81
45
|
const value = args[3];
|
|
82
46
|
|
|
83
47
|
const ext = path.extname(filePath).toLowerCase();
|
|
@@ -105,31 +69,26 @@ function run(args) {
|
|
|
105
69
|
return;
|
|
106
70
|
}
|
|
107
71
|
|
|
108
|
-
//
|
|
109
|
-
|
|
72
|
+
// 调用节点的 setProp 方法
|
|
73
|
+
if (typeof node.setProp !== 'function') {
|
|
74
|
+
console.log(JSON.stringify({ error: '节点不支持 setProp 方法' }));
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
110
77
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
if (comp[parts[1]] === undefined) {
|
|
120
|
-
console.log(JSON.stringify({ error: `属性不存在: ${parts[1]}` }));
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
comp[parts[1]] = value;
|
|
125
|
-
} else {
|
|
126
|
-
// 节点属性
|
|
127
|
-
if (!setNodeProp(node, propPath, value)) {
|
|
128
|
-
console.log(JSON.stringify({ error: `属性不存在: ${propPath}` }));
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
78
|
+
// 转换数值类型
|
|
79
|
+
let parsedValue = value;
|
|
80
|
+
if (!isNaN(value) && value !== '') {
|
|
81
|
+
parsedValue = parseFloat(value);
|
|
82
|
+
} else if (value === 'true') {
|
|
83
|
+
parsedValue = true;
|
|
84
|
+
} else if (value === 'false') {
|
|
85
|
+
parsedValue = false;
|
|
131
86
|
}
|
|
132
87
|
|
|
88
|
+
// 构造属性对象并调用 setProp
|
|
89
|
+
const props = { [prop]: parsedValue };
|
|
90
|
+
node.setProp(props);
|
|
91
|
+
|
|
133
92
|
// 保存
|
|
134
93
|
const data = asset.toJSON();
|
|
135
94
|
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
|
package/src/lib/cc/CCCanvas.js
CHANGED
|
@@ -8,35 +8,25 @@ class CCCanvas extends CCComponent {
|
|
|
8
8
|
constructor() {
|
|
9
9
|
super();
|
|
10
10
|
this.__type__ = 'cc.Canvas';
|
|
11
|
-
|
|
11
|
+
|
|
12
12
|
this._designResolution = new CCSize(960, 640);
|
|
13
13
|
this._fitWidth = false;
|
|
14
14
|
this._fitHeight = true;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* 设置适配模式
|
|
27
|
-
*/
|
|
28
|
-
setFit(fitWidth, fitHeight) {
|
|
29
|
-
this._fitWidth = fitWidth;
|
|
30
|
-
this._fitHeight = fitHeight;
|
|
17
|
+
setProp(props) {
|
|
18
|
+
super.setProp(props);
|
|
19
|
+
if (props.designResolution) {
|
|
20
|
+
this._designResolution = new CCSize(props.designResolution.width, props.designResolution.height);
|
|
21
|
+
}
|
|
22
|
+
if (props.fitWidth !== undefined) this._fitWidth = props.fitWidth;
|
|
23
|
+
if (props.fitHeight !== undefined) this._fitHeight = props.fitHeight;
|
|
31
24
|
return this;
|
|
32
25
|
}
|
|
33
26
|
|
|
34
|
-
|
|
35
|
-
* 转换为属性面板显示格式
|
|
36
|
-
*/
|
|
37
|
-
toPanelJSON() {
|
|
27
|
+
getProp() {
|
|
38
28
|
return {
|
|
39
|
-
...super.
|
|
29
|
+
...super.getProp(),
|
|
40
30
|
designResolution: {
|
|
41
31
|
width: this._designResolution?.width ?? 960,
|
|
42
32
|
height: this._designResolution?.height ?? 640
|