miepzerino
2025-03-29 ad79d9ca49274cc660fc2030a071b24314f0f210
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
using System;
using System.Collections.Generic;
using UnityEngine;
 
namespace Flexalon
{
    /// <summary>
    /// Use a grid layout to position children at fixed intervals.
    /// Objects are placed in cells in column-row-layer order.
    /// </summary>
    [AddComponentMenu("Flexalon/Flexalon Grid Layout"), HelpURL("https://www.flexalon.com/docs/gridLayout")]
    public class FlexalonGridLayout : LayoutBase
    {
        /// <summary> The type of cell to use on the column-row axes. </summary>
        public enum CellTypes
        {
            /// <summary> A rectangular cell. </summary>
            Rectangle,
 
            /// <summary> A hexagonal cell. </summary>
            Hexagonal
        }
 
        [SerializeField]
        private CellTypes _cellType = CellTypes.Rectangle;
        /// <summary> The type of cell to use on the column-row axes. </summary>
        public CellTypes CellType
        {
            get { return _cellType; }
            set { _cellType = value; _node.MarkDirty(); }
        }
 
        [SerializeField, Min(1)]
        private uint _columns = 3;
        /// <summary> The number of columns in the grid. </summary>
        public uint Columns
        {
            get { return _columns; }
            set { _columns = System.Math.Max(value, 1); _node.MarkDirty(); }
        }
 
        [SerializeField, Min(1)]
        private uint _rows = 3;
        /// <summary> The number of rows in the grid. </summary>
        public uint Rows
        {
            get { return _rows; }
            set { _rows = System.Math.Max(value, 1); _node.MarkDirty(); }
        }
 
        [SerializeField, Min(1)]
        private uint _layers = 1;
        /// <summary> The number of layers in the grid. </summary>
        public uint Layers
        {
            get { return _layers; }
            set { _layers = System.Math.Max(value, 1); _node.MarkDirty(); }
        }
 
        [SerializeField]
        private Direction _columnDirection = Direction.PositiveX;
        /// <summary> The direction of the column axis. </summary>
        public Direction ColumnDirection
        {
            get { return _columnDirection; }
            set { _columnDirection = value; _node.MarkDirty(); }
        }
 
        [SerializeField]
        private Direction _rowDirection = Direction.NegativeY;
        /// <summary> The direction of the row axis. </summary>
        public Direction RowDirection
        {
            get { return _rowDirection; }
            set { _rowDirection = value; _node.MarkDirty(); }
        }
 
        [SerializeField]
        private Direction _layerDirection = Direction.PositiveZ;
        /// <summary> The direction of the layer axis. </summary>
        public Direction LayerDirection
        {
            get { return _layerDirection; }
            set { _layerDirection = value; _node.MarkDirty(); }
        }
 
        /// <summary> How to determine the size of the cell. </summary>
        public enum CellSizeTypes
        {
            /// <summary> The object size is divided by the number of columns. </summary>
            Fill,
 
            /// <summary> The cell size is fixed. </summary>
            Fixed,
        }
 
        [SerializeField]
        private CellSizeTypes _columnSizeType = CellSizeTypes.Fill;
        /// <summary> How to determine the size of the columns, </summary>
        public CellSizeTypes ColumnSizeType
        {
            get { return _columnSizeType; }
            set { _columnSizeType = value; _node.MarkDirty(); }
        }
 
        [SerializeField, Min(0)]
        private float _columnSize = 1.0f;
        /// <summary> The fixed size of the columns. </summary>
        public float ColumnSize
        {
            get { return _columnSize; }
            set
            {
                _columnSize = Mathf.Max(0, value);
                _columnSizeType = CellSizeTypes.Fixed;
                _node.MarkDirty();
            }
        }
 
        [SerializeField]
        private CellSizeTypes _rowSizeType = CellSizeTypes.Fill;
        /// <summary> How to determine the size of the rows. </summary>
        public CellSizeTypes RowSizeType
        {
            get { return _rowSizeType; }
            set { _rowSizeType = value; _node.MarkDirty(); }
        }
 
