cocos2d-cli 1.6.5 → 2.1.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.
- package/data/script_map.json +25 -25
- package/dist/bin/cocos2d-cli.js +64 -0
- package/dist/src/commands/add-component.js +3 -0
- package/dist/src/commands/add.js +3 -0
- package/dist/src/commands/build.js +6 -0
- package/dist/src/commands/create-scene.js +3 -0
- package/dist/src/commands/get.js +3 -0
- package/dist/src/commands/prefab-create.js +109 -0
- package/dist/src/commands/remove-component.js +3 -0
- package/dist/src/commands/remove.js +3 -0
- package/dist/src/commands/screenshot.js +41 -0
- package/dist/src/commands/set-component.js +3 -0
- package/dist/src/commands/set.js +3 -0
- package/dist/src/commands/tree.js +24 -0
- package/{src → dist/src}/lib/cc/CCButton.js +115 -122
- package/{src → dist/src}/lib/cc/CCCamera.js +83 -93
- package/{src → dist/src}/lib/cc/CCCanvas.js +49 -54
- package/{src → dist/src}/lib/cc/CCColor.js +30 -32
- package/{src → dist/src}/lib/cc/CCComponent.js +39 -60
- package/{src → dist/src}/lib/cc/CCLabel.js +139 -146
- package/{src → dist/src}/lib/cc/CCNode.js +190 -256
- package/{src → dist/src}/lib/cc/CCObject.js +19 -23
- package/{src → dist/src}/lib/cc/CCPrefab.js +219 -242
- package/{src → dist/src}/lib/cc/CCRect.js +30 -32
- package/{src → dist/src}/lib/cc/CCRichText.js +38 -44
- package/{src → dist/src}/lib/cc/CCScene.js +32 -42
- package/dist/src/lib/cc/CCSceneAsset.js +242 -0
- package/{src → dist/src}/lib/cc/CCSize.js +22 -26
- package/{src → dist/src}/lib/cc/CCSprite.js +82 -94
- package/{src → dist/src}/lib/cc/CCTrs.js +49 -74
- package/{src → dist/src}/lib/cc/CCVec2.js +22 -26
- package/{src → dist/src}/lib/cc/CCVec3.js +26 -29
- package/{src → dist/src}/lib/cc/CCWidget.js +94 -98
- package/dist/src/lib/fire-utils.js +86 -0
- package/dist/src/lib/json-parser.js +114 -0
- package/dist/src/lib/node-utils.js +131 -0
- package/{src → dist/src}/lib/screenshot-core.js +242 -285
- package/dist/src/lib/templates.js +17 -0
- package/dist/src/lib/utils.js +81 -0
- package/package.json +40 -33
- package/bin/cocos2d-cli.js +0 -152
- package/src/commands/add-component.js +0 -112
- package/src/commands/add.js +0 -177
- package/src/commands/build.js +0 -78
- package/src/commands/create-scene.js +0 -181
- package/src/commands/get.js +0 -108
- package/src/commands/prefab-create.js +0 -111
- package/src/commands/remove-component.js +0 -111
- package/src/commands/remove.js +0 -99
- package/src/commands/screenshot.js +0 -108
- package/src/commands/set-component.js +0 -119
- package/src/commands/set.js +0 -107
- package/src/commands/tree.js +0 -29
- package/src/lib/cc/CCSceneAsset.js +0 -303
- package/src/lib/cc/index.js +0 -42
- package/src/lib/fire-utils.js +0 -374
- package/src/lib/json-parser.js +0 -185
- package/src/lib/node-utils.js +0 -395
- package/src/lib/screenshot/favicon.ico +0 -0
- package/src/lib/screenshot/index.html +0 -30
- package/src/lib/templates.js +0 -49
- package/src/lib/utils.js +0 -202
|
@@ -1,108 +0,0 @@
|
|
|
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
|
-
const filename = path.basename(result.screenshotPath);
|
|
94
|
-
console.log(JSON.stringify({
|
|
95
|
-
success: true,
|
|
96
|
-
filename: filename
|
|
97
|
-
}));
|
|
98
|
-
} catch (error) {
|
|
99
|
-
console.log(JSON.stringify({
|
|
100
|
-
success: false,
|
|
101
|
-
error: error.message,
|
|
102
|
-
logDir: error.logDir || null
|
|
103
|
-
}));
|
|
104
|
-
process.exit(1);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
module.exports = { run };
|
|
@@ -1,119 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* set 命令 - 设置节点属性
|
|
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
|
-
function run(args) {
|
|
37
|
-
if (args.length < 4) {
|
|
38
|
-
console.log(JSON.stringify({ error: '用法: cocos2d-cli set <场景.fire|预制体.prefab> <节点路径> <属性名> <值>' }));
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const filePath = args[0];
|
|
43
|
-
const nodePath = args[1];
|
|
44
|
-
const prop = args[2];
|
|
45
|
-
const value = args[3];
|
|
46
|
-
|
|
47
|
-
const ext = path.extname(filePath).toLowerCase();
|
|
48
|
-
|
|
49
|
-
try {
|
|
50
|
-
let root;
|
|
51
|
-
let asset;
|
|
52
|
-
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
53
|
-
|
|
54
|
-
if (ext === '.fire') {
|
|
55
|
-
asset = CCSceneAsset.fromJSON(json);
|
|
56
|
-
root = asset._scene;
|
|
57
|
-
} else if (ext === '.prefab') {
|
|
58
|
-
asset = CCPrefab.fromJSON(json);
|
|
59
|
-
root = asset._root;
|
|
60
|
-
} else {
|
|
61
|
-
console.log(JSON.stringify({ error: '不支持的文件类型,仅支持 .fire 和 .prefab' }));
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const node = findNode(root, nodePath);
|
|
66
|
-
|
|
67
|
-
if (!node) {
|
|
68
|
-
console.log(JSON.stringify({ error: `节点不存在: ${nodePath}` }));
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// 调用节点的 setProp 方法
|
|
73
|
-
if (typeof node.setProp !== 'function') {
|
|
74
|
-
console.log(JSON.stringify({ error: '节点不支持 setProp 方法' }));
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
|
|
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;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// 构造属性对象并调用 setProp
|
|
89
|
-
const props = { [prop]: parsedValue };
|
|
90
|
-
node.setProp(props);
|
|
91
|
-
|
|
92
|
-
// 保存
|
|
93
|
-
const data = asset.toJSON();
|
|
94
|
-
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
|
|
95
|
-
|
|
96
|
-
// 输出节点树
|
|
97
|
-
const scriptMap = loadScriptMap(filePath);
|
|
98
|
-
const prefab = isPrefab(data);
|
|
99
|
-
const startIndex = prefab ? 0 : 1;
|
|
100
|
-
console.log(buildTree(data, scriptMap, startIndex).trim());
|
|
101
|
-
|
|
102
|
-
} catch (err) {
|
|
103
|
-
console.log(JSON.stringify({ error: err.message }));
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
module.exports = { run };
|
package/src/commands/tree.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* tree 命令 - 查看节点树(支持场景和预制体)
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const { loadScene, loadScriptMap, isPrefab } = require('../lib/fire-utils');
|
|
6
|
-
const { buildTree } = require('../lib/node-utils');
|
|
7
|
-
|
|
8
|
-
function run(args) {
|
|
9
|
-
const filePath = args[0];
|
|
10
|
-
|
|
11
|
-
if (!filePath) {
|
|
12
|
-
console.log(JSON.stringify({ error: '用法: cocos2d-cli tree <场景.fire | 预制体.prefab>' }));
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
try {
|
|
17
|
-
const data = loadScene(filePath);
|
|
18
|
-
const scriptMap = loadScriptMap(filePath);
|
|
19
|
-
const prefab = isPrefab(data);
|
|
20
|
-
|
|
21
|
-
// Prefab 从索引 0 开始,Scene 从索引 1 开始
|
|
22
|
-
const startIndex = prefab ? 0 : 1;
|
|
23
|
-
console.log(buildTree(data, scriptMap, startIndex).trim());
|
|
24
|
-
} catch (err) {
|
|
25
|
-
console.log(JSON.stringify({ error: err.message }));
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
module.exports = { run };
|
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
const CCObject = require('./CCObject');
|
|
2
|
-
const CCScene = require('./CCScene');
|
|
3
|
-
const CCNode = require('./CCNode');
|
|
4
|
-
const CCCanvas = require('./CCCanvas');
|
|
5
|
-
const CCWidget = require('./CCWidget');
|
|
6
|
-
const CCSprite = require('./CCSprite');
|
|
7
|
-
const CCLabel = require('./CCLabel');
|
|
8
|
-
const CCButton = require('./CCButton');
|
|
9
|
-
const CCCamera = require('./CCCamera');
|
|
10
|
-
const { generateCompressedUUID } = require('../utils');
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Cocos Creator 场景资源类
|
|
14
|
-
* 场景文件的容器,包含 CCScene 根节点
|
|
15
|
-
*/
|
|
16
|
-
class CCSceneAsset extends CCObject {
|
|
17
|
-
constructor() {
|
|
18
|
-
super('');
|
|
19
|
-
this.__type__ = 'cc.SceneAsset';
|
|
20
|
-
|
|
21
|
-
this._native = '';
|
|
22
|
-
this._scene = null; // CCScene 引用
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* 获取场景根节点
|
|
27
|
-
*/
|
|
28
|
-
getScene() {
|
|
29
|
-
return this._scene;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* 通过路径查找节点
|
|
34
|
-
*/
|
|
35
|
-
findNode(path) {
|
|
36
|
-
if (!this._scene) return null;
|
|
37
|
-
return this._scene.findChild(path);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* 添加节点到场景
|
|
42
|
-
*/
|
|
43
|
-
addNode(node) {
|
|
44
|
-
if (!this._scene) return null;
|
|
45
|
-
|
|
46
|
-
node._parent = this._scene;
|
|
47
|
-
if (!this._scene._children) this._scene._children = [];
|
|
48
|
-
this._scene._children.push(node);
|
|
49
|
-
|
|
50
|
-
// 场景中的节点需要 _id
|
|
51
|
-
node._id = generateCompressedUUID();
|
|
52
|
-
|
|
53
|
-
return node;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* 删除节点
|
|
58
|
-
*/
|
|
59
|
-
removeNode(node) {
|
|
60
|
-
if (!node || !node._parent) return false;
|
|
61
|
-
|
|
62
|
-
const idx = node._parent._children.indexOf(node);
|
|
63
|
-
if (idx > -1) {
|
|
64
|
-
node._parent._children.splice(idx, 1);
|
|
65
|
-
return true;
|
|
66
|
-
}
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* 添加组件到节点
|
|
72
|
-
*/
|
|
73
|
-
addComponent(node, component) {
|
|
74
|
-
if (!node._components) node._components = [];
|
|
75
|
-
component.node = node;
|
|
76
|
-
node._components.push(component);
|
|
77
|
-
return component;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* 从 JSON 解析
|
|
82
|
-
*/
|
|
83
|
-
static fromJSON(json) {
|
|
84
|
-
const asset = new CCSceneAsset();
|
|
85
|
-
const objects = [];
|
|
86
|
-
|
|
87
|
-
// 第一遍:创建所有对象
|
|
88
|
-
json.forEach((item, index) => {
|
|
89
|
-
objects[index] = createObject(item);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
// 第二遍:建立引用关系
|
|
93
|
-
json.forEach((item, index) => {
|
|
94
|
-
setupReferences(objects[index], item, objects);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
asset._scene = objects[1];
|
|
98
|
-
return asset;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* 序列化为 JSON
|
|
103
|
-
* 顺序:SceneAsset → Scene → 节点树(节点 → 子节点 → 组件)
|
|
104
|
-
*/
|
|
105
|
-
toJSON() {
|
|
106
|
-
const result = [];
|
|
107
|
-
const indexMap = new Map();
|
|
108
|
-
|
|
109
|
-
// 0: SceneAsset 头
|
|
110
|
-
indexMap.set(this, 0);
|
|
111
|
-
result.push({
|
|
112
|
-
__type__: 'cc.SceneAsset',
|
|
113
|
-
_name: '',
|
|
114
|
-
_objFlags: 0,
|
|
115
|
-
_native: '',
|
|
116
|
-
scene: { __id__: 1 }
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
// 1: Scene
|
|
120
|
-
indexMap.set(this._scene, 1);
|
|
121
|
-
result.push(null); // 占位
|
|
122
|
-
|
|
123
|
-
// 递归处理节点
|
|
124
|
-
const processNode = (node) => {
|
|
125
|
-
if (!node) return;
|
|
126
|
-
|
|
127
|
-
// 添加节点
|
|
128
|
-
indexMap.set(node, result.length);
|
|
129
|
-
result.push(null); // 占位
|
|
130
|
-
|
|
131
|
-
// 递归处理子节点
|
|
132
|
-
if (node._children) {
|
|
133
|
-
node._children.forEach(child => processNode(child));
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// 添加组件
|
|
137
|
-
if (node._components) {
|
|
138
|
-
node._components.forEach(comp => {
|
|
139
|
-
indexMap.set(comp, result.length);
|
|
140
|
-
result.push(componentToJSON(comp, indexMap));
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// 生成节点 JSON
|
|
145
|
-
result[indexMap.get(node)] = {
|
|
146
|
-
__type__: 'cc.Node',
|
|
147
|
-
_name: node._name,
|
|
148
|
-
_objFlags: node._objFlags,
|
|
149
|
-
_parent: node._parent ? { __id__: indexMap.get(node._parent) } : null,
|
|
150
|
-
_children: (node._children || []).map(c => ({ __id__: indexMap.get(c) })),
|
|
151
|
-
_active: node._active,
|
|
152
|
-
_components: (node._components || []).map(c => ({ __id__: indexMap.get(c) })),
|
|
153
|
-
_prefab: null,
|
|
154
|
-
_opacity: node._opacity,
|
|
155
|
-
_color: node._color.toJSON(),
|
|
156
|
-
_contentSize: node._contentSize.toJSON(),
|
|
157
|
-
_anchorPoint: node._anchorPoint.toJSON(),
|
|
158
|
-
_trs: node._trs.toJSON(),
|
|
159
|
-
_eulerAngles: node._eulerAngles.toJSON(),
|
|
160
|
-
_skewX: node._skewX,
|
|
161
|
-
_skewY: node._skewY,
|
|
162
|
-
_is3DNode: node._is3DNode,
|
|
163
|
-
_groupIndex: node._groupIndex,
|
|
164
|
-
groupIndex: node.groupIndex,
|
|
165
|
-
_id: node._id || ''
|
|
166
|
-
};
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
// 处理 Scene 的子节点
|
|
170
|
-
if (this._scene && this._scene._children) {
|
|
171
|
-
this._scene._children.forEach(child => processNode(child));
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// 填充 Scene
|
|
175
|
-
result[1] = {
|
|
176
|
-
__type__: 'cc.Scene',
|
|
177
|
-
_objFlags: this._scene._objFlags,
|
|
178
|
-
_parent: null,
|
|
179
|
-
_children: (this._scene._children || []).map(c => ({ __id__: indexMap.get(c) })),
|
|
180
|
-
_active: this._scene._active,
|
|
181
|
-
_components: [],
|
|
182
|
-
_prefab: null,
|
|
183
|
-
_opacity: this._scene._opacity,
|
|
184
|
-
_color: this._scene._color.toJSON(),
|
|
185
|
-
_contentSize: this._scene._contentSize.toJSON(),
|
|
186
|
-
_anchorPoint: this._scene._anchorPoint.toJSON(),
|
|
187
|
-
_trs: this._scene._trs.toJSON(),
|
|
188
|
-
_is3DNode: this._scene._is3DNode,
|
|
189
|
-
_groupIndex: this._scene._groupIndex,
|
|
190
|
-
groupIndex: this._scene.groupIndex,
|
|
191
|
-
autoReleaseAssets: this._scene.autoReleaseAssets || false,
|
|
192
|
-
_id: this._scene._id || ''
|
|
193
|
-
};
|
|
194
|
-
|
|
195
|
-
return result;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function componentToJSON(comp, indexMap) {
|
|
200
|
-
const json = {
|
|
201
|
-
__type__: comp.__type__,
|
|
202
|
-
_name: comp._name || '',
|
|
203
|
-
_objFlags: comp._objFlags || 0,
|
|
204
|
-
node: { __id__: indexMap.get(comp.node) },
|
|
205
|
-
_enabled: comp._enabled !== false
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
for (const key of Object.keys(comp)) {
|
|
209
|
-
if (['__type__', '_name', '_objFlags', 'node', '_enabled', '_id'].includes(key)) continue;
|
|
210
|
-
const val = comp[key];
|
|
211
|
-
if (val === undefined) continue;
|
|
212
|
-
json[key] = val && typeof val.toJSON === 'function' ? val.toJSON() : val;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
json._id = comp._id || '';
|
|
216
|
-
|
|
217
|
-
return json;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
function createObject(item) {
|
|
221
|
-
const type = item.__type__;
|
|
222
|
-
|
|
223
|
-
if (type === 'cc.SceneAsset') return null; // 跳过,由 fromJSON 处理
|
|
224
|
-
if (type === 'cc.Scene') {
|
|
225
|
-
const scene = new CCScene();
|
|
226
|
-
scene._id = item._id || '';
|
|
227
|
-
if (item._active !== undefined) scene._active = item._active;
|
|
228
|
-
if (item.autoReleaseAssets !== undefined) scene.autoReleaseAssets = item.autoReleaseAssets;
|
|
229
|
-
return scene;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
if (type === 'cc.Node') {
|
|
233
|
-
const node = new CCNode(item._name || 'Node');
|
|
234
|
-
node._id = item._id || '';
|
|
235
|
-
copyNodeProps(node, item);
|
|
236
|
-
return node;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
// 组件 - 创建类实例
|
|
240
|
-
const comp = createComponentInstance(type);
|
|
241
|
-
if (comp) {
|
|
242
|
-
copyComponentProps(comp, item);
|
|
243
|
-
return comp;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// 未知类型,返回普通对象
|
|
247
|
-
const obj = { __type__: type };
|
|
248
|
-
for (const key of Object.keys(item)) {
|
|
249
|
-
obj[key] = item[key];
|
|
250
|
-
}
|
|
251
|
-
return obj;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
function createComponentInstance(type) {
|
|
255
|
-
switch (type) {
|
|
256
|
-
case 'cc.Canvas': return new CCCanvas();
|
|
257
|
-
case 'cc.Widget': return new CCWidget();
|
|
258
|
-
case 'cc.Sprite': return new CCSprite();
|
|
259
|
-
case 'cc.Label': return new CCLabel();
|
|
260
|
-
case 'cc.Button': return new CCButton();
|
|
261
|
-
case 'cc.Camera': return new CCCamera();
|
|
262
|
-
default: return null;
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
function copyComponentProps(comp, item) {
|
|
267
|
-
for (const key of Object.keys(item)) {
|
|
268
|
-
if (['__type__', '_name', '_objFlags', 'node', '_enabled', '_id'].includes(key)) continue;
|
|
269
|
-
comp[key] = item[key];
|
|
270
|
-
}
|
|
271
|
-
if (item._id) comp._id = item._id;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
function copyNodeProps(node, item) {
|
|
275
|
-
if (item._active !== undefined) node._active = item._active;
|
|
276
|
-
if (item._opacity !== undefined) node._opacity = item._opacity;
|
|
277
|
-
if (item._is3DNode !== undefined) node._is3DNode = item._is3DNode;
|
|
278
|
-
if (item._groupIndex !== undefined) node._groupIndex = item._groupIndex;
|
|
279
|
-
if (item.groupIndex !== undefined) node.groupIndex = item.groupIndex;
|
|
280
|
-
if (item._skewX !== undefined) node._skewX = item._skewX;
|
|
281
|
-
if (item._skewY !== undefined) node._skewY = item._skewY;
|
|
282
|
-
|
|
283
|
-
if (item._color) node._color.set(item._color.r, item._color.g, item._color.b, item._color.a);
|
|
284
|
-
if (item._contentSize) node._contentSize.set(item._contentSize.width, item._contentSize.height);
|
|
285
|
-
if (item._anchorPoint) node._anchorPoint.set(item._anchorPoint.x, item._anchorPoint.y);
|
|
286
|
-
if (item._trs?.array) item._trs.array.forEach((v, i) => node._trs.array[i] = v);
|
|
287
|
-
if (item._eulerAngles) node._eulerAngles.set(item._eulerAngles.x, item._eulerAngles.y, item._eulerAngles.z);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
function setupReferences(obj, item, objects) {
|
|
291
|
-
if (!obj) return;
|
|
292
|
-
|
|
293
|
-
if (obj.__type__ === 'cc.Scene' || obj.__type__ === 'cc.Node') {
|
|
294
|
-
if (item._parent) obj._parent = objects[item._parent.__id__];
|
|
295
|
-
if (item._children) obj._children = item._children.map(c => objects[c.__id__]).filter(Boolean);
|
|
296
|
-
if (item._components) {
|
|
297
|
-
obj._components = item._components.map(c => objects[c.__id__]).filter(Boolean);
|
|
298
|
-
obj._components.forEach(c => { if (c) c.node = obj; });
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
module.exports = CCSceneAsset;
|
package/src/lib/cc/index.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
const CCObject = require('./CCObject');
|
|
2
|
-
const CCNode = require('./CCNode');
|
|
3
|
-
const CCScene = require('./CCScene');
|
|
4
|
-
const CCSceneAsset = require('./CCSceneAsset');
|
|
5
|
-
const { CCPrefab, CCPrefabInfo } = require('./CCPrefab');
|
|
6
|
-
const CCColor = require('./CCColor');
|
|
7
|
-
const CCSize = require('./CCSize');
|
|
8
|
-
const CCVec2 = require('./CCVec2');
|
|
9
|
-
const CCVec3 = require('./CCVec3');
|
|
10
|
-
const CCTrs = require('./CCTrs');
|
|
11
|
-
const CCRect = require('./CCRect');
|
|
12
|
-
const CCComponent = require('./CCComponent');
|
|
13
|
-
const CCCanvas = require('./CCCanvas');
|
|
14
|
-
const CCWidget = require('./CCWidget');
|
|
15
|
-
const CCCamera = require('./CCCamera');
|
|
16
|
-
const CCSprite = require('./CCSprite');
|
|
17
|
-
const CCLabel = require('./CCLabel');
|
|
18
|
-
const CCButton = require('./CCButton');
|
|
19
|
-
const CCRichText = require('./CCRichText');
|
|
20
|
-
|
|
21
|
-
module.exports = {
|
|
22
|
-
CCObject,
|
|
23
|
-
CCNode,
|
|
24
|
-
CCScene,
|
|
25
|
-
CCSceneAsset,
|
|
26
|
-
CCPrefab,
|
|
27
|
-
CCPrefabInfo,
|
|
28
|
-
CCColor,
|
|
29
|
-
CCSize,
|
|
30
|
-
CCVec2,
|
|
31
|
-
CCVec3,
|
|
32
|
-
CCTrs,
|
|
33
|
-
CCRect,
|
|
34
|
-
CCComponent,
|
|
35
|
-
CCCanvas,
|
|
36
|
-
CCWidget,
|
|
37
|
-
CCCamera,
|
|
38
|
-
CCSprite,
|
|
39
|
-
CCLabel,
|
|
40
|
-
CCButton,
|
|
41
|
-
CCRichText
|
|
42
|
-
};
|