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
using UnityEngine;
 
namespace Flexalon
{
    /// <summary>
    /// The curve animator applies a curve the the position, rotation, and scale
    /// of the object. The curve is restarted each time the layout position changes.
    /// This is ideal for scenarios in which the layout position does not change often.
    /// </summary>
    [AddComponentMenu("Flexalon/Flexalon Curve Animator"), HelpURL("https://www.flexalon.com/docs/animators")]
    public class FlexalonCurveAnimator : MonoBehaviour, TransformUpdater
    {
        private FlexalonNode _node;
        private RectTransform _rectTransform;
 
        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 AnimationCurve _curve = AnimationCurve.Linear(0, 0, 1, 1);
        /// <summary> The curve to apply. Should begin at 0 and end at 1. </summary>
        public AnimationCurve Curve
        {
            get => _curve;
            set { _curve = 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 scale should be animated. </summary>
        public bool AnimateScale
        {
            get => _animateScale;
            set { _animateScale = value; }
        }
 
        private Vector3 _startPosition;
        private Quaternion _startRotation;
        private Vector3 _startScale;
        private Vector2 _startRectSize;
 
        private Vector3 _endPosition;
        private Quaternion _endRotation;
        private Vector3 _endScale;
        private Vector2 _endRectSize;
 
        private float _positionTime;
        private float _rotationTime;
        private float _scaleTime;
        private float _rectSizeTime;
 
        private Vector3 _fromPosition;
        private Quaternion _fromRotation;
        private Vector3 _fromScale;
        private Vector2 _fromRectSize;
 
        void OnEnable()
        {
            _startPosition = _endPosition = new Vector3(float.NaN, float.NaN, float.NaN);
            _startRotation = _endRotation = new Quaternion(float.NaN, float.NaN, float.NaN, float.NaN);
            _startScale = _endScale = new Vector3(float.NaN, float.NaN, float.NaN);
            _positionTime = _rotationTime = _scaleTime = 0;
            _rectTransform = (transform is RectTransform) ? (RectTransform)transform : null;
 
            _node = Flexalon.GetOrCreateNode(gameObject);
            _node.SetTransformUpdater(this);
        }
 
        void OnDisable()
        {
            _node?.SetTransformUpdater(null);
            _node = null;
        }
 
        /// <inheritdoc />
        public void PreUpdate(FlexalonNode node)
        {
            _fromPosition = transform.position;
            _fromRotation = transform.rotation;
            _fromScale = transform.lossyScale;
            _fromRectSize = _rectTransform?.rect.size ?? Vector2.zero;
        }
 
        /// <inheritdoc />
        public bool UpdatePosition(FlexalonNode node, Vector3 position)
        {
            var newEndPosition = position;
            var newStartPosition = transform.localPosition;
 
            if (_animateInWorldSpace)
            {
                newEndPosition = transform.parent ? transform.parent.localToWorldMatrix.MultiplyPoint(position) : position;;
                newStartPosition = _fromPosition;
            }
 
            if (newEndPosition != _endPosition)
            {
                _startPosition = newStartPosition;
                _endPosition = newEndPosition;
                _positionTime = 0;
            }
 
            _positionTime += Time.smoothDeltaTime;
 
            if (!_animatePosition || _positionTime > _curve.keys[_curve.keys.Length - 1].time)
            {
                transform.localPosition = position;
                _endPosition = new Vector3(float.NaN, float.NaN, float.NaN);
                return true;
            }
            else
            {
                var newPosition = Vector3.Lerp(_startPosition, _endPosition, _curve.Evaluate(_positionTime));
                if (_animateInWorldSpace)
                {
                    transform.position = newPosition;
                }
                else
                {
                    transform.localPosition = newPosition;
                }
 
                return false;
            }
        }
 
        /// <inheritdoc />
        public bool UpdateRotation(FlexalonNode node, Quaternion rotation)
        {
            var newEndRotation = rotation;
            var newStartRotation = transform.localRotation;
 
            if (_animateInWorldSpace)
            {
                newEndRotation = transform.parent ? transform.parent.rotation * rotation : rotation;;
                newStartRotation = _fromRotation;
            }
 
            if (newEndRotation != _endRotation)
            {
                _startRotation = newStartRotation;
                _endRotation = newEndRotation;
                _rotationTime = 0;
            }
 
            _rotationTime += Time.smoothDeltaTime;
 
            if (!_animateRotation || _rotationTime > _curve.keys[_curve.keys.Length - 1].time)
            {
                transform.localRotation = rotation;
                _endRotation = new Quaternion(float.NaN, float.NaN, float.NaN, float.NaN);
                return true;
            }
            else
            {
                var newRotation = Quaternion.Slerp(_startRotation, _endRotation, _curve.Evaluate(_rotationTime));
                if (_animateInWorldSpace)
                {
                    transform.rotation = newRotation;
                }
                else
                {
                    transform.localRotation = newRotation;
                }
 
                return false;
            }
        }
 
        /// <inheritdoc />
        public bool UpdateScale(FlexalonNode node, Vector3 scale)
        {
            var newEndScale = scale;
            var newStartScale = transform.localScale;
 
            if (_animateInWorldSpace)
            {
                newEndScale = transform.parent ? Math.Mul(scale, transform.parent.lossyScale) : scale;
                newStartScale = _fromScale;
            }
 
            if (newEndScale != _endScale)
            {
                _startScale = newStartScale;
                _endScale = newEndScale;
                _scaleTime = 0;
            }
 
            _scaleTime += Time.smoothDeltaTime;
 
            if (!_animateScale || _scaleTime > _curve.keys[_curve.keys.Length - 1].time)
            {
                transform.localScale = scale;
                _endScale = new Vector3(float.NaN, float.NaN, float.NaN);
                return true;
            }
            else
            {
                var newScale = Vector3.Lerp(_startScale, _endScale, _curve.Evaluate(_scaleTime));
 
                if (_animateInWorldSpace)
                {
                    transform.localScale = transform.parent ? Math.Div(newScale, transform.parent.lossyScale) : newScale;
                }
                else
                {
                    transform.localScale = newScale;
                }
 
                return false;
            }
        }
 
        /// <inheritdoc />
        public bool UpdateRectSize(FlexalonNode node, Vector2 size)
        {
            if (size != _endRectSize)
            {
                _startRectSize = _fromRectSize;
                _endRectSize = size;
                _rectSizeTime = 0;
            }
 
            _rectSizeTime += Time.smoothDeltaTime;
            bool done = !_animateScale || _rectSizeTime > _curve.keys[_curve.keys.Length - 1].time;
            var newSize = done ? size : Vector2.Lerp(_startRectSize, _endRectSize, _curve.Evaluate(_rectSizeTime));
            _rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, newSize.x);
            _rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, newSize.y);
 
            if (done)
            {
                _endRectSize = new Vector2(float.NaN, float.NaN);
            }
 
            return done;
        }
    }
}