-
Notifications
You must be signed in to change notification settings - Fork 1
/
Transform.ts
489 lines (431 loc) · 11 KB
/
Transform.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import { Component, Container } from "../component";
import EE from 'eventemitter3'
import { Matrix, Vector2 } from "../math";
import { TouchEvent } from "../event";
import { ValueOf } from "../types/ValueOf";
import { addComponent, removeComponent } from "./componentBridge";
import { Props, setProps } from "../utils";
export type Constructor<T = unknown> = new (...args: any[]) => T;
export type TransformEvent = ValueOf<typeof Transform.Event> | `${TouchEvent}`;
let id = 0;
/**
* 节点,用于构建整个项目的节点树,也是所有组件挂载的节点
* ```ts
* const node = new Transform();
* ```
*/
export class Transform<T extends Container = Container> {
id: number = 0;
alone = false;
// 是否激活
active: boolean = true;
name: string = 'node';
emitter = new EE<TransformEvent>();
redraw = false;
/**
* 事件
*/
static Event = {
/**
* 当添加到显示舞台时
*/
ADDED: 'ADDED',
/**
* 当被移除于舞台时
*/
REMOVED: 'REMOVED',
/**
* 可见元素发生变化时(需要渲染的元素)
*/
CHANGE_DISPLY: 'CHANGE_DISPLY',
/**
* 当添加新的字节点时
*/
CHILD_ADDED: 'CHILD_ADDED',
/**
* 尺寸发生变化时
*/
RESIZE: 'RESIZE',
/**
* 位置发生变化
*/
REPOSITION: 'REPOSITION',
/**
* 帧刷新前
*/
TICKER_BEFORE: 'TICKER_BEFORE',
/**
* 帧刷新后
*/
TICKER_AFTER: 'TICKER_AFTER'
} as const;
/**
*
* 一个节点只能挂载一个视觉组件
* @param classConstructor
*/
constructor(classConstructor?: Constructor<T>) {
this.id = ++id;
if (classConstructor) {
this.container = this.addComponent(classConstructor);
}
}
private _position: Vector2 = new Vector2();
/**
* 位置
*/
get position() {
return this._position;
}
set position(value: Vector2) {
this._position = value;
}
get x() {
return this.position.x;
}
set x(val: number) {
this.position.x = val;
}
get y() {
return this.position.y;
}
set y(val: number) {
this.position.y = val;
}
private _size: Vector2 = new Vector2();
/**
* 大小
*/
get size() {
return this._size;
}
set size(value: Vector2) {
this._size = value;
this.emitter.emit(Transform.Event.RESIZE);
}
get width() {
return this.size.x;
}
set width(val: number) {
this.size.x = val;
}
get height() {
return this.size.y;
}
set height(val: number) {
this.size.y = val;
}
private _scale: Vector2 = new Vector2(1, 1);
/**
* 缩放
*/
get scale() {
return this._scale;
}
set scale(value: Vector2) {
this._scale = value;
}
get scaleX() {
return this.scale.x;
}
set scaleX(val: number) {
this.scale.x = val;
}
get scaleY() {
return this.scale.y;
}
set scaleY(val: number) {
this.scale.y = val;
}
_rotation: number = 0;//Vector2 = new Vector2(0, 1);
/**
* 旋转
*/
get rotation() {
return this._rotation;
}
set rotation(value: number) {
this._rotation = value;
}
_anchor: Vector2 = new Vector2(0, 0);
/**
* 锚点
*/
get anchor() {
return this._anchor;
}
set anchor(value: Vector2) {
this._anchor = value;
}
get anchorX() {
return this.anchor.x;
}
set anchorX(val: number) {
this.anchor.x = val;
}
get anchorY() {
return this.anchor.y;
}
set anchorY(val: number) {
this.anchor.y = val;
}
// 要应用位置等信息的元素
container!: T;
/**
* 所有组件
* 默认包含一个容器组件,当添加了其他容器组件后自动替换,只能包含一个视觉组件
*/
components: Component[] = [];
/**
* 父节点
*/
parent?: Transform;
private _touch = false;
/**
* 触摸事件,默认关闭
*/
set touch(val: boolean) {
this._touch = true;
}
get touch() {
return this._touch;
}
/**
* 是否继续向父元素传递事件(冒泡)
*/
deliver = true;
/**
* 是否穿透,可触发非父元素外的其它元素事件
*/
pierce = false;
/**
* 透明度
*/
alpha = 1;
/**
* 添加一个组件
* @param classConstructor - 要挂载的组件
*/
addComponent<T extends Component>(classConstructor: Constructor<T> | T, props?: Props<T>): T {
return addComponent(this, classConstructor, props);
}
/**
* 移除一个组件
* @param component - 将要移除的组件
*/
removeComponent(component: Component) {
removeComponent(this, component)
}
/**
* 移除所有组件
*/
removeAllComponent() {
for (let i = this.components.length - 1; i >= 0; i--) {
this.removeComponent(this.components[i]);
}
}
/**
* 获取单个组件,获取首个匹配到的组件
* @param classConstructor - 组件
*/
getComponent<T extends Component>(classConstructor: Constructor<T>): T | undefined {
return <T>this.components.find(value => value instanceof classConstructor);
}
/**
* 获取所有组件
* @param classConstructor - 组件
* @returns - 所有满足条件的组件
*/
getComponents<T>(classConstructor: Constructor<T>): T[] {
let comps: T[] = [];
this.components.forEach(value => {
if (value instanceof classConstructor) {
comps.push(value);
}
});
return comps;
}
/**
* 从子节点获取所有组件,不会包含当前节点
* 递归查询所有子节点的
* @param classConstructor - 组件
* @returns - 所有满足条件的组件
*/
getComponentsInChildren<T>(classConstructor: Constructor<T>): T[] {
let list: T[] = [];
this.components.forEach(value => {
if (value instanceof classConstructor) {
list.push(value);
}
if (value.node.components.length == 0) {
return;
}
list.push.apply(list, value.node.getComponentsInChildren(classConstructor));
});
return list;
}
/**
* 子节点
*/
children: Transform[] = [];
/**
* 插入一个子节点
* @param transform - 待插入的节点
*/
addChild<U extends Transform>(transform: U, props?: Props<U>) {
if (transform.parent) {
transform.parent.removeChild(transform);
}
this.children.push(transform);
transform.parent = this;
props && setProps(transform, props)
return transform;
}
/**
* 在指定位置插入节点
* @param child - 待插入的节点
* @param index - 要插入的位置
*/
addChildAt(child: Transform, index: number) {
if (child.parent) {
child.parent.removeChild(child);
}
this.children.splice(index, 0, child);
child.parent = this;
}
/**
* 移除一个节点
* @param transform - 将要移除的节点
*/
removeChild(transform: Transform) {
let index = this.children.indexOf(transform);
if (index == -1) {
return;
}
this.removeChildAt(index);
}
/**
* 移除一个指定位置的元素
* @param index - 要移除节点的位置
*/
removeChildAt(index: number) {
const node = this.children.splice(index, 1)[0];
node.parent = undefined;
return node;
}
/**
* 移除所有子元素
*/
removeChildren() {
if (this.children.length == 0) {
return;
}
this.removeChildAt(0);
this.removeChildren();
}
update(time: number) {
this.components.forEach(val => val.update && val.update(time));
}
/**
* 消毁
*/
destroy() {
this.parent?.removeChild(this);
// this.removeAllComponent();
this.onDestroy && this.onDestroy();
}
onDestroy?(): void;
/**
* 路径查找节点
* ```ts
* // 创建身体节点
* const body = new Transform();
* // 设置名称
* body.name = 'body';
* // 创建头部节点
* const head = new Transform();
* // 设置名称
* head.name = 'head';
* // 创建左眼节点
* const leftEye = new Transform();
* // 设置名称
* leftEye.name = 'lEye';
* // 创建右眼节点
* const rightEye = new Transform();
* // 设置名称
* rightEye.name = 'rEye';
* // 将右眼添加到头部
* head.addChild(leftEye);
* // 将左眼添加到头部
* head.addChild(rightEye);
* // 将头部添加到身体
* body.addChild(head);
* // 从身体获取左眼
* body.find('head/lEye');
* // 从身体获取右眼
* body.find('head/rEye');
* ```
* @path - 路径
*/
find(path: string | string[]): Transform | undefined {
path = typeof path == 'string' ? path.split('/') : path;
let node: Transform | undefined;
let name = path.shift();
node = this.children.find(val => val.name == name);
if (!node) {
return undefined;
}
if (path.length > 0) {
return node.find(path);
}
return node;
}
/**
* 根据name查找节点
* @param queryString
* @returns
*/
select(queryString: string | string[]) {
queryString = typeof queryString === 'string' ? queryString.split(',') : queryString;
const children = [];
this.children.forEach(val => {
if (queryString.includes(val.name)) {
children.push(val);
}
if (val.children.length > 0) {
children.push(...val.select(queryString));
}
});
return children;
}
/**
* 获取右上角偏真实移量
* @returns
*/
getOffset(): Vector2 {
return this.size.clone().mul(this.anchor).mul(this.scale);
}
/**
* 获取相对世界坐标
*/
getWordPoisition(): Vector2 {
const position = this.position.clone();
let parent = this.parent;
if (!!parent) {
return position.add(parent.getWordPoisition().add(parent.getOffset().mul(-1)));
}
return position;
}
/**
* 获取相对于世界的缩放
*/
getWordScale() {
let scale = this.scale.clone();
let parent = this.parent;
while (parent) {
scale.mul(parent.scale);
parent = parent.parent;
}
return scale;
}
}