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
using UnityEngine;
 
namespace Flexalon
{
    /// <summary> A transform updater determines how an object
    /// gets from its current position to the computed layout position. </summary>
    public interface TransformUpdater
    {
        /// <summary> Called before the layout system starts updating any transforms.
        /// Use this to capture the transform position. </summary>
        /// <param name="node"> The node being updated. </param>
        void PreUpdate(FlexalonNode node);
 
        /// <summary> Called to update the position of the object. </summary>
        /// <param name="node"> The node being updated. </param>
        /// <param name="position"> The computed local position of the object. </param>
        bool UpdatePosition(FlexalonNode node, Vector3 position);
 
        /// <summary> Called to update the rotation of the object. </summary>
        /// <param name="node"> The node being updated. </param>
        /// <param name="rotation"> The computed local rotation of the object. </param>
        bool UpdateRotation(FlexalonNode node, Quaternion rotation);
 
        /// <summary> Called to update the scale of the object. </summary>
        /// <param name="node"> The node being updated. </param>
        /// <param name="scale"> The computed local scale of the object. </param>
        bool UpdateScale(FlexalonNode node, Vector3 scale);
 
        /// <summary> Called to update the rect of the object. </summary>
        /// <param name="node"> The node being updated. </param>
        /// <param name="rect"> The computed rect of the object. </param>
        bool UpdateRectSize(FlexalonNode node, Vector2 rect);
    }
 
    internal class DefaultTransformUpdater : TransformUpdater
    {
        private void RecordEdit(FlexalonNode node)
        {
#if UNITY_EDITOR
            if (Flexalon.RecordFrameChanges)
            {
                UnityEditor.Undo.RecordObject(node.GameObject.transform, "Flexalon transform change");
                UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(node.GameObject.transform);
            }
#endif
        }
 
        public void PreUpdate(FlexalonNode node)
        {
        }
 
        public bool UpdatePosition(FlexalonNode node, Vector3 position)
        {
            RecordEdit(node);
            node.GameObject.transform.localPosition = position;
            return true;
        }
 
        public bool UpdateRotation(FlexalonNode node, Quaternion rotation)
        {
            RecordEdit(node);
            node.GameObject.transform.localRotation = rotation;
            return true;
        }
 
        public bool UpdateScale(FlexalonNode node, Vector3 scale)
        {
            RecordEdit(node);
            node.GameObject.transform.localScale = scale;
            return true;
        }
 
        public bool UpdateRectSize(FlexalonNode node, Vector2 size)
        {
            RecordEdit(node);
            var rectTransform = node.GameObject.transform as RectTransform;
            rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size.x);
            rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size.y);
            return true;
        }
    }
}