        [SerializeField, Min(0)]
        private float _rowSize = 1.0f;
        /// <summary> The fixed size of the rows. </summary>
        public float RowSize
        {
            get { return _rowSize; }
            set
            {
                _rowSize = Mathf.Max(0, value);
                _rowSizeType = CellSizeTypes.Fixed;
                _node.MarkDirty();
            }
        }
 
        [SerializeField]
        private CellSizeTypes _layerSizeType = CellSizeTypes.Fill;
        /// <summary> How to determine the size of the layers. </summary>
        public CellSizeTypes LayerSizeType
        {
            get { return _layerSizeType; }
            set { _layerSizeType = value; _node.MarkDirty(); }
        }
 
        [SerializeField, Min(0)]
        private float _layerSize = 1.0f;
        /// <summary> The fixed size of the layers. </summary>
        public float LayerSizeSize
        {
            get { return _layerSize; }
            set
            {
                _layerSize = Mathf.Max(0, value);
                _layerSizeType = CellSizeTypes.Fixed;
                _node.MarkDirty();
            }
        }
 
        [SerializeField]
        private float _columnSpacing = 0;
        /// <summary> The spacing between columns. </summary>
        public float ColumnSpacing
        {
            get { return _columnSpacing; }
            set { _columnSpacing = value; _node.MarkDirty(); }
        }
 
        [SerializeField]
        private float _rowSpacing = 0;
        /// <summary> The spacing between rows. </summary>
        public float RowSpacing
        {
            get { return _rowSpacing; }
            set { _rowSpacing = value; _node.MarkDirty(); }
        }
 
        [SerializeField]
        private float _layerSpacing = 0;
        /// <summary> The spacing between layers. </summary>
        public float LayerSpacing
        {
            get { return _layerSpacing; }
            set { _layerSpacing = value; _node.MarkDirty(); }
        }
 
        [SerializeField]
        private Align _horizontalAlign = Align.Center;
        /// <summary> How to align each child in its cell horizontally. </summary>
        public Align HorizontalAlign
        {
            get { return _horizontalAlign; }
            set { _horizontalAlign = value; _node.MarkDirty(); }
        }
 
        [SerializeField]
        private Align _verticalAlign = Align.Center;
        /// <summary> How to align each child in its cell vertically. </summary>
        public Align VerticalAlign
        {
            get { return _verticalAlign; }
            set { _verticalAlign = value; _node.MarkDirty(); }
        }
 
        [SerializeField]
        private Align _depthAlign = Align.Center;
        /// <summary> How to align each child in its cell in depth. </summary>
        public Align DepthAlign
        {
            get { return _depthAlign; }
            set { _depthAlign = value; _node.MarkDirty(); }
        }
 
        [Serializable]
        private class TransformList
        {
            public List<Transform> Items;
        }
 
        [Serializable]
        private class CellDict : FlexalonDict<Vector3Int, TransformList> {}
 
        [SerializeField, HideInInspector]
        private CellDict _cellToChildren;
 
        private Dictionary<Transform, Vector3Int> _childToCell;
 
        /// <summary> Returns the first child in the cell. </summary>
        /// <param name="column"> The column of the cell. </param>
        /// <param name="row"> The row of the cell. </param>
        /// <param name="layer"> The layer of the cell. </param>
        /// <returns> The first child in the cell. </returns>
        public Transform GetChildAt(int column, int row, int layer = 0)
        {
            if (_cellToChildren != null)
            {
                if (_cellToChildren.TryGetValue(new Vector3Int(column, row, layer), out var children))
                {
                    return children.Items[0];
                }
            }
 
            return null;
        }
 
        /// <summary> Returns all children in the cell. </summary>
        /// <param name="column"> The column of the cell. </param>
        /// <param name="row"> The row of the cell. </param>
        /// <param name="layer"> The layer of the cell. </param>
        /// <returns> A list of children in the cell. </returns>
        public Transform[] GetChildrenAt(int column, int row, int layer = 0)
        {
            if (_cellToChildren != null)
            {
                if (_cellToChildren.TryGetValue(new Vector3Int(column, row, layer), out var children))
                {
                    return children.Items.ToArray();
                }
            }
 
            return new Transform[0];
        }
 
        private void SetCell(Vector3Int cell, Transform child)
        {
            if (!_cellToChildren.TryGetValue(cell, out var list))
            {
                list = new TransformList { Items = new List<Transform>() };
                _cellToChildren.Add(cell, list);
            }
 
            list.Items.Add(child);
            _childToCell.Add(child, cell);
        }
 
