@gedit/editor-2d 0.3.0 → 0.3.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.
@@ -5,9 +5,18 @@ import { PlaygroundContext2d } from './playground-context';
5
5
  import { Editor2dDocument, Editor2dNode } from '../model';
6
6
  import { Rectangle } from '@gedit/math';
7
7
  import { SelectionEntityManager } from './selection-entity-manager';
8
- import { CanvasDraw, CanvasDrawLoadingState, GameObjectChangeEvent, GameObjectEventType } from './canvas-draw';
8
+ import {
9
+ CanvasDraw,
10
+ CanvasDrawLoadingState,
11
+ GameObjectChangeEvent,
12
+ GameObjectEventType,
13
+ } from './canvas-draw';
9
14
  import { debounce, OriginSchema } from '@gedit/utils';
10
- import { GameObject, GameObjectBaseType, GameWidgetIDE } from '@gedit/render-engine-ide';
15
+ import {
16
+ GameObject,
17
+ GameObjectBaseType,
18
+ GameWidgetIDE,
19
+ } from '@gedit/render-engine-ide';
11
20
  import { toFixedValue } from '@gedit/canvas-draw';
12
21
 
13
22
  const childrenMap = (list: GameObject[], c: GameObject) => {
@@ -31,7 +40,7 @@ const parentMap = (list: GameObject[], p: GameObject) => {
31
40
  list.push(p);
32
41
  }
33
42
  if (p.parent) {
34
- parentMap(list , p.parent);
43
+ parentMap(list, p.parent);
35
44
  }
36
45
  };
37
46
 
@@ -57,11 +66,13 @@ const getListData = (list: GameObject[]) => {
57
66
  childrenMap(updateList, item.parent);
58
67
  }
59
68
  });
60
- updateList = updateList.filter(c => !c.disposed).sort((a, b) => {
61
- const aa = a.getDepth?.() || 0;
62
- const bb = b.getDepth?.() || 0;
63
- return aa - bb;
64
- });
69
+ updateList = updateList
70
+ .filter(c => !c.disposed)
71
+ .sort((a, b) => {
72
+ const aa = a.getDepth?.() || 0;
73
+ const bb = b.getDepth?.() || 0;
74
+ return aa - bb;
75
+ });
65
76
  return updateList;
66
77
  };
67
78
  export class CanvasLayer extends Layer<PlaygroundContext2d> {
@@ -86,7 +97,8 @@ export class CanvasLayer extends Layer<PlaygroundContext2d> {
86
97
  host: this.node,
87
98
  renderEngine: this.context.renderEngine,
88
99
  assetsURI: this.getAssetsURI(),
89
- getComponentContent: (compId, parentNode, getParent) => this.document?.getComponentContent(compId, parentNode, getParent),
100
+ getComponentContent: (compId, parentNode, getParent) =>
101
+ this.document?.getComponentContent(compId, parentNode, getParent),
90
102
  isTemplate: this.context.options.isTemplate,
91
103
  });
92
104
  this.toDispose.pushAll([
@@ -94,25 +106,28 @@ export class CanvasLayer extends Layer<PlaygroundContext2d> {
94
106
  this.canvasDrawer.onRenderWidgetCreate(this.onWidgetUpdate.bind(this)),
95
107
  this.canvasDrawer.onGameObjectChange(this.onGameObjectChange.bind(this)),
96
108
  this.canvasDrawer.onLoading(this.updateLoading.bind(this)),
97
- this.listenPlaygroundEvent('mousedown', () => {
98
- this.context.syncToSelectionTree(this.selectionEntityManager);
99
- }, -2),
109
+ this.listenPlaygroundEvent(
110
+ 'mousedown',
111
+ () => {
112
+ this.context.syncToSelectionTree(this.selectionEntityManager);
113
+ },
114
+ -2
115
+ ),
100
116
  ]);
101
117
  // this.entityManager.getEntity<RulerConfigEntity>(RulerConfigEntity)?.customizeOriginClick(this.config.scrollPageBoundsToCenter.bind(this));
102
118
  this.config.loading = true;
103
119
  if (this.stateService) {
104
- this.toDispose.push(this.stateService?.onStateChanged(state => {
105
- // 布局完成后再归位画布;
106
- if (state === 'ready') {
107
- this.tryToResizeToCenter();
108
- }
109
- }));
120
+ this.toDispose.push(
121
+ this.stateService?.onStateChanged(state => {
122
+ // 布局完成后再归位画布;
123
+ if (state === 'ready') {
124
+ this.tryToResizeToCenter();
125
+ }
126
+ })
127
+ );
110
128
  }
111
129
  }
