cocos2d-cli 1.6.5 → 2.0.0

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.
Files changed (61) hide show
  1. package/dist/bin/cocos2d-cli.js +64 -0
  2. package/dist/src/commands/add-component.js +3 -0
  3. package/dist/src/commands/add.js +3 -0
  4. package/dist/src/commands/build.js +6 -0
  5. package/dist/src/commands/create-scene.js +3 -0
  6. package/dist/src/commands/get.js +3 -0
  7. package/dist/src/commands/prefab-create.js +109 -0
  8. package/dist/src/commands/remove-component.js +3 -0
  9. package/dist/src/commands/remove.js +3 -0
  10. package/dist/src/commands/screenshot.js +41 -0
  11. package/dist/src/commands/set-component.js +3 -0
  12. package/dist/src/commands/set.js +3 -0
  13. package/dist/src/commands/tree.js +24 -0
  14. package/{src → dist/src}/lib/cc/CCButton.js +115 -122
  15. package/{src → dist/src}/lib/cc/CCCamera.js +83 -93
  16. package/{src → dist/src}/lib/cc/CCCanvas.js +49 -54
  17. package/{src → dist/src}/lib/cc/CCColor.js +30 -32
  18. package/{src → dist/src}/lib/cc/CCComponent.js +39 -60
  19. package/{src → dist/src}/lib/cc/CCLabel.js +139 -146
  20. package/{src → dist/src}/lib/cc/CCNode.js +190 -256
  21. package/{src → dist/src}/lib/cc/CCObject.js +19 -23
  22. package/{src → dist/src}/lib/cc/CCPrefab.js +219 -242
  23. package/{src → dist/src}/lib/cc/CCRect.js +30 -32
  24. package/{src → dist/src}/lib/cc/CCRichText.js +38 -44
  25. package/{src → dist/src}/lib/cc/CCScene.js +32 -42
  26. package/dist/src/lib/cc/CCSceneAsset.js +242 -0
  27. package/{src → dist/src}/lib/cc/CCSize.js +22 -26
  28. package/{src → dist/src}/lib/cc/CCSprite.js +82 -94
  29. package/{src → dist/src}/lib/cc/CCTrs.js +49 -74
  30. package/{src → dist/src}/lib/cc/CCVec2.js +22 -26
  31. package/{src → dist/src}/lib/cc/CCVec3.js +26 -29
  32. package/{src → dist/src}/lib/cc/CCWidget.js +94 -98
  33. package/dist/src/lib/fire-utils.js +86 -0
  34. package/dist/src/lib/json-parser.js +114 -0
  35. package/dist/src/lib/node-utils.js +131 -0
  36. package/{src → dist/src}/lib/screenshot-core.js +221 -285
  37. package/dist/src/lib/templates.js +17 -0
  38. package/dist/src/lib/utils.js +81 -0
  39. package/package.json +13 -6
  40. package/bin/cocos2d-cli.js +0 -152
  41. package/src/commands/add-component.js +0 -112
  42. package/src/commands/add.js +0 -177
  43. package/src/commands/build.js +0 -78
  44. package/src/commands/create-scene.js +0 -181
  45. package/src/commands/get.js +0 -108
  46. package/src/commands/prefab-create.js +0 -111
  47. package/src/commands/remove-component.js +0 -111
  48. package/src/commands/remove.js +0 -99
  49. package/src/commands/screenshot.js +0 -108
  50. package/src/commands/set-component.js +0 -119
  51. package/src/commands/set.js +0 -107
  52. package/src/commands/tree.js +0 -29
  53. package/src/lib/cc/CCSceneAsset.js +0 -303
  54. package/src/lib/cc/index.js +0 -42
  55. package/src/lib/fire-utils.js +0 -374
  56. package/src/lib/json-parser.js +0 -185
  57. package/src/lib/node-utils.js +0 -395
  58. package/src/lib/screenshot/favicon.ico +0 -0
  59. package/src/lib/screenshot/index.html +0 -30
  60. package/src/lib/templates.js +0 -49
  61. package/src/lib/utils.js +0 -202
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/env node
2
+ import { fileURLToPath } from 'url';
3
+ import * as path from 'path';
4
+ const __filename = fileURLToPath(import.meta.url);
5
+ const __dirname = path.dirname(__filename);
6
+ const commands = {
7
+ tree: '../src/commands/tree.js',
8
+ get: '../src/commands/get.js',
9
+ set: '../src/commands/set.js',
10
+ 'set-component': '../src/commands/set-component.js',
11
+ add: '../src/commands/add.js',
12
+ 'add-component': '../src/commands/add-component.js',
13
+ 'remove-component': '../src/commands/remove-component.js',
14
+ 'remove': '../src/commands/remove.js',
15
+ build: '../src/commands/build.js',
16
+ 'create-prefab': '../src/commands/prefab-create.js',
17
+ 'create-scene': '../src/commands/create-scene.js',
18
+ 'screenshot': '../src/commands/screenshot.js'
19
+ };
20
+ function showHelp() {
21
+ console.log(`
22
+ Cocos Creator CLI - 场景/预制体操作工具集
23
+
24
+ 用法:
25
+ cocos2d-cli <command> [options]
26
+
27
+ 命令:
28
+ tree <场景.fire | 预制体.prefab> 查看节点树
29
+ get <场景.fire | 预制体.prefab> <节点路径> [属性名|组件类型] 获取节点或组件属性
30
+ set <场景.fire | 预制体.prefab> <节点路径> <属性名> <值> 修改节点属性
31
+ set-component <文件> <节点路径> <组件类型> <属性名> <值> 修改组件属性
32
+ add <场景.fire | 预制体.prefab> <父节点路径> <名称> 添加节点
33
+ add-component <文件> <节点路径> <类型> 给节点添加组件
34
+ remove-component <文件> <节点路径> <类型> 删除节点组件
35
+ remove <文件> <节点路径> 删除节点
36
+ build <项目目录> 构建组件映射
37
+ create-prefab [JSON文件路径] <输出.prefab> 创建预制体
38
+ create-scene [JSON文件路径] <输出.fire> 创建场景
39
+ screenshot <json文件> [选项] 渲染JSON并截图
40
+ `);
41
+ }
42
+ const args = process.argv.slice(2);
43
+ if (args.length === 0 || args[0] === '--help' || args[0] === '-h' || args[0] === 'help') {
44
+ showHelp();
45
+ process.exit(0);
46
+ }
47
+ const commandName = args[0];
48
+ const commandPath = commands[commandName];
49
+ if (!commandPath) {
50
+ console.error(`未知命令: ${commandName}`);
51
+ console.error('运行 cocos2d-cli --help 查看可用命令');
52
+ process.exit(1);
53
+ }
54
+ async function main() {
55
+ try {
56
+ const command = await import(commandPath);
57
+ await command.run(args.slice(1));
58
+ }
59
+ catch (err) {
60
+ console.error(`命令执行失败: ${err.message}`);
61
+ process.exit(1);
62
+ }
63
+ }
64
+ main();
@@ -0,0 +1,3 @@
1
+ export function run(args) {
2
+ console.log(JSON.stringify({ args }));
3
+ }
@@ -0,0 +1,3 @@
1
+ export function run(args) {
2
+ console.log(JSON.stringify({ args }));
3
+ }
@@ -0,0 +1,6 @@
1
+ import * as path from 'path';
2
+ import { fileURLToPath } from 'url';
3
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
4
+ export function run(args) {
5
+ console.log(JSON.stringify({ args }));
6
+ }
@@ -0,0 +1,3 @@
1
+ export function run(args) {
2
+ console.log(JSON.stringify({ args }));
3
+ }
@@ -0,0 +1,3 @@
1
+ export function run(args) {
2
+ console.log(JSON.stringify({ args }));
3
+ }
@@ -0,0 +1,109 @@
1
+ import * as fs from 'fs';
2
+ import { CCPrefab } from '../lib/cc/CCPrefab.js';
3
+ import CCNode from '../lib/cc/CCNode.js';
4
+ import CCCanvas from '../lib/cc/CCCanvas.js';
5
+ import CCWidget from '../lib/cc/CCWidget.js';
6
+ import CCSprite from '../lib/cc/CCSprite.js';
7
+ import CCLabel from '../lib/cc/CCLabel.js';
8
+ import CCButton from '../lib/cc/CCButton.js';
9
+ function createNodeFromJSON(json) {
10
+ const node = new CCNode(json.name || 'Node');
11
+ if (json.x !== undefined)
12
+ node.x = json.x;
13
+ if (json.y !== undefined)
14
+ node.y = json.y;
15
+ if (json.width !== undefined)
16
+ node.width = json.width;
17
+ if (json.height !== undefined)
18
+ node.height = json.height;
19
+ if (json.anchorX !== undefined)
20
+ node.anchorX = json.anchorX;
21
+ if (json.anchorY !== undefined)
22
+ node.anchorY = json.anchorY;
23
+ if (json.color !== undefined) {
24
+ const color = hexToRgb(json.color);
25
+ if (color)
26
+ node.setColor(color.r, color.g, color.b);
27
+ }
28
+ if (json.active !== undefined)
29
+ node._active = json.active;
30
+ if (json.components) {
31
+ for (const comp of json.components) {
32
+ let component = null;
33
+ if (typeof comp === 'string') {
34
+ component = createComponent(comp);
35
+ }
36
+ else if (comp.type) {
37
+ component = createComponent(comp.type, comp);
38
+ }
39
+ if (component) {
40
+ component.node = node;
41
+ node._components.push(component);
42
+ }
43
+ }
44
+ }
45
+ if (json.children) {
46
+ for (const childJson of json.children) {
47
+ const childNode = createNodeFromJSON(childJson);
48
+ childNode._parent = node;
49
+ node._children.push(childNode);
50
+ }
51
+ }
52
+ return node;
53
+ }
54
+ function createComponent(type, config) {
55
+ switch (type.toLowerCase()) {
56
+ case 'sprite':
57
+ return new CCSprite();
58
+ case 'label':
59
+ const label = new CCLabel();
60
+ if (config) {
61
+ if (config.string)
62
+ label.setString(config.string);
63
+ if (config.fontSize)
64
+ label._fontSize = config.fontSize;
65
+ if (config.horizontalAlign) {
66
+ const alignMap = { left: 0, center: 1, right: 2 };
67
+ label._N$horizontalAlign = alignMap[config.horizontalAlign] ?? 1;
68
+ }
69
+ }
70
+ return label;
71
+ case 'button':
72
+ return new CCButton();
73
+ case 'canvas':
74
+ return new CCCanvas();
75
+ case 'widget':
76
+ return new CCWidget();
77
+ default:
78
+ return null;
79
+ }
80
+ }
81
+ function hexToRgb(hex) {
82
+ const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
83
+ return result ? {
84
+ r: parseInt(result[1], 16),
85
+ g: parseInt(result[2], 16),
86
+ b: parseInt(result[3], 16)
87
+ } : null;
88
+ }
89
+ export function run(args) {
90
+ const jsonPath = args[0];
91
+ const outputPath = args[1] || 'output.prefab';
92
+ if (!jsonPath) {
93
+ console.log(JSON.stringify({ error: '用法: cocos2d-cli create-prefab <json文件> [输出.prefab]' }));
94
+ return;
95
+ }
96
+ try {
97
+ const jsonContent = fs.readFileSync(jsonPath, 'utf8');
98
+ const jsonData = JSON.parse(jsonContent);
99
+ const rootNode = createNodeFromJSON(jsonData);
100
+ const prefab = new CCPrefab();
101
+ prefab.setRoot(rootNode);
102
+ const prefabData = prefab.toJSON();
103
+ fs.writeFileSync(outputPath, JSON.stringify(prefabData, null, 2), 'utf8');
104
+ console.log(JSON.stringify({ success: true, outputPath }));
105
+ }
106
+ catch (err) {
107
+ console.log(JSON.stringify({ error: err.message }));
108
+ }
109
+ }
@@ -0,0 +1,3 @@
1
+ export function run(args) {
2
+ console.log(JSON.stringify({ args }));
3
+ }
@@ -0,0 +1,3 @@
1
+ export function run(args) {
2
+ console.log(JSON.stringify({ args }));
3
+ }
@@ -0,0 +1,41 @@
1
+ import * as path from 'path';
2
+ import { takeScreenshot } from '../lib/screenshot-core.js';
3
+ export async function run(args) {
4
+ const jsonFilePath = args[0];
5
+ if (!jsonFilePath) {
6
+ console.log(JSON.stringify({ error: '用法: cocos2d-cli screenshot <json文件> [--width <宽度>] [--height <高度>] [--output <输出路径>]' }));
7
+ return;
8
+ }
9
+ let width = 750;
10
+ let height = 1334;
11
+ let outputPath = path.join(process.cwd(), `screenshot-${Date.now()}.png`);
12
+ for (let i = 1; i < args.length; i++) {
13
+ const arg = args[i];
14
+ if (arg === '--width' && args[i + 1]) {
15
+ width = parseInt(args[i + 1]);
16
+ i++;
17
+ }
18
+ else if (arg === '--height' && args[i + 1]) {
19
+ height = parseInt(args[i + 1]);
20
+ i++;
21
+ }
22
+ else if (arg === '--output' && args[i + 1]) {
23
+ outputPath = args[i + 1];
24
+ i++;
25
+ }
26
+ }
27
+ try {
28
+ const { screenshotPath, logs } = await takeScreenshot({
29
+ jsonPath: jsonFilePath,
30
+ viewport: { width, height },
31
+ outputDir: path.dirname(outputPath),
32
+ fullPage: true,
33
+ timeout: 30000,
34
+ waitTime: 1000
35
+ });
36
+ console.log(JSON.stringify({ success: true, outputPath: screenshotPath }));
37
+ }
38
+ catch (err) {
39
+ console.log(JSON.stringify({ error: err.message }));
40
+ }
41
+ }
@@ -0,0 +1,3 @@
1
+ export function run(args) {
2
+ console.log(JSON.stringify({ args }));
3
+ }
@@ -0,0 +1,3 @@
1
+ export function run(args) {
2
+ console.log(JSON.stringify({ args }));
3
+ }
@@ -0,0 +1,24 @@
1
+ import { loadScene, loadScriptMap, isPrefab } from '../lib/fire-utils.js';
2
+ import { buildTree } from '../lib/node-utils.js';
3
+ export function run(args) {
4
+ const filePath = args[0];
5
+ if (!filePath) {
6
+ console.log(JSON.stringify({ error: '用法: cocos2d-cli tree <场景.fire | 预制体.prefab>' }));
7
+ return;
8
+ }
9
+ try {
10
+ const data = loadScene(filePath);
11
+ if (!data || data.length === 0) {
12
+ console.log(JSON.stringify({ error: '文件为空或格式错误' }));
13
+ return;
14
+ }
15
+ const scriptMap = loadScriptMap(filePath);
16
+ const prefab = isPrefab(data);
17
+ const startIndex = prefab ? 0 : 1;
18
+ const tree = buildTree(data, scriptMap, startIndex);
19
+ console.log(tree ? tree.trim() : '{}');
20
+ }
21
+ catch (err) {
22
+ console.log(JSON.stringify({ error: err.message }));
23
+ }
24
+ }
@@ -1,122 +1,115 @@
1
- const CCComponent = require('./CCComponent');
2
- const CCColor = require('./CCColor');
3
-
4
- /**
5
- * Cocos Creator Button 组件
6
- */
7
- class CCButton extends CCComponent {
8
- constructor() {
9
- super();
10
- this.__type__ = 'cc.Button';
11
-
12
- this._normalMaterial = null;
13
- this._grayMaterial = null;
14
- this.duration = 0.1;
15
- this.zoomScale = 1.2;
16
- this.clickEvents = [];
17
- this._N$interactable = true;
18
- this._N$enableAutoGrayEffect = false;
19
- this._N$transition = 3;
20
- this.transition = 3;
21
- this._N$normalColor = new CCColor();
22
- this._N$pressedColor = new CCColor(200, 200, 200, 255);
23
- this.pressedColor = new CCColor(200, 200, 200, 255);
24
- this._N$hoverColor = new CCColor();
25
- this.hoverColor = new CCColor();
26
- this._N$disabledColor = new CCColor(120, 120, 120, 200);
27
- this._N$normalSprite = null;
28
- this._N$pressedSprite = null;
29
- this.pressedSprite = null;
30
- this._N$hoverSprite = null;
31
- this.hoverSprite = null;
32
- this._N$disabledSprite = null;
33
- this._N$target = null;
34
- }
35
-
36
- /**
37
- * 设置缩放
38
- */
39
- setZoomScale(scale) {
40
- this.zoomScale = scale;
41
- return this;
42
- }
43
-
44
- /**
45
- * 设置过渡类型
46
- * 0: NONE, 1: COLOR, 2: SPRITE, 3: SCALE
47
- */
48
- setTransition(type) {
49
- this._N$transition = type;
50
- this.transition = type;
51
- return this;
52
- }
53
-
54
- /**
55
- * 设置正常精灵
56
- */
57
- setNormalSprite(uuid) {
58
- this._N$normalSprite = { __uuid__: uuid };
59
- return this;
60
- }
61
-
62
- /**
63
- * 设置点击事件
64
- */
65
- addClickEvent(component, handler, target = null) {
66
- this.clickEvents.push({
67
- target: target,
68
- component: component,
69
- handler: handler
70
- });
71
- return this;
72
- }
73
-
74
- /**
75
- * 转换为属性面板显示格式
76
- */
77
- toPanelJSON() {
78
- const TRANSITION = ['NONE', 'COLOR', 'SPRITE', 'SCALE'];
79
- return {
80
- ...super.toPanelJSON(),
81
- interactable: this._N$interactable,
82
- transition: TRANSITION[this._N$transition] || this._N$transition,
83
- zoomScale: this.zoomScale,
84
- duration: this.duration
85
- };
86
- }
87
-
88
- toJSON() {
89
- return {
90
- __type__: this.__type__,
91
- _name: this._name,
92
- _objFlags: this._objFlags,
93
- node: this.node,
94
- _enabled: this._enabled,
95
- _normalMaterial: this._normalMaterial,
96
- _grayMaterial: this._grayMaterial,
97
- duration: this.duration,
98
- zoomScale: this.zoomScale,
99
- clickEvents: this.clickEvents,
100
- _N$interactable: this._N$interactable,
101
- _N$enableAutoGrayEffect: this._N$enableAutoGrayEffect,
102
- _N$transition: this._N$transition,
103
- transition: this.transition,
104
- _N$normalColor: this._N$normalColor.toJSON(),
105
- _N$pressedColor: this._N$pressedColor.toJSON(),
106
- pressedColor: this.pressedColor.toJSON(),
107
- _N$hoverColor: this._N$hoverColor.toJSON(),
108
- hoverColor: this.hoverColor.toJSON(),
109
- _N$disabledColor: this._N$disabledColor.toJSON(),
110
- _N$normalSprite: this._N$normalSprite,
111
- _N$pressedSprite: this._N$pressedSprite,
112
- pressedSprite: this.pressedSprite,
113
- _N$hoverSprite: this._N$hoverSprite,
114
- hoverSprite: this.hoverSprite,
115
- _N$disabledSprite: this._N$disabledSprite,
116
- _N$target: this._N$target,
117
- _id: this._id
118
- };
119
- }
120
- }
121
-
122
- module.exports = CCButton;
1
+ import CCComponent from './CCComponent.js';
2
+ import CCColor from './CCColor.js';
3
+ export default class CCButton extends CCComponent {
4
+ _normalMaterial;
5
+ _grayMaterial;
6
+ duration;
7
+ zoomScale;
8
+ clickEvents;
9
+ _N$interactable;
10
+ _N$enableAutoGrayEffect;
11
+ _N$transition;
12
+ transition;
13
+ _N$normalColor;
14
+ _N$pressedColor;
15
+ pressedColor;
16
+ _N$hoverColor;
17
+ hoverColor;
18
+ _N$disabledColor;
19
+ _N$normalSprite;
20
+ _N$pressedSprite;
21
+ pressedSprite;
22
+ _N$hoverSprite;
23
+ hoverSprite;
24
+ _N$disabledSprite;
25
+ _N$target;
26
+ constructor() {
27
+ super();
28
+ this.__type__ = 'cc.Button';
29
+ this._normalMaterial = null;
30
+ this._grayMaterial = null;
31
+ this.duration = 0.1;
32
+ this.zoomScale = 1.2;
33
+ this.clickEvents = [];
34
+ this._N$interactable = true;
35
+ this._N$enableAutoGrayEffect = false;
36
+ this._N$transition = 3;
37
+ this.transition = 3;
38
+ this._N$normalColor = new CCColor();
39
+ this._N$pressedColor = new CCColor(200, 200, 200, 255);
40
+ this.pressedColor = new CCColor(200, 200, 200, 255);
41
+ this._N$hoverColor = new CCColor();
42
+ this.hoverColor = new CCColor();
43
+ this._N$disabledColor = new CCColor(120, 120, 120, 200);
44
+ this._N$normalSprite = null;
45
+ this._N$pressedSprite = null;
46
+ this.pressedSprite = null;
47
+ this._N$hoverSprite = null;
48
+ this.hoverSprite = null;
49
+ this._N$disabledSprite = null;
50
+ this._N$target = null;
51
+ }
52
+ setZoomScale(scale) {
53
+ this.zoomScale = scale;
54
+ return this;
55
+ }
56
+ setTransition(type) {
57
+ this._N$transition = type;
58
+ this.transition = type;
59
+ return this;
60
+ }
61
+ setNormalSprite(uuid) {
62
+ this._N$normalSprite = { __uuid__: uuid };
63
+ return this;
64
+ }
65
+ addClickEvent(component, handler, target = null) {
66
+ this.clickEvents.push({
67
+ target: target,
68
+ component: component,
69
+ handler: handler
70
+ });
71
+ return this;
72
+ }
73
+ toPanelJSON() {
74
+ const TRANSITION = ['NONE', 'COLOR', 'SPRITE', 'SCALE'];
75
+ return {
76
+ ...super.getProp(),
77
+ interactable: this._N$interactable,
78
+ transition: TRANSITION[this._N$transition] || this._N$transition,
79
+ zoomScale: this.zoomScale,
80
+ duration: this.duration
81
+ };
82
+ }
83
+ toJSON() {
84
+ return {
85
+ __type__: this.__type__,
86
+ _name: this._name,
87
+ _objFlags: this._objFlags,
88
+ node: this.node,
89
+ _enabled: this._enabled,
90
+ _normalMaterial: this._normalMaterial,
91
+ _grayMaterial: this._grayMaterial,
92
+ duration: this.duration,
93
+ zoomScale: this.zoomScale,
94
+ clickEvents: this.clickEvents,
95
+ _N$interactable: this._N$interactable,
96
+ _N$enableAutoGrayEffect: this._N$enableAutoGrayEffect,
97
+ _N$transition: this._N$transition,
98
+ transition: this.transition,
99
+ _N$normalColor: this._N$normalColor.toJSON(),
100
+ _N$pressedColor: this._N$pressedColor.toJSON(),
101
+ pressedColor: this.pressedColor.toJSON(),
102
+ _N$hoverColor: this._N$hoverColor.toJSON(),
103
+ hoverColor: this.hoverColor.toJSON(),
104
+ _N$disabledColor: this._N$disabledColor.toJSON(),
105
+ _N$normalSprite: this._N$normalSprite,
106
+ _N$pressedSprite: this._N$pressedSprite,
107
+ pressedSprite: this.pressedSprite,
108
+ _N$hoverSprite: this._N$hoverSprite,
109
+ hoverSprite: this.hoverSprite,
110
+ _N$disabledSprite: this._N$disabledSprite,
111
+ _N$target: this._N$target,
112
+ _id: this._id
113
+ };
114
+ }
115
+ }