cocos2d-cli 1.5.2 → 1.6.2

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.
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- /**
2
+ /**
3
3
  * Cocos Creator CLI
4
4
  * Command-line tools for AI to read and manipulate Cocos Creator 2.4.x project scenes
5
5
  */
@@ -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
@@ -79,41 +81,21 @@ add 支持的选项:
79
81
  --string=<文字> Label 文字内容 (配合 --type=label)
80
82
  --fontSize=<数值> Label 字体大小 (配合 --type=label)
81
83
 
82
- JSON 格式 (create-prefab / create-scene):
83
- 注意: 参数必须是 JSON 文件路径,不支持直接传递 JSON 字符串
84
-
84
+ JSON 格式:
85
+ 详见 SKILL.md 完整文档。简要示例:
85
86
  {
86
- "name": "节点名称",
87
- "width": 400,
88
- "height": 300,
89
- "x": 0,
90
- "y": 0,
91
- "color": "#336699",
92
- "opacity": 255,
87
+ "name": "Node",
88
+ "width": 400, "height": 300,
89
+ "x": 0, "y": 0,
90
+ "anchorX": 0, "color": "#336699",
93
91
  "components": [
94
- "sprite",
95
- { "type": "widget", "top": 0, "left": 0, "right": 0, "bottom": 0 },
96
- { "type": "label", "string": "Hello", "fontSize": 32 }
92
+ { "type": "label", "string": "Hello", "horizontalAlign": "left" },
93
+ { "type": "richText", "string": "<color=#3cb034>绿色</color>" }
97
94
  ],
98
95
  "children": [...]
99
96
  }
100
97
 
101
- 节点属性: name, width, height, x, y, color, opacity, anchorX, anchorY, rotation, scaleX, scaleY, active
102
-
103
- 组件写法:
104
- 简写: "sprite" 或 "label"
105
- 完整: { "type": "sprite", "sizeMode": 1 }
106
- 完整: { "type": "label", "string": "文本", "fontSize": 32, "color": "#fff" }
107
-
108
- 组件类型:
109
- sprite - 精灵(默认白色方块,节点设置什么颜色就显示什么颜色)
110
- label - 文本,支持 string, fontSize, color(兼容)
111
- button - 按钮,通常配合 sprite 使用才能看见
112
- widget - 对齐,支持 top, bottom, left, right
113
- layout - 布局,自动排列子节点
114
- canvas - 画布,根节点使用
115
- camera - 相机
116
- particle - 粒子效果
98
+ 提示:配合 anchorX + horizontalAlign 实现靠左/靠右布局(见 SKILL.md)
117
99
 
118
100
  示例:
119
101
  cocos2d-cli tree assets/main.fire
@@ -136,6 +118,10 @@ JSON 格式 (create-prefab / create-scene):
136
118
 
137
119
  # 创建默认预制体(不传JSON)
138
120
  cocos2d-cli create-prefab assets/NewNode.prefab