112
- onContainerInitOrigin(
113
- entity?: Editor2dEntity,
114
- update?: boolean
115
- ): void {
130
+ onContainerInitOrigin(entity?: Editor2dEntity, update?: boolean): void {
116
131
  const config = this.documentEntity.config;
117
132
  if (config.appConfig?.engine !== 'pixi' || !entity) {
118
133
  return;
@@ -120,16 +135,14 @@ export class CanvasLayer extends Layer<PlaygroundContext2d> {
120
135
  const node = this.nodeCache.get(entity.id);
121
136
  const type = node?.displayType;
122
137
  // 分组与组件创建完后初始化中心点;
123
- if (
124
- node && (type === GameObjectBaseType.COMPONENT)
125
- ) {
138
+ if (node && type === GameObjectBaseType.COMPONENT) {
126
139
  const { transform } = entity;
127
140
  const { origin: org, localBounds } = transform;
128
141
  // console.log(localBounds, entity.gameObject.rendererObject._localBoundsRect);
129
142
  const { x: minX, y: minY, width, height } = localBounds; // entity.gameObject.rendererObject._localBoundsRect;
130
143
  // 算出组件与组的 0 点位置;
131
- const x = toFixedValue((-minX / width) || 0, 6);
132
- const y = toFixedValue((-minY / height) || 0, 6);
144
+ const x = toFixedValue(-minX / width || 0, 6);
145
+ const y = toFixedValue(-minY / height || 0, 6);
133
146
  const initOrigin = { x, y };
134
147
  const o: OriginSchema = update ? org : initOrigin;
135
148
  if (update || typeof o.initX === 'undefined') {
@@ -138,7 +151,7 @@ export class CanvasLayer extends Layer<PlaygroundContext2d> {
138
151
  x: o.x || 0,
139
152
  y: o.y || 0,
140
153
  initX: initOrigin.x || 0,
141
- initY: initOrigin.y || 0
154
+ initY: initOrigin.y || 0,
142
155
  };
143
156
  // group origin 始终为 0;
144
157
  // transform.update({ origin });
@@ -150,7 +163,7 @@ export class CanvasLayer extends Layer<PlaygroundContext2d> {
150
163
  // this.selectionEntityManager.updateEntity(obj.id, obj);
151
164
  }
152
165
  }
153
- };
166
+ }
154
167
  onUpdateEntity = debounce((update: boolean) => {
155
168
  const updateList = getListData(this.updateList);
156
169
  updateList.forEach(item => {
@@ -158,7 +171,11 @@ export class CanvasLayer extends Layer<PlaygroundContext2d> {
158
171
  return;
159
172
  }
160
173
  // console.log('------------------updateddddddddddddddd-----------', item);
161
- this.selectionEntityManager.updateEntity(item.id, item, this.config.config.zoom);
174
+ this.selectionEntityManager.updateEntity(
175
+ item.id,
176
+ item,
177
+ this.config.config.zoom
178
+ );
162
179
  const entity = this.selectionEntityManager.nodeEntitiesCache.get(item.id);
163
180
  this.onContainerInitOrigin(entity, update);
164
181
  });
@@ -170,7 +187,13 @@ export class CanvasLayer extends Layer<PlaygroundContext2d> {
170
187
  switch (e.type) {
171
188
  case GameObjectEventType.CREATE:
172
189
  this.document?.updateNode(node, { error: undefined });
173
- this.selectionEntityManager.createEntity(gameObject.id, node, gameObject!, !!state?.selectable, !!state?.adsorbable);
190
+ this.selectionEntityManager.createEntity(
191
+ gameObject.id,
192
+ node,
193
+ gameObject!,
194
+ !!state?.selectable,
195
+ !!state?.adsorbable
196
+ );
174
197
  break;
175
198
  case GameObjectEventType.DESTROY:
176
199
  this.selectionEntityManager.removeEntity(gameObject.id);
@@ -185,7 +208,7 @@ export class CanvasLayer extends Layer<PlaygroundContext2d> {
185
208
  this.onUpdateEntity(true);
186
209
  break;
187
210
  case GameObjectEventType.ERROR:
188
- this.document?.updateNode(node, {error: e.error});
211
+ this.document?.updateNode(node, { error: e.error });
189
212
  break;
190
213
  }
191
214
  this.context.onCanvasDataChangedEmitter.fire(this.canvasDrawer!);
@@ -215,13 +238,18 @@ export class CanvasLayer extends Layer<PlaygroundContext2d> {
215
238
  this.config.loading = true;
216
239
  this.node.classList.remove('ready');
217
240
  this.config.updateConfig({
218
- loadingHTML: `${Math.round(currentCount / allCount * 100)}%`
241
+ loadingHTML: `${Math.round((currentCount / allCount) * 100)}%`,
219
242
  });
220
243
  }
221
244
  }
222
245
  protected loaded(): void {
223
246
  // TODO 目前只有模板编辑器需要触发
224
- if (!this.canvasDrawer || this.config.loading || !this.context.options.isTemplate) return;
247
+ if (
248
+ !this.canvasDrawer ||
249
+ this.config.loading ||
250
+ !this.context.options.isTemplate
251
+ )
252
+ return;
225
253
  for (const item of this.canvasDrawer.gameObjectCache.values()) {
226
254
  if (item.onSceneCreated) {
227
255
  item.onSceneCreated();
@@ -240,9 +268,13 @@ export class CanvasLayer extends Layer<PlaygroundContext2d> {
240
268
  const selectedEntities = this.selectionEntityManager.getSelectedEntities();
241
269
  if (this.document?.engineName === 'dom' && selectedEntities?.length) {
242
270
  selectedEntities.forEach(c => {
243
- const {gameObject} = c;
271
+ const { gameObject } = c;
244
272
  if (gameObject) {
245
- this.selectionEntityManager.updateEntity(gameObject.id, gameObject, this.config.config.zoom);
273
+ this.selectionEntityManager.updateEntity(
274
+ gameObject.id,
275
+ gameObject,
276
+ this.config.config.zoom
277
+ );
246
278
  }
247
279
  });
248
280
  }
@@ -265,17 +297,24 @@ export class CanvasLayer extends Layer<PlaygroundContext2d> {
265
297
  // private currentResolution = 1;
266
298
 
267
299
  tryToResizeToCenter = debounce(() => {
268
- const {visible } = this.documentEntity.config;
300
+ const { visible } = this.documentEntity.config;
269
301
  // const resolution = appConfig?.resolution || 1;
270
302
  const size = this.canvasDrawer?.getSceneSize();
271
- if (!visible || !size || this.config.config.width === 0 || this.config.config.height === 0) return;
303
+ if (
304
+ !visible ||
305
+ !size ||
306
+ this.config.config.width === 0 ||
307
+ this.config.config.height === 0
308
+ )
309
+ return;
272
310
  const newRect = new Rectangle(0, 0, size.width, size.height);
273
311
  if (!this.currentRectangle || !this.currentRectangle.isEqual(newRect)) {
274
312
  this.currentRectangle = newRect;
275
313
  // this.currentResolution = resolution;
276
314
  // this.config.updateConfig({resolution});
277
315
  this.config.setPageBounds(newRect);
278
- this.config.scrollPageBoundsToCenter(true, 16, !this.context.options.isTemplate)
316
+ this.config
317
+ .scrollPageBoundsToCenter(true, 16, !this.context.options.isTemplate)
279
318
  .then(() => this.onZoom());
280
319
  }
281
320
  }, 100); // 打开时会有一次 tag 大小的视图,宽是 98px,,dom 下没有引擎加载,会响应 98 宽,加 100 ms 延时
@@ -288,7 +327,7 @@ export class CanvasLayer extends Layer<PlaygroundContext2d> {
288
327
  engineName: config.appConfig?.engine || '',
289
328
  appConfig: config.appConfig,
290
329
  configVersion: config.configVersion,
291
- uri: document?.uri
330
+ uri: document?.uri,
292
331
  });
293
332
  /**
294
333
  * 尝试触发居中
@@ -0,0 +1,136 @@
1
+ import { toFixedValue } from '@gedit/canvas-draw';
2
+ import { Matrix } from '@gedit/math';
3
+ import { Bounds } from '@gedit/playground/lib/common/utils/bounds';
4
+ import {
5
+ GameObjectBaseType,
6
+ GameObjectData,
7
+ } from '@gedit/render-engine/lib/common';
8
+
9
+ interface ChildBounds {
10
+ x: number;
11
+ y: number;
12
+ maxX: number;
13
+ maxY: number;
14
+ isGroup?: boolean;
15
+ isMask?: boolean;
16
+ mask?: string;
17
+ id?: string;
18
+ }
19
+
20
+ const ignoreBounds: string[] = [
21
+ GameObjectBaseType.ANIMATION,
22
+ GameObjectBaseType.AUDIO,
23
+ GameObjectBaseType.CAMERA,
24
+ ];
25
+
26
+ const boundsMask = (
27
+ bounds: ChildBounds,
28
+ maskBounds?: ChildBounds
29
+ ): ChildBounds => {
30
+ if (!maskBounds) {
31
+ return bounds;
32
+ }
33
+ const minX = bounds.x > maskBounds.x ? bounds.x : maskBounds.x;
34
+ const minY = bounds.y > maskBounds.y ? bounds.y : maskBounds.y;
35
+ const maxX = bounds.maxX < maskBounds.maxX ? bounds.maxX : maskBounds.maxX;
36
+ const maxY = bounds.maxY < maskBounds.maxY ? bounds.maxY : maskBounds.maxY;
37
+ if (minX <= maxX && minY <= maxY) {
38
+ return { x: minX, y: minY, maxX, maxY };
39
+ }
40
+ return { x: 0, y: 0, maxX: 0, maxY: 0 };
41
+ };
42
+
43
+ export const getPIXILocalBoundsByNode = (obj: GameObjectData): ChildBounds => {
44
+ const children: GameObjectData[] = obj.children || [];
45
+ const bounds: ChildBounds[] = children
46
+ .map(c => {
47
+ const {
48
+ position = { x: 0, y: 0 },
49
+ scale = { x: 1, y: 1 },
50
+ rotation = 0,
51
+ origin = { x: 1, y: 1 },
52
+ } = c;
53
+ let width;
54
+ let height;
55
+ if (c.canvasHide) {
56
+ return { x: 0, y: 0, maxX: 0, maxY: 0 };
57
+ }
58
+ if (
59
+ c.displayType === GameObjectBaseType.COMPONENT ||
60
+ c.displayType === GameObjectBaseType.GROUP
61
+ ) {
62
+ if (c.children?.length) {
63
+ const childSize = getPIXILocalBoundsByNode(c);
64
+ console.log(childSize, c);
65
+ width = childSize.maxX - childSize.x;
66
+ height = childSize.maxY - childSize.y;
67
+ } else {
68
+ width = c.size?.width || 0;
69
+ height = c.size?.height || 0;
70
+ }
71
+ return {
72
+ x: position.x - origin.x * width * scale.x,
73
+ y: position.y - origin.y * height * scale?.y,
74
+ maxX: position.x + (width - origin.x * width) * scale.x,
75
+ maxY: position.y + (height - origin.y * height) * scale.y,
76
+ isGroup: true,
77
+ isMask: c.isMask,
78
+ mask: c.mask,
79
+ id: c.id,
80
+ };
81
+ }
82
+
83
+ const transformData = new Matrix();
84
+ // data 里的 size 已经是缩放后的值了,不需要再乘 scale
85
+ // transformData.scale(scale.x, scale.y);
86
+ transformData.rotate(rotation);
87
+ transformData.translate(position.x, position.y);
88
+ // 文字有误差,,粒子乱飘;
89
+ width = c.size?.width;
90
+ height = c.size?.height;
91
+ const data = {
92
+ ...c,
93
+ size: {
94
+ ...c.size,
95
+ width,
96
+ height,
97
+ },
98
+ };
99
+ const bounds =
100
+ ignoreBounds.includes(c.displayType) || c.displayType.match('Filter')
101
+ ? { x: 0, y: 0, height: 0, width: 0 }
102
+ : Bounds.getBounds(data as any, transformData);
103
+ return {
104
+ x: toFixedValue(bounds.x, 6),
105
+ y: toFixedValue(bounds.y, 6),
106
+ maxX: toFixedValue(bounds.x + bounds.width, 6),
107
+ maxY: toFixedValue(bounds.y + bounds.height, 6),
108
+ isMask: c.isMask,
109
+ mask: c.mask,
110
+ id: c.id,
111
+ };
112
+ })
113
+ .filter(c => c && c.maxX - c.x > 0 && c.maxY - c.y > 0);
114
+ const maskClearBounds: ChildBounds[] = bounds.map(c => {
115
+ if (c.isMask) {
116
+ return { x: 0, y: 0, maxX: 0, maxY: 0 };
117
+ }
118
+ if (c.mask) {
119
+ const maskBounds = bounds.find(d => d.id === c.mask);
120
+ if (maskBounds) {
121
+ return boundsMask(c, maskBounds);
122
+ }
123
+ }
124
+ return c;
125
+ });
126
+ const x = Math.min(...maskClearBounds.map(c => c.x));
127
+ const y = Math.min(...maskClearBounds.map(c => c.y));
128
+ const maxX = Math.max(...maskClearBounds.map(c => c.maxX));
129
+ const maxY = Math.max(...maskClearBounds.map(c => c.maxY));
130
+ return {
131
+ x,
132
+ y,
133
+ maxX,
134
+ maxY,
135
+ };
136
+ };