/
CCLayout.js
998 lines (868 loc) · 35.7 KB
/
CCLayout.js
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
/****************************************************************************
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.
****************************************************************************/
const NodeEvent = require('../CCNode').EventType;
/**
* !#en Enum for Layout type
* !#zh 布局类型
* @enum Layout.Type
*/
var Type = cc.Enum({
/**
* !#en None Layout
* !#zh 取消布局
*@property {Number} NONE
*/
NONE: 0,
/**
* !#en Horizontal Layout
* !#zh 水平布局
* @property {Number} HORIZONTAL
*/
HORIZONTAL: 1,
/**
* !#en Vertical Layout
* !#zh 垂直布局
* @property {Number} VERTICAL
*/
VERTICAL: 2,
/**
* !#en Grid Layout
* !#zh 网格布局
* @property {Number} GRID
*/
GRID: 3,
});
/**
* !#en Enum for Layout Resize Mode
* !#zh 缩放模式
* @enum Layout.ResizeMode
*/
var ResizeMode = cc.Enum({
/**
* !#en Don't do any scale.
* !#zh 不做任何缩放
* @property {Number} NONE
*/
NONE: 0,
/**
* !#en The container size will be expanded with its children's size.
* !#zh 容器的大小会根据子节点的大小自动缩放。
* @property {Number} CONTAINER
*/
CONTAINER: 1,
/**
* !#en Child item size will be adjusted with the container's size.
* !#zh 子节点的大小会随着容器的大小自动缩放。
* @property {Number} CHILDREN
*/
CHILDREN: 2
});
/**
* !#en Enum for Grid Layout start axis direction.
* The items in grid layout will be arranged in each axis at first.;
* !#zh 布局轴向,只用于 GRID 布局。
* @enum Layout.AxisDirection
*/
var AxisDirection = cc.Enum({
/**
* !#en The horizontal axis.
* !#zh 进行水平方向布局
* @property {Number} HORIZONTAL
*/
HORIZONTAL: 0,
/**
* !#en The vertical axis.
* !#zh 进行垂直方向布局
* @property {Number} VERTICAL
*/
VERTICAL: 1,
});
/**
* !#en Enum for vertical layout direction.
* Used in Grid Layout together with AxisDirection is VERTICAL
* !#zh 垂直方向布局方式
* @enum Layout.VerticalDirection
*/
var VerticalDirection = cc.Enum({
/**
* !#en Items arranged from bottom to top.
* !#zh 从下到上排列
* @property {Number} BOTTOM_TO_TOP
*/
BOTTOM_TO_TOP: 0,
/**
* !#en Items arranged from top to bottom.
* !#zh 从上到下排列
* @property {Number} TOP_TO_BOTTOM
*/
TOP_TO_BOTTOM: 1,
});
/**
* !#en Enum for horizontal layout direction.
* Used in Grid Layout together with AxisDirection is HORIZONTAL
* !#zh 水平方向布局方式
* @enum Layout.HorizontalDirection
*/
var HorizontalDirection = cc.Enum({
/**
* !#en Items arranged from left to right.
* !#zh 从左往右排列
* @property {Number} LEFT_TO_RIGHT
*/
LEFT_TO_RIGHT: 0,
/**
* !#en Items arranged from right to left.
* !#zh 从右往左排列
*@property {Number} RIGHT_TO_LEFT
*/
RIGHT_TO_LEFT: 1,
});
/**
* !#en
* The Layout is a container component, use it to arrange child elements easily.<br>
* Note:<br>
* 1.Scaling and rotation of child nodes are not considered.<br>
* 2.After setting the Layout, the results need to be updated until the next frame,
* unless you manually call {{#crossLink "Layout/updateLayout:method"}}{{/crossLink}}。
* !#zh
* Layout 组件相当于一个容器,能自动对它的所有子节点进行统一排版。<br>
* 注意:<br>
* 1.不会考虑子节点的缩放和旋转。<br>
* 2.对 Layout 设置后结果需要到下一帧才会更新,除非你设置完以后手动调用 {{#crossLink "Layout/updateLayout:method"}}{{/crossLink}}。
* @class Layout
* @extends Component
*/
var Layout = cc.Class({
name: 'cc.Layout',
extends: require('./CCComponent'),
editor: CC_EDITOR && {
menu: 'i18n:MAIN_MENU.component.ui/Layout',
help: 'i18n:COMPONENT.help_url.layout',
inspector: 'packages://inspector/inspectors/comps/cclayout.js',
executeInEditMode: true,
},
properties: {
_layoutSize: cc.size(300, 200),
_layoutDirty: {
default: true,
serializable: false,
},
_resize: ResizeMode.NONE,
//TODO: refactoring this name after data upgrade machanism is out.
_N$layoutType: Type.NONE,
/**
* !#en The layout type.
* !#zh 布局类型
* @property {Layout.Type} type
* @default Layout.Type.NONE
*/
type: {
type: Type,
get: function () {
return this._N$layoutType;
},
set: function (value) {
this._N$layoutType = value;
if (CC_EDITOR && this.type !== Type.NONE && this._resize === ResizeMode.CONTAINER && !cc.engine.isPlaying) {
var reLayouted = _Scene.DetectConflict.checkConflict_Layout(this);
if (reLayouted) {
return;
}
}
this._doLayoutDirty();
},
tooltip: CC_DEV && 'i18n:COMPONENT.layout.layout_type',
animatable: false,
},
/**
* !#en
* The are three resize modes for Layout.
* None, resize Container and resize children.
* !#zh 缩放模式
* @property {Layout.ResizeMode} resizeMode
* @default ResizeMode.NONE
*/
resizeMode: {
type: ResizeMode,
tooltip: CC_DEV && 'i18n:COMPONENT.layout.resize_mode',
animatable: false,
get: function () {
return this._resize;
},
set: function (value) {
if (this.type === Type.NONE && value === ResizeMode.CHILDREN) {
return;
}
this._resize = value;
if (CC_EDITOR && value === ResizeMode.CONTAINER && !cc.engine.isPlaying) {
var reLayouted = _Scene.DetectConflict.checkConflict_Layout(this);
if (reLayouted) {
return;
}
}
this._doLayoutDirty();
},
},
/**
* !#en The cell size for grid layout.
* !#zh 每个格子的大小,只有布局类型为 GRID 的时候才有效。
* @property {Size} cellSize
* @default cc.size(40, 40)
*/
cellSize: {
default: cc.size(40, 40),
tooltip: CC_DEV && 'i18n:COMPONENT.layout.cell_size',
type: cc.Size,
notify: function () {
this._doLayoutDirty();
},
},
/**
* !#en
* The start axis for grid layout. If you choose horizontal, then children will layout horizontally at first,
* and then break line on demand. Choose vertical if you want to layout vertically at first .
* !#zh 起始轴方向类型,可进行水平和垂直布局排列,只有布局类型为 GRID 的时候才有效。
* @property {Layout.AxisDirection} startAxis
*/
startAxis: {
default: AxisDirection.HORIZONTAL,
tooltip: CC_DEV && 'i18n:COMPONENT.layout.start_axis',
type: AxisDirection,
notify: function () {
if (CC_EDITOR && this._resize === ResizeMode.CONTAINER && !cc.engine.isPlaying) {
var reLayouted = _Scene.DetectConflict.checkConflict_Layout(this);
if (reLayouted) {
return;
}
}
this._doLayoutDirty();
},
animatable: false
},
_N$padding: {
default: 0
},
/**
* !#en The left padding of layout, it only effect the layout in one direction.
* !#zh 容器内左边距,只会在一个布局方向上生效。
* @property {Number} paddingLeft
*/
paddingLeft: {
default: 0,
tooltip: CC_DEV && 'i18n:COMPONENT.layout.padding_left',
notify: function () {
this._doLayoutDirty();
},
},
/**
* !#en The right padding of layout, it only effect the layout in one direction.
* !#zh 容器内右边距,只会在一个布局方向上生效。
* @property {Number} paddingRight
*/
paddingRight: {
default: 0,
tooltip: CC_DEV && 'i18n:COMPONENT.layout.padding_right',
notify: function () {
this._doLayoutDirty();
},
},
/**
* !#en The top padding of layout, it only effect the layout in one direction.
* !#zh 容器内上边距,只会在一个布局方向上生效。
* @property {Number} paddingTop
*/
paddingTop: {
default: 0,
tooltip: CC_DEV && 'i18n:COMPONENT.layout.padding_top',
notify: function () {
this._doLayoutDirty();
},
},
/**
* !#en The bottom padding of layout, it only effect the layout in one direction.
* !#zh 容器内下边距,只会在一个布局方向上生效。
* @property {Number} paddingBottom
*/
paddingBottom: {
default: 0,
tooltip: CC_DEV && 'i18n:COMPONENT.layout.padding_bottom',
notify: function () {
this._doLayoutDirty();
},
},
/**
* !#en The distance in x-axis between each element in layout.
* !#zh 子节点之间的水平间距。
* @property {Number} spacingX
*/
spacingX: {
default: 0,
notify: function () {
this._doLayoutDirty();
},
tooltip: CC_DEV && 'i18n:COMPONENT.layout.space_x'
},
/**
* !#en The distance in y-axis between each element in layout.
* !#zh 子节点之间的垂直间距。
* @property {Number} spacingY
*/
spacingY: {
default: 0,
notify: function () {
this._doLayoutDirty();
},
tooltip: CC_DEV && 'i18n:COMPONENT.layout.space_y'
},
/**
* !#en
* Only take effect in Vertical layout mode.
* This option changes the start element's positioning.
* !#zh 垂直排列子节点的方向。
* @property {Layout.VerticalDirection} verticalDirection
*/
verticalDirection: {
default: VerticalDirection.TOP_TO_BOTTOM,
type: VerticalDirection,
notify: function () {
this._doLayoutDirty();
},
tooltip: CC_DEV && 'i18n:COMPONENT.layout.vertical_direction',
animatable: false
},
/**
* !#en
* Only take effect in Horizontal layout mode.
* This option changes the start element's positioning.
* !#zh 水平排列子节点的方向。
* @property {Layout.HorizontalDirection} horizontalDirection
*/
horizontalDirection: {
default: HorizontalDirection.LEFT_TO_RIGHT,
type: HorizontalDirection,
notify: function () {
this._doLayoutDirty();
},
tooltip: CC_DEV && 'i18n:COMPONENT.layout.horizontal_direction',
animatable: false
},
},
statics: {
Type: Type,
VerticalDirection: VerticalDirection,
HorizontalDirection: HorizontalDirection,
ResizeMode: ResizeMode,
AxisDirection: AxisDirection,
},
_migratePaddingData: function () {
this.paddingLeft = this._N$padding;
this.paddingRight = this._N$padding;
this.paddingTop = this._N$padding;
this.paddingBottom = this._N$padding;
this._N$padding = 0;
},
onEnable: function () {
this._addEventListeners();
if (this.node.getContentSize().equals(cc.size(0, 0))) {
this.node.setContentSize(this._layoutSize);
}
if (this._N$padding !== 0) {
this._migratePaddingData();
}
this._doLayoutDirty();
},
onDisable: function () {
this._removeEventListeners();
},
_doLayoutDirty: function () {
this._layoutDirty = true;
},
_addEventListeners: function () {
cc.director.on(cc.Director.EVENT_AFTER_UPDATE, this.updateLayout, this);
this.node.on(NodeEvent.SIZE_CHANGED, this._resized, this);
this.node.on(NodeEvent.ANCHOR_CHANGED, this._doLayoutDirty, this);
this.node.on(NodeEvent.CHILD_ADDED, this._childAdded, this);
this.node.on(NodeEvent.CHILD_REMOVED, this._childRemoved, this);
this.node.on(NodeEvent.CHILD_REORDER, this._doLayoutDirty, this);
this._addChildrenEventListeners();
},
_removeEventListeners: function () {
cc.director.off(cc.Director.EVENT_AFTER_UPDATE, this.updateLayout, this);
this.node.off(NodeEvent.SIZE_CHANGED, this._resized, this);
this.node.off(NodeEvent.ANCHOR_CHANGED, this._doLayoutDirty, this);
this.node.off(NodeEvent.CHILD_ADDED, this._childAdded, this);
this.node.off(NodeEvent.CHILD_REMOVED, this._childRemoved, this);
this.node.off(NodeEvent.CHILD_REORDER, this._doLayoutDirty, this);
this._removeChildrenEventListeners();
},
_addChildrenEventListeners: function () {
var children = this.node.children;
for (var i = 0; i < children.length; ++i) {
var child = children[i];
child.on(NodeEvent.SIZE_CHANGED, this._doLayoutDirty, this);
child.on(NodeEvent.POSITION_CHANGED, this._doLayoutDirty, this);
child.on(NodeEvent.ANCHOR_CHANGED, this._doLayoutDirty, this);
child.on('active-in-hierarchy-changed', this._doLayoutDirty, this);
}
},
_removeChildrenEventListeners: function () {
var children = this.node.children;
for (var i = 0; i < children.length; ++i) {
var child = children[i];
child.off(NodeEvent.SIZE_CHANGED, this._doLayoutDirty, this);
child.off(NodeEvent.POSITION_CHANGED, this._doLayoutDirty, this);
child.off(NodeEvent.ANCHOR_CHANGED, this._doLayoutDirty, this);
child.off('active-in-hierarchy-changed', this._doLayoutDirty, this);
}
},
_childAdded: function (child) {
child.on(NodeEvent.SIZE_CHANGED, this._doLayoutDirty, this);
child.on(NodeEvent.POSITION_CHANGED, this._doLayoutDirty, this);
child.on(NodeEvent.ANCHOR_CHANGED, this._doLayoutDirty, this);
child.on('active-in-hierarchy-changed', this._doLayoutDirty, this);
this._doLayoutDirty();
},
_childRemoved: function (child) {
child.off(NodeEvent.SIZE_CHANGED, this._doLayoutDirty, this);
child.off(NodeEvent.POSITION_CHANGED, this._doLayoutDirty, this);
child.off(NodeEvent.ANCHOR_CHANGED, this._doLayoutDirty, this);
child.off('active-in-hierarchy-changed', this._doLayoutDirty, this);
this._doLayoutDirty();
},
_resized: function () {
this._layoutSize = this.node.getContentSize();
this._doLayoutDirty();
},
_doLayoutHorizontally: function (baseWidth, rowBreak, fnPositionY, applyChildren) {
var layoutAnchor = this.node.getAnchorPoint();
var children = this.node.children;
var sign = 1;
var paddingX = this.paddingLeft;
var leftBoundaryOfLayout = -layoutAnchor.x * baseWidth;
if (this.horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT) {
sign = -1;
leftBoundaryOfLayout = (1 - layoutAnchor.x) * baseWidth;
paddingX = this.paddingRight;
}
var nextX = leftBoundaryOfLayout + sign * paddingX - sign * this.spacingX;
var rowMaxHeight = 0;
var tempMaxHeight = 0;
var secondMaxHeight = 0;
var row = 0;
var containerResizeBoundary = 0;
var maxHeightChildAnchorY = 0;
var activeChildCount = 0;
for (var i = 0; i < children.length; ++i) {
var child = children[i];
if (child.activeInHierarchy) {
activeChildCount++;
}
}
var newChildWidth = this.cellSize.width;
if (this.type !== Type.GRID && this.resizeMode === ResizeMode.CHILDREN) {
newChildWidth = (baseWidth - (this.paddingLeft + this.paddingRight) - (activeChildCount - 1) * this.spacingX) / activeChildCount;
}
for (var i = 0; i < children.length; ++i) {
var child = children[i];
if (!child.activeInHierarchy) {
continue;
}
//for resizing children
if (this._resize === ResizeMode.CHILDREN) {
child.width = newChildWidth / child.scaleX;
if (this.type === Type.GRID) {
child.height = this.cellSize.height / child.scaleY;
}
}
var anchorX = child.anchorX;
var childBoundingBoxWidth = child.width * child.scaleX;
var childBoundingBoxHeight = child.height * child.scaleY;
if (secondMaxHeight > tempMaxHeight) {
tempMaxHeight = secondMaxHeight;
}
if (childBoundingBoxHeight >= tempMaxHeight) {
secondMaxHeight = tempMaxHeight;
tempMaxHeight = childBoundingBoxHeight;
maxHeightChildAnchorY = child.getAnchorPoint().y;
}
if (this.horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT) {
anchorX = 1 - child.anchorX;
}
nextX = nextX + sign * anchorX * childBoundingBoxWidth + sign * this.spacingX;
var rightBoundaryOfChild = sign * (1 - anchorX) * childBoundingBoxWidth;
if (rowBreak) {
var rowBreakBoundary = nextX + rightBoundaryOfChild + sign * (sign > 0 ? this.paddingRight : this.paddingLeft);
var leftToRightRowBreak = this.horizontalDirection === HorizontalDirection.LEFT_TO_RIGHT && rowBreakBoundary > (1 - layoutAnchor.x) * baseWidth;
var rightToLeftRowBreak = this.horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT && rowBreakBoundary < -layoutAnchor.x * baseWidth;
if (leftToRightRowBreak || rightToLeftRowBreak) {
if (childBoundingBoxHeight >= tempMaxHeight) {
if (secondMaxHeight === 0) {
secondMaxHeight = tempMaxHeight;
}
rowMaxHeight += secondMaxHeight;
secondMaxHeight = tempMaxHeight;
}
else {
rowMaxHeight += tempMaxHeight;
secondMaxHeight = childBoundingBoxHeight;
tempMaxHeight = 0;
}
nextX = leftBoundaryOfLayout + sign * (paddingX + anchorX * childBoundingBoxWidth);
row++;
}
}
var finalPositionY = fnPositionY(child, rowMaxHeight, row);
if (baseWidth >= (childBoundingBoxWidth + this.paddingLeft + this.paddingRight)) {
if (applyChildren) {
child.setPosition(cc.v2(nextX, finalPositionY));
}
}
var signX = 1;
var tempFinalPositionY;
var topMarign = (tempMaxHeight === 0) ? childBoundingBoxHeight : tempMaxHeight;
if (this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM) {
containerResizeBoundary = containerResizeBoundary || this.node._contentSize.height;
signX = -1;
tempFinalPositionY = finalPositionY + signX * (topMarign * maxHeightChildAnchorY + this.paddingBottom);
if (tempFinalPositionY < containerResizeBoundary) {
containerResizeBoundary = tempFinalPositionY;
}
}
else {
containerResizeBoundary = containerResizeBoundary || -this.node._contentSize.height;
tempFinalPositionY = finalPositionY + signX * (topMarign * maxHeightChildAnchorY + this.paddingTop);
if (tempFinalPositionY > containerResizeBoundary) {
containerResizeBoundary = tempFinalPositionY;
}
}
nextX += rightBoundaryOfChild;
}
return containerResizeBoundary;
},
_getVerticalBaseHeight: function (children) {
var newHeight = 0;
var activeChildCount = 0;
if (this.resizeMode === ResizeMode.CONTAINER) {
for (var i = 0; i < children.length; ++i) {
var child = children[i];
if (child.activeInHierarchy) {
activeChildCount++;
newHeight += child.height * child.scaleY;
}
}
newHeight += (activeChildCount - 1) * this.spacingY + this.paddingBottom + this.paddingTop;
}
else {
newHeight = this.node.getContentSize().height;
}
return newHeight;
},
_doLayoutVertically: function (baseHeight, columnBreak, fnPositionX, applyChildren) {
var layoutAnchor = this.node.getAnchorPoint();
var children = this.node.children;
var sign = 1;
var paddingY = this.paddingBottom;
var bottomBoundaryOfLayout = -layoutAnchor.y * baseHeight;
if (this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM) {
sign = -1;
bottomBoundaryOfLayout = (1 - layoutAnchor.y) * baseHeight;
paddingY = this.paddingTop;
}
var nextY = bottomBoundaryOfLayout + sign * paddingY - sign * this.spacingY;
var columnMaxWidth = 0;
var tempMaxWidth = 0;
var secondMaxWidth = 0;
var column = 0;
var containerResizeBoundary = 0;
var maxWidthChildAnchorX = 0;
var activeChildCount = 0;
for (var i = 0; i < children.length; ++i) {
var child = children[i];
if (child.activeInHierarchy) {
activeChildCount++;
}
}
var newChildHeight = this.cellSize.height;
if (this.type !== Type.GRID && this.resizeMode === ResizeMode.CHILDREN) {
newChildHeight = (baseHeight - (this.paddingTop + this.paddingBottom) - (activeChildCount - 1) * this.spacingY) / activeChildCount;
}
for (var i = 0; i < children.length; ++i) {
var child = children[i];
if (!child.activeInHierarchy) {
continue;
}
//for resizing children
if (this.resizeMode === ResizeMode.CHILDREN) {
child.height = newChildHeight / child.scaleY;
if (this.type === Type.GRID) {
child.width = this.cellSize.width / child.scaleX;
}
}
var anchorY = child.anchorY;
var childBoundingBoxWidth = child.width * child.scaleX;
var childBoundingBoxHeight = child.height * child.scaleY;
if (secondMaxWidth > tempMaxWidth) {
tempMaxWidth = secondMaxWidth;
}
if (childBoundingBoxWidth >= tempMaxWidth) {
secondMaxWidth = tempMaxWidth;
tempMaxWidth = childBoundingBoxWidth;
maxWidthChildAnchorX = child.getAnchorPoint().x;
}
if (this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM) {
anchorY = 1 - child.anchorY;
}
nextY = nextY + sign * anchorY * childBoundingBoxHeight + sign * this.spacingY;
var topBoundaryOfChild = sign * (1 - anchorY) * childBoundingBoxHeight;
if (columnBreak) {
var columnBreakBoundary = nextY + topBoundaryOfChild + sign * (sign > 0 ? this.paddingTop : this.paddingBottom);
var bottomToTopColumnBreak = this.verticalDirection === VerticalDirection.BOTTOM_TO_TOP && columnBreakBoundary > (1 - layoutAnchor.y) * baseHeight;
var topToBottomColumnBreak = this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM && columnBreakBoundary < -layoutAnchor.y * baseHeight;
if (bottomToTopColumnBreak || topToBottomColumnBreak) {
if (childBoundingBoxWidth >= tempMaxWidth) {
if (secondMaxWidth === 0) {
secondMaxWidth = tempMaxWidth;
}
columnMaxWidth += secondMaxWidth;
secondMaxWidth = tempMaxWidth;
}
else {
columnMaxWidth += tempMaxWidth;
secondMaxWidth = childBoundingBoxWidth;
tempMaxWidth = 0;
}
nextY = bottomBoundaryOfLayout + sign * (paddingY + anchorY * childBoundingBoxHeight);
column++;
}
}
var finalPositionX = fnPositionX(child, columnMaxWidth, column);
if (baseHeight >= (childBoundingBoxHeight + (this.paddingTop + this.paddingBottom))) {
if (applyChildren) {
child.setPosition(cc.v2(finalPositionX, nextY));
}
}
var signX = 1;
var tempFinalPositionX;
//when the item is the last column break item, the tempMaxWidth will be 0.
var rightMarign = (tempMaxWidth === 0) ? childBoundingBoxWidth : tempMaxWidth;
if (this.horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT) {
signX = -1;
containerResizeBoundary = containerResizeBoundary || this.node._contentSize.width;
tempFinalPositionX = finalPositionX + signX * (rightMarign * maxWidthChildAnchorX + this.paddingLeft);
if (tempFinalPositionX < containerResizeBoundary) {
containerResizeBoundary = tempFinalPositionX;
}
}
else {
containerResizeBoundary = containerResizeBoundary || -this.node._contentSize.width;
tempFinalPositionX = finalPositionX + signX * (rightMarign * maxWidthChildAnchorX + this.paddingRight);
if (tempFinalPositionX > containerResizeBoundary) {
containerResizeBoundary = tempFinalPositionX;
}
}
nextY += topBoundaryOfChild;
}
return containerResizeBoundary;
},
_doLayoutBasic: function () {
var children = this.node.children;
var allChildrenBoundingBox = null;
for (var i = 0; i < children.length; ++i) {
var child = children[i];
if (child.activeInHierarchy) {
if (!allChildrenBoundingBox) {
allChildrenBoundingBox = child.getBoundingBoxToWorld();
} else {
allChildrenBoundingBox.union(allChildrenBoundingBox, child.getBoundingBoxToWorld());
}
}
}
if (allChildrenBoundingBox) {
var leftBottomInParentSpace = this.node.parent.convertToNodeSpaceAR(cc.v2(allChildrenBoundingBox.x, allChildrenBoundingBox.y));
leftBottomInParentSpace = cc.v2(leftBottomInParentSpace.x - this.paddingLeft, leftBottomInParentSpace.y - this.paddingBottom);
var rightTopInParentSpace = this.node.parent.convertToNodeSpaceAR(cc.v2(allChildrenBoundingBox.x + allChildrenBoundingBox.width,
allChildrenBoundingBox.y + allChildrenBoundingBox.height));
rightTopInParentSpace = cc.v2(rightTopInParentSpace.x + this.paddingRight, rightTopInParentSpace.y + this.paddingTop);
var newSize = cc.size(parseFloat((rightTopInParentSpace.x - leftBottomInParentSpace.x).toFixed(2)),
parseFloat((rightTopInParentSpace.y - leftBottomInParentSpace.y).toFixed(2)));
var layoutPosition = this.node.getPosition();
var newAnchorX = (layoutPosition.x - leftBottomInParentSpace.x) / newSize.width;
var newAnchorY = (layoutPosition.y - leftBottomInParentSpace.y) / newSize.height;
var newAnchor = cc.v2(parseFloat(newAnchorX.toFixed(2)), parseFloat(newAnchorY.toFixed(2)));
this.node.setAnchorPoint(newAnchor);
this.node.setContentSize(newSize);
}
},
_doLayoutGridAxisHorizontal: function (layoutAnchor, layoutSize) {
var baseWidth = layoutSize.width;
var sign = 1;
var bottomBoundaryOfLayout = -layoutAnchor.y * layoutSize.height;
var paddingY = this.paddingBottom;
if (this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM) {
sign = -1;
bottomBoundaryOfLayout = (1 - layoutAnchor.y) * layoutSize.height;
paddingY = this.paddingTop;
}
var fnPositionY = function (child, topOffset, row) {
return bottomBoundaryOfLayout + sign * (topOffset + child.anchorY * child.height * child.scaleY + paddingY + row * this.spacingY);
}.bind(this);
var newHeight = 0;
if (this.resizeMode === ResizeMode.CONTAINER) {
//calculate the new height of container, it won't change the position of it's children
var boundary = this._doLayoutHorizontally(baseWidth, true, fnPositionY, false);
newHeight = bottomBoundaryOfLayout - boundary;
if (newHeight < 0) {
newHeight *= -1;
}
bottomBoundaryOfLayout = -layoutAnchor.y * newHeight;
if (this.verticalDirection === VerticalDirection.TOP_TO_BOTTOM) {
sign = -1;
bottomBoundaryOfLayout = (1 - layoutAnchor.y) * newHeight;
}
}
this._doLayoutHorizontally(baseWidth, true, fnPositionY, true);
if (this.resizeMode === ResizeMode.CONTAINER) {
this.node.setContentSize(baseWidth, newHeight);
}
},
_doLayoutGridAxisVertical: function (layoutAnchor, layoutSize) {
var baseHeight = layoutSize.height;
var sign = 1;
var leftBoundaryOfLayout = -layoutAnchor.x * layoutSize.width;
var paddingX = this.paddingLeft;
if (this.horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT) {
sign = -1;
leftBoundaryOfLayout = (1 - layoutAnchor.x) * layoutSize.width;
paddingX = this.paddingRight;
}
var fnPositionX = function (child, leftOffset, column) {
return leftBoundaryOfLayout + sign * (leftOffset + child.anchorX * child.width * child.scaleX + paddingX + column * this.spacingX);
}.bind(this);
var newWidth = 0;
if (this.resizeMode === ResizeMode.CONTAINER) {
var boundary = this._doLayoutVertically(baseHeight, true, fnPositionX, false);
newWidth = leftBoundaryOfLayout - boundary;
if (newWidth < 0) {
newWidth *= -1;
}
leftBoundaryOfLayout = -layoutAnchor.x * newWidth;
if (this.horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT) {
sign = -1;
leftBoundaryOfLayout = (1 - layoutAnchor.x) * newWidth;
}
}
this._doLayoutVertically(baseHeight, true, fnPositionX, true);
if (this.resizeMode === ResizeMode.CONTAINER) {
this.node.setContentSize(newWidth, baseHeight);
}
},
_doLayoutGrid: function () {
var layoutAnchor = this.node.getAnchorPoint();
var layoutSize = this.node.getContentSize();
if (this.startAxis === AxisDirection.HORIZONTAL) {
this._doLayoutGridAxisHorizontal(layoutAnchor, layoutSize);
}
else if (this.startAxis === AxisDirection.VERTICAL) {
this._doLayoutGridAxisVertical(layoutAnchor, layoutSize);
}
},
_getHorizontalBaseWidth: function (children) {
var newWidth = 0;
var activeChildCount = 0;
if (this.resizeMode === ResizeMode.CONTAINER) {
for (var i = 0; i < children.length; ++i) {
var child = children[i];
if (child.activeInHierarchy) {
activeChildCount++;
newWidth += child.width * child.scaleX;
}
}
newWidth += (activeChildCount - 1) * this.spacingX + this.paddingLeft + this.paddingRight;
}
else {
newWidth = this.node.getContentSize().width;
}
return newWidth;
},
_doLayout: function () {
if (this.type === Type.HORIZONTAL) {
var newWidth = this._getHorizontalBaseWidth(this.node.children);
var fnPositionY = function (child) {
return child.y;
};
this._doLayoutHorizontally(newWidth, false, fnPositionY, true);
this.node.width = newWidth;
}
else if (this.type === Type.VERTICAL) {
var newHeight = this._getVerticalBaseHeight(this.node.children);
var fnPositionX = function (child) {
return child.x;
};
this._doLayoutVertically(newHeight, false, fnPositionX, true);
this.node.height = newHeight;
}
else if (this.type === Type.NONE) {
if (this.resizeMode === ResizeMode.CONTAINER) {
this._doLayoutBasic();
}
}
else if (this.type === Type.GRID) {
this._doLayoutGrid();
}
},
/**
* !#en Perform the layout update
* !#zh 立即执行更新布局
*
* @method updateLayout
*
* @example
* layout.type = cc.Layout.HORIZONTAL;
* layout.node.addChild(childNode);
* cc.log(childNode.x); // not yet changed
* layout.updateLayout();
* cc.log(childNode.x); // changed
*/
updateLayout: function () {
if (this._layoutDirty && this.node.children.length > 0) {
this._doLayout();
this._layoutDirty = false;
}
}
});
/**
* !#en The padding of layout, it effects the layout in four direction.
* !#zh 容器内边距,该属性会在四个布局方向上生效。
* @property {Number} padding
*/
Object.defineProperty(Layout.prototype, "padding", {
get: function () {
cc.warnID(4100);
return this.paddingLeft;
},
set: function (value) {
this._N$padding = value;
this._migratePaddingData();
this._doLayoutDirty();
}
});
cc.Layout = module.exports = Layout;