cocos2d-cli 1.6.4 → 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.
- 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 +26 -33
- package/{src → dist/src}/lib/cc/CCCamera.js +20 -30
- package/{src → dist/src}/lib/cc/CCCanvas.js +10 -15
- package/{src → dist/src}/lib/cc/CCColor.js +6 -8
- package/{src → dist/src}/lib/cc/CCComponent.js +8 -29
- package/{src → dist/src}/lib/cc/CCLabel.js +44 -51
- package/{src → dist/src}/lib/cc/CCNode.js +52 -118
- package/{src → dist/src}/lib/cc/CCObject.js +4 -8
- package/{src → dist/src}/lib/cc/CCPrefab.js +77 -100
- package/{src → dist/src}/lib/cc/CCRect.js +6 -8
- package/{src → dist/src}/lib/cc/CCRichText.js +10 -16
- package/{src → dist/src}/lib/cc/CCScene.js +3 -13
- package/dist/src/lib/cc/CCSceneAsset.js +242 -0
- package/{src → dist/src}/lib/cc/CCSize.js +4 -8
- package/{src → dist/src}/lib/cc/CCSprite.js +21 -33
- package/{src → dist/src}/lib/cc/CCTrs.js +4 -29
- package/{src → dist/src}/lib/cc/CCVec2.js +4 -8
- package/{src → dist/src}/lib/cc/CCVec3.js +5 -8
- package/{src → dist/src}/lib/cc/CCWidget.js +20 -24
- 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 +54 -119
- 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,99 +1,74 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Cocos Creator 预制体类(只是元数据头,不管理节点树)
|
|
6
|
-
*/
|
|
1
|
+
import CCObject from './CCObject.js';
|
|
2
|
+
import { generateFileId } from '../fire-utils.js';
|
|
3
|
+
import CCNode from './CCNode.js';
|
|
7
4
|
class CCPrefab extends CCObject {
|
|
5
|
+
_native;
|
|
6
|
+
_root;
|
|
7
|
+
optimizationPolicy;
|
|
8
|
+
asyncLoadAssets;
|
|
9
|
+
readonly;
|
|
8
10
|
constructor() {
|
|
9
11
|
super('');
|
|
10
12
|
this.__type__ = 'cc.Prefab';
|
|
11
|
-
|
|
12
13
|
this._native = '';
|
|
13
|
-
this._root = null;
|
|
14
|
+
this._root = null;
|
|
14
15
|
this.optimizationPolicy = 0;
|
|
15
16
|
this.asyncLoadAssets = false;
|
|
16
17
|
this.readonly = false;
|
|
17
18
|
}
|
|
18
|
-
|
|
19
19
|
setRoot(node) {
|
|
20
20
|
this._root = node;
|
|
21
21
|
return this;
|
|
22
22
|
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 从 JSON 解析
|
|
26
|
-
*/
|
|
27
23
|
static fromJSON(json) {
|
|
28
24
|
const prefab = new CCPrefab();
|
|
29
25
|
const objects = [];
|
|
30
|
-
|
|
31
|
-
// 第一遍:创建所有对象
|
|
32
26
|
json.forEach((item, index) => {
|
|
33
27
|
objects[index] = createObject(item);
|
|
34
28
|
});
|
|
35
|
-
|
|
36
|
-
// 第二遍:建立引用关系
|
|
37
29
|
json.forEach((item, index) => {
|
|
38
30
|
setupReferences(objects[index], item, objects);
|
|
39
31
|
});
|
|
40
|
-
|
|
41
32
|
prefab._root = objects[1];
|
|
42
33
|
return prefab;
|
|
43
34
|
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* 序列化为 JSON
|
|
47
|
-
* 顺序:Prefab头 → 节点树(节点 → 子节点们+PrefabInfo → 组件 → PrefabInfo) → 根PrefabInfo
|
|
48
|
-
*/
|
|
49
35
|
toJSON() {
|
|
50
36
|
const result = [];
|
|
51
37
|
const indexMap = new Map();
|
|
52
|
-
|
|
53
|
-
// 0: Prefab 头
|
|
54
38
|
indexMap.set(this, 0);
|
|
55
39
|
result.push({
|
|
56
40
|
__type__: 'cc.Prefab',
|
|
57
41
|
_name: '',
|
|
58
42
|
_objFlags: 0,
|
|
59
43
|
_native: '',
|
|
60
|
-
data: null,
|
|
44
|
+
data: null,
|
|
61
45
|
optimizationPolicy: this.optimizationPolicy,
|
|
62
46
|
asyncLoadAssets: this.asyncLoadAssets,
|
|
63
47
|
readonly: this.readonly
|
|
64
48
|
});
|
|
65
|
-
|
|
66
|
-
// 递归处理节点
|
|
67
49
|
const processNode = (node, isRoot = false) => {
|
|
68
|
-
if (!node)
|
|
69
|
-
|
|
70
|
-
// 添加节点
|
|
50
|
+
if (!node)
|
|
51
|
+
return;
|
|
71
52
|
indexMap.set(node, result.length);
|
|
72
|
-
result.push(null);
|
|
73
|
-
|
|
74
|
-
// 递归处理子节点(每个子节点处理完会带上它的 PrefabInfo)
|
|
53
|
+
result.push(null);
|
|
75
54
|
if (node._children) {
|
|
76
55
|
node._children.forEach(child => processNode(child, false));
|
|
77
56
|
}
|
|
78
|
-
|
|
79
|
-
// 添加组件
|
|
80
57
|
if (node._components) {
|
|
81
58
|
node._components.forEach(comp => {
|
|
82
59
|
indexMap.set(comp, result.length);
|
|
83
60
|
result.push(componentToJSON(comp, indexMap));
|
|
84
61
|
});
|
|
85
62
|
}
|
|
86
|
-
|
|
87
|
-
// 生成节点 JSON
|
|
88
63
|
result[indexMap.get(node)] = {
|
|
89
64
|
__type__: 'cc.Node',
|
|
90
65
|
_name: node._name,
|
|
91
66
|
_objFlags: node._objFlags,
|
|
92
67
|
_parent: node._parent ? { __id__: indexMap.get(node._parent) } : null,
|
|
93
|
-
_children:
|
|
68
|
+
_children: node._children?.map(c => ({ __id__: indexMap.get(c) })) || [],
|
|
94
69
|
_active: node._active,
|
|
95
|
-
_components:
|
|
96
|
-
_prefab: null,
|
|
70
|
+
_components: node._components?.map(c => ({ __id__: indexMap.get(c) })) || [],
|
|
71
|
+
_prefab: null,
|
|
97
72
|
_opacity: node._opacity,
|
|
98
73
|
_color: node._color.toJSON(),
|
|
99
74
|
_contentSize: node._contentSize.toJSON(),
|
|
@@ -107,8 +82,6 @@ class CCPrefab extends CCObject {
|
|
|
107
82
|
groupIndex: node.groupIndex,
|
|
108
83
|
_id: node._id || ''
|
|
109
84
|
};
|
|
110
|
-
|
|
111
|
-
// 非根节点:立即添加 PrefabInfo
|
|
112
85
|
if (!isRoot) {
|
|
113
86
|
const infoIdx = result.length;
|
|
114
87
|
result.push({
|
|
@@ -121,15 +94,11 @@ class CCPrefab extends CCObject {
|
|
|
121
94
|
result[indexMap.get(node)]._prefab = { __id__: infoIdx };
|
|
122
95
|
}
|
|
123
96
|
};
|
|
124
|
-
|
|
125
|
-
// 处理根节点
|
|
126
|
-
processNode(this._root, true);
|
|
127
|
-
|
|
128
|
-
// 填充 Prefab 头的 data
|
|
129
|
-
result[0].data = { __id__: indexMap.get(this._root) };
|
|
130
|
-
|
|
131
|
-
// 最后添加根节点的 PrefabInfo
|
|
132
97
|
if (this._root) {
|
|
98
|
+
processNode(this._root, true);
|
|
99
|
+
}
|
|
100
|
+
if (this._root) {
|
|
101
|
+
result[0].data = { __id__: indexMap.get(this._root) };
|
|
133
102
|
const infoIdx = result.length;
|
|
134
103
|
result.push({
|
|
135
104
|
__type__: 'cc.PrefabInfo',
|
|
@@ -140,11 +109,9 @@ class CCPrefab extends CCObject {
|
|
|
140
109
|
});
|
|
141
110
|
result[indexMap.get(this._root)]._prefab = { __id__: infoIdx };
|
|
142
111
|
}
|
|
143
|
-
|
|
144
112
|
return result;
|
|
145
113
|
}
|
|
146
114
|
}
|
|
147
|
-
|
|
148
115
|
function componentToJSON(comp, indexMap) {
|
|
149
116
|
const json = {
|
|
150
117
|
__type__: comp.__type__,
|
|
@@ -153,83 +120,94 @@ function componentToJSON(comp, indexMap) {
|
|
|
153
120
|
node: { __id__: indexMap.get(comp.node) },
|
|
154
121
|
_enabled: comp._enabled !== false
|
|
155
122
|
};
|
|
156
|
-
|
|
157
|
-
// 其他属性
|
|
158
123
|
for (const key of Object.keys(comp)) {
|
|
159
|
-
if (['__type__', '_name', '_objFlags', 'node', '_enabled', '_id'].includes(key))
|
|
124
|
+
if (['__type__', '_name', '_objFlags', 'node', '_enabled', '_id'].includes(key))
|
|
125
|
+
continue;
|
|
160
126
|
const val = comp[key];
|
|
161
|
-
if (val === undefined)
|
|
127
|
+
if (val === undefined)
|
|
128
|
+
continue;
|
|
162
129
|
json[key] = val && typeof val.toJSON === 'function' ? val.toJSON() : val;
|
|
163
130
|
}
|
|
164
|
-
|
|
165
|
-
// _id 放最后
|
|
166
131
|
json._id = comp._id || '';
|
|
167
|
-
|
|
168
132
|
return json;
|
|
169
133
|
}
|
|
170
|
-
|
|
171
134
|
function createObject(item) {
|
|
172
135
|
const type = item.__type__;
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
if (type === 'cc.PrefabInfo')
|
|
176
|
-
|
|
136
|
+
if (type === 'cc.Prefab')
|
|
137
|
+
return null;
|
|
138
|
+
if (type === 'cc.PrefabInfo')
|
|
139
|
+
return new CCPrefabInfo();
|
|
177
140
|
if (type === 'cc.Node') {
|
|
178
|
-
const CCNode = require('./CCNode');
|
|
179
141
|
const node = new CCNode(item._name || 'Node');
|
|
180
142
|
node._id = item._id || '';
|
|
181
143
|
copyNodeProps(node, item);
|
|
182
144
|
return node;
|
|
183
145
|
}
|
|
184
|
-
|
|
185
|
-
// 组件
|
|
186
146
|
const comp = { __type__: type };
|
|
187
147
|
for (const key of Object.keys(item)) {
|
|
188
|
-
if (['__type__', '_name', '_objFlags', 'node', '_enabled', '_id'].includes(key))
|
|
148
|
+
if (['__type__', '_name', '_objFlags', 'node', '_enabled', '_id'].includes(key))
|
|
149
|
+
continue;
|
|
189
150
|
comp[key] = item[key];
|
|
190
151
|
}
|
|
191
152
|
return comp;
|
|
192
153
|
}
|
|
193
|
-
|
|
194
154
|
function copyNodeProps(node, item) {
|
|
195
|
-
if (item._active !== undefined)
|
|
196
|
-
|
|
197
|
-
if (item.
|
|
198
|
-
|
|
199
|
-
if (item.
|
|
200
|
-
|
|
201
|
-
if (item.
|
|
202
|
-
|
|
203
|
-
if (item.
|
|
204
|
-
|
|
205
|
-
if (item.
|
|
206
|
-
|
|
207
|
-
if (item.
|
|
155
|
+
if (item._active !== undefined)
|
|
156
|
+
node._active = item._active;
|
|
157
|
+
if (item._opacity !== undefined)
|
|
158
|
+
node._opacity = item._opacity;
|
|
159
|
+
if (item._is3DNode !== undefined)
|
|
160
|
+
node._is3DNode = item._is3DNode;
|
|
161
|
+
if (item._groupIndex !== undefined)
|
|
162
|
+
node._groupIndex = item._groupIndex;
|
|
163
|
+
if (item.groupIndex !== undefined)
|
|
164
|
+
node.groupIndex = item.groupIndex;
|
|
165
|
+
if (item._skewX !== undefined)
|
|
166
|
+
node._skewX = item._skewX;
|
|
167
|
+
if (item._skewY !== undefined)
|
|
168
|
+
node._skewY = item._skewY;
|
|
169
|
+
if (item._color)
|
|
170
|
+
node._color.set(item._color.r, item._color.g, item._color.b, item._color.a);
|
|
171
|
+
if (item._contentSize)
|
|
172
|
+
node._contentSize.set(item._contentSize.width, item._contentSize.height);
|
|
173
|
+
if (item._anchorPoint)
|
|
174
|
+
node._anchorPoint.set(item._anchorPoint.x, item._anchorPoint.y);
|
|
175
|
+
if (item._trs?.array)
|
|
176
|
+
item._trs.array.forEach((v, i) => node._trs.array[i] = v);
|
|
177
|
+
if (item._eulerAngles)
|
|
178
|
+
node._eulerAngles.set(item._eulerAngles.x, item._eulerAngles.y, item._eulerAngles.z);
|
|
208
179
|
}
|
|
209
|
-
|
|
210
180
|
function setupReferences(obj, item, objects) {
|
|
211
|
-
if (!obj)
|
|
212
|
-
|
|
181
|
+
if (!obj)
|
|
182
|
+
return;
|
|
213
183
|
if (obj.__type__ === 'cc.Node') {
|
|
214
|
-
if (item._parent)
|
|
215
|
-
|
|
184
|
+
if (item._parent)
|
|
185
|
+
obj._parent = objects[item._parent.__id__];
|
|
186
|
+
if (item._children)
|
|
187
|
+
obj._children = item._children.map((c) => objects[c.__id__]).filter(Boolean);
|
|
216
188
|
if (item._components) {
|
|
217
|
-
obj._components = item._components.map(c => objects[c.__id__]).filter(Boolean);
|
|
218
|
-
obj._components.forEach(c => { if (c)
|
|
189
|
+
obj._components = item._components.map((c) => objects[c.__id__]).filter(Boolean);
|
|
190
|
+
obj._components.forEach((c) => { if (c)
|
|
191
|
+
c.node = obj; });
|
|
219
192
|
}
|
|
220
|
-
if (item._prefab)
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
193
|
+
if (item._prefab)
|
|
194
|
+
obj._prefab = objects[item._prefab.__id__];
|
|
195
|
+
}
|
|
196
|
+
else if (obj.__type__ === 'cc.PrefabInfo') {
|
|
197
|
+
if (item.root)
|
|
198
|
+
obj.root = objects[item.root.__id__];
|
|
199
|
+
if (item.asset)
|
|
200
|
+
obj.asset = objects[item.asset.__id__];
|
|
224
201
|
obj.fileId = item.fileId || '';
|
|
225
202
|
obj.sync = item.sync || false;
|
|
226
203
|
}
|
|
227
204
|
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* 预制体信息类
|
|
231
|
-
*/
|
|
232
205
|
class CCPrefabInfo {
|
|
206
|
+
__type__;
|
|
207
|
+
root;
|
|
208
|
+
asset;
|
|
209
|
+
fileId;
|
|
210
|
+
sync;
|
|
233
211
|
constructor() {
|
|
234
212
|
this.__type__ = 'cc.PrefabInfo';
|
|
235
213
|
this.root = null;
|
|
@@ -238,5 +216,4 @@ class CCPrefabInfo {
|
|
|
238
216
|
this.sync = false;
|
|
239
217
|
}
|
|
240
218
|
}
|
|
241
|
-
|
|
242
|
-
module.exports = { CCPrefab, CCPrefabInfo };
|
|
219
|
+
export { CCPrefab, CCPrefabInfo };
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
export default class CCRect {
|
|
2
|
+
__type__;
|
|
3
|
+
x;
|
|
4
|
+
y;
|
|
5
|
+
width;
|
|
6
|
+
height;
|
|
5
7
|
constructor(x = 0, y = 0, width = 0, height = 0) {
|
|
6
8
|
this.__type__ = 'cc.Rect';
|
|
7
9
|
this.x = x;
|
|
@@ -9,7 +11,6 @@ class CCRect {
|
|
|
9
11
|
this.width = width;
|
|
10
12
|
this.height = height;
|
|
11
13
|
}
|
|
12
|
-
|
|
13
14
|
set(x, y, width, height) {
|
|
14
15
|
this.x = x;
|
|
15
16
|
this.y = y;
|
|
@@ -17,7 +18,6 @@ class CCRect {
|
|
|
17
18
|
this.height = height;
|
|
18
19
|
return this;
|
|
19
20
|
}
|
|
20
|
-
|
|
21
21
|
toJSON() {
|
|
22
22
|
return {
|
|
23
23
|
__type__: this.__type__,
|
|
@@ -28,5 +28,3 @@ class CCRect {
|
|
|
28
28
|
};
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
-
|
|
32
|
-
module.exports = CCRect;
|
|
@@ -1,27 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*/
|
|
11
|
-
class CCRichText extends CCComponent {
|
|
1
|
+
import CCComponent from './CCComponent.js';
|
|
2
|
+
export default class CCRichText extends CCComponent {
|
|
3
|
+
_string;
|
|
4
|
+
_horizontalAlign;
|
|
5
|
+
_fontSize;
|
|
6
|
+
_maxWidth;
|
|
7
|
+
_lineHeight;
|
|
8
|
+
_imageAtlas;
|
|
9
|
+
_handleTouchEvent;
|
|
12
10
|
constructor() {
|
|
13
11
|
super();
|
|
14
12
|
this.__type__ = 'cc.RichText';
|
|
15
|
-
|
|
16
13
|
this._string = '';
|
|
17
|
-
this._horizontalAlign = 0;
|
|
14
|
+
this._horizontalAlign = 0;
|
|
18
15
|
this._fontSize = 40;
|
|
19
16
|
this._maxWidth = 0;
|
|
20
17
|
this._lineHeight = 40;
|
|
21
18
|
this._imageAtlas = null;
|
|
22
19
|
this._handleTouchEvent = true;
|
|
23
20
|
}
|
|
24
|
-
|
|
25
21
|
toJSON() {
|
|
26
22
|
return {
|
|
27
23
|
__type__: this.__type__,
|
|
@@ -40,5 +36,3 @@ class CCRichText extends CCComponent {
|
|
|
40
36
|
};
|
|
41
37
|
}
|
|
42
38
|
}
|
|
43
|
-
|
|
44
|
-
module.exports = CCRichText;
|
|
@@ -1,22 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* Cocos Creator 场景类
|
|
5
|
-
* 继承自 CCNode
|
|
6
|
-
*/
|
|
7
|
-
class CCScene extends CCNode {
|
|
1
|
+
import CCNode from './CCNode.js';
|
|
2
|
+
export default class CCScene extends CCNode {
|
|
3
|
+
autoReleaseAssets;
|
|
8
4
|
constructor(name = 'NewScene') {
|
|
9
5
|
super(name);
|
|
10
6
|
this.__type__ = 'cc.Scene';
|
|
11
|
-
|
|
12
|
-
// 场景特有属性
|
|
13
7
|
this._is3DNode = true;
|
|
14
8
|
this._anchorPoint.set(0, 0);
|
|
15
9
|
this.autoReleaseAssets = false;
|
|
16
10
|
}
|
|
17
|
-
|
|
18
11
|
toJSON() {
|
|
19
|
-
// 显式控制属性顺序,Scene 特有顺序
|
|
20
12
|
return {
|
|
21
13
|
__type__: this.__type__,
|
|
22
14
|
_objFlags: this._objFlags,
|
|
@@ -38,5 +30,3 @@ class CCScene extends CCNode {
|
|
|
38
30
|
};
|
|
39
31
|
}
|
|
40
32
|
}
|
|
41
|
-
|
|
42
|
-
module.exports = CCScene;
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import CCObject from './CCObject.js';
|
|
2
|
+
import CCScene from './CCScene.js';
|
|
3
|
+
import CCNode from './CCNode.js';
|
|
4
|
+
import CCCanvas from './CCCanvas.js';
|
|
5
|
+
import CCWidget from './CCWidget.js';
|
|
6
|
+
import CCSprite from './CCSprite.js';
|
|
7
|
+
import CCLabel from './CCLabel.js';
|
|
8
|
+
import CCButton from './CCButton.js';
|
|
9
|
+
import CCCamera from './CCCamera.js';
|
|
10
|
+
import { generateCompressedUUID } from '../utils.js';
|
|
11
|
+
export default class CCSceneAsset extends CCObject {
|
|
12
|
+
_native;
|
|
13
|
+
_scene;
|
|
14
|
+
constructor() {
|
|
15
|
+
super('');
|
|
16
|
+
this.__type__ = 'cc.SceneAsset';
|
|
17
|
+
this._native = '';
|
|
18
|
+
this._scene = null;
|
|
19
|
+
}
|
|
20
|
+
getScene() {
|
|
21
|
+
return this._scene;
|
|
22
|
+
}
|
|
23
|
+
addNode(node) {
|
|
24
|
+
if (!this._scene)
|
|
25
|
+
return null;
|
|
26
|
+
node._parent = { __id__: 1 };
|
|
27
|
+
if (!this._scene._children)
|
|
28
|
+
this._scene._children = [];
|
|
29
|
+
this._scene._children.push({ __id__: 0 });
|
|
30
|
+
node._id = generateCompressedUUID();
|
|
31
|
+
return node;
|
|
32
|
+
}
|
|
33
|
+
removeNode(node) {
|
|
34
|
+
if (!node._parent)
|
|
35
|
+
return false;
|
|
36
|
+
const idx = -1;
|
|
37
|
+
if (idx > -1) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
static fromJSON(json) {
|
|
43
|
+
const asset = new CCSceneAsset();
|
|
44
|
+
const objects = [];
|
|
45
|
+
json.forEach((item, index) => {
|
|
46
|
+
objects[index] = createObject(item);
|
|
47
|
+
});
|
|
48
|
+
json.forEach((item, index) => {
|
|
49
|
+
setupReferences(objects[index], item, objects);
|
|
50
|
+
});
|
|
51
|
+
asset._scene = objects[1];
|
|
52
|
+
return asset;
|
|
53
|
+
}
|
|
54
|
+
toJSON() {
|
|
55
|
+
const result = [];
|
|
56
|
+
const indexMap = new Map();
|
|
57
|
+
indexMap.set(this, 0);
|
|
58
|
+
result.push({
|
|
59
|
+
__type__: 'cc.SceneAsset',
|
|
60
|
+
_name: '',
|
|
61
|
+
_objFlags: 0,
|
|
62
|
+
_native: '',
|
|
63
|
+
scene: { __id__: 1 }
|
|
64
|
+
});
|
|
65
|
+
if (this._scene) {
|
|
66
|
+
indexMap.set(this._scene, 1);
|
|
67
|
+
}
|
|
68
|
+
result.push(null);
|
|
69
|
+
const processNode = (node) => {
|
|
70
|
+
if (!node)
|
|
71
|
+
return;
|
|
72
|
+
indexMap.set(node, result.length);
|
|
73
|
+
result.push(null);
|
|
74
|
+
if (node._children) {
|
|
75
|
+
node._children.forEach(child => processNode(child));
|
|
76
|
+
}
|
|
77
|
+
if (node._components) {
|
|
78
|
+
node._components.forEach(comp => {
|
|
79
|
+
indexMap.set(comp, result.length);
|
|
80
|
+
result.push(componentToJSON(comp, indexMap));
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
result[indexMap.get(node)] = {
|
|
84
|
+
__type__: 'cc.Node',
|
|
85
|
+
_name: node._name,
|
|
86
|
+
_objFlags: node._objFlags,
|
|
87
|
+
_parent: node._parent ? { __id__: indexMap.get(node._parent) } : null,
|
|
88
|
+
_children: node._children?.map(c => ({ __id__: indexMap.get(c) })) || [],
|
|
89
|
+
_active: node._active,
|
|
90
|
+
_components: node._components?.map(c => ({ __id__: indexMap.get(c) })) || [],
|
|
91
|
+
_prefab: null,
|
|
92
|
+
_opacity: node._opacity,
|
|
93
|
+
_color: node._color.toJSON(),
|
|
94
|
+
_contentSize: node._contentSize.toJSON(),
|
|
95
|
+
_anchorPoint: node._anchorPoint.toJSON(),
|
|
96
|
+
_trs: node._trs.toJSON(),
|
|
97
|
+
_eulerAngles: node._eulerAngles.toJSON(),
|
|
98
|
+
_skewX: node._skewX,
|
|
99
|
+
_skewY: node._skewY,
|
|
100
|
+
_is3DNode: node._is3DNode,
|
|
101
|
+
_groupIndex: node._groupIndex,
|
|
102
|
+
groupIndex: node.groupIndex,
|
|
103
|
+
_id: node._id || ''
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
if (this._scene?._children) {
|
|
107
|
+
this._scene._children.forEach(child => processNode(child));
|
|
108
|
+
}
|
|
109
|
+
if (this._scene) {
|
|
110
|
+
result[1] = {
|
|
111
|
+
__type__: 'cc.Scene',
|
|
112
|
+
_objFlags: this._scene._objFlags,
|
|
113
|
+
_parent: null,
|
|
114
|
+
_children: this._scene._children?.map(c => ({ __id__: indexMap.get(c) })) || [],
|
|
115
|
+
_active: this._scene._active,
|
|
116
|
+
_components: [],
|
|
117
|
+
_prefab: null,
|
|
118
|
+
_opacity: this._scene._opacity,
|
|
119
|
+
_color: this._scene._color.toJSON(),
|
|
120
|
+
_contentSize: this._scene._contentSize.toJSON(),
|
|
121
|
+
_anchorPoint: this._scene._anchorPoint.toJSON(),
|
|
122
|
+
_trs: this._scene._trs.toJSON(),
|
|
123
|
+
_is3DNode: this._scene._is3DNode,
|
|
124
|
+
_groupIndex: this._scene._groupIndex,
|
|
125
|
+
groupIndex: this._scene.groupIndex,
|
|
126
|
+
autoReleaseAssets: this._scene.autoReleaseAssets || false,
|
|
127
|
+
_id: this._scene._id || ''
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function componentToJSON(comp, indexMap) {
|
|
134
|
+
const json = {
|
|
135
|
+
__type__: comp.__type__,
|
|
136
|
+
_name: comp._name || '',
|
|
137
|
+
_objFlags: comp._objFlags || 0,
|
|
138
|
+
node: { __id__: indexMap.get(comp.node) },
|
|
139
|
+
_enabled: comp._enabled !== false
|
|
140
|
+
};
|
|
141
|
+
for (const key of Object.keys(comp)) {
|
|
142
|
+
if (['__type__', '_name', '_objFlags', 'node', '_enabled', '_id'].includes(key))
|
|
143
|
+
continue;
|
|
144
|
+
const val = comp[key];
|
|
145
|
+
if (val === undefined)
|
|
146
|
+
continue;
|
|
147
|
+
json[key] = val && typeof val.toJSON === 'function' ? val.toJSON() : val;
|
|
148
|
+
}
|
|
149
|
+
json._id = comp._id || '';
|
|
150
|
+
return json;
|
|
151
|
+
}
|
|
152
|
+
function createObject(item) {
|
|
153
|
+
const type = item.__type__;
|
|
154
|
+
if (type === 'cc.SceneAsset')
|
|
155
|
+
return null;
|
|
156
|
+
if (type === 'cc.Scene') {
|
|
157
|
+
const scene = new CCScene();
|
|
158
|
+
scene._id = item._id || '';
|
|
159
|
+
if (item._active !== undefined)
|
|
160
|
+
scene._active = item._active;
|
|
161
|
+
if (item.autoReleaseAssets !== undefined)
|
|
162
|
+
scene.autoReleaseAssets = item.autoReleaseAssets;
|
|
163
|
+
return scene;
|
|
164
|
+
}
|
|
165
|
+
if (type === 'cc.Node') {
|
|
166
|
+
const node = new CCNode(item._name || 'Node');
|
|
167
|
+
node._id = item._id || '';
|
|
168
|
+
copyNodeProps(node, item);
|
|
169
|
+
return node;
|
|
170
|
+
}
|
|
171
|
+
const comp = createComponentInstance(type);
|
|
172
|
+
if (comp) {
|
|
173
|
+
copyComponentProps(comp, item);
|
|
174
|
+
return comp;
|
|
175
|
+
}
|
|
176
|
+
const obj = { __type__: type };
|
|
177
|
+
for (const key of Object.keys(item)) {
|
|
178
|
+
obj[key] = item[key];
|
|
179
|
+
}
|
|
180
|
+
return obj;
|
|
181
|
+
}
|
|
182
|
+
function createComponentInstance(type) {
|
|
183
|
+
switch (type) {
|
|
184
|
+
case 'cc.Canvas': return new CCCanvas();
|
|
185
|
+
case 'cc.Widget': return new CCWidget();
|
|
186
|
+
case 'cc.Sprite': return new CCSprite();
|
|
187
|
+
case 'cc.Label': return new CCLabel();
|
|
188
|
+
case 'cc.Button': return new CCButton();
|
|
189
|
+
case 'cc.Camera': return new CCCamera();
|
|
190
|
+
default: return null;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
function copyComponentProps(comp, item) {
|
|
194
|
+
for (const key of Object.keys(item)) {
|
|
195
|
+
if (['__type__', '_name', '_objFlags', 'node', '_enabled', '_id'].includes(key))
|
|
196
|
+
continue;
|
|
197
|
+
comp[key] = item[key];
|
|
198
|
+
}
|
|
199
|
+
if (item._id)
|
|
200
|
+
comp._id = item._id;
|
|
201
|
+
}
|
|
202
|
+
function copyNodeProps(node, item) {
|
|
203
|
+
if (item._active !== undefined)
|
|
204
|
+
node._active = item._active;
|
|
205
|
+
if (item._opacity !== undefined)
|
|
206
|
+
node._opacity = item._opacity;
|
|
207
|
+
if (item._is3DNode !== undefined)
|
|
208
|
+
node._is3DNode = item._is3DNode;
|
|
209
|
+
if (item._groupIndex !== undefined)
|
|
210
|
+
node._groupIndex = item._groupIndex;
|
|
211
|
+
if (item.groupIndex !== undefined)
|
|
212
|
+
node.groupIndex = item.groupIndex;
|
|
213
|
+
if (item._skewX !== undefined)
|
|
214
|
+
node._skewX = item._skewX;
|
|
215
|
+
if (item._skewY !== undefined)
|
|
216
|
+
node._skewY = item._skewY;
|
|
217
|
+
if (item._color)
|
|
218
|
+
node._color.set(item._color.r, item._color.g, item._color.b, item._color.a);
|
|
219
|
+
if (item._contentSize)
|
|
220
|
+
node._contentSize.set(item._contentSize.width, item._contentSize.height);
|
|
221
|
+
if (item._anchorPoint)
|
|
222
|
+
node._anchorPoint.set(item._anchorPoint.x, item._anchorPoint.y);
|
|
223
|
+
if (item._trs?.array)
|
|
224
|
+
item._trs.array.forEach((v, i) => node._trs.array[i] = v);
|
|
225
|
+
if (item._eulerAngles)
|
|
226
|
+
node._eulerAngles.set(item._eulerAngles.x, item._eulerAngles.y, item._eulerAngles.z);
|
|
227
|
+
}
|
|
228
|
+
function setupReferences(obj, item, objects) {
|
|
229
|
+
if (!obj)
|
|
230
|
+
return;
|
|
231
|
+
if (obj.__type__ === 'cc.Scene' || obj.__type__ === 'cc.Node') {
|
|
232
|
+
if (item._parent)
|
|
233
|
+
obj._parent = objects[item._parent.__id__];
|
|
234
|
+
if (item._children)
|
|
235
|
+
obj._children = item._children.map((c) => objects[c.__id__]).filter(Boolean);
|
|
236
|
+
if (item._components) {
|
|
237
|
+
obj._components = item._components.map((c) => objects[c.__id__]).filter(Boolean);
|
|
238
|
+
obj._components.forEach((c) => { if (c)
|
|
239
|
+
c.node = obj; });
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
@@ -1,19 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
export default class CCSize {
|
|
2
|
+
__type__;
|
|
3
|
+
width;
|
|
4
|
+
height;
|
|
5
5
|
constructor(width = 0, height = 0) {
|
|
6
6
|
this.__type__ = 'cc.Size';
|
|
7
7
|
this.width = width;
|
|
8
8
|
this.height = height;
|
|
9
9
|
}
|
|
10
|
-
|
|
11
10
|
set(width, height) {
|
|
12
11
|
this.width = width;
|
|
13
12
|
this.height = height;
|
|
14
13
|
return this;
|
|
15
14
|
}
|
|
16
|
-
|
|
17
15
|
toJSON() {
|
|
18
16
|
return {
|
|
19
17
|
__type__: this.__type__,
|
|
@@ -22,5 +20,3 @@ class CCSize {
|
|
|
22
20
|
};
|
|
23
21
|
}
|
|
24
22
|
}
|
|
25
|
-
|
|
26
|
-
module.exports = CCSize;
|