cocos2d-cli 1.4.0 → 1.5.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.
Files changed (47) hide show
  1. package/README.md +142 -0
  2. package/bin/cocos2d-cli.js +21 -12
  3. package/package.json +1 -1
  4. package/src/commands/add-component.js +77 -85
  5. package/src/commands/add.js +116 -212
  6. package/src/commands/create-scene.js +133 -109
  7. package/src/commands/get.js +113 -18
  8. package/src/commands/prefab-create.js +49 -139
  9. package/src/commands/remove-component.js +111 -0
  10. package/src/commands/remove.js +59 -122
  11. package/src/commands/set.js +121 -36
  12. package/src/commands/tree.js +3 -8
  13. package/src/lib/cc/CCButton.js +122 -0
  14. package/src/lib/cc/CCCamera.js +93 -0
  15. package/src/lib/cc/CCCanvas.js +64 -0
  16. package/src/lib/cc/CCColor.js +32 -0
  17. package/src/lib/cc/CCComponent.js +60 -0
  18. package/src/lib/cc/CCLabel.js +109 -0
  19. package/src/lib/cc/CCNode.js +242 -0
  20. package/src/lib/cc/CCObject.js +23 -0
  21. package/src/lib/cc/CCPrefab.js +242 -0
  22. package/src/lib/cc/CCRect.js +32 -0
  23. package/src/lib/cc/CCScene.js +42 -0
  24. package/src/lib/cc/CCSceneAsset.js +271 -0
  25. package/src/lib/cc/CCSize.js +26 -0
  26. package/src/lib/cc/CCSprite.js +82 -0
  27. package/src/lib/cc/CCTrs.js +74 -0
  28. package/src/lib/cc/CCVec2.js +26 -0
  29. package/src/lib/cc/CCVec3.js +29 -0
  30. package/src/lib/cc/CCWidget.js +98 -0
  31. package/src/lib/cc/index.js +40 -0
  32. package/src/lib/fire-utils.js +75 -1
  33. package/src/lib/json-parser.js +166 -0
  34. package/src/lib/node-utils.js +64 -28
  35. package/src/lib/templates.js +31 -194
  36. package/src/lib/utils.js +63 -0
  37. package/data/prefab-template.json +0 -72
  38. package/data/scene-template.json +0 -241
  39. package/src/lib/components/button.js +0 -137
  40. package/src/lib/components/camera.js +0 -107
  41. package/src/lib/components/canvas.js +0 -89
  42. package/src/lib/components/index.js +0 -157
  43. package/src/lib/components/label.js +0 -120
  44. package/src/lib/components/layout.js +0 -110
  45. package/src/lib/components/particle-system.js +0 -160
  46. package/src/lib/components/sprite.js +0 -98
  47. package/src/lib/components/widget.js +0 -122
