cocos2d-cli 1.5.1 → 1.6.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.
@@ -8,13 +8,13 @@ class CCComponent extends CCObject {
8
8
  constructor() {
9
9
  super('');
10
10
  this.__type__ = 'cc.Component';
11
-
11
+
12
12
  // 关联的节点
13
13
  this.node = null;
14
-
14
+
15
15
  // 启用状态
16
16
  this._enabled = true;
17
-
17
+
18
18
  // 唯一标识(22位压缩格式)
19
19
  this._id = generateId();
20
20
  }
@@ -28,21 +28,21 @@ class CCComponent extends CCObject {
28
28
  }
29
29
 
30
30
  /**
31
- * 设置启用状态
31
+ * 获取属性(子类重写)
32
32
  */
33
- setEnabled(enabled) {
34
- this._enabled = enabled;
35
- return this;
33
+ getProp() {
34
+ return {
35
+ class: this.__type__,
36
+ enabled: this._enabled
37
+ };
36
38
  }
37
39
 
38
40
  /**
39
- * 转换为属性面板显示格式(子类重写)
41
+ * 设置属性(子类重写)
40
42
  */
41
- toPanelJSON() {
42
- return {
43
- type: this.__type__,
44
- enabled: this._enabled
45
- };
43
+ setProp(props) {
44
+ if (props.enabled !== undefined) this._enabled = props.enabled;
45
+ return this;
46
46
  }
47
47
 
48
48
  toJSON() {
@@ -56,14 +56,13 @@ class CCLabel extends CCComponent {
56
56
  }
57
57
 
58
58
  /**
59
- * 转换为属性面板显示格式
59
+ * 获取属性
60
60
  */
61
- toPanelJSON() {
61
+ getProp() {
62
62
  const H_ALIGN = ['LEFT', 'CENTER', 'RIGHT'];
63
63
  const V_ALIGN = ['TOP', 'CENTER', 'BOTTOM'];
64
64
  const OVERFLOW = ['NONE', 'CLAMP', 'SHRINK', 'RESIZE_HEIGHT'];
65
65
  return {
66
- ...super.toPanelJSON(),
67
66
  string: this._string,
68
67
  fontSize: this._fontSize,
69
68
  lineHeight: this._lineHeight,
@@ -75,6 +74,19 @@ class CCLabel extends CCComponent {
75
74
  };
76
75
  }
77
76
 
77
+ /**
78
+ * 设置属性
79
+ */
80
+ setProp(props) {
81
+ if (props.string !== undefined) this.setString(props.string);
82
+ if (props.fontSize !== undefined) this.setFontSize(props.fontSize);
83
+ if (props.lineHeight !== undefined) this._lineHeight = props.lineHeight;
84
+ if (props.fontFamily !== undefined) this.setFontFamily(props.fontFamily);
85
+ if (props.horizontalAlign !== undefined) this._N$horizontalAlign = props.horizontalAlign;
86
+ if (props.verticalAlign !== undefined) this._N$verticalAlign = props.verticalAlign;
87
+ return this;
88
+ }
89
+
78
90
  toJSON() {
79
91
  return {
80
92
  __type__: this.__type__,
@@ -12,37 +12,37 @@ class CCNode extends CCObject {
12
12
  constructor(name = 'Node') {
13
13
  super(name);
14
14
  this.__type__ = 'cc.Node';
15
-
15
+
16
16
  // 父子关系
17
17
  this._parent = null;
18
18
  this._children = [];
19
-
19
+
20
20
  // 激活状态
21
21
  this._active = true;
22
-
22
+
23
23
  // 组件列表
24
24
  this._components = [];
25
-
25
+
26
26
  // 预制体信息
27
27
  this._prefab = null;
28
-
28
+
29
29
  // 显示属性
30
30
  this._opacity = 255;
31
31
  this._color = new CCColor();
32
32
  this._contentSize = new CCSize();
33
33
  this._anchorPoint = new CCVec2(0.5, 0.5);
34
-
34
+
35
35
  // 变换属性
36
36
  this._trs = new CCTrs();
37
37
  this._eulerAngles = new CCVec3();
38
38
  this._skewX = 0;
39
39
  this._skewY = 0;
40
40
  this._is3DNode = false;
41
-
41
+
42
42
  // 分组
43
43
  this._groupIndex = 0;
44
44
  this.groupIndex = 0;
45
-
45
+
46
46
  // 唯一标识(预制体中为空,场景中生成)
47
47
  this._id = '';
48
48
  }
@@ -176,36 +176,50 @@ class CCNode extends CCObject {
176
176
  }
177
177
 
178
178
  /**
179
- * 转换为属性面板显示格式(人类可读)
179
+ * 获取属性
180
180
  */
181
- toPanelJSON() {
181
+ getProp() {
182
182
  const trs = this._trs?.array || [0, 0, 0, 0, 0, 0, 1, 1, 1, 1];
183
183
  const result = {
184
184
  name: this._name,
185
185
  active: this._active,
186
- position: { x: trs[0], y: trs[1] },
186
+ x: trs[0],
187
+ y: trs[1],
188
+ width: this._contentSize?.width ?? 0,
189
+ height: this._contentSize?.height ?? 0,
190
+ anchorX: this._anchorPoint?.x ?? 0.5,
191
+ anchorY: this._anchorPoint?.y ?? 0.5,
192
+ scaleX: trs[7],
193
+ scaleY: trs[8],
187
194
  rotation: this._eulerAngles?.z ?? 0,
188
- scale: { x: trs[7], y: trs[8] },
189
- anchor: { x: this._anchorPoint?.x ?? 0.5, y: this._anchorPoint?.y ?? 0.5 },
190
- size: { w: this._contentSize?.width ?? 0, h: this._contentSize?.height ?? 0 },
191
- color: this._color ? `#${this._color.r.toString(16).padStart(2,'0')}${this._color.g.toString(16).padStart(2,'0')}${this._color.b.toString(16).padStart(2,'0')}` : '#ffffff',
192
195
  opacity: this._opacity ?? 255,
193
- skew: { x: this._skewX ?? 0, y: this._skewY ?? 0 },
194
- group: this._groupIndex ?? 0
196
+ color: this._color ? `#${this._color.r.toString(16).padStart(2, '0')}${this._color.g.toString(16).padStart(2, '0')}${this._color.b.toString(16).padStart(2, '0')}` : '#ffffff'
195
197
  };
196
-
197
- // 组件列表
198
- if (this._components && this._components.length > 0) {
199
- result.components = {};
200
- this._components.forEach(c => {
201
- const typeName = c.__type__.replace('cc.', '');
202
- result.components[typeName] = c.toPanelJSON ? c.toPanelJSON() : {};
203
- });
204
- }
205
-
206
198
  return result;
207
199
  }
208
200
 
201
+ /**
202
+ * 设置属性
203
+ */
204
+ setProp(props) {
205
+ if (props.name !== undefined) this._name = props.name;
206
+ if (props.active !== undefined) this._active = props.active;
207
+ if (props.x !== undefined) this._trs.array[0] = props.x;
208
+ if (props.y !== undefined) this._trs.array[1] = props.y;
209
+ if (props.width !== undefined) this._contentSize.width = props.width;
210
+ if (props.height !== undefined) this._contentSize.height = props.height;
211
+ if (props.anchorX !== undefined) this._anchorPoint.x = props.anchorX;
212
+ if (props.anchorY !== undefined) this._anchorPoint.y = props.anchorY;
213
+ if (props.scaleX !== undefined) this._trs.array[7] = props.scaleX;
214
+ if (props.scaleY !== undefined) this._trs.array[8] = props.scaleY;
215
+ if (props.rotation !== undefined) {
216
+ this._trs.array[5] = props.rotation * Math.PI / 180;
217
+ this._eulerAngles.z = props.rotation;
218
+ }
219
+ if (props.opacity !== undefined) this._opacity = props.opacity;
220
+ return this;
221
+ }
222
+
209
223
  toJSON(indexMap) {
210
224
  // 处理引用
211
225
  const parent = this._parent ? (indexMap ? { __id__: indexMap.get(this._parent) } : this._parent) : null;
@@ -1,6 +1,12 @@
1
1
  const CCObject = require('./CCObject');
2
2
  const CCScene = require('./CCScene');
3
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');
4
10
  const { generateCompressedUUID } = require('../utils');
5
11
 
6
12
  /**
@@ -230,13 +236,39 @@ function createObject(item) {
230
236
  return node;
231
237
  }
232
238
 
233
- // 组件
234
- const comp = { __type__: type };
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) {
235
267
  for (const key of Object.keys(item)) {
236
268
  if (['__type__', '_name', '_objFlags', 'node', '_enabled', '_id'].includes(key)) continue;
237
269
  comp[key] = item[key];
238
270
  }
239
- return comp;
271
+ if (item._id) comp._id = item._id;
240
272
  }
241
273
 
242
274
  function copyNodeProps(node, item) {
@@ -1,7 +1,8 @@
1
1
  const CCComponent = require('./CCComponent');
2
- const CCColor = require('./CCColor');
3
2
  const CCVec2 = require('./CCVec2');
4
3
 
4
+ const default_sprite_splash = 'a23235d1-15db-4b95-8439-a2e005bfff91';
5
+
5
6
  /**
6
7
  * Cocos Creator Sprite 组件
7
8
  */
@@ -9,7 +10,7 @@ class CCSprite extends CCComponent {
9
10
  constructor() {
10
11
  super();
11
12
  this.__type__ = 'cc.Sprite';
12
-
13
+
13
14
  this._materials = [{ __uuid__: 'eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432' }];
14
15
  this._srcBlendFactor = 770;
15
16
  this._dstBlendFactor = 771;
@@ -22,6 +23,8 @@ class CCSprite extends CCComponent {
22
23
  this._fillRange = 0;
23
24
  this._isTrimmedMode = true;
24
25
  this._atlas = null;
26
+
27
+ this.setSpriteFrame(default_sprite_splash);
25
28
  }
26
29
 
27
30
  /**
@@ -42,19 +45,28 @@ class CCSprite extends CCComponent {
42
45
  }
43
46
 
44
47
  /**
45
- * 转换为属性面板显示格式
48
+ * 获取属性
46
49
  */
47
- toPanelJSON() {
50
+ getProp() {
48
51
  const SIZE_MODE = ['CUSTOM', 'RAW', 'TRIMMED'];
49
52
  const SPRITE_TYPE = ['SIMPLE', 'SLICED', 'TILED', 'FILLED', 'MESH'];
50
53
  return {
51
- ...super.toPanelJSON(),
52
54
  sizeMode: SIZE_MODE[this._sizeMode] || this._sizeMode,
53
55
  type: SPRITE_TYPE[this._type] || this._type,
54
56
  trim: this._isTrimmedMode
55
57
  };
56
58
  }
57
59
 
60
+ /**
61
+ * 设置属性
62
+ */
63
+ setProp(props) {
64
+ if (props.sizeMode !== undefined) this.setSizeMode(props.sizeMode);
65
+ if (props.spriteFrame !== undefined) this.setSpriteFrame(props.spriteFrame);
66
+ if (props.type !== undefined) this._type = props.type;
67
+ return this;
68
+ }
69
+
58
70
  toJSON() {
59
71
  return {
60
72
  __type__: this.__type__,
Binary file