121
+
122
+ # 截图
123
+ cocos2d-cli screenshot data.json
124
+ cocos2d-cli screenshot data.json -o ./screenshots --width 1080 --height 1920
139
125
  `);
140
126
  }
141
127
 
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
- "name": "cocos2d-cli",
3
- "version": "1.5.2",
2
+ "name": "cocos2d-cli",
3
+ "version": "1.6.2",
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": {
7
- "cocos2d-cli": "./bin/cocos2d-cli.js"
7
+ "cocos2d-cli": "bin/cocos2d-cli.js"
8
8
  },
9
9
  "files": [
10
10
  "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.setDesignResolution(width, height);
86
- canvasComp.node = canvas;
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,100 @@
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
+ --width <数值> 视口宽度,默认 750(不支持简写)
17
+ --height <数值> 视口高度,默认 1334(不支持简写)
18
+ --full-page 全页截图(默认)
19
+ --no-full-page 仅视口截图
20
+ --debug-bounds 叠加节点边界框和名称,方便定位布局问题
21
+ --timeout <毫秒> 页面加载超时,默认 30000
22
+ --wait <毫秒> 截图前等待时间,默认 1000
23
+
24
+ 示例:
25
+ cocos2d-cli screenshot data.json
26
+ cocos2d-cli screenshot data.json -o ./screenshots
27
+ cocos2d-cli screenshot data.json --width 1080 --height 1920
28
+ cocos2d-cli screenshot data.json --debug-bounds
29
+ cocos2d-cli screenshot data.json --no-full-page
30
+ `);
31
+ }
32
+
33
+ function parseArgs(args) {
34
+ const options = {
35
+ jsonPath: null,
36
+ outputDir: '.',
37
+ viewport: { width: 750, height: 1334 },
38
+ fullPage: true,
39
+ debugBounds: false,
40
+ timeout: 30000,
41
+ waitTime: 1000
42
+ };
43
+
44
+ let i = 0;
45
+ while (i < args.length) {
46
+ const arg = args[i];
47
+
48
+ if (arg === '--help' || arg === '-h') {
49
+ showHelp();
50
+ process.exit(0);
51
+ }
52
+
53
+ if (arg === '-o' || arg === '--output') {
54
+ options.outputDir = args[++i];
55
+ } else if (arg === '--width') {
56
+ options.viewport.width = parseInt(args[++i], 10);
57
+ } else if (arg === '--height') {
58
+ options.viewport.height = parseInt(args[++i], 10);
59
+ } else if (arg === '--full-page') {
60
+ options.fullPage = true;
61
+ } else if (arg === '--no-full-page') {
62
+ options.fullPage = false;
63
+ } else if (arg === '--debug-bounds') {
64
+ options.debugBounds = true;
65
+ } else if (arg === '--timeout') {
66
+ options.timeout = parseInt(args[++i], 10);
67
+ } else if (arg === '--wait') {
68
+ options.waitTime = parseInt(args[++i], 10);
69
+ } else if (!arg.startsWith('-')) {
70
+ options.jsonPath = arg;
71
+ }
72
+
73
+ i++;
74
+ }
75
+
76
+ return options;
77
+ }
78
+
79
+ async function run(args) {
80
+ const options = parseArgs(args);
81
+
82
+ if (!options.jsonPath) {
83
+ console.error('错误: 请指定 JSON 文件路径');
84
+ showHelp();
85
+ process.exit(1);
86
+ }
87
+
88
+ options.jsonPath = path.resolve(options.jsonPath);
89
+ options.outputDir = path.resolve(options.outputDir);
90
+
91
+ try {
92
+ const result = await takeScreenshot(options);
93
+ console.log(`\n截图成功: ${result.screenshotPath}`);
94
+ } catch (error) {
95
+ console.error(`\n截图失败: ${error.message}`);
96
+ process.exit(1);
97
+ }
98
+ }
99
+
100
+ module.exports = { run };
@@ -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
- setDesignResolution(width, height) {
21
- this._designResolution.set(width, height);
22
- return this;
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.toPanelJSON(),
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
  }
@@ -74,6 +74,31 @@ class CCLabel extends CCComponent {
74
74
  };
75
75
  }
76
76
 
77
+ /**
78
+ * 将语义化对齐字符串转为数字
79
+ * horizontalAlign: left=0, center=1, right=2
80
+ * verticalAlign: top=0, center=1, bottom=2
81
+ */
82
+ static parseHAlign(value) {
83
+ if (typeof value === 'number') return value;
84
+ switch (String(value).toLowerCase()) {
85
+ case 'left': return 0;
86
+ case 'center': return 1;
87
+ case 'right': return 2;
88
+ default: return 1;
89
+ }
90
+ }
91
+
92
+ static parseVAlign(value) {
93
+ if (typeof value === 'number') return value;
94
+ switch (String(value).toLowerCase()) {
95
+ case 'top': return 0;
96
+ case 'center': return 1;
97
+ case 'bottom': return 2;
98
+ default: return 1;
99
+ }
100
+ }
101
+
77
102
  /**
78
103
  * 设置属性
79
104
  */
@@ -82,8 +107,8 @@ class CCLabel extends CCComponent {
82
107
  if (props.fontSize !== undefined) this.setFontSize(props.fontSize);
83
108
  if (props.lineHeight !== undefined) this._lineHeight = props.lineHeight;
84
109
  if (props.fontFamily !== undefined) this.setFontFamily(props.fontFamily);
85
- if (props.horizontalAlign !== undefined) this._N$horizontalAlign = props.horizontalAlign;
86
- if (props.verticalAlign !== undefined) this._N$verticalAlign = props.verticalAlign;
110
+ if (props.horizontalAlign !== undefined) this._N$horizontalAlign = CCLabel.parseHAlign(props.horizontalAlign);
111
+ if (props.verticalAlign !== undefined) this._N$verticalAlign = CCLabel.parseVAlign(props.verticalAlign);
87
112
  return this;
88
113
  }
89
114
 
@@ -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;
@@ -0,0 +1,44 @@
1
+ const CCComponent = require('./CCComponent');
2
+
3
+ /**
4
+ * Cocos Creator RichText 组件
5
+ * 支持 BBCode 标签语法:
6
+ * <color=#ff0000>红色</color>
7
+ * <size=30>大字</size>
8
+ * <b>加粗</b> <i>斜体</i> <u>下划线</u>
9
+ * <br/> 换行
10
+ */
11
+ class CCRichText extends CCComponent {
12
+ constructor() {
13
+ super();
14
+ this.__type__ = 'cc.RichText';
15
+
16
+ this._string = '';
17
+ this._horizontalAlign = 0; // 0=LEFT 1=CENTER 2=RIGHT
18
+ this._fontSize = 40;
19
+ this._maxWidth = 0;
20
+ this._lineHeight = 40;
21
+ this._imageAtlas = null;
22
+ this._handleTouchEvent = true;
23
+ }
24
+
25
+ toJSON() {
26
+ return {
27
+ __type__: this.__type__,
28
+ _name: this._name,
29
+ _objFlags: this._objFlags,
30
+ node: this.node,
31
+ _enabled: this._enabled,
32
+ _string: this._string,
33
+ _horizontalAlign: this._horizontalAlign,
34
+ _fontSize: this._fontSize,
35
+ _maxWidth: this._maxWidth,
36
+ _lineHeight: this._lineHeight,
37
+ _imageAtlas: this._imageAtlas,
38
+ _handleTouchEvent: this._handleTouchEvent,
39
+ _id: this._id
40
+ };
41
+ }
42
+ }
43
+
44
+ module.exports = CCRichText;
@@ -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,19 +10,21 @@ 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;
16
17
  this._spriteFrame = null;