        private void UpdateCells()
        {
            if (_cellToChildren == null)
            {
                _cellToChildren = new CellDict();
            }
 
            if (_childToCell == null)
            {
                _childToCell = new Dictionary<Transform, Vector3Int>();
            }
 
            _cellToChildren.Clear();
            _childToCell.Clear();
 
            Vector3Int nextCell = Vector3Int.zero;
            foreach (var child in Node.Children)
            {
                var childTransform = child.GameObject.transform;
                if (child.GameObject.TryGetComponent<FlexalonGridCell>(out var cellComponent))
                {
                    SetCell(cellComponent.Cell, childTransform);
                }
                else
                {
                    SetCell(nextCell, childTransform);
                    nextCell[0]++;
                    if (nextCell[0] >= _columns)
                    {
                        nextCell[0] = 0;
                        nextCell[1]++;
                        if (nextCell[1] >= _rows)
                        {
                            nextCell[1] = 0;
                            nextCell[2]++;
                        }
                    }
                }
            }
        }
 
        private Vector3 GetCellSize(Vector3Int axes, Vector3 layoutSize)
        {
            var cellSize = layoutSize;
            cellSize[axes[0]] = GetColumnSize(layoutSize[axes[0]]);
            cellSize[axes[1]] = GetRowSize(layoutSize[axes[1]]);
            cellSize[axes[2]] = GetLayerSize(layoutSize[axes[2]]);
            return cellSize;
        }
 
        private Vector3 GetGridSize(Vector3Int axes, Vector3 cellSize)
        {
            int columnAxis = axes[0];
            int rowAxis = axes[1];
            int layerAxis = axes[2];
 
            Vector3 gridSize = Vector3.zero;
            if (_cellType == CellTypes.Hexagonal && _rows > 1)
            {
                gridSize[rowAxis] = cellSize[rowAxis] + (0.75f * cellSize[rowAxis] + _rowSpacing) * (_rows - 1);
            }
            else
            {
                gridSize[rowAxis] = cellSize[rowAxis] * _rows + (_rowSpacing * (_rows - 1));
            }
 
            gridSize[columnAxis] = cellSize[columnAxis] * _columns + (_columnSpacing * (_columns - 1));
            if (_cellType == CellTypes.Hexagonal && _rows > 1)
            {
                gridSize[columnAxis] += cellSize[columnAxis] * 0.5f;
            }
 
            gridSize[layerAxis] = cellSize[layerAxis] * _layers + (_layerSpacing * (_layers - 1));
            return gridSize;
        }
 
        private Vector3Int GetAxes()
        {
            var columnAxis = (int) Math.GetAxisFromDirection(_columnDirection);
            var rowAxis = (int) Math.GetAxisFromDirection(_rowDirection);
            var layerAxis = (int) Math.GetAxisFromDirection(_layerDirection);
 
            var otherAxes = Math.GetOtherAxes(columnAxis);
            if (columnAxis == rowAxis)
            {
                rowAxis = (layerAxis == otherAxes.Item1) ? otherAxes.Item2 : otherAxes.Item1;
            }
 
            if (columnAxis == layerAxis)
            {
                layerAxis = (rowAxis == otherAxes.Item1) ? otherAxes.Item2 : otherAxes.Item1;
            }
 
            if (rowAxis == layerAxis)
            {
                layerAxis = Math.GetThirdAxis(columnAxis, rowAxis);
            }
 
            return new Vector3Int(columnAxis, rowAxis, layerAxis);
        }
 
        private CellSizeTypes[] GetCellSizeTypes()
        {
            return new CellSizeTypes[3] {
                _columnSizeType,
                _rowSizeType,
                _layerSizeType
            };
        }
 
