/
widget.ts
1077 lines (972 loc) · 32.9 KB
/
widget.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) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* @packageDocumentation
* @module ui
*/
import { Component } from '../../core/components';
import { UITransform } from '../../core/components/ui-base/ui-transform';
import { ccclass, help, executeInEditMode, executionOrder, menu, requireComponent, tooltip, type, editorOnly, editable, serializable, visible } from 'cc.decorator';
import { Size, Vec3 } from '../../core/math';
import { errorID, warnID } from '../../core/platform/debug';
import { SystemEventType } from '../../core/platform/event-manager/event-enum';
import { View } from '../../core/platform/view';
import visibleRect from '../../core/platform/visible-rect';
import { Scene } from '../../core/scene-graph';
import { Node } from '../../core/scene-graph/node';
import { ccenum } from '../../core/value-types/enum';
import { TransformBit } from '../../core/scene-graph/node-enum';
import { EDITOR, DEV } from 'internal:constants';
import { legacyCC } from '../../core/global-exports';
const _zeroVec3 = new Vec3();
// returns a readonly size of the node
export function getReadonlyNodeSize (parent: Node | Scene) {
if (parent instanceof Scene) {
// @ts-ignore
if (EDITOR) {
// const canvasComp = parent.getComponentInChildren(Canvas);
if (!View.instance) {
throw new Error('cc.view uninitiated');
}
return View.instance.getDesignResolutionSize();
}
return visibleRect;
}
else if (parent._uiProps.uiTransformComp) {
return parent._uiProps.uiTransformComp.contentSize;
}
else {
return Size.ZERO;
}
}
export function computeInverseTransForTarget (widgetNode: Node, target: Node, out_inverseTranslate: Vec3, out_inverseScale: Vec3) {
let scale = widgetNode.parent ? widgetNode.parent.getScale() : _zeroVec3;
let scaleX = scale.x;
let scaleY = scale.y;
let translateX = 0;
let translateY = 0;
for (let node = widgetNode.parent; ;) {
if (!node) {
// ERROR: widgetNode should be child of target
out_inverseTranslate.x = out_inverseTranslate.y = 0;
out_inverseScale.x = out_inverseScale.y = 1;
return;
}
const pos = node.getPosition();
translateX += pos.x;
translateY += pos.y;
node = node.parent; // loop increment
if (node !== target) {
scale = node ? node.getScale() : _zeroVec3;
const sx = scale.x;
const sy = scale.y;
translateX *= sx;
translateY *= sy;
scaleX *= sx;
scaleY *= sy;
} else {
break;
}
}
out_inverseScale.x = scaleX !== 0 ? (1 / scaleX) : 1;
out_inverseScale.y = scaleY !== 0 ? (1 / scaleY) : 1;
out_inverseTranslate.x = -translateX;
out_inverseTranslate.y = -translateY;
}
/**
* @en Enum for Widget's alignment mode, indicating when the widget should refresh.
*
* @zh Widget 的对齐模式,表示 Widget 应该何时刷新。
*/
export enum AlignMode {
/**
* @en Only align once when the Widget is enabled for the first time.
* This will allow the script or animation to continue controlling the current node.
* It will only be aligned once before the end of frame when onEnable is called,then immediately disables the Widget.
*
* @zh 仅在 Widget 第一次激活时对齐一次,便于脚本或动画继续控制当前节点。<br/>
* 开启后会在 onEnable 时所在的那一帧结束前对齐一次,然后立刻禁用该 Widget。
*/
ONCE = 0,
/**
* @en Keep aligning all the way.
*
* @zh 始终保持对齐。
*/
ALWAYS = 1,
/**
* @zh
* 一开始会像 ONCE 一样对齐一次,之后每当窗口大小改变时还会重新对齐。
*/
ON_WINDOW_RESIZE = 2,
}
ccenum(AlignMode);
/**
* @en Enum for Widget's alignment flag, indicating when the widget select alignment.
*
* @zh Widget 的对齐标志,表示 Widget 选择对齐状态。
*/
export enum AlignFlags {
/**
* @en Align top.
*
* @zh 上边对齐。
*/
TOP = 1 << 0,
/**
* @en Align middle.
*
* @zh 垂直中心对齐。
*/
MID = 1 << 1,
/**
* @en Align bottom.
*
* @zh 下边对齐。
*/
BOT = 1 << 2,
/**
* @en Align left.
*
* @zh 左边对齐。
*/
LEFT = 1 << 3,
/**
* @en Align center.
*
* @zh 横向中心对齐。
*/
CENTER = 1 << 4,
/**
* @en Align right.
*
* @zh 右边对齐。
*/
RIGHT = 1 << 5,
/**
* @en Align horizontal.
*
* @zh 横向对齐。
*/
HORIZONTAL = LEFT | CENTER | RIGHT,
/**
* @en Align vertical.
*
* @zh 纵向对齐。
*/
VERTICAL = TOP | MID | BOT,
}
const TOP_BOT = AlignFlags.TOP | AlignFlags.BOT;
const LEFT_RIGHT = AlignFlags.LEFT | AlignFlags.RIGHT;
/**
* @en
* Stores and manipulate the anchoring based on its parent.
* Widget are used for GUI but can also be used for other things.
* Widget will adjust current node's position and size automatically,
* but the results after adjustment can not be obtained until the next frame unless you call [[updateAlignment]] manually.
*
* @zh Widget 组件,用于设置和适配其相对于父节点的边距,Widget 通常被用于 UI 界面,也可以用于其他地方。<br/>
* Widget 会自动调整当前节点的坐标和宽高,不过目前调整后的结果要到下一帧才能在脚本里获取到,除非你先手动调用 [[updateAlignment]]。
*/
@ccclass('cc.Widget')
@help('i18n:cc.Widget')
@executionOrder(110)
@menu('UI/Widget')
@requireComponent(UITransform)
@executeInEditMode
export class Widget extends Component {
/**
* @en
* Specifies an alignment target that can only be one of the parent nodes of the current node.
* The default value is null, and when null, indicates the current parent.
*
* @zh
* 指定一个对齐目标,只能是当前节点的其中一个父节点,默认为空,为空时表示当前父节点。
*/
@type(Node)
@tooltip('对齐目标')
get target () {
return this._target;
}
set target (value) {
if (this._target === value) {
return;
}
this._unregisterTargetEvents();
this._target = value;
this._registerTargetEvents();
if (EDITOR /*&& !cc.engine._isPlaying*/ && this.node.parent) {
// adjust the offsets to keep the size and position unchanged after target changed
legacyCC._widgetManager.updateOffsetsToStayPut(this);
}
this._validateTargetInDEV();
this._recursiveDirty();
}
/**
* @en
* Whether to align to the top.
*
* @zh
* 是否对齐上边。
*/
@tooltip('是否对齐上边')
get isAlignTop () {
return (this._alignFlags & AlignFlags.TOP) > 0;
}
set isAlignTop (value) {
this._setAlign(AlignFlags.TOP, value);
this._recursiveDirty();
}
/**
* @en
* Whether to align to the bottom.
*
* @zh
* 是否对齐下边。
*/
@tooltip('是否对齐下边')
get isAlignBottom () {
return (this._alignFlags & AlignFlags.BOT) > 0;
}
set isAlignBottom (value) {
this._setAlign(AlignFlags.BOT, value);
this._recursiveDirty();
}
/**
* @en
* Whether to align to the left.
*
* @zh
* 是否对齐左边。
*/
@tooltip('是否对齐左边')
get isAlignLeft () {
return (this._alignFlags & AlignFlags.LEFT) > 0;
}
set isAlignLeft (value) {
this._setAlign(AlignFlags.LEFT, value);
this._recursiveDirty();
}
/**
* @en
* Whether to align to the right.
*
* @zh
* 是否对齐右边。
*/
@tooltip('是否对齐右边')
get isAlignRight () {
return (this._alignFlags & AlignFlags.RIGHT) > 0;
}
set isAlignRight (value) {
this._setAlign(AlignFlags.RIGHT, value);
this._recursiveDirty();
}
/**
* @en
* Whether to align vertically.
*
* @zh
* 是否垂直方向对齐中点,开启此项会将垂直方向其他对齐选项取消。
*/
@tooltip('是否垂直方向对齐中点,开启此项会将垂直方向其他对齐选项取消')
get isAlignVerticalCenter () {
return (this._alignFlags & AlignFlags.MID) > 0;
}
set isAlignVerticalCenter (value) {
if (value) {
this.isAlignTop = false;
this.isAlignBottom = false;
this._alignFlags |= AlignFlags.MID;
} else {
this._alignFlags &= ~AlignFlags.MID;
}
this._recursiveDirty();
}
/**
* @en
* Whether to align horizontally.
*
* @zh
* 是否水平方向对齐中点,开启此选项会将水平方向其他对齐选项取消。
*/
@tooltip('是否水平方向对齐中点,开启此选项会将水平方向其他对齐选项取消')
get isAlignHorizontalCenter () {
return (this._alignFlags & AlignFlags.CENTER) > 0;
}
set isAlignHorizontalCenter (value) {
if (value) {
this.isAlignLeft = false;
this.isAlignRight = false;
this._alignFlags |= AlignFlags.CENTER;
} else {
this._alignFlags &= ~AlignFlags.CENTER;
}
this._recursiveDirty();
}
/**
* @en
* Whether to stretch horizontally, when enable the left and right alignment will be stretched horizontally,
* the width setting is invalid (read only).
*
* @zh
* 当前是否水平拉伸。当同时启用左右对齐时,节点将会被水平拉伸。此时节点的宽度(只读)。
*/
@visible(false)
get isStretchWidth () {
return (this._alignFlags & LEFT_RIGHT) === LEFT_RIGHT;
}
/**
* @en
* Whether to stretch vertically, when enable the left and right alignment will be stretched vertically,
* then height setting is invalid (read only).
*
* @zh
* 当前是否垂直拉伸。当同时启用上下对齐时,节点将会被垂直拉伸,此时节点的高度(只读)。
*/
@visible(false)
get isStretchHeight () {
return (this._alignFlags & TOP_BOT) === TOP_BOT;
}
// ALIGN MARGINS
/**
* @en
* The margins between the top of this node and the top of parent node,
* the value can be negative, Only available in 'isAlignTop' open.
*
* @zh
* 本节点顶边和父节点顶边的距离,可填写负值,只有在 isAlignTop 开启时才有作用。
*/
get top () {
return this._top;
}
set top (value) {
this._top = value;
this._recursiveDirty();
}
/**
* @EditorOnly Not for user
*/
@editable
get editorTop () {
return this._isAbsTop ? this._top : (this._top * 100);
}
set editorTop (value) {
this._top = this._isAbsTop ? value : (value / 100);
this._recursiveDirty();
}
/**
* @en
* The margins between the bottom of this node and the bottom of parent node,
* the value can be negative, Only available in 'isAlignBottom' open.
*
* @zh
* 本节点底边和父节点底边的距离,可填写负值,只有在 isAlignBottom 开启时才有作用。
*/
get bottom () {
return this._bottom;
}
set bottom (value) {
this._bottom = value;
this._recursiveDirty();
}
/**
* @EditorOnly Not for user
*/
@editable
get editorBottom () {
return this._isAbsBottom ? this._bottom : (this._bottom * 100);
}
set editorBottom (value) {
this._bottom = this._isAbsBottom ? value : (value / 100);
this._recursiveDirty();
}
/**
* @en
* The margins between the left of this node and the left of parent node,
* the value can be negative, Only available in 'isAlignLeft' open.
*
* @zh
* 本节点左边和父节点左边的距离,可填写负值,只有在 isAlignLeft 开启时才有作用。
*/
get left () {
return this._left;
}
set left (value) {
this._left = value;
this._recursiveDirty();
}
/**
* @EditorOnly Not for user
*/
@editable
get editorLeft () {
return this._isAbsLeft ? this._left : (this._left * 100);
}
set editorLeft (value) {
this._left = this._isAbsLeft ? value : (value / 100);
this._recursiveDirty();
}
/**
* @en
* The margins between the right of this node and the right of parent node,
* the value can be negative, Only available in 'isAlignRight' open.
*
* @zh
* 本节点右边和父节点右边的距离,可填写负值,只有在 isAlignRight 开启时才有作用。
*/
get right () {
return this._right;
}
set right (value) {
this._right = value;
this._recursiveDirty();
}
/**
* @EditorOnly Not for user
*/
@editable
get editorRight () {
return this._isAbsRight ? this._right : (this._right * 100);
}
set editorRight (value) {
this._right = this._isAbsRight ? value : (value / 100);
this._recursiveDirty();
}
/**
* @en
* Horizontally aligns the midpoint offset value,
* the value can be negative, Only available in 'isAlignHorizontalCenter' open.
*
* @zh
* 水平居中的偏移值,可填写负值,只有在 isAlignHorizontalCenter 开启时才有作用。
*/
get horizontalCenter () {
return this._horizontalCenter;
}
set horizontalCenter (value) {
this._horizontalCenter = value;
this._recursiveDirty();
}
/**
* @EditorOnly Not for user
*/
@editable
get editorHorizontalCenter () {
return this._isAbsHorizontalCenter ? this._horizontalCenter : (this._horizontalCenter * 100);
}
set editorHorizontalCenter (value) {
this._horizontalCenter = this._isAbsHorizontalCenter ? value : (value / 100);
this._recursiveDirty();
}
/**
* @en
* Vertically aligns the midpoint offset value,
* the value can be negative, Only available in 'isAlignVerticalCenter' open.
*
* @zh
* 垂直居中的偏移值,可填写负值,只有在 isAlignVerticalCenter 开启时才有作用。
*/
get verticalCenter () {
return this._verticalCenter;
}
set verticalCenter (value) {
this._verticalCenter = value;
this._recursiveDirty();
}
/**
* @EditorOnly Not for user
*/
@editable
get editorVerticalCenter () {
return this._isAbsVerticalCenter ? this._verticalCenter : (this._verticalCenter * 100);
}
set editorVerticalCenter (value) {
this._verticalCenter = this._isAbsVerticalCenter ? value : (value / 100);
this._recursiveDirty();
}
/**
* @en
* If true, top is pixel margin, otherwise is percentage (0 - 1) margin relative to the parent's height.
*
* @zh
* 如果为 true,"top" 将会以像素作为边距,否则将会以相对父物体高度的比例(0 到 1)作为边距。
*/
@editable
get isAbsoluteTop () {
return this._isAbsTop;
}
set isAbsoluteTop (value) {
if (this._isAbsTop === value) {
return;
}
this._isAbsTop = value;
this._autoChangedValue(AlignFlags.TOP, this._isAbsTop);
}
/**
* @en
* If true, bottom is pixel margin, otherwise is percentage (0 - 1) margin relative to the parent's height.
*
* @zh
* 如果为 true,"bottom" 将会以像素作为边距,否则将会以相对父物体高度的比例(0 到 1)作为边距。
*/
@editable
get isAbsoluteBottom () {
return this._isAbsBottom;
}
set isAbsoluteBottom (value) {
if (this._isAbsBottom === value) {
return;
}
this._isAbsBottom = value;
this._autoChangedValue(AlignFlags.BOT, this._isAbsBottom);
}
/**
* @en
* If true, left is pixel margin, otherwise is percentage (0 - 1) margin relative to the parent's width.
*
* @zh
* 如果为 true,"left" 将会以像素作为边距,否则将会以相对父物体宽度的比例(0 到 1)作为边距。
*/
@editable
get isAbsoluteLeft () {
return this._isAbsLeft;
}
set isAbsoluteLeft (value) {
if (this._isAbsLeft === value) {
return;
}
this._isAbsLeft = value;
this._autoChangedValue(AlignFlags.LEFT, this._isAbsLeft);
}
/**
* @en
* If true, right is pixel margin, otherwise is percentage (0 - 1) margin relative to the parent's width.
*
* @zh
* 如果为 true,"right" 将会以像素作为边距,否则将会以相对父物体宽度的比例(0 到 1)作为边距。
*/
@editable
get isAbsoluteRight () {
return this._isAbsRight;
}
set isAbsoluteRight (value) {
if (this._isAbsRight === value) {
return;
}
this._isAbsRight = value;
this._autoChangedValue(AlignFlags.RIGHT, this._isAbsRight);
}
/**
* @en
* If true, horizontalCenter is pixel margin, otherwise is percentage (0 - 1) margin.
*
* @zh
* 如果为 true,"horizontalCenter" 将会以像素作为偏移值,反之为比例(0 到 1)。
*/
@editable
get isAbsoluteHorizontalCenter () {
return this._isAbsHorizontalCenter;
}
set isAbsoluteHorizontalCenter (value) {
if (this._isAbsHorizontalCenter === value) {
return;
}
this._isAbsHorizontalCenter = value;
this._autoChangedValue(AlignFlags.CENTER, this._isAbsHorizontalCenter);
}
/**
* @en
* If true, verticalCenter is pixel margin, otherwise is percentage (0 - 1) margin.
*
* @zh
* 如果为 true,"verticalCenter" 将会以像素作为偏移值,反之为比例(0 到 1)。
*/
@editable
get isAbsoluteVerticalCenter () {
return this._isAbsVerticalCenter;
}
set isAbsoluteVerticalCenter (value) {
if (this._isAbsVerticalCenter === value) {
return;
}
this._isAbsVerticalCenter = value;
this._autoChangedValue(AlignFlags.MID, this._isAbsVerticalCenter);
}
/**
* @en
* Specifies the alignment mode of the Widget, which determines when the widget should refresh.
*
* @zh
* 指定 Widget 的对齐模式,用于决定 Widget 应该何时刷新。
*
* @example
* ```
* import { Widget } from 'cc';
* widget.alignMode = Widget.AlignMode.ON_WINDOW_RESIZE;
* ```
*/
@type(AlignMode)
@tooltip('指定 widget 的对齐方式,用于决定运行时 widget 应何时更新')
get alignMode () {
return this._alignMode;
}
set alignMode (value) {
this._alignMode = value;
this._recursiveDirty();
}
/**
* @zh
* 对齐开关,由 AlignFlags 组成
*/
@editable
get alignFlags () {
return this._alignFlags;
}
set alignFlags (value) {
if (this._alignFlags === value) {
return;
}
this._alignFlags = value;
this._recursiveDirty();
}
public static AlignMode = AlignMode;
public _lastPos = new Vec3();
public _lastSize = new Size();
public _dirty = true;
@serializable
private _alignFlags = 0;
@serializable
private _target: Node | null = null;
@serializable
private _left = 0;
@serializable
private _right = 0;
@serializable
private _top = 0;
@serializable
private _bottom = 0;
@serializable
private _horizontalCenter = 0;
@serializable
private _verticalCenter = 0;
@serializable
private _isAbsLeft = true;
@serializable
private _isAbsRight = true;
@serializable
private _isAbsTop = true;
@serializable
private _isAbsBottom = true;
@serializable
private _isAbsHorizontalCenter = true;
@serializable
private _isAbsVerticalCenter = true;
// original size before align
@serializable
private _originalWidth = 0;
@serializable
private _originalHeight = 0;
@serializable
private _alignMode = AlignMode.ON_WINDOW_RESIZE;
@serializable
@editorOnly
private _lockFlags = 0;
/**
* @en
* Immediately perform the widget alignment. You need to manually call this method only if
* you need to get the latest results after the alignment before the end of current frame.
*
* @zh
* 立刻执行 widget 对齐操作。这个接口一般不需要手工调用。
* 只有当你需要在当前帧结束前获得 widget 对齐后的最新结果时才需要手动调用这个方法。
*
* @example
* ```ts
* import { log } from 'cc';
* widget.top = 10; // change top margin
* log(widget.node.y); // not yet changed
* widget.updateAlignment();
* log(widget.node.y); // changed
* ```
*/
public updateAlignment () {
legacyCC._widgetManager.updateAlignment(this.node as Node);
}
public _validateTargetInDEV () {
if (!DEV) {
return;
}
const target = this._target;
if (target) {
const isParent = this.node !== target && this.node.isChildOf(target);
if (!isParent) {
errorID(6500);
this.target = null;
}
}
}
public setDirty () {
this._recursiveDirty();
}
public onEnable () {
this.node.getPosition(this._lastPos);
this._lastSize.set(this.node._uiProps.uiTransformComp!.contentSize);
legacyCC._widgetManager.add(this);
this._registerEvent();
this._registerTargetEvents();
}
public onDisable () {
legacyCC._widgetManager.remove(this);
this._unregisterEvent();
this._unregisterTargetEvents();
}
public onDestroy () {
this._removeParentEvent();
}
public _adjustWidgetToAllowMovingInEditor (eventType: TransformBit) {
if (/*!EDITOR ||*/ !(eventType & TransformBit.POSITION)) {
return;
}
if (legacyCC._widgetManager.isAligning) {
return;
}
const self = this;
const newPos = self.node.getPosition();
const oldPos = this._lastPos;
const delta = new Vec3(newPos);
delta.subtract(oldPos);
let target = self.node.parent;
const inverseScale = new Vec3(1, 1, 1);
if (self.target) {
target = self.target;
computeInverseTransForTarget(self.node, target, new Vec3(), inverseScale);
}
if (!target) {
return;
}
const targetSize = getReadonlyNodeSize(target);
const deltaInPercent = new Vec3();
if (targetSize.width !== 0 && targetSize.height !== 0) {
Vec3.set(deltaInPercent, delta.x / targetSize.width, delta.y / targetSize.height, deltaInPercent.z);
}
if (self.isAlignTop) {
self._top -= (self._isAbsTop ? delta.y : deltaInPercent.y) * inverseScale.y;
}
if (self.isAlignBottom) {
self._bottom += (self._isAbsBottom ? delta.y : deltaInPercent.y) * inverseScale.y;
}
if (self.isAlignLeft) {
self._left += (self._isAbsLeft ? delta.x : deltaInPercent.x) * inverseScale.x;
}
if (self.isAlignRight) {
self._right -= (self._isAbsRight ? delta.x : deltaInPercent.x) * inverseScale.x;
}
if (self.isAlignHorizontalCenter) {
self._horizontalCenter += (self._isAbsHorizontalCenter ? delta.x : deltaInPercent.x) * inverseScale.x;
}
if (self.isAlignVerticalCenter) {
self._verticalCenter += (self._isAbsVerticalCenter ? delta.y : deltaInPercent.y) * inverseScale.y;
}
this._recursiveDirty();
}
public _adjustWidgetToAllowResizingInEditor () {
// if (!EDITOR) {
// return;
// }
if (legacyCC._widgetManager.isAligning) {
return;
}
this.setDirty();
const self = this;
const trans = self.node._uiProps.uiTransformComp!;
const newSize = trans.contentSize;
const oldSize = this._lastSize;
const delta = new Vec3(newSize.width - oldSize.width, newSize.height - oldSize.height, 0);
let target = self.node.parent;
const inverseScale = new Vec3(1, 1, 1);
if (self.target) {
target = self.target;
computeInverseTransForTarget(self.node, target, new Vec3(), inverseScale);
}
if (!target) {
return;
}
const targetSize = getReadonlyNodeSize(target);
const deltaInPercent = new Vec3();
if (targetSize.width !== 0 && targetSize.height !== 0) {
Vec3.set(deltaInPercent, delta.x / targetSize.width, delta.y / targetSize.height, deltaInPercent.z);
}
const anchor = trans.anchorPoint;
if (self.isAlignTop) {
self._top -= (self._isAbsTop ? delta.y : deltaInPercent.y) * (1 - anchor.y) * inverseScale.y;
}
if (self.isAlignBottom) {
self._bottom -= (self._isAbsBottom ? delta.y : deltaInPercent.y) * anchor.y * inverseScale.y;
}
if (self.isAlignLeft) {
self._left -= (self._isAbsLeft ? delta.x : deltaInPercent.x) * anchor.x * inverseScale.x;
}
if (self.isAlignRight) {
self._right -= (self._isAbsRight ? delta.x : deltaInPercent.x) * (1 - anchor.x) * inverseScale.x;
}
this._recursiveDirty();
}
public _adjustWidgetToAnchorChanged () {
this.setDirty();
}
public _adjustTargetToParentChanged (oldParent: Node) {
if (oldParent) {
this._unregisterOldParentEvents(oldParent);
}
if (this.node.getParent()) {
this._registerTargetEvents();
}
}
protected _registerEvent () {
this.node.on(SystemEventType.TRANSFORM_CHANGED, this._adjustWidgetToAllowMovingInEditor, this);
this.node.on(SystemEventType.SIZE_CHANGED, this._adjustWidgetToAllowResizingInEditor, this);
this.node.on(SystemEventType.ANCHOR_CHANGED, this._adjustWidgetToAnchorChanged, this);
this.node.on(SystemEventType.PARENT_CHANGED, this._adjustTargetToParentChanged, this);
}
protected _unregisterEvent () {
this.node.off(SystemEventType.TRANSFORM_CHANGED, this._adjustWidgetToAllowMovingInEditor, this);
this.node.off(SystemEventType.SIZE_CHANGED, this._adjustWidgetToAllowResizingInEditor, this);
this.node.off(SystemEventType.ANCHOR_CHANGED, this._adjustWidgetToAnchorChanged, this);
}
protected _removeParentEvent () {
this.node.off(SystemEventType.PARENT_CHANGED, this._adjustTargetToParentChanged, this);
}
protected _autoChangedValue (flag: AlignFlags, isAbs: boolean) {
const current = (this._alignFlags & flag) > 0;
const parentTrans = this.node.parent && this.node.parent._uiProps.uiTransformComp;
if (!current || !parentTrans) {
return;
}
const size = parentTrans.contentSize;
if (this.isAlignLeft && flag === AlignFlags.LEFT) {
this._left = isAbs ? this._left * size.width : this._left / size.width;
} else if (this.isAlignRight && flag === AlignFlags.RIGHT) {
this._right = isAbs ? this._right * size.width : this._right / size.width;
} else if (this.isAlignHorizontalCenter && flag === AlignFlags.CENTER) {
this._horizontalCenter = isAbs ? this._horizontalCenter * size.width : this._horizontalCenter / size.width;
} else if (this.isAlignTop && flag === AlignFlags.TOP) {
this._top = isAbs ? this._top * size.height : this._top / size.height;
} else if (this.isAlignBottom && flag === AlignFlags.BOT) {
this._bottom = isAbs ? this._bottom * size.height : this._bottom / size.height;
} else if (this.isAbsoluteVerticalCenter && flag === AlignFlags.MID) {
this._verticalCenter = isAbs ? this._verticalCenter / size.height : this._verticalCenter / size.height;
}
this._recursiveDirty();
}
protected _registerTargetEvents () {
const target = this._target || this.node.parent;
if (target) {
if (target.getComponent(UITransform)) {
target.on(SystemEventType.TRANSFORM_CHANGED, this._targetChangedOperation, this);
target.on(SystemEventType.SIZE_CHANGED, this._targetChangedOperation, this);
} else {
warnID(6501, this.node.name);
}
}
}
protected _unregisterTargetEvents () {
const target = this._target || this.node.parent;
if (target) {
target.off(SystemEventType.TRANSFORM_CHANGED, this._targetChangedOperation, this);
target.off(SystemEventType.SIZE_CHANGED, this._targetChangedOperation, this);
}
}
protected _unregisterOldParentEvents (oldParent: Node) {
const target = this._target || oldParent;
if (target) {
target.off(SystemEventType.TRANSFORM_CHANGED, this._targetChangedOperation, this);
target.off(SystemEventType.SIZE_CHANGED, this._targetChangedOperation, this);
}