miepzerino
2025-03-30 d2ab30e7a69bfe7efda63ae75812207377917bd3
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
using UnityEngine;
 
namespace Flexalon
{
    /// <summary>
    /// Base class for all layout componets. See [custom layout](/docs/customLayout) for details
    /// on how to extend this class. Assigns the Layout method to FlexalonNode and keeps the
    /// node's children up to date.
    /// </summary>
    [DisallowMultipleComponent, RequireComponent(typeof(FlexalonObject))]
    public abstract class LayoutBase : FlexalonComponent, Layout
    {
        /// <inheritdoc />
        protected override void DoOnEnable()
        {
            _node.DetachAllChildren();
            for (int i = 0; i < transform.childCount; i++)
            {
                _node.AddChild(Flexalon.GetOrCreateNode(transform.GetChild(i).gameObject));
            }
 
            Flexalon.GetOrCreate().PreUpdate += DetectChanges;
            _node.SetMethod(this);
        }
 
        /// <inheritdoc />
        protected override void DoOnDisable()
        {
            _node.SetMethod(null);
            var flexalon = Flexalon.Get();
            if (flexalon)
            {
                flexalon.PreUpdate -= DetectChanges;
            }
        }
 
        /// <inheritdoc />
        protected override void ResetProperties()
        {
            _node.DetachAllChildren();
        }
 
        // This function is complicated because it's working around two issues.
        // First, OnTransformChildrenChanged doesn't always run on 2019.4 due to a bug.
        // See https://issuetracker.unity3d.com/issues/ontransformchildrenchanged-doesnt-get-called-in-the-edit-mode-when-dragging-a-prefab-from-the-project-window-to-the-hierarchy
        // Second, we need to deal with undo/redo. The strategy here is to do nothing on undo/redo except fix
        // the node.Children list, since it isn't serialzed. To detect undo/redo, we check if the Parent or SiblingIndex
        // values change in the serialized FlexalonResult matches the transform children.
        private void DetectChanges()
        {
            // Check if any old children changed parents. They need to be marked dirty
            // since their size may change after leaving the layout.
            for (int i = 0; i < _node.Children.Count; i++)
            {
                var childNode = _node.Children[i];
                if (!childNode.GameObject)
                {
                    Flexalon.RecordFrameChanges = true;
                    childNode.Detach();
                    MarkDirty();
                }
                else if (childNode.GameObject.transform.parent != transform || childNode.IsDragging || childNode.SkipLayout || childNode.Constraint != null)
                {
                    i--;
                    childNode.Detach();
                    if (childNode.Result.Parent == transform)
                    {
#if UNITY_EDITOR
                        UnityEditor.Undo.RecordObject(childNode.Result, "Parent change");
                        UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(childNode.Result);
                        Flexalon.RecordFrameChanges = true;
#endif
                        childNode.Result.Parent = null;
                        childNode.Result.SiblingIndex = 0;
                        childNode.MarkDirty();
                        MarkDirty();
                    }
                }
            }
 
            // Check if we have any new or out of order children.
            int index = 0;
            for (int i = 0; i < transform.childCount; i++)
            {
                var child = transform.GetChild(i);
                var childNode = Flexalon.GetOrCreateNode(child.gameObject);
                if (childNode.IsDragging || childNode.SkipLayout || childNode.Constraint != null)
                {
                    continue;
                }
 
                _node.InsertChild(childNode, index);
                if (childNode.Result.Parent != transform || childNode.Result.SiblingIndex != index)
                {
#if UNITY_EDITOR
                    UnityEditor.Undo.RecordObject(childNode.Result, "Parent change");
                    UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(childNode.Result);
                    Flexalon.RecordFrameChanges = true;
#endif
                    childNode.Result.Parent = transform;
                    childNode.Result.SiblingIndex = index;
 
                    childNode.MarkDirty();
                    MarkDirty();
                }
 
                index++;
            }
        }
 
        protected override void Initialize()
        {
            base.Initialize();
 
            if (!gameObject.TryGetComponent<FlexalonObject>(out var obj))
            {
                obj = Flexalon.AddComponent<FlexalonObject>(gameObject);
            }
 
            if (!Flexalon.IsRootCanvas(gameObject))
            {
                if (obj.WidthType == SizeType.Component)
                {
                    obj.WidthType = SizeType.Layout;
                }
 
                if (obj.HeightType == SizeType.Component)
                {
                    obj.HeightType = SizeType.Layout;
                }
 
                if (obj.DepthType == SizeType.Component)
                {
                    obj.DepthType = SizeType.Layout;
                }
            }
        }
 
        /// <inheritdoc />
        public virtual Bounds Measure(FlexalonNode node, Vector3 size, Vector3 min, Vector3 max)
        {
            throw new System.NotImplementedException();
        }
 
        /// <summary> Helper to assign the fill and shrink size for all children. </summary>
        protected void SetChildrenFillShrinkSize(FlexalonNode node, Vector3 childSize, Vector3 layoutSize)
        {
            foreach (var child in node.Children)
            {
                child.SetShrinkFillSize(childSize, layoutSize);
            }
        }
 
        /// <inheritdoc />
        public virtual void Arrange(FlexalonNode node, Vector3 layoutSize) {}
    }
}