17
18
  this._type = 0;
18
- this._sizeMode = 1;
19
+ this._sizeMode = 0;
19
20
  this._fillType = 0;
20
21
  this._fillCenter = new CCVec2();
21
22
  this._fillStart = 0;
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
  /**
@@ -34,7 +37,7 @@ class CCSprite extends CCComponent {
34
37
 
35
38
  /**
36
39
  * 设置尺寸模式
37
- * 0: CUSTOM, 1: RAW, 2: TRIMMED
40
+ * 0: CUSTOM, 1: TRIMMED, 2: RAW
38
41
  */
39
42
  setSizeMode(mode) {
40
43
  this._sizeMode = mode;
@@ -16,6 +16,7 @@ const CCCamera = require('./CCCamera');
16
16
  const CCSprite = require('./CCSprite');
17
17
  const CCLabel = require('./CCLabel');
18
18
  const CCButton = require('./CCButton');
19
+ const CCRichText = require('./CCRichText');
19
20
 
20
21
  module.exports = {
21
22
  CCObject,
@@ -36,5 +37,6 @@ module.exports = {
36
37
  CCCamera,
37
38
  CCSprite,
38
39
  CCLabel,
39
- CCButton
40
+ CCButton,
41
+ CCRichText
40
42
  };
@@ -3,7 +3,7 @@
3
3
  * 将简化JSON转换为CCNode对象树
4
4
  */
5
5
 
6
- const { CCNode, CCCanvas, CCWidget, CCSprite, CCLabel, CCButton, CCCamera } = require('./cc');
6
+ const { CCNode, CCCanvas, CCWidget, CCSprite, CCLabel, CCButton, CCCamera, CCRichText } = require('./cc');
7
7
  const { parseColor } = require('./utils');
8
8
 
9
9
  /**
@@ -11,13 +11,14 @@ const { parseColor } = require('./utils');
11
11
  */
12
12
  function createComponent(type) {
13
13
  switch (type.toLowerCase()) {
14
- case 'canvas': return new CCCanvas();
15
- case 'widget': return new CCWidget();
16
- case 'sprite': return new CCSprite();
17
- case 'label': return new CCLabel();
18
- case 'button': return new CCButton();
19
- case 'camera': return new CCCamera();
20
- default: return null;
14
+ case 'canvas': return new CCCanvas();
15
+ case 'widget': return new CCWidget();
16
+ case 'sprite': return new CCSprite();
17
+ case 'label': return new CCLabel();
18
+ case 'button': return new CCButton();
19
+ case 'camera': return new CCCamera();
20
+ case 'richtext': return new CCRichText();
21
+ default: return null;
21
22
  }
22
23
  }
23
24
 
@@ -59,7 +60,25 @@ function applyComponentProps(comp, props, node) {
59
60
  case 'sizeMode':
60
61
  if (comp._sizeMode !== undefined) comp._sizeMode = value;
61
62
  break;
63
+ case 'horizontalAlign':
64
+ // 支持语义化字符串:left / center / right
65
+ if (comp._N$horizontalAlign !== undefined) {
66
+ comp._N$horizontalAlign = CCLabel.parseHAlign(value);
67
+ } else if (comp._horizontalAlign !== undefined) {
68
+ comp._horizontalAlign = CCLabel.parseHAlign(value);
69
+ }
70
+ break;
71
+ case 'verticalAlign':
72
+ // 支持语义化字符串:top / center / bottom
73
+ if (comp._N$verticalAlign !== undefined) {
74
+ comp._N$verticalAlign = CCLabel.parseVAlign(value);
75
+ } else if (comp._verticalAlign !== undefined) {
76
+ comp._verticalAlign = CCLabel.parseVAlign(value);
77
+ }
78
+ break;
62
79
  case 'color':
80
+ // 兼容写法:组件内的 color 同步到节点颜色
81
+ // (Cocos 中文字/富文本颜色本质是节点颜色,不是组件属性)
63
82
  if (node) {
64
83
  const parsed = parseColor(value);
65
84
  if (parsed && node._color) {
Binary file