-
Notifications
You must be signed in to change notification settings - Fork 18
/
d-base.ts
2147 lines (1885 loc) · 49.6 KB
/
d-base.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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2019 Toshiba Corporation
* SPDX-License-Identifier: Apache-2.0
*/
import {
Container,
DisplayObject,
InteractionEvent,
InteractionManager,
Point,
Rectangle,
Renderer,
Texture,
Transform
} from "pixi.js";
import { DApplications } from "./d-applications";
import { DBackgroundStateAware } from "./d-background";
import { DBaseBackground } from "./d-base-background";
import { DBaseBorder } from "./d-base-border";
import { DBaseCorner } from "./d-base-corner";
import { DBaseInteractive } from "./d-base-interactive";
import { DBaseOutline } from "./d-base-outline";
import { DBasePadding } from "./d-base-padding";
import { DBasePoint } from "./d-base-point";
import { DBaseReflowableImpl } from "./d-base-reflowable-impl";
import { DBaseStateSet } from "./d-base-state-set";
import { DBaseStateSetImplObservable } from "./d-base-state-set-impl-observable";
import { DBorderStateAware } from "./d-border";
import { DBorderMask } from "./d-border-mask";
import { DCoordinatePosition, DCoordinateSize } from "./d-coordinate";
import { DCorner } from "./d-corner";
import { DCornerMask } from "./d-corner-mask";
import { DThemeFont } from "./d-font";
import { DLayoutClearType } from "./d-layout-clear-type";
import { DOutline } from "./d-outline";
import { DPadding } from "./d-padding";
import { DScalarFunctions } from "./d-scalar-functions";
import { DScalarSet } from "./d-scalar-set";
import { DShadow } from "./d-shadow";
import { DStateAwareOrValueMightBe } from "./d-state-aware";
import { DThemes } from "./theme/d-themes";
import { isFunction } from "./util/is-function";
import { isNumber } from "./util/is-number";
import { isString } from "./util/is-string";
import { UtilKeyboardEvent, UtilKeyboardEventShortcut } from "./util/util-keyboard-event";
import { UtilPointerEvent } from "./util/util-pointer-event";
import { UtilWheelEventDeltas } from "./util/util-wheel-event";
import { DBaseAutoSet } from "./d-base-auto-set";
import { toEnum } from "./util/to-enum";
import { DOnOptions } from "./d-on-options";
import { DBaseSnippetContainer } from "./d-base-snippet-container";
import { DBaseReflowableContainer } from "./d-base-reflowable-container";
import { isArray } from "./util/is-array";
/**
* {@link DBase} padding options.
*/
export interface DBasePaddingOptions {
/** A top padding */
top?: number;
/** A right padding */
right?: number;
/** A bottom padding */
bottom?: number;
/** A left padding */
left?: number;
}
/**
* {@link DBase} corner options.
*/
export interface DBaseCornerOptions {
/** A radius */
radius?: number;
/** Masked corners get unrounded. */
mask?: keyof typeof DCornerMask | DCornerMask;
}
/**
* {@link DBase} events.
*/
export interface DBaseEvents<EMITTER> {
/**
* Triggered when an initialization is finished.
*
* @param emitter an emitter
*/
init(emitter: EMITTER): void;
/**
* Triggered when added to a container.
*
* @param container a container added to
*/
added(container: Container): void;
/**
* Triggered when removed from a container.
*
* @param container a container removed from
*/
removed(container: Container): void;
/**
* Triggered when moved.
*
* @param newX a new x
* @param newY a new y
* @param oldX an old x
* @param oldY an old y
* @param emitter an emitter
*/
move(newX: number, newY: number, oldX: number, oldY: number, emitter: EMITTER): void;
/**
* Triggered when resized.
*
* @param newWidth a new width
* @param newHeight a new height
* @param oldWidth an old width
* @param oldHeight an old height
* @param emitter an emitter
*/
resize(
newWidth: number,
newHeight: number,
oldWidth: number,
oldHeight: number,
emitter: EMITTER
): void;
/**
* Triggered when scaled.
*
* @param newX a new x
* @param newY a new y
* @param oldX an old x
* @param oldY an old y
* @param emitter an emitter
*/
scale(newX: number, newY: number, oldX: number, oldY: number, emitter: EMITTER): void;
/**
* Triggered when skewed.
*
* @param newX a new x
* @param newY a new y
* @param oldX an old x
* @param oldY an old y
* @param emitter an emitter
*/
skew(newX: number, newY: number, oldX: number, oldY: number, emitter: EMITTER): void;
/**
* Triggered when a state is changed.
*
* @param newState a new state
* @param oldState an old state
* @param emitter an emitter
*/
statechange(newState: DBaseStateSet, oldState: DBaseStateSet, emitter: EMITTER): void;
/**
* Triggered when a wheel moves.
*
* @param e a wheel event
* @param deltas wheel move amounts
* @param global a point wheel moved
* @param emitter an emitter
*/
wheel(e: WheelEvent, deltas: UtilWheelEventDeltas, global: Point, emitter: EMITTER): void;
/**
* Triggered when a key gets down.
*
* @param e a keyboard event
* @param emitter an emitter
*/
keydown(e: KeyboardEvent, emitter: EMITTER): void;
/**
* Triggered when a key gets up.
*
* @param e a keyboard event
* @param emitter an emitter
*/
keyup(e: KeyboardEvent, emitter: EMITTER): void;
/**
* Triggered when a pointer gets on an emitter.
*
* @param e an interaction event.
* @param emitter an emitter
*/
over(e: InteractionEvent, emitter: EMITTER): void;
/**
* Triggered when a pointer gets out of an emitter.
*
* @param e an interaction event
* @param emitter an emitter
*/
out(e: InteractionEvent, emitter: EMITTER): void;
/**
* Triggered when a pointer gets down on an emitter.
*
* @param e an interaction event.
* @param emitter an emitter
*/
down(e: InteractionEvent, emitter: EMITTER): void;
/**
* Triggered when a pointer gets up on an emitter.
*
* @param e an interaction event
* @param emitter an emitter
*/
up(e: InteractionEvent, emitter: EMITTER): void;
/**
* Triggered when an emitter is clicked.
*
* @param e an interaction event
* @param emitter an emitter
*/
click(e: InteractionEvent, emitter: EMITTER): void;
/**
* Triggered when an emitter is double clicked.
*
* @param e an event
* @param interactionManager an interaction manager
* @param emitter an emitter
*/
dblclick(
e: MouseEvent | TouchEvent,
interactionManager: InteractionManager,
emitter: EMITTER
): void;
/**
* Triggered when a shortcut is pressed.
*
* @param e an event
* @param emitter an emitter
*/
shortcut(e: KeyboardEvent, emitter: EMITTER): void;
}
/**
* {@link DBase} "on" options.
*/
export interface DBaseOnOptions<EMITTER> extends Partial<DBaseEvents<EMITTER>>, DOnOptions {}
/**
* {@link DBase} background options.
*/
export interface DBaseBackgroundOptions {
/**
* A color code or a function returning a color code.
* If a computed value is undefined, falls back to the theme color.
* If a computed value is null, a background is not rendered.
*/
color?: DStateAwareOrValueMightBe<number | null>;
/**
* An alpha or a function returning an alpha.
* If a computed value is undefined, falls back to an background alpha of a theme.
*/
alpha?: DStateAwareOrValueMightBe<number>;
}
/**
* {@link DBase} border options.
*/
export interface DBaseBorderOptions {
/**
* A color code or a function returning a color code.
* If a computed value is undefined, falls back to the theme color.
* If a computed value is null, a background is not rendered.
*/
color?: DStateAwareOrValueMightBe<number | null>;
/**
* An alpha or a function returning an alpha.
* If a computed value is undefined, falls back to a border alpha of a theme.
*/
alpha?: DStateAwareOrValueMightBe<number>;
/**
* A width or a function returning a width.
* If a computed value is undefined, falls back to a border width of a theme.
*/
width?: DStateAwareOrValueMightBe<number>;
/**
* An align or a function returning an align.
* If a computed value is undefined, falls back to a border align of a theme.
* If an align is 0, a border is rendered completely inside of a {@link DBase}.
* If an align is 1, a border is rendered completely outside of a {@link DBase}.
*/
align?: DStateAwareOrValueMightBe<number>;
/** Masked borders get removed. */
mask?: DStateAwareOrValueMightBe<DBorderMask> | keyof typeof DBorderMask;
}
/**
* {@link DBase} outline optons.
*/
export interface DBaseOutlineOptions {
/**
* A color code or a function returning a color code.
* If a computed value is undefined, falls back to the theme color.
* If a computed value is null, a background is not rendered.
*/
color?: DStateAwareOrValueMightBe<number | null>;
/**
* An alpha or a function returning an alpha.
* If a computed value is undefined, falls back to an outline alpha of a theme.
*/
alpha?: DStateAwareOrValueMightBe<number>;
/**
* A width or a function returning a width.
* If a computed value is undefined, falls back to an outline width of a theme.
*/
width?: DStateAwareOrValueMightBe<number>;
/**
* An offset or a function returning an offset.
* If a computed value is undefined, falls back to an outline align of a theme.
* A outline moves to outside when an offset gets larger.
*/
offset?: DStateAwareOrValueMightBe<number>;
/**
* An align or a function returning an align.
* If a computed value is undefined, falls back to an outline align of a theme.
* If an align is 0, an outline is rendered completely inside.
* If an align is 1, an outline is rendered completely outside.
*/
align?: DStateAwareOrValueMightBe<number>;
/** Masked outlines get removed. */
mask?: DStateAwareOrValueMightBe<DBorderMask> | keyof typeof DBorderMask;
}
export type DBaseShadow = "NONE" | "WEAK" | "DEFAULT" | DShadow;
/**
* {@link DBase} state blinker options.
*/
export interface DBaseStateBlinkerOptions {
state: string;
interval: number;
}
/**
* {@link DBase} state ticker options.
*/
export interface DBaseStateTickerOptions {
state: string;
interval: number;
}
/**
* {@link DBase} state options.
*/
export interface DBaseStateOptions {
values?: string | string[];
blinkers?: DBaseStateBlinkerOptions[];
tickers?: DBaseStateTickerOptions[];
}
/**
* {@link DBase} options.
*/
export interface DBaseOptions<THEME extends DThemeBase = DThemeBase, EMITTER = any> {
/**
* A parent.
*
* In the case of UI classes which pop up (e.g., {@link DDialog} and {@link DMenu}),
* if multiple application instances are there, better to set
* this to an `application.stage` so that they pick a right application.
* By default, they assume the last created application is
* the one they belong to at the time when they are created.
*/
parent?: Container;
/** Children. */
children?: Array<DisplayObject | null>;
/** A name. */
name?: string;
/**
* One of the followings:
* * A X position
* * A position keyword
* * A position expression (Parsed by {@link DScalarExpression})
* * A function returning a X position ({@link DScalarFunction})
* * An object returning a X position ({@link DScalar})
*/
x?: DCoordinatePosition;
/**
* One of the followings:
* * A Y position
* * A position keyword
* * A position expresion (Parsed by {@link DScalarExpression})
* * A function returning a Y position ({@link DScalarFunction})
* * An object returning a Y position ({@link DScalar})
*/
y?: DCoordinatePosition;
/**
* One of the followings:
* * A width
* * A size keyword
* * A size expression (Parsed by {@link DScalarExpression})
* * A function returning a width ({@link DScalarFunction})
* * An object returning a width ({@link DScalar})
*/
width?: DCoordinateSize;
/**
* One of the followings:
* * A height
* * A size keyword
* * A size expression (Parsed by {@link DScalarExpression})
* * A function returning a hight ({@link DScalarFunction})
* * An object returning a hight ({@link DScalar})
*/
height?: DCoordinateSize;
/**
* A visibility.
* Set to true to make {@link DBase} visible.
* Set to false to make {@link DBase} invisible.
* The default value is true.
*/
visible?: boolean;
/**
* A renderability.
* Unlike {@link visible}, non-renderable element occupies a space.
* Set to true to make {@link DBase} renderable.
* Set to false to make {@link DBase} not renderable.
* The default value is true.
*/
renderable?: boolean;
/** A default state. */
state?: string | string[] | DBaseStateOptions;
/** An interactivity option. */
interactive?: keyof typeof DBaseInteractive | DBaseInteractive;
/** A padding options. */
padding?: number | DBasePaddingOptions;
/** A corner options. */
corner?: number | DBaseCornerOptions;
/** A theme or a theme name. */
theme?: THEME | string;
/**
* Mappings of event names and event handlers.
*/
on?: DBaseOnOptions<EMITTER>;
/**
* A weight used by {@link DLayoutVertical} and {@link DLayoutHorizontal}.
*/
weight?: number;
/** A tooltip text. */
title?: string;
/** A shortcut option. */
shortcut?: string | UtilKeyboardEventShortcut;
/** A shortcut options. */
shortcuts?: Array<string | UtilKeyboardEventShortcut>;
/** A background options. */
background?: DBaseBackgroundOptions;
/** A border options. */
border?: DBaseBorderOptions;
/** An outline options. */
outline?: DBaseOutlineOptions;
/** A shadow. */
shadow?: DBaseShadow | null;
/** A clear type used by {@link DLayoutVertical} and {@link DLayoutHorizontal}. */
clear?: keyof typeof DLayoutClearType | DLayoutClearType;
/** A cursor shape. */
cursor?: DStateAwareOrValueMightBe<string>;
}
/**
* {@link DBase} theme.
*/
export interface DThemeBase extends DThemeFont {
/**
* Returns a X coordinate.
*/
getX(): DCoordinatePosition;
/**
* Returns a Y coordinate.
*/
getY(): DCoordinatePosition;
/**
* Returns a height.
*/
getHeight(): DCoordinateSize;
/**
* Returns a width.
*/
getWidth(): DCoordinateSize;
/**
* Returns a background color.
* If the color is null, backgrounds are not be rendered.
*
* @param state a state
*/
getBackgroundColor(state: DBaseStateSet): number | null;
/**
* Returns a background alpha.
*
* @param state a state
*/
getBackgroundAlpha(state: DBaseStateSet): number;
/**
* Returns a background texture.
*/
getBackgroundTexture(): Texture;
/**
* Returns a border color.
* If the color is null, borders are not be rendered.
*
* @param state a state
*/
getBorderColor(state: DBaseStateSet): number | null;
/**
* Returns a border alpha.
*
* @param state a state
*/
getBorderAlpha(state: DBaseStateSet): number;
/**
* Returns a border width.
*
* @param state a state
*/
getBorderWidth(state: DBaseStateSet): number;
/**
* Returns a border align.
*
* @param state a state
*/
getBorderAlign(state: DBaseStateSet): number;
/**
* Returns a border mask.
*
* @param state a mask
*/
getBorderMask(state: DBaseStateSet): DBorderMask;
/**
* Returns a border texture.
*/
getBorderTexture(): Texture;
/**
* Returns a left padding.
*/
getPaddingLeft(): number;
/**
* Returns a right padding.
*/
getPaddingRight(): number;
/**
* Returns a top padding.
*/
getPaddingTop(): number;
/**
* Returns a bottom padding.
*/
getPaddingBottom(): number;
/**
* Returns a corner radius.
*/
getCornerRadius(): number;
/**
* Returns a corner mask.
*/
getCornerMask(): DCornerMask;
/**
* Returns an outline color.
* If the color is null, outlines are not be rendered.
*
* @param state a state
*/
getOutlineColor(state: DBaseStateSet): number | null;
/**
* Returns an outline alpha.
*
* @param state a state
*/
getOutlineAlpha(state: DBaseStateSet): number;
/**
* Returns an outline width.
*
* @param state a state
*/
getOutlineWidth(state: DBaseStateSet): number;
/**
* Returns an outline offset.
*
* @param state a state
*/
getOutlineOffset(state: DBaseStateSet): number;
/**
* Returns an outline align.
*
* @param state a state
*/
getOutlineAlign(state: DBaseStateSet): number;
/**
* Returns an outline mask.
*
* @param state a state
*/
getOutlineMask(state: DBaseStateSet): DBorderMask;
/**
* Returns a shadow.
* If a shadow is null, no shadow is rendered.
*/
getShadow(): DShadow | null;
/**
* Returns an interactivity.
*/
getInteractive(): DBaseInteractive;
/**
* Returns true if focusable.
*/
newState(state: DBaseStateSet): void;
/**
* Returns a tooltip text.
*/
getTitle(): string;
/**
* Returns a clear type.
* A clear type is for layout classes including {@link DLayoutVertical}.
*/
getClearType(): DLayoutClearType;
/**
* Returns a weight.
* Weights are for layout classes including {@link DLayoutVertical}.
* If a weight is less than or equals to zero, layout classes are supposed not to change a width / height.
*/
getWeight(): number;
/**
* Creates a new shadow.
*/
newShadow(): DShadow | null;
/**
* Creates a new weak shadow.
*/
newShadowWeak(): DShadow | null;
/**
* Returns a cursor.
*/
getCursor(state: DBaseStateSet): string;
}
const toTheme = <THEME extends DThemeBase>(options?: DBaseOptions<THEME>): THEME | undefined => {
const theme = options?.theme;
if (theme) {
if (isString(theme)) {
return DThemes.getInstance().get(theme);
}
return theme;
}
};
export interface DRefitable {
readonly width: number;
readonly height: number;
}
const toShortcuts = <THEME extends DThemeBase>(
options?: DBaseOptions<THEME>
): UtilKeyboardEventShortcut[] | undefined => {
if (options) {
const shortcut = options.shortcut;
const shortcuts = options.shortcuts;
if (shortcuts != null || shortcut != null) {
const result: UtilKeyboardEventShortcut[] = [];
if (shortcut != null) {
result.push(UtilKeyboardEvent.toShortcut(shortcut));
}
if (shortcuts != null) {
for (let i = 0, imax = shortcuts.length; i < imax; ++i) {
result.push(UtilKeyboardEvent.toShortcut(shortcuts[i]));
}
}
return result;
}
}
return undefined;
};
/**
* A base class for UI classes.
* See {@link DBaseEvents} for event details.
*/
export class DBase<
THEME extends DThemeBase = DThemeBase,
OPTIONS extends DBaseOptions<THEME> = DBaseOptions<THEME>
> extends Container {
protected static WORK_CONTAINS_POINT: Point = new Point();
private _state: DBaseStateSet;
private _theme: THEME;
protected _options: OPTIONS | undefined;
private _width: number;
private _height: number;
protected _padding: DPadding;
protected _corner: DCorner;
private _scalarSet: DScalarSet;
protected _auto: DBaseAutoSet;
private _isDirty: boolean;
private _hasDirty: boolean;
private _isHierarchyDirty: boolean;
private _shadow: DShadow | null;
private _onShadowUpdateBound: () => void;
private _weight: number;
private _position: DBasePoint;
private _scale: DBasePoint;
private _skew: DBasePoint;
protected _title: string;
protected _background: DBackgroundStateAware;
protected _border: DBorderStateAware;
protected _outline: DOutline;
protected _clearType: DLayoutClearType;
protected _shortcuts?: UtilKeyboardEventShortcut[];
protected _snippet: DBaseSnippetContainer;
protected _reflowable: DBaseReflowableContainer;
protected _lastDownPoint?: Point;
protected _cursor?: DStateAwareOrValueMightBe<string>;
constructor(options?: OPTIONS) {
super();
// Transform
const transform: Transform = this.transform;
this._position = new DBasePoint(transform.position, (newX, newY, oldX, oldY): void => {
this.onMove(newX, newY, oldX, oldY);
});
this._scale = new DBasePoint(transform.scale, (newX, newY, oldX, oldY): void => {
this.onScale(newX, newY, oldX, oldY);
});
this._skew = new DBasePoint(transform.skew, (newX, newY, oldX, oldY): void => {
this.onSkew(newX, newY, oldX, oldY);
});
//
this._options = options;
const scalarSet: DScalarSet = (this._scalarSet = {});
this._auto = new DBaseAutoSet();
this._isDirty = true;
this._hasDirty = false;
this._isHierarchyDirty = false;
this._shadow = null;
this.name = options?.name ?? "";
const theme = toTheme(options) || this.getThemeDefault();
this._theme = theme;
this._snippet = new DBaseSnippetContainer(this);
this._reflowable = new DBaseReflowableContainer();
this._clearType = toEnum(options?.clear ?? theme.getClearType(), DLayoutClearType);
this._padding = this.newPadding(theme, options, (): void => {
this.toDirty();
this.toParentResized();
this.toHierarchyDirty();
DApplications.update(this);
});
const toDirtyAndUpdate = (): void => {
this.toDirty();
DApplications.update(this);
};
this._background = new DBaseBackground(theme, options, toDirtyAndUpdate);
this._border = new DBaseBorder(theme, options, toDirtyAndUpdate);
this._outline = new DBaseOutline(theme, options, toDirtyAndUpdate);
this._corner = new DBaseCorner(theme, options, toDirtyAndUpdate);
// X
const position = transform.position;
const x = options?.x ?? theme.getX();
if (isNumber(x)) {
position.x = x;
} else {
position.x = 0;
scalarSet.x = DScalarFunctions.position(x);
}
// Y
const y = options?.y ?? theme.getY();
if (isNumber(y)) {
position.y = y;
} else {
position.y = 0;
scalarSet.y = DScalarFunctions.position(y);
}
// Width
const width = options?.width ?? theme.getWidth();
if (!this._auto.width.from(width)) {
if (isNumber(width)) {
this._width = width;
} else {
this._width = 100;
scalarSet.width = DScalarFunctions.size(width);
}
} else {
this._width = 100;
}
// Height
const height = options?.height ?? theme.getHeight();
if (!this._auto.height.from(height)) {
if (isNumber(height)) {
this._height = height;
} else {
this._height = 100;
scalarSet.height = DScalarFunctions.size(height);
}
} else {
this._height = 100;
}
// Visibility
if (options != null) {
const visible = options.visible;
if (visible != null) {
this.visible = visible;
}
}
// Renderability
if (options != null) {
const renderable = options.renderable;
if (renderable != null) {
this.renderable = renderable;
}
}
// State
this._state = new DBaseStateSetImplObservable((newState, oldState): void => {
this.onStateChange(newState, oldState);
});
// Interactive
const interactive = toEnum(
options?.interactive ?? theme.getInteractive(),
DBaseInteractive
);
this.interactive = !!(interactive & DBaseInteractive.SELF);
this.interactiveChildren = !!(interactive & DBaseInteractive.CHILDREN);
// Events
if (options != null) {
const on = options.on;
if (on != null) {
for (const name in on) {
const handler = on[name];
if (handler) {
this.on(name, handler);
}
}
}
}
// Title
this._title = options?.title ?? theme.getTitle();
// Weight
this._weight = options?.weight ?? theme.getWeight();
// Reflowable
this.initReflowable();
// Shadow
this._onShadowUpdateBound = (): void => {
DApplications.update(this);
};
let shadow: DBaseShadow | null | undefined;
if (options != null) {
shadow = options.shadow;
}
if (shadow === undefined) {
shadow = theme.getShadow();
}
if (shadow) {
if (isString(shadow)) {
switch (shadow) {
case "WEAK":
this.shadow = theme.newShadowWeak();
break;
case "DEFAULT":
this.shadow = theme.newShadow();
break;
}
} else {
this.shadow = shadow;
}
}
// Event handlers
this.on(UtilPointerEvent.over, (e: InteractionEvent): void => {
this.onOver(e);
});
this.on(UtilPointerEvent.out, (e: InteractionEvent): void => {
this.onOut(e);
});
this.on(UtilPointerEvent.down, (e: InteractionEvent): void => {
this.onDown(e);
});
this.on(UtilPointerEvent.up, (e: InteractionEvent): void => {
this.onUp(e);
});
// Children change detection
this.on("added", (): void => {
this.toParentResized();
if (this.isDirty() || this.hasDirty()) {
this.toParentHasDirty();
}
if (this._isHierarchyDirty) {
this.toParentHierarchyDirty();
}
const newParent = this.parent;
if (newParent instanceof DBase) {
this.state.parent = newParent.state;
}
DApplications.update(this);
});
this.on("removed", (): void => {
this.blur(true);
this.state.parent = null;
DApplications.update(this);