-
Notifications
You must be signed in to change notification settings - Fork 14
/
YogaLayout.ts
618 lines (503 loc) · 18.2 KB
/
YogaLayout.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
import * as Yoga from "yoga-layout-prebuilt";
import { YogaConstants } from "./YogaContants";
import { YogaLayoutConfig } from "./YogaLayoutConfig";
import YogaEdges = YogaConstants.YogaEdges;
import DisplayObject = PIXI.DisplayObject;
import ComputedLayout = YogaConstants.ComputedLayout;
import FlexDirection = YogaConstants.FlexDirection;
import JustifyContent = YogaConstants.JustifyContent;
import Align = YogaConstants.Align;
import FlexWrap = YogaConstants.FlexWrap;
import Display = YogaConstants.Display;
import PositionType = YogaConstants.PositionType;
export type PixelsOrPercentage = number | string;
export type YogaSize = PixelsOrPercentage | "pixi" | "auto";
export class YogaLayout {
/**
* Experimental feature for building layouts independent of pixi tree
*/
public static roots: Map<string, YogaLayout> = new Map();
public static readonly LAYOUT_UPDATED_EVENT = "LAYOUT_UPDATED_EVENT";
public static readonly NEED_LAYOUT_UPDATE = "NEED_LAYOUT_UPDATE";
public readonly target: DisplayObject;
public readonly node: Yoga.YogaNode;
public children: YogaLayout[] = [];
public parent?: YogaLayout;
/**
* True if Yoga should manage PIXI objects width/height
*/
public rescaleToYoga: boolean = false;
private _width: YogaSize;
private _height: YogaSize;
private _cachedLayout: ComputedLayout | undefined;
private _lastRecalculationDuration = 0;
/**
* Will be recalculated in next frame
*/
private _needUpdateAsRoot: boolean = false;
/**
* Used instead of Yoga.AspectRatio because of Yoga issue https://github.com/facebook/yoga/issues/677
*/
private _aspectRatio: number;
constructor(pixiObject: DisplayObject = new DisplayObject()) {
this.node = Yoga.Node.create();
pixiObject.__hasYoga = true;
this.fillDefaults();
this.target = pixiObject;
if ((<any>this.target)._texture) {
this.width = this.height = "pixi";
} else {
this.width = this.height = "auto";
}
// broadcast event
pixiObject.on(YogaLayout.LAYOUT_UPDATED_EVENT as any, () => {
this._cachedLayout = undefined;
this.children.forEach(child => child.target.emit(YogaLayout.LAYOUT_UPDATED_EVENT))
})
pixiObject.on(YogaLayout.NEED_LAYOUT_UPDATE as any, () => {
// size change of this element wont change size/positions of its parent, so there is no need to update whole tree
if (!this.parent || (this.hasContantDeclaredSize && this.parent.width !== "auto" && this.parent.height !== "auto")) {
this._needUpdateAsRoot = true;
} else {
this.parent.target.emit(YogaLayout.NEED_LAYOUT_UPDATE)
}
})
}
public set root(val: string) {
const root = YogaLayout.roots.get(val);
if (root) {
root.addChild(this);
}
}
/**
* Assigns given properties to this yoga layout
* @param config
*/
public fromConfig(config: YogaLayoutConfig) {
Object.assign(this, config);
}
/**
* Same as 'fromConfig()'
* @param config
*/
public set config(config: YogaLayoutConfig) {
this.fromConfig(config);
}
/**
* Copies all properties (styles, size, rescaleToYoga etc) from other YogaLayout objects
* @param layout
*/
public copy(layout: YogaLayout): void {
this.node.copyStyle(layout.node);
this.rescaleToYoga = layout.rescaleToYoga;
this.aspectRatio = layout.aspectRatio;
this._width = layout._width;
this._height = layout._height;
}
public fillDefaults() {
this.node.setFlexDirection(Yoga.FLEX_DIRECTION_ROW);
this.node.setAlignItems(Yoga.ALIGN_FLEX_START);
this.node.setAlignContent(Yoga.ALIGN_FLEX_START)
this.node.setWidth("auto");
this.node.setHeight("auto");
}
public addChild(yoga: YogaLayout, index = this.node.getChildCount()): void {
if (yoga.parent) {
yoga.parent.removeChild(yoga);
}
this.node.insertChild(yoga.node, index)
this.children.splice(index, 0, yoga);
yoga.parent = this;
}
public removeChild(yoga: YogaLayout): void {
this.children = this.children.filter(child => child === yoga);
this.node.removeChild(yoga.node);
yoga.parent = undefined;
}
/**
* Mark object as dirty and request layout recalculation
*/
public requestLayoutUpdate(): void {
this.target.emit(YogaLayout.NEED_LAYOUT_UPDATE);
}
public recalculateLayout(): void {
const start = performance.now()
this.node.calculateLayout();
this._lastRecalculationDuration = performance.now() - start;
// console.log("recalculated: ", this._lastRecalculationDuration, this)
this.target.emit(YogaLayout.LAYOUT_UPDATED_EVENT);
}
public update(): void {
if (this._needUpdateAsRoot && !this.parent) {
this.recalculateLayout();
}
this._needUpdateAsRoot = false;
}
public get isRoot(): boolean {
return !this.parent;
}
/**
* Returns true if object size is independent of its children sizes.
*/
public get hasContantDeclaredSize(): boolean {
return !!this._width && this._width !== "pixi" && this._width !== "auto"
&& !!this._height && this._height !== "pixi" && this._height !== "auto";
}
public get computedLayout(): ComputedLayout {
if (!this._cachedLayout) {
this._cachedLayout = this.node.getComputedLayout();
// YOGA FIX for percent widht/height for absolute positioned elements
if (this.position === "absolute" && this.parent && this.node.getWidth().unit === Yoga.UNIT_PERCENT) {
this._cachedLayout.width = Math.round(parseFloat(this._width as string) / 100 * this.parent.calculatedWidth)
}
if (this.position === "absolute" && this.parent && this.node.getHeight().unit === Yoga.UNIT_PERCENT) {
this._cachedLayout.height = Math.round(parseFloat(this._height as string) / 100 * this.parent.calculatedHeight)
}
// YOGA FIX for not working aspect ratio https://github.com/facebook/yoga/issues/677
if (this._aspectRatio) {
const newHeight = this.calculatedWidth / this._aspectRatio;
this._cachedLayout.top += (this.calculatedHeight - newHeight) / 2;
this._cachedLayout.height = newHeight;
this.height = this.calculatedHeight;
}
}
return this._cachedLayout;
}
public set aspectRatio(value: number) {
if (this._aspectRatio === value) {
return;
}
this._aspectRatio = value;
this.requestLayoutUpdate();
}
public get aspectRatio(): number {
return this._aspectRatio;
}
public get isWidthCalculatedFromPixi(): boolean {
return this._width === "pixi"
}
public get isHeightCalculatedFromPixi(): boolean {
return this._height === "pixi"
}
/**
* Returns computed width in pixels
*/
public get calculatedWidth(): number {
return this._cachedLayout ? this._cachedLayout.width : this.node.getComputedWidth();
}
/**
* Returns computed height in pixels
*/
public get calculatedHeight(): number {
return this._cachedLayout ? this._cachedLayout.height : this.node.getComputedHeight();
}
/**
* Can handle:
* - pixels (eg 150)
* - percents ("50%")
* - "auto" to use values from yoga
* - "pixi" to use DisplayObject.width/height
* @param value
*/
public set width(value: YogaSize) {
this._width = value;
if (value !== "pixi") {
this.node.setWidth(value);
}
this.requestLayoutUpdate();
}
public get width(): YogaSize {
return this._parseValue(this.node.getWidth());
}
/**
* Can handle:
* - pixels (eg 150)
* - percents ("50%")
* - "auto" to use values from yoga
* - "pixi" to use DisplayObject.width/height
* @param value
*/
public set height(value: YogaSize) {
this._height = value;
if (value !== "pixi") {
this.node.setHeight(value);
}
this.requestLayoutUpdate();
}
public get height(): YogaSize {
return this._parseValue(this.node.getHeight());
}
public set flexDirection(direction: keyof typeof FlexDirection) {
this.node.setFlexDirection(<Yoga.YogaFlexDirection>YogaConstants.FlexDirection[direction]);
this.requestLayoutUpdate();
}
public get flexDirection(): keyof typeof FlexDirection {
return YogaConstants.FlexDirection[this.node.getFlexDirection()] as any;
}
public set justifyContent(just: keyof typeof JustifyContent) {
this.node.setJustifyContent(<Yoga.YogaJustifyContent>YogaConstants.JustifyContent[just]);
this.requestLayoutUpdate();
}
public get justifyContent(): keyof typeof JustifyContent {
return YogaConstants.JustifyContent[this.node.getJustifyContent()] as any;
}
public set alignContent(align: keyof typeof Align) {
this.node.setAlignContent(<Yoga.YogaAlign>YogaConstants.Align[align]);
this.requestLayoutUpdate();
}
public get alignContent(): keyof typeof Align {
return YogaConstants.Align[this.node.getAlignContent()] as any;
}
public set alignItems(align: keyof typeof Align) {
this.node.setAlignItems(<Yoga.YogaAlign>YogaConstants.Align[align]);
this.requestLayoutUpdate();
}
public get alignItems(): keyof typeof Align {
// @ts-ignore
return YogaConstants.Align[this.node.getAlignItems()];
}
public set alignSelf(align: keyof typeof Align) {
this.node.setAlignSelf(<Yoga.YogaAlign>YogaConstants.Align[align]);
this.requestLayoutUpdate();
}
public get alignSelf(): keyof typeof Align {
// @ts-ignore
return YogaConstants.Align[this.node.getAlignSelf()];
}
public set flexWrap(wrap: keyof typeof FlexWrap) {
this.node.setFlexWrap(<Yoga.YogaFlexWrap>YogaConstants.FlexWrap[wrap]);
this.requestLayoutUpdate();
}
public get flexWrap(): keyof typeof FlexWrap {
// @ts-ignore
return YogaConstants.FlexWrap[this.node.getFlexWrap()];
}
public set flexGrow(grow: number) {
this.node.setFlexGrow(grow)
this.requestLayoutUpdate();
}
public get flexGrow(): number {
return this.node.getFlexGrow();
}
public set flexShrink(shrink: number) {
this.node.setFlexShrink(shrink);
this.requestLayoutUpdate();
}
public get flexShrink(): number {
return this.node.getFlexShrink();
}
public set flexBasis(basis: number) {
this.node.setFlexBasis(basis);
this.requestLayoutUpdate();
}
public get flexBasis(): number {
return this.node.getFlexBasis();
}
public set position(type: keyof typeof PositionType) {
this.node.setPositionType(<Yoga.YogaPositionType>YogaConstants.PositionType[type]);
this.requestLayoutUpdate();
}
public get position(): keyof typeof PositionType {
// @ts-ignore
return YogaConstants.PositionType[this.node.getPositionType()];
}
public set padding(margin: number[]) {
YogaEdges.forEach((edge, index) => {
const value = margin[index];
this.node.setPadding(edge, value)
})
this.requestLayoutUpdate();
}
public get padding(): number[] {
return YogaEdges.map(edge => this.node.getPadding(edge).value || 0);
}
public set paddingAll(value: number) {
this.padding = [value, value, value, value];
}
public set paddingTop(value: number) {
this.node.setPadding(Yoga.EDGE_TOP, value)
this.requestLayoutUpdate();
}
public get paddingTop(): number {
return this.node.getPadding(Yoga.EDGE_TOP).value || 0;
}
public set paddingBottom(value: number) {
this.node.setPadding(Yoga.EDGE_BOTTOM, value)
this.requestLayoutUpdate();
}
public get paddingBottom(): number {
return this.node.getPadding(Yoga.EDGE_BOTTOM).value || 0;
}
public set paddingLeft(value: number) {
this.node.setPadding(Yoga.EDGE_LEFT, value)
this.requestLayoutUpdate();
}
public get paddingLeft(): number {
return this.node.getPadding(Yoga.EDGE_LEFT).value || 0;
}
public set paddingRight(value: number) {
this.node.setPadding(Yoga.EDGE_RIGHT, value)
this.requestLayoutUpdate();
}
public get paddingRight(): number {
return this.node.getPadding(Yoga.EDGE_RIGHT).value || 0;
}
public set margin(margin: number[]) {
YogaEdges.forEach((edge, index) => {
const value = margin[index];
this.node.setMargin(edge, value)
})
this.requestLayoutUpdate();
}
public set marginAll(value: number) {
this.margin = [value, value, value, value];
}
public get margin(): number[] {
return YogaEdges.map(edge => this.node.getMargin(edge).value || 0);
}
public set marginTop(value: number) {
this.node.setMargin(Yoga.EDGE_TOP, value)
this.requestLayoutUpdate();
}
public get marginTop(): number {
return this.node.getMargin(Yoga.EDGE_TOP).value || 0;
}
public set marginBottom(value: number) {
this.node.setMargin(Yoga.EDGE_BOTTOM, value)
this.requestLayoutUpdate();
}
public get marginBottom(): number {
return this.node.getMargin(Yoga.EDGE_BOTTOM).value || 0;
}
public set marginLeft(value: number) {
this.node.setMargin(Yoga.EDGE_LEFT, value)
this.requestLayoutUpdate();
}
public get marginLeft(): number {
return this.node.getMargin(Yoga.EDGE_LEFT).value || 0;
}
public set marginRight(value: number) {
this.node.setMargin(Yoga.EDGE_RIGHT, value)
this.requestLayoutUpdate();
}
public get marginRight(): number {
return this.node.getMargin(Yoga.EDGE_RIGHT).value || 0;
}
public set border(margin: number[]) {
YogaEdges.forEach((edge, index) => {
const value = margin[index];
this.node.setBorder(edge, value)
})
this.requestLayoutUpdate();
}
public get border(): number[] {
return YogaEdges.map(edge => this.node.getBorder(edge));
}
public set borderAll(value: number) {
this.border = [value, value, value, value];
}
public set borderTop(value: number) {
this.node.setBorder(Yoga.EDGE_TOP, value)
this.requestLayoutUpdate();
}
public get borderTop(): number {
return this.node.getBorder(Yoga.EDGE_TOP);
}
public set borderBottom(value: number) {
this.node.setBorder(Yoga.EDGE_BOTTOM, value)
this.requestLayoutUpdate();
}
public get borderBottom(): number {
return this.node.getBorder(Yoga.EDGE_BOTTOM);
}
public set borderLeft(value: number) {
this.node.setBorder(Yoga.EDGE_LEFT, value)
this.requestLayoutUpdate();
}
public get bordereft(): number {
return this.node.getBorder(Yoga.EDGE_LEFT);
}
public set borderRight(value: number) {
this.node.setBorder(Yoga.EDGE_RIGHT, value)
this.requestLayoutUpdate();
}
public get borderRight(): number {
return this.node.getBorder(Yoga.EDGE_RIGHT);
}
public set top(value: PixelsOrPercentage) {
this.node.setPosition(Yoga.EDGE_TOP, value)
this.requestLayoutUpdate();
}
public get top(): PixelsOrPercentage {
return this._parseValue(this.node.getPosition(Yoga.EDGE_TOP));
}
public set bottom(value: PixelsOrPercentage) {
this.node.setPosition(Yoga.EDGE_BOTTOM, value)
this.requestLayoutUpdate();
}
public get bottom(): PixelsOrPercentage {
return this._parseValue(this.node.getPosition(Yoga.EDGE_BOTTOM));
}
public set left(value: PixelsOrPercentage) {
this.node.setPosition(Yoga.EDGE_LEFT, value)
this.requestLayoutUpdate();
}
public get left(): PixelsOrPercentage {
return this._parseValue(this.node.getPosition(Yoga.EDGE_LEFT));
}
public set right(value: PixelsOrPercentage) {
this.node.setPosition(Yoga.EDGE_RIGHT, value)
this.requestLayoutUpdate();
}
public get right(): PixelsOrPercentage {
return this._parseValue(this.node.getPosition(Yoga.EDGE_RIGHT));
}
public set minWidth(value: PixelsOrPercentage) {
this.node.setMinWidth(value);
this.requestLayoutUpdate();
}
public get minWidth(): PixelsOrPercentage {
return this._parseValue(this.node.getMinWidth())
}
public set minHeight(value: PixelsOrPercentage) {
this.node.setMinHeight(value);
this.requestLayoutUpdate();
}
public get minHeight(): PixelsOrPercentage {
return this._parseValue(this.node.getMinHeight())
}
public set maxWidth(value: PixelsOrPercentage) {
this.node.setMaxWidth(value);
this.requestLayoutUpdate();
}
public get maxWidth(): PixelsOrPercentage {
return this._parseValue(this.node.getMaxWidth())
}
public set maxHeight(value: PixelsOrPercentage) {
this.node.setMaxHeight(value);
this.requestLayoutUpdate();
}
public get maxHeight(): PixelsOrPercentage {
return this._parseValue(this.node.getMaxHeight())
}
public set display(value: keyof typeof Display) {
this.node.setDisplay(<Yoga.YogaDisplay>YogaConstants.Display[value]);
this.requestLayoutUpdate();
}
public get display(): keyof typeof Display {
// @ts-ignore
return Display[this.node.getDisplay()];
}
private _parseValue(value: { unit: any, value: any }): PixelsOrPercentage {
if (value.unit === Yoga.UNIT_POINT) {
return parseFloat(value.value)
}
if (value.unit === Yoga.UNIT_PERCENT) {
return value.value.toString() + "%";
}
if (value.unit === Yoga.UNIT_AUTO) {
return "auto";
}
return undefined as any;
}
}