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
using UnityEngine;
 
namespace Flexalon
{
    /// <summary>
    /// The lerp animator constnatly performs a linear interpolation between
    /// the object's current position and its layout position. This is useful
    /// if the layout position is continuously changing.
    /// </summary>
    [AddComponentMenu("Flexalon/Flexalon Lerp Animator"), HelpURL("https://www.flexalon.com/docs/animators")]
    public class FlexalonLerpAnimator : MonoBehaviour, TransformUpdater
    {
        private FlexalonNode _node;
        private RectTransform _rectTransform;
 
        [SerializeField]
        private bool _animateInWorldSpace = true;
        /// <summary> Determines if the animation should be performed in world space. </summary>
        public bool AnimateInWorldSpace
        {
            get => _animateInWorldSpace;
            set { _animateInWorldSpace = value; }
        }
 
        [SerializeField]
        private float _interpolationSpeed = 5.0f;
        /// <summary> Amount the object should be interpolated towards the target at each frame.
        /// This value is multiplied by Time.deltaTime. </summary>
        public float InterpolationSpeed
        {
            get => _interpolationSpeed;
            set { _interpolationSpeed = value; }
        }
 
        [SerializeField]
        private bool _animatePosition = true;
        /// <summary> Determines if the position should be animated. </summary>
        public bool AnimatePosition
        {
            get => _animatePosition;
            set { _animatePosition = value; }
        }
 
        [SerializeField]
        private bool _animateRotation = true;
        /// <summary> Determines if the rotation should be animated. </summary>
        public bool AnimateRotation
        {
            get => _animateRotation;
            set { _animateRotation = value; }
        }
 
        [SerializeField]
        private bool _animateScale = true;
        /// <summary> Determines if the rotation should be animated. </summary>
        public bool AnimateScale
        {
            get => _animateScale;
            set { _animateScale = value; }
        }
 
        private Vector3 _fromPosition;
        private Quaternion _fromRotation;
        private Vector3 _fromScale;
 
        void OnEnable()
        {
            _node = Flexalon.GetOrCreateNode(gameObject);
            _node.SetTransformUpdater(this);
            _rectTransform = (transform is RectTransform) ? (RectTransform)transform : null;
        }
 
        void OnDisable()
        {
            _node?.SetTransformUpdater(null);
            _node = null;
        }
 
        /// <inheritdoc />
        public void PreUpdate(FlexalonNode node)
        {
            _fromPosition = transform.position;
            _fromRotation = transform.rotation;
            _fromScale = transform.lossyScale;
        }
 
        /// <inheritdoc />
        public bool UpdatePosition(FlexalonNode node, Vector3 position)
        {
            if (_animateInWorldSpace)
            {
                var worldPosition = transform.parent ? transform.parent.localToWorldMatrix.MultiplyPoint(position) : position;
                if (!_animatePosition || Vector3.Distance(_fromPosition, worldPosition) < 0.001f)
                {
                    transform.localPosition = position;
                    return true;
                }
                else
                {
                    transform.position = Vector3.Lerp(_fromPosition, worldPosition, _interpolationSpeed * Time.smoothDeltaTime);
                    return false;
                }
            }
            else
            {
                if (!_animatePosition || Vector3.Distance(transform.localPosition, position) < 0.001f)
                {
                    transform.localPosition = position;
                    return true;
                }
                else
                {
                    transform.localPosition = Vector3.Lerp(transform.localPosition, position, _interpolationSpeed * Time.smoothDeltaTime);
                    return false;
                }
            }
        }
 
        /// <inheritdoc />
        public bool UpdateRotation(FlexalonNode node, Quaternion rotation)
        {
            if (_animateInWorldSpace)
            {
                var worldRotation = transform.parent ? transform.parent.rotation * rotation : rotation;
                if (!_animateRotation || Mathf.Abs(Quaternion.Angle(_fromRotation, worldRotation)) < 0.001f)
                {
                    transform.localRotation = rotation;
                    return true;
                }
                else
                {
                    transform.rotation = Quaternion.Slerp(_fromRotation, worldRotation, _interpolationSpeed * Time.smoothDeltaTime);
                    return false;
                }
            }
            else
            {
                if (!_animateRotation || Mathf.Abs(Quaternion.Angle(transform.localRotation, rotation)) < 0.001f)
                {
                    transform.localRotation = rotation;
                    return true;
                }
                else
                {
                    transform.localRotation = Quaternion.Slerp(transform.localRotation, rotation, _interpolationSpeed * Time.smoothDeltaTime);
                    return false;
                }
            }
        }
 
        /// <inheritdoc />
        public bool UpdateScale(FlexalonNode node, Vector3 scale)
        {
            if (_animateInWorldSpace)
            {
                var worldScale = transform.parent ? Math.Mul(scale, transform.parent.lossyScale) : scale;
                if (!_animateScale || Vector3.Distance(_fromScale, worldScale) < 0.001f)
                {
                    transform.localScale = scale;
                    return true;
                }
                else
                {
                    var newWorldScale = Vector3.Lerp(_fromScale, worldScale, _interpolationSpeed * Time.smoothDeltaTime);
                    transform.localScale = transform.parent ? Math.Div(newWorldScale, transform.parent.lossyScale) : newWorldScale;
                    return false;
                }
            }
            else
            {
                if (!_animateScale || Vector3.Distance(transform.localScale, scale) < 0.001f)
                {
                    transform.localScale = scale;
                    return true;
                }
                else
                {
                    transform.localScale = Vector3.Lerp(transform.localScale, scale, _interpolationSpeed * Time.smoothDeltaTime);
                    return false;
                }
            }
        }
 
        /// <inheritdoc />
        public bool UpdateRectSize(FlexalonNode node, Vector2 size)
        {
            bool done = !_animateScale || Vector2.Distance(_rectTransform.sizeDelta, size) < 0.001f;
            var newSize = done ? size : Vector2.Lerp(_rectTransform.sizeDelta, size, _interpolationSpeed * Time.smoothDeltaTime);
            _rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, newSize.x);
            _rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, newSize.y);
            return done;
        }
    }
}