        /// <inheritdoc />
        public override Bounds Measure(FlexalonNode node, Vector3 size, Vector3 min, Vector3 max)
        {
            FlexalonLog.Log("GridMeasure | Size", node, size, min, max);
 
            var axes = GetAxes();
            var cellSize = GetCellSize(axes, size);
            var sizeTypes = GetCellSizeTypes();
 
            foreach (var child in node.Children)
            {
                var childSize = child.GetMeasureSize(size);
                for (int i = 0; i < 3; i++)
                {
                    if (node.GetSizeType(i) == SizeType.Layout && sizeTypes[i] == CellSizeTypes.Fill)
                    {
                        cellSize[i] = Mathf.Max(childSize[i], cellSize[i]);
                    }
                }
            }
 
            var minCellSize = GetCellSize(axes, min);
            var maxCellSize = GetCellSize(axes, max);
            cellSize = Math.Clamp(cellSize, minCellSize, maxCellSize);
 
            var gridSize = GetGridSize(axes, cellSize);
            for (int i = 0; i < 3; i++)
            {
                if (node.GetSizeType(i) == SizeType.Layout)
                {
                    size[i] = gridSize[i];
                }
            }
 
            SetChildrenFillShrinkSize(node, cellSize, size);
            return new Bounds(Vector3.zero, size);
        }
 
        private float GetRowSize(float availableRowSize)
        {
            if (_rowSizeType == CellSizeTypes.Fixed)
            {
                return _rowSize;
            }
 
            if (_cellType == CellTypes.Rectangle)
            {
                return (availableRowSize - _rowSpacing * (_rows - 1)) / _rows;
            }
            else
            {
                return (availableRowSize - _rowSpacing * (_rows - 1)) / (1 + (_rows - 1) * 0.75f);
            }
        }
 
        private float GetColumnSize(float availableColumnSize)
        {
            if (_columnSizeType == CellSizeTypes.Fixed)
            {
                return _columnSize;
            }
 
            if (_cellType == CellTypes.Rectangle)
            {
                return (availableColumnSize - _columnSpacing * (_columns - 1)) / _columns;
            }
            else
            {
                var sz = (availableColumnSize - _columnSpacing * (_columns - 1)) / _columns;
                if (_rows > 1)
                {
                    sz *= _columns / (_columns + 0.5f);
                }
 
                return sz;
            }
        }
 
        private float GetLayerSize(float availableColumnSize)
        {
            if (_layerSizeType == CellSizeTypes.Fixed)
            {
                return _layerSize;
            }
 
            return (availableColumnSize - _layerSpacing * (_layers - 1)) / _layers;
        }
 
        private Vector3 GetPosition(Vector3Int cell, Vector3Int axes, Vector3 cellSize, Vector3 gridSize)
        {
            var columnAxis = axes[0];
            var rowAxis = axes[1];
            var layerAxis = axes[2];
 
            var columnSize = cellSize[axes[0]];
            var rowSize = cellSize[axes[1]];
            var layerSize = cellSize[axes[2]];
 
            var position = -gridSize / 2;
 
            if (_cellType == CellTypes.Rectangle)
            {
                position[rowAxis] += rowSize * cell[1] + _rowSpacing * cell[1] + rowSize / 2;
                position[columnAxis] += columnSize * cell[0] + _columnSpacing * cell[0] + columnSize / 2;
            }
            else
            {
                bool rowEven = (cell[1] % 2) == 0;
                position[axes[1]] += rowSize * 0.75f * cell[1] + _rowSpacing * cell[1] + rowSize / 2;
                position[columnAxis] += columnSize * cell[0] + columnSize / 2 + _columnSpacing * cell[0] + (rowEven ? 0 : columnSize / 2);
            }
 
            position[layerAxis] += layerSize * cell[2] + _layerSpacing * cell[2] + layerSize / 2;
 
            position[rowAxis] *= Math.GetPositiveFromDirection(_rowDirection);
            position[columnAxis] *= Math.GetPositiveFromDirection(_columnDirection);
            position[layerAxis] *= Math.GetPositiveFromDirection(_layerDirection);
            return position;
        }
 
        private void PositionChild(FlexalonNode child, Vector3Int cell, Vector3Int axes, Vector3 cellSize, Vector3 gridSize)
        {
            Vector3 position;
            position = GetPosition(cell, axes, cellSize, gridSize);
            var aligned = Math.Align(child.GetArrangeSize(), cellSize, _horizontalAlign, _verticalAlign, _depthAlign);
            child.SetPositionResult(position + aligned);
        }
 