@@ -0,0 +1,242 @@
1
+ const CCObject = require('./CCObject');
2
+ const { generateFileId } = require('../fire-utils');
3
+
4
+ /**
5
+ * Cocos Creator 预制体类(只是元数据头,不管理节点树)
6
+ */
7
+ class CCPrefab extends CCObject {
8
+ constructor() {
9
+ super('');
10
+ this.__type__ = 'cc.Prefab';
11
+
12
+ this._native = '';
13
+ this._root = null; // 根节点引用
14
+ this.optimizationPolicy = 0;
15
+ this.asyncLoadAssets = false;
16
+ this.readonly = false;
17
+ }
18
+
19
+ setRoot(node) {
20
+ this._root = node;
21
+ return this;
22
+ }
23
+
24
+ /**
25
+ * 从 JSON 解析
26
+ */
27
+ static fromJSON(json) {
28
+ const prefab = new CCPrefab();
29
+ const objects = [];
30
+
31
+ // 第一遍:创建所有对象
32
+ json.forEach((item, index) => {
33
+ objects[index] = createObject(item);
34
+ });
35
+
36
+ // 第二遍:建立引用关系
37
+ json.forEach((item, index) => {
38
+ setupReferences(objects[index], item, objects);
39
+ });
40
+
41
+ prefab._root = objects[1];
42
+ return prefab;
43
+ }
44
+
45
+ /**
46
+ * 序列化为 JSON
47
+ * 顺序:Prefab头 → 节点树(节点 → 子节点们+PrefabInfo → 组件 → PrefabInfo) → 根PrefabInfo
48
+ */
49
+ toJSON() {
50
+ const result = [];
51
+ const indexMap = new Map();
52
+
53
+ // 0: Prefab 头
54
+ indexMap.set(this, 0);
55
+ result.push({
56
+ __type__: 'cc.Prefab',
57
+ _name: '',
58
+ _objFlags: 0,
59
+ _native: '',
60
+ data: null, // 后面填充
61
+ optimizationPolicy: this.optimizationPolicy,
62
+ asyncLoadAssets: this.asyncLoadAssets,
63
+ readonly: this.readonly
64
+ });
65
+
66
+ // 递归处理节点
67
+ const processNode = (node, isRoot = false) => {
68
+ if (!node) return;
69
+
70
+ // 添加节点
71
+ indexMap.set(node, result.length);
72
+ result.push(null); // 占位
73
+
74
+ // 递归处理子节点(每个子节点处理完会带上它的 PrefabInfo)
75
+ if (node._children) {
76
+ node._children.forEach(child => processNode(child, false));
77
+ }
78
+
79
+ // 添加组件
80
+ if (node._components) {
81
+ node._components.forEach(comp => {
82
+ indexMap.set(comp, result.length);
83
+ result.push(componentToJSON(comp, indexMap));
84
+ });
85
+ }
86
+
87
+ // 生成节点 JSON
88
+ result[indexMap.get(node)] = {
89
+ __type__: 'cc.Node',
90
+ _name: node._name,
91
+ _objFlags: node._objFlags,
92
+ _parent: node._parent ? { __id__: indexMap.get(node._parent) } : null,
93
+ _children: (node._children || []).map(c => ({ __id__: indexMap.get(c) })),
94
+ _active: node._active,
95
+ _components: (node._components || []).map(c => ({ __id__: indexMap.get(c) })),
96
+ _prefab: null, // 后面填充
97
+ _opacity: node._opacity,
98
+ _color: node._color.toJSON(),
99
+ _contentSize: node._contentSize.toJSON(),
100
+ _anchorPoint: node._anchorPoint.toJSON(),
101
+ _trs: node._trs.toJSON(),
102
+ _eulerAngles: node._eulerAngles.toJSON(),
103
+ _skewX: node._skewX,
104
+ _skewY: node._skewY,
105
+ _is3DNode: node._is3DNode,
106
+ _groupIndex: node._groupIndex,
107
+ groupIndex: node.groupIndex,
108
+ _id: node._id || ''
109
+ };
110
+
111
+ // 非根节点:立即添加 PrefabInfo
112
+ if (!isRoot) {
113
+ const infoIdx = result.length;
114
+ result.push({
115
+ __type__: 'cc.PrefabInfo',
116
+ root: { __id__: indexMap.get(this._root) },
117
+ asset: { __id__: 0 },
118
+ fileId: generateFileId(),
119
+ sync: false
120
+ });
121
+ result[indexMap.get(node)]._prefab = { __id__: infoIdx };
122
+ }
123
+ };
124
+
125
+ // 处理根节点
126
+ processNode(this._root, true);
127
+
128
+ // 填充 Prefab 头的 data
129
+ result[0].data = { __id__: indexMap.get(this._root) };
130
+
131
+ // 最后添加根节点的 PrefabInfo
132
+ if (this._root) {
133
+ const infoIdx = result.length;
134
+ result.push({
135
+ __type__: 'cc.PrefabInfo',
136
+ root: { __id__: indexMap.get(this._root) },
137
+ asset: { __id__: 0 },
138
+ fileId: '',
139
+ sync: false
140
+ });
141
+ result[indexMap.get(this._root)]._prefab = { __id__: infoIdx };
142
+ }
143
+
144
+ return result;
145
+ }
146
+ }
147
+
148
+ function componentToJSON(comp, indexMap) {
149
+ const json = {
150
+ __type__: comp.__type__,
151
+ _name: comp._name || '',
152
+ _objFlags: comp._objFlags || 0,
153
+ node: { __id__: indexMap.get(comp.node) },
154
+ _enabled: comp._enabled !== false
155
+ };
156
+
157
+ // 其他属性
158
+ for (const key of Object.keys(comp)) {
159
+ if (['__type__', '_name', '_objFlags', 'node', '_enabled', '_id'].includes(key)) continue;
160
+ const val = comp[key];
161
+ if (val === undefined) continue;
162
+ json[key] = val && typeof val.toJSON === 'function' ? val.toJSON() : val;
163
+ }
164
+
165
+ // _id 放最后
166
+ json._id = comp._id || '';
167
+
168
+ return json;
169
+ }
170
+
171
+ function createObject(item) {
172
+ const type = item.__type__;
173
+
174
+ if (type === 'cc.Prefab') return null; // 跳过,由 CCPrefab.fromJSON 处理
175
+ if (type === 'cc.PrefabInfo') return new CCPrefabInfo();
176
+
177
+ if (type === 'cc.Node') {
178
+ const CCNode = require('./CCNode');
179
+ const node = new CCNode(item._name || 'Node');
180
+ node._id = item._id || '';
181
+ copyNodeProps(node, item);
182
+ return node;
183
+ }
184
+
185
+ // 组件
186
+ const comp = { __type__: type };
187
+ for (const key of Object.keys(item)) {
188
+ if (['__type__', '_name', '_objFlags', 'node', '_enabled', '_id'].includes(key)) continue;
189
+ comp[key] = item[key];
190
+ }
191
+ return comp;
192
+ }
193
+
194
+ function copyNodeProps(node, item) {
195
+ if (item._active !== undefined) node._active = item._active;
196
+ if (item._opacity !== undefined) node._opacity = item._opacity;
197
+ if (item._is3DNode !== undefined) node._is3DNode = item._is3DNode;
198
+ if (item._groupIndex !== undefined) node._groupIndex = item._groupIndex;
199
+ if (item.groupIndex !== undefined) node.groupIndex = item.groupIndex;
200
+ if (item._skewX !== undefined) node._skewX = item._skewX;
201
+ if (item._skewY !== undefined) node._skewY = item._skewY;
202
+
203
+ if (item._color) node._color.set(item._color.r, item._color.g, item._color.b, item._color.a);
204
+ if (item._contentSize) node._contentSize.set(item._contentSize.width, item._contentSize.height);
205
+ if (item._anchorPoint) node._anchorPoint.set(item._anchorPoint.x, item._anchorPoint.y);
206
+ if (item._trs?.array) item._trs.array.forEach((v, i) => node._trs.array[i] = v);
207
+ if (item._eulerAngles) node._eulerAngles.set(item._eulerAngles.x, item._eulerAngles.y, item._eulerAngles.z);
208
+ }
209
+
210
+ function setupReferences(obj, item, objects) {
211
+ if (!obj) return;
212
+
213
+ if (obj.__type__ === 'cc.Node') {
214
+ if (item._parent) obj._parent = objects[item._parent.__id__];
215
+ if (item._children) obj._children = item._children.map(c => objects[c.__id__]).filter(Boolean);
216
+ if (item._components) {
217
+ obj._components = item._components.map(c => objects[c.__id__]).filter(Boolean);
218
+ obj._components.forEach(c => { if (c) c.node = obj; });
219
+ }
220
+ if (item._prefab) obj._prefab = objects[item._prefab.__id__];
221
+ } else if (obj.__type__ === 'cc.PrefabInfo') {
222
+ if (item.root) obj.root = objects[item.root.__id__];
223
+ if (item.asset) obj.asset = objects[item.asset.__id__];
224
+ obj.fileId = item.fileId || '';
225
+ obj.sync = item.sync || false;
226
+ }
227
+ }
228
+
229
+ /**
230
+ * 预制体信息类
231
+ */
232
+ class CCPrefabInfo {
233
+ constructor() {
234
+ this.__type__ = 'cc.PrefabInfo';
235
+ this.root = null;
236
+ this.asset = null;
237
+ this.fileId = '';
238
+ this.sync = false;
239
+ }
240
+ }
241
+
242
+ module.exports = { CCPrefab, CCPrefabInfo };
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Cocos Creator 矩形类
3
+ */
4
+ class CCRect {
5
+ constructor(x = 0, y = 0, width = 0, height = 0) {
6
+ this.__type__ = 'cc.Rect';
7
+ this.x = x;
8
+ this.y = y;
9
+ this.width = width;
10
+ this.height = height;
11
+ }
12
+
13
+ set(x, y, width, height) {
14
+ this.x = x;
15
+ this.y = y;
16
+ this.width = width;
17
+ this.height = height;
18
+ return this;
19
+ }
20
+
21
+ toJSON() {
22
+ return {
23
+ __type__: this.__type__,
24
+ x: this.x,
25
+ y: this.y,
26
+ width: this.width,
27
+ height: this.height
28
+ };
29
+ }
30
+ }
31
+
32
+ module.exports = CCRect;
@@ -0,0 +1,42 @@
1
+ const CCNode = require('./CCNode');
2
+
3
+ /**
4
+ * Cocos Creator 场景类
5
+ * 继承自 CCNode
6
+ */
7
+ class CCScene extends CCNode {
8
+ constructor(name = 'NewScene') {
9
+ super(name);
10
+ this.__type__ = 'cc.Scene';
11
+
12
+ // 场景特有属性
13
+ this._is3DNode = true;
14
+ this._anchorPoint.set(0, 0);
15
+ this.autoReleaseAssets = false;
16
+ }
17
+
18
+ toJSON() {
19
+ // 显式控制属性顺序,Scene 特有顺序
20
+ return {
21
+ __type__: this.__type__,
22
+ _objFlags: this._objFlags,
23
+ _parent: this._parent,
24
+ _children: this._children,
25
+ _active: this._active,
26
+ _components: this._components,
27
+ _prefab: this._prefab,
28
+ _opacity: this._opacity,
29
+ _color: this._color.toJSON(),
30
+ _contentSize: this._contentSize.toJSON(),
31
+ _anchorPoint: this._anchorPoint.toJSON(),
32
+ _trs: this._trs.toJSON(),
33
+ _is3DNode: this._is3DNode,
34
+ _groupIndex: this._groupIndex,
35
+ groupIndex: this.groupIndex,
36
+ autoReleaseAssets: this.autoReleaseAssets,
37
+ _id: this._id
38
+ };
39
+ }
40
+ }
41
+
42
+ module.exports = CCScene;
@@ -0,0 +1,271 @@
1
+ const CCObject = require('./CCObject');
2
+ const CCScene = require('./CCScene');
3
+ const CCNode = require('./CCNode');
4
+ const { generateCompressedUUID } = require('../utils');
5
+
6
+ /**
7
+ * Cocos Creator 场景资源类
8
+ * 场景文件的容器,包含 CCScene 根节点
9
+ */
10
+ class CCSceneAsset extends CCObject {
11
+ constructor() {
12
+ super('');
13
+ this.__type__ = 'cc.SceneAsset';
14
+
15
+ this._native = '';
16
+ this._scene = null; // CCScene 引用
17
+ }
18
+
19
+ /**
20
+ * 获取场景根节点
21
+ */
22
+ getScene() {
23
+ return this._scene;
24
+ }
25
+
26
+ /**
27
+ * 通过路径查找节点
28
+ */
29
+ findNode(path) {
30
+ if (!this._scene) return null;
31
+ return this._scene.findChild(path);
32
+ }
33
+
34
+ /**
35
+ * 添加节点到场景
36
+ */
37
+ addNode(node) {
38
+ if (!this._scene) return null;
39
+
40
+ node._parent = this._scene;
41
+ if (!this._scene._children) this._scene._children = [];
42
+ this._scene._children.push(node);
43
+
44
+ // 场景中的节点需要 _id
45
+ node._id = generateCompressedUUID();
46
+
47
+ return node;
48
+ }
49
+
50
+ /**
51
+ * 删除节点
52
+ */
53
+ removeNode(node) {
54
+ if (!node || !node._parent) return false;
55
+
56
+ const idx = node._parent._children.indexOf(node);
57
+ if (idx > -1) {
58
+ node._parent._children.splice(idx, 1);
59
+ return true;
60
+ }
61
+ return false;
62
+ }
63
+
64
+ /**
65
+ * 添加组件到节点
66
+ */
67
+ addComponent(node, component) {
68
+ if (!node._components) node._components = [];
69
+ component.node = node;
70
+ node._components.push(component);
71
+ return component;
72
+ }
73
+
74
+ /**
75
+ * 从 JSON 解析
76
+ */
77
+ static fromJSON(json) {
78
+ const asset = new CCSceneAsset();
79
+ const objects = [];
80
+
81
+ // 第一遍:创建所有对象
82
+ json.forEach((item, index) => {
83
+ objects[index] = createObject(item);
84
+ });
85
+
86
+ // 第二遍:建立引用关系
87
+ json.forEach((item, index) => {
88
+ setupReferences(objects[index], item, objects);
89
+ });
90
+
91
+ asset._scene = objects[1];
92
+ return asset;
93
+ }
94
+
95
+ /**
96
+ * 序列化为 JSON
97
+ * 顺序:SceneAsset → Scene → 节点树(节点 → 子节点 → 组件)
98
+ */
99
+ toJSON() {
100
+ const result = [];
101
+ const indexMap = new Map();
102
+
103
+ // 0: SceneAsset 头
104
+ indexMap.set(this, 0);
105
+ result.push({
106
+ __type__: 'cc.SceneAsset',
107
+ _name: '',
108
+ _objFlags: 0,
109
+ _native: '',
110
+ scene: { __id__: 1 }
111
+ });
112
+
113
+ // 1: Scene
114
+ indexMap.set(this._scene, 1);
115
+ result.push(null); // 占位
116
+
117
+ // 递归处理节点
118
+ const processNode = (node) => {
119
+ if (!node) return;
120
+
121
+ // 添加节点
122
+ indexMap.set(node, result.length);
123
+ result.push(null); // 占位
124
+
125
+ // 递归处理子节点
126
+ if (node._children) {
127
+ node._children.forEach(child => processNode(child));
128
+ }
129
+
130
+ // 添加组件
131
+ if (node._components) {
132
+ node._components.forEach(comp => {
133
+ indexMap.set(comp, result.length);
134
+ result.push(componentToJSON(comp, indexMap));
135
+ });
136
+ }
137
+
138
+ // 生成节点 JSON
139
+ result[indexMap.get(node)] = {
140
+ __type__: 'cc.Node',
141
+ _name: node._name,
142
+ _objFlags: node._objFlags,
143
+ _parent: node._parent ? { __id__: indexMap.get(node._parent) } : null,
144
+ _children: (node._children || []).map(c => ({ __id__: indexMap.get(c) })),
145
+ _active: node._active,
146
+ _components: (node._components || []).map(c => ({ __id__: indexMap.get(c) })),
147
+ _prefab: null,
148
+ _opacity: node._opacity,
149
+ _color: node._color.toJSON(),
150
+ _contentSize: node._contentSize.toJSON(),
151
+ _anchorPoint: node._anchorPoint.toJSON(),
152
+ _trs: node._trs.toJSON(),
153
+ _eulerAngles: node._eulerAngles.toJSON(),
154
+ _skewX: node._skewX,
155
+ _skewY: node._skewY,
156
+ _is3DNode: node._is3DNode,
157
+ _groupIndex: node._groupIndex,
158
+ groupIndex: node.groupIndex,
159
+ _id: node._id || ''
160
+ };
161
+ };
162
+
163
+ // 处理 Scene 的子节点
164
+ if (this._scene && this._scene._children) {
165
+ this._scene._children.forEach(child => processNode(child));
166
+ }
167
+
168
+ // 填充 Scene
169
+ result[1] = {
170
+ __type__: 'cc.Scene',
171
+ _objFlags: this._scene._objFlags,
172
+ _parent: null,
173
+ _children: (this._scene._children || []).map(c => ({ __id__: indexMap.get(c) })),
174
+ _active: this._scene._active,
175
+ _components: [],
176
+ _prefab: null,
177
+ _opacity: this._scene._opacity,
178
+ _color: this._scene._color.toJSON(),
179
+ _contentSize: this._scene._contentSize.toJSON(),
180
+ _anchorPoint: this._scene._anchorPoint.toJSON(),
181
+ _trs: this._scene._trs.toJSON(),
182
+ _is3DNode: this._scene._is3DNode,
183
+ _groupIndex: this._scene._groupIndex,
184
+ groupIndex: this._scene.groupIndex,
185
+ autoReleaseAssets: this._scene.autoReleaseAssets || false,
186
+ _id: this._scene._id || ''
187
+ };
188
+
189
+ return result;
190
+ }
191
+ }
192
+
193
+ function componentToJSON(comp, indexMap) {
194
+ const json = {
195
+ __type__: comp.__type__,
196
+ _name: comp._name || '',
197
+ _objFlags: comp._objFlags || 0,
198
+ node: { __id__: indexMap.get(comp.node) },
199
+ _enabled: comp._enabled !== false
200
+ };
201
+
202
+ for (const key of Object.keys(comp)) {
203
+ if (['__type__', '_name', '_objFlags', 'node', '_enabled', '_id'].includes(key)) continue;
204
+ const val = comp[key];
205
+ if (val === undefined) continue;
206
+ json[key] = val && typeof val.toJSON === 'function' ? val.toJSON() : val;
207
+ }
208
+
209
+ json._id = comp._id || '';
210
+
211
+ return json;
212
+ }
213
+
214
+ function createObject(item) {
215
+ const type = item.__type__;
216
+
217
+ if (type === 'cc.SceneAsset') return null; // 跳过,由 fromJSON 处理
218
+ if (type === 'cc.Scene') {
219
+ const scene = new CCScene();
220
+ scene._id = item._id || '';
221
+ if (item._active !== undefined) scene._active = item._active;
222
+ if (item.autoReleaseAssets !== undefined) scene.autoReleaseAssets = item.autoReleaseAssets;
223
+ return scene;
224
+ }
225
+
226
+ if (type === 'cc.Node') {
227
+ const node = new CCNode(item._name || 'Node');
228
+ node._id = item._id || '';
229
+ copyNodeProps(node, item);
230
+ return node;
231
+ }
232
+
233
+ // 组件
234
+ const comp = { __type__: type };
235
+ for (const key of Object.keys(item)) {
236
+ if (['__type__', '_name', '_objFlags', 'node', '_enabled', '_id'].includes(key)) continue;
237
+ comp[key] = item[key];
238
+ }
239
+ return comp;
240
+ }
241
+
242
+ function copyNodeProps(node, item) {
243
+ if (item._active !== undefined) node._active = item._active;
244
+ if (item._opacity !== undefined) node._opacity = item._opacity;
245
+ if (item._is3DNode !== undefined) node._is3DNode = item._is3DNode;
246
+ if (item._groupIndex !== undefined) node._groupIndex = item._groupIndex;
247
+ if (item.groupIndex !== undefined) node.groupIndex = item.groupIndex;
248
+ if (item._skewX !== undefined) node._skewX = item._skewX;
249
+ if (item._skewY !== undefined) node._skewY = item._skewY;
250
+
251
+ if (item._color) node._color.set(item._color.r, item._color.g, item._color.b, item._color.a);
252
+ if (item._contentSize) node._contentSize.set(item._contentSize.width, item._contentSize.height);
253
+ if (item._anchorPoint) node._anchorPoint.set(item._anchorPoint.x, item._anchorPoint.y);
254
+ if (item._trs?.array) item._trs.array.forEach((v, i) => node._trs.array[i] = v);
255
+ if (item._eulerAngles) node._eulerAngles.set(item._eulerAngles.x, item._eulerAngles.y, item._eulerAngles.z);
256
+ }
257
+
258
+ function setupReferences(obj, item, objects) {
259
+ if (!obj) return;
260
+
261
+ if (obj.__type__ === 'cc.Scene' || obj.__type__ === 'cc.Node') {
262
+ if (item._parent) obj._parent = objects[item._parent.__id__];
263
+ if (item._children) obj._children = item._children.map(c => objects[c.__id__]).filter(Boolean);
264
+ if (item._components) {
265
+ obj._components = item._components.map(c => objects[c.__id__]).filter(Boolean);
266
+ obj._components.forEach(c => { if (c) c.node = obj; });
267
+ }
268
+ }
269
+ }
270
+
271
+ module.exports = CCSceneAsset;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Cocos Creator 尺寸类
3
+ */
4
+ class CCSize {
5
+ constructor(width = 0, height = 0) {
6
+ this.__type__ = 'cc.Size';
7
+ this.width = width;
8
+ this.height = height;
9
+ }
10
+
11
+ set(width, height) {
12
+ this.width = width;
13
+ this.height = height;
14
+ return this;
15
+ }
16
+
17
+ toJSON() {
18
+ return {
19
+ __type__: this.__type__,
20
+ width: this.width,
21
+ height: this.height
22
+ };
23
+ }
24
+ }
25
+
26
+ module.exports = CCSize;
@@ -0,0 +1,82 @@
1
+ const CCComponent = require('./CCComponent');
2
+ const CCColor = require('./CCColor');
3
+ const CCVec2 = require('./CCVec2');
4
+
5
+ /**
6
+ * Cocos Creator Sprite 组件
7
+ */
8
+ class CCSprite extends CCComponent {
9
+ constructor() {
10
+ super();
11
+ this.__type__ = 'cc.Sprite';
12
+
13
+ this._materials = [{ __uuid__: 'eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432' }];
14
+ this._srcBlendFactor = 770;
15
+ this._dstBlendFactor = 771;
16
+ this._spriteFrame = null;
17
+ this._type = 0;
18
+ this._sizeMode = 1;
19
+ this._fillType = 0;
20
+ this._fillCenter = new CCVec2();
21
+ this._fillStart = 0;
22
+ this._fillRange = 0;
23
+ this._isTrimmedMode = true;
24
+ this._atlas = null;
25
+ }
26
+
27
+ /**
28
+ * 设置精灵帧
29
+ */
30
+ setSpriteFrame(uuid) {
31
+ this._spriteFrame = { __uuid__: uuid };
32
+ return this;
33
+ }
34
+
35
+ /**
36
+ * 设置尺寸模式
37
+ * 0: CUSTOM, 1: RAW, 2: TRIMMED
38
+ */
39
+ setSizeMode(mode) {
40
+ this._sizeMode = mode;
41
+ return this;
42
+ }
43
+
44
+ /**
45
+ * 转换为属性面板显示格式
46
+ */
47
+ toPanelJSON() {
48
+ const SIZE_MODE = ['CUSTOM', 'RAW', 'TRIMMED'];
49
+ const SPRITE_TYPE = ['SIMPLE', 'SLICED', 'TILED', 'FILLED', 'MESH'];
50
+ return {
51
+ ...super.toPanelJSON(),
52
+ sizeMode: SIZE_MODE[this._sizeMode] || this._sizeMode,
53
+ type: SPRITE_TYPE[this._type] || this._type,
54
+ trim: this._isTrimmedMode
55
+ };
56
+ }
57
+
58
+ toJSON() {
59
+ return {
60
+ __type__: this.__type__,
61
+ _name: this._name,
62
+ _objFlags: this._objFlags,
63
+ node: this.node,
64
+ _enabled: this._enabled,
65
+ _materials: this._materials,
66
+ _srcBlendFactor: this._srcBlendFactor,
67
+ _dstBlendFactor: this._dstBlendFactor,
68
+ _spriteFrame: this._spriteFrame,
69
+ _type: this._type,
70
+ _sizeMode: this._sizeMode,
71
+ _fillType: this._fillType,
72
+ _fillCenter: this._fillCenter.toJSON(),
73
+ _fillStart: this._fillStart,
74
+ _fillRange: this._fillRange,
75
+ _isTrimmedMode: this._isTrimmedMode,
76
+ _atlas: this._atlas,
77
+ _id: this._id
78
+ };
79
+ }
80
+ }
81
+
82
+ module.exports = CCSprite;