cocos2d-cli 1.1.2 → 1.4.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.
@@ -0,0 +1,107 @@
1
+ /**
2
+ * cc.Camera 组件
3
+ */
4
+
5
+ const { generateId, parseColorToCcColor } = require('../utils');
6
+
7
+ // 属性映射
8
+ const PROP_MAP = {
9
+ 'depth': '_depth',
10
+ 'zoomRatio': '_zoomRatio',
11
+ 'ortho': '_ortho',
12
+ 'orthoSize': '_orthoSize',
13
+ 'cullingMask': '_cullingMask',
14
+ 'backgroundColor': '_backgroundColor',
15
+ 'fov': '_fov',
16
+ 'nearClip': '_nearClip',
17
+ 'farClip': '_farClip'
18
+ };
19
+
20
+ /**
21
+ * 创建 Camera 组件
22
+ * @param {number} nodeId - 节点索引
23
+ * @returns {object} 组件数据
24
+ */
25
+ function create(nodeId) {
26
+ return {
27
+ "__type__": "cc.Camera",
28
+ "_name": "",
29
+ "_objFlags": 0,
30
+ "node": { "__id__": nodeId },
31
+ "_enabled": true,
32
+ "_cullingMask": 4294967295,
33
+ "_clearFlags": 7,
34
+ "_backgroundColor": {
35
+ "__type__": "cc.Color",
36
+ "r": 0,
37
+ "g": 0,
38
+ "b": 0,
39
+ "a": 255
40
+ },
41
+ "_depth": -1,
42
+ "_zoomRatio": 1,
43
+ "_targetTexture": null,
44
+ "_fov": 60,
45
+ "_orthoSize": 10,
46
+ "_nearClip": 1,
47
+ "_farClip": 4096,
48
+ "_ortho": true,
49
+ "_rect": {
50
+ "__type__": "cc.Rect",
51
+ "x": 0,
52
+ "y": 0,
53
+ "width": 1,
54
+ "height": 1
55
+ },
56
+ "_renderStages": 1,
57
+ "_alignWithScreen": true,
58
+ "_id": generateId()
59
+ };
60
+ }
61
+
62
+ /**
63
+ * 应用属性
64
+ * @param {object} comp - 组件对象
65
+ * @param {object} props - 属性对象
66
+ */
67
+ function applyProps(comp, props) {
68
+ if (!props) return;
69
+
70
+ for (const [key, value] of Object.entries(props)) {
71
+ const compKey = PROP_MAP[key];
72
+ if (compKey) {
73
+ // backgroundColor 特殊处理
74
+ if (key === 'backgroundColor' && typeof value === 'string') {
75
+ const ccColor = parseColorToCcColor(value);
76
+ if (ccColor) {
77
+ comp[compKey] = ccColor;
78
+ }
79
+ } else {
80
+ comp[compKey] = value;
81
+ }
82
+ }
83
+ }
84
+ }
85
+
86
+ /**
87
+ * 提取属性(用于显示)
88
+ * @param {object} comp - 组件对象
89
+ * @returns {object} 提取的属性
90
+ */
91
+ function extractProps(comp) {
92
+ return {
93
+ depth: comp._depth,
94
+ zoomRatio: comp._zoomRatio,
95
+ ortho: comp._ortho,
96
+ cullingMask: comp._cullingMask
97
+ };
98
+ }
99
+
100
+ module.exports = {
101
+ type: 'camera',
102
+ ccType: 'cc.Camera',
103
+ create,
104
+ applyProps,
105
+ extractProps,
106
+ PROP_MAP
107
+ };
@@ -0,0 +1,89 @@
1
+ /**
2
+ * cc.Canvas 组件
3
+ */
4
+
5
+ const { generateId } = require('../utils');
6
+
7
+ // 属性映射
8
+ const PROP_MAP = {
9
+ 'designResolution': '_designResolution',
10
+ 'fitWidth': '_fitWidth',
11
+ 'fitHeight': '_fitHeight'
12
+ };
13
+
14
+ /**
15
+ * 创建 Canvas 组件
16
+ * @param {number} nodeId - 节点索引
17
+ * @returns {object} 组件数据
18
+ */
19
+ function create(nodeId) {
20
+ return {
21
+ "__type__": "cc.Canvas",
22
+ "_name": "",
23
+ "_objFlags": 0,
24
+ "node": { "__id__": nodeId },
25
+ "_enabled": true,
26
+ "_designResolution": {
27
+ "__type__": "cc.Size",
28
+ "width": 960,
29
+ "height": 640
30
+ },
31
+ "_fitWidth": false,
32
+ "_fitHeight": true,
33
+ "_id": generateId()
34
+ };
35
+ }
36
+
37
+ /**
38
+ * 应用属性
39
+ * @param {object} comp - 组件对象
40
+ * @param {object} props - 属性对象
41
+ */
42
+ function applyProps(comp, props) {
43
+ if (!props) return;
44
+
45
+ for (const [key, value] of Object.entries(props)) {
46
+ const compKey = PROP_MAP[key];
47
+ if (compKey) {
48
+ // designResolution 特殊处理
49
+ if (key === 'designResolution' && Array.isArray(value)) {
50
+ comp[compKey] = { "__type__": "cc.Size", "width": value[0], "height": value[1] };
51
+ } else {
52
+ comp[compKey] = value;
53
+ }
54
+ }
55
+ }
56
+ }
57
+
58
+ /**
59
+ * 提取属性(用于显示)
60
+ * @param {object} comp - 组件对象
61
+ * @returns {object} 提取的属性
62
+ */
63
+ function extractProps(comp) {
64
+ const clean = (obj) => {
65
+ if (!obj || typeof obj !== 'object') return obj;
66
+ const result = {};
67
+ for (const [k, v] of Object.entries(obj)) {
68
+ if (k !== '__type__') {
69
+ result[k] = v;
70
+ }
71
+ }
72
+ return result;
73
+ };
74
+
75
+ return {
76
+ designResolution: clean(comp._designResolution),
77
+ fitWidth: comp._fitWidth,
78
+ fitHeight: comp._fitHeight
79
+ };
80
+ }
81
+
82
+ module.exports = {
83
+ type: 'canvas',
84
+ ccType: 'cc.Canvas',
85
+ create,
86
+ applyProps,
87
+ extractProps,
88
+ PROP_MAP
89
+ };
@@ -0,0 +1,157 @@
1
+ /**
2
+ * 组件模块入口
3
+ * 统一导出所有组件
4
+ */
5
+
6
+ const sprite = require('./sprite');
7
+ const label = require('./label');
8
+ const button = require('./button');
9
+ const widget = require('./widget');
10
+ const layout = require('./layout');
11
+ const canvas = require('./canvas');
12
+ const camera = require('./camera');
13
+ const particleSystem = require('./particle-system');
14
+
15
+ // 组件类型映射
16
+ const components = {
17
+ sprite,
18
+ label,
19
+ button,
20
+ widget,
21
+ layout,
22
+ canvas,
23
+ camera,
24
+ particleSystem,
25
+ // 别名
26
+ particle: particleSystem
27
+ };
28
+
29
+ // 类型名称映射(支持多种写法)
30
+ const typeAliases = {
31
+ 'sprite': 'sprite',
32
+ 'label': 'label',
33
+ 'button': 'button',
34
+ 'widget': 'widget',
35
+ 'layout': 'layout',
36
+ 'canvas': 'canvas',
37
+ 'camera': 'camera',
38
+ 'particle': 'particleSystem',
39
+ 'particlesystem': 'particleSystem',
40
+ 'particleSystem': 'particleSystem'
41
+ };
42
+
43
+ /**
44
+ * 获取组件模块
45
+ * @param {string} type - 组件类型
46
+ * @returns {object|null} 组件模块
47
+ */
48
+ function getComponent(type) {
49
+ const normalizedName = typeAliases[type.toLowerCase()];
50
+ return normalizedName ? components[normalizedName] : null;
51
+ }
52
+
53
+ /**
54
+ * 创建组件
55
+ * @param {string} type - 组件类型
56
+ * @param {number} nodeId - 节点索引
57
+ * @returns {object|null} 组件数据
58
+ */
59
+ function createComponent(type, nodeId) {
60
+ const comp = getComponent(type);
61
+ return comp ? comp.create(nodeId) : null;
62
+ }
63
+
64
+ /**
65
+ * 应用组件属性
66
+ * @param {object} comp - 组件对象
67
+ * @param {object} props - 属性对象
68
+ * @param {object} node - 节点对象(可选)
69
+ */
70
+ function applyComponentProps(comp, props, node) {
71
+ if (!comp || !props) return;
72
+
73
+ const ccType = comp.__type__;
74
+ const compModule = getComponentByCcType(ccType);
75
+
76
+ if (compModule && compModule.applyProps) {
77
+ compModule.applyProps(comp, props, node);
78
+ }
79
+ }
80
+
81
+ /**
82
+ * 提取组件属性
83
+ * @param {object} comp - 组件对象
84
+ * @returns {object} 提取的属性
85
+ */
86
+ function extractComponentProps(comp) {
87
+ if (!comp) return null;
88
+
89
+ const ccType = comp.__type__;
90
+ const compModule = getComponentByCcType(ccType);
91
+
92
+ const base = comp._enabled ? { type: ccType } : { type: ccType, enabled: false };
93
+
94
+ if (compModule && compModule.extractProps) {
95
+ return { ...base, ...compModule.extractProps(comp) };
96
+ }
97
+
98
+ // 默认提取:非 _ 开头的属性
99
+ const result = { ...base };
100
+ for (const key of Object.keys(comp)) {
101
+ if (!key.startsWith('_') && key !== '__type__') {
102
+ result[key] = comp[key];
103
+ }
104
+ }
105
+ return result;
106
+ }
107
+
108
+ /**
109
+ * 通过 cc.__type__ 获取组件模块
110
+ * @param {string} ccType - cc 类型名
111
+ * @returns {object|null} 组件模块
112
+ */
113
+ function getComponentByCcType(ccType) {
114
+ for (const comp of Object.values(components)) {
115
+ if (comp.ccType === ccType) {
116
+ return comp;
117
+ }
118
+ }
119
+ return null;
120
+ }
121
+
122
+ /**
123
+ * 解析组件定义
124
+ * 支持两种格式:
125
+ * 1. 字符串: "sprite"
126
+ * 2. 对象: { "type": "sprite", "sizeMode": 1 }
127
+ * @param {string|object} compDef - 组件定义
128
+ * @returns {object|null} - { type, props } 或 null
129
+ */
130
+ function parseComponent(compDef) {
131
+ if (typeof compDef === 'string') {
132
+ const normalizedName = typeAliases[compDef.toLowerCase()];
133
+ return normalizedName ? { type: normalizedName, props: {} } : null;
134
+ }
135
+
136
+ if (typeof compDef === 'object' && compDef.type) {
137
+ const normalizedName = typeAliases[compDef.type.toLowerCase()];
138
+ if (!normalizedName) return null;
139
+
140
+ const props = { ...compDef };
141
+ delete props.type;
142
+ return { type: normalizedName, props };
143
+ }
144
+
145
+ return null;
146
+ }
147
+
148
+ module.exports = {
149
+ components,
150
+ typeAliases,
151
+ getComponent,
152
+ getComponentByCcType,
153
+ createComponent,
154
+ applyComponentProps,
155
+ extractComponentProps,
156
+ parseComponent
157
+ };
@@ -0,0 +1,120 @@
1
+ /**
2
+ * cc.Label 组件
3
+ */
4
+
5
+ const { generateId, parseColorToCcColor } = require('../utils');
6
+
7
+ const DEFAULT_MATERIAL = { "__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432" };
8
+
9
+ // 属性映射
10
+ const PROP_MAP = {
11
+ 'string': '_string',
12
+ 'fontSize': '_fontSize',
13
+ 'lineHeight': '_lineHeight',
14
+ 'horizontalAlign': '_N$horizontalAlign',
15
+ 'verticalAlign': '_N$verticalAlign',
16
+ 'overflow': '_N$overflow',
17
+ 'fontFamily': '_N$fontFamily',
18
+ 'wrap': '_enableWrapText',
19
+ 'color': '_color' // 特殊处理,设置到节点
20
+ };
21
+
22
+ // 枚举值
23
+ const ENUMS = {
24
+ horizontalAlign: ['LEFT', 'CENTER', 'RIGHT'],
25
+ verticalAlign: ['TOP', 'CENTER', 'BOTTOM'],
26
+ overflow: ['NONE', 'CLAMP', 'SHRINK', 'RESIZE_HEIGHT']
27
+ };
28
+
29
+ /**
30
+ * 创建 Label 组件
31
+ * @param {number} nodeId - 节点索引
32
+ * @returns {object} 组件数据
33
+ */
34
+ function create(nodeId) {
35
+ return {
36
+ "__type__": "cc.Label",
37
+ "_name": "",
38
+ "_objFlags": 0,
39
+ "node": { "__id__": nodeId },
40
+ "_enabled": true,
41
+ "_materials": [DEFAULT_MATERIAL],
42
+ "_srcBlendFactor": 770,
43
+ "_dstBlendFactor": 771,
44
+ "_string": "Label",
45
+ "_N$string": "Label",
46
+ "_fontSize": 40,
47
+ "_lineHeight": 40,
48
+ "_enableWrapText": true,
49
+ "_N$file": null,
50
+ "_isSystemFontUsed": true,
51
+ "_spacingX": 0,
52
+ "_batchAsBitmap": false,
53
+ "_styleFlags": 0,
54
+ "_underlineHeight": 0,
55
+ "_N$horizontalAlign": 1,
56
+ "_N$verticalAlign": 1,
57
+ "_N$fontFamily": "Arial",
58
+ "_N$overflow": 0,
59
+ "_N$cacheMode": 0,
60
+ "_id": generateId()
61
+ };
62
+ }
63
+
64
+ /**
65
+ * 应用属性
66
+ * @param {object} comp - 组件对象
67
+ * @param {object} props - 属性对象
68
+ * @param {object} node - 节点对象(可选,用于 color)
69
+ */
70
+ function applyProps(comp, props, node) {
71
+ if (!props) return;
72
+
73
+ // color 特殊处理:设置到节点
74
+ if (props.color && node) {
75
+ const ccColor = parseColorToCcColor(props.color);
76
+ if (ccColor) {
77
+ node._color = ccColor;
78
+ }
79
+ delete props.color;
80
+ }
81
+
82
+ for (const [key, value] of Object.entries(props)) {
83
+ const compKey = PROP_MAP[key];
84
+ if (compKey) {
85
+ comp[compKey] = value;
86
+ // string 需要同步到 _N$string
87
+ if (key === 'string') {
88
+ comp._N$string = value;
89
+ }
90
+ }
91
+ }
92
+ }
93
+
94
+ /**
95
+ * 提取属性(用于显示)
96
+ * @param {object} comp - 组件对象
97
+ * @returns {object} 提取的属性
98
+ */
99
+ function extractProps(comp) {
100
+ return {
101
+ string: comp._string,
102
+ fontSize: comp._fontSize,
103
+ lineHeight: comp._lineHeight,
104
+ horizontalAlign: ENUMS.horizontalAlign[comp._N$horizontalAlign] || comp._N$horizontalAlign,
105
+ verticalAlign: ENUMS.verticalAlign[comp._N$verticalAlign] || comp._N$verticalAlign,
106
+ overflow: ENUMS.overflow[comp._N$overflow] || comp._N$overflow,
107
+ fontFamily: comp._N$fontFamily,
108
+ enableWrapText: comp._enableWrapText
109
+ };
110
+ }
111
+
112
+ module.exports = {
113
+ type: 'label',
114
+ ccType: 'cc.Label',
115
+ create,
116
+ applyProps,
117
+ extractProps,
118
+ PROP_MAP,
119
+ ENUMS
120
+ };
@@ -0,0 +1,110 @@
1
+ /**
2
+ * cc.Layout 组件
3
+ */
4
+
5
+ const { generateId } = require('../utils');
6
+
7
+ // 属性映射
8
+ const PROP_MAP = {
9
+ 'layoutType': '_N$layoutType',
10
+ 'cellSize': '_N$cellSize',
11
+ 'startAxis': '_N$startAxis',
12
+ 'paddingLeft': '_N$paddingLeft',
13
+ 'paddingRight': '_N$paddingRight',
14
+ 'paddingTop': '_N$paddingTop',
15
+ 'paddingBottom': '_N$paddingBottom',
16
+ 'spacingX': '_N$spacingX',
17
+ 'spacingY': '_N$spacingY',
18
+ 'resize': '_resize'
19
+ };
20
+
21
+ // 枚举值
22
+ const ENUMS = {
23
+ layoutType: ['NONE', 'HORIZONTAL', 'VERTICAL', 'GRID']
24
+ };
25
+
26
+ /**
27
+ * 创建 Layout 组件
28
+ * @param {number} nodeId - 节点索引
29
+ * @returns {object} 组件数据
30
+ */
31
+ function create(nodeId) {
32
+ return {
33
+ "__type__": "cc.Layout",
34
+ "_name": "",
35
+ "_objFlags": 0,
36
+ "node": { "__id__": nodeId },
37
+ "_enabled": true,
38
+ "_layoutSize": {
39
+ "__type__": "cc.Size",
40
+ "width": 200,
41
+ "height": 150
42
+ },
43
+ "_resize": 1,
44
+ "_N$layoutType": 2,
45
+ "_N$cellSize": {
46
+ "__type__": "cc.Size",
47
+ "width": 40,
48
+ "height": 40
49
+ },
50
+ "_N$startAxis": 0,
51
+ "_N$paddingLeft": 0,
52
+ "_N$paddingRight": 0,
53
+ "_N$paddingTop": 0,
54
+ "_N$paddingBottom": 0,
55
+ "_N$spacingX": 0,
56
+ "_N$spacingY": 0,
57
+ "_N$verticalDirection": 1,
58
+ "_N$horizontalDirection": 0,
59
+ "_N$affectedByScale": false,
60
+ "_id": generateId()
61
+ };
62
+ }
63
+
64
+ /**
65
+ * 应用属性
66
+ * @param {object} comp - 组件对象
67
+ * @param {object} props - 属性对象
68
+ */
69
+ function applyProps(comp, props) {
70
+ if (!props) return;
71
+
72
+ for (const [key, value] of Object.entries(props)) {
73
+ const compKey = PROP_MAP[key];
74
+ if (compKey) {
75
+ // cellSize 特殊处理
76
+ if (key === 'cellSize' && Array.isArray(value)) {
77
+ comp[compKey] = { "__type__": "cc.Size", "width": value[0], "height": value[1] };
78
+ } else {
79
+ comp[compKey] = value;
80
+ }
81
+ }
82
+ }
83
+ }
84
+
85
+ /**
86
+ * 提取属性(用于显示)
87
+ * @param {object} comp - 组件对象
88
+ * @returns {object} 提取的属性
89
+ */
90
+ function extractProps(comp) {
91
+ return {
92
+ layoutType: ENUMS.layoutType[comp._N$layoutType] || comp._N$layoutType,
93
+ spacingX: comp._N$spacingX,
94
+ spacingY: comp._N$spacingY,
95
+ paddingLeft: comp._N$paddingLeft,
96
+ paddingRight: comp._N$paddingRight,
97
+ paddingTop: comp._N$paddingTop,
98
+ paddingBottom: comp._N$paddingBottom
99
+ };
100
+ }
101
+
102
+ module.exports = {
103
+ type: 'layout',
104
+ ccType: 'cc.Layout',
105
+ create,
106
+ applyProps,
107
+ extractProps,
108
+ PROP_MAP,
109
+ ENUMS
110
+ };