        /// <inheritdoc />
        public override void Arrange(FlexalonNode node, Vector3 layoutSize)
        {
            FlexalonLog.Log("GridArrange | LayoutSize", node, layoutSize);
 
            var axes = GetAxes();
            var cellSize = GetCellSize(axes, layoutSize);
            var gridSize = GetGridSize(axes, cellSize);
 
            UpdateCells();
 
            foreach (var child in node.Children)
            {
                if (_childToCell.TryGetValue(child.GameObject.transform, out var cell))
                {
                    PositionChild(child, cell, axes, cellSize, gridSize);
                }
            }
        }
 
        void OnDrawGizmosSelected()
        {
            if (_node != null)
            {
                var axes = GetAxes();
                var sz = _node.Result.AdapterBounds.size - _node.Padding.Size;
                var cellSize = GetCellSize(axes, sz);
                var gridSize = GetGridSize(axes, cellSize);
 
                Gizmos.color = new Color(1, 1, 0, 0.5f);
                var scale = _node.GetWorldBoxScale(true);
                Gizmos.matrix = Matrix4x4.TRS(_node.GetWorldBoxPosition(scale, true), transform.rotation, scale);
 
                for (int r = 0; r < _rows; r++)
                {
                    for (int c = 0; c < _columns; c++)
                    {
                        for (int l = 0; l < _layers; l++)
                        {
                            var position = GetPosition(new Vector3Int(c, r, l), axes, cellSize, gridSize);
                            if (_cellType == CellTypes.Rectangle)
                            {
                                DrawRectangle(position, axes, cellSize);
                            }
                            else
                            {
                                DrawHexagon(position, axes, cellSize);
                            }
                        }
                    }
                }
            }
        }
 
        void DrawRectangle(Vector3 position, Vector3Int axes, Vector3 cellSize)
        {
            var columnAxis = axes[0];
            var rowAxis = axes[1];
 
            var columnSize = cellSize[axes[0]];
            var rowSize = cellSize[axes[1]];
 
            var p1 = new Vector3(); // top right
            p1[rowAxis] = rowSize / 2;
            p1[columnAxis] = columnSize / 2;
 
            var p2 = new Vector3(); // bottom right
            p2[rowAxis] = -rowSize / 2;
            p2[columnAxis] = columnSize / 2;
 
            var p3 = new Vector3(); // bottom left
            p3[rowAxis] = -rowSize / 2;
            p3[columnAxis] = -columnSize / 2;
 
            var p4 = new Vector3(); // top left
            p4[rowAxis] = rowSize / 2;
            p4[columnAxis] = -columnSize / 2;
 
            Gizmos.DrawLine(position + p1, position + p2);
            Gizmos.DrawLine(position + p2, position + p3);
            Gizmos.DrawLine(position + p3, position + p4);
            Gizmos.DrawLine(position + p4, position + p1);
        }
 
        void DrawHexagon(Vector3 position, Vector3Int axes, Vector3 cellSize)
        {
            var columnAxis = axes[0];
            var rowAxis = axes[1];
 
            var columnSize = cellSize[axes[0]];
            var rowSize = cellSize[axes[1]];
 
            var p1 = new Vector3(); // top
            p1[rowAxis] = rowSize / 2;
 
            var p2 = new Vector3(); // top right
            p2[rowAxis] = rowSize / 4;
            p2[columnAxis] = columnSize / 2;
 
            var p3 = new Vector3(); // bottom right
            p3[rowAxis] = -rowSize / 4;
            p3[columnAxis] = columnSize / 2;
 
            var p4 = new Vector3(); // bottom
            p4[rowAxis] = -rowSize / 2;
 
            var p5 = new Vector3(); // bottom left
            p5[rowAxis] = -rowSize / 4;
            p5[columnAxis] = -columnSize / 2;
 
            var p6 = new Vector3(); // top left
            p6[rowAxis] = rowSize / 4;
            p6[columnAxis] = -columnSize / 2;
 
            Gizmos.DrawLine(position + p1, position + p2);
            Gizmos.DrawLine(position + p2, position + p3);
            Gizmos.DrawLine(position + p3, position + p4);
            Gizmos.DrawLine(position + p4, position + p5);
            Gizmos.DrawLine(position + p5, position + p6);
            Gizmos.DrawLine(position + p6, position + p1);
        }
    }
}