cocos2d-cli 1.5.2 → 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 +7 -1
- package/package.json +6 -2
- package/src/commands/create-scene.js +2 -7
- package/src/commands/screenshot.js +95 -0
- package/src/lib/cc/CCCanvas.js +10 -20
- package/src/lib/cc/CCComponent.js +4 -11
- package/src/lib/cc/CCNode.js +9 -40
- package/src/lib/cc/CCSprite.js +5 -2
- 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
|
@@ -18,7 +18,8 @@ const commands = {
|
|
|
18
18
|
'remove': '../src/commands/remove',
|
|
19
19
|
build: '../src/commands/build',
|
|
20
20
|
'create-prefab': '../src/commands/prefab-create',
|
|
21
|
-
'create-scene': '../src/commands/create-scene'
|
|
21
|
+
'create-scene': '../src/commands/create-scene',
|
|
22
|
+
'screenshot': '../src/commands/screenshot'
|
|
22
23
|
};
|
|
23
24
|
|
|
24
25
|
// 帮助信息
|
|
@@ -41,6 +42,7 @@ Cocos Creator CLI - 场景/预制体操作工具集
|
|
|
41
42
|
build <项目目录> 构建组件映射
|
|
42
43
|
create-prefab [JSON文件路径] <输出.prefab> 创建预制体(不传JSON则创建默认)
|
|
43
44
|
create-scene [JSON文件路径] <输出.fire> 创建场景(不传JSON则创建默认)
|
|
45
|
+
screenshot <json文件> [选项] 渲染JSON并截图
|
|
44
46
|
|
|
45
47
|
节点路径格式:
|
|
46
48
|
Canvas - 根节点下的 Canvas
|
|
@@ -136,6 +138,10 @@ JSON 格式 (create-prefab / create-scene):
|
|
|
136
138
|
|
|
137
139
|
# 创建默认预制体(不传JSON)
|
|
138
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
|
|
139
145
|
`);
|
|
140
146
|
}
|
|
141
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 组件
|
|
@@ -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 };
|
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
|
|
@@ -8,13 +8,13 @@ class CCComponent extends CCObject {
|
|
|
8
8
|
constructor() {
|
|
9
9
|
super('');
|
|
10
10
|
this.__type__ = 'cc.Component';
|
|
11
|
-
|
|
11
|
+
|
|
12
12
|
// 关联的节点
|
|
13
13
|
this.node = null;
|
|
14
|
-
|
|
14
|
+
|
|
15
15
|
// 启用状态
|
|
16
16
|
this._enabled = true;
|
|
17
|
-
|
|
17
|
+
|
|
18
18
|
// 唯一标识(22位压缩格式)
|
|
19
19
|
this._id = generateId();
|
|
20
20
|
}
|
|
@@ -27,19 +27,12 @@ class CCComponent extends CCObject {
|
|
|
27
27
|
return this;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
/**
|
|
31
|
-
* 设置启用状态
|
|
32
|
-
*/
|
|
33
|
-
setEnabled(enabled) {
|
|
34
|
-
this._enabled = enabled;
|
|
35
|
-
return this;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
30
|
/**
|
|
39
31
|
* 获取属性(子类重写)
|
|
40
32
|
*/
|
|
41
33
|
getProp() {
|
|
42
34
|
return {
|
|
35
|
+
class: this.__type__,
|
|
43
36
|
enabled: this._enabled
|
|
44
37
|
};
|
|
45
38
|
}
|
package/src/lib/cc/CCNode.js
CHANGED
|
@@ -12,37 +12,37 @@ class CCNode extends CCObject {
|
|
|
12
12
|
constructor(name = 'Node') {
|
|
13
13
|
super(name);
|
|
14
14
|
this.__type__ = 'cc.Node';
|
|
15
|
-
|
|
15
|
+
|
|
16
16
|
// 父子关系
|
|
17
17
|
this._parent = null;
|
|
18
18
|
this._children = [];
|
|
19
|
-
|
|
19
|
+
|
|
20
20
|
// 激活状态
|
|
21
21
|
this._active = true;
|
|
22
|
-
|
|
22
|
+
|
|
23
23
|
// 组件列表
|
|
24
24
|
this._components = [];
|
|
25
|
-
|
|
25
|
+
|
|
26
26
|
// 预制体信息
|
|
27
27
|
this._prefab = null;
|
|
28
|
-
|
|
28
|
+
|
|
29
29
|
// 显示属性
|
|
30
30
|
this._opacity = 255;
|
|
31
31
|
this._color = new CCColor();
|
|
32
32
|
this._contentSize = new CCSize();
|
|
33
33
|
this._anchorPoint = new CCVec2(0.5, 0.5);
|
|
34
|
-
|
|
34
|
+
|
|
35
35
|
// 变换属性
|
|
36
36
|
this._trs = new CCTrs();
|
|
37
37
|
this._eulerAngles = new CCVec3();
|
|
38
38
|
this._skewX = 0;
|
|
39
39
|
this._skewY = 0;
|
|
40
40
|
this._is3DNode = false;
|
|
41
|
-
|
|
41
|
+
|
|
42
42
|
// 分组
|
|
43
43
|
this._groupIndex = 0;
|
|
44
44
|
this.groupIndex = 0;
|
|
45
|
-
|
|
45
|
+
|
|
46
46
|
// 唯一标识(预制体中为空,场景中生成)
|
|
47
47
|
this._id = '';
|
|
48
48
|
}
|
|
@@ -193,7 +193,7 @@ class CCNode extends CCObject {
|
|
|
193
193
|
scaleY: trs[8],
|
|
194
194
|
rotation: this._eulerAngles?.z ?? 0,
|
|
195
195
|
opacity: this._opacity ?? 255,
|
|
196
|
-
color: this._color ? `#${this._color.r.toString(16).padStart(2,'0')}${this._color.g.toString(16).padStart(2,'0')}${this._color.b.toString(16).padStart(2,'0')}` : '#ffffff'
|
|
196
|
+
color: this._color ? `#${this._color.r.toString(16).padStart(2, '0')}${this._color.g.toString(16).padStart(2, '0')}${this._color.b.toString(16).padStart(2, '0')}` : '#ffffff'
|
|
197
197
|
};
|
|
198
198
|
return result;
|
|
199
199
|
}
|
|
@@ -220,37 +220,6 @@ class CCNode extends CCObject {
|
|
|
220
220
|
return this;
|
|
221
221
|
}
|
|
222
222
|
|
|
223
|
-
/**
|
|
224
|
-
* 转换为属性面板显示格式(人类可读)
|
|
225
|
-
*/
|
|
226
|
-
toPanelJSON() {
|
|
227
|
-
const trs = this._trs?.array || [0, 0, 0, 0, 0, 0, 1, 1, 1, 1];
|
|
228
|
-
const result = {
|
|
229
|
-
name: this._name,
|
|
230
|
-
active: this._active,
|
|
231
|
-
position: { x: trs[0], y: trs[1] },
|
|
232
|
-
rotation: this._eulerAngles?.z ?? 0,
|
|
233
|
-
scale: { x: trs[7], y: trs[8] },
|
|
234
|
-
anchor: { x: this._anchorPoint?.x ?? 0.5, y: this._anchorPoint?.y ?? 0.5 },
|
|
235
|
-
size: { w: this._contentSize?.width ?? 0, h: this._contentSize?.height ?? 0 },
|
|
236
|
-
color: this._color ? `#${this._color.r.toString(16).padStart(2,'0')}${this._color.g.toString(16).padStart(2,'0')}${this._color.b.toString(16).padStart(2,'0')}` : '#ffffff',
|
|
237
|
-
opacity: this._opacity ?? 255,
|
|
238
|
-
skew: { x: this._skewX ?? 0, y: this._skewY ?? 0 },
|
|
239
|
-
group: this._groupIndex ?? 0
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
// 组件列表
|
|
243
|
-
if (this._components && this._components.length > 0) {
|
|
244
|
-
result.components = {};
|
|
245
|
-
this._components.forEach(c => {
|
|
246
|
-
const typeName = c.__type__.replace('cc.', '');
|
|
247
|
-
result.components[typeName] = c.getProp ? c.getProp() : {};
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
return result;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
223
|
toJSON(indexMap) {
|
|
255
224
|
// 处理引用
|
|
256
225
|
const parent = this._parent ? (indexMap ? { __id__: indexMap.get(this._parent) } : this._parent) : null;
|
package/src/lib/cc/CCSprite.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
const CCComponent = require('./CCComponent');
|
|
2
|
-
const CCColor = require('./CCColor');
|
|
3
2
|
const CCVec2 = require('./CCVec2');
|
|
4
3
|
|
|
4
|
+
const default_sprite_splash = 'a23235d1-15db-4b95-8439-a2e005bfff91';
|
|
5
|
+
|
|
5
6
|
/**
|
|
6
7
|
* Cocos Creator Sprite 组件
|
|
7
8
|
*/
|
|
@@ -9,7 +10,7 @@ class CCSprite extends CCComponent {
|
|
|
9
10
|
constructor() {
|
|
10
11
|
super();
|
|
11
12
|
this.__type__ = 'cc.Sprite';
|
|
12
|
-
|
|
13
|
+
|
|
13
14
|
this._materials = [{ __uuid__: 'eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432' }];
|
|
14
15
|
this._srcBlendFactor = 770;
|
|
15
16
|
this._dstBlendFactor = 771;
|
|
@@ -22,6 +23,8 @@ class CCSprite extends CCComponent {
|
|
|
22
23
|
this._fillRange = 0;
|
|
23
24
|
this._isTrimmedMode = true;
|
|
24
25
|
this._atlas = null;
|
|
26
|
+
|
|
27
|
+
this.setSpriteFrame(default_sprite_splash);
|
|
25
28
|
}
|
|
26
29
|
|
|
27
30
|
/**
|
|
Binary file
|